]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.3.43: Fixed spinlock hang.
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
92c13f29 4 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e
JB
20 *******************************************************************/
21
dea3101e
JB
22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
5a0e3ad6 26#include <linux/slab.h>
dea3101e 27
91886523 28#include <scsi/scsi.h>
dea3101e
JB
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
f888ba3c 32#include <scsi/scsi_transport_fc.h>
da0436e9 33#include <scsi/fc/fc_fs.h>
0d878419 34#include <linux/aer.h>
dea3101e 35
da0436e9 36#include "lpfc_hw4.h"
dea3101e
JB
37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e
JB
41#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"
858c9f6c 47#include "lpfc_debugfs.h"
04c68496 48#include "lpfc_vport.h"
dea3101e
JB
49
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
4f774513
JS
58
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 *,
45ed1190
JS
63 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
6669f9bb
JS
66static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
0558056c
JS
68static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
8a9d2e80
JS
70static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
ba20c853
JS
72static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
e8d3c3b1
JS
74static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
75static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
0558056c 76
4f774513
JS
77static IOCB_t *
78lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
79{
80 return &iocbq->iocb;
81}
82
83/**
84 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
85 * @q: The Work Queue to operate on.
86 * @wqe: The work Queue Entry to put on the Work queue.
87 *
88 * This routine will copy the contents of @wqe to the next available entry on
89 * the @q. This function will then ring the Work Queue Doorbell to signal the
90 * HBA to start processing the Work Queue Entry. This function returns 0 if
91 * successful. If no entries are available on @q then this function will return
92 * -ENOMEM.
93 * The caller is expected to hold the hbalock when calling this routine.
94 **/
95static uint32_t
96lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
97{
2e90f4b5 98 union lpfc_wqe *temp_wqe;
4f774513
JS
99 struct lpfc_register doorbell;
100 uint32_t host_index;
027140ea 101 uint32_t idx;
4f774513 102
2e90f4b5
JS
103 /* sanity check on queue memory */
104 if (unlikely(!q))
105 return -ENOMEM;
106 temp_wqe = q->qe[q->host_index].wqe;
107
4f774513 108 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
109 idx = ((q->host_index + 1) % q->entry_count);
110 if (idx == q->hba_index) {
b84daac9 111 q->WQ_overflow++;
4f774513 112 return -ENOMEM;
b84daac9
JS
113 }
114 q->WQ_posted++;
4f774513 115 /* set consumption flag every once in a while */
ff78d8f9 116 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 117 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
118 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
119 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513
JS
120 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
121
122 /* Update the host index before invoking device */
123 host_index = q->host_index;
027140ea
JS
124
125 q->host_index = idx;
4f774513
JS
126
127 /* Ring Doorbell */
128 doorbell.word0 = 0;
962bc51b
JS
129 if (q->db_format == LPFC_DB_LIST_FORMAT) {
130 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
131 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
132 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
133 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
134 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
135 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
136 } else {
137 return -EINVAL;
138 }
139 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
140
141 return 0;
142}
143
144/**
145 * lpfc_sli4_wq_release - Updates internal hba index for WQ
146 * @q: The Work Queue to operate on.
147 * @index: The index to advance the hba index to.
148 *
149 * This routine will update the HBA index of a queue to reflect consumption of
150 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
151 * an entry the host calls this function to update the queue's internal
152 * pointers. This routine returns the number of entries that were consumed by
153 * the HBA.
154 **/
155static uint32_t
156lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
157{
158 uint32_t released = 0;
159
2e90f4b5
JS
160 /* sanity check on queue memory */
161 if (unlikely(!q))
162 return 0;
163
4f774513
JS
164 if (q->hba_index == index)
165 return 0;
166 do {
167 q->hba_index = ((q->hba_index + 1) % q->entry_count);
168 released++;
169 } while (q->hba_index != index);
170 return released;
171}
172
173/**
174 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
175 * @q: The Mailbox Queue to operate on.
176 * @wqe: The Mailbox Queue Entry to put on the Work queue.
177 *
178 * This routine will copy the contents of @mqe to the next available entry on
179 * the @q. This function will then ring the Work Queue Doorbell to signal the
180 * HBA to start processing the Work Queue Entry. This function returns 0 if
181 * successful. If no entries are available on @q then this function will return
182 * -ENOMEM.
183 * The caller is expected to hold the hbalock when calling this routine.
184 **/
185static uint32_t
186lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
187{
2e90f4b5 188 struct lpfc_mqe *temp_mqe;
4f774513
JS
189 struct lpfc_register doorbell;
190 uint32_t host_index;
191
2e90f4b5
JS
192 /* sanity check on queue memory */
193 if (unlikely(!q))
194 return -ENOMEM;
195 temp_mqe = q->qe[q->host_index].mqe;
196
4f774513
JS
197 /* If the host has not yet processed the next entry then we are done */
198 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
199 return -ENOMEM;
200 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
201 /* Save off the mailbox pointer for completion */
202 q->phba->mbox = (MAILBOX_t *)temp_mqe;
203
204 /* Update the host index before invoking device */
205 host_index = q->host_index;
206 q->host_index = ((q->host_index + 1) % q->entry_count);
207
208 /* Ring Doorbell */
209 doorbell.word0 = 0;
210 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
211 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
212 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
213 return 0;
214}
215
216/**
217 * lpfc_sli4_mq_release - Updates internal hba index for MQ
218 * @q: The Mailbox Queue to operate on.
219 *
220 * This routine will update the HBA index of a queue to reflect consumption of
221 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
222 * an entry the host calls this function to update the queue's internal
223 * pointers. This routine returns the number of entries that were consumed by
224 * the HBA.
225 **/
226static uint32_t
227lpfc_sli4_mq_release(struct lpfc_queue *q)
228{
2e90f4b5
JS
229 /* sanity check on queue memory */
230 if (unlikely(!q))
231 return 0;
232
4f774513
JS
233 /* Clear the mailbox pointer for completion */
234 q->phba->mbox = NULL;
235 q->hba_index = ((q->hba_index + 1) % q->entry_count);
236 return 1;
237}
238
239/**
240 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
241 * @q: The Event Queue to get the first valid EQE from
242 *
243 * This routine will get the first valid Event Queue Entry from @q, update
244 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
245 * the Queue (no more work to do), or the Queue is full of EQEs that have been
246 * processed, but not popped back to the HBA then this routine will return NULL.
247 **/
248static struct lpfc_eqe *
249lpfc_sli4_eq_get(struct lpfc_queue *q)
250{
2e90f4b5 251 struct lpfc_eqe *eqe;
027140ea 252 uint32_t idx;
2e90f4b5
JS
253
254 /* sanity check on queue memory */
255 if (unlikely(!q))
256 return NULL;
257 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
258
259 /* If the next EQE is not valid then we are done */
cb5172ea 260 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
261 return NULL;
262 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
263 idx = ((q->hba_index + 1) % q->entry_count);
264 if (idx == q->host_index)
4f774513
JS
265 return NULL;
266
027140ea 267 q->hba_index = idx;
4f774513
JS
268 return eqe;
269}
270
ba20c853
JS
271/**
272 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
273 * @q: The Event Queue to disable interrupts
274 *
275 **/
276static inline void
277lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
278{
279 struct lpfc_register doorbell;
280
281 doorbell.word0 = 0;
282 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
283 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
284 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
285 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
286 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
287 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
288}
289
4f774513
JS
290/**
291 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
292 * @q: The Event Queue that the host has completed processing for.
293 * @arm: Indicates whether the host wants to arms this CQ.
294 *
295 * This routine will mark all Event Queue Entries on @q, from the last
296 * known completed entry to the last entry that was processed, as completed
297 * by clearing the valid bit for each completion queue entry. Then it will
298 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
299 * The internal host index in the @q will be updated by this routine to indicate
300 * that the host has finished processing the entries. The @arm parameter
301 * indicates that the queue should be rearmed when ringing the doorbell.
302 *
303 * This function will return the number of EQEs that were popped.
304 **/
305uint32_t
306lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
307{
308 uint32_t released = 0;
309 struct lpfc_eqe *temp_eqe;
310 struct lpfc_register doorbell;
311
2e90f4b5
JS
312 /* sanity check on queue memory */
313 if (unlikely(!q))
314 return 0;
315
4f774513
JS
316 /* while there are valid entries */
317 while (q->hba_index != q->host_index) {
318 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 319 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
320 released++;
321 q->host_index = ((q->host_index + 1) % q->entry_count);
322 }
323 if (unlikely(released == 0 && !arm))
324 return 0;
325
326 /* ring doorbell for number popped */
327 doorbell.word0 = 0;
328 if (arm) {
329 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
330 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
331 }
332 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
333 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
334 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
335 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
336 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 337 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
338 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
339 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
340 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
341 return released;
342}
343
344/**
345 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
346 * @q: The Completion Queue to get the first valid CQE from
347 *
348 * This routine will get the first valid Completion Queue Entry from @q, update
349 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
350 * the Queue (no more work to do), or the Queue is full of CQEs that have been
351 * processed, but not popped back to the HBA then this routine will return NULL.
352 **/
353static struct lpfc_cqe *
354lpfc_sli4_cq_get(struct lpfc_queue *q)
355{
356 struct lpfc_cqe *cqe;
027140ea 357 uint32_t idx;
4f774513 358
2e90f4b5
JS
359 /* sanity check on queue memory */
360 if (unlikely(!q))
361 return NULL;
362
4f774513 363 /* If the next CQE is not valid then we are done */
cb5172ea 364 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
365 return NULL;
366 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
367 idx = ((q->hba_index + 1) % q->entry_count);
368 if (idx == q->host_index)
4f774513
JS
369 return NULL;
370
371 cqe = q->qe[q->hba_index].cqe;
027140ea 372 q->hba_index = idx;
4f774513
JS
373 return cqe;
374}
375
376/**
377 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
378 * @q: The Completion Queue that the host has completed processing for.
379 * @arm: Indicates whether the host wants to arms this CQ.
380 *
381 * This routine will mark all Completion queue entries on @q, from the last
382 * known completed entry to the last entry that was processed, as completed
383 * by clearing the valid bit for each completion queue entry. Then it will
384 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
385 * The internal host index in the @q will be updated by this routine to indicate
386 * that the host has finished processing the entries. The @arm parameter
387 * indicates that the queue should be rearmed when ringing the doorbell.
388 *
389 * This function will return the number of CQEs that were released.
390 **/
391uint32_t
392lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
393{
394 uint32_t released = 0;
395 struct lpfc_cqe *temp_qe;
396 struct lpfc_register doorbell;
397
2e90f4b5
JS
398 /* sanity check on queue memory */
399 if (unlikely(!q))
400 return 0;
4f774513
JS
401 /* while there are valid entries */
402 while (q->hba_index != q->host_index) {
403 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 404 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
405 released++;
406 q->host_index = ((q->host_index + 1) % q->entry_count);
407 }
408 if (unlikely(released == 0 && !arm))
409 return 0;
410
411 /* ring doorbell for number popped */
412 doorbell.word0 = 0;
413 if (arm)
414 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
415 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
416 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
417 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
418 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
419 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
420 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
421 return released;
422}
423
424/**
425 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
426 * @q: The Header Receive Queue to operate on.
427 * @wqe: The Receive Queue Entry to put on the Receive queue.
428 *
429 * This routine will copy the contents of @wqe to the next available entry on
430 * the @q. This function will then ring the Receive Queue Doorbell to signal the
431 * HBA to start processing the Receive Queue Entry. This function returns the
432 * index that the rqe was copied to if successful. If no entries are available
433 * on @q then this function will return -ENOMEM.
434 * The caller is expected to hold the hbalock when calling this routine.
435 **/
436static int
437lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
438 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
439{
2e90f4b5
JS
440 struct lpfc_rqe *temp_hrqe;
441 struct lpfc_rqe *temp_drqe;
4f774513 442 struct lpfc_register doorbell;
5a25bf36 443 int put_index;
4f774513 444
2e90f4b5
JS
445 /* sanity check on queue memory */
446 if (unlikely(!hq) || unlikely(!dq))
447 return -ENOMEM;
5a25bf36 448 put_index = hq->host_index;
2e90f4b5
JS
449 temp_hrqe = hq->qe[hq->host_index].rqe;
450 temp_drqe = dq->qe[dq->host_index].rqe;
451
4f774513
JS
452 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
453 return -EINVAL;
454 if (hq->host_index != dq->host_index)
455 return -EINVAL;
456 /* If the host has not yet processed the next entry then we are done */
457 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
458 return -EBUSY;
459 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
460 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
461
462 /* Update the host index to point to the next slot */
463 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
464 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
465
466 /* Ring The Header Receive Queue Doorbell */
73d91e50 467 if (!(hq->host_index % hq->entry_repost)) {
4f774513 468 doorbell.word0 = 0;
962bc51b
JS
469 if (hq->db_format == LPFC_DB_RING_FORMAT) {
470 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
471 hq->entry_repost);
472 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
473 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
474 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
475 hq->entry_repost);
476 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
477 hq->host_index);
478 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
479 } else {
480 return -EINVAL;
481 }
482 writel(doorbell.word0, hq->db_regaddr);
4f774513
JS
483 }
484 return put_index;
485}
486
487/**
488 * lpfc_sli4_rq_release - Updates internal hba index for RQ
489 * @q: The Header Receive Queue to operate on.
490 *
491 * This routine will update the HBA index of a queue to reflect consumption of
492 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
493 * consumed an entry the host calls this function to update the queue's
494 * internal pointers. This routine returns the number of entries that were
495 * consumed by the HBA.
496 **/
497static uint32_t
498lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
499{
2e90f4b5
JS
500 /* sanity check on queue memory */
501 if (unlikely(!hq) || unlikely(!dq))
502 return 0;
503
4f774513
JS
504 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
505 return 0;
506 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
507 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
508 return 1;
509}
510
e59058c4 511/**
3621a710 512 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
513 * @phba: Pointer to HBA context object.
514 * @pring: Pointer to driver SLI ring object.
515 *
516 * This function returns pointer to next command iocb entry
517 * in the command ring. The caller must hold hbalock to prevent
518 * other threads consume the next command iocb.
519 * SLI-2/SLI-3 provide different sized iocbs.
520 **/
ed957684
JS
521static inline IOCB_t *
522lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
523{
7e56aa25
JS
524 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
525 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
526}
527
e59058c4 528/**
3621a710 529 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
530 * @phba: Pointer to HBA context object.
531 * @pring: Pointer to driver SLI ring object.
532 *
533 * This function returns pointer to next response iocb entry
534 * in the response ring. The caller must hold hbalock to make sure
535 * that no other thread consume the next response iocb.
536 * SLI-2/SLI-3 provide different sized iocbs.
537 **/
ed957684
JS
538static inline IOCB_t *
539lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
540{
7e56aa25
JS
541 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
542 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
543}
544
e59058c4 545/**
3621a710 546 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
547 * @phba: Pointer to HBA context object.
548 *
549 * This function is called with hbalock held. This function
550 * allocates a new driver iocb object from the iocb pool. If the
551 * allocation is successful, it returns pointer to the newly
552 * allocated iocb object else it returns NULL.
553 **/
4f2e66c6 554struct lpfc_iocbq *
2e0fef85 555__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
556{
557 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
558 struct lpfc_iocbq * iocbq = NULL;
559
560 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
561 if (iocbq)
562 phba->iocb_cnt++;
563 if (phba->iocb_cnt > phba->iocb_max)
564 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
565 return iocbq;
566}
567
da0436e9
JS
568/**
569 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
570 * @phba: Pointer to HBA context object.
571 * @xritag: XRI value.
572 *
573 * This function clears the sglq pointer from the array of acive
574 * sglq's. The xritag that is passed in is used to index into the
575 * array. Before the xritag can be used it needs to be adjusted
576 * by subtracting the xribase.
577 *
578 * Returns sglq ponter = success, NULL = Failure.
579 **/
580static struct lpfc_sglq *
581__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
582{
da0436e9 583 struct lpfc_sglq *sglq;
6d368e53
JS
584
585 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
586 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
587 return sglq;
588}
589
590/**
591 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
592 * @phba: Pointer to HBA context object.
593 * @xritag: XRI value.
594 *
595 * This function returns the sglq pointer from the array of acive
596 * sglq's. The xritag that is passed in is used to index into the
597 * array. Before the xritag can be used it needs to be adjusted
598 * by subtracting the xribase.
599 *
600 * Returns sglq ponter = success, NULL = Failure.
601 **/
0f65ff68 602struct lpfc_sglq *
da0436e9
JS
603__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
604{
da0436e9 605 struct lpfc_sglq *sglq;
6d368e53
JS
606
607 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
608 return sglq;
609}
610
19ca7609 611/**
1151e3ec 612 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
613 * @phba: Pointer to HBA context object.
614 * @xritag: xri used in this exchange.
615 * @rrq: The RRQ to be cleared.
616 *
19ca7609 617 **/
1151e3ec
JS
618void
619lpfc_clr_rrq_active(struct lpfc_hba *phba,
620 uint16_t xritag,
621 struct lpfc_node_rrq *rrq)
19ca7609 622{
1151e3ec 623 struct lpfc_nodelist *ndlp = NULL;
19ca7609 624
1151e3ec
JS
625 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
626 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
627
628 /* The target DID could have been swapped (cable swap)
629 * we should use the ndlp from the findnode if it is
630 * available.
631 */
1151e3ec 632 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
633 ndlp = rrq->ndlp;
634
1151e3ec
JS
635 if (!ndlp)
636 goto out;
637
6d368e53 638 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
19ca7609
JS
639 rrq->send_rrq = 0;
640 rrq->xritag = 0;
641 rrq->rrq_stop_time = 0;
642 }
1151e3ec 643out:
19ca7609
JS
644 mempool_free(rrq, phba->rrq_pool);
645}
646
647/**
648 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
649 * @phba: Pointer to HBA context object.
650 *
651 * This function is called with hbalock held. This function
652 * Checks if stop_time (ratov from setting rrq active) has
653 * been reached, if it has and the send_rrq flag is set then
654 * it will call lpfc_send_rrq. If the send_rrq flag is not set
655 * then it will just call the routine to clear the rrq and
656 * free the rrq resource.
657 * The timer is set to the next rrq that is going to expire before
658 * leaving the routine.
659 *
660 **/
661void
662lpfc_handle_rrq_active(struct lpfc_hba *phba)
663{
664 struct lpfc_node_rrq *rrq;
665 struct lpfc_node_rrq *nextrrq;
666 unsigned long next_time;
667 unsigned long iflags;
1151e3ec 668 LIST_HEAD(send_rrq);
19ca7609
JS
669
670 spin_lock_irqsave(&phba->hbalock, iflags);
671 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 672 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 673 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
674 &phba->active_rrq_list, list) {
675 if (time_after(jiffies, rrq->rrq_stop_time))
676 list_move(&rrq->list, &send_rrq);
677 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
678 next_time = rrq->rrq_stop_time;
679 }
680 spin_unlock_irqrestore(&phba->hbalock, iflags);
681 if (!list_empty(&phba->active_rrq_list))
682 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
683 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
684 list_del(&rrq->list);
685 if (!rrq->send_rrq)
686 /* this call will free the rrq */
687 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
688 else if (lpfc_send_rrq(phba, rrq)) {
689 /* if we send the rrq then the completion handler
690 * will clear the bit in the xribitmap.
691 */
692 lpfc_clr_rrq_active(phba, rrq->xritag,
693 rrq);
694 }
695 }
19ca7609
JS
696}
697
698/**
699 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
700 * @vport: Pointer to vport context object.
701 * @xri: The xri used in the exchange.
702 * @did: The targets DID for this exchange.
703 *
704 * returns NULL = rrq not found in the phba->active_rrq_list.
705 * rrq = rrq for this xri and target.
706 **/
707struct lpfc_node_rrq *
708lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
709{
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_node_rrq *rrq;
712 struct lpfc_node_rrq *nextrrq;
713 unsigned long iflags;
714
715 if (phba->sli_rev != LPFC_SLI_REV4)
716 return NULL;
717 spin_lock_irqsave(&phba->hbalock, iflags);
718 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
719 if (rrq->vport == vport && rrq->xritag == xri &&
720 rrq->nlp_DID == did){
721 list_del(&rrq->list);
722 spin_unlock_irqrestore(&phba->hbalock, iflags);
723 return rrq;
724 }
725 }
726 spin_unlock_irqrestore(&phba->hbalock, iflags);
727 return NULL;
728}
729
730/**
731 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
732 * @vport: Pointer to vport context object.
1151e3ec
JS
733 * @ndlp: Pointer to the lpfc_node_list structure.
734 * If ndlp is NULL Remove all active RRQs for this vport from the
735 * phba->active_rrq_list and clear the rrq.
736 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
737 **/
738void
1151e3ec 739lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
740
741{
742 struct lpfc_hba *phba = vport->phba;
743 struct lpfc_node_rrq *rrq;
744 struct lpfc_node_rrq *nextrrq;
745 unsigned long iflags;
1151e3ec 746 LIST_HEAD(rrq_list);
19ca7609
JS
747
748 if (phba->sli_rev != LPFC_SLI_REV4)
749 return;
1151e3ec
JS
750 if (!ndlp) {
751 lpfc_sli4_vport_delete_els_xri_aborted(vport);
752 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 753 }
1151e3ec
JS
754 spin_lock_irqsave(&phba->hbalock, iflags);
755 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
756 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
757 list_move(&rrq->list, &rrq_list);
19ca7609 758 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
759
760 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
761 list_del(&rrq->list);
762 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
763 }
19ca7609
JS
764}
765
766/**
767 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
768 * @phba: Pointer to HBA context object.
769 *
770 * Remove all rrqs from the phba->active_rrq_list and free them by
771 * calling __lpfc_clr_active_rrq
772 *
773 **/
774void
775lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
776{
777 struct lpfc_node_rrq *rrq;
778 struct lpfc_node_rrq *nextrrq;
779 unsigned long next_time;
780 unsigned long iflags;
1151e3ec 781 LIST_HEAD(rrq_list);
19ca7609
JS
782
783 if (phba->sli_rev != LPFC_SLI_REV4)
784 return;
785 spin_lock_irqsave(&phba->hbalock, iflags);
786 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 787 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2));
1151e3ec
JS
788 list_splice_init(&phba->active_rrq_list, &rrq_list);
789 spin_unlock_irqrestore(&phba->hbalock, iflags);
790
791 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
19ca7609 792 list_del(&rrq->list);
1151e3ec 793 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
19ca7609 794 }
19ca7609
JS
795 if (!list_empty(&phba->active_rrq_list))
796 mod_timer(&phba->rrq_tmr, next_time);
797}
798
799
800/**
1151e3ec 801 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
802 * @phba: Pointer to HBA context object.
803 * @ndlp: Targets nodelist pointer for this exchange.
804 * @xritag the xri in the bitmap to test.
805 *
806 * This function is called with hbalock held. This function
807 * returns 0 = rrq not active for this xri
808 * 1 = rrq is valid for this xri.
809 **/
1151e3ec
JS
810int
811lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
812 uint16_t xritag)
813{
19ca7609
JS
814 if (!ndlp)
815 return 0;
6d368e53 816 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
19ca7609
JS
817 return 1;
818 else
819 return 0;
820}
821
822/**
823 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
824 * @phba: Pointer to HBA context object.
825 * @ndlp: nodelist pointer for this target.
826 * @xritag: xri used in this exchange.
827 * @rxid: Remote Exchange ID.
828 * @send_rrq: Flag used to determine if we should send rrq els cmd.
829 *
830 * This function takes the hbalock.
831 * The active bit is always set in the active rrq xri_bitmap even
832 * if there is no slot avaiable for the other rrq information.
833 *
834 * returns 0 rrq actived for this xri
835 * < 0 No memory or invalid ndlp.
836 **/
837int
838lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 839 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 840{
19ca7609 841 unsigned long iflags;
b42c07c8
JS
842 struct lpfc_node_rrq *rrq;
843 int empty;
844
845 if (!ndlp)
846 return -EINVAL;
847
848 if (!phba->cfg_enable_rrq)
849 return -EINVAL;
19ca7609
JS
850
851 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
852 if (phba->pport->load_flag & FC_UNLOADING) {
853 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
854 goto out;
855 }
856
857 /*
858 * set the active bit even if there is no mem available.
859 */
860 if (NLP_CHK_FREE_REQ(ndlp))
861 goto out;
862
863 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
864 goto out;
865
866 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
867 goto out;
868
19ca7609 869 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
870 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
871 if (!rrq) {
872 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
873 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
874 " DID:0x%x Send:%d\n",
875 xritag, rxid, ndlp->nlp_DID, send_rrq);
876 return -EINVAL;
877 }
e5771b4d
JS
878 if (phba->cfg_enable_rrq == 1)
879 rrq->send_rrq = send_rrq;
880 else
881 rrq->send_rrq = 0;
b42c07c8 882 rrq->xritag = xritag;
256ec0d0
JS
883 rrq->rrq_stop_time = jiffies +
884 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
885 rrq->ndlp = ndlp;
886 rrq->nlp_DID = ndlp->nlp_DID;
887 rrq->vport = ndlp->vport;
888 rrq->rxid = rxid;
b42c07c8
JS
889 spin_lock_irqsave(&phba->hbalock, iflags);
890 empty = list_empty(&phba->active_rrq_list);
891 list_add_tail(&rrq->list, &phba->active_rrq_list);
892 phba->hba_flag |= HBA_RRQ_ACTIVE;
893 if (empty)
894 lpfc_worker_wake_up(phba);
895 spin_unlock_irqrestore(&phba->hbalock, iflags);
896 return 0;
897out:
898 spin_unlock_irqrestore(&phba->hbalock, iflags);
899 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
900 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
901 " DID:0x%x Send:%d\n",
902 xritag, rxid, ndlp->nlp_DID, send_rrq);
903 return -EINVAL;
19ca7609
JS
904}
905
da0436e9
JS
906/**
907 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
908 * @phba: Pointer to HBA context object.
19ca7609 909 * @piocb: Pointer to the iocbq.
da0436e9
JS
910 *
911 * This function is called with hbalock held. This function
6d368e53 912 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
913 * list is not empty then it is successful, it returns pointer to the newly
914 * allocated sglq object else it returns NULL.
915 **/
916static struct lpfc_sglq *
19ca7609 917__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9
JS
918{
919 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
920 struct lpfc_sglq *sglq = NULL;
19ca7609 921 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
922 struct lpfc_scsi_buf *lpfc_cmd;
923 struct lpfc_nodelist *ndlp;
924 int found = 0;
925
926 if (piocbq->iocb_flag & LPFC_IO_FCP) {
927 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
928 ndlp = lpfc_cmd->rdata->pnode;
be858b65
JS
929 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
930 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
19ca7609 931 ndlp = piocbq->context_un.ndlp;
d5ce53b7 932 else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
93d1379e 933 ndlp = piocbq->context_un.ndlp;
19ca7609
JS
934 else
935 ndlp = piocbq->context1;
936
da0436e9 937 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
938 start_sglq = sglq;
939 while (!found) {
940 if (!sglq)
941 return NULL;
ee0f4fe1 942 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
19ca7609
JS
943 /* This xri has an rrq outstanding for this DID.
944 * put it back in the list and get another xri.
945 */
946 list_add_tail(&sglq->list, lpfc_sgl_list);
947 sglq = NULL;
948 list_remove_head(lpfc_sgl_list, sglq,
949 struct lpfc_sglq, list);
950 if (sglq == start_sglq) {
951 sglq = NULL;
952 break;
953 } else
954 continue;
955 }
956 sglq->ndlp = ndlp;
957 found = 1;
6d368e53 958 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
959 sglq->state = SGL_ALLOCATED;
960 }
da0436e9
JS
961 return sglq;
962}
963
e59058c4 964/**
3621a710 965 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
966 * @phba: Pointer to HBA context object.
967 *
968 * This function is called with no lock held. This function
969 * allocates a new driver iocb object from the iocb pool. If the
970 * allocation is successful, it returns pointer to the newly
971 * allocated iocb object else it returns NULL.
972 **/
2e0fef85
JS
973struct lpfc_iocbq *
974lpfc_sli_get_iocbq(struct lpfc_hba *phba)
975{
976 struct lpfc_iocbq * iocbq = NULL;
977 unsigned long iflags;
978
979 spin_lock_irqsave(&phba->hbalock, iflags);
980 iocbq = __lpfc_sli_get_iocbq(phba);
981 spin_unlock_irqrestore(&phba->hbalock, iflags);
982 return iocbq;
983}
984
4f774513
JS
985/**
986 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
987 * @phba: Pointer to HBA context object.
988 * @iocbq: Pointer to driver iocb object.
989 *
990 * This function is called with hbalock held to release driver
991 * iocb object to the iocb pool. The iotag in the iocb object
992 * does not change for each use of the iocb object. This function
993 * clears all other fields of the iocb object when it is freed.
994 * The sqlq structure that holds the xritag and phys and virtual
995 * mappings for the scatter gather list is retrieved from the
996 * active array of sglq. The get of the sglq pointer also clears
997 * the entry in the array. If the status of the IO indiactes that
998 * this IO was aborted then the sglq entry it put on the
999 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1000 * IO has good status or fails for any other reason then the sglq
1001 * entry is added to the free list (lpfc_sgl_list).
1002 **/
1003static void
1004__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1005{
1006 struct lpfc_sglq *sglq;
1007 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
1008 unsigned long iflag = 0;
1009 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513
JS
1010
1011 if (iocbq->sli4_xritag == NO_XRI)
1012 sglq = NULL;
1013 else
6d368e53
JS
1014 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1015
0e9bb8d7 1016
4f774513 1017 if (sglq) {
0f65ff68
JS
1018 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1019 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
1020 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1021 iflag);
1022 list_add(&sglq->list,
1023 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1024 spin_unlock_irqrestore(
1025 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
1026 } else {
1027 sglq->state = SGL_FREED;
19ca7609 1028 sglq->ndlp = NULL;
fedd3b7b
JS
1029 list_add_tail(&sglq->list,
1030 &phba->sli4_hba.lpfc_sgl_list);
2a9bf3d0
JS
1031
1032 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1033 if (!list_empty(&pring->txq))
2a9bf3d0 1034 lpfc_worker_wake_up(phba);
0f65ff68 1035 }
4f774513
JS
1036 }
1037
1038
1039 /*
1040 * Clean all volatile data fields, preserve iotag and node struct.
1041 */
1042 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1043 iocbq->sli4_lxritag = NO_XRI;
4f774513
JS
1044 iocbq->sli4_xritag = NO_XRI;
1045 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1046}
1047
2a9bf3d0 1048
e59058c4 1049/**
3772a991 1050 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1051 * @phba: Pointer to HBA context object.
1052 * @iocbq: Pointer to driver iocb object.
1053 *
1054 * This function is called with hbalock held to release driver
1055 * iocb object to the iocb pool. The iotag in the iocb object
1056 * does not change for each use of the iocb object. This function
1057 * clears all other fields of the iocb object when it is freed.
1058 **/
a6ababd2 1059static void
3772a991 1060__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1061{
2e0fef85 1062 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1063
0e9bb8d7 1064
604a3e30
JB
1065 /*
1066 * Clean all volatile data fields, preserve iotag and node struct.
1067 */
1068 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1069 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1070 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1071}
1072
3772a991
JS
1073/**
1074 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1075 * @phba: Pointer to HBA context object.
1076 * @iocbq: Pointer to driver iocb object.
1077 *
1078 * This function is called with hbalock held to release driver
1079 * iocb object to the iocb pool. The iotag in the iocb object
1080 * does not change for each use of the iocb object. This function
1081 * clears all other fields of the iocb object when it is freed.
1082 **/
1083static void
1084__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1085{
1086 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1087 phba->iocb_cnt--;
3772a991
JS
1088}
1089
e59058c4 1090/**
3621a710 1091 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1092 * @phba: Pointer to HBA context object.
1093 * @iocbq: Pointer to driver iocb object.
1094 *
1095 * This function is called with no lock held to release the iocb to
1096 * iocb pool.
1097 **/
2e0fef85
JS
1098void
1099lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1100{
1101 unsigned long iflags;
1102
1103 /*
1104 * Clean all volatile data fields, preserve iotag and node struct.
1105 */
1106 spin_lock_irqsave(&phba->hbalock, iflags);
1107 __lpfc_sli_release_iocbq(phba, iocbq);
1108 spin_unlock_irqrestore(&phba->hbalock, iflags);
1109}
1110
a257bf90
JS
1111/**
1112 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1113 * @phba: Pointer to HBA context object.
1114 * @iocblist: List of IOCBs.
1115 * @ulpstatus: ULP status in IOCB command field.
1116 * @ulpWord4: ULP word-4 in IOCB command field.
1117 *
1118 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1119 * on the list by invoking the complete callback function associated with the
1120 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1121 * fields.
1122 **/
1123void
1124lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1125 uint32_t ulpstatus, uint32_t ulpWord4)
1126{
1127 struct lpfc_iocbq *piocb;
1128
1129 while (!list_empty(iocblist)) {
1130 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1131 if (!piocb->iocb_cmpl)
1132 lpfc_sli_release_iocbq(phba, piocb);
1133 else {
1134 piocb->iocb.ulpStatus = ulpstatus;
1135 piocb->iocb.un.ulpWord[4] = ulpWord4;
1136 (piocb->iocb_cmpl) (phba, piocb, piocb);
1137 }
1138 }
1139 return;
1140}
1141
e59058c4 1142/**
3621a710
JS
1143 * lpfc_sli_iocb_cmd_type - Get the iocb type
1144 * @iocb_cmnd: iocb command code.
e59058c4
JS
1145 *
1146 * This function is called by ring event handler function to get the iocb type.
1147 * This function translates the iocb command to an iocb command type used to
1148 * decide the final disposition of each completed IOCB.
1149 * The function returns
1150 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1151 * LPFC_SOL_IOCB if it is a solicited iocb completion
1152 * LPFC_ABORT_IOCB if it is an abort iocb
1153 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1154 *
1155 * The caller is not required to hold any lock.
1156 **/
dea3101e
JB
1157static lpfc_iocb_type
1158lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1159{
1160 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1161
1162 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1163 return 0;
1164
1165 switch (iocb_cmnd) {
1166 case CMD_XMIT_SEQUENCE_CR:
1167 case CMD_XMIT_SEQUENCE_CX:
1168 case CMD_XMIT_BCAST_CN:
1169 case CMD_XMIT_BCAST_CX:
1170 case CMD_ELS_REQUEST_CR:
1171 case CMD_ELS_REQUEST_CX:
1172 case CMD_CREATE_XRI_CR:
1173 case CMD_CREATE_XRI_CX:
1174 case CMD_GET_RPI_CN:
1175 case CMD_XMIT_ELS_RSP_CX:
1176 case CMD_GET_RPI_CR:
1177 case CMD_FCP_IWRITE_CR:
1178 case CMD_FCP_IWRITE_CX:
1179 case CMD_FCP_IREAD_CR:
1180 case CMD_FCP_IREAD_CX:
1181 case CMD_FCP_ICMND_CR:
1182 case CMD_FCP_ICMND_CX:
f5603511
JS
1183 case CMD_FCP_TSEND_CX:
1184 case CMD_FCP_TRSP_CX:
1185 case CMD_FCP_TRECEIVE_CX:
1186 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1187 case CMD_ADAPTER_MSG:
1188 case CMD_ADAPTER_DUMP:
1189 case CMD_XMIT_SEQUENCE64_CR:
1190 case CMD_XMIT_SEQUENCE64_CX:
1191 case CMD_XMIT_BCAST64_CN:
1192 case CMD_XMIT_BCAST64_CX:
1193 case CMD_ELS_REQUEST64_CR:
1194 case CMD_ELS_REQUEST64_CX:
1195 case CMD_FCP_IWRITE64_CR:
1196 case CMD_FCP_IWRITE64_CX:
1197 case CMD_FCP_IREAD64_CR:
1198 case CMD_FCP_IREAD64_CX:
1199 case CMD_FCP_ICMND64_CR:
1200 case CMD_FCP_ICMND64_CX:
f5603511
JS
1201 case CMD_FCP_TSEND64_CX:
1202 case CMD_FCP_TRSP64_CX:
1203 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1204 case CMD_GEN_REQUEST64_CR:
1205 case CMD_GEN_REQUEST64_CX:
1206 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1207 case DSSCMD_IWRITE64_CR:
1208 case DSSCMD_IWRITE64_CX:
1209 case DSSCMD_IREAD64_CR:
1210 case DSSCMD_IREAD64_CX:
dea3101e
JB
1211 type = LPFC_SOL_IOCB;
1212 break;
1213 case CMD_ABORT_XRI_CN:
1214 case CMD_ABORT_XRI_CX:
1215 case CMD_CLOSE_XRI_CN:
1216 case CMD_CLOSE_XRI_CX:
1217 case CMD_XRI_ABORTED_CX:
1218 case CMD_ABORT_MXRI64_CN:
6669f9bb 1219 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1220 type = LPFC_ABORT_IOCB;
1221 break;
1222 case CMD_RCV_SEQUENCE_CX:
1223 case CMD_RCV_ELS_REQ_CX:
1224 case CMD_RCV_SEQUENCE64_CX:
1225 case CMD_RCV_ELS_REQ64_CX:
57127f15 1226 case CMD_ASYNC_STATUS:
ed957684
JS
1227 case CMD_IOCB_RCV_SEQ64_CX:
1228 case CMD_IOCB_RCV_ELS64_CX:
1229 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1230 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1231 type = LPFC_UNSOL_IOCB;
1232 break;
3163f725
JS
1233 case CMD_IOCB_XMIT_MSEQ64_CR:
1234 case CMD_IOCB_XMIT_MSEQ64_CX:
1235 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1236 case CMD_IOCB_RCV_ELS_LIST64_CX:
1237 case CMD_IOCB_CLOSE_EXTENDED_CN:
1238 case CMD_IOCB_ABORT_EXTENDED_CN:
1239 case CMD_IOCB_RET_HBQE64_CN:
1240 case CMD_IOCB_FCP_IBIDIR64_CR:
1241 case CMD_IOCB_FCP_IBIDIR64_CX:
1242 case CMD_IOCB_FCP_ITASKMGT64_CX:
1243 case CMD_IOCB_LOGENTRY_CN:
1244 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1245 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1246 __func__, iocb_cmnd);
3163f725
JS
1247 type = LPFC_UNKNOWN_IOCB;
1248 break;
dea3101e
JB
1249 default:
1250 type = LPFC_UNKNOWN_IOCB;
1251 break;
1252 }
1253
1254 return type;
1255}
1256
e59058c4 1257/**
3621a710 1258 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1259 * @phba: Pointer to HBA context object.
1260 *
1261 * This function is called from SLI initialization code
1262 * to configure every ring of the HBA's SLI interface. The
1263 * caller is not required to hold any lock. This function issues
1264 * a config_ring mailbox command for each ring.
1265 * This function returns zero if successful else returns a negative
1266 * error code.
1267 **/
dea3101e 1268static int
ed957684 1269lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1270{
1271 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1272 LPFC_MBOXQ_t *pmb;
1273 MAILBOX_t *pmbox;
1274 int i, rc, ret = 0;
dea3101e 1275
ed957684
JS
1276 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1277 if (!pmb)
1278 return -ENOMEM;
04c68496 1279 pmbox = &pmb->u.mb;
ed957684 1280 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1281 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1282 lpfc_config_ring(phba, i, pmb);
1283 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1284 if (rc != MBX_SUCCESS) {
92d7f7b0 1285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1286 "0446 Adapter failed to init (%d), "
dea3101e
JB
1287 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1288 "ring %d\n",
e8b62011
JS
1289 rc, pmbox->mbxCommand,
1290 pmbox->mbxStatus, i);
2e0fef85 1291 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1292 ret = -ENXIO;
1293 break;
dea3101e
JB
1294 }
1295 }
ed957684
JS
1296 mempool_free(pmb, phba->mbox_mem_pool);
1297 return ret;
dea3101e
JB
1298}
1299
e59058c4 1300/**
3621a710 1301 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1302 * @phba: Pointer to HBA context object.
1303 * @pring: Pointer to driver SLI ring object.
1304 * @piocb: Pointer to the driver iocb object.
1305 *
1306 * This function is called with hbalock held. The function adds the
1307 * new iocb to txcmplq of the given ring. This function always returns
1308 * 0. If this function is called for ELS ring, this function checks if
1309 * there is a vport associated with the ELS command. This function also
1310 * starts els_tmofunc timer if this is an ELS command.
1311 **/
dea3101e 1312static int
2e0fef85
JS
1313lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1314 struct lpfc_iocbq *piocb)
dea3101e 1315{
dea3101e 1316 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1317 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1318
92d7f7b0
JS
1319 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1320 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1321 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1322 if (!piocb->vport)
1323 BUG();
1324 else
1325 mod_timer(&piocb->vport->els_tmofunc,
256ec0d0
JS
1326 jiffies +
1327 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
92d7f7b0
JS
1328 }
1329
dea3101e 1330
2e0fef85 1331 return 0;
dea3101e
JB
1332}
1333
e59058c4 1334/**
3621a710 1335 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1336 * @phba: Pointer to HBA context object.
1337 * @pring: Pointer to driver SLI ring object.
1338 *
1339 * This function is called with hbalock held to get next
1340 * iocb in txq of the given ring. If there is any iocb in
1341 * the txq, the function returns first iocb in the list after
1342 * removing the iocb from the list, else it returns NULL.
1343 **/
2a9bf3d0 1344struct lpfc_iocbq *
2e0fef85 1345lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1346{
dea3101e
JB
1347 struct lpfc_iocbq *cmd_iocb;
1348
858c9f6c 1349 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1350 return cmd_iocb;
dea3101e
JB
1351}
1352
e59058c4 1353/**
3621a710 1354 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1355 * @phba: Pointer to HBA context object.
1356 * @pring: Pointer to driver SLI ring object.
1357 *
1358 * This function is called with hbalock held and the caller must post the
1359 * iocb without releasing the lock. If the caller releases the lock,
1360 * iocb slot returned by the function is not guaranteed to be available.
1361 * The function returns pointer to the next available iocb slot if there
1362 * is available slot in the ring, else it returns NULL.
1363 * If the get index of the ring is ahead of the put index, the function
1364 * will post an error attention event to the worker thread to take the
1365 * HBA to offline state.
1366 **/
dea3101e
JB
1367static IOCB_t *
1368lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1369{
34b02dcd 1370 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25
JS
1371 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1372 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1373 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1374 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1375
7e56aa25
JS
1376 if (unlikely(pring->sli.sli3.local_getidx ==
1377 pring->sli.sli3.next_cmdidx)) {
dea3101e 1378
7e56aa25 1379 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1380
7e56aa25 1381 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1383 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1384 "is bigger than cmd ring %d\n",
e8b62011 1385 pring->ringno,
7e56aa25
JS
1386 pring->sli.sli3.local_getidx,
1387 max_cmd_idx);
dea3101e 1388
2e0fef85 1389 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1390 /*
1391 * All error attention handlers are posted to
1392 * worker thread
1393 */
1394 phba->work_ha |= HA_ERATT;
1395 phba->work_hs = HS_FFER3;
92d7f7b0 1396
5e9d9b82 1397 lpfc_worker_wake_up(phba);
dea3101e
JB
1398
1399 return NULL;
1400 }
1401
7e56aa25 1402 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1403 return NULL;
1404 }
1405
ed957684 1406 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1407}
1408
e59058c4 1409/**
3621a710 1410 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1411 * @phba: Pointer to HBA context object.
1412 * @iocbq: Pointer to driver iocb object.
1413 *
1414 * This function gets an iotag for the iocb. If there is no unused iotag and
1415 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1416 * array and assigns a new iotag.
1417 * The function returns the allocated iotag if successful, else returns zero.
1418 * Zero is not a valid iotag.
1419 * The caller is not required to hold any lock.
1420 **/
604a3e30 1421uint16_t
2e0fef85 1422lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1423{
2e0fef85
JS
1424 struct lpfc_iocbq **new_arr;
1425 struct lpfc_iocbq **old_arr;
604a3e30
JB
1426 size_t new_len;
1427 struct lpfc_sli *psli = &phba->sli;
1428 uint16_t iotag;
dea3101e 1429
2e0fef85 1430 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1431 iotag = psli->last_iotag;
1432 if(++iotag < psli->iocbq_lookup_len) {
1433 psli->last_iotag = iotag;
1434 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1435 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1436 iocbq->iotag = iotag;
1437 return iotag;
2e0fef85 1438 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1439 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1440 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1441 spin_unlock_irq(&phba->hbalock);
1442 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1443 GFP_KERNEL);
1444 if (new_arr) {
2e0fef85 1445 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1446 old_arr = psli->iocbq_lookup;
1447 if (new_len <= psli->iocbq_lookup_len) {
1448 /* highly unprobable case */
1449 kfree(new_arr);
1450 iotag = psli->last_iotag;
1451 if(++iotag < psli->iocbq_lookup_len) {
1452 psli->last_iotag = iotag;
1453 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1454 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1455 iocbq->iotag = iotag;
1456 return iotag;
1457 }
2e0fef85 1458 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1459 return 0;
1460 }
1461 if (psli->iocbq_lookup)
1462 memcpy(new_arr, old_arr,
1463 ((psli->last_iotag + 1) *
311464ec 1464 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1465 psli->iocbq_lookup = new_arr;
1466 psli->iocbq_lookup_len = new_len;
1467 psli->last_iotag = iotag;
1468 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1469 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1470 iocbq->iotag = iotag;
1471 kfree(old_arr);
1472 return iotag;
1473 }
8f6d98d2 1474 } else
2e0fef85 1475 spin_unlock_irq(&phba->hbalock);
dea3101e 1476
bc73905a 1477 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1478 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1479 psli->last_iotag);
dea3101e 1480
604a3e30 1481 return 0;
dea3101e
JB
1482}
1483
e59058c4 1484/**
3621a710 1485 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1486 * @phba: Pointer to HBA context object.
1487 * @pring: Pointer to driver SLI ring object.
1488 * @iocb: Pointer to iocb slot in the ring.
1489 * @nextiocb: Pointer to driver iocb object which need to be
1490 * posted to firmware.
1491 *
1492 * This function is called with hbalock held to post a new iocb to
1493 * the firmware. This function copies the new iocb to ring iocb slot and
1494 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1495 * a completion call back for this iocb else the function will free the
1496 * iocb object.
1497 **/
dea3101e
JB
1498static void
1499lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1500 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1501{
1502 /*
604a3e30 1503 * Set up an iotag
dea3101e 1504 */
604a3e30 1505 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1506
e2a0a9d6 1507
a58cbd52
JS
1508 if (pring->ringno == LPFC_ELS_RING) {
1509 lpfc_debugfs_slow_ring_trc(phba,
1510 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1511 *(((uint32_t *) &nextiocb->iocb) + 4),
1512 *(((uint32_t *) &nextiocb->iocb) + 6),
1513 *(((uint32_t *) &nextiocb->iocb) + 7));
1514 }
1515
dea3101e
JB
1516 /*
1517 * Issue iocb command to adapter
1518 */
92d7f7b0 1519 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1520 wmb();
1521 pring->stats.iocb_cmd++;
1522
1523 /*
1524 * If there is no completion routine to call, we can release the
1525 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1526 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1527 */
1528 if (nextiocb->iocb_cmpl)
1529 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1530 else
2e0fef85 1531 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1532
1533 /*
1534 * Let the HBA know what IOCB slot will be the next one the
1535 * driver will put a command into.
1536 */
7e56aa25
JS
1537 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1538 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1539}
1540
e59058c4 1541/**
3621a710 1542 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1543 * @phba: Pointer to HBA context object.
1544 * @pring: Pointer to driver SLI ring object.
1545 *
1546 * The caller is not required to hold any lock for calling this function.
1547 * This function updates the chip attention bits for the ring to inform firmware
1548 * that there are pending work to be done for this ring and requests an
1549 * interrupt when there is space available in the ring. This function is
1550 * called when the driver is unable to post more iocbs to the ring due
1551 * to unavailability of space in the ring.
1552 **/
dea3101e 1553static void
2e0fef85 1554lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1555{
1556 int ringno = pring->ringno;
1557
1558 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1559
1560 wmb();
1561
1562 /*
1563 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1564 * The HBA will tell us when an IOCB entry is available.
1565 */
1566 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1567 readl(phba->CAregaddr); /* flush */
1568
1569 pring->stats.iocb_cmd_full++;
1570}
1571
e59058c4 1572/**
3621a710 1573 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1574 * @phba: Pointer to HBA context object.
1575 * @pring: Pointer to driver SLI ring object.
1576 *
1577 * This function updates the chip attention register bit for the
1578 * given ring to inform HBA that there is more work to be done
1579 * in this ring. The caller is not required to hold any lock.
1580 **/
dea3101e 1581static void
2e0fef85 1582lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1583{
1584 int ringno = pring->ringno;
1585
1586 /*
1587 * Tell the HBA that there is work to do in this ring.
1588 */
34b02dcd
JS
1589 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1590 wmb();
1591 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1592 readl(phba->CAregaddr); /* flush */
1593 }
dea3101e
JB
1594}
1595
e59058c4 1596/**
3621a710 1597 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1598 * @phba: Pointer to HBA context object.
1599 * @pring: Pointer to driver SLI ring object.
1600 *
1601 * This function is called with hbalock held to post pending iocbs
1602 * in the txq to the firmware. This function is called when driver
1603 * detects space available in the ring.
1604 **/
dea3101e 1605static void
2e0fef85 1606lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1607{
1608 IOCB_t *iocb;
1609 struct lpfc_iocbq *nextiocb;
1610
1611 /*
1612 * Check to see if:
1613 * (a) there is anything on the txq to send
1614 * (b) link is up
1615 * (c) link attention events can be processed (fcp ring only)
1616 * (d) IOCB processing is not blocked by the outstanding mbox command.
1617 */
0e9bb8d7
JS
1618
1619 if (lpfc_is_link_up(phba) &&
1620 (!list_empty(&pring->txq)) &&
dea3101e 1621 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1622 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1623
1624 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1625 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1626 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1627
1628 if (iocb)
1629 lpfc_sli_update_ring(phba, pring);
1630 else
1631 lpfc_sli_update_full_ring(phba, pring);
1632 }
1633
1634 return;
1635}
1636
e59058c4 1637/**
3621a710 1638 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1639 * @phba: Pointer to HBA context object.
1640 * @hbqno: HBQ number.
1641 *
1642 * This function is called with hbalock held to get the next
1643 * available slot for the given HBQ. If there is free slot
1644 * available for the HBQ it will return pointer to the next available
1645 * HBQ entry else it will return NULL.
1646 **/
a6ababd2 1647static struct lpfc_hbq_entry *
ed957684
JS
1648lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1649{
1650 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1651
1652 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1653 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1654 hbqp->next_hbqPutIdx = 0;
1655
1656 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1657 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1658 uint32_t getidx = le32_to_cpu(raw_index);
1659
1660 hbqp->local_hbqGetIdx = getidx;
1661
1662 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1663 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1664 LOG_SLI | LOG_VPORT,
e8b62011 1665 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1666 "%u is > than hbqp->entry_count %u\n",
e8b62011 1667 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1668 hbqp->entry_count);
1669
1670 phba->link_state = LPFC_HBA_ERROR;
1671 return NULL;
1672 }
1673
1674 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1675 return NULL;
1676 }
1677
51ef4c26
JS
1678 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1679 hbqp->hbqPutIdx;
ed957684
JS
1680}
1681
e59058c4 1682/**
3621a710 1683 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1684 * @phba: Pointer to HBA context object.
1685 *
1686 * This function is called with no lock held to free all the
1687 * hbq buffers while uninitializing the SLI interface. It also
1688 * frees the HBQ buffers returned by the firmware but not yet
1689 * processed by the upper layers.
1690 **/
ed957684
JS
1691void
1692lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1693{
92d7f7b0
JS
1694 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1695 struct hbq_dmabuf *hbq_buf;
3163f725 1696 unsigned long flags;
51ef4c26 1697 int i, hbq_count;
3163f725 1698 uint32_t hbqno;
ed957684 1699
51ef4c26 1700 hbq_count = lpfc_sli_hbq_count();
ed957684 1701 /* Return all memory used by all HBQs */
3163f725 1702 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1703 for (i = 0; i < hbq_count; ++i) {
1704 list_for_each_entry_safe(dmabuf, next_dmabuf,
1705 &phba->hbqs[i].hbq_buffer_list, list) {
1706 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1707 list_del(&hbq_buf->dbuf.list);
1708 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1709 }
a8adb832 1710 phba->hbqs[i].buffer_count = 0;
ed957684 1711 }
3163f725 1712 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1713 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1714 list) {
3163f725
JS
1715 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1716 list_del(&hbq_buf->dbuf.list);
1717 if (hbq_buf->tag == -1) {
1718 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1719 (phba, hbq_buf);
1720 } else {
1721 hbqno = hbq_buf->tag >> 16;
1722 if (hbqno >= LPFC_MAX_HBQS)
1723 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1724 (phba, hbq_buf);
1725 else
1726 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1727 hbq_buf);
1728 }
1729 }
1730
1731 /* Mark the HBQs not in use */
1732 phba->hbq_in_use = 0;
1733 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1734}
1735
e59058c4 1736/**
3621a710 1737 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1738 * @phba: Pointer to HBA context object.
1739 * @hbqno: HBQ number.
1740 * @hbq_buf: Pointer to HBQ buffer.
1741 *
1742 * This function is called with the hbalock held to post a
1743 * hbq buffer to the firmware. If the function finds an empty
1744 * slot in the HBQ, it will post the buffer. The function will return
1745 * pointer to the hbq entry if it successfully post the buffer
1746 * else it will return NULL.
1747 **/
3772a991 1748static int
ed957684 1749lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1750 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1751{
1752 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1753}
1754
1755/**
1756 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1757 * @phba: Pointer to HBA context object.
1758 * @hbqno: HBQ number.
1759 * @hbq_buf: Pointer to HBQ buffer.
1760 *
1761 * This function is called with the hbalock held to post a hbq buffer to the
1762 * firmware. If the function finds an empty slot in the HBQ, it will post the
1763 * buffer and place it on the hbq_buffer_list. The function will return zero if
1764 * it successfully post the buffer else it will return an error.
1765 **/
1766static int
1767lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1768 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1769{
1770 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1771 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1772
1773 /* Get next HBQ entry slot to use */
1774 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1775 if (hbqe) {
1776 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1777
92d7f7b0
JS
1778 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1779 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1780 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1781 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1782 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1783 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1784 /* Sync SLIM */
ed957684
JS
1785 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1786 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1787 /* flush */
ed957684 1788 readl(phba->hbq_put + hbqno);
51ef4c26 1789 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1790 return 0;
1791 } else
1792 return -ENOMEM;
ed957684
JS
1793}
1794
4f774513
JS
1795/**
1796 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1797 * @phba: Pointer to HBA context object.
1798 * @hbqno: HBQ number.
1799 * @hbq_buf: Pointer to HBQ buffer.
1800 *
1801 * This function is called with the hbalock held to post an RQE to the SLI4
1802 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1803 * the hbq_buffer_list and return zero, otherwise it will return an error.
1804 **/
1805static int
1806lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1807 struct hbq_dmabuf *hbq_buf)
1808{
1809 int rc;
1810 struct lpfc_rqe hrqe;
1811 struct lpfc_rqe drqe;
1812
1813 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1814 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1815 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1816 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1817 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1818 &hrqe, &drqe);
1819 if (rc < 0)
1820 return rc;
1821 hbq_buf->tag = rc;
1822 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1823 return 0;
1824}
1825
e59058c4 1826/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1827static struct lpfc_hbq_init lpfc_els_hbq = {
1828 .rn = 1,
def9c7a9 1829 .entry_count = 256,
92d7f7b0
JS
1830 .mask_count = 0,
1831 .profile = 0,
51ef4c26 1832 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1833 .buffer_count = 0,
a257bf90
JS
1834 .init_count = 40,
1835 .add_count = 40,
92d7f7b0 1836};
ed957684 1837
e59058c4 1838/* HBQ for the extra ring if needed */
51ef4c26
JS
1839static struct lpfc_hbq_init lpfc_extra_hbq = {
1840 .rn = 1,
1841 .entry_count = 200,
1842 .mask_count = 0,
1843 .profile = 0,
1844 .ring_mask = (1 << LPFC_EXTRA_RING),
1845 .buffer_count = 0,
1846 .init_count = 0,
1847 .add_count = 5,
1848};
1849
e59058c4 1850/* Array of HBQs */
78b2d852 1851struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1852 &lpfc_els_hbq,
51ef4c26 1853 &lpfc_extra_hbq,
92d7f7b0 1854};
ed957684 1855
e59058c4 1856/**
3621a710 1857 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1858 * @phba: Pointer to HBA context object.
1859 * @hbqno: HBQ number.
1860 * @count: Number of HBQ buffers to be posted.
1861 *
d7c255b2
JS
1862 * This function is called with no lock held to post more hbq buffers to the
1863 * given HBQ. The function returns the number of HBQ buffers successfully
1864 * posted.
e59058c4 1865 **/
311464ec 1866static int
92d7f7b0 1867lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1868{
d7c255b2 1869 uint32_t i, posted = 0;
3163f725 1870 unsigned long flags;
92d7f7b0 1871 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1872 LIST_HEAD(hbq_buf_list);
eafe1df9 1873 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1874 return 0;
51ef4c26 1875
d7c255b2
JS
1876 if ((phba->hbqs[hbqno].buffer_count + count) >
1877 lpfc_hbq_defs[hbqno]->entry_count)
1878 count = lpfc_hbq_defs[hbqno]->entry_count -
1879 phba->hbqs[hbqno].buffer_count;
1880 if (!count)
1881 return 0;
1882 /* Allocate HBQ entries */
1883 for (i = 0; i < count; i++) {
1884 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1885 if (!hbq_buffer)
1886 break;
1887 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1888 }
3163f725
JS
1889 /* Check whether HBQ is still in use */
1890 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1891 if (!phba->hbq_in_use)
d7c255b2
JS
1892 goto err;
1893 while (!list_empty(&hbq_buf_list)) {
1894 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1895 dbuf.list);
1896 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1897 (hbqno << 16));
3772a991 1898 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1899 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1900 posted++;
1901 } else
51ef4c26 1902 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1903 }
3163f725 1904 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1905 return posted;
1906err:
eafe1df9 1907 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1908 while (!list_empty(&hbq_buf_list)) {
1909 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1910 dbuf.list);
1911 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1912 }
1913 return 0;
ed957684
JS
1914}
1915
e59058c4 1916/**
3621a710 1917 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1918 * @phba: Pointer to HBA context object.
1919 * @qno: HBQ number.
1920 *
1921 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1922 * is called with no lock held. The function returns the number of HBQ entries
1923 * successfully allocated.
e59058c4 1924 **/
92d7f7b0
JS
1925int
1926lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1927{
def9c7a9
JS
1928 if (phba->sli_rev == LPFC_SLI_REV4)
1929 return 0;
1930 else
1931 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1932 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1933}
ed957684 1934
e59058c4 1935/**
3621a710 1936 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1937 * @phba: Pointer to HBA context object.
1938 * @qno: HBQ queue number.
1939 *
1940 * This function is called from SLI initialization code path with
1941 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1942 * function returns the number of HBQ entries successfully allocated.
e59058c4 1943 **/
a6ababd2 1944static int
92d7f7b0
JS
1945lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1946{
def9c7a9
JS
1947 if (phba->sli_rev == LPFC_SLI_REV4)
1948 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 1949 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
1950 else
1951 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1952 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1953}
1954
3772a991
JS
1955/**
1956 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1957 * @phba: Pointer to HBA context object.
1958 * @hbqno: HBQ number.
1959 *
1960 * This function removes the first hbq buffer on an hbq list and returns a
1961 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1962 **/
1963static struct hbq_dmabuf *
1964lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1965{
1966 struct lpfc_dmabuf *d_buf;
1967
1968 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1969 if (!d_buf)
1970 return NULL;
1971 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1972}
1973
e59058c4 1974/**
3621a710 1975 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1976 * @phba: Pointer to HBA context object.
1977 * @tag: Tag of the hbq buffer.
1978 *
1979 * This function is called with hbalock held. This function searches
1980 * for the hbq buffer associated with the given tag in the hbq buffer
1981 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1982 * it returns NULL.
1983 **/
a6ababd2 1984static struct hbq_dmabuf *
92d7f7b0 1985lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1986{
92d7f7b0
JS
1987 struct lpfc_dmabuf *d_buf;
1988 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1989 uint32_t hbqno;
1990
1991 hbqno = tag >> 16;
a0a74e45 1992 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1993 return NULL;
ed957684 1994
3772a991 1995 spin_lock_irq(&phba->hbalock);
51ef4c26 1996 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1997 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1998 if (hbq_buf->tag == tag) {
3772a991 1999 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2000 return hbq_buf;
ed957684
JS
2001 }
2002 }
3772a991 2003 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2005 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2006 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2007 return NULL;
ed957684
JS
2008}
2009
e59058c4 2010/**
3621a710 2011 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2012 * @phba: Pointer to HBA context object.
2013 * @hbq_buffer: Pointer to HBQ buffer.
2014 *
2015 * This function is called with hbalock. This function gives back
2016 * the hbq buffer to firmware. If the HBQ does not have space to
2017 * post the buffer, it will free the buffer.
2018 **/
ed957684 2019void
51ef4c26 2020lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2021{
2022 uint32_t hbqno;
2023
51ef4c26
JS
2024 if (hbq_buffer) {
2025 hbqno = hbq_buffer->tag >> 16;
3772a991 2026 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2027 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2028 }
2029}
2030
e59058c4 2031/**
3621a710 2032 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2033 * @mbxCommand: mailbox command code.
2034 *
2035 * This function is called by the mailbox event handler function to verify
2036 * that the completed mailbox command is a legitimate mailbox command. If the
2037 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2038 * and the mailbox event handler will take the HBA offline.
2039 **/
dea3101e
JB
2040static int
2041lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2042{
2043 uint8_t ret;
2044
2045 switch (mbxCommand) {
2046 case MBX_LOAD_SM:
2047 case MBX_READ_NV:
2048 case MBX_WRITE_NV:
a8adb832 2049 case MBX_WRITE_VPARMS:
dea3101e
JB
2050 case MBX_RUN_BIU_DIAG:
2051 case MBX_INIT_LINK:
2052 case MBX_DOWN_LINK:
2053 case MBX_CONFIG_LINK:
2054 case MBX_CONFIG_RING:
2055 case MBX_RESET_RING:
2056 case MBX_READ_CONFIG:
2057 case MBX_READ_RCONFIG:
2058 case MBX_READ_SPARM:
2059 case MBX_READ_STATUS:
2060 case MBX_READ_RPI:
2061 case MBX_READ_XRI:
2062 case MBX_READ_REV:
2063 case MBX_READ_LNK_STAT:
2064 case MBX_REG_LOGIN:
2065 case MBX_UNREG_LOGIN:
dea3101e
JB
2066 case MBX_CLEAR_LA:
2067 case MBX_DUMP_MEMORY:
2068 case MBX_DUMP_CONTEXT:
2069 case MBX_RUN_DIAGS:
2070 case MBX_RESTART:
2071 case MBX_UPDATE_CFG:
2072 case MBX_DOWN_LOAD:
2073 case MBX_DEL_LD_ENTRY:
2074 case MBX_RUN_PROGRAM:
2075 case MBX_SET_MASK:
09372820 2076 case MBX_SET_VARIABLE:
dea3101e 2077 case MBX_UNREG_D_ID:
41415862 2078 case MBX_KILL_BOARD:
dea3101e 2079 case MBX_CONFIG_FARP:
41415862 2080 case MBX_BEACON:
dea3101e
JB
2081 case MBX_LOAD_AREA:
2082 case MBX_RUN_BIU_DIAG64:
2083 case MBX_CONFIG_PORT:
2084 case MBX_READ_SPARM64:
2085 case MBX_READ_RPI64:
2086 case MBX_REG_LOGIN64:
76a95d75 2087 case MBX_READ_TOPOLOGY:
09372820 2088 case MBX_WRITE_WWN:
dea3101e
JB
2089 case MBX_SET_DEBUG:
2090 case MBX_LOAD_EXP_ROM:
57127f15 2091 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2092 case MBX_REG_VPI:
2093 case MBX_UNREG_VPI:
858c9f6c 2094 case MBX_HEARTBEAT:
84774a4d
JS
2095 case MBX_PORT_CAPABILITIES:
2096 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2097 case MBX_SLI4_CONFIG:
2098 case MBX_SLI4_REQ_FTRS:
2099 case MBX_REG_FCFI:
2100 case MBX_UNREG_FCFI:
2101 case MBX_REG_VFI:
2102 case MBX_UNREG_VFI:
2103 case MBX_INIT_VPI:
2104 case MBX_INIT_VFI:
2105 case MBX_RESUME_RPI:
c7495937
JS
2106 case MBX_READ_EVENT_LOG_STATUS:
2107 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2108 case MBX_SECURITY_MGMT:
2109 case MBX_AUTH_PORT:
940eb687 2110 case MBX_ACCESS_VDATA:
dea3101e
JB
2111 ret = mbxCommand;
2112 break;
2113 default:
2114 ret = MBX_SHUTDOWN;
2115 break;
2116 }
2e0fef85 2117 return ret;
dea3101e 2118}
e59058c4
JS
2119
2120/**
3621a710 2121 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2122 * @phba: Pointer to HBA context object.
2123 * @pmboxq: Pointer to mailbox command.
2124 *
2125 * This is completion handler function for mailbox commands issued from
2126 * lpfc_sli_issue_mbox_wait function. This function is called by the
2127 * mailbox event handler function with no lock held. This function
2128 * will wake up thread waiting on the wait queue pointed by context1
2129 * of the mailbox.
2130 **/
04c68496 2131void
2e0fef85 2132lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2133{
2134 wait_queue_head_t *pdone_q;
858c9f6c 2135 unsigned long drvr_flag;
dea3101e
JB
2136
2137 /*
2138 * If pdone_q is empty, the driver thread gave up waiting and
2139 * continued running.
2140 */
7054a606 2141 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2142 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2143 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2144 if (pdone_q)
2145 wake_up_interruptible(pdone_q);
858c9f6c 2146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2147 return;
2148}
2149
e59058c4
JS
2150
2151/**
3621a710 2152 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2153 * @phba: Pointer to HBA context object.
2154 * @pmb: Pointer to mailbox object.
2155 *
2156 * This function is the default mailbox completion handler. It
2157 * frees the memory resources associated with the completed mailbox
2158 * command. If the completed command is a REG_LOGIN mailbox command,
2159 * this function will issue a UREG_LOGIN to re-claim the RPI.
2160 **/
dea3101e 2161void
2e0fef85 2162lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2163{
d439d286 2164 struct lpfc_vport *vport = pmb->vport;
dea3101e 2165 struct lpfc_dmabuf *mp;
d439d286 2166 struct lpfc_nodelist *ndlp;
5af5eee7 2167 struct Scsi_Host *shost;
04c68496 2168 uint16_t rpi, vpi;
7054a606
JS
2169 int rc;
2170
dea3101e 2171 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2172
dea3101e
JB
2173 if (mp) {
2174 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2175 kfree(mp);
2176 }
7054a606
JS
2177
2178 /*
2179 * If a REG_LOGIN succeeded after node is destroyed or node
2180 * is in re-discovery driver need to cleanup the RPI.
2181 */
2e0fef85 2182 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2183 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2184 !pmb->u.mb.mbxStatus) {
2185 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2186 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2187 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 2188 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2189 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2190 if (rc != MBX_NOT_FINISHED)
2191 return;
2192 }
2193
695a814e
JS
2194 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2195 !(phba->pport->load_flag & FC_UNLOADING) &&
2196 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2197 shost = lpfc_shost_from_vport(vport);
2198 spin_lock_irq(shost->host_lock);
2199 vport->vpi_state |= LPFC_VPI_REGISTERED;
2200 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2201 spin_unlock_irq(shost->host_lock);
695a814e
JS
2202 }
2203
d439d286
JS
2204 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2205 ndlp = (struct lpfc_nodelist *)pmb->context2;
2206 lpfc_nlp_put(ndlp);
2207 pmb->context2 = NULL;
2208 }
2209
dcf2a4e0
JS
2210 /* Check security permission status on INIT_LINK mailbox command */
2211 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2212 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2213 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2214 "2860 SLI authentication is required "
2215 "for INIT_LINK but has not done yet\n");
2216
04c68496
JS
2217 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2218 lpfc_sli4_mbox_cmd_free(phba, pmb);
2219 else
2220 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e
JB
2221}
2222
e59058c4 2223/**
3621a710 2224 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2225 * @phba: Pointer to HBA context object.
2226 *
2227 * This function is called with no lock held. This function processes all
2228 * the completed mailbox commands and gives it to upper layers. The interrupt
2229 * service routine processes mailbox completion interrupt and adds completed
2230 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2231 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2232 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2233 * function returns the mailbox commands to the upper layer by calling the
2234 * completion handler function of each mailbox.
2235 **/
dea3101e 2236int
2e0fef85 2237lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2238{
92d7f7b0 2239 MAILBOX_t *pmbox;
dea3101e 2240 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2241 int rc;
2242 LIST_HEAD(cmplq);
dea3101e
JB
2243
2244 phba->sli.slistat.mbox_event++;
2245
92d7f7b0
JS
2246 /* Get all completed mailboxe buffers into the cmplq */
2247 spin_lock_irq(&phba->hbalock);
2248 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2249 spin_unlock_irq(&phba->hbalock);
dea3101e 2250
92d7f7b0
JS
2251 /* Get a Mailbox buffer to setup mailbox commands for callback */
2252 do {
2253 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2254 if (pmb == NULL)
2255 break;
2e0fef85 2256
04c68496 2257 pmbox = &pmb->u.mb;
dea3101e 2258
858c9f6c
JS
2259 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2260 if (pmb->vport) {
2261 lpfc_debugfs_disc_trc(pmb->vport,
2262 LPFC_DISC_TRC_MBOX_VPORT,
2263 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2264 (uint32_t)pmbox->mbxCommand,
2265 pmbox->un.varWords[0],
2266 pmbox->un.varWords[1]);
2267 }
2268 else {
2269 lpfc_debugfs_disc_trc(phba->pport,
2270 LPFC_DISC_TRC_MBOX,
2271 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2272 (uint32_t)pmbox->mbxCommand,
2273 pmbox->un.varWords[0],
2274 pmbox->un.varWords[1]);
2275 }
2276 }
2277
dea3101e
JB
2278 /*
2279 * It is a fatal error if unknown mbox command completion.
2280 */
2281 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2282 MBX_SHUTDOWN) {
af901ca1 2283 /* Unknown mailbox command compl */
92d7f7b0 2284 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2285 "(%d):0323 Unknown Mailbox command "
a183a15f 2286 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2287 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2288 pmbox->mbxCommand,
a183a15f
JS
2289 lpfc_sli_config_mbox_subsys_get(phba,
2290 pmb),
2291 lpfc_sli_config_mbox_opcode_get(phba,
2292 pmb));
2e0fef85 2293 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2294 phba->work_hs = HS_FFER3;
2295 lpfc_handle_eratt(phba);
92d7f7b0 2296 continue;
dea3101e
JB
2297 }
2298
dea3101e
JB
2299 if (pmbox->mbxStatus) {
2300 phba->sli.slistat.mbox_stat_err++;
2301 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2302 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2303 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2304 LOG_MBOX | LOG_SLI,
2305 "(%d):0305 Mbox cmd cmpl "
2306 "error - RETRYing Data: x%x "
2307 "(x%x/x%x) x%x x%x x%x\n",
2308 pmb->vport ? pmb->vport->vpi : 0,
2309 pmbox->mbxCommand,
2310 lpfc_sli_config_mbox_subsys_get(phba,
2311 pmb),
2312 lpfc_sli_config_mbox_opcode_get(phba,
2313 pmb),
2314 pmbox->mbxStatus,
2315 pmbox->un.varWords[0],
2316 pmb->vport->port_state);
dea3101e
JB
2317 pmbox->mbxStatus = 0;
2318 pmbox->mbxOwner = OWN_HOST;
dea3101e 2319 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2320 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2321 continue;
dea3101e
JB
2322 }
2323 }
2324
2325 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2326 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2327 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2328 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2329 "x%x x%x x%x\n",
92d7f7b0 2330 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2331 pmbox->mbxCommand,
a183a15f
JS
2332 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2333 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2334 pmb->mbox_cmpl,
2335 *((uint32_t *) pmbox),
2336 pmbox->un.varWords[0],
2337 pmbox->un.varWords[1],
2338 pmbox->un.varWords[2],
2339 pmbox->un.varWords[3],
2340 pmbox->un.varWords[4],
2341 pmbox->un.varWords[5],
2342 pmbox->un.varWords[6],
e74c03c8
JS
2343 pmbox->un.varWords[7],
2344 pmbox->un.varWords[8],
2345 pmbox->un.varWords[9],
2346 pmbox->un.varWords[10]);
dea3101e 2347
92d7f7b0 2348 if (pmb->mbox_cmpl)
dea3101e 2349 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2350 } while (1);
2351 return 0;
2352}
dea3101e 2353
e59058c4 2354/**
3621a710 2355 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2356 * @phba: Pointer to HBA context object.
2357 * @pring: Pointer to driver SLI ring object.
2358 * @tag: buffer tag.
2359 *
2360 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2361 * is set in the tag the buffer is posted for a particular exchange,
2362 * the function will return the buffer without replacing the buffer.
2363 * If the buffer is for unsolicited ELS or CT traffic, this function
2364 * returns the buffer and also posts another buffer to the firmware.
2365 **/
76bb24ef
JS
2366static struct lpfc_dmabuf *
2367lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2368 struct lpfc_sli_ring *pring,
2369 uint32_t tag)
76bb24ef 2370{
9f1e1b50
JS
2371 struct hbq_dmabuf *hbq_entry;
2372
76bb24ef
JS
2373 if (tag & QUE_BUFTAG_BIT)
2374 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2375 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2376 if (!hbq_entry)
2377 return NULL;
2378 return &hbq_entry->dbuf;
76bb24ef 2379}
57127f15 2380
3772a991
JS
2381/**
2382 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2383 * @phba: Pointer to HBA context object.
2384 * @pring: Pointer to driver SLI ring object.
2385 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2386 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2387 * @fch_type: the type for the first frame of the sequence.
2388 *
2389 * This function is called with no lock held. This function uses the r_ctl and
2390 * type of the received sequence to find the correct callback function to call
2391 * to process the sequence.
2392 **/
2393static int
2394lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2395 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2396 uint32_t fch_type)
2397{
2398 int i;
2399
2400 /* unSolicited Responses */
2401 if (pring->prt[0].profile) {
2402 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2403 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2404 saveq);
2405 return 1;
2406 }
2407 /* We must search, based on rctl / type
2408 for the right routine */
2409 for (i = 0; i < pring->num_mask; i++) {
2410 if ((pring->prt[i].rctl == fch_r_ctl) &&
2411 (pring->prt[i].type == fch_type)) {
2412 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2413 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2414 (phba, pring, saveq);
2415 return 1;
2416 }
2417 }
2418 return 0;
2419}
e59058c4
JS
2420
2421/**
3621a710 2422 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2423 * @phba: Pointer to HBA context object.
2424 * @pring: Pointer to driver SLI ring object.
2425 * @saveq: Pointer to the unsolicited iocb.
2426 *
2427 * This function is called with no lock held by the ring event handler
2428 * when there is an unsolicited iocb posted to the response ring by the
2429 * firmware. This function gets the buffer associated with the iocbs
2430 * and calls the event handler for the ring. This function handles both
2431 * qring buffers and hbq buffers.
2432 * When the function returns 1 the caller can free the iocb object otherwise
2433 * upper layer functions will free the iocb objects.
2434 **/
dea3101e
JB
2435static int
2436lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2437 struct lpfc_iocbq *saveq)
2438{
2439 IOCB_t * irsp;
2440 WORD5 * w5p;
2441 uint32_t Rctl, Type;
3772a991 2442 uint32_t match;
76bb24ef 2443 struct lpfc_iocbq *iocbq;
3163f725 2444 struct lpfc_dmabuf *dmzbuf;
dea3101e
JB
2445
2446 match = 0;
2447 irsp = &(saveq->iocb);
57127f15
JS
2448
2449 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2450 if (pring->lpfc_sli_rcv_async_status)
2451 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2452 else
2453 lpfc_printf_log(phba,
2454 KERN_WARNING,
2455 LOG_SLI,
2456 "0316 Ring %d handler: unexpected "
2457 "ASYNC_STATUS iocb received evt_code "
2458 "0x%x\n",
2459 pring->ringno,
2460 irsp->un.asyncstat.evt_code);
2461 return 1;
2462 }
2463
3163f725
JS
2464 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2465 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2466 if (irsp->ulpBdeCount > 0) {
2467 dmzbuf = lpfc_sli_get_buff(phba, pring,
2468 irsp->un.ulpWord[3]);
2469 lpfc_in_buf_free(phba, dmzbuf);
2470 }
2471
2472 if (irsp->ulpBdeCount > 1) {
2473 dmzbuf = lpfc_sli_get_buff(phba, pring,
2474 irsp->unsli3.sli3Words[3]);
2475 lpfc_in_buf_free(phba, dmzbuf);
2476 }
2477
2478 if (irsp->ulpBdeCount > 2) {
2479 dmzbuf = lpfc_sli_get_buff(phba, pring,
2480 irsp->unsli3.sli3Words[7]);
2481 lpfc_in_buf_free(phba, dmzbuf);
2482 }
2483
2484 return 1;
2485 }
2486
92d7f7b0 2487 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2488 if (irsp->ulpBdeCount != 0) {
2489 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2490 irsp->un.ulpWord[3]);
2491 if (!saveq->context2)
2492 lpfc_printf_log(phba,
2493 KERN_ERR,
2494 LOG_SLI,
2495 "0341 Ring %d Cannot find buffer for "
2496 "an unsolicited iocb. tag 0x%x\n",
2497 pring->ringno,
2498 irsp->un.ulpWord[3]);
76bb24ef
JS
2499 }
2500 if (irsp->ulpBdeCount == 2) {
2501 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2502 irsp->unsli3.sli3Words[7]);
2503 if (!saveq->context3)
2504 lpfc_printf_log(phba,
2505 KERN_ERR,
2506 LOG_SLI,
2507 "0342 Ring %d Cannot find buffer for an"
2508 " unsolicited iocb. tag 0x%x\n",
2509 pring->ringno,
2510 irsp->unsli3.sli3Words[7]);
2511 }
2512 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2513 irsp = &(iocbq->iocb);
76bb24ef
JS
2514 if (irsp->ulpBdeCount != 0) {
2515 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2516 irsp->un.ulpWord[3]);
9c2face6 2517 if (!iocbq->context2)
76bb24ef
JS
2518 lpfc_printf_log(phba,
2519 KERN_ERR,
2520 LOG_SLI,
2521 "0343 Ring %d Cannot find "
2522 "buffer for an unsolicited iocb"
2523 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2524 irsp->un.ulpWord[3]);
76bb24ef
JS
2525 }
2526 if (irsp->ulpBdeCount == 2) {
2527 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2528 irsp->unsli3.sli3Words[7]);
9c2face6 2529 if (!iocbq->context3)
76bb24ef
JS
2530 lpfc_printf_log(phba,
2531 KERN_ERR,
2532 LOG_SLI,
2533 "0344 Ring %d Cannot find "
2534 "buffer for an unsolicited "
2535 "iocb. tag 0x%x\n",
2536 pring->ringno,
2537 irsp->unsli3.sli3Words[7]);
2538 }
2539 }
92d7f7b0 2540 }
9c2face6
JS
2541 if (irsp->ulpBdeCount != 0 &&
2542 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2543 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2544 int found = 0;
2545
2546 /* search continue save q for same XRI */
2547 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2548 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2549 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2550 list_add_tail(&saveq->list, &iocbq->list);
2551 found = 1;
2552 break;
2553 }
2554 }
2555 if (!found)
2556 list_add_tail(&saveq->clist,
2557 &pring->iocb_continue_saveq);
2558 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2559 list_del_init(&iocbq->clist);
2560 saveq = iocbq;
2561 irsp = &(saveq->iocb);
2562 } else
2563 return 0;
2564 }
2565 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2566 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2567 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2568 Rctl = FC_RCTL_ELS_REQ;
2569 Type = FC_TYPE_ELS;
9c2face6
JS
2570 } else {
2571 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2572 Rctl = w5p->hcsw.Rctl;
2573 Type = w5p->hcsw.Type;
2574
2575 /* Firmware Workaround */
2576 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2577 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2578 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2579 Rctl = FC_RCTL_ELS_REQ;
2580 Type = FC_TYPE_ELS;
9c2face6
JS
2581 w5p->hcsw.Rctl = Rctl;
2582 w5p->hcsw.Type = Type;
2583 }
2584 }
92d7f7b0 2585
3772a991 2586 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2588 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2589 "Type x%x received\n",
e8b62011 2590 pring->ringno, Rctl, Type);
3772a991 2591
92d7f7b0 2592 return 1;
dea3101e
JB
2593}
2594
e59058c4 2595/**
3621a710 2596 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2597 * @phba: Pointer to HBA context object.
2598 * @pring: Pointer to driver SLI ring object.
2599 * @prspiocb: Pointer to response iocb object.
2600 *
2601 * This function looks up the iocb_lookup table to get the command iocb
2602 * corresponding to the given response iocb using the iotag of the
2603 * response iocb. This function is called with the hbalock held.
2604 * This function returns the command iocb object if it finds the command
2605 * iocb else returns NULL.
2606 **/
dea3101e 2607static struct lpfc_iocbq *
2e0fef85
JS
2608lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2609 struct lpfc_sli_ring *pring,
2610 struct lpfc_iocbq *prspiocb)
dea3101e 2611{
dea3101e
JB
2612 struct lpfc_iocbq *cmd_iocb = NULL;
2613 uint16_t iotag;
2614
604a3e30
JB
2615 iotag = prspiocb->iocb.ulpIoTag;
2616
2617 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2618 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2619 list_del_init(&cmd_iocb->list);
4f2e66c6 2620 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
4f2e66c6 2621 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 2622 }
604a3e30 2623 return cmd_iocb;
dea3101e
JB
2624 }
2625
dea3101e 2626 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2627 "0317 iotag x%x is out off "
604a3e30 2628 "range: max iotag x%x wd0 x%x\n",
e8b62011 2629 iotag, phba->sli.last_iotag,
604a3e30 2630 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2631 return NULL;
2632}
2633
3772a991
JS
2634/**
2635 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2636 * @phba: Pointer to HBA context object.
2637 * @pring: Pointer to driver SLI ring object.
2638 * @iotag: IOCB tag.
2639 *
2640 * This function looks up the iocb_lookup table to get the command iocb
2641 * corresponding to the given iotag. This function is called with the
2642 * hbalock held.
2643 * This function returns the command iocb object if it finds the command
2644 * iocb else returns NULL.
2645 **/
2646static struct lpfc_iocbq *
2647lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2648 struct lpfc_sli_ring *pring, uint16_t iotag)
2649{
2650 struct lpfc_iocbq *cmd_iocb;
2651
2652 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2653 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2654 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2655 /* remove from txcmpl queue list */
2656 list_del_init(&cmd_iocb->list);
2657 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2658 return cmd_iocb;
2a9bf3d0 2659 }
3772a991 2660 }
3772a991
JS
2661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2662 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2663 iotag, phba->sli.last_iotag);
2664 return NULL;
2665}
2666
e59058c4 2667/**
3621a710 2668 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2669 * @phba: Pointer to HBA context object.
2670 * @pring: Pointer to driver SLI ring object.
2671 * @saveq: Pointer to the response iocb to be processed.
2672 *
2673 * This function is called by the ring event handler for non-fcp
2674 * rings when there is a new response iocb in the response ring.
2675 * The caller is not required to hold any locks. This function
2676 * gets the command iocb associated with the response iocb and
2677 * calls the completion handler for the command iocb. If there
2678 * is no completion handler, the function will free the resources
2679 * associated with command iocb. If the response iocb is for
2680 * an already aborted command iocb, the status of the completion
2681 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2682 * This function always returns 1.
2683 **/
dea3101e 2684static int
2e0fef85 2685lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2686 struct lpfc_iocbq *saveq)
2687{
2e0fef85 2688 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2689 int rc = 1;
2690 unsigned long iflag;
2691
2692 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2693 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2694 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2695 spin_unlock_irqrestore(&phba->hbalock, iflag);
2696
dea3101e
JB
2697 if (cmdiocbp) {
2698 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2699 /*
2700 * If an ELS command failed send an event to mgmt
2701 * application.
2702 */
2703 if (saveq->iocb.ulpStatus &&
2704 (pring->ringno == LPFC_ELS_RING) &&
2705 (cmdiocbp->iocb.ulpCommand ==
2706 CMD_ELS_REQUEST64_CR))
2707 lpfc_send_els_failure_event(phba,
2708 cmdiocbp, saveq);
2709
dea3101e
JB
2710 /*
2711 * Post all ELS completions to the worker thread.
2712 * All other are passed to the completion callback.
2713 */
2714 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2715 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2716 (cmdiocbp->iocb_flag &
2717 LPFC_DRIVER_ABORTED)) {
2718 spin_lock_irqsave(&phba->hbalock,
2719 iflag);
07951076
JS
2720 cmdiocbp->iocb_flag &=
2721 ~LPFC_DRIVER_ABORTED;
341af102
JS
2722 spin_unlock_irqrestore(&phba->hbalock,
2723 iflag);
07951076
JS
2724 saveq->iocb.ulpStatus =
2725 IOSTAT_LOCAL_REJECT;
2726 saveq->iocb.un.ulpWord[4] =
2727 IOERR_SLI_ABORTED;
0ff10d46
JS
2728
2729 /* Firmware could still be in progress
2730 * of DMAing payload, so don't free data
2731 * buffer till after a hbeat.
2732 */
341af102
JS
2733 spin_lock_irqsave(&phba->hbalock,
2734 iflag);
0ff10d46 2735 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2736 spin_unlock_irqrestore(&phba->hbalock,
2737 iflag);
2738 }
0f65ff68
JS
2739 if (phba->sli_rev == LPFC_SLI_REV4) {
2740 if (saveq->iocb_flag &
2741 LPFC_EXCHANGE_BUSY) {
2742 /* Set cmdiocb flag for the
2743 * exchange busy so sgl (xri)
2744 * will not be released until
2745 * the abort xri is received
2746 * from hba.
2747 */
2748 spin_lock_irqsave(
2749 &phba->hbalock, iflag);
2750 cmdiocbp->iocb_flag |=
2751 LPFC_EXCHANGE_BUSY;
2752 spin_unlock_irqrestore(
2753 &phba->hbalock, iflag);
2754 }
2755 if (cmdiocbp->iocb_flag &
2756 LPFC_DRIVER_ABORTED) {
2757 /*
2758 * Clear LPFC_DRIVER_ABORTED
2759 * bit in case it was driver
2760 * initiated abort.
2761 */
2762 spin_lock_irqsave(
2763 &phba->hbalock, iflag);
2764 cmdiocbp->iocb_flag &=
2765 ~LPFC_DRIVER_ABORTED;
2766 spin_unlock_irqrestore(
2767 &phba->hbalock, iflag);
2768 cmdiocbp->iocb.ulpStatus =
2769 IOSTAT_LOCAL_REJECT;
2770 cmdiocbp->iocb.un.ulpWord[4] =
2771 IOERR_ABORT_REQUESTED;
2772 /*
2773 * For SLI4, irsiocb contains
2774 * NO_XRI in sli_xritag, it
2775 * shall not affect releasing
2776 * sgl (xri) process.
2777 */
2778 saveq->iocb.ulpStatus =
2779 IOSTAT_LOCAL_REJECT;
2780 saveq->iocb.un.ulpWord[4] =
2781 IOERR_SLI_ABORTED;
2782 spin_lock_irqsave(
2783 &phba->hbalock, iflag);
2784 saveq->iocb_flag |=
2785 LPFC_DELAY_MEM_FREE;
2786 spin_unlock_irqrestore(
2787 &phba->hbalock, iflag);
2788 }
07951076 2789 }
dea3101e 2790 }
2e0fef85 2791 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2792 } else
2793 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2794 } else {
2795 /*
2796 * Unknown initiating command based on the response iotag.
2797 * This could be the case on the ELS ring because of
2798 * lpfc_els_abort().
2799 */
2800 if (pring->ringno != LPFC_ELS_RING) {
2801 /*
2802 * Ring <ringno> handler: unexpected completion IoTag
2803 * <IoTag>
2804 */
a257bf90 2805 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2806 "0322 Ring %d handler: "
2807 "unexpected completion IoTag x%x "
2808 "Data: x%x x%x x%x x%x\n",
2809 pring->ringno,
2810 saveq->iocb.ulpIoTag,
2811 saveq->iocb.ulpStatus,
2812 saveq->iocb.un.ulpWord[4],
2813 saveq->iocb.ulpCommand,
2814 saveq->iocb.ulpContext);
dea3101e
JB
2815 }
2816 }
68876920 2817
dea3101e
JB
2818 return rc;
2819}
2820
e59058c4 2821/**
3621a710 2822 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2823 * @phba: Pointer to HBA context object.
2824 * @pring: Pointer to driver SLI ring object.
2825 *
2826 * This function is called from the iocb ring event handlers when
2827 * put pointer is ahead of the get pointer for a ring. This function signal
2828 * an error attention condition to the worker thread and the worker
2829 * thread will transition the HBA to offline state.
2830 **/
2e0fef85
JS
2831static void
2832lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2833{
34b02dcd 2834 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2835 /*
025dfdaf 2836 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2837 * rsp ring <portRspMax>
2838 */
2839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2840 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2841 "is bigger than rsp ring %d\n",
e8b62011 2842 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2843 pring->sli.sli3.numRiocb);
875fbdfe 2844
2e0fef85 2845 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2846
2847 /*
2848 * All error attention handlers are posted to
2849 * worker thread
2850 */
2851 phba->work_ha |= HA_ERATT;
2852 phba->work_hs = HS_FFER3;
92d7f7b0 2853
5e9d9b82 2854 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2855
2856 return;
2857}
2858
9399627f 2859/**
3621a710 2860 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2861 * @ptr: Pointer to address of HBA context object.
2862 *
2863 * This function is invoked by the Error Attention polling timer when the
2864 * timer times out. It will check the SLI Error Attention register for
2865 * possible attention events. If so, it will post an Error Attention event
2866 * and wake up worker thread to process it. Otherwise, it will set up the
2867 * Error Attention polling timer for the next poll.
2868 **/
2869void lpfc_poll_eratt(unsigned long ptr)
2870{
2871 struct lpfc_hba *phba;
aa6fbb75
JS
2872 uint32_t eratt = 0, rem;
2873 uint64_t sli_intr, cnt;
9399627f
JS
2874
2875 phba = (struct lpfc_hba *)ptr;
2876
aa6fbb75
JS
2877 /* Here we will also keep track of interrupts per sec of the hba */
2878 sli_intr = phba->sli.slistat.sli_intr;
2879
2880 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2881 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2882 sli_intr);
2883 else
2884 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2885
2886 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2887 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2888 phba->sli.slistat.sli_ips = cnt;
2889
2890 phba->sli.slistat.sli_prev_intr = sli_intr;
2891
9399627f
JS
2892 /* Check chip HA register for error event */
2893 eratt = lpfc_sli_check_eratt(phba);
2894
2895 if (eratt)
2896 /* Tell the worker thread there is work to do */
2897 lpfc_worker_wake_up(phba);
2898 else
2899 /* Restart the timer for next eratt poll */
256ec0d0
JS
2900 mod_timer(&phba->eratt_poll,
2901 jiffies +
2902 msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
9399627f
JS
2903 return;
2904}
2905
875fbdfe 2906
e59058c4 2907/**
3621a710 2908 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2909 * @phba: Pointer to HBA context object.
2910 * @pring: Pointer to driver SLI ring object.
2911 * @mask: Host attention register mask for this ring.
2912 *
2913 * This function is called from the interrupt context when there is a ring
2914 * event for the fcp ring. The caller does not hold any lock.
2915 * The function processes each response iocb in the response ring until it
25985edc 2916 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
2917 * LE bit set. The function will call the completion handler of the command iocb
2918 * if the response iocb indicates a completion for a command iocb or it is
2919 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2920 * function if this is an unsolicited iocb.
dea3101e 2921 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2922 * to check it explicitly.
2923 */
2924int
2e0fef85
JS
2925lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2926 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2927{
34b02dcd 2928 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2929 IOCB_t *irsp = NULL;
87f6eaff 2930 IOCB_t *entry = NULL;
dea3101e
JB
2931 struct lpfc_iocbq *cmdiocbq = NULL;
2932 struct lpfc_iocbq rspiocbq;
dea3101e
JB
2933 uint32_t status;
2934 uint32_t portRspPut, portRspMax;
2935 int rc = 1;
2936 lpfc_iocb_type type;
2937 unsigned long iflag;
2938 uint32_t rsp_cmpl = 0;
dea3101e 2939
2e0fef85 2940 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
2941 pring->stats.iocb_event++;
2942
dea3101e
JB
2943 /*
2944 * The next available response entry should never exceed the maximum
2945 * entries. If it does, treat it as an adapter hardware error.
2946 */
7e56aa25 2947 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
2948 portRspPut = le32_to_cpu(pgp->rspPutInx);
2949 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2950 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2951 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
2952 return 1;
2953 }
45ed1190
JS
2954 if (phba->fcp_ring_in_use) {
2955 spin_unlock_irqrestore(&phba->hbalock, iflag);
2956 return 1;
2957 } else
2958 phba->fcp_ring_in_use = 1;
dea3101e
JB
2959
2960 rmb();
7e56aa25 2961 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
2962 /*
2963 * Fetch an entry off the ring and copy it into a local data
2964 * structure. The copy involves a byte-swap since the
2965 * network byte order and pci byte orders are different.
2966 */
ed957684 2967 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2968 phba->last_completion_time = jiffies;
875fbdfe 2969
7e56aa25
JS
2970 if (++pring->sli.sli3.rspidx >= portRspMax)
2971 pring->sli.sli3.rspidx = 0;
875fbdfe 2972
87f6eaff
JSEC
2973 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2974 (uint32_t *) &rspiocbq.iocb,
ed957684 2975 phba->iocb_rsp_size);
a4bc3379 2976 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2977 irsp = &rspiocbq.iocb;
2978
dea3101e
JB
2979 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2980 pring->stats.iocb_rsp++;
2981 rsp_cmpl++;
2982
2983 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2984 /*
2985 * If resource errors reported from HBA, reduce
2986 * queuedepths of the SCSI device.
2987 */
2988 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
2989 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
2990 IOERR_NO_RESOURCES)) {
92d7f7b0 2991 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2992 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2993 spin_lock_irqsave(&phba->hbalock, iflag);
2994 }
2995
dea3101e
JB
2996 /* Rsp ring <ringno> error: IOCB */
2997 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2998 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2999 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3000 pring->ringno,
92d7f7b0
JS
3001 irsp->un.ulpWord[0],
3002 irsp->un.ulpWord[1],
3003 irsp->un.ulpWord[2],
3004 irsp->un.ulpWord[3],
3005 irsp->un.ulpWord[4],
3006 irsp->un.ulpWord[5],
d7c255b2
JS
3007 *(uint32_t *)&irsp->un1,
3008 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3009 }
3010
3011 switch (type) {
3012 case LPFC_ABORT_IOCB:
3013 case LPFC_SOL_IOCB:
3014 /*
3015 * Idle exchange closed via ABTS from port. No iocb
3016 * resources need to be recovered.
3017 */
3018 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3019 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3020 "0333 IOCB cmd 0x%x"
dca9479b 3021 " processed. Skipping"
92d7f7b0 3022 " completion\n",
dca9479b 3023 irsp->ulpCommand);
dea3101e
JB
3024 break;
3025 }
3026
604a3e30
JB
3027 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3028 &rspiocbq);
0f65ff68
JS
3029 if (unlikely(!cmdiocbq))
3030 break;
3031 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3032 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3033 if (cmdiocbq->iocb_cmpl) {
3034 spin_unlock_irqrestore(&phba->hbalock, iflag);
3035 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3036 &rspiocbq);
3037 spin_lock_irqsave(&phba->hbalock, iflag);
3038 }
dea3101e 3039 break;
a4bc3379 3040 case LPFC_UNSOL_IOCB:
2e0fef85 3041 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3042 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3043 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3044 break;
dea3101e
JB
3045 default:
3046 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3047 char adaptermsg[LPFC_MAX_ADPTMSG];
3048 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3049 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3050 MAX_MSG_DATA);
898eb71c
JP
3051 dev_warn(&((phba->pcidev)->dev),
3052 "lpfc%d: %s\n",
dea3101e
JB
3053 phba->brd_no, adaptermsg);
3054 } else {
3055 /* Unknown IOCB command */
3056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3057 "0334 Unknown IOCB command "
92d7f7b0 3058 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3059 type, irsp->ulpCommand,
92d7f7b0
JS
3060 irsp->ulpStatus,
3061 irsp->ulpIoTag,
3062 irsp->ulpContext);
dea3101e
JB
3063 }
3064 break;
3065 }
3066
3067 /*
3068 * The response IOCB has been processed. Update the ring
3069 * pointer in SLIM. If the port response put pointer has not
3070 * been updated, sync the pgp->rspPutInx and fetch the new port
3071 * response put pointer.
3072 */
7e56aa25
JS
3073 writel(pring->sli.sli3.rspidx,
3074 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3075
7e56aa25 3076 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3077 portRspPut = le32_to_cpu(pgp->rspPutInx);
3078 }
3079
3080 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3081 pring->stats.iocb_rsp_full++;
3082 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3083 writel(status, phba->CAregaddr);
3084 readl(phba->CAregaddr);
3085 }
3086 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3087 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3088 pring->stats.iocb_cmd_empty++;
3089
3090 /* Force update of the local copy of cmdGetInx */
7e56aa25 3091 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3092 lpfc_sli_resume_iocb(phba, pring);
3093
3094 if ((pring->lpfc_sli_cmd_available))
3095 (pring->lpfc_sli_cmd_available) (phba, pring);
3096
3097 }
3098
45ed1190 3099 phba->fcp_ring_in_use = 0;
2e0fef85 3100 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3101 return rc;
3102}
3103
e59058c4 3104/**
3772a991
JS
3105 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3106 * @phba: Pointer to HBA context object.
3107 * @pring: Pointer to driver SLI ring object.
3108 * @rspiocbp: Pointer to driver response IOCB object.
3109 *
3110 * This function is called from the worker thread when there is a slow-path
3111 * response IOCB to process. This function chains all the response iocbs until
3112 * seeing the iocb with the LE bit set. The function will call
3113 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3114 * completion of a command iocb. The function will call the
3115 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3116 * The function frees the resources or calls the completion handler if this
3117 * iocb is an abort completion. The function returns NULL when the response
3118 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3119 * this function shall chain the iocb on to the iocb_continueq and return the
3120 * response iocb passed in.
3121 **/
3122static struct lpfc_iocbq *
3123lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3124 struct lpfc_iocbq *rspiocbp)
3125{
3126 struct lpfc_iocbq *saveq;
3127 struct lpfc_iocbq *cmdiocbp;
3128 struct lpfc_iocbq *next_iocb;
3129 IOCB_t *irsp = NULL;
3130 uint32_t free_saveq;
3131 uint8_t iocb_cmd_type;
3132 lpfc_iocb_type type;
3133 unsigned long iflag;
3134 int rc;
3135
3136 spin_lock_irqsave(&phba->hbalock, iflag);
3137 /* First add the response iocb to the countinueq list */
3138 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3139 pring->iocb_continueq_cnt++;
3140
70f23fd6 3141 /* Now, determine whether the list is completed for processing */
3772a991
JS
3142 irsp = &rspiocbp->iocb;
3143 if (irsp->ulpLe) {
3144 /*
3145 * By default, the driver expects to free all resources
3146 * associated with this iocb completion.
3147 */
3148 free_saveq = 1;
3149 saveq = list_get_first(&pring->iocb_continueq,
3150 struct lpfc_iocbq, list);
3151 irsp = &(saveq->iocb);
3152 list_del_init(&pring->iocb_continueq);
3153 pring->iocb_continueq_cnt = 0;
3154
3155 pring->stats.iocb_rsp++;
3156
3157 /*
3158 * If resource errors reported from HBA, reduce
3159 * queuedepths of the SCSI device.
3160 */
3161 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3162 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3163 IOERR_NO_RESOURCES)) {
3772a991
JS
3164 spin_unlock_irqrestore(&phba->hbalock, iflag);
3165 phba->lpfc_rampdown_queue_depth(phba);
3166 spin_lock_irqsave(&phba->hbalock, iflag);
3167 }
3168
3169 if (irsp->ulpStatus) {
3170 /* Rsp ring <ringno> error: IOCB */
3171 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3172 "0328 Rsp Ring %d error: "
3173 "IOCB Data: "
3174 "x%x x%x x%x x%x "
3175 "x%x x%x x%x x%x "
3176 "x%x x%x x%x x%x "
3177 "x%x x%x x%x x%x\n",
3178 pring->ringno,
3179 irsp->un.ulpWord[0],
3180 irsp->un.ulpWord[1],
3181 irsp->un.ulpWord[2],
3182 irsp->un.ulpWord[3],
3183 irsp->un.ulpWord[4],
3184 irsp->un.ulpWord[5],
3185 *(((uint32_t *) irsp) + 6),
3186 *(((uint32_t *) irsp) + 7),
3187 *(((uint32_t *) irsp) + 8),
3188 *(((uint32_t *) irsp) + 9),
3189 *(((uint32_t *) irsp) + 10),
3190 *(((uint32_t *) irsp) + 11),
3191 *(((uint32_t *) irsp) + 12),
3192 *(((uint32_t *) irsp) + 13),
3193 *(((uint32_t *) irsp) + 14),
3194 *(((uint32_t *) irsp) + 15));
3195 }
3196
3197 /*
3198 * Fetch the IOCB command type and call the correct completion
3199 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3200 * get freed back to the lpfc_iocb_list by the discovery
3201 * kernel thread.
3202 */
3203 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3204 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3205 switch (type) {
3206 case LPFC_SOL_IOCB:
3207 spin_unlock_irqrestore(&phba->hbalock, iflag);
3208 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3209 spin_lock_irqsave(&phba->hbalock, iflag);
3210 break;
3211
3212 case LPFC_UNSOL_IOCB:
3213 spin_unlock_irqrestore(&phba->hbalock, iflag);
3214 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3215 spin_lock_irqsave(&phba->hbalock, iflag);
3216 if (!rc)
3217 free_saveq = 0;
3218 break;
3219
3220 case LPFC_ABORT_IOCB:
3221 cmdiocbp = NULL;
3222 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3223 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3224 saveq);
3225 if (cmdiocbp) {
3226 /* Call the specified completion routine */
3227 if (cmdiocbp->iocb_cmpl) {
3228 spin_unlock_irqrestore(&phba->hbalock,
3229 iflag);
3230 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3231 saveq);
3232 spin_lock_irqsave(&phba->hbalock,
3233 iflag);
3234 } else
3235 __lpfc_sli_release_iocbq(phba,
3236 cmdiocbp);
3237 }
3238 break;
3239
3240 case LPFC_UNKNOWN_IOCB:
3241 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3242 char adaptermsg[LPFC_MAX_ADPTMSG];
3243 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3244 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3245 MAX_MSG_DATA);
3246 dev_warn(&((phba->pcidev)->dev),
3247 "lpfc%d: %s\n",
3248 phba->brd_no, adaptermsg);
3249 } else {
3250 /* Unknown IOCB command */
3251 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3252 "0335 Unknown IOCB "
3253 "command Data: x%x "
3254 "x%x x%x x%x\n",
3255 irsp->ulpCommand,
3256 irsp->ulpStatus,
3257 irsp->ulpIoTag,
3258 irsp->ulpContext);
3259 }
3260 break;
3261 }
3262
3263 if (free_saveq) {
3264 list_for_each_entry_safe(rspiocbp, next_iocb,
3265 &saveq->list, list) {
61f35bff 3266 list_del_init(&rspiocbp->list);
3772a991
JS
3267 __lpfc_sli_release_iocbq(phba, rspiocbp);
3268 }
3269 __lpfc_sli_release_iocbq(phba, saveq);
3270 }
3271 rspiocbp = NULL;
3272 }
3273 spin_unlock_irqrestore(&phba->hbalock, iflag);
3274 return rspiocbp;
3275}
3276
3277/**
3278 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3279 * @phba: Pointer to HBA context object.
3280 * @pring: Pointer to driver SLI ring object.
3281 * @mask: Host attention register mask for this ring.
3282 *
3772a991
JS
3283 * This routine wraps the actual slow_ring event process routine from the
3284 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3285 **/
3772a991 3286void
2e0fef85
JS
3287lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3288 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3289{
3290 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3291}
3292
3293/**
3294 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3295 * @phba: Pointer to HBA context object.
3296 * @pring: Pointer to driver SLI ring object.
3297 * @mask: Host attention register mask for this ring.
3298 *
3299 * This function is called from the worker thread when there is a ring event
3300 * for non-fcp rings. The caller does not hold any lock. The function will
3301 * remove each response iocb in the response ring and calls the handle
3302 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3303 **/
3304static void
3305lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3306 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3307{
34b02dcd 3308 struct lpfc_pgp *pgp;
dea3101e
JB
3309 IOCB_t *entry;
3310 IOCB_t *irsp = NULL;
3311 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3312 uint32_t portRspPut, portRspMax;
dea3101e 3313 unsigned long iflag;
3772a991 3314 uint32_t status;
dea3101e 3315
34b02dcd 3316 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3317 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3318 pring->stats.iocb_event++;
3319
dea3101e
JB
3320 /*
3321 * The next available response entry should never exceed the maximum
3322 * entries. If it does, treat it as an adapter hardware error.
3323 */
7e56aa25 3324 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3325 portRspPut = le32_to_cpu(pgp->rspPutInx);
3326 if (portRspPut >= portRspMax) {
3327 /*
025dfdaf 3328 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3329 * rsp ring <portRspMax>
3330 */
ed957684 3331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3332 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3333 "is bigger than rsp ring %d\n",
e8b62011 3334 pring->ringno, portRspPut, portRspMax);
dea3101e 3335
2e0fef85
JS
3336 phba->link_state = LPFC_HBA_ERROR;
3337 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3338
3339 phba->work_hs = HS_FFER3;
3340 lpfc_handle_eratt(phba);
3341
3772a991 3342 return;
dea3101e
JB
3343 }
3344
3345 rmb();
7e56aa25 3346 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3347 /*
3348 * Build a completion list and call the appropriate handler.
3349 * The process is to get the next available response iocb, get
3350 * a free iocb from the list, copy the response data into the
3351 * free iocb, insert to the continuation list, and update the
3352 * next response index to slim. This process makes response
3353 * iocb's in the ring available to DMA as fast as possible but
3354 * pays a penalty for a copy operation. Since the iocb is
3355 * only 32 bytes, this penalty is considered small relative to
3356 * the PCI reads for register values and a slim write. When
3357 * the ulpLe field is set, the entire Command has been
3358 * received.
3359 */
ed957684
JS
3360 entry = lpfc_resp_iocb(phba, pring);
3361
858c9f6c 3362 phba->last_completion_time = jiffies;
2e0fef85 3363 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3364 if (rspiocbp == NULL) {
3365 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3366 "completion.\n", __func__);
dea3101e
JB
3367 break;
3368 }
3369
ed957684
JS
3370 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3371 phba->iocb_rsp_size);
dea3101e
JB
3372 irsp = &rspiocbp->iocb;
3373
7e56aa25
JS
3374 if (++pring->sli.sli3.rspidx >= portRspMax)
3375 pring->sli.sli3.rspidx = 0;
dea3101e 3376
a58cbd52
JS
3377 if (pring->ringno == LPFC_ELS_RING) {
3378 lpfc_debugfs_slow_ring_trc(phba,
3379 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3380 *(((uint32_t *) irsp) + 4),
3381 *(((uint32_t *) irsp) + 6),
3382 *(((uint32_t *) irsp) + 7));
3383 }
3384
7e56aa25
JS
3385 writel(pring->sli.sli3.rspidx,
3386 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3387
3772a991
JS
3388 spin_unlock_irqrestore(&phba->hbalock, iflag);
3389 /* Handle the response IOCB */
3390 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3391 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3392
3393 /*
3394 * If the port response put pointer has not been updated, sync
3395 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3396 * response put pointer.
3397 */
7e56aa25 3398 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3399 portRspPut = le32_to_cpu(pgp->rspPutInx);
3400 }
7e56aa25 3401 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3402
92d7f7b0 3403 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3404 /* At least one response entry has been freed */
3405 pring->stats.iocb_rsp_full++;
3406 /* SET RxRE_RSP in Chip Att register */
3407 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3408 writel(status, phba->CAregaddr);
3409 readl(phba->CAregaddr); /* flush */
3410 }
3411 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3412 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3413 pring->stats.iocb_cmd_empty++;
3414
3415 /* Force update of the local copy of cmdGetInx */
7e56aa25 3416 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3417 lpfc_sli_resume_iocb(phba, pring);
3418
3419 if ((pring->lpfc_sli_cmd_available))
3420 (pring->lpfc_sli_cmd_available) (phba, pring);
3421
3422 }
3423
2e0fef85 3424 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3425 return;
dea3101e
JB
3426}
3427
4f774513
JS
3428/**
3429 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3430 * @phba: Pointer to HBA context object.
3431 * @pring: Pointer to driver SLI ring object.
3432 * @mask: Host attention register mask for this ring.
3433 *
3434 * This function is called from the worker thread when there is a pending
3435 * ELS response iocb on the driver internal slow-path response iocb worker
3436 * queue. The caller does not hold any lock. The function will remove each
3437 * response iocb from the response worker queue and calls the handle
3438 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3439 **/
3440static void
3441lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3442 struct lpfc_sli_ring *pring, uint32_t mask)
3443{
3444 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3445 struct hbq_dmabuf *dmabuf;
3446 struct lpfc_cq_event *cq_event;
4f774513
JS
3447 unsigned long iflag;
3448
45ed1190
JS
3449 spin_lock_irqsave(&phba->hbalock, iflag);
3450 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3451 spin_unlock_irqrestore(&phba->hbalock, iflag);
3452 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3453 /* Get the response iocb from the head of work queue */
3454 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3455 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3456 cq_event, struct lpfc_cq_event, list);
4f774513 3457 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3458
3459 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3460 case CQE_CODE_COMPL_WQE:
3461 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3462 cq_event);
45ed1190
JS
3463 /* Translate ELS WCQE to response IOCBQ */
3464 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3465 irspiocbq);
3466 if (irspiocbq)
3467 lpfc_sli_sp_handle_rspiocb(phba, pring,
3468 irspiocbq);
4d9ab994
JS
3469 break;
3470 case CQE_CODE_RECEIVE:
7851fe2c 3471 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3472 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3473 cq_event);
3474 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3475 break;
3476 default:
3477 break;
3478 }
4f774513
JS
3479 }
3480}
3481
e59058c4 3482/**
3621a710 3483 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3484 * @phba: Pointer to HBA context object.
3485 * @pring: Pointer to driver SLI ring object.
3486 *
3487 * This function aborts all iocbs in the given ring and frees all the iocb
3488 * objects in txq. This function issues an abort iocb for all the iocb commands
3489 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3490 * the return of this function. The caller is not required to hold any locks.
3491 **/
2e0fef85 3492void
dea3101e
JB
3493lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3494{
2534ba75 3495 LIST_HEAD(completions);
dea3101e 3496 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3497
92d7f7b0
JS
3498 if (pring->ringno == LPFC_ELS_RING) {
3499 lpfc_fabric_abort_hba(phba);
3500 }
3501
dea3101e
JB
3502 /* Error everything on txq and txcmplq
3503 * First do the txq.
3504 */
2e0fef85 3505 spin_lock_irq(&phba->hbalock);
2534ba75 3506 list_splice_init(&pring->txq, &completions);
dea3101e
JB
3507
3508 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3509 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3510 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3511
2e0fef85 3512 spin_unlock_irq(&phba->hbalock);
dea3101e 3513
a257bf90
JS
3514 /* Cancel all the IOCBs from the completions list */
3515 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3516 IOERR_SLI_ABORTED);
dea3101e
JB
3517}
3518
a8e497d5 3519/**
3621a710 3520 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3521 * @phba: Pointer to HBA context object.
3522 *
3523 * This function flushes all iocbs in the fcp ring and frees all the iocb
3524 * objects in txq and txcmplq. This function will not issue abort iocbs
3525 * for all the iocb commands in txcmplq, they will just be returned with
3526 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3527 * slot has been permanently disabled.
3528 **/
3529void
3530lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3531{
3532 LIST_HEAD(txq);
3533 LIST_HEAD(txcmplq);
a8e497d5
JS
3534 struct lpfc_sli *psli = &phba->sli;
3535 struct lpfc_sli_ring *pring;
3536
3537 /* Currently, only one fcp ring */
3538 pring = &psli->ring[psli->fcp_ring];
3539
3540 spin_lock_irq(&phba->hbalock);
3541 /* Retrieve everything on txq */
3542 list_splice_init(&pring->txq, &txq);
a8e497d5
JS
3543
3544 /* Retrieve everything on the txcmplq */
3545 list_splice_init(&pring->txcmplq, &txcmplq);
4f2e66c6
JS
3546
3547 /* Indicate the I/O queues are flushed */
3548 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3549 spin_unlock_irq(&phba->hbalock);
3550
3551 /* Flush the txq */
a257bf90
JS
3552 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3553 IOERR_SLI_DOWN);
a8e497d5
JS
3554
3555 /* Flush the txcmpq */
a257bf90
JS
3556 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3557 IOERR_SLI_DOWN);
a8e497d5
JS
3558}
3559
e59058c4 3560/**
3772a991 3561 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3562 * @phba: Pointer to HBA context object.
3563 * @mask: Bit mask to be checked.
3564 *
3565 * This function reads the host status register and compares
3566 * with the provided bit mask to check if HBA completed
3567 * the restart. This function will wait in a loop for the
3568 * HBA to complete restart. If the HBA does not restart within
3569 * 15 iterations, the function will reset the HBA again. The
3570 * function returns 1 when HBA fail to restart otherwise returns
3571 * zero.
3572 **/
3772a991
JS
3573static int
3574lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3575{
41415862
JW
3576 uint32_t status;
3577 int i = 0;
3578 int retval = 0;
dea3101e 3579
41415862 3580 /* Read the HBA Host Status Register */
9940b97b
JS
3581 if (lpfc_readl(phba->HSregaddr, &status))
3582 return 1;
dea3101e 3583
41415862
JW
3584 /*
3585 * Check status register every 100ms for 5 retries, then every
3586 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3587 * every 2.5 sec for 4.
3588 * Break our of the loop if errors occurred during init.
3589 */
3590 while (((status & mask) != mask) &&
3591 !(status & HS_FFERM) &&
3592 i++ < 20) {
dea3101e 3593
41415862
JW
3594 if (i <= 5)
3595 msleep(10);
3596 else if (i <= 10)
3597 msleep(500);
3598 else
3599 msleep(2500);
dea3101e 3600
41415862 3601 if (i == 15) {
2e0fef85 3602 /* Do post */
92d7f7b0 3603 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3604 lpfc_sli_brdrestart(phba);
3605 }
3606 /* Read the HBA Host Status Register */
9940b97b
JS
3607 if (lpfc_readl(phba->HSregaddr, &status)) {
3608 retval = 1;
3609 break;
3610 }
41415862 3611 }
dea3101e 3612
41415862
JW
3613 /* Check to see if any errors occurred during init */
3614 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3616 "2751 Adapter failed to restart, "
3617 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3618 status,
3619 readl(phba->MBslimaddr + 0xa8),
3620 readl(phba->MBslimaddr + 0xac));
2e0fef85 3621 phba->link_state = LPFC_HBA_ERROR;
41415862 3622 retval = 1;
dea3101e 3623 }
dea3101e 3624
41415862
JW
3625 return retval;
3626}
dea3101e 3627
da0436e9
JS
3628/**
3629 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3630 * @phba: Pointer to HBA context object.
3631 * @mask: Bit mask to be checked.
3632 *
3633 * This function checks the host status register to check if HBA is
3634 * ready. This function will wait in a loop for the HBA to be ready
3635 * If the HBA is not ready , the function will will reset the HBA PCI
3636 * function again. The function returns 1 when HBA fail to be ready
3637 * otherwise returns zero.
3638 **/
3639static int
3640lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3641{
3642 uint32_t status;
3643 int retval = 0;
3644
3645 /* Read the HBA Host Status Register */
3646 status = lpfc_sli4_post_status_check(phba);
3647
3648 if (status) {
3649 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3650 lpfc_sli_brdrestart(phba);
3651 status = lpfc_sli4_post_status_check(phba);
3652 }
3653
3654 /* Check to see if any errors occurred during init */
3655 if (status) {
3656 phba->link_state = LPFC_HBA_ERROR;
3657 retval = 1;
3658 } else
3659 phba->sli4_hba.intr_enable = 0;
3660
3661 return retval;
3662}
3663
3664/**
3665 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3666 * @phba: Pointer to HBA context object.
3667 * @mask: Bit mask to be checked.
3668 *
3669 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3670 * from the API jump table function pointer from the lpfc_hba struct.
3671 **/
3672int
3673lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3674{
3675 return phba->lpfc_sli_brdready(phba, mask);
3676}
3677
9290831f
JS
3678#define BARRIER_TEST_PATTERN (0xdeadbeef)
3679
e59058c4 3680/**
3621a710 3681 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3682 * @phba: Pointer to HBA context object.
3683 *
1b51197d
JS
3684 * This function is called before resetting an HBA. This function is called
3685 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 3686 **/
2e0fef85 3687void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3688{
65a29c16
JS
3689 uint32_t __iomem *resp_buf;
3690 uint32_t __iomem *mbox_buf;
9290831f 3691 volatile uint32_t mbox;
9940b97b 3692 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3693 int i;
3694 uint8_t hdrtype;
3695
3696 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3697 if (hdrtype != 0x80 ||
3698 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3699 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3700 return;
3701
3702 /*
3703 * Tell the other part of the chip to suspend temporarily all
3704 * its DMA activity.
3705 */
65a29c16 3706 resp_buf = phba->MBslimaddr;
9290831f
JS
3707
3708 /* Disable the error attention */
9940b97b
JS
3709 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3710 return;
9290831f
JS
3711 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3712 readl(phba->HCregaddr); /* flush */
2e0fef85 3713 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 3714
9940b97b
JS
3715 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3716 return;
3717 if (ha_copy & HA_ERATT) {
9290831f
JS
3718 /* Clear Chip error bit */
3719 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3720 phba->pport->stopped = 1;
9290831f
JS
3721 }
3722
3723 mbox = 0;
3724 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3725 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3726
3727 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3728 mbox_buf = phba->MBslimaddr;
9290831f
JS
3729 writel(mbox, mbox_buf);
3730
9940b97b
JS
3731 for (i = 0; i < 50; i++) {
3732 if (lpfc_readl((resp_buf + 1), &resp_data))
3733 return;
3734 if (resp_data != ~(BARRIER_TEST_PATTERN))
3735 mdelay(1);
3736 else
3737 break;
3738 }
3739 resp_data = 0;
3740 if (lpfc_readl((resp_buf + 1), &resp_data))
3741 return;
3742 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3743 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3744 phba->pport->stopped)
9290831f
JS
3745 goto restore_hc;
3746 else
3747 goto clear_errat;
3748 }
3749
3750 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
3751 resp_data = 0;
3752 for (i = 0; i < 500; i++) {
3753 if (lpfc_readl(resp_buf, &resp_data))
3754 return;
3755 if (resp_data != mbox)
3756 mdelay(1);
3757 else
3758 break;
3759 }
9290831f
JS
3760
3761clear_errat:
3762
9940b97b
JS
3763 while (++i < 500) {
3764 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3765 return;
3766 if (!(ha_copy & HA_ERATT))
3767 mdelay(1);
3768 else
3769 break;
3770 }
9290831f
JS
3771
3772 if (readl(phba->HAregaddr) & HA_ERATT) {
3773 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3774 phba->pport->stopped = 1;
9290831f
JS
3775 }
3776
3777restore_hc:
2e0fef85 3778 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3779 writel(hc_copy, phba->HCregaddr);
3780 readl(phba->HCregaddr); /* flush */
3781}
3782
e59058c4 3783/**
3621a710 3784 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3785 * @phba: Pointer to HBA context object.
3786 *
3787 * This function issues a kill_board mailbox command and waits for
3788 * the error attention interrupt. This function is called for stopping
3789 * the firmware processing. The caller is not required to hold any
3790 * locks. This function calls lpfc_hba_down_post function to free
3791 * any pending commands after the kill. The function will return 1 when it
3792 * fails to kill the board else will return 0.
3793 **/
41415862 3794int
2e0fef85 3795lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3796{
3797 struct lpfc_sli *psli;
3798 LPFC_MBOXQ_t *pmb;
3799 uint32_t status;
3800 uint32_t ha_copy;
3801 int retval;
3802 int i = 0;
dea3101e 3803
41415862 3804 psli = &phba->sli;
dea3101e 3805
41415862 3806 /* Kill HBA */
ed957684 3807 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3808 "0329 Kill HBA Data: x%x x%x\n",
3809 phba->pport->port_state, psli->sli_flag);
41415862 3810
98c9ea5c
JS
3811 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3812 if (!pmb)
41415862 3813 return 1;
41415862
JW
3814
3815 /* Disable the error attention */
2e0fef85 3816 spin_lock_irq(&phba->hbalock);
9940b97b
JS
3817 if (lpfc_readl(phba->HCregaddr, &status)) {
3818 spin_unlock_irq(&phba->hbalock);
3819 mempool_free(pmb, phba->mbox_mem_pool);
3820 return 1;
3821 }
41415862
JW
3822 status &= ~HC_ERINT_ENA;
3823 writel(status, phba->HCregaddr);
3824 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3825 phba->link_flag |= LS_IGNORE_ERATT;
3826 spin_unlock_irq(&phba->hbalock);
41415862
JW
3827
3828 lpfc_kill_board(phba, pmb);
3829 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3830 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3831
3832 if (retval != MBX_SUCCESS) {
3833 if (retval != MBX_BUSY)
3834 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3835 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3836 "2752 KILL_BOARD command failed retval %d\n",
3837 retval);
2e0fef85
JS
3838 spin_lock_irq(&phba->hbalock);
3839 phba->link_flag &= ~LS_IGNORE_ERATT;
3840 spin_unlock_irq(&phba->hbalock);
41415862
JW
3841 return 1;
3842 }
3843
f4b4c68f
JS
3844 spin_lock_irq(&phba->hbalock);
3845 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3846 spin_unlock_irq(&phba->hbalock);
9290831f 3847
41415862
JW
3848 mempool_free(pmb, phba->mbox_mem_pool);
3849
3850 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3851 * attention every 100ms for 3 seconds. If we don't get ERATT after
3852 * 3 seconds we still set HBA_ERROR state because the status of the
3853 * board is now undefined.
3854 */
9940b97b
JS
3855 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3856 return 1;
41415862
JW
3857 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3858 mdelay(100);
9940b97b
JS
3859 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3860 return 1;
41415862
JW
3861 }
3862
3863 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3864 if (ha_copy & HA_ERATT) {
3865 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3866 phba->pport->stopped = 1;
9290831f 3867 }
2e0fef85 3868 spin_lock_irq(&phba->hbalock);
41415862 3869 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3870 psli->mbox_active = NULL;
2e0fef85
JS
3871 phba->link_flag &= ~LS_IGNORE_ERATT;
3872 spin_unlock_irq(&phba->hbalock);
41415862 3873
41415862 3874 lpfc_hba_down_post(phba);
2e0fef85 3875 phba->link_state = LPFC_HBA_ERROR;
41415862 3876
2e0fef85 3877 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
3878}
3879
e59058c4 3880/**
3772a991 3881 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3882 * @phba: Pointer to HBA context object.
3883 *
3884 * This function resets the HBA by writing HC_INITFF to the control
3885 * register. After the HBA resets, this function resets all the iocb ring
3886 * indices. This function disables PCI layer parity checking during
3887 * the reset.
3888 * This function returns 0 always.
3889 * The caller is not required to hold any locks.
3890 **/
41415862 3891int
2e0fef85 3892lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3893{
41415862 3894 struct lpfc_sli *psli;
dea3101e 3895 struct lpfc_sli_ring *pring;
41415862 3896 uint16_t cfg_value;
dea3101e 3897 int i;
dea3101e 3898
41415862 3899 psli = &phba->sli;
dea3101e 3900
41415862
JW
3901 /* Reset HBA */
3902 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3903 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3904 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
3905
3906 /* perform board reset */
3907 phba->fc_eventTag = 0;
4d9ab994 3908 phba->link_events = 0;
2e0fef85
JS
3909 phba->pport->fc_myDID = 0;
3910 phba->pport->fc_prevDID = 0;
dea3101e 3911
41415862
JW
3912 /* Turn off parity checking and serr during the physical reset */
3913 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3914 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3915 (cfg_value &
3916 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3917
3772a991
JS
3918 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3919
41415862
JW
3920 /* Now toggle INITFF bit in the Host Control Register */
3921 writel(HC_INITFF, phba->HCregaddr);
3922 mdelay(1);
3923 readl(phba->HCregaddr); /* flush */
3924 writel(0, phba->HCregaddr);
3925 readl(phba->HCregaddr); /* flush */
3926
3927 /* Restore PCI cmd register */
3928 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
3929
3930 /* Initialize relevant SLI info */
41415862
JW
3931 for (i = 0; i < psli->num_rings; i++) {
3932 pring = &psli->ring[i];
dea3101e 3933 pring->flag = 0;
7e56aa25
JS
3934 pring->sli.sli3.rspidx = 0;
3935 pring->sli.sli3.next_cmdidx = 0;
3936 pring->sli.sli3.local_getidx = 0;
3937 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
3938 pring->missbufcnt = 0;
3939 }
dea3101e 3940
2e0fef85 3941 phba->link_state = LPFC_WARM_START;
41415862
JW
3942 return 0;
3943}
3944
e59058c4 3945/**
da0436e9
JS
3946 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3947 * @phba: Pointer to HBA context object.
3948 *
3949 * This function resets a SLI4 HBA. This function disables PCI layer parity
3950 * checking during resets the device. The caller is not required to hold
3951 * any locks.
3952 *
3953 * This function returns 0 always.
3954 **/
3955int
3956lpfc_sli4_brdreset(struct lpfc_hba *phba)
3957{
3958 struct lpfc_sli *psli = &phba->sli;
3959 uint16_t cfg_value;
27b01b82 3960 int rc;
da0436e9
JS
3961
3962 /* Reset HBA */
3963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3964 "0295 Reset HBA Data: x%x x%x\n",
3965 phba->pport->port_state, psli->sli_flag);
3966
3967 /* perform board reset */
3968 phba->fc_eventTag = 0;
4d9ab994 3969 phba->link_events = 0;
da0436e9
JS
3970 phba->pport->fc_myDID = 0;
3971 phba->pport->fc_prevDID = 0;
3972
da0436e9
JS
3973 spin_lock_irq(&phba->hbalock);
3974 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3975 phba->fcf.fcf_flag = 0;
da0436e9
JS
3976 spin_unlock_irq(&phba->hbalock);
3977
3978 /* Now physically reset the device */
3979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3980 "0389 Performing PCI function reset!\n");
be858b65
JS
3981
3982 /* Turn off parity checking and serr during the physical reset */
3983 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3984 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3985 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3986
88318816 3987 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 3988 rc = lpfc_pci_function_reset(phba);
88318816 3989 lpfc_sli4_queue_destroy(phba);
da0436e9 3990
be858b65
JS
3991 /* Restore PCI cmd register */
3992 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3993
27b01b82 3994 return rc;
da0436e9
JS
3995}
3996
3997/**
3998 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3999 * @phba: Pointer to HBA context object.
4000 *
4001 * This function is called in the SLI initialization code path to
4002 * restart the HBA. The caller is not required to hold any lock.
4003 * This function writes MBX_RESTART mailbox command to the SLIM and
4004 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4005 * function to free any pending commands. The function enables
4006 * POST only during the first initialization. The function returns zero.
4007 * The function does not guarantee completion of MBX_RESTART mailbox
4008 * command before the return of this function.
4009 **/
da0436e9
JS
4010static int
4011lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4012{
4013 MAILBOX_t *mb;
4014 struct lpfc_sli *psli;
41415862
JW
4015 volatile uint32_t word0;
4016 void __iomem *to_slim;
0d878419 4017 uint32_t hba_aer_enabled;
41415862 4018
2e0fef85 4019 spin_lock_irq(&phba->hbalock);
41415862 4020
0d878419
JS
4021 /* Take PCIe device Advanced Error Reporting (AER) state */
4022 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4023
41415862
JW
4024 psli = &phba->sli;
4025
4026 /* Restart HBA */
4027 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4028 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 4029 phba->pport->port_state, psli->sli_flag);
41415862
JW
4030
4031 word0 = 0;
4032 mb = (MAILBOX_t *) &word0;
4033 mb->mbxCommand = MBX_RESTART;
4034 mb->mbxHc = 1;
4035
9290831f
JS
4036 lpfc_reset_barrier(phba);
4037
41415862
JW
4038 to_slim = phba->MBslimaddr;
4039 writel(*(uint32_t *) mb, to_slim);
4040 readl(to_slim); /* flush */
4041
4042 /* Only skip post after fc_ffinit is completed */
eaf15d5b 4043 if (phba->pport->port_state)
41415862 4044 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4045 else
41415862 4046 word0 = 0; /* This is really setting up word1 */
65a29c16 4047 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4048 writel(*(uint32_t *) mb, to_slim);
4049 readl(to_slim); /* flush */
dea3101e 4050
41415862 4051 lpfc_sli_brdreset(phba);
2e0fef85
JS
4052 phba->pport->stopped = 0;
4053 phba->link_state = LPFC_INIT_START;
da0436e9 4054 phba->hba_flag = 0;
2e0fef85 4055 spin_unlock_irq(&phba->hbalock);
41415862 4056
64ba8818
JS
4057 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4058 psli->stats_start = get_seconds();
4059
eaf15d5b
JS
4060 /* Give the INITFF and Post time to settle. */
4061 mdelay(100);
41415862 4062
0d878419
JS
4063 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4064 if (hba_aer_enabled)
4065 pci_disable_pcie_error_reporting(phba->pcidev);
4066
41415862 4067 lpfc_hba_down_post(phba);
dea3101e
JB
4068
4069 return 0;
4070}
4071
da0436e9
JS
4072/**
4073 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4074 * @phba: Pointer to HBA context object.
4075 *
4076 * This function is called in the SLI initialization code path to restart
4077 * a SLI4 HBA. The caller is not required to hold any lock.
4078 * At the end of the function, it calls lpfc_hba_down_post function to
4079 * free any pending commands.
4080 **/
4081static int
4082lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4083{
4084 struct lpfc_sli *psli = &phba->sli;
75baf696 4085 uint32_t hba_aer_enabled;
27b01b82 4086 int rc;
da0436e9
JS
4087
4088 /* Restart HBA */
4089 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4090 "0296 Restart HBA Data: x%x x%x\n",
4091 phba->pport->port_state, psli->sli_flag);
4092
75baf696
JS
4093 /* Take PCIe device Advanced Error Reporting (AER) state */
4094 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4095
27b01b82 4096 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4097
4098 spin_lock_irq(&phba->hbalock);
4099 phba->pport->stopped = 0;
4100 phba->link_state = LPFC_INIT_START;
4101 phba->hba_flag = 0;
4102 spin_unlock_irq(&phba->hbalock);
4103
4104 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4105 psli->stats_start = get_seconds();
4106
75baf696
JS
4107 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4108 if (hba_aer_enabled)
4109 pci_disable_pcie_error_reporting(phba->pcidev);
4110
da0436e9
JS
4111 lpfc_hba_down_post(phba);
4112
27b01b82 4113 return rc;
da0436e9
JS
4114}
4115
4116/**
4117 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4118 * @phba: Pointer to HBA context object.
4119 *
4120 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4121 * API jump table function pointer from the lpfc_hba struct.
4122**/
4123int
4124lpfc_sli_brdrestart(struct lpfc_hba *phba)
4125{
4126 return phba->lpfc_sli_brdrestart(phba);
4127}
4128
e59058c4 4129/**
3621a710 4130 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4131 * @phba: Pointer to HBA context object.
4132 *
4133 * This function is called after a HBA restart to wait for successful
4134 * restart of the HBA. Successful restart of the HBA is indicated by
4135 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4136 * iteration, the function will restart the HBA again. The function returns
4137 * zero if HBA successfully restarted else returns negative error code.
4138 **/
dea3101e
JB
4139static int
4140lpfc_sli_chipset_init(struct lpfc_hba *phba)
4141{
4142 uint32_t status, i = 0;
4143
4144 /* Read the HBA Host Status Register */
9940b97b
JS
4145 if (lpfc_readl(phba->HSregaddr, &status))
4146 return -EIO;
dea3101e
JB
4147
4148 /* Check status register to see what current state is */
4149 i = 0;
4150 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4151
dcf2a4e0
JS
4152 /* Check every 10ms for 10 retries, then every 100ms for 90
4153 * retries, then every 1 sec for 50 retires for a total of
4154 * ~60 seconds before reset the board again and check every
4155 * 1 sec for 50 retries. The up to 60 seconds before the
4156 * board ready is required by the Falcon FIPS zeroization
4157 * complete, and any reset the board in between shall cause
4158 * restart of zeroization, further delay the board ready.
dea3101e 4159 */
dcf2a4e0 4160 if (i++ >= 200) {
dea3101e
JB
4161 /* Adapter failed to init, timeout, status reg
4162 <status> */
ed957684 4163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4164 "0436 Adapter failed to init, "
09372820
JS
4165 "timeout, status reg x%x, "
4166 "FW Data: A8 x%x AC x%x\n", status,
4167 readl(phba->MBslimaddr + 0xa8),
4168 readl(phba->MBslimaddr + 0xac));
2e0fef85 4169 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4170 return -ETIMEDOUT;
4171 }
4172
4173 /* Check to see if any errors occurred during init */
4174 if (status & HS_FFERM) {
4175 /* ERROR: During chipset initialization */
4176 /* Adapter failed to init, chipset, status reg
4177 <status> */
ed957684 4178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4179 "0437 Adapter failed to init, "
09372820
JS
4180 "chipset, status reg x%x, "
4181 "FW Data: A8 x%x AC x%x\n", status,
4182 readl(phba->MBslimaddr + 0xa8),
4183 readl(phba->MBslimaddr + 0xac));
2e0fef85 4184 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4185 return -EIO;
4186 }
4187
dcf2a4e0 4188 if (i <= 10)
dea3101e 4189 msleep(10);
dcf2a4e0
JS
4190 else if (i <= 100)
4191 msleep(100);
4192 else
4193 msleep(1000);
dea3101e 4194
dcf2a4e0
JS
4195 if (i == 150) {
4196 /* Do post */
92d7f7b0 4197 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4198 lpfc_sli_brdrestart(phba);
dea3101e
JB
4199 }
4200 /* Read the HBA Host Status Register */
9940b97b
JS
4201 if (lpfc_readl(phba->HSregaddr, &status))
4202 return -EIO;
dea3101e
JB
4203 }
4204
4205 /* Check to see if any errors occurred during init */
4206 if (status & HS_FFERM) {
4207 /* ERROR: During chipset initialization */
4208 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4210 "0438 Adapter failed to init, chipset, "
09372820
JS
4211 "status reg x%x, "
4212 "FW Data: A8 x%x AC x%x\n", status,
4213 readl(phba->MBslimaddr + 0xa8),
4214 readl(phba->MBslimaddr + 0xac));
2e0fef85 4215 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4216 return -EIO;
4217 }
4218
4219 /* Clear all interrupt enable conditions */
4220 writel(0, phba->HCregaddr);
4221 readl(phba->HCregaddr); /* flush */
4222
4223 /* setup host attn register */
4224 writel(0xffffffff, phba->HAregaddr);
4225 readl(phba->HAregaddr); /* flush */
4226 return 0;
4227}
4228
e59058c4 4229/**
3621a710 4230 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4231 *
4232 * This function calculates and returns the number of HBQs required to be
4233 * configured.
4234 **/
78b2d852 4235int
ed957684
JS
4236lpfc_sli_hbq_count(void)
4237{
92d7f7b0 4238 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4239}
4240
e59058c4 4241/**
3621a710 4242 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4243 *
4244 * This function adds the number of hbq entries in every HBQ to get
4245 * the total number of hbq entries required for the HBA and returns
4246 * the total count.
4247 **/
ed957684
JS
4248static int
4249lpfc_sli_hbq_entry_count(void)
4250{
4251 int hbq_count = lpfc_sli_hbq_count();
4252 int count = 0;
4253 int i;
4254
4255 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4256 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4257 return count;
4258}
4259
e59058c4 4260/**
3621a710 4261 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4262 *
4263 * This function calculates amount of memory required for all hbq entries
4264 * to be configured and returns the total memory required.
4265 **/
dea3101e 4266int
ed957684
JS
4267lpfc_sli_hbq_size(void)
4268{
4269 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4270}
4271
e59058c4 4272/**
3621a710 4273 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4274 * @phba: Pointer to HBA context object.
4275 *
4276 * This function is called during the SLI initialization to configure
4277 * all the HBQs and post buffers to the HBQ. The caller is not
4278 * required to hold any locks. This function will return zero if successful
4279 * else it will return negative error code.
4280 **/
ed957684
JS
4281static int
4282lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4283{
4284 int hbq_count = lpfc_sli_hbq_count();
4285 LPFC_MBOXQ_t *pmb;
4286 MAILBOX_t *pmbox;
4287 uint32_t hbqno;
4288 uint32_t hbq_entry_index;
ed957684 4289
92d7f7b0
JS
4290 /* Get a Mailbox buffer to setup mailbox
4291 * commands for HBA initialization
4292 */
ed957684
JS
4293 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4294
4295 if (!pmb)
4296 return -ENOMEM;
4297
04c68496 4298 pmbox = &pmb->u.mb;
ed957684
JS
4299
4300 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4301 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4302 phba->hbq_in_use = 1;
ed957684
JS
4303
4304 hbq_entry_index = 0;
4305 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4306 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4307 phba->hbqs[hbqno].hbqPutIdx = 0;
4308 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4309 phba->hbqs[hbqno].entry_count =
92d7f7b0 4310 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4311 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4312 hbq_entry_index, pmb);
ed957684
JS
4313 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4314
4315 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4316 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4317 mbxStatus <status>, ring <num> */
4318
4319 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4320 LOG_SLI | LOG_VPORT,
e8b62011 4321 "1805 Adapter failed to init. "
ed957684 4322 "Data: x%x x%x x%x\n",
e8b62011 4323 pmbox->mbxCommand,
ed957684
JS
4324 pmbox->mbxStatus, hbqno);
4325
4326 phba->link_state = LPFC_HBA_ERROR;
4327 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4328 return -ENXIO;
ed957684
JS
4329 }
4330 }
4331 phba->hbq_count = hbq_count;
4332
ed957684
JS
4333 mempool_free(pmb, phba->mbox_mem_pool);
4334
92d7f7b0 4335 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4336 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4337 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4338 return 0;
4339}
4340
4f774513
JS
4341/**
4342 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4343 * @phba: Pointer to HBA context object.
4344 *
4345 * This function is called during the SLI initialization to configure
4346 * all the HBQs and post buffers to the HBQ. The caller is not
4347 * required to hold any locks. This function will return zero if successful
4348 * else it will return negative error code.
4349 **/
4350static int
4351lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4352{
4353 phba->hbq_in_use = 1;
4354 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4355 phba->hbq_count = 1;
4356 /* Initially populate or replenish the HBQs */
4357 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4358 return 0;
4359}
4360
e59058c4 4361/**
3621a710 4362 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4363 * @phba: Pointer to HBA context object.
4364 * @sli_mode: sli mode - 2/3
4365 *
4366 * This function is called by the sli intialization code path
4367 * to issue config_port mailbox command. This function restarts the
4368 * HBA firmware and issues a config_port mailbox command to configure
4369 * the SLI interface in the sli mode specified by sli_mode
4370 * variable. The caller is not required to hold any locks.
4371 * The function returns 0 if successful, else returns negative error
4372 * code.
4373 **/
9399627f
JS
4374int
4375lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4376{
4377 LPFC_MBOXQ_t *pmb;
4378 uint32_t resetcount = 0, rc = 0, done = 0;
4379
4380 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4381 if (!pmb) {
2e0fef85 4382 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4383 return -ENOMEM;
4384 }
4385
ed957684 4386 phba->sli_rev = sli_mode;
dea3101e 4387 while (resetcount < 2 && !done) {
2e0fef85 4388 spin_lock_irq(&phba->hbalock);
1c067a42 4389 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4390 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4391 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4392 lpfc_sli_brdrestart(phba);
dea3101e
JB
4393 rc = lpfc_sli_chipset_init(phba);
4394 if (rc)
4395 break;
4396
2e0fef85 4397 spin_lock_irq(&phba->hbalock);
1c067a42 4398 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4399 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4400 resetcount++;
4401
ed957684
JS
4402 /* Call pre CONFIG_PORT mailbox command initialization. A
4403 * value of 0 means the call was successful. Any other
4404 * nonzero value is a failure, but if ERESTART is returned,
4405 * the driver may reset the HBA and try again.
4406 */
dea3101e
JB
4407 rc = lpfc_config_port_prep(phba);
4408 if (rc == -ERESTART) {
ed957684 4409 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4410 continue;
34b02dcd 4411 } else if (rc)
dea3101e 4412 break;
6d368e53 4413
2e0fef85 4414 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4415 lpfc_config_port(phba, pmb);
4416 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4417 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4418 LPFC_SLI3_HBQ_ENABLED |
4419 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4420 LPFC_SLI3_BG_ENABLED |
4421 LPFC_SLI3_DSS_ENABLED);
ed957684 4422 if (rc != MBX_SUCCESS) {
dea3101e 4423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4424 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4425 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4426 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4427 spin_lock_irq(&phba->hbalock);
04c68496 4428 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4429 spin_unlock_irq(&phba->hbalock);
4430 rc = -ENXIO;
04c68496
JS
4431 } else {
4432 /* Allow asynchronous mailbox command to go through */
4433 spin_lock_irq(&phba->hbalock);
4434 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4435 spin_unlock_irq(&phba->hbalock);
ed957684 4436 done = 1;
cb69f7de
JS
4437
4438 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4439 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4440 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4441 "3110 Port did not grant ASABT\n");
04c68496 4442 }
dea3101e 4443 }
ed957684
JS
4444 if (!done) {
4445 rc = -EINVAL;
4446 goto do_prep_failed;
4447 }
04c68496
JS
4448 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4449 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4450 rc = -ENXIO;
4451 goto do_prep_failed;
4452 }
04c68496 4453 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4454 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4455 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4456 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4457 phba->max_vpi : phba->max_vports;
4458
34b02dcd
JS
4459 } else
4460 phba->max_vpi = 0;
bc73905a
JS
4461 phba->fips_level = 0;
4462 phba->fips_spec_rev = 0;
4463 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4464 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4465 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4466 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4468 "2850 Security Crypto Active. FIPS x%d "
4469 "(Spec Rev: x%d)",
4470 phba->fips_level, phba->fips_spec_rev);
4471 }
4472 if (pmb->u.mb.un.varCfgPort.sec_err) {
4473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4474 "2856 Config Port Security Crypto "
4475 "Error: x%x ",
4476 pmb->u.mb.un.varCfgPort.sec_err);
4477 }
04c68496 4478 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4479 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4480 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4481 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4482
4483 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4484 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4485
4486 if (phba->cfg_enable_bg) {
04c68496 4487 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4488 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4489 else
4490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4491 "0443 Adapter did not grant "
4492 "BlockGuard\n");
4493 }
34b02dcd 4494 } else {
8f34f4ce 4495 phba->hbq_get = NULL;
34b02dcd 4496 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4497 phba->max_vpi = 0;
ed957684 4498 }
92d7f7b0 4499do_prep_failed:
ed957684
JS
4500 mempool_free(pmb, phba->mbox_mem_pool);
4501 return rc;
4502}
4503
e59058c4
JS
4504
4505/**
3621a710 4506 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4507 * @phba: Pointer to HBA context object.
4508 *
4509 * This function is the main SLI intialization function. This function
4510 * is called by the HBA intialization code, HBA reset code and HBA
4511 * error attention handler code. Caller is not required to hold any
4512 * locks. This function issues config_port mailbox command to configure
4513 * the SLI, setup iocb rings and HBQ rings. In the end the function
4514 * calls the config_port_post function to issue init_link mailbox
4515 * command and to start the discovery. The function will return zero
4516 * if successful, else it will return negative error code.
4517 **/
ed957684
JS
4518int
4519lpfc_sli_hba_setup(struct lpfc_hba *phba)
4520{
4521 uint32_t rc;
6d368e53
JS
4522 int mode = 3, i;
4523 int longs;
ed957684
JS
4524
4525 switch (lpfc_sli_mode) {
4526 case 2:
78b2d852 4527 if (phba->cfg_enable_npiv) {
92d7f7b0 4528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4529 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4530 "parameter (%d) to auto (0).\n",
e8b62011 4531 lpfc_sli_mode);
92d7f7b0
JS
4532 break;
4533 }
ed957684
JS
4534 mode = 2;
4535 break;
4536 case 0:
4537 case 3:
4538 break;
4539 default:
92d7f7b0 4540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4541 "1819 Unrecognized lpfc_sli_mode "
4542 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4543
4544 break;
4545 }
4546
9399627f
JS
4547 rc = lpfc_sli_config_port(phba, mode);
4548
ed957684 4549 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4551 "1820 Unable to select SLI-3. "
4552 "Not supported by adapter.\n");
ed957684 4553 if (rc && mode != 2)
9399627f 4554 rc = lpfc_sli_config_port(phba, 2);
ed957684 4555 if (rc)
dea3101e
JB
4556 goto lpfc_sli_hba_setup_error;
4557
0d878419
JS
4558 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4559 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4560 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4561 if (!rc) {
4562 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4563 "2709 This device supports "
4564 "Advanced Error Reporting (AER)\n");
4565 spin_lock_irq(&phba->hbalock);
4566 phba->hba_flag |= HBA_AER_ENABLED;
4567 spin_unlock_irq(&phba->hbalock);
4568 } else {
4569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4570 "2708 This device does not support "
b069d7eb
JS
4571 "Advanced Error Reporting (AER): %d\n",
4572 rc);
0d878419
JS
4573 phba->cfg_aer_support = 0;
4574 }
4575 }
4576
ed957684
JS
4577 if (phba->sli_rev == 3) {
4578 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4579 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4580 } else {
4581 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4582 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4583 phba->sli3_options = 0;
ed957684
JS
4584 }
4585
4586 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4587 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4588 phba->sli_rev, phba->max_vpi);
ed957684 4589 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4590
4591 if (rc)
4592 goto lpfc_sli_hba_setup_error;
4593
6d368e53
JS
4594 /* Initialize VPIs. */
4595 if (phba->sli_rev == LPFC_SLI_REV3) {
4596 /*
4597 * The VPI bitmask and physical ID array are allocated
4598 * and initialized once only - at driver load. A port
4599 * reset doesn't need to reinitialize this memory.
4600 */
4601 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4602 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4603 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4604 GFP_KERNEL);
4605 if (!phba->vpi_bmask) {
4606 rc = -ENOMEM;
4607 goto lpfc_sli_hba_setup_error;
4608 }
4609
4610 phba->vpi_ids = kzalloc(
4611 (phba->max_vpi+1) * sizeof(uint16_t),
4612 GFP_KERNEL);
4613 if (!phba->vpi_ids) {
4614 kfree(phba->vpi_bmask);
4615 rc = -ENOMEM;
4616 goto lpfc_sli_hba_setup_error;
4617 }
4618 for (i = 0; i < phba->max_vpi; i++)
4619 phba->vpi_ids[i] = i;
4620 }
4621 }
4622
9399627f 4623 /* Init HBQs */
ed957684
JS
4624 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4625 rc = lpfc_sli_hbq_setup(phba);
4626 if (rc)
4627 goto lpfc_sli_hba_setup_error;
4628 }
04c68496 4629 spin_lock_irq(&phba->hbalock);
dea3101e 4630 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4631 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4632
4633 rc = lpfc_config_port_post(phba);
4634 if (rc)
4635 goto lpfc_sli_hba_setup_error;
4636
ed957684
JS
4637 return rc;
4638
92d7f7b0 4639lpfc_sli_hba_setup_error:
2e0fef85 4640 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4642 "0445 Firmware initialization failed\n");
dea3101e
JB
4643 return rc;
4644}
4645
e59058c4 4646/**
da0436e9
JS
4647 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4648 * @phba: Pointer to HBA context object.
4649 * @mboxq: mailbox pointer.
4650 * This function issue a dump mailbox command to read config region
4651 * 23 and parse the records in the region and populate driver
4652 * data structure.
e59058c4 4653 **/
da0436e9 4654static int
ff78d8f9 4655lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4656{
ff78d8f9 4657 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4658 struct lpfc_dmabuf *mp;
4659 struct lpfc_mqe *mqe;
4660 uint32_t data_length;
4661 int rc;
dea3101e 4662
da0436e9
JS
4663 /* Program the default value of vlan_id and fc_map */
4664 phba->valid_vlan = 0;
4665 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4666 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4667 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4668
ff78d8f9
JS
4669 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4670 if (!mboxq)
da0436e9
JS
4671 return -ENOMEM;
4672
ff78d8f9
JS
4673 mqe = &mboxq->u.mqe;
4674 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4675 rc = -ENOMEM;
4676 goto out_free_mboxq;
4677 }
4678
da0436e9
JS
4679 mp = (struct lpfc_dmabuf *) mboxq->context1;
4680 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4681
4682 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4683 "(%d):2571 Mailbox cmd x%x Status x%x "
4684 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4685 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4686 "CQ: x%x x%x x%x x%x\n",
4687 mboxq->vport ? mboxq->vport->vpi : 0,
4688 bf_get(lpfc_mqe_command, mqe),
4689 bf_get(lpfc_mqe_status, mqe),
4690 mqe->un.mb_words[0], mqe->un.mb_words[1],
4691 mqe->un.mb_words[2], mqe->un.mb_words[3],
4692 mqe->un.mb_words[4], mqe->un.mb_words[5],
4693 mqe->un.mb_words[6], mqe->un.mb_words[7],
4694 mqe->un.mb_words[8], mqe->un.mb_words[9],
4695 mqe->un.mb_words[10], mqe->un.mb_words[11],
4696 mqe->un.mb_words[12], mqe->un.mb_words[13],
4697 mqe->un.mb_words[14], mqe->un.mb_words[15],
4698 mqe->un.mb_words[16], mqe->un.mb_words[50],
4699 mboxq->mcqe.word0,
4700 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4701 mboxq->mcqe.trailer);
4702
4703 if (rc) {
4704 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4705 kfree(mp);
ff78d8f9
JS
4706 rc = -EIO;
4707 goto out_free_mboxq;
da0436e9
JS
4708 }
4709 data_length = mqe->un.mb_words[5];
a0c87cbd 4710 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4711 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4712 kfree(mp);
ff78d8f9
JS
4713 rc = -EIO;
4714 goto out_free_mboxq;
d11e31dd 4715 }
dea3101e 4716
da0436e9
JS
4717 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4718 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4719 kfree(mp);
ff78d8f9
JS
4720 rc = 0;
4721
4722out_free_mboxq:
4723 mempool_free(mboxq, phba->mbox_mem_pool);
4724 return rc;
da0436e9 4725}
e59058c4
JS
4726
4727/**
da0436e9
JS
4728 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4729 * @phba: pointer to lpfc hba data structure.
4730 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4731 * @vpd: pointer to the memory to hold resulting port vpd data.
4732 * @vpd_size: On input, the number of bytes allocated to @vpd.
4733 * On output, the number of data bytes in @vpd.
e59058c4 4734 *
da0436e9
JS
4735 * This routine executes a READ_REV SLI4 mailbox command. In
4736 * addition, this routine gets the port vpd data.
4737 *
4738 * Return codes
af901ca1 4739 * 0 - successful
d439d286 4740 * -ENOMEM - could not allocated memory.
e59058c4 4741 **/
da0436e9
JS
4742static int
4743lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4744 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4745{
da0436e9
JS
4746 int rc = 0;
4747 uint32_t dma_size;
4748 struct lpfc_dmabuf *dmabuf;
4749 struct lpfc_mqe *mqe;
dea3101e 4750
da0436e9
JS
4751 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4752 if (!dmabuf)
4753 return -ENOMEM;
4754
4755 /*
4756 * Get a DMA buffer for the vpd data resulting from the READ_REV
4757 * mailbox command.
a257bf90 4758 */
da0436e9
JS
4759 dma_size = *vpd_size;
4760 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4761 dma_size,
4762 &dmabuf->phys,
4763 GFP_KERNEL);
4764 if (!dmabuf->virt) {
4765 kfree(dmabuf);
4766 return -ENOMEM;
a257bf90 4767 }
da0436e9 4768 memset(dmabuf->virt, 0, dma_size);
a257bf90 4769
da0436e9
JS
4770 /*
4771 * The SLI4 implementation of READ_REV conflicts at word1,
4772 * bits 31:16 and SLI4 adds vpd functionality not present
4773 * in SLI3. This code corrects the conflicts.
1dcb58e5 4774 */
da0436e9
JS
4775 lpfc_read_rev(phba, mboxq);
4776 mqe = &mboxq->u.mqe;
4777 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4778 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4779 mqe->un.read_rev.word1 &= 0x0000FFFF;
4780 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4781 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4782
4783 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4784 if (rc) {
4785 dma_free_coherent(&phba->pcidev->dev, dma_size,
4786 dmabuf->virt, dmabuf->phys);
def9c7a9 4787 kfree(dmabuf);
da0436e9
JS
4788 return -EIO;
4789 }
1dcb58e5 4790
da0436e9
JS
4791 /*
4792 * The available vpd length cannot be bigger than the
4793 * DMA buffer passed to the port. Catch the less than
4794 * case and update the caller's size.
4795 */
4796 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4797 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4798
d7c47992
JS
4799 memcpy(vpd, dmabuf->virt, *vpd_size);
4800
da0436e9
JS
4801 dma_free_coherent(&phba->pcidev->dev, dma_size,
4802 dmabuf->virt, dmabuf->phys);
4803 kfree(dmabuf);
4804 return 0;
dea3101e
JB
4805}
4806
cd1c8301
JS
4807/**
4808 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4809 * @phba: pointer to lpfc hba data structure.
4810 *
4811 * This routine retrieves SLI4 device physical port name this PCI function
4812 * is attached to.
4813 *
4814 * Return codes
4907cb7b 4815 * 0 - successful
cd1c8301
JS
4816 * otherwise - failed to retrieve physical port name
4817 **/
4818static int
4819lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4820{
4821 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
4822 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4823 struct lpfc_controller_attribute *cntl_attr;
4824 struct lpfc_mbx_get_port_name *get_port_name;
4825 void *virtaddr = NULL;
4826 uint32_t alloclen, reqlen;
4827 uint32_t shdr_status, shdr_add_status;
4828 union lpfc_sli4_cfg_shdr *shdr;
4829 char cport_name = 0;
4830 int rc;
4831
4832 /* We assume nothing at this point */
4833 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4834 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4835
4836 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4837 if (!mboxq)
4838 return -ENOMEM;
cd1c8301 4839 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
4840 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4841 lpfc_sli4_read_config(phba);
4842 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4843 goto retrieve_ppname;
cd1c8301
JS
4844
4845 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4846 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4847 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4848 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4849 LPFC_SLI4_MBX_NEMBED);
4850 if (alloclen < reqlen) {
4851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4852 "3084 Allocated DMA memory size (%d) is "
4853 "less than the requested DMA memory size "
4854 "(%d)\n", alloclen, reqlen);
4855 rc = -ENOMEM;
4856 goto out_free_mboxq;
4857 }
4858 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4859 virtaddr = mboxq->sge_array->addr[0];
4860 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4861 shdr = &mbx_cntl_attr->cfg_shdr;
4862 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4863 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4864 if (shdr_status || shdr_add_status || rc) {
4865 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4866 "3085 Mailbox x%x (x%x/x%x) failed, "
4867 "rc:x%x, status:x%x, add_status:x%x\n",
4868 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4869 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4870 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4871 rc, shdr_status, shdr_add_status);
4872 rc = -ENXIO;
4873 goto out_free_mboxq;
4874 }
4875 cntl_attr = &mbx_cntl_attr->cntl_attr;
4876 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4877 phba->sli4_hba.lnk_info.lnk_tp =
4878 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4879 phba->sli4_hba.lnk_info.lnk_no =
4880 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4882 "3086 lnk_type:%d, lnk_numb:%d\n",
4883 phba->sli4_hba.lnk_info.lnk_tp,
4884 phba->sli4_hba.lnk_info.lnk_no);
4885
4886retrieve_ppname:
4887 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4888 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4889 sizeof(struct lpfc_mbx_get_port_name) -
4890 sizeof(struct lpfc_sli4_cfg_mhdr),
4891 LPFC_SLI4_MBX_EMBED);
4892 get_port_name = &mboxq->u.mqe.un.get_port_name;
4893 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4894 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4895 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4896 phba->sli4_hba.lnk_info.lnk_tp);
4897 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4898 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4899 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4900 if (shdr_status || shdr_add_status || rc) {
4901 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4902 "3087 Mailbox x%x (x%x/x%x) failed: "
4903 "rc:x%x, status:x%x, add_status:x%x\n",
4904 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4905 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4906 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4907 rc, shdr_status, shdr_add_status);
4908 rc = -ENXIO;
4909 goto out_free_mboxq;
4910 }
4911 switch (phba->sli4_hba.lnk_info.lnk_no) {
4912 case LPFC_LINK_NUMBER_0:
4913 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4914 &get_port_name->u.response);
4915 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4916 break;
4917 case LPFC_LINK_NUMBER_1:
4918 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4919 &get_port_name->u.response);
4920 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4921 break;
4922 case LPFC_LINK_NUMBER_2:
4923 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4924 &get_port_name->u.response);
4925 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4926 break;
4927 case LPFC_LINK_NUMBER_3:
4928 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4929 &get_port_name->u.response);
4930 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4931 break;
4932 default:
4933 break;
4934 }
4935
4936 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4937 phba->Port[0] = cport_name;
4938 phba->Port[1] = '\0';
4939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4940 "3091 SLI get port name: %s\n", phba->Port);
4941 }
4942
4943out_free_mboxq:
4944 if (rc != MBX_TIMEOUT) {
4945 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4946 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4947 else
4948 mempool_free(mboxq, phba->mbox_mem_pool);
4949 }
4950 return rc;
4951}
4952
e59058c4 4953/**
da0436e9
JS
4954 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4955 * @phba: pointer to lpfc hba data structure.
e59058c4 4956 *
da0436e9
JS
4957 * This routine is called to explicitly arm the SLI4 device's completion and
4958 * event queues
4959 **/
4960static void
4961lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4962{
962bc51b 4963 int fcp_eqidx;
da0436e9
JS
4964
4965 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4966 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
0558056c 4967 fcp_eqidx = 0;
2e90f4b5 4968 if (phba->sli4_hba.fcp_cq) {
67d12733 4969 do {
2e90f4b5
JS
4970 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4971 LPFC_QUEUE_REARM);
67d12733 4972 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
2e90f4b5 4973 }
67d12733
JS
4974 if (phba->sli4_hba.hba_eq) {
4975 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
2e90f4b5 4976 fcp_eqidx++)
67d12733 4977 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
2e90f4b5
JS
4978 LPFC_QUEUE_REARM);
4979 }
da0436e9
JS
4980}
4981
6d368e53
JS
4982/**
4983 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4984 * @phba: Pointer to HBA context object.
4985 * @type: The resource extent type.
b76f2dc9
JS
4986 * @extnt_count: buffer to hold port available extent count.
4987 * @extnt_size: buffer to hold element count per extent.
6d368e53 4988 *
b76f2dc9
JS
4989 * This function calls the port and retrievs the number of available
4990 * extents and their size for a particular extent type.
4991 *
4992 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 4993 **/
b76f2dc9 4994int
6d368e53
JS
4995lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4996 uint16_t *extnt_count, uint16_t *extnt_size)
4997{
4998 int rc = 0;
4999 uint32_t length;
5000 uint32_t mbox_tmo;
5001 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5002 LPFC_MBOXQ_t *mbox;
5003
5004 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5005 if (!mbox)
5006 return -ENOMEM;
5007
5008 /* Find out how many extents are available for this resource type */
5009 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5010 sizeof(struct lpfc_sli4_cfg_mhdr));
5011 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5012 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5013 length, LPFC_SLI4_MBX_EMBED);
5014
5015 /* Send an extents count of 0 - the GET doesn't use it. */
5016 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5017 LPFC_SLI4_MBX_EMBED);
5018 if (unlikely(rc)) {
5019 rc = -EIO;
5020 goto err_exit;
5021 }
5022
5023 if (!phba->sli4_hba.intr_enable)
5024 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5025 else {
a183a15f 5026 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5027 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5028 }
5029 if (unlikely(rc)) {
5030 rc = -EIO;
5031 goto err_exit;
5032 }
5033
5034 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5035 if (bf_get(lpfc_mbox_hdr_status,
5036 &rsrc_info->header.cfg_shdr.response)) {
5037 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5038 "2930 Failed to get resource extents "
5039 "Status 0x%x Add'l Status 0x%x\n",
5040 bf_get(lpfc_mbox_hdr_status,
5041 &rsrc_info->header.cfg_shdr.response),
5042 bf_get(lpfc_mbox_hdr_add_status,
5043 &rsrc_info->header.cfg_shdr.response));
5044 rc = -EIO;
5045 goto err_exit;
5046 }
5047
5048 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5049 &rsrc_info->u.rsp);
5050 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5051 &rsrc_info->u.rsp);
8a9d2e80
JS
5052
5053 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5054 "3162 Retrieved extents type-%d from port: count:%d, "
5055 "size:%d\n", type, *extnt_count, *extnt_size);
5056
5057err_exit:
6d368e53
JS
5058 mempool_free(mbox, phba->mbox_mem_pool);
5059 return rc;
5060}
5061
5062/**
5063 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5064 * @phba: Pointer to HBA context object.
5065 * @type: The extent type to check.
5066 *
5067 * This function reads the current available extents from the port and checks
5068 * if the extent count or extent size has changed since the last access.
5069 * Callers use this routine post port reset to understand if there is a
5070 * extent reprovisioning requirement.
5071 *
5072 * Returns:
5073 * -Error: error indicates problem.
5074 * 1: Extent count or size has changed.
5075 * 0: No changes.
5076 **/
5077static int
5078lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5079{
5080 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5081 uint16_t size_diff, rsrc_ext_size;
5082 int rc = 0;
5083 struct lpfc_rsrc_blks *rsrc_entry;
5084 struct list_head *rsrc_blk_list = NULL;
5085
5086 size_diff = 0;
5087 curr_ext_cnt = 0;
5088 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5089 &rsrc_ext_cnt,
5090 &rsrc_ext_size);
5091 if (unlikely(rc))
5092 return -EIO;
5093
5094 switch (type) {
5095 case LPFC_RSC_TYPE_FCOE_RPI:
5096 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5097 break;
5098 case LPFC_RSC_TYPE_FCOE_VPI:
5099 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5100 break;
5101 case LPFC_RSC_TYPE_FCOE_XRI:
5102 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5103 break;
5104 case LPFC_RSC_TYPE_FCOE_VFI:
5105 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5106 break;
5107 default:
5108 break;
5109 }
5110
5111 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5112 curr_ext_cnt++;
5113 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5114 size_diff++;
5115 }
5116
5117 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5118 rc = 1;
5119
5120 return rc;
5121}
5122
5123/**
5124 * lpfc_sli4_cfg_post_extnts -
5125 * @phba: Pointer to HBA context object.
5126 * @extnt_cnt - number of available extents.
5127 * @type - the extent type (rpi, xri, vfi, vpi).
5128 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5129 * @mbox - pointer to the caller's allocated mailbox structure.
5130 *
5131 * This function executes the extents allocation request. It also
5132 * takes care of the amount of memory needed to allocate or get the
5133 * allocated extents. It is the caller's responsibility to evaluate
5134 * the response.
5135 *
5136 * Returns:
5137 * -Error: Error value describes the condition found.
5138 * 0: if successful
5139 **/
5140static int
8a9d2e80 5141lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5142 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5143{
5144 int rc = 0;
5145 uint32_t req_len;
5146 uint32_t emb_len;
5147 uint32_t alloc_len, mbox_tmo;
5148
5149 /* Calculate the total requested length of the dma memory */
8a9d2e80 5150 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5151
5152 /*
5153 * Calculate the size of an embedded mailbox. The uint32_t
5154 * accounts for extents-specific word.
5155 */
5156 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5157 sizeof(uint32_t);
5158
5159 /*
5160 * Presume the allocation and response will fit into an embedded
5161 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5162 */
5163 *emb = LPFC_SLI4_MBX_EMBED;
5164 if (req_len > emb_len) {
8a9d2e80 5165 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5166 sizeof(union lpfc_sli4_cfg_shdr) +
5167 sizeof(uint32_t);
5168 *emb = LPFC_SLI4_MBX_NEMBED;
5169 }
5170
5171 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5172 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5173 req_len, *emb);
5174 if (alloc_len < req_len) {
5175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5176 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5177 "less than the requested DMA memory "
5178 "size (x%x)\n", alloc_len, req_len);
5179 return -ENOMEM;
5180 }
8a9d2e80 5181 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5182 if (unlikely(rc))
5183 return -EIO;
5184
5185 if (!phba->sli4_hba.intr_enable)
5186 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5187 else {
a183a15f 5188 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5189 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5190 }
5191
5192 if (unlikely(rc))
5193 rc = -EIO;
5194 return rc;
5195}
5196
5197/**
5198 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5199 * @phba: Pointer to HBA context object.
5200 * @type: The resource extent type to allocate.
5201 *
5202 * This function allocates the number of elements for the specified
5203 * resource type.
5204 **/
5205static int
5206lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5207{
5208 bool emb = false;
5209 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5210 uint16_t rsrc_id, rsrc_start, j, k;
5211 uint16_t *ids;
5212 int i, rc;
5213 unsigned long longs;
5214 unsigned long *bmask;
5215 struct lpfc_rsrc_blks *rsrc_blks;
5216 LPFC_MBOXQ_t *mbox;
5217 uint32_t length;
5218 struct lpfc_id_range *id_array = NULL;
5219 void *virtaddr = NULL;
5220 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5221 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5222 struct list_head *ext_blk_list;
5223
5224 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5225 &rsrc_cnt,
5226 &rsrc_size);
5227 if (unlikely(rc))
5228 return -EIO;
5229
5230 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5231 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5232 "3009 No available Resource Extents "
5233 "for resource type 0x%x: Count: 0x%x, "
5234 "Size 0x%x\n", type, rsrc_cnt,
5235 rsrc_size);
5236 return -ENOMEM;
5237 }
5238
8a9d2e80
JS
5239 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5240 "2903 Post resource extents type-0x%x: "
5241 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5242
5243 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5244 if (!mbox)
5245 return -ENOMEM;
5246
8a9d2e80 5247 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5248 if (unlikely(rc)) {
5249 rc = -EIO;
5250 goto err_exit;
5251 }
5252
5253 /*
5254 * Figure out where the response is located. Then get local pointers
5255 * to the response data. The port does not guarantee to respond to
5256 * all extents counts request so update the local variable with the
5257 * allocated count from the port.
5258 */
5259 if (emb == LPFC_SLI4_MBX_EMBED) {
5260 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5261 id_array = &rsrc_ext->u.rsp.id[0];
5262 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5263 } else {
5264 virtaddr = mbox->sge_array->addr[0];
5265 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5266 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5267 id_array = &n_rsrc->id;
5268 }
5269
5270 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5271 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5272
5273 /*
5274 * Based on the resource size and count, correct the base and max
5275 * resource values.
5276 */
5277 length = sizeof(struct lpfc_rsrc_blks);
5278 switch (type) {
5279 case LPFC_RSC_TYPE_FCOE_RPI:
5280 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5281 sizeof(unsigned long),
5282 GFP_KERNEL);
5283 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5284 rc = -ENOMEM;
5285 goto err_exit;
5286 }
5287 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5288 sizeof(uint16_t),
5289 GFP_KERNEL);
5290 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5291 kfree(phba->sli4_hba.rpi_bmask);
5292 rc = -ENOMEM;
5293 goto err_exit;
5294 }
5295
5296 /*
5297 * The next_rpi was initialized with the maximum available
5298 * count but the port may allocate a smaller number. Catch
5299 * that case and update the next_rpi.
5300 */
5301 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5302
5303 /* Initialize local ptrs for common extent processing later. */
5304 bmask = phba->sli4_hba.rpi_bmask;
5305 ids = phba->sli4_hba.rpi_ids;
5306 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5307 break;
5308 case LPFC_RSC_TYPE_FCOE_VPI:
5309 phba->vpi_bmask = kzalloc(longs *
5310 sizeof(unsigned long),
5311 GFP_KERNEL);
5312 if (unlikely(!phba->vpi_bmask)) {
5313 rc = -ENOMEM;
5314 goto err_exit;
5315 }
5316 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5317 sizeof(uint16_t),
5318 GFP_KERNEL);
5319 if (unlikely(!phba->vpi_ids)) {
5320 kfree(phba->vpi_bmask);
5321 rc = -ENOMEM;
5322 goto err_exit;
5323 }
5324
5325 /* Initialize local ptrs for common extent processing later. */
5326 bmask = phba->vpi_bmask;
5327 ids = phba->vpi_ids;
5328 ext_blk_list = &phba->lpfc_vpi_blk_list;
5329 break;
5330 case LPFC_RSC_TYPE_FCOE_XRI:
5331 phba->sli4_hba.xri_bmask = kzalloc(longs *
5332 sizeof(unsigned long),
5333 GFP_KERNEL);
5334 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5335 rc = -ENOMEM;
5336 goto err_exit;
5337 }
8a9d2e80 5338 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5339 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5340 sizeof(uint16_t),
5341 GFP_KERNEL);
5342 if (unlikely(!phba->sli4_hba.xri_ids)) {
5343 kfree(phba->sli4_hba.xri_bmask);
5344 rc = -ENOMEM;
5345 goto err_exit;
5346 }
5347
5348 /* Initialize local ptrs for common extent processing later. */
5349 bmask = phba->sli4_hba.xri_bmask;
5350 ids = phba->sli4_hba.xri_ids;
5351 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5352 break;
5353 case LPFC_RSC_TYPE_FCOE_VFI:
5354 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5355 sizeof(unsigned long),
5356 GFP_KERNEL);
5357 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5358 rc = -ENOMEM;
5359 goto err_exit;
5360 }
5361 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5362 sizeof(uint16_t),
5363 GFP_KERNEL);
5364 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5365 kfree(phba->sli4_hba.vfi_bmask);
5366 rc = -ENOMEM;
5367 goto err_exit;
5368 }
5369
5370 /* Initialize local ptrs for common extent processing later. */
5371 bmask = phba->sli4_hba.vfi_bmask;
5372 ids = phba->sli4_hba.vfi_ids;
5373 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5374 break;
5375 default:
5376 /* Unsupported Opcode. Fail call. */
5377 id_array = NULL;
5378 bmask = NULL;
5379 ids = NULL;
5380 ext_blk_list = NULL;
5381 goto err_exit;
5382 }
5383
5384 /*
5385 * Complete initializing the extent configuration with the
5386 * allocated ids assigned to this function. The bitmask serves
5387 * as an index into the array and manages the available ids. The
5388 * array just stores the ids communicated to the port via the wqes.
5389 */
5390 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5391 if ((i % 2) == 0)
5392 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5393 &id_array[k]);
5394 else
5395 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5396 &id_array[k]);
5397
5398 rsrc_blks = kzalloc(length, GFP_KERNEL);
5399 if (unlikely(!rsrc_blks)) {
5400 rc = -ENOMEM;
5401 kfree(bmask);
5402 kfree(ids);
5403 goto err_exit;
5404 }
5405 rsrc_blks->rsrc_start = rsrc_id;
5406 rsrc_blks->rsrc_size = rsrc_size;
5407 list_add_tail(&rsrc_blks->list, ext_blk_list);
5408 rsrc_start = rsrc_id;
5409 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5410 phba->sli4_hba.scsi_xri_start = rsrc_start +
5411 lpfc_sli4_get_els_iocb_cnt(phba);
5412
5413 while (rsrc_id < (rsrc_start + rsrc_size)) {
5414 ids[j] = rsrc_id;
5415 rsrc_id++;
5416 j++;
5417 }
5418 /* Entire word processed. Get next word.*/
5419 if ((i % 2) == 1)
5420 k++;
5421 }
5422 err_exit:
5423 lpfc_sli4_mbox_cmd_free(phba, mbox);
5424 return rc;
5425}
5426
5427/**
5428 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5429 * @phba: Pointer to HBA context object.
5430 * @type: the extent's type.
5431 *
5432 * This function deallocates all extents of a particular resource type.
5433 * SLI4 does not allow for deallocating a particular extent range. It
5434 * is the caller's responsibility to release all kernel memory resources.
5435 **/
5436static int
5437lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5438{
5439 int rc;
5440 uint32_t length, mbox_tmo = 0;
5441 LPFC_MBOXQ_t *mbox;
5442 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5443 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5444
5445 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5446 if (!mbox)
5447 return -ENOMEM;
5448
5449 /*
5450 * This function sends an embedded mailbox because it only sends the
5451 * the resource type. All extents of this type are released by the
5452 * port.
5453 */
5454 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5455 sizeof(struct lpfc_sli4_cfg_mhdr));
5456 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5457 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5458 length, LPFC_SLI4_MBX_EMBED);
5459
5460 /* Send an extents count of 0 - the dealloc doesn't use it. */
5461 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5462 LPFC_SLI4_MBX_EMBED);
5463 if (unlikely(rc)) {
5464 rc = -EIO;
5465 goto out_free_mbox;
5466 }
5467 if (!phba->sli4_hba.intr_enable)
5468 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5469 else {
a183a15f 5470 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5471 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5472 }
5473 if (unlikely(rc)) {
5474 rc = -EIO;
5475 goto out_free_mbox;
5476 }
5477
5478 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5479 if (bf_get(lpfc_mbox_hdr_status,
5480 &dealloc_rsrc->header.cfg_shdr.response)) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5482 "2919 Failed to release resource extents "
5483 "for type %d - Status 0x%x Add'l Status 0x%x. "
5484 "Resource memory not released.\n",
5485 type,
5486 bf_get(lpfc_mbox_hdr_status,
5487 &dealloc_rsrc->header.cfg_shdr.response),
5488 bf_get(lpfc_mbox_hdr_add_status,
5489 &dealloc_rsrc->header.cfg_shdr.response));
5490 rc = -EIO;
5491 goto out_free_mbox;
5492 }
5493
5494 /* Release kernel memory resources for the specific type. */
5495 switch (type) {
5496 case LPFC_RSC_TYPE_FCOE_VPI:
5497 kfree(phba->vpi_bmask);
5498 kfree(phba->vpi_ids);
5499 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5500 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5501 &phba->lpfc_vpi_blk_list, list) {
5502 list_del_init(&rsrc_blk->list);
5503 kfree(rsrc_blk);
5504 }
16a3a208 5505 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5506 break;
5507 case LPFC_RSC_TYPE_FCOE_XRI:
5508 kfree(phba->sli4_hba.xri_bmask);
5509 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5510 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5511 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5512 list_del_init(&rsrc_blk->list);
5513 kfree(rsrc_blk);
5514 }
5515 break;
5516 case LPFC_RSC_TYPE_FCOE_VFI:
5517 kfree(phba->sli4_hba.vfi_bmask);
5518 kfree(phba->sli4_hba.vfi_ids);
5519 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5520 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5521 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5522 list_del_init(&rsrc_blk->list);
5523 kfree(rsrc_blk);
5524 }
5525 break;
5526 case LPFC_RSC_TYPE_FCOE_RPI:
5527 /* RPI bitmask and physical id array are cleaned up earlier. */
5528 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5529 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5530 list_del_init(&rsrc_blk->list);
5531 kfree(rsrc_blk);
5532 }
5533 break;
5534 default:
5535 break;
5536 }
5537
5538 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5539
5540 out_free_mbox:
5541 mempool_free(mbox, phba->mbox_mem_pool);
5542 return rc;
5543}
5544
5545/**
5546 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5547 * @phba: Pointer to HBA context object.
5548 *
5549 * This function allocates all SLI4 resource identifiers.
5550 **/
5551int
5552lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5553{
5554 int i, rc, error = 0;
5555 uint16_t count, base;
5556 unsigned long longs;
5557
ff78d8f9
JS
5558 if (!phba->sli4_hba.rpi_hdrs_in_use)
5559 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5560 if (phba->sli4_hba.extents_in_use) {
5561 /*
5562 * The port supports resource extents. The XRI, VPI, VFI, RPI
5563 * resource extent count must be read and allocated before
5564 * provisioning the resource id arrays.
5565 */
5566 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5567 LPFC_IDX_RSRC_RDY) {
5568 /*
5569 * Extent-based resources are set - the driver could
5570 * be in a port reset. Figure out if any corrective
5571 * actions need to be taken.
5572 */
5573 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5574 LPFC_RSC_TYPE_FCOE_VFI);
5575 if (rc != 0)
5576 error++;
5577 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5578 LPFC_RSC_TYPE_FCOE_VPI);
5579 if (rc != 0)
5580 error++;
5581 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5582 LPFC_RSC_TYPE_FCOE_XRI);
5583 if (rc != 0)
5584 error++;
5585 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5586 LPFC_RSC_TYPE_FCOE_RPI);
5587 if (rc != 0)
5588 error++;
5589
5590 /*
5591 * It's possible that the number of resources
5592 * provided to this port instance changed between
5593 * resets. Detect this condition and reallocate
5594 * resources. Otherwise, there is no action.
5595 */
5596 if (error) {
5597 lpfc_printf_log(phba, KERN_INFO,
5598 LOG_MBOX | LOG_INIT,
5599 "2931 Detected extent resource "
5600 "change. Reallocating all "
5601 "extents.\n");
5602 rc = lpfc_sli4_dealloc_extent(phba,
5603 LPFC_RSC_TYPE_FCOE_VFI);
5604 rc = lpfc_sli4_dealloc_extent(phba,
5605 LPFC_RSC_TYPE_FCOE_VPI);
5606 rc = lpfc_sli4_dealloc_extent(phba,
5607 LPFC_RSC_TYPE_FCOE_XRI);
5608 rc = lpfc_sli4_dealloc_extent(phba,
5609 LPFC_RSC_TYPE_FCOE_RPI);
5610 } else
5611 return 0;
5612 }
5613
5614 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5615 if (unlikely(rc))
5616 goto err_exit;
5617
5618 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5619 if (unlikely(rc))
5620 goto err_exit;
5621
5622 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5623 if (unlikely(rc))
5624 goto err_exit;
5625
5626 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5627 if (unlikely(rc))
5628 goto err_exit;
5629 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5630 LPFC_IDX_RSRC_RDY);
5631 return rc;
5632 } else {
5633 /*
5634 * The port does not support resource extents. The XRI, VPI,
5635 * VFI, RPI resource ids were determined from READ_CONFIG.
5636 * Just allocate the bitmasks and provision the resource id
5637 * arrays. If a port reset is active, the resources don't
5638 * need any action - just exit.
5639 */
5640 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
5641 LPFC_IDX_RSRC_RDY) {
5642 lpfc_sli4_dealloc_resource_identifiers(phba);
5643 lpfc_sli4_remove_rpis(phba);
5644 }
6d368e53
JS
5645 /* RPIs. */
5646 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
5647 if (count <= 0) {
5648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5649 "3279 Invalid provisioning of "
5650 "rpi:%d\n", count);
5651 rc = -EINVAL;
5652 goto err_exit;
5653 }
6d368e53
JS
5654 base = phba->sli4_hba.max_cfg_param.rpi_base;
5655 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5656 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5657 sizeof(unsigned long),
5658 GFP_KERNEL);
5659 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5660 rc = -ENOMEM;
5661 goto err_exit;
5662 }
5663 phba->sli4_hba.rpi_ids = kzalloc(count *
5664 sizeof(uint16_t),
5665 GFP_KERNEL);
5666 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5667 rc = -ENOMEM;
5668 goto free_rpi_bmask;
5669 }
5670
5671 for (i = 0; i < count; i++)
5672 phba->sli4_hba.rpi_ids[i] = base + i;
5673
5674 /* VPIs. */
5675 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
5676 if (count <= 0) {
5677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5678 "3280 Invalid provisioning of "
5679 "vpi:%d\n", count);
5680 rc = -EINVAL;
5681 goto free_rpi_ids;
5682 }
6d368e53
JS
5683 base = phba->sli4_hba.max_cfg_param.vpi_base;
5684 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5685 phba->vpi_bmask = kzalloc(longs *
5686 sizeof(unsigned long),
5687 GFP_KERNEL);
5688 if (unlikely(!phba->vpi_bmask)) {
5689 rc = -ENOMEM;
5690 goto free_rpi_ids;
5691 }
5692 phba->vpi_ids = kzalloc(count *
5693 sizeof(uint16_t),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->vpi_ids)) {
5696 rc = -ENOMEM;
5697 goto free_vpi_bmask;
5698 }
5699
5700 for (i = 0; i < count; i++)
5701 phba->vpi_ids[i] = base + i;
5702
5703 /* XRIs. */
5704 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
5705 if (count <= 0) {
5706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5707 "3281 Invalid provisioning of "
5708 "xri:%d\n", count);
5709 rc = -EINVAL;
5710 goto free_vpi_ids;
5711 }
6d368e53
JS
5712 base = phba->sli4_hba.max_cfg_param.xri_base;
5713 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5714 phba->sli4_hba.xri_bmask = kzalloc(longs *
5715 sizeof(unsigned long),
5716 GFP_KERNEL);
5717 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5718 rc = -ENOMEM;
5719 goto free_vpi_ids;
5720 }
41899be7 5721 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5722 phba->sli4_hba.xri_ids = kzalloc(count *
5723 sizeof(uint16_t),
5724 GFP_KERNEL);
5725 if (unlikely(!phba->sli4_hba.xri_ids)) {
5726 rc = -ENOMEM;
5727 goto free_xri_bmask;
5728 }
5729
5730 for (i = 0; i < count; i++)
5731 phba->sli4_hba.xri_ids[i] = base + i;
5732
5733 /* VFIs. */
5734 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
5735 if (count <= 0) {
5736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5737 "3282 Invalid provisioning of "
5738 "vfi:%d\n", count);
5739 rc = -EINVAL;
5740 goto free_xri_ids;
5741 }
6d368e53
JS
5742 base = phba->sli4_hba.max_cfg_param.vfi_base;
5743 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5744 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5745 sizeof(unsigned long),
5746 GFP_KERNEL);
5747 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5748 rc = -ENOMEM;
5749 goto free_xri_ids;
5750 }
5751 phba->sli4_hba.vfi_ids = kzalloc(count *
5752 sizeof(uint16_t),
5753 GFP_KERNEL);
5754 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5755 rc = -ENOMEM;
5756 goto free_vfi_bmask;
5757 }
5758
5759 for (i = 0; i < count; i++)
5760 phba->sli4_hba.vfi_ids[i] = base + i;
5761
5762 /*
5763 * Mark all resources ready. An HBA reset doesn't need
5764 * to reset the initialization.
5765 */
5766 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5767 LPFC_IDX_RSRC_RDY);
5768 return 0;
5769 }
5770
5771 free_vfi_bmask:
5772 kfree(phba->sli4_hba.vfi_bmask);
5773 free_xri_ids:
5774 kfree(phba->sli4_hba.xri_ids);
5775 free_xri_bmask:
5776 kfree(phba->sli4_hba.xri_bmask);
5777 free_vpi_ids:
5778 kfree(phba->vpi_ids);
5779 free_vpi_bmask:
5780 kfree(phba->vpi_bmask);
5781 free_rpi_ids:
5782 kfree(phba->sli4_hba.rpi_ids);
5783 free_rpi_bmask:
5784 kfree(phba->sli4_hba.rpi_bmask);
5785 err_exit:
5786 return rc;
5787}
5788
5789/**
5790 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5791 * @phba: Pointer to HBA context object.
5792 *
5793 * This function allocates the number of elements for the specified
5794 * resource type.
5795 **/
5796int
5797lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5798{
5799 if (phba->sli4_hba.extents_in_use) {
5800 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5801 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5802 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5803 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5804 } else {
5805 kfree(phba->vpi_bmask);
16a3a208 5806 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5807 kfree(phba->vpi_ids);
5808 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5809 kfree(phba->sli4_hba.xri_bmask);
5810 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5811 kfree(phba->sli4_hba.vfi_bmask);
5812 kfree(phba->sli4_hba.vfi_ids);
5813 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5814 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5815 }
5816
5817 return 0;
5818}
5819
b76f2dc9
JS
5820/**
5821 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5822 * @phba: Pointer to HBA context object.
5823 * @type: The resource extent type.
5824 * @extnt_count: buffer to hold port extent count response
5825 * @extnt_size: buffer to hold port extent size response.
5826 *
5827 * This function calls the port to read the host allocated extents
5828 * for a particular type.
5829 **/
5830int
5831lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5832 uint16_t *extnt_cnt, uint16_t *extnt_size)
5833{
5834 bool emb;
5835 int rc = 0;
5836 uint16_t curr_blks = 0;
5837 uint32_t req_len, emb_len;
5838 uint32_t alloc_len, mbox_tmo;
5839 struct list_head *blk_list_head;
5840 struct lpfc_rsrc_blks *rsrc_blk;
5841 LPFC_MBOXQ_t *mbox;
5842 void *virtaddr = NULL;
5843 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5844 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5845 union lpfc_sli4_cfg_shdr *shdr;
5846
5847 switch (type) {
5848 case LPFC_RSC_TYPE_FCOE_VPI:
5849 blk_list_head = &phba->lpfc_vpi_blk_list;
5850 break;
5851 case LPFC_RSC_TYPE_FCOE_XRI:
5852 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5853 break;
5854 case LPFC_RSC_TYPE_FCOE_VFI:
5855 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5856 break;
5857 case LPFC_RSC_TYPE_FCOE_RPI:
5858 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5859 break;
5860 default:
5861 return -EIO;
5862 }
5863
5864 /* Count the number of extents currently allocatd for this type. */
5865 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5866 if (curr_blks == 0) {
5867 /*
5868 * The GET_ALLOCATED mailbox does not return the size,
5869 * just the count. The size should be just the size
5870 * stored in the current allocated block and all sizes
5871 * for an extent type are the same so set the return
5872 * value now.
5873 */
5874 *extnt_size = rsrc_blk->rsrc_size;
5875 }
5876 curr_blks++;
5877 }
5878
5879 /* Calculate the total requested length of the dma memory. */
5880 req_len = curr_blks * sizeof(uint16_t);
5881
5882 /*
5883 * Calculate the size of an embedded mailbox. The uint32_t
5884 * accounts for extents-specific word.
5885 */
5886 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5887 sizeof(uint32_t);
5888
5889 /*
5890 * Presume the allocation and response will fit into an embedded
5891 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5892 */
5893 emb = LPFC_SLI4_MBX_EMBED;
5894 req_len = emb_len;
5895 if (req_len > emb_len) {
5896 req_len = curr_blks * sizeof(uint16_t) +
5897 sizeof(union lpfc_sli4_cfg_shdr) +
5898 sizeof(uint32_t);
5899 emb = LPFC_SLI4_MBX_NEMBED;
5900 }
5901
5902 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5903 if (!mbox)
5904 return -ENOMEM;
5905 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5906
5907 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5908 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5909 req_len, emb);
5910 if (alloc_len < req_len) {
5911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5912 "2983 Allocated DMA memory size (x%x) is "
5913 "less than the requested DMA memory "
5914 "size (x%x)\n", alloc_len, req_len);
5915 rc = -ENOMEM;
5916 goto err_exit;
5917 }
5918 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5919 if (unlikely(rc)) {
5920 rc = -EIO;
5921 goto err_exit;
5922 }
5923
5924 if (!phba->sli4_hba.intr_enable)
5925 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5926 else {
a183a15f 5927 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
5928 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5929 }
5930
5931 if (unlikely(rc)) {
5932 rc = -EIO;
5933 goto err_exit;
5934 }
5935
5936 /*
5937 * Figure out where the response is located. Then get local pointers
5938 * to the response data. The port does not guarantee to respond to
5939 * all extents counts request so update the local variable with the
5940 * allocated count from the port.
5941 */
5942 if (emb == LPFC_SLI4_MBX_EMBED) {
5943 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5944 shdr = &rsrc_ext->header.cfg_shdr;
5945 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5946 } else {
5947 virtaddr = mbox->sge_array->addr[0];
5948 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5949 shdr = &n_rsrc->cfg_shdr;
5950 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5951 }
5952
5953 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5955 "2984 Failed to read allocated resources "
5956 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5957 type,
5958 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5959 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5960 rc = -EIO;
5961 goto err_exit;
5962 }
5963 err_exit:
5964 lpfc_sli4_mbox_cmd_free(phba, mbox);
5965 return rc;
5966}
5967
8a9d2e80
JS
5968/**
5969 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5970 * @phba: pointer to lpfc hba data structure.
5971 *
5972 * This routine walks the list of els buffers that have been allocated and
5973 * repost them to the port by using SGL block post. This is needed after a
5974 * pci_function_reset/warm_start or start. It attempts to construct blocks
5975 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5976 * SGL block post mailbox commands to post them to the port. For single els
5977 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5978 * mailbox command for posting.
5979 *
5980 * Returns: 0 = success, non-zero failure.
5981 **/
5982static int
5983lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5984{
5985 struct lpfc_sglq *sglq_entry = NULL;
5986 struct lpfc_sglq *sglq_entry_next = NULL;
5987 struct lpfc_sglq *sglq_entry_first = NULL;
711ea882 5988 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
5989 int last_xritag = NO_XRI;
5990 LIST_HEAD(prep_sgl_list);
5991 LIST_HEAD(blck_sgl_list);
5992 LIST_HEAD(allc_sgl_list);
5993 LIST_HEAD(post_sgl_list);
5994 LIST_HEAD(free_sgl_list);
5995
38c20673 5996 spin_lock_irq(&phba->hbalock);
8a9d2e80 5997 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
38c20673 5998 spin_unlock_irq(&phba->hbalock);
8a9d2e80 5999
711ea882 6000 total_cnt = phba->sli4_hba.els_xri_cnt;
8a9d2e80
JS
6001 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6002 &allc_sgl_list, list) {
6003 list_del_init(&sglq_entry->list);
6004 block_cnt++;
6005 if ((last_xritag != NO_XRI) &&
6006 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6007 /* a hole in xri block, form a sgl posting block */
6008 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6009 post_cnt = block_cnt - 1;
6010 /* prepare list for next posting block */
6011 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6012 block_cnt = 1;
6013 } else {
6014 /* prepare list for next posting block */
6015 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6016 /* enough sgls for non-embed sgl mbox command */
6017 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6018 list_splice_init(&prep_sgl_list,
6019 &blck_sgl_list);
6020 post_cnt = block_cnt;
6021 block_cnt = 0;
6022 }
6023 }
6024 num_posted++;
6025
6026 /* keep track of last sgl's xritag */
6027 last_xritag = sglq_entry->sli4_xritag;
6028
6029 /* end of repost sgl list condition for els buffers */
6030 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6031 if (post_cnt == 0) {
6032 list_splice_init(&prep_sgl_list,
6033 &blck_sgl_list);
6034 post_cnt = block_cnt;
6035 } else if (block_cnt == 1) {
6036 status = lpfc_sli4_post_sgl(phba,
6037 sglq_entry->phys, 0,
6038 sglq_entry->sli4_xritag);
6039 if (!status) {
6040 /* successful, put sgl to posted list */
6041 list_add_tail(&sglq_entry->list,
6042 &post_sgl_list);
6043 } else {
6044 /* Failure, put sgl to free list */
6045 lpfc_printf_log(phba, KERN_WARNING,
6046 LOG_SLI,
6047 "3159 Failed to post els "
6048 "sgl, xritag:x%x\n",
6049 sglq_entry->sli4_xritag);
6050 list_add_tail(&sglq_entry->list,
6051 &free_sgl_list);
711ea882 6052 total_cnt--;
8a9d2e80
JS
6053 }
6054 }
6055 }
6056
6057 /* continue until a nembed page worth of sgls */
6058 if (post_cnt == 0)
6059 continue;
6060
6061 /* post the els buffer list sgls as a block */
6062 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6063 post_cnt);
6064
6065 if (!status) {
6066 /* success, put sgl list to posted sgl list */
6067 list_splice_init(&blck_sgl_list, &post_sgl_list);
6068 } else {
6069 /* Failure, put sgl list to free sgl list */
6070 sglq_entry_first = list_first_entry(&blck_sgl_list,
6071 struct lpfc_sglq,
6072 list);
6073 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6074 "3160 Failed to post els sgl-list, "
6075 "xritag:x%x-x%x\n",
6076 sglq_entry_first->sli4_xritag,
6077 (sglq_entry_first->sli4_xritag +
6078 post_cnt - 1));
6079 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6080 total_cnt -= post_cnt;
8a9d2e80
JS
6081 }
6082
6083 /* don't reset xirtag due to hole in xri block */
6084 if (block_cnt == 0)
6085 last_xritag = NO_XRI;
6086
6087 /* reset els sgl post count for next round of posting */
6088 post_cnt = 0;
6089 }
711ea882
JS
6090 /* update the number of XRIs posted for ELS */
6091 phba->sli4_hba.els_xri_cnt = total_cnt;
8a9d2e80
JS
6092
6093 /* free the els sgls failed to post */
6094 lpfc_free_sgl_list(phba, &free_sgl_list);
6095
6096 /* push els sgls posted to the availble list */
6097 if (!list_empty(&post_sgl_list)) {
38c20673 6098 spin_lock_irq(&phba->hbalock);
8a9d2e80
JS
6099 list_splice_init(&post_sgl_list,
6100 &phba->sli4_hba.lpfc_sgl_list);
38c20673 6101 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6102 } else {
6103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6104 "3161 Failure to post els sgl to port.\n");
6105 return -EIO;
6106 }
6107 return 0;
6108}
6109
da0436e9
JS
6110/**
6111 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6112 * @phba: Pointer to HBA context object.
6113 *
6114 * This function is the main SLI4 device intialization PCI function. This
6115 * function is called by the HBA intialization code, HBA reset code and
6116 * HBA error attention handler code. Caller is not required to hold any
6117 * locks.
6118 **/
6119int
6120lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6121{
6122 int rc;
6123 LPFC_MBOXQ_t *mboxq;
6124 struct lpfc_mqe *mqe;
6125 uint8_t *vpd;
6126 uint32_t vpd_size;
6127 uint32_t ftr_rsp = 0;
6128 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6129 struct lpfc_vport *vport = phba->pport;
6130 struct lpfc_dmabuf *mp;
6131
6132 /* Perform a PCI function reset to start from clean */
6133 rc = lpfc_pci_function_reset(phba);
6134 if (unlikely(rc))
6135 return -ENODEV;
6136
6137 /* Check the HBA Host Status Register for readyness */
6138 rc = lpfc_sli4_post_status_check(phba);
6139 if (unlikely(rc))
6140 return -ENODEV;
6141 else {
6142 spin_lock_irq(&phba->hbalock);
6143 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6144 spin_unlock_irq(&phba->hbalock);
6145 }
6146
6147 /*
6148 * Allocate a single mailbox container for initializing the
6149 * port.
6150 */
6151 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6152 if (!mboxq)
6153 return -ENOMEM;
6154
da0436e9 6155 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6156 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6157 vpd = kzalloc(vpd_size, GFP_KERNEL);
6158 if (!vpd) {
6159 rc = -ENOMEM;
6160 goto out_free_mbox;
6161 }
6162
6163 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6164 if (unlikely(rc)) {
6165 kfree(vpd);
6166 goto out_free_mbox;
6167 }
572709e2 6168
da0436e9 6169 mqe = &mboxq->u.mqe;
f1126688
JS
6170 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6171 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
76a95d75
JS
6172 phba->hba_flag |= HBA_FCOE_MODE;
6173 else
6174 phba->hba_flag &= ~HBA_FCOE_MODE;
45ed1190
JS
6175
6176 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6177 LPFC_DCBX_CEE_MODE)
6178 phba->hba_flag |= HBA_FIP_SUPPORT;
6179 else
6180 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6181
4f2e66c6
JS
6182 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6183
c31098ce 6184 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6185 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6186 "0376 READ_REV Error. SLI Level %d "
6187 "FCoE enabled %d\n",
76a95d75 6188 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6189 rc = -EIO;
76a95d75
JS
6190 kfree(vpd);
6191 goto out_free_mbox;
da0436e9 6192 }
cd1c8301 6193
ff78d8f9
JS
6194 /*
6195 * Continue initialization with default values even if driver failed
6196 * to read FCoE param config regions, only read parameters if the
6197 * board is FCoE
6198 */
6199 if (phba->hba_flag & HBA_FCOE_MODE &&
6200 lpfc_sli4_read_fcoe_params(phba))
6201 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6202 "2570 Failed to read FCoE parameters\n");
6203
cd1c8301
JS
6204 /*
6205 * Retrieve sli4 device physical port name, failure of doing it
6206 * is considered as non-fatal.
6207 */
6208 rc = lpfc_sli4_retrieve_pport_name(phba);
6209 if (!rc)
6210 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6211 "3080 Successful retrieving SLI4 device "
6212 "physical port name: %s.\n", phba->Port);
6213
da0436e9
JS
6214 /*
6215 * Evaluate the read rev and vpd data. Populate the driver
6216 * state with the results. If this routine fails, the failure
6217 * is not fatal as the driver will use generic values.
6218 */
6219 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6220 if (unlikely(!rc)) {
6221 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6222 "0377 Error %d parsing vpd. "
6223 "Using defaults.\n", rc);
6224 rc = 0;
6225 }
76a95d75 6226 kfree(vpd);
da0436e9 6227
f1126688
JS
6228 /* Save information as VPD data */
6229 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6230 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6231 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6232 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6233 &mqe->un.read_rev);
6234 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6235 &mqe->un.read_rev);
6236 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6237 &mqe->un.read_rev);
6238 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6239 &mqe->un.read_rev);
6240 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6241 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6242 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6243 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6244 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6245 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6246 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6247 "(%d):0380 READ_REV Status x%x "
6248 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6249 mboxq->vport ? mboxq->vport->vpi : 0,
6250 bf_get(lpfc_mqe_status, mqe),
6251 phba->vpd.rev.opFwName,
6252 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6253 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6254
572709e2
JS
6255 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6256 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6257 if (phba->pport->cfg_lun_queue_depth > rc) {
6258 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6259 "3362 LUN queue depth changed from %d to %d\n",
6260 phba->pport->cfg_lun_queue_depth, rc);
6261 phba->pport->cfg_lun_queue_depth = rc;
6262 }
6263
6264
da0436e9
JS
6265 /*
6266 * Discover the port's supported feature set and match it against the
6267 * hosts requests.
6268 */
6269 lpfc_request_features(phba, mboxq);
6270 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6271 if (unlikely(rc)) {
6272 rc = -EIO;
76a95d75 6273 goto out_free_mbox;
da0436e9
JS
6274 }
6275
6276 /*
6277 * The port must support FCP initiator mode as this is the
6278 * only mode running in the host.
6279 */
6280 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6281 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6282 "0378 No support for fcpi mode.\n");
6283 ftr_rsp++;
6284 }
fedd3b7b
JS
6285 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6286 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6287 else
6288 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6289 /*
6290 * If the port cannot support the host's requested features
6291 * then turn off the global config parameters to disable the
6292 * feature in the driver. This is not a fatal error.
6293 */
bf08611b
JS
6294 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6295 if (phba->cfg_enable_bg) {
6296 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6297 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6298 else
6299 ftr_rsp++;
6300 }
da0436e9
JS
6301
6302 if (phba->max_vpi && phba->cfg_enable_npiv &&
6303 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6304 ftr_rsp++;
6305
6306 if (ftr_rsp) {
6307 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6308 "0379 Feature Mismatch Data: x%08x %08x "
6309 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6310 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6311 phba->cfg_enable_npiv, phba->max_vpi);
6312 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6313 phba->cfg_enable_bg = 0;
6314 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6315 phba->cfg_enable_npiv = 0;
6316 }
6317
6318 /* These SLI3 features are assumed in SLI4 */
6319 spin_lock_irq(&phba->hbalock);
6320 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6321 spin_unlock_irq(&phba->hbalock);
6322
6d368e53
JS
6323 /*
6324 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6325 * calls depends on these resources to complete port setup.
6326 */
6327 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6328 if (rc) {
6329 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6330 "2920 Failed to alloc Resource IDs "
6331 "rc = x%x\n", rc);
6332 goto out_free_mbox;
6333 }
6334
da0436e9 6335 /* Read the port's service parameters. */
9f1177a3
JS
6336 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6337 if (rc) {
6338 phba->link_state = LPFC_HBA_ERROR;
6339 rc = -ENOMEM;
76a95d75 6340 goto out_free_mbox;
9f1177a3
JS
6341 }
6342
da0436e9
JS
6343 mboxq->vport = vport;
6344 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6345 mp = (struct lpfc_dmabuf *) mboxq->context1;
6346 if (rc == MBX_SUCCESS) {
6347 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6348 rc = 0;
6349 }
6350
6351 /*
6352 * This memory was allocated by the lpfc_read_sparam routine. Release
6353 * it to the mbuf pool.
6354 */
6355 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6356 kfree(mp);
6357 mboxq->context1 = NULL;
6358 if (unlikely(rc)) {
6359 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6360 "0382 READ_SPARAM command failed "
6361 "status %d, mbxStatus x%x\n",
6362 rc, bf_get(lpfc_mqe_status, mqe));
6363 phba->link_state = LPFC_HBA_ERROR;
6364 rc = -EIO;
76a95d75 6365 goto out_free_mbox;
da0436e9
JS
6366 }
6367
0558056c 6368 lpfc_update_vport_wwn(vport);
da0436e9
JS
6369
6370 /* Update the fc_host data structures with new wwn. */
6371 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6372 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6373
8a9d2e80
JS
6374 /* update host els and scsi xri-sgl sizes and mappings */
6375 rc = lpfc_sli4_xri_sgl_update(phba);
6376 if (unlikely(rc)) {
6377 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6378 "1400 Failed to update xri-sgl size and "
6379 "mapping: %d\n", rc);
6380 goto out_free_mbox;
da0436e9
JS
6381 }
6382
8a9d2e80
JS
6383 /* register the els sgl pool to the port */
6384 rc = lpfc_sli4_repost_els_sgl_list(phba);
6385 if (unlikely(rc)) {
6386 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6387 "0582 Error %d during els sgl post "
6388 "operation\n", rc);
6389 rc = -ENODEV;
6390 goto out_free_mbox;
6391 }
6392
6393 /* register the allocated scsi sgl pool to the port */
da0436e9
JS
6394 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6395 if (unlikely(rc)) {
6d368e53 6396 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
6397 "0383 Error %d during scsi sgl post "
6398 "operation\n", rc);
da0436e9
JS
6399 /* Some Scsi buffers were moved to the abort scsi list */
6400 /* A pci function reset will repost them */
6401 rc = -ENODEV;
76a95d75 6402 goto out_free_mbox;
da0436e9
JS
6403 }
6404
6405 /* Post the rpi header region to the device. */
6406 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6407 if (unlikely(rc)) {
6408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6409 "0393 Error %d during rpi post operation\n",
6410 rc);
6411 rc = -ENODEV;
76a95d75 6412 goto out_free_mbox;
da0436e9 6413 }
97f2ecf1 6414 lpfc_sli4_node_prep(phba);
da0436e9 6415
5350d872
JS
6416 /* Create all the SLI4 queues */
6417 rc = lpfc_sli4_queue_create(phba);
6418 if (rc) {
6419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6420 "3089 Failed to allocate queues\n");
6421 rc = -ENODEV;
6422 goto out_stop_timers;
6423 }
da0436e9
JS
6424 /* Set up all the queues to the device */
6425 rc = lpfc_sli4_queue_setup(phba);
6426 if (unlikely(rc)) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6428 "0381 Error %d during queue setup.\n ", rc);
5350d872 6429 goto out_destroy_queue;
da0436e9
JS
6430 }
6431
6432 /* Arm the CQs and then EQs on device */
6433 lpfc_sli4_arm_cqeq_intr(phba);
6434
6435 /* Indicate device interrupt mode */
6436 phba->sli4_hba.intr_enable = 1;
6437
6438 /* Allow asynchronous mailbox command to go through */
6439 spin_lock_irq(&phba->hbalock);
6440 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6441 spin_unlock_irq(&phba->hbalock);
6442
6443 /* Post receive buffers to the device */
6444 lpfc_sli4_rb_setup(phba);
6445
fc2b989b
JS
6446 /* Reset HBA FCF states after HBA reset */
6447 phba->fcf.fcf_flag = 0;
6448 phba->fcf.current_rec.flag = 0;
6449
da0436e9 6450 /* Start the ELS watchdog timer */
8fa38513 6451 mod_timer(&vport->els_tmofunc,
256ec0d0 6452 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
6453
6454 /* Start heart beat timer */
6455 mod_timer(&phba->hb_tmofunc,
256ec0d0 6456 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
6457 phba->hb_outstanding = 0;
6458 phba->last_completion_time = jiffies;
6459
6460 /* Start error attention (ERATT) polling timer */
256ec0d0
JS
6461 mod_timer(&phba->eratt_poll,
6462 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
da0436e9 6463
75baf696
JS
6464 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6465 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6466 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6467 if (!rc) {
6468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6469 "2829 This device supports "
6470 "Advanced Error Reporting (AER)\n");
6471 spin_lock_irq(&phba->hbalock);
6472 phba->hba_flag |= HBA_AER_ENABLED;
6473 spin_unlock_irq(&phba->hbalock);
6474 } else {
6475 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6476 "2830 This device does not support "
6477 "Advanced Error Reporting (AER)\n");
6478 phba->cfg_aer_support = 0;
6479 }
0a96e975 6480 rc = 0;
75baf696
JS
6481 }
6482
76a95d75
JS
6483 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6484 /*
6485 * The FC Port needs to register FCFI (index 0)
6486 */
6487 lpfc_reg_fcfi(phba, mboxq);
6488 mboxq->vport = phba->pport;
6489 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9589b062 6490 if (rc != MBX_SUCCESS)
76a95d75 6491 goto out_unset_queue;
9589b062
JS
6492 rc = 0;
6493 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6494 &mboxq->u.mqe.un.reg_fcfi);
026abb87
JS
6495
6496 /* Check if the port is configured to be disabled */
6497 lpfc_sli_read_link_ste(phba);
76a95d75 6498 }
026abb87 6499
da0436e9
JS
6500 /*
6501 * The port is ready, set the host's link state to LINK_DOWN
6502 * in preparation for link interrupts.
6503 */
da0436e9
JS
6504 spin_lock_irq(&phba->hbalock);
6505 phba->link_state = LPFC_LINK_DOWN;
6506 spin_unlock_irq(&phba->hbalock);
026abb87
JS
6507 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6508 (phba->hba_flag & LINK_DISABLED)) {
6509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6510 "3103 Adapter Link is disabled.\n");
6511 lpfc_down_link(phba, mboxq);
6512 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6513 if (rc != MBX_SUCCESS) {
6514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6515 "3104 Adapter failed to issue "
6516 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6517 goto out_unset_queue;
6518 }
6519 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
6520 /* don't perform init_link on SLI4 FC port loopback test */
6521 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6522 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6523 if (rc)
6524 goto out_unset_queue;
6525 }
5350d872
JS
6526 }
6527 mempool_free(mboxq, phba->mbox_mem_pool);
6528 return rc;
76a95d75 6529out_unset_queue:
da0436e9 6530 /* Unset all the queues set up in this routine when error out */
5350d872
JS
6531 lpfc_sli4_queue_unset(phba);
6532out_destroy_queue:
6533 lpfc_sli4_queue_destroy(phba);
da0436e9 6534out_stop_timers:
5350d872 6535 lpfc_stop_hba_timers(phba);
da0436e9
JS
6536out_free_mbox:
6537 mempool_free(mboxq, phba->mbox_mem_pool);
6538 return rc;
6539}
6540
6541/**
6542 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6543 * @ptr: context object - pointer to hba structure.
6544 *
6545 * This is the callback function for mailbox timer. The mailbox
6546 * timer is armed when a new mailbox command is issued and the timer
6547 * is deleted when the mailbox complete. The function is called by
6548 * the kernel timer code when a mailbox does not complete within
6549 * expected time. This function wakes up the worker thread to
6550 * process the mailbox timeout and returns. All the processing is
6551 * done by the worker thread function lpfc_mbox_timeout_handler.
6552 **/
6553void
6554lpfc_mbox_timeout(unsigned long ptr)
6555{
6556 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6557 unsigned long iflag;
6558 uint32_t tmo_posted;
6559
6560 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6561 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6562 if (!tmo_posted)
6563 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6564 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6565
6566 if (!tmo_posted)
6567 lpfc_worker_wake_up(phba);
6568 return;
6569}
6570
e8d3c3b1
JS
6571/**
6572 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6573 * are pending
6574 * @phba: Pointer to HBA context object.
6575 *
6576 * This function checks if any mailbox completions are present on the mailbox
6577 * completion queue.
6578 **/
6579bool
6580lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6581{
6582
6583 uint32_t idx;
6584 struct lpfc_queue *mcq;
6585 struct lpfc_mcqe *mcqe;
6586 bool pending_completions = false;
6587
6588 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6589 return false;
6590
6591 /* Check for completions on mailbox completion queue */
6592
6593 mcq = phba->sli4_hba.mbx_cq;
6594 idx = mcq->hba_index;
6595 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6596 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6597 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6598 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6599 pending_completions = true;
6600 break;
6601 }
6602 idx = (idx + 1) % mcq->entry_count;
6603 if (mcq->hba_index == idx)
6604 break;
6605 }
6606 return pending_completions;
6607
6608}
6609
6610/**
6611 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6612 * that were missed.
6613 * @phba: Pointer to HBA context object.
6614 *
6615 * For sli4, it is possible to miss an interrupt. As such mbox completions
6616 * maybe missed causing erroneous mailbox timeouts to occur. This function
6617 * checks to see if mbox completions are on the mailbox completion queue
6618 * and will process all the completions associated with the eq for the
6619 * mailbox completion queue.
6620 **/
6621bool
6622lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6623{
6624
6625 uint32_t eqidx;
6626 struct lpfc_queue *fpeq = NULL;
6627 struct lpfc_eqe *eqe;
6628 bool mbox_pending;
6629
6630 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6631 return false;
6632
6633 /* Find the eq associated with the mcq */
6634
6635 if (phba->sli4_hba.hba_eq)
6636 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6637 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6638 phba->sli4_hba.mbx_cq->assoc_qid) {
6639 fpeq = phba->sli4_hba.hba_eq[eqidx];
6640 break;
6641 }
6642 if (!fpeq)
6643 return false;
6644
6645 /* Turn off interrupts from this EQ */
6646
6647 lpfc_sli4_eq_clr_intr(fpeq);
6648
6649 /* Check to see if a mbox completion is pending */
6650
6651 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6652
6653 /*
6654 * If a mbox completion is pending, process all the events on EQ
6655 * associated with the mbox completion queue (this could include
6656 * mailbox commands, async events, els commands, receive queue data
6657 * and fcp commands)
6658 */
6659
6660 if (mbox_pending)
6661 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6662 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6663 fpeq->EQ_processed++;
6664 }
6665
6666 /* Always clear and re-arm the EQ */
6667
6668 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6669
6670 return mbox_pending;
6671
6672}
da0436e9
JS
6673
6674/**
6675 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6676 * @phba: Pointer to HBA context object.
6677 *
6678 * This function is called from worker thread when a mailbox command times out.
6679 * The caller is not required to hold any locks. This function will reset the
6680 * HBA and recover all the pending commands.
6681 **/
6682void
6683lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6684{
6685 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 6686 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
6687 struct lpfc_sli *psli = &phba->sli;
6688 struct lpfc_sli_ring *pring;
6689
e8d3c3b1
JS
6690 /* If the mailbox completed, process the completion and return */
6691 if (lpfc_sli4_process_missed_mbox_completions(phba))
6692 return;
6693
da0436e9
JS
6694 /* Check the pmbox pointer first. There is a race condition
6695 * between the mbox timeout handler getting executed in the
6696 * worklist and the mailbox actually completing. When this
6697 * race condition occurs, the mbox_active will be NULL.
6698 */
6699 spin_lock_irq(&phba->hbalock);
6700 if (pmbox == NULL) {
6701 lpfc_printf_log(phba, KERN_WARNING,
6702 LOG_MBOX | LOG_SLI,
6703 "0353 Active Mailbox cleared - mailbox timeout "
6704 "exiting\n");
6705 spin_unlock_irq(&phba->hbalock);
6706 return;
6707 }
6708
6709 /* Mbox cmd <mbxCommand> timeout */
6710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6711 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6712 mb->mbxCommand,
6713 phba->pport->port_state,
6714 phba->sli.sli_flag,
6715 phba->sli.mbox_active);
6716 spin_unlock_irq(&phba->hbalock);
6717
6718 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6719 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 6720 * it to fail all outstanding SCSI IO.
da0436e9
JS
6721 */
6722 spin_lock_irq(&phba->pport->work_port_lock);
6723 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6724 spin_unlock_irq(&phba->pport->work_port_lock);
6725 spin_lock_irq(&phba->hbalock);
6726 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 6727 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
6728 spin_unlock_irq(&phba->hbalock);
6729
6730 pring = &psli->ring[psli->fcp_ring];
6731 lpfc_sli_abort_iocb_ring(phba, pring);
6732
6733 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6734 "0345 Resetting board due to mailbox timeout\n");
6735
6736 /* Reset the HBA device */
6737 lpfc_reset_hba(phba);
6738}
6739
6740/**
6741 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6742 * @phba: Pointer to HBA context object.
6743 * @pmbox: Pointer to mailbox object.
6744 * @flag: Flag indicating how the mailbox need to be processed.
6745 *
6746 * This function is called by discovery code and HBA management code
6747 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6748 * function gets the hbalock to protect the data structures.
6749 * The mailbox command can be submitted in polling mode, in which case
6750 * this function will wait in a polling loop for the completion of the
6751 * mailbox.
6752 * If the mailbox is submitted in no_wait mode (not polling) the
6753 * function will submit the command and returns immediately without waiting
6754 * for the mailbox completion. The no_wait is supported only when HBA
6755 * is in SLI2/SLI3 mode - interrupts are enabled.
6756 * The SLI interface allows only one mailbox pending at a time. If the
6757 * mailbox is issued in polling mode and there is already a mailbox
6758 * pending, then the function will return an error. If the mailbox is issued
6759 * in NO_WAIT mode and there is a mailbox pending already, the function
6760 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6761 * The sli layer owns the mailbox object until the completion of mailbox
6762 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6763 * return codes the caller owns the mailbox command after the return of
6764 * the function.
e59058c4 6765 **/
3772a991
JS
6766static int
6767lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6768 uint32_t flag)
dea3101e 6769{
bf07bdea 6770 MAILBOX_t *mbx;
2e0fef85 6771 struct lpfc_sli *psli = &phba->sli;
dea3101e 6772 uint32_t status, evtctr;
9940b97b 6773 uint32_t ha_copy, hc_copy;
dea3101e 6774 int i;
09372820 6775 unsigned long timeout;
dea3101e 6776 unsigned long drvr_flag = 0;
34b02dcd 6777 uint32_t word0, ldata;
dea3101e 6778 void __iomem *to_slim;
58da1ffb
JS
6779 int processing_queue = 0;
6780
6781 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6782 if (!pmbox) {
8568a4d2 6783 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 6784 /* processing mbox queue from intr_handler */
3772a991
JS
6785 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6786 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6787 return MBX_SUCCESS;
6788 }
58da1ffb 6789 processing_queue = 1;
58da1ffb
JS
6790 pmbox = lpfc_mbox_get(phba);
6791 if (!pmbox) {
6792 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6793 return MBX_SUCCESS;
6794 }
6795 }
dea3101e 6796
ed957684 6797 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 6798 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 6799 if(!pmbox->vport) {
58da1ffb 6800 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 6801 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 6802 LOG_MBOX | LOG_VPORT,
e8b62011 6803 "1806 Mbox x%x failed. No vport\n",
3772a991 6804 pmbox->u.mb.mbxCommand);
ed957684 6805 dump_stack();
58da1ffb 6806 goto out_not_finished;
ed957684
JS
6807 }
6808 }
6809
8d63f375 6810 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
6811 if (unlikely(pci_channel_offline(phba->pcidev))) {
6812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6813 goto out_not_finished;
6814 }
8d63f375 6815
a257bf90
JS
6816 /* If HBA has a deferred error attention, fail the iocb. */
6817 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6818 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6819 goto out_not_finished;
6820 }
6821
dea3101e 6822 psli = &phba->sli;
92d7f7b0 6823
bf07bdea 6824 mbx = &pmbox->u.mb;
dea3101e
JB
6825 status = MBX_SUCCESS;
6826
2e0fef85
JS
6827 if (phba->link_state == LPFC_HBA_ERROR) {
6828 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
6829
6830 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6831 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6832 "(%d):0311 Mailbox command x%x cannot "
6833 "issue Data: x%x x%x\n",
6834 pmbox->vport ? pmbox->vport->vpi : 0,
6835 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 6836 goto out_not_finished;
41415862
JW
6837 }
6838
bf07bdea 6839 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
6840 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6841 !(hc_copy & HC_MBINT_ENA)) {
6842 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6843 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
6844 "(%d):2528 Mailbox command x%x cannot "
6845 "issue Data: x%x x%x\n",
6846 pmbox->vport ? pmbox->vport->vpi : 0,
6847 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
6848 goto out_not_finished;
6849 }
9290831f
JS
6850 }
6851
dea3101e
JB
6852 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6853 /* Polling for a mbox command when another one is already active
6854 * is not allowed in SLI. Also, the driver must have established
6855 * SLI2 mode to queue and process multiple mbox commands.
6856 */
6857
6858 if (flag & MBX_POLL) {
2e0fef85 6859 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
6860
6861 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6863 "(%d):2529 Mailbox command x%x "
6864 "cannot issue Data: x%x x%x\n",
6865 pmbox->vport ? pmbox->vport->vpi : 0,
6866 pmbox->u.mb.mbxCommand,
6867 psli->sli_flag, flag);
58da1ffb 6868 goto out_not_finished;
dea3101e
JB
6869 }
6870
3772a991 6871 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 6872 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6873 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6874 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875 "(%d):2530 Mailbox command x%x "
6876 "cannot issue Data: x%x x%x\n",
6877 pmbox->vport ? pmbox->vport->vpi : 0,
6878 pmbox->u.mb.mbxCommand,
6879 psli->sli_flag, flag);
58da1ffb 6880 goto out_not_finished;
dea3101e
JB
6881 }
6882
dea3101e
JB
6883 /* Another mailbox command is still being processed, queue this
6884 * command to be processed later.
6885 */
6886 lpfc_mbox_put(phba, pmbox);
6887
6888 /* Mbox cmd issue - BUSY */
ed957684 6889 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6890 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 6891 "x%x x%x x%x x%x\n",
92d7f7b0 6892 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
bf07bdea 6893 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 6894 psli->sli_flag, flag);
dea3101e
JB
6895
6896 psli->slistat.mbox_busy++;
2e0fef85 6897 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6898
858c9f6c
JS
6899 if (pmbox->vport) {
6900 lpfc_debugfs_disc_trc(pmbox->vport,
6901 LPFC_DISC_TRC_MBOX_VPORT,
6902 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6903 (uint32_t)mbx->mbxCommand,
6904 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6905 }
6906 else {
6907 lpfc_debugfs_disc_trc(phba->pport,
6908 LPFC_DISC_TRC_MBOX,
6909 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6910 (uint32_t)mbx->mbxCommand,
6911 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6912 }
6913
2e0fef85 6914 return MBX_BUSY;
dea3101e
JB
6915 }
6916
dea3101e
JB
6917 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6918
6919 /* If we are not polling, we MUST be in SLI2 mode */
6920 if (flag != MBX_POLL) {
3772a991 6921 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 6922 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 6923 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6924 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6925 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6926 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6927 "(%d):2531 Mailbox command x%x "
6928 "cannot issue Data: x%x x%x\n",
6929 pmbox->vport ? pmbox->vport->vpi : 0,
6930 pmbox->u.mb.mbxCommand,
6931 psli->sli_flag, flag);
58da1ffb 6932 goto out_not_finished;
dea3101e
JB
6933 }
6934 /* timeout active mbox command */
256ec0d0
JS
6935 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6936 1000);
6937 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
6938 }
6939
6940 /* Mailbox cmd <cmd> issue */
ed957684 6941 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6942 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 6943 "x%x\n",
e8b62011 6944 pmbox->vport ? pmbox->vport->vpi : 0,
bf07bdea 6945 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 6946 psli->sli_flag, flag);
dea3101e 6947
bf07bdea 6948 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
6949 if (pmbox->vport) {
6950 lpfc_debugfs_disc_trc(pmbox->vport,
6951 LPFC_DISC_TRC_MBOX_VPORT,
6952 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6953 (uint32_t)mbx->mbxCommand,
6954 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6955 }
6956 else {
6957 lpfc_debugfs_disc_trc(phba->pport,
6958 LPFC_DISC_TRC_MBOX,
6959 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
6960 (uint32_t)mbx->mbxCommand,
6961 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
6962 }
6963 }
6964
dea3101e
JB
6965 psli->slistat.mbox_cmd++;
6966 evtctr = psli->slistat.mbox_event;
6967
6968 /* next set own bit for the adapter and copy over command word */
bf07bdea 6969 mbx->mbxOwner = OWN_CHIP;
dea3101e 6970
3772a991 6971 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
6972 /* Populate mbox extension offset word. */
6973 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 6974 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
6975 = (uint8_t *)phba->mbox_ext
6976 - (uint8_t *)phba->mbox;
6977 }
6978
6979 /* Copy the mailbox extension data */
6980 if (pmbox->in_ext_byte_len && pmbox->context2) {
6981 lpfc_sli_pcimem_bcopy(pmbox->context2,
6982 (uint8_t *)phba->mbox_ext,
6983 pmbox->in_ext_byte_len);
6984 }
6985 /* Copy command data to host SLIM area */
bf07bdea 6986 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6987 } else {
7a470277
JS
6988 /* Populate mbox extension offset word. */
6989 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 6990 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
6991 = MAILBOX_HBA_EXT_OFFSET;
6992
6993 /* Copy the mailbox extension data */
6994 if (pmbox->in_ext_byte_len && pmbox->context2) {
6995 lpfc_memcpy_to_slim(phba->MBslimaddr +
6996 MAILBOX_HBA_EXT_OFFSET,
6997 pmbox->context2, pmbox->in_ext_byte_len);
6998
6999 }
bf07bdea 7000 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7001 /* copy command data into host mbox for cmpl */
bf07bdea 7002 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e
JB
7003 }
7004
7005 /* First copy mbox command data to HBA SLIM, skip past first
7006 word */
7007 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7008 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7009 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7010
7011 /* Next copy over first word, with mbxOwner set */
bf07bdea 7012 ldata = *((uint32_t *)mbx);
dea3101e
JB
7013 to_slim = phba->MBslimaddr;
7014 writel(ldata, to_slim);
7015 readl(to_slim); /* flush */
7016
bf07bdea 7017 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7018 /* switch over to host mailbox */
3772a991 7019 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7020 }
7021 }
7022
7023 wmb();
dea3101e
JB
7024
7025 switch (flag) {
7026 case MBX_NOWAIT:
09372820 7027 /* Set up reference to mailbox command */
dea3101e 7028 psli->mbox_active = pmbox;
09372820
JS
7029 /* Interrupt board to do it */
7030 writel(CA_MBATT, phba->CAregaddr);
7031 readl(phba->CAregaddr); /* flush */
7032 /* Don't wait for it to finish, just return */
dea3101e
JB
7033 break;
7034
7035 case MBX_POLL:
09372820 7036 /* Set up null reference to mailbox command */
dea3101e 7037 psli->mbox_active = NULL;
09372820
JS
7038 /* Interrupt board to do it */
7039 writel(CA_MBATT, phba->CAregaddr);
7040 readl(phba->CAregaddr); /* flush */
7041
3772a991 7042 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7043 /* First read mbox status word */
34b02dcd 7044 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7045 word0 = le32_to_cpu(word0);
7046 } else {
7047 /* First read mbox status word */
9940b97b
JS
7048 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7049 spin_unlock_irqrestore(&phba->hbalock,
7050 drvr_flag);
7051 goto out_not_finished;
7052 }
dea3101e
JB
7053 }
7054
7055 /* Read the HBA Host Attention Register */
9940b97b
JS
7056 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7057 spin_unlock_irqrestore(&phba->hbalock,
7058 drvr_flag);
7059 goto out_not_finished;
7060 }
a183a15f
JS
7061 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7062 1000) + jiffies;
09372820 7063 i = 0;
dea3101e 7064 /* Wait for command to complete */
41415862
JW
7065 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7066 (!(ha_copy & HA_MBATT) &&
2e0fef85 7067 (phba->link_state > LPFC_WARM_START))) {
09372820 7068 if (time_after(jiffies, timeout)) {
dea3101e 7069 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7070 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7071 drvr_flag);
58da1ffb 7072 goto out_not_finished;
dea3101e
JB
7073 }
7074
7075 /* Check if we took a mbox interrupt while we were
7076 polling */
7077 if (((word0 & OWN_CHIP) != OWN_CHIP)
7078 && (evtctr != psli->slistat.mbox_event))
7079 break;
7080
09372820
JS
7081 if (i++ > 10) {
7082 spin_unlock_irqrestore(&phba->hbalock,
7083 drvr_flag);
7084 msleep(1);
7085 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7086 }
dea3101e 7087
3772a991 7088 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7089 /* First copy command data */
34b02dcd 7090 word0 = *((uint32_t *)phba->mbox);
dea3101e 7091 word0 = le32_to_cpu(word0);
bf07bdea 7092 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7093 MAILBOX_t *slimmb;
34b02dcd 7094 uint32_t slimword0;
dea3101e
JB
7095 /* Check real SLIM for any errors */
7096 slimword0 = readl(phba->MBslimaddr);
7097 slimmb = (MAILBOX_t *) & slimword0;
7098 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7099 && slimmb->mbxStatus) {
7100 psli->sli_flag &=
3772a991 7101 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7102 word0 = slimword0;
7103 }
7104 }
7105 } else {
7106 /* First copy command data */
7107 word0 = readl(phba->MBslimaddr);
7108 }
7109 /* Read the HBA Host Attention Register */
9940b97b
JS
7110 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7111 spin_unlock_irqrestore(&phba->hbalock,
7112 drvr_flag);
7113 goto out_not_finished;
7114 }
dea3101e
JB
7115 }
7116
3772a991 7117 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7118 /* copy results back to user */
bf07bdea 7119 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7a470277
JS
7120 /* Copy the mailbox extension data */
7121 if (pmbox->out_ext_byte_len && pmbox->context2) {
7122 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7123 pmbox->context2,
7124 pmbox->out_ext_byte_len);
7125 }
dea3101e
JB
7126 } else {
7127 /* First copy command data */
bf07bdea 7128 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea3101e 7129 MAILBOX_CMD_SIZE);
7a470277
JS
7130 /* Copy the mailbox extension data */
7131 if (pmbox->out_ext_byte_len && pmbox->context2) {
7132 lpfc_memcpy_from_slim(pmbox->context2,
7133 phba->MBslimaddr +
7134 MAILBOX_HBA_EXT_OFFSET,
7135 pmbox->out_ext_byte_len);
dea3101e
JB
7136 }
7137 }
7138
7139 writel(HA_MBATT, phba->HAregaddr);
7140 readl(phba->HAregaddr); /* flush */
7141
7142 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7143 status = mbx->mbxStatus;
dea3101e
JB
7144 }
7145
2e0fef85
JS
7146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7147 return status;
58da1ffb
JS
7148
7149out_not_finished:
7150 if (processing_queue) {
da0436e9 7151 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7152 lpfc_mbox_cmpl_put(phba, pmbox);
7153 }
7154 return MBX_NOT_FINISHED;
dea3101e
JB
7155}
7156
f1126688
JS
7157/**
7158 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7159 * @phba: Pointer to HBA context object.
7160 *
7161 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7162 * the driver internal pending mailbox queue. It will then try to wait out the
7163 * possible outstanding mailbox command before return.
7164 *
7165 * Returns:
7166 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7167 * the outstanding mailbox command timed out.
7168 **/
7169static int
7170lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7171{
7172 struct lpfc_sli *psli = &phba->sli;
f1126688 7173 int rc = 0;
a183a15f 7174 unsigned long timeout = 0;
f1126688
JS
7175
7176 /* Mark the asynchronous mailbox command posting as blocked */
7177 spin_lock_irq(&phba->hbalock);
7178 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7179 /* Determine how long we might wait for the active mailbox
7180 * command to be gracefully completed by firmware.
7181 */
a183a15f
JS
7182 if (phba->sli.mbox_active)
7183 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7184 phba->sli.mbox_active) *
7185 1000) + jiffies;
7186 spin_unlock_irq(&phba->hbalock);
7187
e8d3c3b1
JS
7188 /* Make sure the mailbox is really active */
7189 if (timeout)
7190 lpfc_sli4_process_missed_mbox_completions(phba);
7191
f1126688
JS
7192 /* Wait for the outstnading mailbox command to complete */
7193 while (phba->sli.mbox_active) {
7194 /* Check active mailbox complete status every 2ms */
7195 msleep(2);
7196 if (time_after(jiffies, timeout)) {
7197 /* Timeout, marked the outstanding cmd not complete */
7198 rc = 1;
7199 break;
7200 }
7201 }
7202
7203 /* Can not cleanly block async mailbox command, fails it */
7204 if (rc) {
7205 spin_lock_irq(&phba->hbalock);
7206 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7207 spin_unlock_irq(&phba->hbalock);
7208 }
7209 return rc;
7210}
7211
7212/**
7213 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7214 * @phba: Pointer to HBA context object.
7215 *
7216 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7217 * commands from the driver internal pending mailbox queue. It makes sure
7218 * that there is no outstanding mailbox command before resuming posting
7219 * asynchronous mailbox commands. If, for any reason, there is outstanding
7220 * mailbox command, it will try to wait it out before resuming asynchronous
7221 * mailbox command posting.
7222 **/
7223static void
7224lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7225{
7226 struct lpfc_sli *psli = &phba->sli;
7227
7228 spin_lock_irq(&phba->hbalock);
7229 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7230 /* Asynchronous mailbox posting is not blocked, do nothing */
7231 spin_unlock_irq(&phba->hbalock);
7232 return;
7233 }
7234
7235 /* Outstanding synchronous mailbox command is guaranteed to be done,
7236 * successful or timeout, after timing-out the outstanding mailbox
7237 * command shall always be removed, so just unblock posting async
7238 * mailbox command and resume
7239 */
7240 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7241 spin_unlock_irq(&phba->hbalock);
7242
7243 /* wake up worker thread to post asynchronlous mailbox command */
7244 lpfc_worker_wake_up(phba);
7245}
7246
2d843edc
JS
7247/**
7248 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7249 * @phba: Pointer to HBA context object.
7250 * @mboxq: Pointer to mailbox object.
7251 *
7252 * The function waits for the bootstrap mailbox register ready bit from
7253 * port for twice the regular mailbox command timeout value.
7254 *
7255 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7256 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7257 **/
7258static int
7259lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7260{
7261 uint32_t db_ready;
7262 unsigned long timeout;
7263 struct lpfc_register bmbx_reg;
7264
7265 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7266 * 1000) + jiffies;
7267
7268 do {
7269 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7270 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7271 if (!db_ready)
7272 msleep(2);
7273
7274 if (time_after(jiffies, timeout))
7275 return MBXERR_ERROR;
7276 } while (!db_ready);
7277
7278 return 0;
7279}
7280
da0436e9
JS
7281/**
7282 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7283 * @phba: Pointer to HBA context object.
7284 * @mboxq: Pointer to mailbox object.
7285 *
7286 * The function posts a mailbox to the port. The mailbox is expected
7287 * to be comletely filled in and ready for the port to operate on it.
7288 * This routine executes a synchronous completion operation on the
7289 * mailbox by polling for its completion.
7290 *
7291 * The caller must not be holding any locks when calling this routine.
7292 *
7293 * Returns:
7294 * MBX_SUCCESS - mailbox posted successfully
7295 * Any of the MBX error values.
7296 **/
7297static int
7298lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7299{
7300 int rc = MBX_SUCCESS;
7301 unsigned long iflag;
da0436e9
JS
7302 uint32_t mcqe_status;
7303 uint32_t mbx_cmnd;
da0436e9
JS
7304 struct lpfc_sli *psli = &phba->sli;
7305 struct lpfc_mqe *mb = &mboxq->u.mqe;
7306 struct lpfc_bmbx_create *mbox_rgn;
7307 struct dma_address *dma_address;
da0436e9
JS
7308
7309 /*
7310 * Only one mailbox can be active to the bootstrap mailbox region
7311 * at a time and there is no queueing provided.
7312 */
7313 spin_lock_irqsave(&phba->hbalock, iflag);
7314 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7315 spin_unlock_irqrestore(&phba->hbalock, iflag);
7316 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7317 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7318 "cannot issue Data: x%x x%x\n",
7319 mboxq->vport ? mboxq->vport->vpi : 0,
7320 mboxq->u.mb.mbxCommand,
a183a15f
JS
7321 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7322 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7323 psli->sli_flag, MBX_POLL);
7324 return MBXERR_ERROR;
7325 }
7326 /* The server grabs the token and owns it until release */
7327 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7328 phba->sli.mbox_active = mboxq;
7329 spin_unlock_irqrestore(&phba->hbalock, iflag);
7330
2d843edc
JS
7331 /* wait for bootstrap mbox register for readyness */
7332 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7333 if (rc)
7334 goto exit;
7335
da0436e9
JS
7336 /*
7337 * Initialize the bootstrap memory region to avoid stale data areas
7338 * in the mailbox post. Then copy the caller's mailbox contents to
7339 * the bmbx mailbox region.
7340 */
7341 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7342 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7343 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7344 sizeof(struct lpfc_mqe));
7345
7346 /* Post the high mailbox dma address to the port and wait for ready. */
7347 dma_address = &phba->sli4_hba.bmbx.dma_address;
7348 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7349
2d843edc
JS
7350 /* wait for bootstrap mbox register for hi-address write done */
7351 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7352 if (rc)
7353 goto exit;
da0436e9
JS
7354
7355 /* Post the low mailbox dma address to the port. */
7356 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7357
2d843edc
JS
7358 /* wait for bootstrap mbox register for low address write done */
7359 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7360 if (rc)
7361 goto exit;
da0436e9
JS
7362
7363 /*
7364 * Read the CQ to ensure the mailbox has completed.
7365 * If so, update the mailbox status so that the upper layers
7366 * can complete the request normally.
7367 */
7368 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7369 sizeof(struct lpfc_mqe));
7370 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7371 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7372 sizeof(struct lpfc_mcqe));
7373 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
7374 /*
7375 * When the CQE status indicates a failure and the mailbox status
7376 * indicates success then copy the CQE status into the mailbox status
7377 * (and prefix it with x4000).
7378 */
da0436e9 7379 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
7380 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7381 bf_set(lpfc_mqe_status, mb,
7382 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 7383 rc = MBXERR_ERROR;
d7c47992
JS
7384 } else
7385 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
7386
7387 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7388 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
7389 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7390 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
7391 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7392 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7393 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7394 bf_get(lpfc_mqe_status, mb),
7395 mb->un.mb_words[0], mb->un.mb_words[1],
7396 mb->un.mb_words[2], mb->un.mb_words[3],
7397 mb->un.mb_words[4], mb->un.mb_words[5],
7398 mb->un.mb_words[6], mb->un.mb_words[7],
7399 mb->un.mb_words[8], mb->un.mb_words[9],
7400 mb->un.mb_words[10], mb->un.mb_words[11],
7401 mb->un.mb_words[12], mboxq->mcqe.word0,
7402 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7403 mboxq->mcqe.trailer);
7404exit:
7405 /* We are holding the token, no needed for lock when release */
7406 spin_lock_irqsave(&phba->hbalock, iflag);
7407 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7408 phba->sli.mbox_active = NULL;
7409 spin_unlock_irqrestore(&phba->hbalock, iflag);
7410 return rc;
7411}
7412
7413/**
7414 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7415 * @phba: Pointer to HBA context object.
7416 * @pmbox: Pointer to mailbox object.
7417 * @flag: Flag indicating how the mailbox need to be processed.
7418 *
7419 * This function is called by discovery code and HBA management code to submit
7420 * a mailbox command to firmware with SLI-4 interface spec.
7421 *
7422 * Return codes the caller owns the mailbox command after the return of the
7423 * function.
7424 **/
7425static int
7426lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7427 uint32_t flag)
7428{
7429 struct lpfc_sli *psli = &phba->sli;
7430 unsigned long iflags;
7431 int rc;
7432
b76f2dc9
JS
7433 /* dump from issue mailbox command if setup */
7434 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7435
8fa38513
JS
7436 rc = lpfc_mbox_dev_check(phba);
7437 if (unlikely(rc)) {
7438 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7439 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
7440 "cannot issue Data: x%x x%x\n",
7441 mboxq->vport ? mboxq->vport->vpi : 0,
7442 mboxq->u.mb.mbxCommand,
a183a15f
JS
7443 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7444 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
7445 psli->sli_flag, flag);
7446 goto out_not_finished;
7447 }
7448
da0436e9
JS
7449 /* Detect polling mode and jump to a handler */
7450 if (!phba->sli4_hba.intr_enable) {
7451 if (flag == MBX_POLL)
7452 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7453 else
7454 rc = -EIO;
7455 if (rc != MBX_SUCCESS)
0558056c 7456 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 7457 "(%d):2541 Mailbox command x%x "
cc459f19
JS
7458 "(x%x/x%x) failure: "
7459 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7460 "Data: x%x x%x\n,",
da0436e9
JS
7461 mboxq->vport ? mboxq->vport->vpi : 0,
7462 mboxq->u.mb.mbxCommand,
a183a15f
JS
7463 lpfc_sli_config_mbox_subsys_get(phba,
7464 mboxq),
7465 lpfc_sli_config_mbox_opcode_get(phba,
7466 mboxq),
cc459f19
JS
7467 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7468 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7469 bf_get(lpfc_mcqe_ext_status,
7470 &mboxq->mcqe),
da0436e9
JS
7471 psli->sli_flag, flag);
7472 return rc;
7473 } else if (flag == MBX_POLL) {
f1126688
JS
7474 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7475 "(%d):2542 Try to issue mailbox command "
a183a15f 7476 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 7477 "mailbox command queue: x%x x%x\n",
da0436e9
JS
7478 mboxq->vport ? mboxq->vport->vpi : 0,
7479 mboxq->u.mb.mbxCommand,
a183a15f
JS
7480 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7481 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 7482 psli->sli_flag, flag);
f1126688
JS
7483 /* Try to block the asynchronous mailbox posting */
7484 rc = lpfc_sli4_async_mbox_block(phba);
7485 if (!rc) {
7486 /* Successfully blocked, now issue sync mbox cmd */
7487 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7488 if (rc != MBX_SUCCESS)
cc459f19 7489 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 7490 LOG_MBOX | LOG_SLI,
cc459f19
JS
7491 "(%d):2597 Sync Mailbox command "
7492 "x%x (x%x/x%x) failure: "
7493 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7494 "Data: x%x x%x\n,",
7495 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
7496 mboxq->u.mb.mbxCommand,
7497 lpfc_sli_config_mbox_subsys_get(phba,
7498 mboxq),
7499 lpfc_sli_config_mbox_opcode_get(phba,
7500 mboxq),
cc459f19
JS
7501 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7502 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7503 bf_get(lpfc_mcqe_ext_status,
7504 &mboxq->mcqe),
a183a15f 7505 psli->sli_flag, flag);
f1126688
JS
7506 /* Unblock the async mailbox posting afterward */
7507 lpfc_sli4_async_mbox_unblock(phba);
7508 }
7509 return rc;
da0436e9
JS
7510 }
7511
7512 /* Now, interrupt mode asynchrous mailbox command */
7513 rc = lpfc_mbox_cmd_check(phba, mboxq);
7514 if (rc) {
7515 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7516 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7517 "cannot issue Data: x%x x%x\n",
7518 mboxq->vport ? mboxq->vport->vpi : 0,
7519 mboxq->u.mb.mbxCommand,
a183a15f
JS
7520 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7521 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7522 psli->sli_flag, flag);
7523 goto out_not_finished;
7524 }
da0436e9
JS
7525
7526 /* Put the mailbox command to the driver internal FIFO */
7527 psli->slistat.mbox_busy++;
7528 spin_lock_irqsave(&phba->hbalock, iflags);
7529 lpfc_mbox_put(phba, mboxq);
7530 spin_unlock_irqrestore(&phba->hbalock, iflags);
7531 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7532 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 7533 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
7534 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7535 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
7536 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7537 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7538 phba->pport->port_state,
7539 psli->sli_flag, MBX_NOWAIT);
7540 /* Wake up worker thread to transport mailbox command from head */
7541 lpfc_worker_wake_up(phba);
7542
7543 return MBX_BUSY;
7544
7545out_not_finished:
7546 return MBX_NOT_FINISHED;
7547}
7548
7549/**
7550 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7551 * @phba: Pointer to HBA context object.
7552 *
7553 * This function is called by worker thread to send a mailbox command to
7554 * SLI4 HBA firmware.
7555 *
7556 **/
7557int
7558lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7559{
7560 struct lpfc_sli *psli = &phba->sli;
7561 LPFC_MBOXQ_t *mboxq;
7562 int rc = MBX_SUCCESS;
7563 unsigned long iflags;
7564 struct lpfc_mqe *mqe;
7565 uint32_t mbx_cmnd;
7566
7567 /* Check interrupt mode before post async mailbox command */
7568 if (unlikely(!phba->sli4_hba.intr_enable))
7569 return MBX_NOT_FINISHED;
7570
7571 /* Check for mailbox command service token */
7572 spin_lock_irqsave(&phba->hbalock, iflags);
7573 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7574 spin_unlock_irqrestore(&phba->hbalock, iflags);
7575 return MBX_NOT_FINISHED;
7576 }
7577 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7578 spin_unlock_irqrestore(&phba->hbalock, iflags);
7579 return MBX_NOT_FINISHED;
7580 }
7581 if (unlikely(phba->sli.mbox_active)) {
7582 spin_unlock_irqrestore(&phba->hbalock, iflags);
7583 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7584 "0384 There is pending active mailbox cmd\n");
7585 return MBX_NOT_FINISHED;
7586 }
7587 /* Take the mailbox command service token */
7588 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7589
7590 /* Get the next mailbox command from head of queue */
7591 mboxq = lpfc_mbox_get(phba);
7592
7593 /* If no more mailbox command waiting for post, we're done */
7594 if (!mboxq) {
7595 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7596 spin_unlock_irqrestore(&phba->hbalock, iflags);
7597 return MBX_SUCCESS;
7598 }
7599 phba->sli.mbox_active = mboxq;
7600 spin_unlock_irqrestore(&phba->hbalock, iflags);
7601
7602 /* Check device readiness for posting mailbox command */
7603 rc = lpfc_mbox_dev_check(phba);
7604 if (unlikely(rc))
7605 /* Driver clean routine will clean up pending mailbox */
7606 goto out_not_finished;
7607
7608 /* Prepare the mbox command to be posted */
7609 mqe = &mboxq->u.mqe;
7610 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7611
7612 /* Start timer for the mbox_tmo and log some mailbox post messages */
7613 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 7614 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
7615
7616 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7617 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
7618 "x%x x%x\n",
7619 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
7620 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7621 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7622 phba->pport->port_state, psli->sli_flag);
7623
7624 if (mbx_cmnd != MBX_HEARTBEAT) {
7625 if (mboxq->vport) {
7626 lpfc_debugfs_disc_trc(mboxq->vport,
7627 LPFC_DISC_TRC_MBOX_VPORT,
7628 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7629 mbx_cmnd, mqe->un.mb_words[0],
7630 mqe->un.mb_words[1]);
7631 } else {
7632 lpfc_debugfs_disc_trc(phba->pport,
7633 LPFC_DISC_TRC_MBOX,
7634 "MBOX Send: cmd:x%x mb:x%x x%x",
7635 mbx_cmnd, mqe->un.mb_words[0],
7636 mqe->un.mb_words[1]);
7637 }
7638 }
7639 psli->slistat.mbox_cmd++;
7640
7641 /* Post the mailbox command to the port */
7642 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7643 if (rc != MBX_SUCCESS) {
7644 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7645 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7646 "cannot issue Data: x%x x%x\n",
7647 mboxq->vport ? mboxq->vport->vpi : 0,
7648 mboxq->u.mb.mbxCommand,
a183a15f
JS
7649 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7650 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7651 psli->sli_flag, MBX_NOWAIT);
7652 goto out_not_finished;
7653 }
7654
7655 return rc;
7656
7657out_not_finished:
7658 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
7659 if (phba->sli.mbox_active) {
7660 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7661 __lpfc_mbox_cmpl_put(phba, mboxq);
7662 /* Release the token */
7663 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7664 phba->sli.mbox_active = NULL;
7665 }
da0436e9
JS
7666 spin_unlock_irqrestore(&phba->hbalock, iflags);
7667
7668 return MBX_NOT_FINISHED;
7669}
7670
7671/**
7672 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7673 * @phba: Pointer to HBA context object.
7674 * @pmbox: Pointer to mailbox object.
7675 * @flag: Flag indicating how the mailbox need to be processed.
7676 *
7677 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7678 * the API jump table function pointer from the lpfc_hba struct.
7679 *
7680 * Return codes the caller owns the mailbox command after the return of the
7681 * function.
7682 **/
7683int
7684lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7685{
7686 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7687}
7688
7689/**
25985edc 7690 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
7691 * @phba: The hba struct for which this call is being executed.
7692 * @dev_grp: The HBA PCI-Device group number.
7693 *
7694 * This routine sets up the mbox interface API function jump table in @phba
7695 * struct.
7696 * Returns: 0 - success, -ENODEV - failure.
7697 **/
7698int
7699lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7700{
7701
7702 switch (dev_grp) {
7703 case LPFC_PCI_DEV_LP:
7704 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7705 phba->lpfc_sli_handle_slow_ring_event =
7706 lpfc_sli_handle_slow_ring_event_s3;
7707 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7708 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7709 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7710 break;
7711 case LPFC_PCI_DEV_OC:
7712 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7713 phba->lpfc_sli_handle_slow_ring_event =
7714 lpfc_sli_handle_slow_ring_event_s4;
7715 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7716 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7717 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7718 break;
7719 default:
7720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7721 "1420 Invalid HBA PCI-device group: 0x%x\n",
7722 dev_grp);
7723 return -ENODEV;
7724 break;
7725 }
7726 return 0;
7727}
7728
e59058c4 7729/**
3621a710 7730 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
7731 * @phba: Pointer to HBA context object.
7732 * @pring: Pointer to driver SLI ring object.
7733 * @piocb: Pointer to address of newly added command iocb.
7734 *
7735 * This function is called with hbalock held to add a command
7736 * iocb to the txq when SLI layer cannot submit the command iocb
7737 * to the ring.
7738 **/
2a9bf3d0 7739void
92d7f7b0 7740__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7741 struct lpfc_iocbq *piocb)
dea3101e
JB
7742{
7743 /* Insert the caller's iocb in the txq tail for later processing. */
7744 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
7745}
7746
e59058c4 7747/**
3621a710 7748 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
7749 * @phba: Pointer to HBA context object.
7750 * @pring: Pointer to driver SLI ring object.
7751 * @piocb: Pointer to address of newly added command iocb.
7752 *
7753 * This function is called with hbalock held before a new
7754 * iocb is submitted to the firmware. This function checks
7755 * txq to flush the iocbs in txq to Firmware before
7756 * submitting new iocbs to the Firmware.
7757 * If there are iocbs in the txq which need to be submitted
7758 * to firmware, lpfc_sli_next_iocb returns the first element
7759 * of the txq after dequeuing it from txq.
7760 * If there is no iocb in the txq then the function will return
7761 * *piocb and *piocb is set to NULL. Caller needs to check
7762 * *piocb to find if there are more commands in the txq.
7763 **/
dea3101e
JB
7764static struct lpfc_iocbq *
7765lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7766 struct lpfc_iocbq **piocb)
dea3101e
JB
7767{
7768 struct lpfc_iocbq * nextiocb;
7769
7770 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7771 if (!nextiocb) {
7772 nextiocb = *piocb;
7773 *piocb = NULL;
7774 }
7775
7776 return nextiocb;
7777}
7778
e59058c4 7779/**
3772a991 7780 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 7781 * @phba: Pointer to HBA context object.
3772a991 7782 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
7783 * @piocb: Pointer to command iocb.
7784 * @flag: Flag indicating if this command can be put into txq.
7785 *
3772a991
JS
7786 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7787 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7788 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7789 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7790 * this function allows only iocbs for posting buffers. This function finds
7791 * next available slot in the command ring and posts the command to the
7792 * available slot and writes the port attention register to request HBA start
7793 * processing new iocb. If there is no slot available in the ring and
7794 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7795 * the function returns IOCB_BUSY.
e59058c4 7796 *
3772a991
JS
7797 * This function is called with hbalock held. The function will return success
7798 * after it successfully submit the iocb to firmware or after adding to the
7799 * txq.
e59058c4 7800 **/
98c9ea5c 7801static int
3772a991 7802__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
7803 struct lpfc_iocbq *piocb, uint32_t flag)
7804{
7805 struct lpfc_iocbq *nextiocb;
7806 IOCB_t *iocb;
3772a991 7807 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 7808
92d7f7b0
JS
7809 if (piocb->iocb_cmpl && (!piocb->vport) &&
7810 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7811 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7812 lpfc_printf_log(phba, KERN_ERR,
7813 LOG_SLI | LOG_VPORT,
e8b62011 7814 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
7815 piocb->iocb.ulpCommand);
7816 dump_stack();
7817 return IOCB_ERROR;
7818 }
7819
7820
8d63f375
LV
7821 /* If the PCI channel is in offline state, do not post iocbs. */
7822 if (unlikely(pci_channel_offline(phba->pcidev)))
7823 return IOCB_ERROR;
7824
a257bf90
JS
7825 /* If HBA has a deferred error attention, fail the iocb. */
7826 if (unlikely(phba->hba_flag & DEFER_ERATT))
7827 return IOCB_ERROR;
7828
dea3101e
JB
7829 /*
7830 * We should never get an IOCB if we are in a < LINK_DOWN state
7831 */
2e0fef85 7832 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
7833 return IOCB_ERROR;
7834
7835 /*
7836 * Check to see if we are blocking IOCB processing because of a
0b727fea 7837 * outstanding event.
dea3101e 7838 */
0b727fea 7839 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
7840 goto iocb_busy;
7841
2e0fef85 7842 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 7843 /*
2680eeaa 7844 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
7845 * can be issued if the link is not up.
7846 */
7847 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
7848 case CMD_GEN_REQUEST64_CR:
7849 case CMD_GEN_REQUEST64_CX:
7850 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7851 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 7852 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
7853 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7854 MENLO_TRANSPORT_TYPE))
7855
7856 goto iocb_busy;
7857 break;
dea3101e
JB
7858 case CMD_QUE_RING_BUF_CN:
7859 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
7860 /*
7861 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7862 * completion, iocb_cmpl MUST be 0.
7863 */
7864 if (piocb->iocb_cmpl)
7865 piocb->iocb_cmpl = NULL;
7866 /*FALLTHROUGH*/
7867 case CMD_CREATE_XRI_CR:
2680eeaa
JS
7868 case CMD_CLOSE_XRI_CN:
7869 case CMD_CLOSE_XRI_CX:
dea3101e
JB
7870 break;
7871 default:
7872 goto iocb_busy;
7873 }
7874
7875 /*
7876 * For FCP commands, we must be in a state where we can process link
7877 * attention events.
7878 */
7879 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 7880 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 7881 goto iocb_busy;
92d7f7b0 7882 }
dea3101e 7883
dea3101e
JB
7884 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7885 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7886 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7887
7888 if (iocb)
7889 lpfc_sli_update_ring(phba, pring);
7890 else
7891 lpfc_sli_update_full_ring(phba, pring);
7892
7893 if (!piocb)
7894 return IOCB_SUCCESS;
7895
7896 goto out_busy;
7897
7898 iocb_busy:
7899 pring->stats.iocb_cmd_delay++;
7900
7901 out_busy:
7902
7903 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 7904 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
7905 return IOCB_SUCCESS;
7906 }
7907
7908 return IOCB_BUSY;
7909}
7910
3772a991 7911/**
4f774513
JS
7912 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7913 * @phba: Pointer to HBA context object.
7914 * @piocb: Pointer to command iocb.
7915 * @sglq: Pointer to the scatter gather queue object.
7916 *
7917 * This routine converts the bpl or bde that is in the IOCB
7918 * to a sgl list for the sli4 hardware. The physical address
7919 * of the bpl/bde is converted back to a virtual address.
7920 * If the IOCB contains a BPL then the list of BDE's is
7921 * converted to sli4_sge's. If the IOCB contains a single
7922 * BDE then it is converted to a single sli_sge.
7923 * The IOCB is still in cpu endianess so the contents of
7924 * the bpl can be used without byte swapping.
7925 *
7926 * Returns valid XRI = Success, NO_XRI = Failure.
7927**/
7928static uint16_t
7929lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7930 struct lpfc_sglq *sglq)
3772a991 7931{
4f774513
JS
7932 uint16_t xritag = NO_XRI;
7933 struct ulp_bde64 *bpl = NULL;
7934 struct ulp_bde64 bde;
7935 struct sli4_sge *sgl = NULL;
1b51197d 7936 struct lpfc_dmabuf *dmabuf;
4f774513
JS
7937 IOCB_t *icmd;
7938 int numBdes = 0;
7939 int i = 0;
63e801ce
JS
7940 uint32_t offset = 0; /* accumulated offset in the sg request list */
7941 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 7942
4f774513
JS
7943 if (!piocbq || !sglq)
7944 return xritag;
7945
7946 sgl = (struct sli4_sge *)sglq->sgl;
7947 icmd = &piocbq->iocb;
6b5151fd
JS
7948 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7949 return sglq->sli4_xritag;
4f774513
JS
7950 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7951 numBdes = icmd->un.genreq64.bdl.bdeSize /
7952 sizeof(struct ulp_bde64);
7953 /* The addrHigh and addrLow fields within the IOCB
7954 * have not been byteswapped yet so there is no
7955 * need to swap them back.
7956 */
1b51197d
JS
7957 if (piocbq->context3)
7958 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7959 else
7960 return xritag;
4f774513 7961
1b51197d 7962 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
7963 if (!bpl)
7964 return xritag;
7965
7966 for (i = 0; i < numBdes; i++) {
7967 /* Should already be byte swapped. */
28baac74
JS
7968 sgl->addr_hi = bpl->addrHigh;
7969 sgl->addr_lo = bpl->addrLow;
7970
0558056c 7971 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7972 if ((i+1) == numBdes)
7973 bf_set(lpfc_sli4_sge_last, sgl, 1);
7974 else
7975 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
7976 /* swap the size field back to the cpu so we
7977 * can assign it to the sgl.
7978 */
7979 bde.tus.w = le32_to_cpu(bpl->tus.w);
7980 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
7981 /* The offsets in the sgl need to be accumulated
7982 * separately for the request and reply lists.
7983 * The request is always first, the reply follows.
7984 */
7985 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7986 /* add up the reply sg entries */
7987 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7988 inbound++;
7989 /* first inbound? reset the offset */
7990 if (inbound == 1)
7991 offset = 0;
7992 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
7993 bf_set(lpfc_sli4_sge_type, sgl,
7994 LPFC_SGE_TYPE_DATA);
63e801ce
JS
7995 offset += bde.tus.f.bdeSize;
7996 }
546fc854 7997 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
7998 bpl++;
7999 sgl++;
8000 }
8001 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8002 /* The addrHigh and addrLow fields of the BDE have not
8003 * been byteswapped yet so they need to be swapped
8004 * before putting them in the sgl.
8005 */
8006 sgl->addr_hi =
8007 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8008 sgl->addr_lo =
8009 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8010 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8011 bf_set(lpfc_sli4_sge_last, sgl, 1);
8012 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8013 sgl->sge_len =
8014 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8015 }
8016 return sglq->sli4_xritag;
3772a991 8017}
92d7f7b0 8018
e59058c4 8019/**
4f774513 8020 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 8021 * @phba: Pointer to HBA context object.
e59058c4 8022 *
a93ff37a 8023 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
8fa38513
JS
8024 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
8025 * held.
4f774513
JS
8026 *
8027 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 8028 **/
2a76a283 8029static inline uint32_t
8fa38513 8030lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 8031{
7bb03bbf
JS
8032 struct lpfc_vector_map_info *cpup;
8033 int chann, cpu;
8034
8035 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU) {
8036 cpu = smp_processor_id();
8037 if (cpu < phba->sli4_hba.num_present_cpu) {
8038 cpup = phba->sli4_hba.cpu_map;
8039 cpup += cpu;
8040 return cpup->channel_id;
8041 }
8042 chann = cpu;
8043 }
8044 chann = atomic_add_return(1, &phba->fcp_qidx);
8045 chann = (chann % phba->cfg_fcp_io_channel);
8046 return chann;
92d7f7b0
JS
8047}
8048
e59058c4 8049/**
4f774513 8050 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8051 * @phba: Pointer to HBA context object.
4f774513
JS
8052 * @piocb: Pointer to command iocb.
8053 * @wqe: Pointer to the work queue entry.
e59058c4 8054 *
4f774513
JS
8055 * This routine converts the iocb command to its Work Queue Entry
8056 * equivalent. The wqe pointer should not have any fields set when
8057 * this routine is called because it will memcpy over them.
8058 * This routine does not set the CQ_ID or the WQEC bits in the
8059 * wqe.
e59058c4 8060 *
4f774513 8061 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8062 **/
cf5bf97e 8063static int
4f774513
JS
8064lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8065 union lpfc_wqe *wqe)
cf5bf97e 8066{
5ffc266e 8067 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8068 uint8_t ct = 0;
8069 uint32_t fip;
8070 uint32_t abort_tag;
8071 uint8_t command_type = ELS_COMMAND_NON_FIP;
8072 uint8_t cmnd;
8073 uint16_t xritag;
dcf2a4e0
JS
8074 uint16_t abrt_iotag;
8075 struct lpfc_iocbq *abrtiocbq;
4f774513 8076 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8077 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8078 int numBdes, i;
8079 struct ulp_bde64 bde;
c31098ce 8080 struct lpfc_nodelist *ndlp;
ff78d8f9 8081 uint32_t *pcmd;
1b51197d 8082 uint32_t if_type;
4f774513 8083
45ed1190 8084 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8085 /* The fcp commands will set command type */
0c287589 8086 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8087 command_type = FCP_COMMAND;
c868595d 8088 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8089 command_type = ELS_COMMAND_FIP;
8090 else
8091 command_type = ELS_COMMAND_NON_FIP;
8092
4f774513
JS
8093 /* Some of the fields are in the right position already */
8094 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8095 abort_tag = (uint32_t) iocbq->iotag;
8096 xritag = iocbq->sli4_xritag;
f0d9bccc 8097 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
4f774513
JS
8098 /* words0-2 bpl convert bde */
8099 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8100 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8101 sizeof(struct ulp_bde64);
4f774513
JS
8102 bpl = (struct ulp_bde64 *)
8103 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8104 if (!bpl)
8105 return IOCB_ERROR;
cf5bf97e 8106
4f774513
JS
8107 /* Should already be byte swapped. */
8108 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8109 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8110 /* swap the size field back to the cpu so we
8111 * can assign it to the sgl.
8112 */
8113 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8114 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8115 total_len = 0;
8116 for (i = 0; i < numBdes; i++) {
8117 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8118 total_len += bde.tus.f.bdeSize;
8119 }
4f774513 8120 } else
5ffc266e 8121 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8122
4f774513
JS
8123 iocbq->iocb.ulpIoTag = iocbq->iotag;
8124 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8125
4f774513
JS
8126 switch (iocbq->iocb.ulpCommand) {
8127 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8128 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8129 ndlp = iocbq->context_un.ndlp;
8130 else
8131 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8132 if (!iocbq->iocb.ulpLe) {
8133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8134 "2007 Only Limited Edition cmd Format"
8135 " supported 0x%x\n",
8136 iocbq->iocb.ulpCommand);
8137 return IOCB_ERROR;
8138 }
ff78d8f9 8139
5ffc266e 8140 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8141 /* Els_reguest64 has a TMO */
8142 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8143 iocbq->iocb.ulpTimeout);
8144 /* Need a VF for word 4 set the vf bit*/
8145 bf_set(els_req64_vf, &wqe->els_req, 0);
8146 /* And a VFID for word 12 */
8147 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8148 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8149 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8150 iocbq->iocb.ulpContext);
8151 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8152 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8153 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8154 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8155 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8156 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8157 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8158 iocbq->context2)->virt);
1b51197d
JS
8159 if_type = bf_get(lpfc_sli_intf_if_type,
8160 &phba->sli4_hba.sli_intf);
8161 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8162 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8163 *pcmd == ELS_CMD_SCR ||
6b5151fd 8164 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8165 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8166 *pcmd == ELS_CMD_PLOGI)) {
8167 bf_set(els_req64_sp, &wqe->els_req, 1);
8168 bf_set(els_req64_sid, &wqe->els_req,
8169 iocbq->vport->fc_myDID);
939723a4
JS
8170 if ((*pcmd == ELS_CMD_FLOGI) &&
8171 !(phba->fc_topology ==
8172 LPFC_TOPOLOGY_LOOP))
8173 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8174 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8175 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8176 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8177 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8178 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8179 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8180 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8181 }
c868595d 8182 }
6d368e53
JS
8183 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8184 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8185 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8186 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8187 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8188 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8189 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8190 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8191 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8192 break;
5ffc266e 8193 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8194 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8195 iocbq->iocb.un.ulpWord[3]);
8196 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8197 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8198 /* The entire sequence is transmitted for this IOCB */
8199 xmit_len = total_len;
8200 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8201 if (phba->link_flag & LS_LOOPBACK_MODE)
8202 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8203 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8204 /* word3 iocb=io_tag32 wqe=reserved */
8205 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8206 /* word4 relative_offset memcpy */
8207 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8208 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8209 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8210 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8211 LPFC_WQE_IOD_WRITE);
8212 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8213 LPFC_WQE_LENLOC_WORD12);
8214 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8215 wqe->xmit_sequence.xmit_len = xmit_len;
8216 command_type = OTHER_COMMAND;
7851fe2c 8217 break;
4f774513 8218 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8219 /* word3 iocb=iotag32 wqe=seq_payload_len */
8220 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8221 /* word4 iocb=rsvd wqe=rsvd */
8222 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8223 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8224 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8225 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8226 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8227 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8228 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8229 LPFC_WQE_LENLOC_WORD3);
8230 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8231 break;
4f774513
JS
8232 case CMD_FCP_IWRITE64_CR:
8233 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8234 /* word3 iocb=iotag wqe=payload_offset_len */
8235 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8236 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8237 xmit_len + sizeof(struct fcp_rsp));
8238 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8239 0);
f0d9bccc
JS
8240 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8241 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8242 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8243 iocbq->iocb.ulpFCP2Rcvy);
8244 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8245 /* Always open the exchange */
8246 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
f0d9bccc
JS
8247 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8248 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8249 LPFC_WQE_LENLOC_WORD4);
8250 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8251 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8252 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7851fe2c 8253 break;
4f774513 8254 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8255 /* word3 iocb=iotag wqe=payload_offset_len */
8256 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8257 bf_set(payload_offset_len, &wqe->fcp_iread,
8258 xmit_len + sizeof(struct fcp_rsp));
8259 bf_set(cmd_buff_len, &wqe->fcp_iread,
8260 0);
f0d9bccc
JS
8261 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8262 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8263 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8264 iocbq->iocb.ulpFCP2Rcvy);
8265 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688
JS
8266 /* Always open the exchange */
8267 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
f0d9bccc
JS
8268 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8269 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8270 LPFC_WQE_LENLOC_WORD4);
8271 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8272 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8273 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7851fe2c 8274 break;
4f774513 8275 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
8276 /* word3 iocb=iotag wqe=payload_offset_len */
8277 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8278 bf_set(payload_offset_len, &wqe->fcp_icmd,
8279 xmit_len + sizeof(struct fcp_rsp));
8280 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8281 0);
f0d9bccc 8282 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 8283 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8284 /* Always open the exchange */
f0d9bccc
JS
8285 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8286 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8287 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8288 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8289 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8290 LPFC_WQE_LENLOC_NONE);
8291 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
2a94aea4
JS
8292 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8293 iocbq->iocb.ulpFCP2Rcvy);
7851fe2c 8294 break;
4f774513 8295 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
8296 /* For this command calculate the xmit length of the
8297 * request bde.
8298 */
8299 xmit_len = 0;
8300 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8301 sizeof(struct ulp_bde64);
8302 for (i = 0; i < numBdes; i++) {
63e801ce 8303 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
8304 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8305 break;
63e801ce
JS
8306 xmit_len += bde.tus.f.bdeSize;
8307 }
f0d9bccc
JS
8308 /* word3 iocb=IO_TAG wqe=request_payload_len */
8309 wqe->gen_req.request_payload_len = xmit_len;
8310 /* word4 iocb=parameter wqe=relative_offset memcpy */
8311 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
8312 /* word6 context tag copied in memcpy */
8313 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8314 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8315 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8316 "2015 Invalid CT %x command 0x%x\n",
8317 ct, iocbq->iocb.ulpCommand);
8318 return IOCB_ERROR;
8319 }
f0d9bccc
JS
8320 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8321 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8322 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8323 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8324 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8325 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8326 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8327 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 8328 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 8329 command_type = OTHER_COMMAND;
7851fe2c 8330 break;
4f774513 8331 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 8332 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 8333 /* words0-2 BDE memcpy */
f0d9bccc
JS
8334 /* word3 iocb=iotag32 wqe=response_payload_len */
8335 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
8336 /* word4 */
8337 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
8338 /* word5 iocb=rsvd wge=did */
8339 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
8340 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8341
8342 if_type = bf_get(lpfc_sli_intf_if_type,
8343 &phba->sli4_hba.sli_intf);
8344 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8345 if (iocbq->vport->fc_flag & FC_PT2PT) {
8346 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8347 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8348 iocbq->vport->fc_myDID);
8349 if (iocbq->vport->fc_myDID == Fabric_DID) {
8350 bf_set(wqe_els_did,
8351 &wqe->xmit_els_rsp.wqe_dest, 0);
8352 }
8353 }
8354 }
f0d9bccc
JS
8355 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8356 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8357 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8358 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 8359 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 8360 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 8361 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 8362 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
8363 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8364 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8365 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8366 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8367 LPFC_WQE_LENLOC_WORD3);
8368 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
8369 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8370 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
8371 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8372 iocbq->context2)->virt);
8373 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
8374 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8375 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 8376 iocbq->vport->fc_myDID);
939723a4
JS
8377 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8378 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
8379 phba->vpi_ids[phba->pport->vpi]);
8380 }
4f774513 8381 command_type = OTHER_COMMAND;
7851fe2c 8382 break;
4f774513
JS
8383 case CMD_CLOSE_XRI_CN:
8384 case CMD_ABORT_XRI_CN:
8385 case CMD_ABORT_XRI_CX:
8386 /* words 0-2 memcpy should be 0 rserved */
8387 /* port will send abts */
dcf2a4e0
JS
8388 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8389 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8390 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8391 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8392 } else
8393 fip = 0;
8394
8395 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 8396 /*
dcf2a4e0
JS
8397 * The link is down, or the command was ELS_FIP
8398 * so the fw does not need to send abts
4f774513
JS
8399 * on the wire.
8400 */
8401 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8402 else
8403 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8404 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
8405 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8406 wqe->abort_cmd.rsrvd5 = 0;
8407 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
8408 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8409 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
8410 /*
8411 * The abort handler will send us CMD_ABORT_XRI_CN or
8412 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8413 */
f0d9bccc
JS
8414 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8415 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8416 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8417 LPFC_WQE_LENLOC_NONE);
4f774513
JS
8418 cmnd = CMD_ABORT_XRI_CX;
8419 command_type = OTHER_COMMAND;
8420 xritag = 0;
7851fe2c 8421 break;
6669f9bb 8422 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 8423 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 8424 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
8425 * we re-construct this WQE here based on information in
8426 * iocbq from scratch.
8427 */
8428 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 8429 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 8430 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
8431 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8432 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
8433 LPFC_ABTS_UNSOL_INT) {
8434 /* ABTS sent by initiator to CT exchange, the
8435 * RX_ID field will be filled with the newly
8436 * allocated responder XRI.
8437 */
8438 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8439 iocbq->sli4_xritag);
8440 } else {
8441 /* ABTS sent by responder to CT exchange, the
8442 * RX_ID field will be filled with the responder
8443 * RX_ID from ABTS.
8444 */
8445 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 8446 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 8447 }
6669f9bb
JS
8448 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8449 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
8450
8451 /* Use CT=VPI */
8452 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8453 ndlp->nlp_DID);
8454 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8455 iocbq->iocb.ulpContext);
8456 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 8457 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 8458 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
8459 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8460 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8461 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
8462 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8463 command_type = OTHER_COMMAND;
546fc854
JS
8464 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8465 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8466 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8467 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8468 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8469 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8470 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8471 }
8472
7851fe2c 8473 break;
4f774513
JS
8474 case CMD_XRI_ABORTED_CX:
8475 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
8476 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8477 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8478 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8479 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8480 default:
8481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8482 "2014 Invalid command 0x%x\n",
8483 iocbq->iocb.ulpCommand);
8484 return IOCB_ERROR;
7851fe2c 8485 break;
4f774513 8486 }
6d368e53 8487
8012cc38
JS
8488 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8489 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8490 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8491 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8492 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8493 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8494 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8495 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
8496 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8497 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8498 wqe->generic.wqe_com.abort_tag = abort_tag;
8499 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8500 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8501 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8502 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
8503 return 0;
8504}
8505
8506/**
8507 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8508 * @phba: Pointer to HBA context object.
8509 * @ring_number: SLI ring number to issue iocb on.
8510 * @piocb: Pointer to command iocb.
8511 * @flag: Flag indicating if this command can be put into txq.
8512 *
8513 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8514 * an iocb command to an HBA with SLI-4 interface spec.
8515 *
8516 * This function is called with hbalock held. The function will return success
8517 * after it successfully submit the iocb to firmware or after adding to the
8518 * txq.
8519 **/
8520static int
8521__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8522 struct lpfc_iocbq *piocb, uint32_t flag)
8523{
8524 struct lpfc_sglq *sglq;
4f774513
JS
8525 union lpfc_wqe wqe;
8526 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
8527
8528 if (piocb->sli4_xritag == NO_XRI) {
8529 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 8530 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
8531 sglq = NULL;
8532 else {
0e9bb8d7 8533 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
8534 if (!(flag & SLI_IOCB_RET_IOCB)) {
8535 __lpfc_sli_ringtx_put(phba,
8536 pring, piocb);
8537 return IOCB_SUCCESS;
8538 } else {
8539 return IOCB_BUSY;
8540 }
8541 } else {
6d368e53 8542 sglq = __lpfc_sli_get_sglq(phba, piocb);
2a9bf3d0
JS
8543 if (!sglq) {
8544 if (!(flag & SLI_IOCB_RET_IOCB)) {
8545 __lpfc_sli_ringtx_put(phba,
8546 pring,
8547 piocb);
8548 return IOCB_SUCCESS;
8549 } else
8550 return IOCB_BUSY;
8551 }
8552 }
4f774513
JS
8553 }
8554 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6d368e53
JS
8555 /* These IO's already have an XRI and a mapped sgl. */
8556 sglq = NULL;
4f774513 8557 } else {
6d368e53
JS
8558 /*
8559 * This is a continuation of a commandi,(CX) so this
4f774513
JS
8560 * sglq is on the active list
8561 */
edccdc17 8562 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
8563 if (!sglq)
8564 return IOCB_ERROR;
8565 }
8566
8567 if (sglq) {
6d368e53 8568 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 8569 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 8570 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
8571 return IOCB_ERROR;
8572 }
8573
8574 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8575 return IOCB_ERROR;
8576
341af102
JS
8577 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8578 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
ea714f3d
JS
8579 if (unlikely(!phba->sli4_hba.fcp_wq))
8580 return IOCB_ERROR;
5ffc266e
JS
8581 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8582 &wqe))
4f774513
JS
8583 return IOCB_ERROR;
8584 } else {
ea714f3d
JS
8585 if (unlikely(!phba->sli4_hba.els_wq))
8586 return IOCB_ERROR;
4f774513
JS
8587 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8588 return IOCB_ERROR;
8589 }
8590 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8591
8592 return 0;
8593}
8594
8595/**
8596 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8597 *
8598 * This routine wraps the actual lockless version for issusing IOCB function
8599 * pointer from the lpfc_hba struct.
8600 *
8601 * Return codes:
8602 * IOCB_ERROR - Error
8603 * IOCB_SUCCESS - Success
8604 * IOCB_BUSY - Busy
8605 **/
2a9bf3d0 8606int
4f774513
JS
8607__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8608 struct lpfc_iocbq *piocb, uint32_t flag)
8609{
8610 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8611}
8612
8613/**
25985edc 8614 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
8615 * @phba: The hba struct for which this call is being executed.
8616 * @dev_grp: The HBA PCI-Device group number.
8617 *
8618 * This routine sets up the SLI interface API function jump table in @phba
8619 * struct.
8620 * Returns: 0 - success, -ENODEV - failure.
8621 **/
8622int
8623lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8624{
8625
8626 switch (dev_grp) {
8627 case LPFC_PCI_DEV_LP:
8628 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8629 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8630 break;
8631 case LPFC_PCI_DEV_OC:
8632 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8633 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8634 break;
8635 default:
8636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8637 "1419 Invalid HBA PCI-device group: 0x%x\n",
8638 dev_grp);
8639 return -ENODEV;
8640 break;
8641 }
8642 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8643 return 0;
8644}
8645
8646/**
8647 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8648 * @phba: Pointer to HBA context object.
8649 * @pring: Pointer to driver SLI ring object.
8650 * @piocb: Pointer to command iocb.
8651 * @flag: Flag indicating if this command can be put into txq.
8652 *
8653 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8654 * function. This function gets the hbalock and calls
8655 * __lpfc_sli_issue_iocb function and will return the error returned
8656 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8657 * functions which do not hold hbalock.
8658 **/
8659int
8660lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8661 struct lpfc_iocbq *piocb, uint32_t flag)
8662{
ba20c853 8663 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
2a76a283 8664 struct lpfc_sli_ring *pring;
ba20c853
JS
8665 struct lpfc_queue *fpeq;
8666 struct lpfc_eqe *eqe;
4f774513 8667 unsigned long iflags;
2a76a283 8668 int rc, idx;
4f774513 8669
7e56aa25 8670 if (phba->sli_rev == LPFC_SLI_REV4) {
2a76a283
JS
8671 if (piocb->iocb_flag & LPFC_IO_FCP) {
8672 if (unlikely(!phba->sli4_hba.fcp_wq))
8673 return IOCB_ERROR;
8674 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8675 piocb->fcp_wqidx = idx;
8676 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
ba20c853
JS
8677
8678 pring = &phba->sli.ring[ring_number];
8679 spin_lock_irqsave(&pring->ring_lock, iflags);
8680 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8681 flag);
8682 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8683
8684 if (lpfc_fcp_look_ahead) {
8685 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8686
8687 if (atomic_dec_and_test(&fcp_eq_hdl->
8688 fcp_eq_in_use)) {
4f774513 8689
ba20c853
JS
8690 /* Get associated EQ with this index */
8691 fpeq = phba->sli4_hba.hba_eq[idx];
8692
8693 /* Turn off interrupts from this EQ */
8694 lpfc_sli4_eq_clr_intr(fpeq);
8695
8696 /*
8697 * Process all the events on FCP EQ
8698 */
8699 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8700 lpfc_sli4_hba_handle_eqe(phba,
8701 eqe, idx);
8702 fpeq->EQ_processed++;
8703 }
8704
8705 /* Always clear and re-arm the EQ */
8706 lpfc_sli4_eq_release(fpeq,
8707 LPFC_QUEUE_REARM);
8708 }
8709 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8710 }
8711 } else {
8712 pring = &phba->sli.ring[ring_number];
8713 spin_lock_irqsave(&pring->ring_lock, iflags);
8714 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8715 flag);
8716 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8717
2a76a283 8718 }
7e56aa25
JS
8719 } else {
8720 /* For now, SLI2/3 will still use hbalock */
8721 spin_lock_irqsave(&phba->hbalock, iflags);
8722 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8723 spin_unlock_irqrestore(&phba->hbalock, iflags);
8724 }
4f774513
JS
8725 return rc;
8726}
8727
8728/**
8729 * lpfc_extra_ring_setup - Extra ring setup function
8730 * @phba: Pointer to HBA context object.
8731 *
8732 * This function is called while driver attaches with the
8733 * HBA to setup the extra ring. The extra ring is used
8734 * only when driver needs to support target mode functionality
8735 * or IP over FC functionalities.
8736 *
8737 * This function is called with no lock held.
8738 **/
8739static int
8740lpfc_extra_ring_setup( struct lpfc_hba *phba)
8741{
8742 struct lpfc_sli *psli;
8743 struct lpfc_sli_ring *pring;
8744
8745 psli = &phba->sli;
8746
8747 /* Adjust cmd/rsp ring iocb entries more evenly */
8748
8749 /* Take some away from the FCP ring */
8750 pring = &psli->ring[psli->fcp_ring];
7e56aa25
JS
8751 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8752 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8753 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8754 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 8755
a4bc3379
JS
8756 /* and give them to the extra ring */
8757 pring = &psli->ring[psli->extra_ring];
8758
7e56aa25
JS
8759 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8760 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8761 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8762 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
8763
8764 /* Setup default profile for this ring */
8765 pring->iotag_max = 4096;
8766 pring->num_mask = 1;
8767 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
8768 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8769 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
8770 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8771 return 0;
8772}
8773
cb69f7de
JS
8774/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8775 * @phba: Pointer to HBA context object.
8776 * @iocbq: Pointer to iocb object.
8777 *
8778 * The async_event handler calls this routine when it receives
8779 * an ASYNC_STATUS_CN event from the port. The port generates
8780 * this event when an Abort Sequence request to an rport fails
8781 * twice in succession. The abort could be originated by the
8782 * driver or by the port. The ABTS could have been for an ELS
8783 * or FCP IO. The port only generates this event when an ABTS
8784 * fails to complete after one retry.
8785 */
8786static void
8787lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8788 struct lpfc_iocbq *iocbq)
8789{
8790 struct lpfc_nodelist *ndlp = NULL;
8791 uint16_t rpi = 0, vpi = 0;
8792 struct lpfc_vport *vport = NULL;
8793
8794 /* The rpi in the ulpContext is vport-sensitive. */
8795 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8796 rpi = iocbq->iocb.ulpContext;
8797
8798 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8799 "3092 Port generated ABTS async event "
8800 "on vpi %d rpi %d status 0x%x\n",
8801 vpi, rpi, iocbq->iocb.ulpStatus);
8802
8803 vport = lpfc_find_vport_by_vpid(phba, vpi);
8804 if (!vport)
8805 goto err_exit;
8806 ndlp = lpfc_findnode_rpi(vport, rpi);
8807 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8808 goto err_exit;
8809
8810 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8811 lpfc_sli_abts_recover_port(vport, ndlp);
8812 return;
8813
8814 err_exit:
8815 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8816 "3095 Event Context not found, no "
8817 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8818 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8819 vpi, rpi);
8820}
8821
8822/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8823 * @phba: pointer to HBA context object.
8824 * @ndlp: nodelist pointer for the impacted rport.
8825 * @axri: pointer to the wcqe containing the failed exchange.
8826 *
8827 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8828 * port. The port generates this event when an abort exchange request to an
8829 * rport fails twice in succession with no reply. The abort could be originated
8830 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8831 */
8832void
8833lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8834 struct lpfc_nodelist *ndlp,
8835 struct sli4_wcqe_xri_aborted *axri)
8836{
8837 struct lpfc_vport *vport;
5c1db2ac 8838 uint32_t ext_status = 0;
cb69f7de 8839
6b5151fd 8840 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
8841 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8842 "3115 Node Context not found, driver "
8843 "ignoring abts err event\n");
6b5151fd
JS
8844 return;
8845 }
8846
cb69f7de
JS
8847 vport = ndlp->vport;
8848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8849 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 8850 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 8851 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 8852 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
8853 bf_get(lpfc_wcqe_xa_status, axri),
8854 axri->parameter);
cb69f7de 8855
5c1db2ac
JS
8856 /*
8857 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8858 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8859 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8860 */
e3d2b802 8861 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
8862 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8863 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
8864 lpfc_sli_abts_recover_port(vport, ndlp);
8865}
8866
e59058c4 8867/**
3621a710 8868 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
8869 * @phba: Pointer to HBA context object.
8870 * @pring: Pointer to driver SLI ring object.
8871 * @iocbq: Pointer to iocb object.
8872 *
8873 * This function is called by the slow ring event handler
8874 * function when there is an ASYNC event iocb in the ring.
8875 * This function is called with no lock held.
8876 * Currently this function handles only temperature related
8877 * ASYNC events. The function decodes the temperature sensor
8878 * event message and posts events for the management applications.
8879 **/
98c9ea5c 8880static void
57127f15
JS
8881lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8882 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8883{
8884 IOCB_t *icmd;
8885 uint16_t evt_code;
57127f15
JS
8886 struct temp_event temp_event_data;
8887 struct Scsi_Host *shost;
a257bf90 8888 uint32_t *iocb_w;
57127f15
JS
8889
8890 icmd = &iocbq->iocb;
8891 evt_code = icmd->un.asyncstat.evt_code;
57127f15 8892
cb69f7de
JS
8893 switch (evt_code) {
8894 case ASYNC_TEMP_WARN:
8895 case ASYNC_TEMP_SAFE:
8896 temp_event_data.data = (uint32_t) icmd->ulpContext;
8897 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8898 if (evt_code == ASYNC_TEMP_WARN) {
8899 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8900 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8901 "0347 Adapter is very hot, please take "
8902 "corrective action. temperature : %d Celsius\n",
8903 (uint32_t) icmd->ulpContext);
8904 } else {
8905 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8906 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8907 "0340 Adapter temperature is OK now. "
8908 "temperature : %d Celsius\n",
8909 (uint32_t) icmd->ulpContext);
8910 }
8911
8912 /* Send temperature change event to applications */
8913 shost = lpfc_shost_from_vport(phba->pport);
8914 fc_host_post_vendor_event(shost, fc_get_event_number(),
8915 sizeof(temp_event_data), (char *) &temp_event_data,
8916 LPFC_NL_VENDOR_ID);
8917 break;
8918 case ASYNC_STATUS_CN:
8919 lpfc_sli_abts_err_handler(phba, iocbq);
8920 break;
8921 default:
a257bf90 8922 iocb_w = (uint32_t *) icmd;
cb69f7de 8923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 8924 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 8925 " evt_code 0x%x\n"
a257bf90
JS
8926 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8927 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8928 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8929 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 8930 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
8931 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8932 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8933 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8934 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8935
cb69f7de 8936 break;
57127f15 8937 }
57127f15
JS
8938}
8939
8940
e59058c4 8941/**
3621a710 8942 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
8943 * @phba: Pointer to HBA context object.
8944 *
8945 * lpfc_sli_setup sets up rings of the SLI interface with
8946 * number of iocbs per ring and iotags. This function is
8947 * called while driver attach to the HBA and before the
8948 * interrupts are enabled. So there is no need for locking.
8949 *
8950 * This function always returns 0.
8951 **/
dea3101e
JB
8952int
8953lpfc_sli_setup(struct lpfc_hba *phba)
8954{
ed957684 8955 int i, totiocbsize = 0;
dea3101e
JB
8956 struct lpfc_sli *psli = &phba->sli;
8957 struct lpfc_sli_ring *pring;
8958
2a76a283
JS
8959 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8960 if (phba->sli_rev == LPFC_SLI_REV4)
67d12733 8961 psli->num_rings += phba->cfg_fcp_io_channel;
dea3101e
JB
8962 psli->sli_flag = 0;
8963 psli->fcp_ring = LPFC_FCP_RING;
8964 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 8965 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 8966
604a3e30
JB
8967 psli->iocbq_lookup = NULL;
8968 psli->iocbq_lookup_len = 0;
8969 psli->last_iotag = 0;
8970
dea3101e
JB
8971 for (i = 0; i < psli->num_rings; i++) {
8972 pring = &psli->ring[i];
8973 switch (i) {
8974 case LPFC_FCP_RING: /* ring 0 - FCP */
8975 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
8976 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8977 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8978 pring->sli.sli3.numCiocb +=
8979 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8980 pring->sli.sli3.numRiocb +=
8981 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8982 pring->sli.sli3.numCiocb +=
8983 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8984 pring->sli.sli3.numRiocb +=
8985 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8986 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8987 SLI3_IOCB_CMD_SIZE :
8988 SLI2_IOCB_CMD_SIZE;
7e56aa25 8989 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8990 SLI3_IOCB_RSP_SIZE :
8991 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
8992 pring->iotag_ctr = 0;
8993 pring->iotag_max =
92d7f7b0 8994 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
8995 pring->fast_iotag = pring->iotag_max;
8996 pring->num_mask = 0;
8997 break;
a4bc3379 8998 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 8999 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9000 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9001 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9002 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9003 SLI3_IOCB_CMD_SIZE :
9004 SLI2_IOCB_CMD_SIZE;
7e56aa25 9005 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9006 SLI3_IOCB_RSP_SIZE :
9007 SLI2_IOCB_RSP_SIZE;
2e0fef85 9008 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9009 pring->num_mask = 0;
9010 break;
9011 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9012 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9013 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9014 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9015 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9016 SLI3_IOCB_CMD_SIZE :
9017 SLI2_IOCB_CMD_SIZE;
7e56aa25 9018 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9019 SLI3_IOCB_RSP_SIZE :
9020 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9021 pring->fast_iotag = 0;
9022 pring->iotag_ctr = 0;
9023 pring->iotag_max = 4096;
57127f15
JS
9024 pring->lpfc_sli_rcv_async_status =
9025 lpfc_sli_async_event_handler;
6669f9bb 9026 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9027 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9028 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9029 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9030 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9031 lpfc_els_unsol_event;
dea3101e 9032 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9033 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9034 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9035 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9036 lpfc_els_unsol_event;
dea3101e
JB
9037 pring->prt[2].profile = 0; /* Mask 2 */
9038 /* NameServer Inquiry */
6a9c52cf 9039 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9040 /* NameServer */
6a9c52cf 9041 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9042 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9043 lpfc_ct_unsol_event;
dea3101e
JB
9044 pring->prt[3].profile = 0; /* Mask 3 */
9045 /* NameServer response */
6a9c52cf 9046 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9047 /* NameServer */
6a9c52cf 9048 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9049 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9050 lpfc_ct_unsol_event;
dea3101e
JB
9051 break;
9052 }
7e56aa25
JS
9053 totiocbsize += (pring->sli.sli3.numCiocb *
9054 pring->sli.sli3.sizeCiocb) +
9055 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 9056 }
ed957684 9057 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 9058 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
9059 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9060 "SLI2 SLIM Data: x%x x%lx\n",
9061 phba->brd_no, totiocbsize,
9062 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 9063 }
cf5bf97e
JW
9064 if (phba->cfg_multi_ring_support == 2)
9065 lpfc_extra_ring_setup(phba);
dea3101e
JB
9066
9067 return 0;
9068}
9069
e59058c4 9070/**
3621a710 9071 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
9072 * @phba: Pointer to HBA context object.
9073 *
9074 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9075 * ring. This function also initializes ring indices of each ring.
9076 * This function is called during the initialization of the SLI
9077 * interface of an HBA.
9078 * This function is called with no lock held and always returns
9079 * 1.
9080 **/
dea3101e 9081int
2e0fef85 9082lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e
JB
9083{
9084 struct lpfc_sli *psli;
9085 struct lpfc_sli_ring *pring;
604a3e30 9086 int i;
dea3101e
JB
9087
9088 psli = &phba->sli;
2e0fef85 9089 spin_lock_irq(&phba->hbalock);
dea3101e 9090 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9091 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
9092 /* Initialize list headers for txq and txcmplq as double linked lists */
9093 for (i = 0; i < psli->num_rings; i++) {
9094 pring = &psli->ring[i];
9095 pring->ringno = i;
7e56aa25
JS
9096 pring->sli.sli3.next_cmdidx = 0;
9097 pring->sli.sli3.local_getidx = 0;
9098 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
9099 INIT_LIST_HEAD(&pring->txq);
9100 INIT_LIST_HEAD(&pring->txcmplq);
9101 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 9102 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 9103 INIT_LIST_HEAD(&pring->postbufq);
7e56aa25 9104 spin_lock_init(&pring->ring_lock);
dea3101e 9105 }
2e0fef85
JS
9106 spin_unlock_irq(&phba->hbalock);
9107 return 1;
dea3101e
JB
9108}
9109
04c68496
JS
9110/**
9111 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9112 * @phba: Pointer to HBA context object.
9113 *
9114 * This routine flushes the mailbox command subsystem. It will unconditionally
9115 * flush all the mailbox commands in the three possible stages in the mailbox
9116 * command sub-system: pending mailbox command queue; the outstanding mailbox
9117 * command; and completed mailbox command queue. It is caller's responsibility
9118 * to make sure that the driver is in the proper state to flush the mailbox
9119 * command sub-system. Namely, the posting of mailbox commands into the
9120 * pending mailbox command queue from the various clients must be stopped;
9121 * either the HBA is in a state that it will never works on the outstanding
9122 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9123 * mailbox command has been completed.
9124 **/
9125static void
9126lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9127{
9128 LIST_HEAD(completions);
9129 struct lpfc_sli *psli = &phba->sli;
9130 LPFC_MBOXQ_t *pmb;
9131 unsigned long iflag;
9132
9133 /* Flush all the mailbox commands in the mbox system */
9134 spin_lock_irqsave(&phba->hbalock, iflag);
9135 /* The pending mailbox command queue */
9136 list_splice_init(&phba->sli.mboxq, &completions);
9137 /* The outstanding active mailbox command */
9138 if (psli->mbox_active) {
9139 list_add_tail(&psli->mbox_active->list, &completions);
9140 psli->mbox_active = NULL;
9141 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9142 }
9143 /* The completed mailbox command queue */
9144 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9145 spin_unlock_irqrestore(&phba->hbalock, iflag);
9146
9147 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9148 while (!list_empty(&completions)) {
9149 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9150 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9151 if (pmb->mbox_cmpl)
9152 pmb->mbox_cmpl(phba, pmb);
9153 }
9154}
9155
e59058c4 9156/**
3621a710 9157 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
9158 * @vport: Pointer to virtual port object.
9159 *
9160 * lpfc_sli_host_down is called to clean up the resources
9161 * associated with a vport before destroying virtual
9162 * port data structures.
9163 * This function does following operations:
9164 * - Free discovery resources associated with this virtual
9165 * port.
9166 * - Free iocbs associated with this virtual port in
9167 * the txq.
9168 * - Send abort for all iocb commands associated with this
9169 * vport in txcmplq.
9170 *
9171 * This function is called with no lock held and always returns 1.
9172 **/
92d7f7b0
JS
9173int
9174lpfc_sli_host_down(struct lpfc_vport *vport)
9175{
858c9f6c 9176 LIST_HEAD(completions);
92d7f7b0
JS
9177 struct lpfc_hba *phba = vport->phba;
9178 struct lpfc_sli *psli = &phba->sli;
9179 struct lpfc_sli_ring *pring;
9180 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
9181 int i;
9182 unsigned long flags = 0;
9183 uint16_t prev_pring_flag;
9184
9185 lpfc_cleanup_discovery_resources(vport);
9186
9187 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
9188 for (i = 0; i < psli->num_rings; i++) {
9189 pring = &psli->ring[i];
9190 prev_pring_flag = pring->flag;
5e9d9b82
JS
9191 /* Only slow rings */
9192 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9193 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9194 /* Set the lpfc data pending flag */
9195 set_bit(LPFC_DATA_READY, &phba->data_flags);
9196 }
92d7f7b0
JS
9197 /*
9198 * Error everything on the txq since these iocbs have not been
9199 * given to the FW yet.
9200 */
92d7f7b0
JS
9201 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9202 if (iocb->vport != vport)
9203 continue;
858c9f6c 9204 list_move_tail(&iocb->list, &completions);
92d7f7b0
JS
9205 }
9206
9207 /* Next issue ABTS for everything on the txcmplq */
9208 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9209 list) {
9210 if (iocb->vport != vport)
9211 continue;
9212 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9213 }
9214
9215 pring->flag = prev_pring_flag;
9216 }
9217
9218 spin_unlock_irqrestore(&phba->hbalock, flags);
9219
a257bf90
JS
9220 /* Cancel all the IOCBs from the completions list */
9221 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9222 IOERR_SLI_DOWN);
92d7f7b0
JS
9223 return 1;
9224}
9225
e59058c4 9226/**
3621a710 9227 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
9228 * @phba: Pointer to HBA context object.
9229 *
9230 * This function cleans up all iocb, buffers, mailbox commands
9231 * while shutting down the HBA. This function is called with no
9232 * lock held and always returns 1.
9233 * This function does the following to cleanup driver resources:
9234 * - Free discovery resources for each virtual port
9235 * - Cleanup any pending fabric iocbs
9236 * - Iterate through the iocb txq and free each entry
9237 * in the list.
9238 * - Free up any buffer posted to the HBA
9239 * - Free mailbox commands in the mailbox queue.
9240 **/
dea3101e 9241int
2e0fef85 9242lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 9243{
2534ba75 9244 LIST_HEAD(completions);
2e0fef85 9245 struct lpfc_sli *psli = &phba->sli;
dea3101e 9246 struct lpfc_sli_ring *pring;
0ff10d46 9247 struct lpfc_dmabuf *buf_ptr;
dea3101e 9248 unsigned long flags = 0;
04c68496
JS
9249 int i;
9250
9251 /* Shutdown the mailbox command sub-system */
618a5230 9252 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 9253
dea3101e
JB
9254 lpfc_hba_down_prep(phba);
9255
92d7f7b0
JS
9256 lpfc_fabric_abort_hba(phba);
9257
2e0fef85 9258 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e
JB
9259 for (i = 0; i < psli->num_rings; i++) {
9260 pring = &psli->ring[i];
5e9d9b82
JS
9261 /* Only slow rings */
9262 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 9263 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
9264 /* Set the lpfc data pending flag */
9265 set_bit(LPFC_DATA_READY, &phba->data_flags);
9266 }
dea3101e
JB
9267
9268 /*
9269 * Error everything on the txq since these iocbs have not been
9270 * given to the FW yet.
9271 */
2534ba75 9272 list_splice_init(&pring->txq, &completions);
2534ba75 9273 }
2e0fef85 9274 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 9275
a257bf90
JS
9276 /* Cancel all the IOCBs from the completions list */
9277 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9278 IOERR_SLI_DOWN);
dea3101e 9279
0ff10d46
JS
9280 spin_lock_irqsave(&phba->hbalock, flags);
9281 list_splice_init(&phba->elsbuf, &completions);
9282 phba->elsbuf_cnt = 0;
9283 phba->elsbuf_prev_cnt = 0;
9284 spin_unlock_irqrestore(&phba->hbalock, flags);
9285
9286 while (!list_empty(&completions)) {
9287 list_remove_head(&completions, buf_ptr,
9288 struct lpfc_dmabuf, list);
9289 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9290 kfree(buf_ptr);
9291 }
9292
dea3101e
JB
9293 /* Return any active mbox cmds */
9294 del_timer_sync(&psli->mbox_tmo);
2e0fef85 9295
da0436e9 9296 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 9297 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 9298 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 9299
da0436e9
JS
9300 return 1;
9301}
9302
e59058c4 9303/**
3621a710 9304 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
9305 * @srcp: Source memory pointer.
9306 * @destp: Destination memory pointer.
9307 * @cnt: Number of words required to be copied.
9308 *
9309 * This function is used for copying data between driver memory
9310 * and the SLI memory. This function also changes the endianness
9311 * of each word if native endianness is different from SLI
9312 * endianness. This function can be called with or without
9313 * lock.
9314 **/
dea3101e
JB
9315void
9316lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9317{
9318 uint32_t *src = srcp;
9319 uint32_t *dest = destp;
9320 uint32_t ldata;
9321 int i;
9322
9323 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9324 ldata = *src;
9325 ldata = le32_to_cpu(ldata);
9326 *dest = ldata;
9327 src++;
9328 dest++;
9329 }
9330}
9331
e59058c4 9332
a0c87cbd
JS
9333/**
9334 * lpfc_sli_bemem_bcopy - SLI memory copy function
9335 * @srcp: Source memory pointer.
9336 * @destp: Destination memory pointer.
9337 * @cnt: Number of words required to be copied.
9338 *
9339 * This function is used for copying data between a data structure
9340 * with big endian representation to local endianness.
9341 * This function can be called with or without lock.
9342 **/
9343void
9344lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9345{
9346 uint32_t *src = srcp;
9347 uint32_t *dest = destp;
9348 uint32_t ldata;
9349 int i;
9350
9351 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9352 ldata = *src;
9353 ldata = be32_to_cpu(ldata);
9354 *dest = ldata;
9355 src++;
9356 dest++;
9357 }
9358}
9359
e59058c4 9360/**
3621a710 9361 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
9362 * @phba: Pointer to HBA context object.
9363 * @pring: Pointer to driver SLI ring object.
9364 * @mp: Pointer to driver buffer object.
9365 *
9366 * This function is called with no lock held.
9367 * It always return zero after adding the buffer to the postbufq
9368 * buffer list.
9369 **/
dea3101e 9370int
2e0fef85
JS
9371lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9372 struct lpfc_dmabuf *mp)
dea3101e
JB
9373{
9374 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9375 later */
2e0fef85 9376 spin_lock_irq(&phba->hbalock);
dea3101e 9377 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 9378 pring->postbufq_cnt++;
2e0fef85 9379 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9380 return 0;
9381}
9382
e59058c4 9383/**
3621a710 9384 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
9385 * @phba: Pointer to HBA context object.
9386 *
9387 * When HBQ is enabled, buffers are searched based on tags. This function
9388 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9389 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9390 * does not conflict with tags of buffer posted for unsolicited events.
9391 * The function returns the allocated tag. The function is called with
9392 * no locks held.
9393 **/
76bb24ef
JS
9394uint32_t
9395lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9396{
9397 spin_lock_irq(&phba->hbalock);
9398 phba->buffer_tag_count++;
9399 /*
9400 * Always set the QUE_BUFTAG_BIT to distiguish between
9401 * a tag assigned by HBQ.
9402 */
9403 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9404 spin_unlock_irq(&phba->hbalock);
9405 return phba->buffer_tag_count;
9406}
9407
e59058c4 9408/**
3621a710 9409 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
9410 * @phba: Pointer to HBA context object.
9411 * @pring: Pointer to driver SLI ring object.
9412 * @tag: Buffer tag.
9413 *
9414 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9415 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9416 * iocb is posted to the response ring with the tag of the buffer.
9417 * This function searches the pring->postbufq list using the tag
9418 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9419 * iocb. If the buffer is found then lpfc_dmabuf object of the
9420 * buffer is returned to the caller else NULL is returned.
9421 * This function is called with no lock held.
9422 **/
76bb24ef
JS
9423struct lpfc_dmabuf *
9424lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9425 uint32_t tag)
9426{
9427 struct lpfc_dmabuf *mp, *next_mp;
9428 struct list_head *slp = &pring->postbufq;
9429
25985edc 9430 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
9431 spin_lock_irq(&phba->hbalock);
9432 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9433 if (mp->buffer_tag == tag) {
9434 list_del_init(&mp->list);
9435 pring->postbufq_cnt--;
9436 spin_unlock_irq(&phba->hbalock);
9437 return mp;
9438 }
9439 }
9440
9441 spin_unlock_irq(&phba->hbalock);
9442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 9443 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
9444 "ring %d Data x%lx x%p x%p x%x\n",
9445 pring->ringno, (unsigned long) tag,
9446 slp->next, slp->prev, pring->postbufq_cnt);
9447
9448 return NULL;
9449}
dea3101e 9450
e59058c4 9451/**
3621a710 9452 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
9453 * @phba: Pointer to HBA context object.
9454 * @pring: Pointer to driver SLI ring object.
9455 * @phys: DMA address of the buffer.
9456 *
9457 * This function searches the buffer list using the dma_address
9458 * of unsolicited event to find the driver's lpfc_dmabuf object
9459 * corresponding to the dma_address. The function returns the
9460 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9461 * This function is called by the ct and els unsolicited event
9462 * handlers to get the buffer associated with the unsolicited
9463 * event.
9464 *
9465 * This function is called with no lock held.
9466 **/
dea3101e
JB
9467struct lpfc_dmabuf *
9468lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9469 dma_addr_t phys)
9470{
9471 struct lpfc_dmabuf *mp, *next_mp;
9472 struct list_head *slp = &pring->postbufq;
9473
25985edc 9474 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 9475 spin_lock_irq(&phba->hbalock);
dea3101e
JB
9476 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9477 if (mp->phys == phys) {
9478 list_del_init(&mp->list);
9479 pring->postbufq_cnt--;
2e0fef85 9480 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9481 return mp;
9482 }
9483 }
9484
2e0fef85 9485 spin_unlock_irq(&phba->hbalock);
dea3101e 9486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 9487 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 9488 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 9489 pring->ringno, (unsigned long long)phys,
dea3101e
JB
9490 slp->next, slp->prev, pring->postbufq_cnt);
9491 return NULL;
9492}
9493
e59058c4 9494/**
3621a710 9495 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
9496 * @phba: Pointer to HBA context object.
9497 * @cmdiocb: Pointer to driver command iocb object.
9498 * @rspiocb: Pointer to driver response iocb object.
9499 *
9500 * This function is the completion handler for the abort iocbs for
9501 * ELS commands. This function is called from the ELS ring event
9502 * handler with no lock held. This function frees memory resources
9503 * associated with the abort iocb.
9504 **/
dea3101e 9505static void
2e0fef85
JS
9506lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9507 struct lpfc_iocbq *rspiocb)
dea3101e 9508{
2e0fef85 9509 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 9510 uint16_t abort_iotag, abort_context;
ff78d8f9 9511 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
9512
9513 if (irsp->ulpStatus) {
ff78d8f9
JS
9514
9515 /*
9516 * Assume that the port already completed and returned, or
9517 * will return the iocb. Just Log the message.
9518 */
2680eeaa
JS
9519 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9520 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9521
2e0fef85 9522 spin_lock_irq(&phba->hbalock);
45ed1190
JS
9523 if (phba->sli_rev < LPFC_SLI_REV4) {
9524 if (abort_iotag != 0 &&
9525 abort_iotag <= phba->sli.last_iotag)
9526 abort_iocb =
9527 phba->sli.iocbq_lookup[abort_iotag];
9528 } else
9529 /* For sli4 the abort_tag is the XRI,
9530 * so the abort routine puts the iotag of the iocb
9531 * being aborted in the context field of the abort
9532 * IOCB.
9533 */
9534 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 9535
2a9bf3d0
JS
9536 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9537 "0327 Cannot abort els iocb %p "
9538 "with tag %x context %x, abort status %x, "
9539 "abort code %x\n",
9540 abort_iocb, abort_iotag, abort_context,
9541 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 9542
ff78d8f9 9543 spin_unlock_irq(&phba->hbalock);
2680eeaa 9544 }
604a3e30 9545 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
9546 return;
9547}
9548
e59058c4 9549/**
3621a710 9550 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
9551 * @phba: Pointer to HBA context object.
9552 * @cmdiocb: Pointer to driver command iocb object.
9553 * @rspiocb: Pointer to driver response iocb object.
9554 *
9555 * The function is called from SLI ring event handler with no
9556 * lock held. This function is the completion handler for ELS commands
9557 * which are aborted. The function frees memory resources used for
9558 * the aborted ELS commands.
9559 **/
92d7f7b0
JS
9560static void
9561lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9562 struct lpfc_iocbq *rspiocb)
9563{
9564 IOCB_t *irsp = &rspiocb->iocb;
9565
9566 /* ELS cmd tag <ulpIoTag> completes */
9567 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 9568 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 9569 "x%x x%x x%x\n",
e8b62011 9570 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 9571 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
9572 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9573 lpfc_ct_free_iocb(phba, cmdiocb);
9574 else
9575 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
9576 return;
9577}
9578
e59058c4 9579/**
5af5eee7 9580 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
9581 * @phba: Pointer to HBA context object.
9582 * @pring: Pointer to driver SLI ring object.
9583 * @cmdiocb: Pointer to driver command iocb object.
9584 *
5af5eee7
JS
9585 * This function issues an abort iocb for the provided command iocb down to
9586 * the port. Other than the case the outstanding command iocb is an abort
9587 * request, this function issues abort out unconditionally. This function is
9588 * called with hbalock held. The function returns 0 when it fails due to
9589 * memory allocation failure or when the command iocb is an abort request.
e59058c4 9590 **/
5af5eee7
JS
9591static int
9592lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 9593 struct lpfc_iocbq *cmdiocb)
dea3101e 9594{
2e0fef85 9595 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 9596 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
9597 IOCB_t *icmd = NULL;
9598 IOCB_t *iabt = NULL;
5af5eee7 9599 int retval;
7e56aa25 9600 unsigned long iflags;
07951076 9601
92d7f7b0
JS
9602 /*
9603 * There are certain command types we don't want to abort. And we
9604 * don't want to abort commands that are already in the process of
9605 * being aborted.
07951076
JS
9606 */
9607 icmd = &cmdiocb->iocb;
2e0fef85 9608 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
9609 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9610 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
9611 return 0;
9612
dea3101e 9613 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 9614 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
9615 if (abtsiocbp == NULL)
9616 return 0;
dea3101e 9617
07951076 9618 /* This signals the response to set the correct status
341af102 9619 * before calling the completion handler
07951076
JS
9620 */
9621 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9622
dea3101e 9623 iabt = &abtsiocbp->iocb;
07951076
JS
9624 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9625 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 9626 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 9627 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
9628 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9629 }
da0436e9
JS
9630 else
9631 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
9632 iabt->ulpLe = 1;
9633 iabt->ulpClass = icmd->ulpClass;
dea3101e 9634
5ffc266e
JS
9635 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9636 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
9637 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9638 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9639
2e0fef85 9640 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
9641 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9642 else
9643 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 9644
07951076 9645 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 9646
e8b62011
JS
9647 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9648 "0339 Abort xri x%x, original iotag x%x, "
9649 "abort cmd iotag x%x\n",
2a9bf3d0 9650 iabt->un.acxri.abortIoTag,
e8b62011 9651 iabt->un.acxri.abortContextTag,
2a9bf3d0 9652 abtsiocbp->iotag);
7e56aa25
JS
9653
9654 if (phba->sli_rev == LPFC_SLI_REV4) {
9655 /* Note: both hbalock and ring_lock need to be set here */
9656 spin_lock_irqsave(&pring->ring_lock, iflags);
9657 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9658 abtsiocbp, 0);
9659 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9660 } else {
9661 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9662 abtsiocbp, 0);
9663 }
dea3101e 9664
d7c255b2
JS
9665 if (retval)
9666 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
9667
9668 /*
9669 * Caller to this routine should check for IOCB_ERROR
9670 * and handle it properly. This routine no longer removes
9671 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9672 */
9673 return retval;
9674}
9675
9676/**
9677 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9678 * @phba: Pointer to HBA context object.
9679 * @pring: Pointer to driver SLI ring object.
9680 * @cmdiocb: Pointer to driver command iocb object.
9681 *
9682 * This function issues an abort iocb for the provided command iocb. In case
9683 * of unloading, the abort iocb will not be issued to commands on the ELS
9684 * ring. Instead, the callback function shall be changed to those commands
9685 * so that nothing happens when them finishes. This function is called with
9686 * hbalock held. The function returns 0 when the command iocb is an abort
9687 * request.
9688 **/
9689int
9690lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9691 struct lpfc_iocbq *cmdiocb)
9692{
9693 struct lpfc_vport *vport = cmdiocb->vport;
9694 int retval = IOCB_ERROR;
9695 IOCB_t *icmd = NULL;
9696
9697 /*
9698 * There are certain command types we don't want to abort. And we
9699 * don't want to abort commands that are already in the process of
9700 * being aborted.
9701 */
9702 icmd = &cmdiocb->iocb;
9703 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9704 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9705 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9706 return 0;
9707
9708 /*
9709 * If we're unloading, don't abort iocb on the ELS ring, but change
9710 * the callback so that nothing happens when it finishes.
9711 */
9712 if ((vport->load_flag & FC_UNLOADING) &&
9713 (pring->ringno == LPFC_ELS_RING)) {
9714 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9715 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9716 else
9717 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9718 goto abort_iotag_exit;
9719 }
9720
9721 /* Now, we try to issue the abort to the cmdiocb out */
9722 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9723
07951076 9724abort_iotag_exit:
2e0fef85
JS
9725 /*
9726 * Caller to this routine should check for IOCB_ERROR
9727 * and handle it properly. This routine no longer removes
9728 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 9729 */
2e0fef85 9730 return retval;
dea3101e
JB
9731}
9732
5af5eee7
JS
9733/**
9734 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9735 * @phba: Pointer to HBA context object.
9736 * @pring: Pointer to driver SLI ring object.
9737 *
9738 * This function aborts all iocbs in the given ring and frees all the iocb
9739 * objects in txq. This function issues abort iocbs unconditionally for all
9740 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9741 * to complete before the return of this function. The caller is not required
9742 * to hold any locks.
9743 **/
9744static void
9745lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9746{
9747 LIST_HEAD(completions);
9748 struct lpfc_iocbq *iocb, *next_iocb;
9749
9750 if (pring->ringno == LPFC_ELS_RING)
9751 lpfc_fabric_abort_hba(phba);
9752
9753 spin_lock_irq(&phba->hbalock);
9754
9755 /* Take off all the iocbs on txq for cancelling */
9756 list_splice_init(&pring->txq, &completions);
9757 pring->txq_cnt = 0;
9758
9759 /* Next issue ABTS for everything on the txcmplq */
9760 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9761 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9762
9763 spin_unlock_irq(&phba->hbalock);
9764
9765 /* Cancel all the IOCBs from the completions list */
9766 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9767 IOERR_SLI_ABORTED);
9768}
9769
9770/**
9771 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9772 * @phba: pointer to lpfc HBA data structure.
9773 *
9774 * This routine will abort all pending and outstanding iocbs to an HBA.
9775 **/
9776void
9777lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9778{
9779 struct lpfc_sli *psli = &phba->sli;
9780 struct lpfc_sli_ring *pring;
9781 int i;
9782
9783 for (i = 0; i < psli->num_rings; i++) {
9784 pring = &psli->ring[i];
9785 lpfc_sli_iocb_ring_abort(phba, pring);
9786 }
9787}
9788
e59058c4 9789/**
3621a710 9790 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
9791 * @iocbq: Pointer to driver iocb object.
9792 * @vport: Pointer to driver virtual port object.
9793 * @tgt_id: SCSI ID of the target.
9794 * @lun_id: LUN ID of the scsi device.
9795 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9796 *
3621a710 9797 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
9798 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9799 * 0 if the filtering criteria is met for the given iocb and will return
9800 * 1 if the filtering criteria is not met.
9801 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9802 * given iocb is for the SCSI device specified by vport, tgt_id and
9803 * lun_id parameter.
9804 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9805 * given iocb is for the SCSI target specified by vport and tgt_id
9806 * parameters.
9807 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9808 * given iocb is for the SCSI host associated with the given vport.
9809 * This function is called with no locks held.
9810 **/
dea3101e 9811static int
51ef4c26
JS
9812lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9813 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 9814 lpfc_ctx_cmd ctx_cmd)
dea3101e 9815{
0bd4ca25 9816 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
9817 int rc = 1;
9818
0bd4ca25
JSEC
9819 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9820 return rc;
9821
51ef4c26
JS
9822 if (iocbq->vport != vport)
9823 return rc;
9824
0bd4ca25 9825 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 9826
495a714c 9827 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
9828 return rc;
9829
9830 switch (ctx_cmd) {
9831 case LPFC_CTX_LUN:
495a714c
JS
9832 if ((lpfc_cmd->rdata->pnode) &&
9833 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9834 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
9835 rc = 0;
9836 break;
9837 case LPFC_CTX_TGT:
495a714c
JS
9838 if ((lpfc_cmd->rdata->pnode) &&
9839 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
9840 rc = 0;
9841 break;
dea3101e
JB
9842 case LPFC_CTX_HOST:
9843 rc = 0;
9844 break;
9845 default:
9846 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 9847 __func__, ctx_cmd);
dea3101e
JB
9848 break;
9849 }
9850
9851 return rc;
9852}
9853
e59058c4 9854/**
3621a710 9855 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
9856 * @vport: Pointer to virtual port.
9857 * @tgt_id: SCSI ID of the target.
9858 * @lun_id: LUN ID of the scsi device.
9859 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9860 *
9861 * This function returns number of FCP commands pending for the vport.
9862 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9863 * commands pending on the vport associated with SCSI device specified
9864 * by tgt_id and lun_id parameters.
9865 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9866 * commands pending on the vport associated with SCSI target specified
9867 * by tgt_id parameter.
9868 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9869 * commands pending on the vport.
9870 * This function returns the number of iocbs which satisfy the filter.
9871 * This function is called without any lock held.
9872 **/
dea3101e 9873int
51ef4c26
JS
9874lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9875 lpfc_ctx_cmd ctx_cmd)
dea3101e 9876{
51ef4c26 9877 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9878 struct lpfc_iocbq *iocbq;
9879 int sum, i;
dea3101e 9880
0bd4ca25
JSEC
9881 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9882 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9883
51ef4c26
JS
9884 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9885 ctx_cmd) == 0)
0bd4ca25 9886 sum++;
dea3101e 9887 }
0bd4ca25 9888
dea3101e
JB
9889 return sum;
9890}
9891
e59058c4 9892/**
3621a710 9893 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
9894 * @phba: Pointer to HBA context object
9895 * @cmdiocb: Pointer to command iocb object.
9896 * @rspiocb: Pointer to response iocb object.
9897 *
9898 * This function is called when an aborted FCP iocb completes. This
9899 * function is called by the ring event handler with no lock held.
9900 * This function frees the iocb.
9901 **/
5eb95af0 9902void
2e0fef85
JS
9903lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9904 struct lpfc_iocbq *rspiocb)
5eb95af0 9905{
cb69f7de 9906 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 9907 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
9908 "original iotag x%x, abort cmd iotag x%x "
9909 "status 0x%x, reason 0x%x\n",
9910 cmdiocb->iocb.un.acxri.abortContextTag,
9911 cmdiocb->iocb.un.acxri.abortIoTag,
9912 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9913 rspiocb->iocb.un.ulpWord[4]);
604a3e30 9914 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
9915 return;
9916}
9917
e59058c4 9918/**
3621a710 9919 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
9920 * @vport: Pointer to virtual port.
9921 * @pring: Pointer to driver SLI ring object.
9922 * @tgt_id: SCSI ID of the target.
9923 * @lun_id: LUN ID of the scsi device.
9924 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9925 *
9926 * This function sends an abort command for every SCSI command
9927 * associated with the given virtual port pending on the ring
9928 * filtered by lpfc_sli_validate_fcp_iocb function.
9929 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9930 * FCP iocbs associated with lun specified by tgt_id and lun_id
9931 * parameters
9932 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9933 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9934 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9935 * FCP iocbs associated with virtual port.
9936 * This function returns number of iocbs it failed to abort.
9937 * This function is called with no locks held.
9938 **/
dea3101e 9939int
51ef4c26
JS
9940lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9941 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 9942{
51ef4c26 9943 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9944 struct lpfc_iocbq *iocbq;
9945 struct lpfc_iocbq *abtsiocb;
dea3101e 9946 IOCB_t *cmd = NULL;
dea3101e 9947 int errcnt = 0, ret_val = 0;
0bd4ca25 9948 int i;
dea3101e 9949
0bd4ca25
JSEC
9950 for (i = 1; i <= phba->sli.last_iotag; i++) {
9951 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9952
51ef4c26 9953 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 9954 abort_cmd) != 0)
dea3101e
JB
9955 continue;
9956
afbd8d88
JS
9957 /*
9958 * If the iocbq is already being aborted, don't take a second
9959 * action, but do count it.
9960 */
9961 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
9962 continue;
9963
dea3101e 9964 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 9965 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
9966 if (abtsiocb == NULL) {
9967 errcnt++;
9968 continue;
9969 }
dea3101e 9970
afbd8d88
JS
9971 /* indicate the IO is being aborted by the driver. */
9972 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
9973
0bd4ca25 9974 cmd = &iocbq->iocb;
dea3101e
JB
9975 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9976 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
9977 if (phba->sli_rev == LPFC_SLI_REV4)
9978 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9979 else
9980 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
9981 abtsiocb->iocb.ulpLe = 1;
9982 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 9983 abtsiocb->vport = vport;
dea3101e 9984
5ffc266e
JS
9985 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9986 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
9987 if (iocbq->iocb_flag & LPFC_IO_FCP)
9988 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9989
2e0fef85 9990 if (lpfc_is_link_up(phba))
dea3101e
JB
9991 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9992 else
9993 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9994
5eb95af0
JSEC
9995 /* Setup callback routine and issue the command. */
9996 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
9997 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9998 abtsiocb, 0);
dea3101e 9999 if (ret_val == IOCB_ERROR) {
604a3e30 10000 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
10001 errcnt++;
10002 continue;
10003 }
10004 }
10005
10006 return errcnt;
10007}
10008
e59058c4 10009/**
3621a710 10010 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
10011 * @phba: Pointer to HBA context object.
10012 * @cmdiocbq: Pointer to command iocb.
10013 * @rspiocbq: Pointer to response iocb.
10014 *
10015 * This function is the completion handler for iocbs issued using
10016 * lpfc_sli_issue_iocb_wait function. This function is called by the
10017 * ring event handler function without any lock held. This function
10018 * can be called from both worker thread context and interrupt
10019 * context. This function also can be called from other thread which
10020 * cleans up the SLI layer objects.
10021 * This function copy the contents of the response iocb to the
10022 * response iocb memory object provided by the caller of
10023 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10024 * sleeps for the iocb completion.
10025 **/
68876920
JSEC
10026static void
10027lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10028 struct lpfc_iocbq *cmdiocbq,
10029 struct lpfc_iocbq *rspiocbq)
dea3101e 10030{
68876920
JSEC
10031 wait_queue_head_t *pdone_q;
10032 unsigned long iflags;
0f65ff68 10033 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 10034
2e0fef85 10035 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
10036 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10037
10038 /*
10039 * A time out has occurred for the iocb. If a time out
10040 * completion handler has been supplied, call it. Otherwise,
10041 * just free the iocbq.
10042 */
10043
10044 spin_unlock_irqrestore(&phba->hbalock, iflags);
10045 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10046 cmdiocbq->wait_iocb_cmpl = NULL;
10047 if (cmdiocbq->iocb_cmpl)
10048 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10049 else
10050 lpfc_sli_release_iocbq(phba, cmdiocbq);
10051 return;
10052 }
10053
68876920
JSEC
10054 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10055 if (cmdiocbq->context2 && rspiocbq)
10056 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10057 &rspiocbq->iocb, sizeof(IOCB_t));
10058
0f65ff68
JS
10059 /* Set the exchange busy flag for task management commands */
10060 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10061 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10062 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10063 cur_iocbq);
10064 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10065 }
10066
68876920 10067 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
10068 if (pdone_q)
10069 wake_up(pdone_q);
858c9f6c 10070 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
10071 return;
10072}
10073
d11e31dd
JS
10074/**
10075 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10076 * @phba: Pointer to HBA context object..
10077 * @piocbq: Pointer to command iocb.
10078 * @flag: Flag to test.
10079 *
10080 * This routine grabs the hbalock and then test the iocb_flag to
10081 * see if the passed in flag is set.
10082 * Returns:
10083 * 1 if flag is set.
10084 * 0 if flag is not set.
10085 **/
10086static int
10087lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10088 struct lpfc_iocbq *piocbq, uint32_t flag)
10089{
10090 unsigned long iflags;
10091 int ret;
10092
10093 spin_lock_irqsave(&phba->hbalock, iflags);
10094 ret = piocbq->iocb_flag & flag;
10095 spin_unlock_irqrestore(&phba->hbalock, iflags);
10096 return ret;
10097
10098}
10099
e59058c4 10100/**
3621a710 10101 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
10102 * @phba: Pointer to HBA context object..
10103 * @pring: Pointer to sli ring.
10104 * @piocb: Pointer to command iocb.
10105 * @prspiocbq: Pointer to response iocb.
10106 * @timeout: Timeout in number of seconds.
10107 *
10108 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
10109 * iocb to complete. The iocb_cmpl field of the shall be used
10110 * to handle iocbs which time out. If the field is NULL, the
10111 * function shall free the iocbq structure. If more clean up is
10112 * needed, the caller is expected to provide a completion function
10113 * that will provide the needed clean up. If the iocb command is
10114 * not completed within timeout seconds, the function will either
10115 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10116 * completion function set in the iocb_cmpl field and then return
10117 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10118 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
10119 * The function waits for the iocb completion using an
10120 * non-interruptible wait.
10121 * This function will sleep while waiting for iocb completion.
10122 * So, this function should not be called from any context which
10123 * does not allow sleeping. Due to the same reason, this function
10124 * cannot be called with interrupt disabled.
10125 * This function assumes that the iocb completions occur while
10126 * this function sleep. So, this function cannot be called from
10127 * the thread which process iocb completion for this ring.
10128 * This function clears the iocb_flag of the iocb object before
10129 * issuing the iocb and the iocb completion handler sets this
10130 * flag and wakes this thread when the iocb completes.
10131 * The contents of the response iocb will be copied to prspiocbq
10132 * by the completion handler when the command completes.
10133 * This function returns IOCB_SUCCESS when success.
10134 * This function is called with no lock held.
10135 **/
dea3101e 10136int
2e0fef85 10137lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 10138 uint32_t ring_number,
2e0fef85
JS
10139 struct lpfc_iocbq *piocb,
10140 struct lpfc_iocbq *prspiocbq,
68876920 10141 uint32_t timeout)
dea3101e 10142{
7259f0d0 10143 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
10144 long timeleft, timeout_req = 0;
10145 int retval = IOCB_SUCCESS;
875fbdfe 10146 uint32_t creg_val;
0e9bb8d7
JS
10147 struct lpfc_iocbq *iocb;
10148 int txq_cnt = 0;
10149 int txcmplq_cnt = 0;
2a9bf3d0 10150 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5a0916b4
JS
10151 unsigned long iflags;
10152 bool iocb_completed = true;
10153
dea3101e 10154 /*
68876920
JSEC
10155 * If the caller has provided a response iocbq buffer, then context2
10156 * is NULL or its an error.
dea3101e 10157 */
68876920
JSEC
10158 if (prspiocbq) {
10159 if (piocb->context2)
10160 return IOCB_ERROR;
10161 piocb->context2 = prspiocbq;
dea3101e
JB
10162 }
10163
5a0916b4 10164 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
10165 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10166 piocb->context_un.wait_queue = &done_q;
5a0916b4 10167 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 10168
875fbdfe 10169 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
10170 if (lpfc_readl(phba->HCregaddr, &creg_val))
10171 return IOCB_ERROR;
875fbdfe
JSEC
10172 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10173 writel(creg_val, phba->HCregaddr);
10174 readl(phba->HCregaddr); /* flush */
10175 }
10176
2a9bf3d0
JS
10177 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10178 SLI_IOCB_RET_IOCB);
68876920 10179 if (retval == IOCB_SUCCESS) {
256ec0d0 10180 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 10181 timeleft = wait_event_timeout(done_q,
d11e31dd 10182 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 10183 timeout_req);
5a0916b4
JS
10184 spin_lock_irqsave(&phba->hbalock, iflags);
10185 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10186
10187 /*
10188 * IOCB timed out. Inform the wake iocb wait
10189 * completion function and set local status
10190 */
dea3101e 10191
5a0916b4
JS
10192 iocb_completed = false;
10193 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10194 }
10195 spin_unlock_irqrestore(&phba->hbalock, iflags);
10196 if (iocb_completed) {
7054a606 10197 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 10198 "0331 IOCB wake signaled\n");
7054a606 10199 } else if (timeleft == 0) {
68876920 10200 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10201 "0338 IOCB wait timeout error - no "
10202 "wake response Data x%x\n", timeout);
68876920 10203 retval = IOCB_TIMEDOUT;
7054a606 10204 } else {
68876920 10205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
10206 "0330 IOCB wake NOT set, "
10207 "Data x%x x%lx\n",
68876920
JSEC
10208 timeout, (timeleft / jiffies));
10209 retval = IOCB_TIMEDOUT;
dea3101e 10210 }
2a9bf3d0 10211 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
10212 if (phba->cfg_log_verbose & LOG_SLI) {
10213 list_for_each_entry(iocb, &pring->txq, list) {
10214 txq_cnt++;
10215 }
10216 list_for_each_entry(iocb, &pring->txcmplq, list) {
10217 txcmplq_cnt++;
10218 }
10219 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10220 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10221 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10222 }
2a9bf3d0 10223 return retval;
68876920
JSEC
10224 } else {
10225 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 10226 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 10227 retval);
68876920 10228 retval = IOCB_ERROR;
dea3101e
JB
10229 }
10230
875fbdfe 10231 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
10232 if (lpfc_readl(phba->HCregaddr, &creg_val))
10233 return IOCB_ERROR;
875fbdfe
JSEC
10234 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10235 writel(creg_val, phba->HCregaddr);
10236 readl(phba->HCregaddr); /* flush */
10237 }
10238
68876920
JSEC
10239 if (prspiocbq)
10240 piocb->context2 = NULL;
10241
10242 piocb->context_un.wait_queue = NULL;
10243 piocb->iocb_cmpl = NULL;
dea3101e
JB
10244 return retval;
10245}
68876920 10246
e59058c4 10247/**
3621a710 10248 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
10249 * @phba: Pointer to HBA context object.
10250 * @pmboxq: Pointer to driver mailbox object.
10251 * @timeout: Timeout in number of seconds.
10252 *
10253 * This function issues the mailbox to firmware and waits for the
10254 * mailbox command to complete. If the mailbox command is not
10255 * completed within timeout seconds, it returns MBX_TIMEOUT.
10256 * The function waits for the mailbox completion using an
10257 * interruptible wait. If the thread is woken up due to a
10258 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10259 * should not free the mailbox resources, if this function returns
10260 * MBX_TIMEOUT.
10261 * This function will sleep while waiting for mailbox completion.
10262 * So, this function should not be called from any context which
10263 * does not allow sleeping. Due to the same reason, this function
10264 * cannot be called with interrupt disabled.
10265 * This function assumes that the mailbox completion occurs while
10266 * this function sleep. So, this function cannot be called from
10267 * the worker thread which processes mailbox completion.
10268 * This function is called in the context of HBA management
10269 * applications.
10270 * This function returns MBX_SUCCESS when successful.
10271 * This function is called with no lock held.
10272 **/
dea3101e 10273int
2e0fef85 10274lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
10275 uint32_t timeout)
10276{
7259f0d0 10277 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 10278 MAILBOX_t *mb = NULL;
dea3101e 10279 int retval;
858c9f6c 10280 unsigned long flag;
dea3101e 10281
b230b8a2 10282 /* The caller might set context1 for extended buffer */
98c9ea5c 10283 if (pmboxq->context1)
b230b8a2 10284 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 10285
495a714c 10286 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
10287 /* setup wake call as IOCB callback */
10288 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10289 /* setup context field to pass wait_queue pointer to wake function */
10290 pmboxq->context1 = &done_q;
10291
dea3101e
JB
10292 /* now issue the command */
10293 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 10294 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
10295 wait_event_interruptible_timeout(done_q,
10296 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 10297 msecs_to_jiffies(timeout * 1000));
7054a606 10298
858c9f6c 10299 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
10300 /* restore the possible extended buffer for free resource */
10301 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
10302 /*
10303 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10304 * else do not free the resources.
10305 */
d7c47992 10306 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 10307 retval = MBX_SUCCESS;
d7c47992 10308 } else {
7054a606 10309 retval = MBX_TIMEOUT;
858c9f6c
JS
10310 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10311 }
10312 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
10313 } else {
10314 /* restore the possible extended buffer for free resource */
10315 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
10316 }
10317
dea3101e
JB
10318 return retval;
10319}
10320
e59058c4 10321/**
3772a991 10322 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
10323 * @phba: Pointer to HBA context.
10324 *
3772a991
JS
10325 * This function is called to shutdown the driver's mailbox sub-system.
10326 * It first marks the mailbox sub-system is in a block state to prevent
10327 * the asynchronous mailbox command from issued off the pending mailbox
10328 * command queue. If the mailbox command sub-system shutdown is due to
10329 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10330 * the mailbox sub-system flush routine to forcefully bring down the
10331 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10332 * as with offline or HBA function reset), this routine will wait for the
10333 * outstanding mailbox command to complete before invoking the mailbox
10334 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 10335 **/
3772a991 10336void
618a5230 10337lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 10338{
3772a991 10339 struct lpfc_sli *psli = &phba->sli;
3772a991 10340 unsigned long timeout;
b4c02652 10341
618a5230
JS
10342 if (mbx_action == LPFC_MBX_NO_WAIT) {
10343 /* delay 100ms for port state */
10344 msleep(100);
10345 lpfc_sli_mbox_sys_flush(phba);
10346 return;
10347 }
a183a15f 10348 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 10349
3772a991
JS
10350 spin_lock_irq(&phba->hbalock);
10351 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 10352
3772a991 10353 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
10354 /* Determine how long we might wait for the active mailbox
10355 * command to be gracefully completed by firmware.
10356 */
a183a15f
JS
10357 if (phba->sli.mbox_active)
10358 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10359 phba->sli.mbox_active) *
10360 1000) + jiffies;
10361 spin_unlock_irq(&phba->hbalock);
10362
3772a991
JS
10363 while (phba->sli.mbox_active) {
10364 /* Check active mailbox complete status every 2ms */
10365 msleep(2);
10366 if (time_after(jiffies, timeout))
10367 /* Timeout, let the mailbox flush routine to
10368 * forcefully release active mailbox command
10369 */
10370 break;
10371 }
d7069f09
JS
10372 } else
10373 spin_unlock_irq(&phba->hbalock);
10374
3772a991
JS
10375 lpfc_sli_mbox_sys_flush(phba);
10376}
ed957684 10377
3772a991
JS
10378/**
10379 * lpfc_sli_eratt_read - read sli-3 error attention events
10380 * @phba: Pointer to HBA context.
10381 *
10382 * This function is called to read the SLI3 device error attention registers
10383 * for possible error attention events. The caller must hold the hostlock
10384 * with spin_lock_irq().
10385 *
25985edc 10386 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
10387 * Register and returns 0 otherwise.
10388 **/
10389static int
10390lpfc_sli_eratt_read(struct lpfc_hba *phba)
10391{
10392 uint32_t ha_copy;
b4c02652 10393
3772a991 10394 /* Read chip Host Attention (HA) register */
9940b97b
JS
10395 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10396 goto unplug_err;
10397
3772a991
JS
10398 if (ha_copy & HA_ERATT) {
10399 /* Read host status register to retrieve error event */
9940b97b
JS
10400 if (lpfc_sli_read_hs(phba))
10401 goto unplug_err;
b4c02652 10402
3772a991
JS
10403 /* Check if there is a deferred error condition is active */
10404 if ((HS_FFER1 & phba->work_hs) &&
10405 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 10406 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 10407 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
10408 /* Clear all interrupt enable conditions */
10409 writel(0, phba->HCregaddr);
10410 readl(phba->HCregaddr);
10411 }
10412
10413 /* Set the driver HA work bitmap */
3772a991
JS
10414 phba->work_ha |= HA_ERATT;
10415 /* Indicate polling handles this ERATT */
10416 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
10417 return 1;
10418 }
10419 return 0;
9940b97b
JS
10420
10421unplug_err:
10422 /* Set the driver HS work bitmap */
10423 phba->work_hs |= UNPLUG_ERR;
10424 /* Set the driver HA work bitmap */
10425 phba->work_ha |= HA_ERATT;
10426 /* Indicate polling handles this ERATT */
10427 phba->hba_flag |= HBA_ERATT_HANDLED;
10428 return 1;
b4c02652
JS
10429}
10430
da0436e9
JS
10431/**
10432 * lpfc_sli4_eratt_read - read sli-4 error attention events
10433 * @phba: Pointer to HBA context.
10434 *
10435 * This function is called to read the SLI4 device error attention registers
10436 * for possible error attention events. The caller must hold the hostlock
10437 * with spin_lock_irq().
10438 *
25985edc 10439 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
10440 * Register and returns 0 otherwise.
10441 **/
10442static int
10443lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10444{
10445 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
10446 uint32_t if_type, portsmphr;
10447 struct lpfc_register portstat_reg;
da0436e9 10448
2fcee4bf
JS
10449 /*
10450 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
10451 * registers for error attention. This can be changed later.
10452 */
2fcee4bf
JS
10453 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10454 switch (if_type) {
10455 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
10456 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10457 &uerr_sta_lo) ||
10458 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10459 &uerr_sta_hi)) {
10460 phba->work_hs |= UNPLUG_ERR;
10461 phba->work_ha |= HA_ERATT;
10462 phba->hba_flag |= HBA_ERATT_HANDLED;
10463 return 1;
10464 }
2fcee4bf
JS
10465 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10466 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10468 "1423 HBA Unrecoverable error: "
10469 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10470 "ue_mask_lo_reg=0x%x, "
10471 "ue_mask_hi_reg=0x%x\n",
10472 uerr_sta_lo, uerr_sta_hi,
10473 phba->sli4_hba.ue_mask_lo,
10474 phba->sli4_hba.ue_mask_hi);
10475 phba->work_status[0] = uerr_sta_lo;
10476 phba->work_status[1] = uerr_sta_hi;
10477 phba->work_ha |= HA_ERATT;
10478 phba->hba_flag |= HBA_ERATT_HANDLED;
10479 return 1;
10480 }
10481 break;
10482 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
10483 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10484 &portstat_reg.word0) ||
10485 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10486 &portsmphr)){
10487 phba->work_hs |= UNPLUG_ERR;
10488 phba->work_ha |= HA_ERATT;
10489 phba->hba_flag |= HBA_ERATT_HANDLED;
10490 return 1;
10491 }
2fcee4bf
JS
10492 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10493 phba->work_status[0] =
10494 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10495 phba->work_status[1] =
10496 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 10498 "2885 Port Status Event: "
2fcee4bf
JS
10499 "port status reg 0x%x, "
10500 "port smphr reg 0x%x, "
10501 "error 1=0x%x, error 2=0x%x\n",
10502 portstat_reg.word0,
10503 portsmphr,
10504 phba->work_status[0],
10505 phba->work_status[1]);
10506 phba->work_ha |= HA_ERATT;
10507 phba->hba_flag |= HBA_ERATT_HANDLED;
10508 return 1;
10509 }
10510 break;
10511 case LPFC_SLI_INTF_IF_TYPE_1:
10512 default:
a747c9ce 10513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
10514 "2886 HBA Error Attention on unsupported "
10515 "if type %d.", if_type);
a747c9ce 10516 return 1;
da0436e9 10517 }
2fcee4bf 10518
da0436e9
JS
10519 return 0;
10520}
10521
e59058c4 10522/**
3621a710 10523 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
10524 * @phba: Pointer to HBA context.
10525 *
3772a991 10526 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
10527 * error attention register bit for error attention events.
10528 *
25985edc 10529 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
10530 * Register and returns 0 otherwise.
10531 **/
10532int
10533lpfc_sli_check_eratt(struct lpfc_hba *phba)
10534{
10535 uint32_t ha_copy;
10536
10537 /* If somebody is waiting to handle an eratt, don't process it
10538 * here. The brdkill function will do this.
10539 */
10540 if (phba->link_flag & LS_IGNORE_ERATT)
10541 return 0;
10542
10543 /* Check if interrupt handler handles this ERATT */
10544 spin_lock_irq(&phba->hbalock);
10545 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10546 /* Interrupt handler has handled ERATT */
10547 spin_unlock_irq(&phba->hbalock);
10548 return 0;
10549 }
10550
a257bf90
JS
10551 /*
10552 * If there is deferred error attention, do not check for error
10553 * attention
10554 */
10555 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10556 spin_unlock_irq(&phba->hbalock);
10557 return 0;
10558 }
10559
3772a991
JS
10560 /* If PCI channel is offline, don't process it */
10561 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 10562 spin_unlock_irq(&phba->hbalock);
3772a991
JS
10563 return 0;
10564 }
10565
10566 switch (phba->sli_rev) {
10567 case LPFC_SLI_REV2:
10568 case LPFC_SLI_REV3:
10569 /* Read chip Host Attention (HA) register */
10570 ha_copy = lpfc_sli_eratt_read(phba);
10571 break;
da0436e9 10572 case LPFC_SLI_REV4:
2fcee4bf 10573 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
10574 ha_copy = lpfc_sli4_eratt_read(phba);
10575 break;
3772a991
JS
10576 default:
10577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10578 "0299 Invalid SLI revision (%d)\n",
10579 phba->sli_rev);
10580 ha_copy = 0;
10581 break;
9399627f
JS
10582 }
10583 spin_unlock_irq(&phba->hbalock);
3772a991
JS
10584
10585 return ha_copy;
10586}
10587
10588/**
10589 * lpfc_intr_state_check - Check device state for interrupt handling
10590 * @phba: Pointer to HBA context.
10591 *
10592 * This inline routine checks whether a device or its PCI slot is in a state
10593 * that the interrupt should be handled.
10594 *
10595 * This function returns 0 if the device or the PCI slot is in a state that
10596 * interrupt should be handled, otherwise -EIO.
10597 */
10598static inline int
10599lpfc_intr_state_check(struct lpfc_hba *phba)
10600{
10601 /* If the pci channel is offline, ignore all the interrupts */
10602 if (unlikely(pci_channel_offline(phba->pcidev)))
10603 return -EIO;
10604
10605 /* Update device level interrupt statistics */
10606 phba->sli.slistat.sli_intr++;
10607
10608 /* Ignore all interrupts during initialization. */
10609 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10610 return -EIO;
10611
9399627f
JS
10612 return 0;
10613}
10614
10615/**
3772a991 10616 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
10617 * @irq: Interrupt number.
10618 * @dev_id: The device context pointer.
10619 *
9399627f 10620 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10621 * service routine when device with SLI-3 interface spec is enabled with
10622 * MSI-X multi-message interrupt mode and there are slow-path events in
10623 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10624 * interrupt mode, this function is called as part of the device-level
10625 * interrupt handler. When the PCI slot is in error recovery or the HBA
10626 * is undergoing initialization, the interrupt handler will not process
10627 * the interrupt. The link attention and ELS ring attention events are
10628 * handled by the worker thread. The interrupt handler signals the worker
10629 * thread and returns for these events. This function is called without
10630 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
10631 * structures.
10632 *
10633 * This function returns IRQ_HANDLED when interrupt is handled else it
10634 * returns IRQ_NONE.
e59058c4 10635 **/
dea3101e 10636irqreturn_t
3772a991 10637lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 10638{
2e0fef85 10639 struct lpfc_hba *phba;
a747c9ce 10640 uint32_t ha_copy, hc_copy;
dea3101e
JB
10641 uint32_t work_ha_copy;
10642 unsigned long status;
5b75da2f 10643 unsigned long iflag;
dea3101e
JB
10644 uint32_t control;
10645
92d7f7b0 10646 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
10647 struct lpfc_vport *vport;
10648 struct lpfc_nodelist *ndlp;
10649 struct lpfc_dmabuf *mp;
92d7f7b0
JS
10650 LPFC_MBOXQ_t *pmb;
10651 int rc;
10652
dea3101e
JB
10653 /*
10654 * Get the driver's phba structure from the dev_id and
10655 * assume the HBA is not interrupting.
10656 */
9399627f 10657 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
10658
10659 if (unlikely(!phba))
10660 return IRQ_NONE;
10661
dea3101e 10662 /*
9399627f
JS
10663 * Stuff needs to be attented to when this function is invoked as an
10664 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 10665 */
9399627f 10666 if (phba->intr_type == MSIX) {
3772a991
JS
10667 /* Check device state for handling interrupt */
10668 if (lpfc_intr_state_check(phba))
9399627f
JS
10669 return IRQ_NONE;
10670 /* Need to read HA REG for slow-path events */
5b75da2f 10671 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
10672 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10673 goto unplug_error;
9399627f
JS
10674 /* If somebody is waiting to handle an eratt don't process it
10675 * here. The brdkill function will do this.
10676 */
10677 if (phba->link_flag & LS_IGNORE_ERATT)
10678 ha_copy &= ~HA_ERATT;
10679 /* Check the need for handling ERATT in interrupt handler */
10680 if (ha_copy & HA_ERATT) {
10681 if (phba->hba_flag & HBA_ERATT_HANDLED)
10682 /* ERATT polling has handled ERATT */
10683 ha_copy &= ~HA_ERATT;
10684 else
10685 /* Indicate interrupt handler handles ERATT */
10686 phba->hba_flag |= HBA_ERATT_HANDLED;
10687 }
a257bf90
JS
10688
10689 /*
10690 * If there is deferred error attention, do not check for any
10691 * interrupt.
10692 */
10693 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10694 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10695 return IRQ_NONE;
10696 }
10697
9399627f 10698 /* Clear up only attention source related to slow-path */
9940b97b
JS
10699 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10700 goto unplug_error;
10701
a747c9ce
JS
10702 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10703 HC_LAINT_ENA | HC_ERINT_ENA),
10704 phba->HCregaddr);
9399627f
JS
10705 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10706 phba->HAregaddr);
a747c9ce 10707 writel(hc_copy, phba->HCregaddr);
9399627f 10708 readl(phba->HAregaddr); /* flush */
5b75da2f 10709 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10710 } else
10711 ha_copy = phba->ha_copy;
dea3101e 10712
dea3101e
JB
10713 work_ha_copy = ha_copy & phba->work_ha_mask;
10714
9399627f 10715 if (work_ha_copy) {
dea3101e
JB
10716 if (work_ha_copy & HA_LATT) {
10717 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10718 /*
10719 * Turn off Link Attention interrupts
10720 * until CLEAR_LA done
10721 */
5b75da2f 10722 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10723 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
10724 if (lpfc_readl(phba->HCregaddr, &control))
10725 goto unplug_error;
dea3101e
JB
10726 control &= ~HC_LAINT_ENA;
10727 writel(control, phba->HCregaddr);
10728 readl(phba->HCregaddr); /* flush */
5b75da2f 10729 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
10730 }
10731 else
10732 work_ha_copy &= ~HA_LATT;
10733 }
10734
9399627f 10735 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
10736 /*
10737 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10738 * the only slow ring.
10739 */
10740 status = (work_ha_copy &
10741 (HA_RXMASK << (4*LPFC_ELS_RING)));
10742 status >>= (4*LPFC_ELS_RING);
10743 if (status & HA_RXMASK) {
5b75da2f 10744 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
10745 if (lpfc_readl(phba->HCregaddr, &control))
10746 goto unplug_error;
a58cbd52
JS
10747
10748 lpfc_debugfs_slow_ring_trc(phba,
10749 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10750 control, status,
10751 (uint32_t)phba->sli.slistat.sli_intr);
10752
858c9f6c 10753 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
10754 lpfc_debugfs_slow_ring_trc(phba,
10755 "ISR Disable ring:"
10756 "pwork:x%x hawork:x%x wait:x%x",
10757 phba->work_ha, work_ha_copy,
10758 (uint32_t)((unsigned long)
5e9d9b82 10759 &phba->work_waitq));
a58cbd52 10760
858c9f6c
JS
10761 control &=
10762 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
10763 writel(control, phba->HCregaddr);
10764 readl(phba->HCregaddr); /* flush */
dea3101e 10765 }
a58cbd52
JS
10766 else {
10767 lpfc_debugfs_slow_ring_trc(phba,
10768 "ISR slow ring: pwork:"
10769 "x%x hawork:x%x wait:x%x",
10770 phba->work_ha, work_ha_copy,
10771 (uint32_t)((unsigned long)
5e9d9b82 10772 &phba->work_waitq));
a58cbd52 10773 }
5b75da2f 10774 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
10775 }
10776 }
5b75da2f 10777 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 10778 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
10779 if (lpfc_sli_read_hs(phba))
10780 goto unplug_error;
a257bf90
JS
10781 /*
10782 * Check if there is a deferred error condition
10783 * is active
10784 */
10785 if ((HS_FFER1 & phba->work_hs) &&
10786 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
10787 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10788 phba->work_hs)) {
a257bf90
JS
10789 phba->hba_flag |= DEFER_ERATT;
10790 /* Clear all interrupt enable conditions */
10791 writel(0, phba->HCregaddr);
10792 readl(phba->HCregaddr);
10793 }
10794 }
10795
9399627f 10796 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 10797 pmb = phba->sli.mbox_active;
04c68496 10798 pmbox = &pmb->u.mb;
34b02dcd 10799 mbox = phba->mbox;
858c9f6c 10800 vport = pmb->vport;
92d7f7b0
JS
10801
10802 /* First check out the status word */
10803 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10804 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 10805 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
10806 /*
10807 * Stray Mailbox Interrupt, mbxCommand <cmd>
10808 * mbxStatus <status>
10809 */
09372820 10810 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 10811 LOG_SLI,
e8b62011 10812 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
10813 "Interrupt mbxCommand x%x "
10814 "mbxStatus x%x\n",
e8b62011 10815 (vport ? vport->vpi : 0),
92d7f7b0
JS
10816 pmbox->mbxCommand,
10817 pmbox->mbxStatus);
09372820
JS
10818 /* clear mailbox attention bit */
10819 work_ha_copy &= ~HA_MBATT;
10820 } else {
97eab634 10821 phba->sli.mbox_active = NULL;
5b75da2f 10822 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
10823 phba->last_completion_time = jiffies;
10824 del_timer(&phba->sli.mbox_tmo);
09372820
JS
10825 if (pmb->mbox_cmpl) {
10826 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10827 MAILBOX_CMD_SIZE);
7a470277
JS
10828 if (pmb->out_ext_byte_len &&
10829 pmb->context2)
10830 lpfc_sli_pcimem_bcopy(
10831 phba->mbox_ext,
10832 pmb->context2,
10833 pmb->out_ext_byte_len);
09372820
JS
10834 }
10835 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10836 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10837
10838 lpfc_debugfs_disc_trc(vport,
10839 LPFC_DISC_TRC_MBOX_VPORT,
10840 "MBOX dflt rpi: : "
10841 "status:x%x rpi:x%x",
10842 (uint32_t)pmbox->mbxStatus,
10843 pmbox->un.varWords[0], 0);
10844
10845 if (!pmbox->mbxStatus) {
10846 mp = (struct lpfc_dmabuf *)
10847 (pmb->context1);
10848 ndlp = (struct lpfc_nodelist *)
10849 pmb->context2;
10850
10851 /* Reg_LOGIN of dflt RPI was
10852 * successful. new lets get
10853 * rid of the RPI using the
10854 * same mbox buffer.
10855 */
10856 lpfc_unreg_login(phba,
10857 vport->vpi,
10858 pmbox->un.varWords[0],
10859 pmb);
10860 pmb->mbox_cmpl =
10861 lpfc_mbx_cmpl_dflt_rpi;
10862 pmb->context1 = mp;
10863 pmb->context2 = ndlp;
10864 pmb->vport = vport;
58da1ffb
JS
10865 rc = lpfc_sli_issue_mbox(phba,
10866 pmb,
10867 MBX_NOWAIT);
10868 if (rc != MBX_BUSY)
10869 lpfc_printf_log(phba,
10870 KERN_ERR,
10871 LOG_MBOX | LOG_SLI,
d7c255b2 10872 "0350 rc should have"
6a9c52cf 10873 "been MBX_BUSY\n");
3772a991
JS
10874 if (rc != MBX_NOT_FINISHED)
10875 goto send_current_mbox;
09372820 10876 }
858c9f6c 10877 }
5b75da2f
JS
10878 spin_lock_irqsave(
10879 &phba->pport->work_port_lock,
10880 iflag);
09372820
JS
10881 phba->pport->work_port_events &=
10882 ~WORKER_MBOX_TMO;
5b75da2f
JS
10883 spin_unlock_irqrestore(
10884 &phba->pport->work_port_lock,
10885 iflag);
09372820 10886 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 10887 }
97eab634 10888 } else
5b75da2f 10889 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 10890
92d7f7b0
JS
10891 if ((work_ha_copy & HA_MBATT) &&
10892 (phba->sli.mbox_active == NULL)) {
858c9f6c 10893send_current_mbox:
92d7f7b0 10894 /* Process next mailbox command if there is one */
58da1ffb
JS
10895 do {
10896 rc = lpfc_sli_issue_mbox(phba, NULL,
10897 MBX_NOWAIT);
10898 } while (rc == MBX_NOT_FINISHED);
10899 if (rc != MBX_SUCCESS)
10900 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10901 LOG_SLI, "0349 rc should be "
6a9c52cf 10902 "MBX_SUCCESS\n");
92d7f7b0
JS
10903 }
10904
5b75da2f 10905 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10906 phba->work_ha |= work_ha_copy;
5b75da2f 10907 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 10908 lpfc_worker_wake_up(phba);
dea3101e 10909 }
9399627f 10910 return IRQ_HANDLED;
9940b97b
JS
10911unplug_error:
10912 spin_unlock_irqrestore(&phba->hbalock, iflag);
10913 return IRQ_HANDLED;
dea3101e 10914
3772a991 10915} /* lpfc_sli_sp_intr_handler */
9399627f
JS
10916
10917/**
3772a991 10918 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
10919 * @irq: Interrupt number.
10920 * @dev_id: The device context pointer.
10921 *
10922 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10923 * service routine when device with SLI-3 interface spec is enabled with
10924 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10925 * ring event in the HBA. However, when the device is enabled with either
10926 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10927 * device-level interrupt handler. When the PCI slot is in error recovery
10928 * or the HBA is undergoing initialization, the interrupt handler will not
10929 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10930 * the intrrupt context. This function is called without any lock held.
10931 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
10932 *
10933 * This function returns IRQ_HANDLED when interrupt is handled else it
10934 * returns IRQ_NONE.
10935 **/
10936irqreturn_t
3772a991 10937lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
10938{
10939 struct lpfc_hba *phba;
10940 uint32_t ha_copy;
10941 unsigned long status;
5b75da2f 10942 unsigned long iflag;
9399627f
JS
10943
10944 /* Get the driver's phba structure from the dev_id and
10945 * assume the HBA is not interrupting.
10946 */
10947 phba = (struct lpfc_hba *) dev_id;
10948
10949 if (unlikely(!phba))
10950 return IRQ_NONE;
10951
10952 /*
10953 * Stuff needs to be attented to when this function is invoked as an
10954 * individual interrupt handler in MSI-X multi-message interrupt mode
10955 */
10956 if (phba->intr_type == MSIX) {
3772a991
JS
10957 /* Check device state for handling interrupt */
10958 if (lpfc_intr_state_check(phba))
9399627f
JS
10959 return IRQ_NONE;
10960 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
10961 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10962 return IRQ_HANDLED;
9399627f 10963 /* Clear up only attention source related to fast-path */
5b75da2f 10964 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
10965 /*
10966 * If there is deferred error attention, do not check for
10967 * any interrupt.
10968 */
10969 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10970 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10971 return IRQ_NONE;
10972 }
9399627f
JS
10973 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10974 phba->HAregaddr);
10975 readl(phba->HAregaddr); /* flush */
5b75da2f 10976 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10977 } else
10978 ha_copy = phba->ha_copy;
dea3101e
JB
10979
10980 /*
9399627f 10981 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 10982 */
9399627f
JS
10983 ha_copy &= ~(phba->work_ha_mask);
10984
10985 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 10986 status >>= (4*LPFC_FCP_RING);
858c9f6c 10987 if (status & HA_RXMASK)
dea3101e
JB
10988 lpfc_sli_handle_fast_ring_event(phba,
10989 &phba->sli.ring[LPFC_FCP_RING],
10990 status);
a4bc3379
JS
10991
10992 if (phba->cfg_multi_ring_support == 2) {
10993 /*
9399627f
JS
10994 * Process all events on extra ring. Take the optimized path
10995 * for extra ring IO.
a4bc3379 10996 */
9399627f 10997 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 10998 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 10999 if (status & HA_RXMASK) {
a4bc3379
JS
11000 lpfc_sli_handle_fast_ring_event(phba,
11001 &phba->sli.ring[LPFC_EXTRA_RING],
11002 status);
11003 }
11004 }
dea3101e 11005 return IRQ_HANDLED;
3772a991 11006} /* lpfc_sli_fp_intr_handler */
9399627f
JS
11007
11008/**
3772a991 11009 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
11010 * @irq: Interrupt number.
11011 * @dev_id: The device context pointer.
11012 *
3772a991
JS
11013 * This function is the HBA device-level interrupt handler to device with
11014 * SLI-3 interface spec, called from the PCI layer when either MSI or
11015 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11016 * requires driver attention. This function invokes the slow-path interrupt
11017 * attention handling function and fast-path interrupt attention handling
11018 * function in turn to process the relevant HBA attention events. This
11019 * function is called without any lock held. It gets the hbalock to access
11020 * and update SLI data structures.
9399627f
JS
11021 *
11022 * This function returns IRQ_HANDLED when interrupt is handled, else it
11023 * returns IRQ_NONE.
11024 **/
11025irqreturn_t
3772a991 11026lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
11027{
11028 struct lpfc_hba *phba;
11029 irqreturn_t sp_irq_rc, fp_irq_rc;
11030 unsigned long status1, status2;
a747c9ce 11031 uint32_t hc_copy;
9399627f
JS
11032
11033 /*
11034 * Get the driver's phba structure from the dev_id and
11035 * assume the HBA is not interrupting.
11036 */
11037 phba = (struct lpfc_hba *) dev_id;
11038
11039 if (unlikely(!phba))
11040 return IRQ_NONE;
11041
3772a991
JS
11042 /* Check device state for handling interrupt */
11043 if (lpfc_intr_state_check(phba))
9399627f
JS
11044 return IRQ_NONE;
11045
11046 spin_lock(&phba->hbalock);
9940b97b
JS
11047 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11048 spin_unlock(&phba->hbalock);
11049 return IRQ_HANDLED;
11050 }
11051
9399627f
JS
11052 if (unlikely(!phba->ha_copy)) {
11053 spin_unlock(&phba->hbalock);
11054 return IRQ_NONE;
11055 } else if (phba->ha_copy & HA_ERATT) {
11056 if (phba->hba_flag & HBA_ERATT_HANDLED)
11057 /* ERATT polling has handled ERATT */
11058 phba->ha_copy &= ~HA_ERATT;
11059 else
11060 /* Indicate interrupt handler handles ERATT */
11061 phba->hba_flag |= HBA_ERATT_HANDLED;
11062 }
11063
a257bf90
JS
11064 /*
11065 * If there is deferred error attention, do not check for any interrupt.
11066 */
11067 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 11068 spin_unlock(&phba->hbalock);
a257bf90
JS
11069 return IRQ_NONE;
11070 }
11071
9399627f 11072 /* Clear attention sources except link and error attentions */
9940b97b
JS
11073 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11074 spin_unlock(&phba->hbalock);
11075 return IRQ_HANDLED;
11076 }
a747c9ce
JS
11077 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11078 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11079 phba->HCregaddr);
9399627f 11080 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 11081 writel(hc_copy, phba->HCregaddr);
9399627f
JS
11082 readl(phba->HAregaddr); /* flush */
11083 spin_unlock(&phba->hbalock);
11084
11085 /*
11086 * Invokes slow-path host attention interrupt handling as appropriate.
11087 */
11088
11089 /* status of events with mailbox and link attention */
11090 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11091
11092 /* status of events with ELS ring */
11093 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11094 status2 >>= (4*LPFC_ELS_RING);
11095
11096 if (status1 || (status2 & HA_RXMASK))
3772a991 11097 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
11098 else
11099 sp_irq_rc = IRQ_NONE;
11100
11101 /*
11102 * Invoke fast-path host attention interrupt handling as appropriate.
11103 */
11104
11105 /* status of events with FCP ring */
11106 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11107 status1 >>= (4*LPFC_FCP_RING);
11108
11109 /* status of events with extra ring */
11110 if (phba->cfg_multi_ring_support == 2) {
11111 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11112 status2 >>= (4*LPFC_EXTRA_RING);
11113 } else
11114 status2 = 0;
11115
11116 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 11117 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
11118 else
11119 fp_irq_rc = IRQ_NONE;
dea3101e 11120
9399627f
JS
11121 /* Return device-level interrupt handling status */
11122 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 11123} /* lpfc_sli_intr_handler */
4f774513
JS
11124
11125/**
11126 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11127 * @phba: pointer to lpfc hba data structure.
11128 *
11129 * This routine is invoked by the worker thread to process all the pending
11130 * SLI4 FCP abort XRI events.
11131 **/
11132void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11133{
11134 struct lpfc_cq_event *cq_event;
11135
11136 /* First, declare the fcp xri abort event has been handled */
11137 spin_lock_irq(&phba->hbalock);
11138 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11139 spin_unlock_irq(&phba->hbalock);
11140 /* Now, handle all the fcp xri abort events */
11141 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11142 /* Get the first event from the head of the event queue */
11143 spin_lock_irq(&phba->hbalock);
11144 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11145 cq_event, struct lpfc_cq_event, list);
11146 spin_unlock_irq(&phba->hbalock);
11147 /* Notify aborted XRI for FCP work queue */
11148 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11149 /* Free the event processed back to the free pool */
11150 lpfc_sli4_cq_event_release(phba, cq_event);
11151 }
11152}
11153
11154/**
11155 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11156 * @phba: pointer to lpfc hba data structure.
11157 *
11158 * This routine is invoked by the worker thread to process all the pending
11159 * SLI4 els abort xri events.
11160 **/
11161void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11162{
11163 struct lpfc_cq_event *cq_event;
11164
11165 /* First, declare the els xri abort event has been handled */
11166 spin_lock_irq(&phba->hbalock);
11167 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11168 spin_unlock_irq(&phba->hbalock);
11169 /* Now, handle all the els xri abort events */
11170 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11171 /* Get the first event from the head of the event queue */
11172 spin_lock_irq(&phba->hbalock);
11173 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11174 cq_event, struct lpfc_cq_event, list);
11175 spin_unlock_irq(&phba->hbalock);
11176 /* Notify aborted XRI for ELS work queue */
11177 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11178 /* Free the event processed back to the free pool */
11179 lpfc_sli4_cq_event_release(phba, cq_event);
11180 }
11181}
11182
341af102
JS
11183/**
11184 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11185 * @phba: pointer to lpfc hba data structure
11186 * @pIocbIn: pointer to the rspiocbq
11187 * @pIocbOut: pointer to the cmdiocbq
11188 * @wcqe: pointer to the complete wcqe
11189 *
11190 * This routine transfers the fields of a command iocbq to a response iocbq
11191 * by copying all the IOCB fields from command iocbq and transferring the
11192 * completion status information from the complete wcqe.
11193 **/
4f774513 11194static void
341af102
JS
11195lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11196 struct lpfc_iocbq *pIocbIn,
4f774513
JS
11197 struct lpfc_iocbq *pIocbOut,
11198 struct lpfc_wcqe_complete *wcqe)
11199{
af22741c 11200 int numBdes, i;
341af102 11201 unsigned long iflags;
af22741c
JS
11202 uint32_t status, max_response;
11203 struct lpfc_dmabuf *dmabuf;
11204 struct ulp_bde64 *bpl, bde;
4f774513
JS
11205 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11206
11207 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11208 sizeof(struct lpfc_iocbq) - offset);
4f774513 11209 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
11210 status = bf_get(lpfc_wcqe_c_status, wcqe);
11211 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
11212 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11213 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11214 pIocbIn->iocb.un.fcpi.fcpi_parm =
11215 pIocbOut->iocb.un.fcpi.fcpi_parm -
11216 wcqe->total_data_placed;
11217 else
11218 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 11219 else {
4f774513 11220 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
11221 switch (pIocbOut->iocb.ulpCommand) {
11222 case CMD_ELS_REQUEST64_CR:
11223 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11224 bpl = (struct ulp_bde64 *)dmabuf->virt;
11225 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11226 max_response = bde.tus.f.bdeSize;
11227 break;
11228 case CMD_GEN_REQUEST64_CR:
11229 max_response = 0;
11230 if (!pIocbOut->context3)
11231 break;
11232 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11233 sizeof(struct ulp_bde64);
11234 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11235 bpl = (struct ulp_bde64 *)dmabuf->virt;
11236 for (i = 0; i < numBdes; i++) {
11237 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11238 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11239 max_response += bde.tus.f.bdeSize;
11240 }
11241 break;
11242 default:
11243 max_response = wcqe->total_data_placed;
11244 break;
11245 }
11246 if (max_response < wcqe->total_data_placed)
11247 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11248 else
11249 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11250 wcqe->total_data_placed;
695a814e 11251 }
341af102 11252
acd6859b
JS
11253 /* Convert BG errors for completion status */
11254 if (status == CQE_STATUS_DI_ERROR) {
11255 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11256
11257 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11258 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11259 else
11260 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11261
11262 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11263 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11264 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11265 BGS_GUARD_ERR_MASK;
11266 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11267 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11268 BGS_APPTAG_ERR_MASK;
11269 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11270 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11271 BGS_REFTAG_ERR_MASK;
11272
11273 /* Check to see if there was any good data before the error */
11274 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11275 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11276 BGS_HI_WATER_MARK_PRESENT_MASK;
11277 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11278 wcqe->total_data_placed;
11279 }
11280
11281 /*
11282 * Set ALL the error bits to indicate we don't know what
11283 * type of error it is.
11284 */
11285 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11286 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11287 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11288 BGS_GUARD_ERR_MASK);
11289 }
11290
341af102
JS
11291 /* Pick up HBA exchange busy condition */
11292 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11293 spin_lock_irqsave(&phba->hbalock, iflags);
11294 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11295 spin_unlock_irqrestore(&phba->hbalock, iflags);
11296 }
4f774513
JS
11297}
11298
45ed1190
JS
11299/**
11300 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11301 * @phba: Pointer to HBA context object.
11302 * @wcqe: Pointer to work-queue completion queue entry.
11303 *
11304 * This routine handles an ELS work-queue completion event and construct
11305 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11306 * discovery engine to handle.
11307 *
11308 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11309 **/
11310static struct lpfc_iocbq *
11311lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11312 struct lpfc_iocbq *irspiocbq)
11313{
11314 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11315 struct lpfc_iocbq *cmdiocbq;
11316 struct lpfc_wcqe_complete *wcqe;
11317 unsigned long iflags;
11318
11319 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 11320 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
11321 pring->stats.iocb_event++;
11322 /* Look up the ELS command IOCB and create pseudo response IOCB */
11323 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11324 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 11325 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
11326
11327 if (unlikely(!cmdiocbq)) {
11328 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11329 "0386 ELS complete with no corresponding "
11330 "cmdiocb: iotag (%d)\n",
11331 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11332 lpfc_sli_release_iocbq(phba, irspiocbq);
11333 return NULL;
11334 }
11335
11336 /* Fake the irspiocbq and copy necessary response information */
341af102 11337 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
11338
11339 return irspiocbq;
11340}
11341
04c68496
JS
11342/**
11343 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11344 * @phba: Pointer to HBA context object.
11345 * @cqe: Pointer to mailbox completion queue entry.
11346 *
11347 * This routine process a mailbox completion queue entry with asynchrous
11348 * event.
11349 *
11350 * Return: true if work posted to worker thread, otherwise false.
11351 **/
11352static bool
11353lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11354{
11355 struct lpfc_cq_event *cq_event;
11356 unsigned long iflags;
11357
11358 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11359 "0392 Async Event: word0:x%x, word1:x%x, "
11360 "word2:x%x, word3:x%x\n", mcqe->word0,
11361 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11362
11363 /* Allocate a new internal CQ_EVENT entry */
11364 cq_event = lpfc_sli4_cq_event_alloc(phba);
11365 if (!cq_event) {
11366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11367 "0394 Failed to allocate CQ_EVENT entry\n");
11368 return false;
11369 }
11370
11371 /* Move the CQE into an asynchronous event entry */
11372 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11373 spin_lock_irqsave(&phba->hbalock, iflags);
11374 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11375 /* Set the async event flag */
11376 phba->hba_flag |= ASYNC_EVENT;
11377 spin_unlock_irqrestore(&phba->hbalock, iflags);
11378
11379 return true;
11380}
11381
11382/**
11383 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11384 * @phba: Pointer to HBA context object.
11385 * @cqe: Pointer to mailbox completion queue entry.
11386 *
11387 * This routine process a mailbox completion queue entry with mailbox
11388 * completion event.
11389 *
11390 * Return: true if work posted to worker thread, otherwise false.
11391 **/
11392static bool
11393lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11394{
11395 uint32_t mcqe_status;
11396 MAILBOX_t *mbox, *pmbox;
11397 struct lpfc_mqe *mqe;
11398 struct lpfc_vport *vport;
11399 struct lpfc_nodelist *ndlp;
11400 struct lpfc_dmabuf *mp;
11401 unsigned long iflags;
11402 LPFC_MBOXQ_t *pmb;
11403 bool workposted = false;
11404 int rc;
11405
11406 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11407 if (!bf_get(lpfc_trailer_completed, mcqe))
11408 goto out_no_mqe_complete;
11409
11410 /* Get the reference to the active mbox command */
11411 spin_lock_irqsave(&phba->hbalock, iflags);
11412 pmb = phba->sli.mbox_active;
11413 if (unlikely(!pmb)) {
11414 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11415 "1832 No pending MBOX command to handle\n");
11416 spin_unlock_irqrestore(&phba->hbalock, iflags);
11417 goto out_no_mqe_complete;
11418 }
11419 spin_unlock_irqrestore(&phba->hbalock, iflags);
11420 mqe = &pmb->u.mqe;
11421 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11422 mbox = phba->mbox;
11423 vport = pmb->vport;
11424
11425 /* Reset heartbeat timer */
11426 phba->last_completion_time = jiffies;
11427 del_timer(&phba->sli.mbox_tmo);
11428
11429 /* Move mbox data to caller's mailbox region, do endian swapping */
11430 if (pmb->mbox_cmpl && mbox)
11431 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 11432
73d91e50
JS
11433 /*
11434 * For mcqe errors, conditionally move a modified error code to
11435 * the mbox so that the error will not be missed.
11436 */
11437 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11438 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11439 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11440 bf_set(lpfc_mqe_status, mqe,
11441 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11442 }
04c68496
JS
11443 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11444 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11445 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11446 "MBOX dflt rpi: status:x%x rpi:x%x",
11447 mcqe_status,
11448 pmbox->un.varWords[0], 0);
11449 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11450 mp = (struct lpfc_dmabuf *)(pmb->context1);
11451 ndlp = (struct lpfc_nodelist *)pmb->context2;
11452 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11453 * RID of the PPI using the same mbox buffer.
11454 */
11455 lpfc_unreg_login(phba, vport->vpi,
11456 pmbox->un.varWords[0], pmb);
11457 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11458 pmb->context1 = mp;
11459 pmb->context2 = ndlp;
11460 pmb->vport = vport;
11461 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11462 if (rc != MBX_BUSY)
11463 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11464 LOG_SLI, "0385 rc should "
11465 "have been MBX_BUSY\n");
11466 if (rc != MBX_NOT_FINISHED)
11467 goto send_current_mbox;
11468 }
11469 }
11470 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11471 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11472 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11473
11474 /* There is mailbox completion work to do */
11475 spin_lock_irqsave(&phba->hbalock, iflags);
11476 __lpfc_mbox_cmpl_put(phba, pmb);
11477 phba->work_ha |= HA_MBATT;
11478 spin_unlock_irqrestore(&phba->hbalock, iflags);
11479 workposted = true;
11480
11481send_current_mbox:
11482 spin_lock_irqsave(&phba->hbalock, iflags);
11483 /* Release the mailbox command posting token */
11484 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11485 /* Setting active mailbox pointer need to be in sync to flag clear */
11486 phba->sli.mbox_active = NULL;
11487 spin_unlock_irqrestore(&phba->hbalock, iflags);
11488 /* Wake up worker thread to post the next pending mailbox command */
11489 lpfc_worker_wake_up(phba);
11490out_no_mqe_complete:
11491 if (bf_get(lpfc_trailer_consumed, mcqe))
11492 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11493 return workposted;
11494}
11495
11496/**
11497 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11498 * @phba: Pointer to HBA context object.
11499 * @cqe: Pointer to mailbox completion queue entry.
11500 *
11501 * This routine process a mailbox completion queue entry, it invokes the
11502 * proper mailbox complete handling or asynchrous event handling routine
11503 * according to the MCQE's async bit.
11504 *
11505 * Return: true if work posted to worker thread, otherwise false.
11506 **/
11507static bool
11508lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11509{
11510 struct lpfc_mcqe mcqe;
11511 bool workposted;
11512
11513 /* Copy the mailbox MCQE and convert endian order as needed */
11514 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11515
11516 /* Invoke the proper event handling routine */
11517 if (!bf_get(lpfc_trailer_async, &mcqe))
11518 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11519 else
11520 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11521 return workposted;
11522}
11523
4f774513
JS
11524/**
11525 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11526 * @phba: Pointer to HBA context object.
2a76a283 11527 * @cq: Pointer to associated CQ
4f774513
JS
11528 * @wcqe: Pointer to work-queue completion queue entry.
11529 *
11530 * This routine handles an ELS work-queue completion event.
11531 *
11532 * Return: true if work posted to worker thread, otherwise false.
11533 **/
11534static bool
2a76a283 11535lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
11536 struct lpfc_wcqe_complete *wcqe)
11537{
4f774513
JS
11538 struct lpfc_iocbq *irspiocbq;
11539 unsigned long iflags;
2a76a283 11540 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
11541 int txq_cnt = 0;
11542 int txcmplq_cnt = 0;
11543 int fcp_txcmplq_cnt = 0;
4f774513 11544
45ed1190 11545 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
11546 irspiocbq = lpfc_sli_get_iocbq(phba);
11547 if (!irspiocbq) {
0e9bb8d7
JS
11548 if (!list_empty(&pring->txq))
11549 txq_cnt++;
11550 if (!list_empty(&pring->txcmplq))
11551 txcmplq_cnt++;
11552 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11553 fcp_txcmplq_cnt++;
4f774513 11554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
11555 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11556 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
11557 txq_cnt, phba->iocb_cnt,
11558 fcp_txcmplq_cnt,
11559 txcmplq_cnt);
45ed1190 11560 return false;
4f774513 11561 }
4f774513 11562
45ed1190
JS
11563 /* Save off the slow-path queue event for work thread to process */
11564 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 11565 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 11566 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
11567 &phba->sli4_hba.sp_queue_event);
11568 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 11569 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 11570
45ed1190 11571 return true;
4f774513
JS
11572}
11573
11574/**
11575 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11576 * @phba: Pointer to HBA context object.
11577 * @wcqe: Pointer to work-queue completion queue entry.
11578 *
11579 * This routine handles slow-path WQ entry comsumed event by invoking the
11580 * proper WQ release routine to the slow-path WQ.
11581 **/
11582static void
11583lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11584 struct lpfc_wcqe_release *wcqe)
11585{
2e90f4b5
JS
11586 /* sanity check on queue memory */
11587 if (unlikely(!phba->sli4_hba.els_wq))
11588 return;
4f774513
JS
11589 /* Check for the slow-path ELS work queue */
11590 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11591 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11592 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11593 else
11594 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11595 "2579 Slow-path wqe consume event carries "
11596 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11597 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11598 phba->sli4_hba.els_wq->queue_id);
11599}
11600
11601/**
11602 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11603 * @phba: Pointer to HBA context object.
11604 * @cq: Pointer to a WQ completion queue.
11605 * @wcqe: Pointer to work-queue completion queue entry.
11606 *
11607 * This routine handles an XRI abort event.
11608 *
11609 * Return: true if work posted to worker thread, otherwise false.
11610 **/
11611static bool
11612lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11613 struct lpfc_queue *cq,
11614 struct sli4_wcqe_xri_aborted *wcqe)
11615{
11616 bool workposted = false;
11617 struct lpfc_cq_event *cq_event;
11618 unsigned long iflags;
11619
11620 /* Allocate a new internal CQ_EVENT entry */
11621 cq_event = lpfc_sli4_cq_event_alloc(phba);
11622 if (!cq_event) {
11623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11624 "0602 Failed to allocate CQ_EVENT entry\n");
11625 return false;
11626 }
11627
11628 /* Move the CQE into the proper xri abort event list */
11629 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11630 switch (cq->subtype) {
11631 case LPFC_FCP:
11632 spin_lock_irqsave(&phba->hbalock, iflags);
11633 list_add_tail(&cq_event->list,
11634 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11635 /* Set the fcp xri abort event flag */
11636 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11637 spin_unlock_irqrestore(&phba->hbalock, iflags);
11638 workposted = true;
11639 break;
11640 case LPFC_ELS:
11641 spin_lock_irqsave(&phba->hbalock, iflags);
11642 list_add_tail(&cq_event->list,
11643 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11644 /* Set the els xri abort event flag */
11645 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11646 spin_unlock_irqrestore(&phba->hbalock, iflags);
11647 workposted = true;
11648 break;
11649 default:
11650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11651 "0603 Invalid work queue CQE subtype (x%x)\n",
11652 cq->subtype);
11653 workposted = false;
11654 break;
11655 }
11656 return workposted;
11657}
11658
4f774513
JS
11659/**
11660 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11661 * @phba: Pointer to HBA context object.
11662 * @rcqe: Pointer to receive-queue completion queue entry.
11663 *
11664 * This routine process a receive-queue completion queue entry.
11665 *
11666 * Return: true if work posted to worker thread, otherwise false.
11667 **/
11668static bool
4d9ab994 11669lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 11670{
4f774513
JS
11671 bool workposted = false;
11672 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11673 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11674 struct hbq_dmabuf *dma_buf;
7851fe2c 11675 uint32_t status, rq_id;
4f774513
JS
11676 unsigned long iflags;
11677
2e90f4b5
JS
11678 /* sanity check on queue memory */
11679 if (unlikely(!hrq) || unlikely(!drq))
11680 return workposted;
11681
7851fe2c
JS
11682 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11683 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11684 else
11685 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11686 if (rq_id != hrq->queue_id)
4f774513
JS
11687 goto out;
11688
4d9ab994 11689 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
11690 switch (status) {
11691 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11693 "2537 Receive Frame Truncated!!\n");
b84daac9 11694 hrq->RQ_buf_trunc++;
4f774513 11695 case FC_STATUS_RQ_SUCCESS:
5ffc266e 11696 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
11697 spin_lock_irqsave(&phba->hbalock, iflags);
11698 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11699 if (!dma_buf) {
b84daac9 11700 hrq->RQ_no_buf_found++;
4f774513
JS
11701 spin_unlock_irqrestore(&phba->hbalock, iflags);
11702 goto out;
11703 }
b84daac9 11704 hrq->RQ_rcv_buf++;
4d9ab994 11705 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 11706 /* save off the frame for the word thread to process */
4d9ab994 11707 list_add_tail(&dma_buf->cq_event.list,
45ed1190 11708 &phba->sli4_hba.sp_queue_event);
4f774513 11709 /* Frame received */
45ed1190 11710 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
11711 spin_unlock_irqrestore(&phba->hbalock, iflags);
11712 workposted = true;
11713 break;
11714 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11715 case FC_STATUS_INSUFF_BUF_FRM_DISC:
b84daac9 11716 hrq->RQ_no_posted_buf++;
4f774513
JS
11717 /* Post more buffers if possible */
11718 spin_lock_irqsave(&phba->hbalock, iflags);
11719 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11720 spin_unlock_irqrestore(&phba->hbalock, iflags);
11721 workposted = true;
11722 break;
11723 }
11724out:
11725 return workposted;
4f774513
JS
11726}
11727
4d9ab994
JS
11728/**
11729 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11730 * @phba: Pointer to HBA context object.
11731 * @cq: Pointer to the completion queue.
11732 * @wcqe: Pointer to a completion queue entry.
11733 *
25985edc 11734 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
11735 * entry.
11736 *
11737 * Return: true if work posted to worker thread, otherwise false.
11738 **/
11739static bool
11740lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11741 struct lpfc_cqe *cqe)
11742{
45ed1190 11743 struct lpfc_cqe cqevt;
4d9ab994
JS
11744 bool workposted = false;
11745
11746 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 11747 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
11748
11749 /* Check and process for different type of WCQE and dispatch */
45ed1190 11750 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 11751 case CQE_CODE_COMPL_WQE:
45ed1190 11752 /* Process the WQ/RQ complete event */
bc73905a 11753 phba->last_completion_time = jiffies;
2a76a283 11754 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 11755 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
11756 break;
11757 case CQE_CODE_RELEASE_WQE:
11758 /* Process the WQ release event */
11759 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 11760 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
11761 break;
11762 case CQE_CODE_XRI_ABORTED:
11763 /* Process the WQ XRI abort event */
bc73905a 11764 phba->last_completion_time = jiffies;
4d9ab994 11765 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 11766 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
11767 break;
11768 case CQE_CODE_RECEIVE:
7851fe2c 11769 case CQE_CODE_RECEIVE_V1:
4d9ab994 11770 /* Process the RQ event */
bc73905a 11771 phba->last_completion_time = jiffies;
4d9ab994 11772 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 11773 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
11774 break;
11775 default:
11776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11777 "0388 Not a valid WCQE code: x%x\n",
45ed1190 11778 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
11779 break;
11780 }
11781 return workposted;
11782}
11783
4f774513
JS
11784/**
11785 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11786 * @phba: Pointer to HBA context object.
11787 * @eqe: Pointer to fast-path event queue entry.
11788 *
11789 * This routine process a event queue entry from the slow-path event queue.
11790 * It will check the MajorCode and MinorCode to determine this is for a
11791 * completion event on a completion queue, if not, an error shall be logged
11792 * and just return. Otherwise, it will get to the corresponding completion
11793 * queue and process all the entries on that completion queue, rearm the
11794 * completion queue, and then return.
11795 *
11796 **/
11797static void
67d12733
JS
11798lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11799 struct lpfc_queue *speq)
4f774513 11800{
67d12733 11801 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
11802 struct lpfc_cqe *cqe;
11803 bool workposted = false;
11804 int ecount = 0;
11805 uint16_t cqid;
11806
4f774513 11807 /* Get the reference to the corresponding CQ */
cb5172ea 11808 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 11809
4f774513
JS
11810 list_for_each_entry(childq, &speq->child_list, list) {
11811 if (childq->queue_id == cqid) {
11812 cq = childq;
11813 break;
11814 }
11815 }
11816 if (unlikely(!cq)) {
75baf696
JS
11817 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11819 "0365 Slow-path CQ identifier "
11820 "(%d) does not exist\n", cqid);
4f774513
JS
11821 return;
11822 }
11823
11824 /* Process all the entries to the CQ */
11825 switch (cq->type) {
11826 case LPFC_MCQ:
11827 while ((cqe = lpfc_sli4_cq_get(cq))) {
11828 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 11829 if (!(++ecount % cq->entry_repost))
4f774513 11830 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
b84daac9 11831 cq->CQ_mbox++;
4f774513
JS
11832 }
11833 break;
11834 case LPFC_WCQ:
11835 while ((cqe = lpfc_sli4_cq_get(cq))) {
0558056c
JS
11836 if (cq->subtype == LPFC_FCP)
11837 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11838 cqe);
11839 else
11840 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11841 cqe);
73d91e50 11842 if (!(++ecount % cq->entry_repost))
4f774513
JS
11843 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11844 }
b84daac9
JS
11845
11846 /* Track the max number of CQEs processed in 1 EQ */
11847 if (ecount > cq->CQ_max_cqe)
11848 cq->CQ_max_cqe = ecount;
4f774513
JS
11849 break;
11850 default:
11851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11852 "0370 Invalid completion queue type (%d)\n",
11853 cq->type);
11854 return;
11855 }
11856
11857 /* Catch the no cq entry condition, log an error */
11858 if (unlikely(ecount == 0))
11859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11860 "0371 No entry from the CQ: identifier "
11861 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11862
11863 /* In any case, flash and re-arm the RCQ */
11864 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11865
11866 /* wake up worker thread if there are works to be done */
11867 if (workposted)
11868 lpfc_worker_wake_up(phba);
11869}
11870
11871/**
11872 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
11873 * @phba: Pointer to HBA context object.
11874 * @cq: Pointer to associated CQ
11875 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
11876 *
11877 * This routine process a fast-path work queue completion entry from fast-path
11878 * event queue for FCP command response completion.
11879 **/
11880static void
2a76a283 11881lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
11882 struct lpfc_wcqe_complete *wcqe)
11883{
2a76a283 11884 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
11885 struct lpfc_iocbq *cmdiocbq;
11886 struct lpfc_iocbq irspiocbq;
11887 unsigned long iflags;
11888
4f774513
JS
11889 /* Check for response status */
11890 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11891 /* If resource errors reported from HBA, reduce queue
11892 * depth of the SCSI device.
11893 */
e3d2b802
JS
11894 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11895 IOSTAT_LOCAL_REJECT)) &&
11896 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11897 IOERR_NO_RESOURCES))
4f774513 11898 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 11899
4f774513
JS
11900 /* Log the error status */
11901 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11902 "0373 FCP complete error: status=x%x, "
11903 "hw_status=x%x, total_data_specified=%d, "
11904 "parameter=x%x, word3=x%x\n",
11905 bf_get(lpfc_wcqe_c_status, wcqe),
11906 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11907 wcqe->total_data_placed, wcqe->parameter,
11908 wcqe->word3);
11909 }
11910
11911 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
11912 spin_lock_irqsave(&pring->ring_lock, iflags);
11913 pring->stats.iocb_event++;
4f774513
JS
11914 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11915 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 11916 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
11917 if (unlikely(!cmdiocbq)) {
11918 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11919 "0374 FCP complete with no corresponding "
11920 "cmdiocb: iotag (%d)\n",
11921 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11922 return;
11923 }
11924 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11925 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11926 "0375 FCP cmdiocb not callback function "
11927 "iotag: (%d)\n",
11928 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11929 return;
11930 }
11931
11932 /* Fake the irspiocb and copy necessary response information */
341af102 11933 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 11934
0f65ff68
JS
11935 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11936 spin_lock_irqsave(&phba->hbalock, iflags);
11937 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11938 spin_unlock_irqrestore(&phba->hbalock, iflags);
11939 }
11940
4f774513
JS
11941 /* Pass the cmd_iocb and the rsp state to the upper layer */
11942 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11943}
11944
11945/**
11946 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11947 * @phba: Pointer to HBA context object.
11948 * @cq: Pointer to completion queue.
11949 * @wcqe: Pointer to work-queue completion queue entry.
11950 *
11951 * This routine handles an fast-path WQ entry comsumed event by invoking the
11952 * proper WQ release routine to the slow-path WQ.
11953 **/
11954static void
11955lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11956 struct lpfc_wcqe_release *wcqe)
11957{
11958 struct lpfc_queue *childwq;
11959 bool wqid_matched = false;
11960 uint16_t fcp_wqid;
11961
11962 /* Check for fast-path FCP work queue release */
11963 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11964 list_for_each_entry(childwq, &cq->child_list, list) {
11965 if (childwq->queue_id == fcp_wqid) {
11966 lpfc_sli4_wq_release(childwq,
11967 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11968 wqid_matched = true;
11969 break;
11970 }
11971 }
11972 /* Report warning log message if no match found */
11973 if (wqid_matched != true)
11974 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11975 "2580 Fast-path wqe consume event carries "
11976 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11977}
11978
11979/**
11980 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11981 * @cq: Pointer to the completion queue.
11982 * @eqe: Pointer to fast-path completion queue entry.
11983 *
11984 * This routine process a fast-path work queue completion entry from fast-path
11985 * event queue for FCP command response completion.
11986 **/
11987static int
11988lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11989 struct lpfc_cqe *cqe)
11990{
11991 struct lpfc_wcqe_release wcqe;
11992 bool workposted = false;
11993
11994 /* Copy the work queue CQE and convert endian order if needed */
11995 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11996
11997 /* Check and process for different type of WCQE and dispatch */
11998 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11999 case CQE_CODE_COMPL_WQE:
b84daac9 12000 cq->CQ_wq++;
4f774513 12001 /* Process the WQ complete event */
98fc5dd9 12002 phba->last_completion_time = jiffies;
2a76a283 12003 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
12004 (struct lpfc_wcqe_complete *)&wcqe);
12005 break;
12006 case CQE_CODE_RELEASE_WQE:
b84daac9 12007 cq->CQ_release_wqe++;
4f774513
JS
12008 /* Process the WQ release event */
12009 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12010 (struct lpfc_wcqe_release *)&wcqe);
12011 break;
12012 case CQE_CODE_XRI_ABORTED:
b84daac9 12013 cq->CQ_xri_aborted++;
4f774513 12014 /* Process the WQ XRI abort event */
bc73905a 12015 phba->last_completion_time = jiffies;
4f774513
JS
12016 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12017 (struct sli4_wcqe_xri_aborted *)&wcqe);
12018 break;
12019 default:
12020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12021 "0144 Not a valid WCQE code: x%x\n",
12022 bf_get(lpfc_wcqe_c_code, &wcqe));
12023 break;
12024 }
12025 return workposted;
12026}
12027
12028/**
67d12733 12029 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
12030 * @phba: Pointer to HBA context object.
12031 * @eqe: Pointer to fast-path event queue entry.
12032 *
12033 * This routine process a event queue entry from the fast-path event queue.
12034 * It will check the MajorCode and MinorCode to determine this is for a
12035 * completion event on a completion queue, if not, an error shall be logged
12036 * and just return. Otherwise, it will get to the corresponding completion
12037 * queue and process all the entries on the completion queue, rearm the
12038 * completion queue, and then return.
12039 **/
12040static void
67d12733
JS
12041lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12042 uint32_t qidx)
4f774513
JS
12043{
12044 struct lpfc_queue *cq;
12045 struct lpfc_cqe *cqe;
12046 bool workposted = false;
12047 uint16_t cqid;
12048 int ecount = 0;
12049
cb5172ea 12050 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 12051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 12052 "0366 Not a valid completion "
4f774513 12053 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
12054 bf_get_le32(lpfc_eqe_major_code, eqe),
12055 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
12056 return;
12057 }
12058
67d12733
JS
12059 /* Get the reference to the corresponding CQ */
12060 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12061
12062 /* Check if this is a Slow path event */
12063 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12064 lpfc_sli4_sp_handle_eqe(phba, eqe,
12065 phba->sli4_hba.hba_eq[qidx]);
12066 return;
12067 }
12068
2e90f4b5
JS
12069 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12070 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12071 "3146 Fast-path completion queues "
12072 "does not exist\n");
12073 return;
12074 }
67d12733 12075 cq = phba->sli4_hba.fcp_cq[qidx];
4f774513 12076 if (unlikely(!cq)) {
75baf696
JS
12077 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12078 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12079 "0367 Fast-path completion queue "
67d12733 12080 "(%d) does not exist\n", qidx);
4f774513
JS
12081 return;
12082 }
12083
4f774513
JS
12084 if (unlikely(cqid != cq->queue_id)) {
12085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12086 "0368 Miss-matched fast-path completion "
12087 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12088 cqid, cq->queue_id);
12089 return;
12090 }
12091
12092 /* Process all the entries to the CQ */
12093 while ((cqe = lpfc_sli4_cq_get(cq))) {
12094 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
73d91e50 12095 if (!(++ecount % cq->entry_repost))
4f774513
JS
12096 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12097 }
12098
b84daac9
JS
12099 /* Track the max number of CQEs processed in 1 EQ */
12100 if (ecount > cq->CQ_max_cqe)
12101 cq->CQ_max_cqe = ecount;
12102
4f774513
JS
12103 /* Catch the no cq entry condition */
12104 if (unlikely(ecount == 0))
12105 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12106 "0369 No entry from fast-path completion "
12107 "queue fcpcqid=%d\n", cq->queue_id);
12108
12109 /* In any case, flash and re-arm the CQ */
12110 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12111
12112 /* wake up worker thread if there are works to be done */
12113 if (workposted)
12114 lpfc_worker_wake_up(phba);
12115}
12116
12117static void
12118lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12119{
12120 struct lpfc_eqe *eqe;
12121
12122 /* walk all the EQ entries and drop on the floor */
12123 while ((eqe = lpfc_sli4_eq_get(eq)))
12124 ;
12125
12126 /* Clear and re-arm the EQ */
12127 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12128}
12129
12130/**
67d12733 12131 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
12132 * @irq: Interrupt number.
12133 * @dev_id: The device context pointer.
12134 *
12135 * This function is directly called from the PCI layer as an interrupt
12136 * service routine when device with SLI-4 interface spec is enabled with
12137 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12138 * ring event in the HBA. However, when the device is enabled with either
12139 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12140 * device-level interrupt handler. When the PCI slot is in error recovery
12141 * or the HBA is undergoing initialization, the interrupt handler will not
12142 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12143 * the intrrupt context. This function is called without any lock held.
12144 * It gets the hbalock to access and update SLI data structures. Note that,
12145 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12146 * equal to that of FCP CQ index.
12147 *
67d12733
JS
12148 * The link attention and ELS ring attention events are handled
12149 * by the worker thread. The interrupt handler signals the worker thread
12150 * and returns for these events. This function is called without any lock
12151 * held. It gets the hbalock to access and update SLI data structures.
12152 *
4f774513
JS
12153 * This function returns IRQ_HANDLED when interrupt is handled else it
12154 * returns IRQ_NONE.
12155 **/
12156irqreturn_t
67d12733 12157lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
12158{
12159 struct lpfc_hba *phba;
12160 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12161 struct lpfc_queue *fpeq;
12162 struct lpfc_eqe *eqe;
12163 unsigned long iflag;
12164 int ecount = 0;
962bc51b 12165 int fcp_eqidx;
4f774513
JS
12166
12167 /* Get the driver's phba structure from the dev_id */
12168 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12169 phba = fcp_eq_hdl->phba;
12170 fcp_eqidx = fcp_eq_hdl->idx;
12171
12172 if (unlikely(!phba))
12173 return IRQ_NONE;
67d12733 12174 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 12175 return IRQ_NONE;
4f774513
JS
12176
12177 /* Get to the EQ struct associated with this vector */
67d12733 12178 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
2e90f4b5
JS
12179 if (unlikely(!fpeq))
12180 return IRQ_NONE;
4f774513 12181
ba20c853
JS
12182 if (lpfc_fcp_look_ahead) {
12183 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12184 lpfc_sli4_eq_clr_intr(fpeq);
12185 else {
12186 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12187 return IRQ_NONE;
12188 }
12189 }
12190
4f774513
JS
12191 /* Check device state for handling interrupt */
12192 if (unlikely(lpfc_intr_state_check(phba))) {
b84daac9 12193 fpeq->EQ_badstate++;
4f774513
JS
12194 /* Check again for link_state with lock held */
12195 spin_lock_irqsave(&phba->hbalock, iflag);
12196 if (phba->link_state < LPFC_LINK_DOWN)
12197 /* Flush, clear interrupt, and rearm the EQ */
12198 lpfc_sli4_eq_flush(phba, fpeq);
12199 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853
JS
12200 if (lpfc_fcp_look_ahead)
12201 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
12202 return IRQ_NONE;
12203 }
12204
12205 /*
12206 * Process all the event on FCP fast-path EQ
12207 */
12208 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
67d12733 12209 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
73d91e50 12210 if (!(++ecount % fpeq->entry_repost))
4f774513 12211 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
b84daac9 12212 fpeq->EQ_processed++;
4f774513
JS
12213 }
12214
b84daac9
JS
12215 /* Track the max number of EQEs processed in 1 intr */
12216 if (ecount > fpeq->EQ_max_eqe)
12217 fpeq->EQ_max_eqe = ecount;
12218
4f774513
JS
12219 /* Always clear and re-arm the fast-path EQ */
12220 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12221
12222 if (unlikely(ecount == 0)) {
b84daac9 12223 fpeq->EQ_no_entry++;
ba20c853
JS
12224
12225 if (lpfc_fcp_look_ahead) {
12226 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12227 return IRQ_NONE;
12228 }
12229
4f774513
JS
12230 if (phba->intr_type == MSIX)
12231 /* MSI-X treated interrupt served as no EQ share INT */
12232 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12233 "0358 MSI-X interrupt with no EQE\n");
12234 else
12235 /* Non MSI-X treated on interrupt as EQ share INT */
12236 return IRQ_NONE;
12237 }
12238
ba20c853
JS
12239 if (lpfc_fcp_look_ahead)
12240 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
4f774513
JS
12241 return IRQ_HANDLED;
12242} /* lpfc_sli4_fp_intr_handler */
12243
12244/**
12245 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12246 * @irq: Interrupt number.
12247 * @dev_id: The device context pointer.
12248 *
12249 * This function is the device-level interrupt handler to device with SLI-4
12250 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12251 * interrupt mode is enabled and there is an event in the HBA which requires
12252 * driver attention. This function invokes the slow-path interrupt attention
12253 * handling function and fast-path interrupt attention handling function in
12254 * turn to process the relevant HBA attention events. This function is called
12255 * without any lock held. It gets the hbalock to access and update SLI data
12256 * structures.
12257 *
12258 * This function returns IRQ_HANDLED when interrupt is handled, else it
12259 * returns IRQ_NONE.
12260 **/
12261irqreturn_t
12262lpfc_sli4_intr_handler(int irq, void *dev_id)
12263{
12264 struct lpfc_hba *phba;
67d12733
JS
12265 irqreturn_t hba_irq_rc;
12266 bool hba_handled = false;
962bc51b 12267 int fcp_eqidx;
4f774513
JS
12268
12269 /* Get the driver's phba structure from the dev_id */
12270 phba = (struct lpfc_hba *)dev_id;
12271
12272 if (unlikely(!phba))
12273 return IRQ_NONE;
12274
4f774513
JS
12275 /*
12276 * Invoke fast-path host attention interrupt handling as appropriate.
12277 */
67d12733
JS
12278 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12279 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
4f774513 12280 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
67d12733
JS
12281 if (hba_irq_rc == IRQ_HANDLED)
12282 hba_handled |= true;
4f774513
JS
12283 }
12284
67d12733 12285 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
12286} /* lpfc_sli4_intr_handler */
12287
12288/**
12289 * lpfc_sli4_queue_free - free a queue structure and associated memory
12290 * @queue: The queue structure to free.
12291 *
b595076a 12292 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
12293 * the host resident queue. This function must be called after destroying the
12294 * queue on the HBA.
12295 **/
12296void
12297lpfc_sli4_queue_free(struct lpfc_queue *queue)
12298{
12299 struct lpfc_dmabuf *dmabuf;
12300
12301 if (!queue)
12302 return;
12303
12304 while (!list_empty(&queue->page_list)) {
12305 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12306 list);
49198b37 12307 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
12308 dmabuf->virt, dmabuf->phys);
12309 kfree(dmabuf);
12310 }
12311 kfree(queue);
12312 return;
12313}
12314
12315/**
12316 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12317 * @phba: The HBA that this queue is being created on.
12318 * @entry_size: The size of each queue entry for this queue.
12319 * @entry count: The number of entries that this queue will handle.
12320 *
12321 * This function allocates a queue structure and the DMAable memory used for
12322 * the host resident queue. This function must be called before creating the
12323 * queue on the HBA.
12324 **/
12325struct lpfc_queue *
12326lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12327 uint32_t entry_count)
12328{
12329 struct lpfc_queue *queue;
12330 struct lpfc_dmabuf *dmabuf;
12331 int x, total_qe_count;
12332 void *dma_pointer;
cb5172ea 12333 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 12334
cb5172ea
JS
12335 if (!phba->sli4_hba.pc_sli4_params.supported)
12336 hw_page_size = SLI4_PAGE_SIZE;
12337
4f774513
JS
12338 queue = kzalloc(sizeof(struct lpfc_queue) +
12339 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12340 if (!queue)
12341 return NULL;
cb5172ea
JS
12342 queue->page_count = (ALIGN(entry_size * entry_count,
12343 hw_page_size))/hw_page_size;
4f774513
JS
12344 INIT_LIST_HEAD(&queue->list);
12345 INIT_LIST_HEAD(&queue->page_list);
12346 INIT_LIST_HEAD(&queue->child_list);
12347 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12348 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12349 if (!dmabuf)
12350 goto out_fail;
12351 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 12352 hw_page_size, &dmabuf->phys,
4f774513
JS
12353 GFP_KERNEL);
12354 if (!dmabuf->virt) {
12355 kfree(dmabuf);
12356 goto out_fail;
12357 }
cb5172ea 12358 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12359 dmabuf->buffer_tag = x;
12360 list_add_tail(&dmabuf->list, &queue->page_list);
12361 /* initialize queue's entry array */
12362 dma_pointer = dmabuf->virt;
12363 for (; total_qe_count < entry_count &&
cb5172ea 12364 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
12365 total_qe_count++, dma_pointer += entry_size) {
12366 queue->qe[total_qe_count].address = dma_pointer;
12367 }
12368 }
12369 queue->entry_size = entry_size;
12370 queue->entry_count = entry_count;
73d91e50
JS
12371
12372 /*
12373 * entry_repost is calculated based on the number of entries in the
12374 * queue. This works out except for RQs. If buffers are NOT initially
12375 * posted for every RQE, entry_repost should be adjusted accordingly.
12376 */
12377 queue->entry_repost = (entry_count >> 3);
12378 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12379 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
12380 queue->phba = phba;
12381
12382 return queue;
12383out_fail:
12384 lpfc_sli4_queue_free(queue);
12385 return NULL;
12386}
12387
962bc51b
JS
12388/**
12389 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12390 * @phba: HBA structure that indicates port to create a queue on.
12391 * @pci_barset: PCI BAR set flag.
12392 *
12393 * This function shall perform iomap of the specified PCI BAR address to host
12394 * memory address if not already done so and return it. The returned host
12395 * memory address can be NULL.
12396 */
12397static void __iomem *
12398lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12399{
12400 struct pci_dev *pdev;
962bc51b
JS
12401
12402 if (!phba->pcidev)
12403 return NULL;
12404 else
12405 pdev = phba->pcidev;
12406
12407 switch (pci_barset) {
12408 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
12409 return phba->pci_bar0_memmap_p;
12410 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
12411 return phba->pci_bar2_memmap_p;
12412 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
12413 return phba->pci_bar4_memmap_p;
12414 default:
12415 break;
12416 }
12417 return NULL;
12418}
12419
173edbb2
JS
12420/**
12421 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12422 * @phba: HBA structure that indicates port to create a queue on.
12423 * @startq: The starting FCP EQ to modify
12424 *
12425 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12426 *
12427 * The @phba struct is used to send mailbox command to HBA. The @startq
12428 * is used to get the starting FCP EQ to change.
12429 * This function is asynchronous and will wait for the mailbox
12430 * command to finish before continuing.
12431 *
12432 * On success this function will return a zero. If unable to allocate enough
12433 * memory this function will return -ENOMEM. If the queue create mailbox command
12434 * fails this function will return -ENXIO.
12435 **/
12436uint32_t
12437lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12438{
12439 struct lpfc_mbx_modify_eq_delay *eq_delay;
12440 LPFC_MBOXQ_t *mbox;
12441 struct lpfc_queue *eq;
12442 int cnt, rc, length, status = 0;
12443 uint32_t shdr_status, shdr_add_status;
ee02006b 12444 uint32_t result;
173edbb2
JS
12445 int fcp_eqidx;
12446 union lpfc_sli4_cfg_shdr *shdr;
12447 uint16_t dmult;
12448
67d12733 12449 if (startq >= phba->cfg_fcp_io_channel)
173edbb2
JS
12450 return 0;
12451
12452 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12453 if (!mbox)
12454 return -ENOMEM;
12455 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12456 sizeof(struct lpfc_sli4_cfg_mhdr));
12457 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12458 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12459 length, LPFC_SLI4_MBX_EMBED);
12460 eq_delay = &mbox->u.mqe.un.eq_delay;
12461
12462 /* Calculate delay multiper from maximum interrupt per second */
ee02006b
JS
12463 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12464 if (result > LPFC_DMULT_CONST)
12465 dmult = 0;
12466 else
12467 dmult = LPFC_DMULT_CONST/result - 1;
173edbb2
JS
12468
12469 cnt = 0;
67d12733 12470 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
173edbb2 12471 fcp_eqidx++) {
67d12733 12472 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
173edbb2
JS
12473 if (!eq)
12474 continue;
12475 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12476 eq_delay->u.request.eq[cnt].phase = 0;
12477 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12478 cnt++;
12479 if (cnt >= LPFC_MAX_EQ_DELAY)
12480 break;
12481 }
12482 eq_delay->u.request.num_eq = cnt;
12483
12484 mbox->vport = phba->pport;
12485 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12486 mbox->context1 = NULL;
12487 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12488 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12489 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12491 if (shdr_status || shdr_add_status || rc) {
12492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12493 "2512 MODIFY_EQ_DELAY mailbox failed with "
12494 "status x%x add_status x%x, mbx status x%x\n",
12495 shdr_status, shdr_add_status, rc);
12496 status = -ENXIO;
12497 }
12498 mempool_free(mbox, phba->mbox_mem_pool);
12499 return status;
12500}
12501
4f774513
JS
12502/**
12503 * lpfc_eq_create - Create an Event Queue on the HBA
12504 * @phba: HBA structure that indicates port to create a queue on.
12505 * @eq: The queue structure to use to create the event queue.
12506 * @imax: The maximum interrupt per second limit.
12507 *
12508 * This function creates an event queue, as detailed in @eq, on a port,
12509 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12510 *
12511 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12512 * is used to get the entry count and entry size that are necessary to
12513 * determine the number of pages to allocate and use for this queue. This
12514 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12515 * event queue. This function is asynchronous and will wait for the mailbox
12516 * command to finish before continuing.
12517 *
12518 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12519 * memory this function will return -ENOMEM. If the queue create mailbox command
12520 * fails this function will return -ENXIO.
4f774513
JS
12521 **/
12522uint32_t
ee02006b 12523lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
12524{
12525 struct lpfc_mbx_eq_create *eq_create;
12526 LPFC_MBOXQ_t *mbox;
12527 int rc, length, status = 0;
12528 struct lpfc_dmabuf *dmabuf;
12529 uint32_t shdr_status, shdr_add_status;
12530 union lpfc_sli4_cfg_shdr *shdr;
12531 uint16_t dmult;
49198b37
JS
12532 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12533
2e90f4b5
JS
12534 /* sanity check on queue memory */
12535 if (!eq)
12536 return -ENODEV;
49198b37
JS
12537 if (!phba->sli4_hba.pc_sli4_params.supported)
12538 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12539
12540 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12541 if (!mbox)
12542 return -ENOMEM;
12543 length = (sizeof(struct lpfc_mbx_eq_create) -
12544 sizeof(struct lpfc_sli4_cfg_mhdr));
12545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12546 LPFC_MBOX_OPCODE_EQ_CREATE,
12547 length, LPFC_SLI4_MBX_EMBED);
12548 eq_create = &mbox->u.mqe.un.eq_create;
12549 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12550 eq->page_count);
12551 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12552 LPFC_EQE_SIZE);
12553 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12554 /* Calculate delay multiper from maximum interrupt per second */
ee02006b
JS
12555 if (imax > LPFC_DMULT_CONST)
12556 dmult = 0;
12557 else
12558 dmult = LPFC_DMULT_CONST/imax - 1;
4f774513
JS
12559 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12560 dmult);
12561 switch (eq->entry_count) {
12562 default:
12563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12564 "0360 Unsupported EQ count. (%d)\n",
12565 eq->entry_count);
12566 if (eq->entry_count < 256)
12567 return -EINVAL;
12568 /* otherwise default to smallest count (drop through) */
12569 case 256:
12570 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12571 LPFC_EQ_CNT_256);
12572 break;
12573 case 512:
12574 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12575 LPFC_EQ_CNT_512);
12576 break;
12577 case 1024:
12578 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12579 LPFC_EQ_CNT_1024);
12580 break;
12581 case 2048:
12582 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12583 LPFC_EQ_CNT_2048);
12584 break;
12585 case 4096:
12586 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12587 LPFC_EQ_CNT_4096);
12588 break;
12589 }
12590 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 12591 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12592 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12593 putPaddrLow(dmabuf->phys);
12594 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12595 putPaddrHigh(dmabuf->phys);
12596 }
12597 mbox->vport = phba->pport;
12598 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12599 mbox->context1 = NULL;
12600 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12601 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12602 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12603 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12604 if (shdr_status || shdr_add_status || rc) {
12605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12606 "2500 EQ_CREATE mailbox failed with "
12607 "status x%x add_status x%x, mbx status x%x\n",
12608 shdr_status, shdr_add_status, rc);
12609 status = -ENXIO;
12610 }
12611 eq->type = LPFC_EQ;
12612 eq->subtype = LPFC_NONE;
12613 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12614 if (eq->queue_id == 0xFFFF)
12615 status = -ENXIO;
12616 eq->host_index = 0;
12617 eq->hba_index = 0;
12618
8fa38513 12619 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12620 return status;
12621}
12622
12623/**
12624 * lpfc_cq_create - Create a Completion Queue on the HBA
12625 * @phba: HBA structure that indicates port to create a queue on.
12626 * @cq: The queue structure to use to create the completion queue.
12627 * @eq: The event queue to bind this completion queue to.
12628 *
12629 * This function creates a completion queue, as detailed in @wq, on a port,
12630 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12631 *
12632 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12633 * is used to get the entry count and entry size that are necessary to
12634 * determine the number of pages to allocate and use for this queue. The @eq
12635 * is used to indicate which event queue to bind this completion queue to. This
12636 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12637 * completion queue. This function is asynchronous and will wait for the mailbox
12638 * command to finish before continuing.
12639 *
12640 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12641 * memory this function will return -ENOMEM. If the queue create mailbox command
12642 * fails this function will return -ENXIO.
4f774513
JS
12643 **/
12644uint32_t
12645lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12646 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12647{
12648 struct lpfc_mbx_cq_create *cq_create;
12649 struct lpfc_dmabuf *dmabuf;
12650 LPFC_MBOXQ_t *mbox;
12651 int rc, length, status = 0;
12652 uint32_t shdr_status, shdr_add_status;
12653 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
12654 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12655
2e90f4b5
JS
12656 /* sanity check on queue memory */
12657 if (!cq || !eq)
12658 return -ENODEV;
49198b37
JS
12659 if (!phba->sli4_hba.pc_sli4_params.supported)
12660 hw_page_size = SLI4_PAGE_SIZE;
12661
4f774513
JS
12662 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12663 if (!mbox)
12664 return -ENOMEM;
12665 length = (sizeof(struct lpfc_mbx_cq_create) -
12666 sizeof(struct lpfc_sli4_cfg_mhdr));
12667 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12668 LPFC_MBOX_OPCODE_CQ_CREATE,
12669 length, LPFC_SLI4_MBX_EMBED);
12670 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 12671 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
12672 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12673 cq->page_count);
12674 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12675 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
12676 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12677 phba->sli4_hba.pc_sli4_params.cqv);
12678 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
12679 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12680 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
12681 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12682 eq->queue_id);
12683 } else {
12684 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12685 eq->queue_id);
12686 }
4f774513
JS
12687 switch (cq->entry_count) {
12688 default:
12689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12690 "0361 Unsupported CQ count. (%d)\n",
12691 cq->entry_count);
4f4c1863
JS
12692 if (cq->entry_count < 256) {
12693 status = -EINVAL;
12694 goto out;
12695 }
4f774513
JS
12696 /* otherwise default to smallest count (drop through) */
12697 case 256:
12698 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12699 LPFC_CQ_CNT_256);
12700 break;
12701 case 512:
12702 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12703 LPFC_CQ_CNT_512);
12704 break;
12705 case 1024:
12706 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12707 LPFC_CQ_CNT_1024);
12708 break;
12709 }
12710 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 12711 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12712 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12713 putPaddrLow(dmabuf->phys);
12714 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12715 putPaddrHigh(dmabuf->phys);
12716 }
12717 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12718
12719 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12720 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12721 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12722 if (shdr_status || shdr_add_status || rc) {
12723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12724 "2501 CQ_CREATE mailbox failed with "
12725 "status x%x add_status x%x, mbx status x%x\n",
12726 shdr_status, shdr_add_status, rc);
12727 status = -ENXIO;
12728 goto out;
12729 }
12730 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12731 if (cq->queue_id == 0xFFFF) {
12732 status = -ENXIO;
12733 goto out;
12734 }
12735 /* link the cq onto the parent eq child list */
12736 list_add_tail(&cq->list, &eq->child_list);
12737 /* Set up completion queue's type and subtype */
12738 cq->type = type;
12739 cq->subtype = subtype;
12740 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 12741 cq->assoc_qid = eq->queue_id;
4f774513
JS
12742 cq->host_index = 0;
12743 cq->hba_index = 0;
4f774513 12744
8fa38513
JS
12745out:
12746 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12747 return status;
12748}
12749
b19a061a
JS
12750/**
12751 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12752 * @phba: HBA structure that indicates port to create a queue on.
12753 * @mq: The queue structure to use to create the mailbox queue.
12754 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12755 * @cq: The completion queue to associate with this cq.
12756 *
12757 * This function provides failback (fb) functionality when the
12758 * mq_create_ext fails on older FW generations. It's purpose is identical
12759 * to mq_create_ext otherwise.
12760 *
12761 * This routine cannot fail as all attributes were previously accessed and
12762 * initialized in mq_create_ext.
12763 **/
12764static void
12765lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12766 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12767{
12768 struct lpfc_mbx_mq_create *mq_create;
12769 struct lpfc_dmabuf *dmabuf;
12770 int length;
12771
12772 length = (sizeof(struct lpfc_mbx_mq_create) -
12773 sizeof(struct lpfc_sli4_cfg_mhdr));
12774 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12775 LPFC_MBOX_OPCODE_MQ_CREATE,
12776 length, LPFC_SLI4_MBX_EMBED);
12777 mq_create = &mbox->u.mqe.un.mq_create;
12778 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12779 mq->page_count);
12780 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12781 cq->queue_id);
12782 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12783 switch (mq->entry_count) {
12784 case 16:
5a6f133e
JS
12785 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12786 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
12787 break;
12788 case 32:
5a6f133e
JS
12789 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12790 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
12791 break;
12792 case 64:
5a6f133e
JS
12793 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12794 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
12795 break;
12796 case 128:
5a6f133e
JS
12797 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12798 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
12799 break;
12800 }
12801 list_for_each_entry(dmabuf, &mq->page_list, list) {
12802 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12803 putPaddrLow(dmabuf->phys);
12804 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12805 putPaddrHigh(dmabuf->phys);
12806 }
12807}
12808
04c68496
JS
12809/**
12810 * lpfc_mq_create - Create a mailbox Queue on the HBA
12811 * @phba: HBA structure that indicates port to create a queue on.
12812 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
12813 * @cq: The completion queue to associate with this cq.
12814 * @subtype: The queue's subtype.
04c68496
JS
12815 *
12816 * This function creates a mailbox queue, as detailed in @mq, on a port,
12817 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12818 *
12819 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12820 * is used to get the entry count and entry size that are necessary to
12821 * determine the number of pages to allocate and use for this queue. This
12822 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12823 * mailbox queue. This function is asynchronous and will wait for the mailbox
12824 * command to finish before continuing.
12825 *
12826 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12827 * memory this function will return -ENOMEM. If the queue create mailbox command
12828 * fails this function will return -ENXIO.
04c68496 12829 **/
b19a061a 12830int32_t
04c68496
JS
12831lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12832 struct lpfc_queue *cq, uint32_t subtype)
12833{
12834 struct lpfc_mbx_mq_create *mq_create;
b19a061a 12835 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
12836 struct lpfc_dmabuf *dmabuf;
12837 LPFC_MBOXQ_t *mbox;
12838 int rc, length, status = 0;
12839 uint32_t shdr_status, shdr_add_status;
12840 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12841 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 12842
2e90f4b5
JS
12843 /* sanity check on queue memory */
12844 if (!mq || !cq)
12845 return -ENODEV;
49198b37
JS
12846 if (!phba->sli4_hba.pc_sli4_params.supported)
12847 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 12848
04c68496
JS
12849 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12850 if (!mbox)
12851 return -ENOMEM;
b19a061a 12852 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
12853 sizeof(struct lpfc_sli4_cfg_mhdr));
12854 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 12855 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 12856 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
12857
12858 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 12859 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
12860 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12861 &mq_create_ext->u.request, mq->page_count);
12862 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12863 &mq_create_ext->u.request, 1);
12864 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
12865 &mq_create_ext->u.request, 1);
12866 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12867 &mq_create_ext->u.request, 1);
70f3c073
JS
12868 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12869 &mq_create_ext->u.request, 1);
12870 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12871 &mq_create_ext->u.request, 1);
b19a061a 12872 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
12873 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12874 phba->sli4_hba.pc_sli4_params.mqv);
12875 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12876 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12877 cq->queue_id);
12878 else
12879 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12880 cq->queue_id);
04c68496
JS
12881 switch (mq->entry_count) {
12882 default:
12883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12884 "0362 Unsupported MQ count. (%d)\n",
12885 mq->entry_count);
4f4c1863
JS
12886 if (mq->entry_count < 16) {
12887 status = -EINVAL;
12888 goto out;
12889 }
04c68496
JS
12890 /* otherwise default to smallest count (drop through) */
12891 case 16:
5a6f133e
JS
12892 bf_set(lpfc_mq_context_ring_size,
12893 &mq_create_ext->u.request.context,
12894 LPFC_MQ_RING_SIZE_16);
04c68496
JS
12895 break;
12896 case 32:
5a6f133e
JS
12897 bf_set(lpfc_mq_context_ring_size,
12898 &mq_create_ext->u.request.context,
12899 LPFC_MQ_RING_SIZE_32);
04c68496
JS
12900 break;
12901 case 64:
5a6f133e
JS
12902 bf_set(lpfc_mq_context_ring_size,
12903 &mq_create_ext->u.request.context,
12904 LPFC_MQ_RING_SIZE_64);
04c68496
JS
12905 break;
12906 case 128:
5a6f133e
JS
12907 bf_set(lpfc_mq_context_ring_size,
12908 &mq_create_ext->u.request.context,
12909 LPFC_MQ_RING_SIZE_128);
04c68496
JS
12910 break;
12911 }
12912 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 12913 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 12914 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 12915 putPaddrLow(dmabuf->phys);
b19a061a 12916 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
12917 putPaddrHigh(dmabuf->phys);
12918 }
12919 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
12920 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12921 &mq_create_ext->u.response);
12922 if (rc != MBX_SUCCESS) {
12923 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12924 "2795 MQ_CREATE_EXT failed with "
12925 "status x%x. Failback to MQ_CREATE.\n",
12926 rc);
12927 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12928 mq_create = &mbox->u.mqe.un.mq_create;
12929 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12930 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12931 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12932 &mq_create->u.response);
12933 }
12934
04c68496 12935 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
12936 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12937 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12938 if (shdr_status || shdr_add_status || rc) {
12939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12940 "2502 MQ_CREATE mailbox failed with "
12941 "status x%x add_status x%x, mbx status x%x\n",
12942 shdr_status, shdr_add_status, rc);
12943 status = -ENXIO;
12944 goto out;
12945 }
04c68496
JS
12946 if (mq->queue_id == 0xFFFF) {
12947 status = -ENXIO;
12948 goto out;
12949 }
12950 mq->type = LPFC_MQ;
2a622bfb 12951 mq->assoc_qid = cq->queue_id;
04c68496
JS
12952 mq->subtype = subtype;
12953 mq->host_index = 0;
12954 mq->hba_index = 0;
12955
12956 /* link the mq onto the parent cq child list */
12957 list_add_tail(&mq->list, &cq->child_list);
12958out:
8fa38513 12959 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
12960 return status;
12961}
12962
4f774513
JS
12963/**
12964 * lpfc_wq_create - Create a Work Queue on the HBA
12965 * @phba: HBA structure that indicates port to create a queue on.
12966 * @wq: The queue structure to use to create the work queue.
12967 * @cq: The completion queue to bind this work queue to.
12968 * @subtype: The subtype of the work queue indicating its functionality.
12969 *
12970 * This function creates a work queue, as detailed in @wq, on a port, described
12971 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12972 *
12973 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12974 * is used to get the entry count and entry size that are necessary to
12975 * determine the number of pages to allocate and use for this queue. The @cq
12976 * is used to indicate which completion queue to bind this work queue to. This
12977 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12978 * work queue. This function is asynchronous and will wait for the mailbox
12979 * command to finish before continuing.
12980 *
12981 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12982 * memory this function will return -ENOMEM. If the queue create mailbox command
12983 * fails this function will return -ENXIO.
4f774513
JS
12984 **/
12985uint32_t
12986lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12987 struct lpfc_queue *cq, uint32_t subtype)
12988{
12989 struct lpfc_mbx_wq_create *wq_create;
12990 struct lpfc_dmabuf *dmabuf;
12991 LPFC_MBOXQ_t *mbox;
12992 int rc, length, status = 0;
12993 uint32_t shdr_status, shdr_add_status;
12994 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12995 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 12996 struct dma_address *page;
962bc51b
JS
12997 void __iomem *bar_memmap_p;
12998 uint32_t db_offset;
12999 uint16_t pci_barset;
49198b37 13000
2e90f4b5
JS
13001 /* sanity check on queue memory */
13002 if (!wq || !cq)
13003 return -ENODEV;
49198b37
JS
13004 if (!phba->sli4_hba.pc_sli4_params.supported)
13005 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13006
13007 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13008 if (!mbox)
13009 return -ENOMEM;
13010 length = (sizeof(struct lpfc_mbx_wq_create) -
13011 sizeof(struct lpfc_sli4_cfg_mhdr));
13012 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13013 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13014 length, LPFC_SLI4_MBX_EMBED);
13015 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 13016 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
13017 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13018 wq->page_count);
13019 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13020 cq->queue_id);
0c651878
JS
13021
13022 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
13023 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13024 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 13025
0c651878
JS
13026 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13027 case LPFC_Q_CREATE_VERSION_0:
13028 switch (wq->entry_size) {
13029 default:
13030 case 64:
13031 /* Nothing to do, version 0 ONLY supports 64 byte */
13032 page = wq_create->u.request.page;
13033 break;
13034 case 128:
13035 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13036 LPFC_WQ_SZ128_SUPPORT)) {
13037 status = -ERANGE;
13038 goto out;
13039 }
13040 /* If we get here the HBA MUST also support V1 and
13041 * we MUST use it
13042 */
13043 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13044 LPFC_Q_CREATE_VERSION_1);
13045
13046 bf_set(lpfc_mbx_wq_create_wqe_count,
13047 &wq_create->u.request_1, wq->entry_count);
13048 bf_set(lpfc_mbx_wq_create_wqe_size,
13049 &wq_create->u.request_1,
13050 LPFC_WQ_WQE_SIZE_128);
13051 bf_set(lpfc_mbx_wq_create_page_size,
13052 &wq_create->u.request_1,
13053 (PAGE_SIZE/SLI4_PAGE_SIZE));
13054 page = wq_create->u.request_1.page;
13055 break;
13056 }
13057 break;
13058 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
13059 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13060 wq->entry_count);
13061 switch (wq->entry_size) {
13062 default:
13063 case 64:
13064 bf_set(lpfc_mbx_wq_create_wqe_size,
13065 &wq_create->u.request_1,
13066 LPFC_WQ_WQE_SIZE_64);
13067 break;
13068 case 128:
0c651878
JS
13069 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13070 LPFC_WQ_SZ128_SUPPORT)) {
13071 status = -ERANGE;
13072 goto out;
13073 }
5a6f133e
JS
13074 bf_set(lpfc_mbx_wq_create_wqe_size,
13075 &wq_create->u.request_1,
13076 LPFC_WQ_WQE_SIZE_128);
13077 break;
13078 }
13079 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13080 (PAGE_SIZE/SLI4_PAGE_SIZE));
13081 page = wq_create->u.request_1.page;
0c651878
JS
13082 break;
13083 default:
13084 status = -ERANGE;
13085 goto out;
5a6f133e 13086 }
0c651878 13087
4f774513 13088 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 13089 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
13090 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13091 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 13092 }
962bc51b
JS
13093
13094 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13095 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13096
4f774513
JS
13097 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13098 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13099 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13100 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13101 if (shdr_status || shdr_add_status || rc) {
13102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13103 "2503 WQ_CREATE mailbox failed with "
13104 "status x%x add_status x%x, mbx status x%x\n",
13105 shdr_status, shdr_add_status, rc);
13106 status = -ENXIO;
13107 goto out;
13108 }
13109 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13110 if (wq->queue_id == 0xFFFF) {
13111 status = -ENXIO;
13112 goto out;
13113 }
962bc51b
JS
13114 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13115 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13116 &wq_create->u.response);
13117 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13118 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13120 "3265 WQ[%d] doorbell format not "
13121 "supported: x%x\n", wq->queue_id,
13122 wq->db_format);
13123 status = -EINVAL;
13124 goto out;
13125 }
13126 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13127 &wq_create->u.response);
13128 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13129 if (!bar_memmap_p) {
13130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13131 "3263 WQ[%d] failed to memmap pci "
13132 "barset:x%x\n", wq->queue_id,
13133 pci_barset);
13134 status = -ENOMEM;
13135 goto out;
13136 }
13137 db_offset = wq_create->u.response.doorbell_offset;
13138 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13139 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13141 "3252 WQ[%d] doorbell offset not "
13142 "supported: x%x\n", wq->queue_id,
13143 db_offset);
13144 status = -EINVAL;
13145 goto out;
13146 }
13147 wq->db_regaddr = bar_memmap_p + db_offset;
13148 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
13149 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13150 "format:x%x\n", wq->queue_id, pci_barset,
13151 db_offset, wq->db_format);
962bc51b
JS
13152 } else {
13153 wq->db_format = LPFC_DB_LIST_FORMAT;
13154 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13155 }
4f774513 13156 wq->type = LPFC_WQ;
2a622bfb 13157 wq->assoc_qid = cq->queue_id;
4f774513
JS
13158 wq->subtype = subtype;
13159 wq->host_index = 0;
13160 wq->hba_index = 0;
ff78d8f9 13161 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
13162
13163 /* link the wq onto the parent cq child list */
13164 list_add_tail(&wq->list, &cq->child_list);
13165out:
8fa38513 13166 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13167 return status;
13168}
13169
73d91e50
JS
13170/**
13171 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13172 * @phba: HBA structure that indicates port to create a queue on.
13173 * @rq: The queue structure to use for the receive queue.
13174 * @qno: The associated HBQ number
13175 *
13176 *
13177 * For SLI4 we need to adjust the RQ repost value based on
13178 * the number of buffers that are initially posted to the RQ.
13179 */
13180void
13181lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13182{
13183 uint32_t cnt;
13184
2e90f4b5
JS
13185 /* sanity check on queue memory */
13186 if (!rq)
13187 return;
73d91e50
JS
13188 cnt = lpfc_hbq_defs[qno]->entry_count;
13189
13190 /* Recalc repost for RQs based on buffers initially posted */
13191 cnt = (cnt >> 3);
13192 if (cnt < LPFC_QUEUE_MIN_REPOST)
13193 cnt = LPFC_QUEUE_MIN_REPOST;
13194
13195 rq->entry_repost = cnt;
13196}
13197
4f774513
JS
13198/**
13199 * lpfc_rq_create - Create a Receive Queue on the HBA
13200 * @phba: HBA structure that indicates port to create a queue on.
13201 * @hrq: The queue structure to use to create the header receive queue.
13202 * @drq: The queue structure to use to create the data receive queue.
13203 * @cq: The completion queue to bind this work queue to.
13204 *
13205 * This function creates a receive buffer queue pair , as detailed in @hrq and
13206 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13207 * to the HBA.
13208 *
13209 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13210 * struct is used to get the entry count that is necessary to determine the
13211 * number of pages to use for this queue. The @cq is used to indicate which
13212 * completion queue to bind received buffers that are posted to these queues to.
13213 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13214 * receive queue pair. This function is asynchronous and will wait for the
13215 * mailbox command to finish before continuing.
13216 *
13217 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13218 * memory this function will return -ENOMEM. If the queue create mailbox command
13219 * fails this function will return -ENXIO.
4f774513
JS
13220 **/
13221uint32_t
13222lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13223 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13224{
13225 struct lpfc_mbx_rq_create *rq_create;
13226 struct lpfc_dmabuf *dmabuf;
13227 LPFC_MBOXQ_t *mbox;
13228 int rc, length, status = 0;
13229 uint32_t shdr_status, shdr_add_status;
13230 union lpfc_sli4_cfg_shdr *shdr;
49198b37 13231 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
13232 void __iomem *bar_memmap_p;
13233 uint32_t db_offset;
13234 uint16_t pci_barset;
49198b37 13235
2e90f4b5
JS
13236 /* sanity check on queue memory */
13237 if (!hrq || !drq || !cq)
13238 return -ENODEV;
49198b37
JS
13239 if (!phba->sli4_hba.pc_sli4_params.supported)
13240 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13241
13242 if (hrq->entry_count != drq->entry_count)
13243 return -EINVAL;
13244 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13245 if (!mbox)
13246 return -ENOMEM;
13247 length = (sizeof(struct lpfc_mbx_rq_create) -
13248 sizeof(struct lpfc_sli4_cfg_mhdr));
13249 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13250 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13251 length, LPFC_SLI4_MBX_EMBED);
13252 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
13253 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13254 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13255 phba->sli4_hba.pc_sli4_params.rqv);
13256 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13257 bf_set(lpfc_rq_context_rqe_count_1,
13258 &rq_create->u.request.context,
13259 hrq->entry_count);
13260 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
13261 bf_set(lpfc_rq_context_rqe_size,
13262 &rq_create->u.request.context,
13263 LPFC_RQE_SIZE_8);
13264 bf_set(lpfc_rq_context_page_size,
13265 &rq_create->u.request.context,
13266 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
13267 } else {
13268 switch (hrq->entry_count) {
13269 default:
13270 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13271 "2535 Unsupported RQ count. (%d)\n",
13272 hrq->entry_count);
4f4c1863
JS
13273 if (hrq->entry_count < 512) {
13274 status = -EINVAL;
13275 goto out;
13276 }
5a6f133e
JS
13277 /* otherwise default to smallest count (drop through) */
13278 case 512:
13279 bf_set(lpfc_rq_context_rqe_count,
13280 &rq_create->u.request.context,
13281 LPFC_RQ_RING_SIZE_512);
13282 break;
13283 case 1024:
13284 bf_set(lpfc_rq_context_rqe_count,
13285 &rq_create->u.request.context,
13286 LPFC_RQ_RING_SIZE_1024);
13287 break;
13288 case 2048:
13289 bf_set(lpfc_rq_context_rqe_count,
13290 &rq_create->u.request.context,
13291 LPFC_RQ_RING_SIZE_2048);
13292 break;
13293 case 4096:
13294 bf_set(lpfc_rq_context_rqe_count,
13295 &rq_create->u.request.context,
13296 LPFC_RQ_RING_SIZE_4096);
13297 break;
13298 }
13299 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13300 LPFC_HDR_BUF_SIZE);
4f774513
JS
13301 }
13302 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13303 cq->queue_id);
13304 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13305 hrq->page_count);
4f774513 13306 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 13307 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13308 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13309 putPaddrLow(dmabuf->phys);
13310 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13311 putPaddrHigh(dmabuf->phys);
13312 }
962bc51b
JS
13313 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13314 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13315
4f774513
JS
13316 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13317 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
13318 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13319 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13320 if (shdr_status || shdr_add_status || rc) {
13321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13322 "2504 RQ_CREATE mailbox failed with "
13323 "status x%x add_status x%x, mbx status x%x\n",
13324 shdr_status, shdr_add_status, rc);
13325 status = -ENXIO;
13326 goto out;
13327 }
13328 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13329 if (hrq->queue_id == 0xFFFF) {
13330 status = -ENXIO;
13331 goto out;
13332 }
962bc51b
JS
13333
13334 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13335 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13336 &rq_create->u.response);
13337 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13338 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13340 "3262 RQ [%d] doorbell format not "
13341 "supported: x%x\n", hrq->queue_id,
13342 hrq->db_format);
13343 status = -EINVAL;
13344 goto out;
13345 }
13346
13347 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13348 &rq_create->u.response);
13349 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13350 if (!bar_memmap_p) {
13351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13352 "3269 RQ[%d] failed to memmap pci "
13353 "barset:x%x\n", hrq->queue_id,
13354 pci_barset);
13355 status = -ENOMEM;
13356 goto out;
13357 }
13358
13359 db_offset = rq_create->u.response.doorbell_offset;
13360 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13361 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13363 "3270 RQ[%d] doorbell offset not "
13364 "supported: x%x\n", hrq->queue_id,
13365 db_offset);
13366 status = -EINVAL;
13367 goto out;
13368 }
13369 hrq->db_regaddr = bar_memmap_p + db_offset;
13370 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
13371 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
13372 "format:x%x\n", hrq->queue_id, pci_barset,
13373 db_offset, hrq->db_format);
962bc51b
JS
13374 } else {
13375 hrq->db_format = LPFC_DB_RING_FORMAT;
13376 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13377 }
4f774513 13378 hrq->type = LPFC_HRQ;
2a622bfb 13379 hrq->assoc_qid = cq->queue_id;
4f774513
JS
13380 hrq->subtype = subtype;
13381 hrq->host_index = 0;
13382 hrq->hba_index = 0;
13383
13384 /* now create the data queue */
13385 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13386 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13387 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
13388 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13389 phba->sli4_hba.pc_sli4_params.rqv);
13390 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13391 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 13392 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 13393 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
13394 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13395 LPFC_RQE_SIZE_8);
13396 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13397 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
13398 } else {
13399 switch (drq->entry_count) {
13400 default:
13401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13402 "2536 Unsupported RQ count. (%d)\n",
13403 drq->entry_count);
4f4c1863
JS
13404 if (drq->entry_count < 512) {
13405 status = -EINVAL;
13406 goto out;
13407 }
5a6f133e
JS
13408 /* otherwise default to smallest count (drop through) */
13409 case 512:
13410 bf_set(lpfc_rq_context_rqe_count,
13411 &rq_create->u.request.context,
13412 LPFC_RQ_RING_SIZE_512);
13413 break;
13414 case 1024:
13415 bf_set(lpfc_rq_context_rqe_count,
13416 &rq_create->u.request.context,
13417 LPFC_RQ_RING_SIZE_1024);
13418 break;
13419 case 2048:
13420 bf_set(lpfc_rq_context_rqe_count,
13421 &rq_create->u.request.context,
13422 LPFC_RQ_RING_SIZE_2048);
13423 break;
13424 case 4096:
13425 bf_set(lpfc_rq_context_rqe_count,
13426 &rq_create->u.request.context,
13427 LPFC_RQ_RING_SIZE_4096);
13428 break;
13429 }
13430 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13431 LPFC_DATA_BUF_SIZE);
4f774513
JS
13432 }
13433 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13434 cq->queue_id);
13435 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13436 drq->page_count);
4f774513
JS
13437 list_for_each_entry(dmabuf, &drq->page_list, list) {
13438 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13439 putPaddrLow(dmabuf->phys);
13440 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13441 putPaddrHigh(dmabuf->phys);
13442 }
962bc51b
JS
13443 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13444 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
13445 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13446 /* The IOCTL status is embedded in the mailbox subheader. */
13447 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13448 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13449 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13450 if (shdr_status || shdr_add_status || rc) {
13451 status = -ENXIO;
13452 goto out;
13453 }
13454 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13455 if (drq->queue_id == 0xFFFF) {
13456 status = -ENXIO;
13457 goto out;
13458 }
13459 drq->type = LPFC_DRQ;
2a622bfb 13460 drq->assoc_qid = cq->queue_id;
4f774513
JS
13461 drq->subtype = subtype;
13462 drq->host_index = 0;
13463 drq->hba_index = 0;
13464
13465 /* link the header and data RQs onto the parent cq child list */
13466 list_add_tail(&hrq->list, &cq->child_list);
13467 list_add_tail(&drq->list, &cq->child_list);
13468
13469out:
8fa38513 13470 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
13471 return status;
13472}
13473
13474/**
13475 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13476 * @eq: The queue structure associated with the queue to destroy.
13477 *
13478 * This function destroys a queue, as detailed in @eq by sending an mailbox
13479 * command, specific to the type of queue, to the HBA.
13480 *
13481 * The @eq struct is used to get the queue ID of the queue to destroy.
13482 *
13483 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13484 * command fails this function will return -ENXIO.
4f774513
JS
13485 **/
13486uint32_t
13487lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13488{
13489 LPFC_MBOXQ_t *mbox;
13490 int rc, length, status = 0;
13491 uint32_t shdr_status, shdr_add_status;
13492 union lpfc_sli4_cfg_shdr *shdr;
13493
2e90f4b5 13494 /* sanity check on queue memory */
4f774513
JS
13495 if (!eq)
13496 return -ENODEV;
13497 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13498 if (!mbox)
13499 return -ENOMEM;
13500 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13501 sizeof(struct lpfc_sli4_cfg_mhdr));
13502 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13503 LPFC_MBOX_OPCODE_EQ_DESTROY,
13504 length, LPFC_SLI4_MBX_EMBED);
13505 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13506 eq->queue_id);
13507 mbox->vport = eq->phba->pport;
13508 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13509
13510 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13511 /* The IOCTL status is embedded in the mailbox subheader. */
13512 shdr = (union lpfc_sli4_cfg_shdr *)
13513 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13514 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13515 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13516 if (shdr_status || shdr_add_status || rc) {
13517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13518 "2505 EQ_DESTROY mailbox failed with "
13519 "status x%x add_status x%x, mbx status x%x\n",
13520 shdr_status, shdr_add_status, rc);
13521 status = -ENXIO;
13522 }
13523
13524 /* Remove eq from any list */
13525 list_del_init(&eq->list);
8fa38513 13526 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
13527 return status;
13528}
13529
13530/**
13531 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13532 * @cq: The queue structure associated with the queue to destroy.
13533 *
13534 * This function destroys a queue, as detailed in @cq by sending an mailbox
13535 * command, specific to the type of queue, to the HBA.
13536 *
13537 * The @cq struct is used to get the queue ID of the queue to destroy.
13538 *
13539 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13540 * command fails this function will return -ENXIO.
4f774513
JS
13541 **/
13542uint32_t
13543lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13544{
13545 LPFC_MBOXQ_t *mbox;
13546 int rc, length, status = 0;
13547 uint32_t shdr_status, shdr_add_status;
13548 union lpfc_sli4_cfg_shdr *shdr;
13549
2e90f4b5 13550 /* sanity check on queue memory */
4f774513
JS
13551 if (!cq)
13552 return -ENODEV;
13553 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13554 if (!mbox)
13555 return -ENOMEM;
13556 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13557 sizeof(struct lpfc_sli4_cfg_mhdr));
13558 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13559 LPFC_MBOX_OPCODE_CQ_DESTROY,
13560 length, LPFC_SLI4_MBX_EMBED);
13561 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13562 cq->queue_id);
13563 mbox->vport = cq->phba->pport;
13564 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13565 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13566 /* The IOCTL status is embedded in the mailbox subheader. */
13567 shdr = (union lpfc_sli4_cfg_shdr *)
13568 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13569 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13570 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13571 if (shdr_status || shdr_add_status || rc) {
13572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13573 "2506 CQ_DESTROY mailbox failed with "
13574 "status x%x add_status x%x, mbx status x%x\n",
13575 shdr_status, shdr_add_status, rc);
13576 status = -ENXIO;
13577 }
13578 /* Remove cq from any list */
13579 list_del_init(&cq->list);
8fa38513 13580 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
13581 return status;
13582}
13583
04c68496
JS
13584/**
13585 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13586 * @qm: The queue structure associated with the queue to destroy.
13587 *
13588 * This function destroys a queue, as detailed in @mq by sending an mailbox
13589 * command, specific to the type of queue, to the HBA.
13590 *
13591 * The @mq struct is used to get the queue ID of the queue to destroy.
13592 *
13593 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13594 * command fails this function will return -ENXIO.
04c68496
JS
13595 **/
13596uint32_t
13597lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13598{
13599 LPFC_MBOXQ_t *mbox;
13600 int rc, length, status = 0;
13601 uint32_t shdr_status, shdr_add_status;
13602 union lpfc_sli4_cfg_shdr *shdr;
13603
2e90f4b5 13604 /* sanity check on queue memory */
04c68496
JS
13605 if (!mq)
13606 return -ENODEV;
13607 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13608 if (!mbox)
13609 return -ENOMEM;
13610 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13611 sizeof(struct lpfc_sli4_cfg_mhdr));
13612 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13613 LPFC_MBOX_OPCODE_MQ_DESTROY,
13614 length, LPFC_SLI4_MBX_EMBED);
13615 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13616 mq->queue_id);
13617 mbox->vport = mq->phba->pport;
13618 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13619 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13620 /* The IOCTL status is embedded in the mailbox subheader. */
13621 shdr = (union lpfc_sli4_cfg_shdr *)
13622 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13623 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13624 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13625 if (shdr_status || shdr_add_status || rc) {
13626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13627 "2507 MQ_DESTROY mailbox failed with "
13628 "status x%x add_status x%x, mbx status x%x\n",
13629 shdr_status, shdr_add_status, rc);
13630 status = -ENXIO;
13631 }
13632 /* Remove mq from any list */
13633 list_del_init(&mq->list);
8fa38513 13634 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
13635 return status;
13636}
13637
4f774513
JS
13638/**
13639 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13640 * @wq: The queue structure associated with the queue to destroy.
13641 *
13642 * This function destroys a queue, as detailed in @wq by sending an mailbox
13643 * command, specific to the type of queue, to the HBA.
13644 *
13645 * The @wq struct is used to get the queue ID of the queue to destroy.
13646 *
13647 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13648 * command fails this function will return -ENXIO.
4f774513
JS
13649 **/
13650uint32_t
13651lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13652{
13653 LPFC_MBOXQ_t *mbox;
13654 int rc, length, status = 0;
13655 uint32_t shdr_status, shdr_add_status;
13656 union lpfc_sli4_cfg_shdr *shdr;
13657
2e90f4b5 13658 /* sanity check on queue memory */
4f774513
JS
13659 if (!wq)
13660 return -ENODEV;
13661 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13662 if (!mbox)
13663 return -ENOMEM;
13664 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13665 sizeof(struct lpfc_sli4_cfg_mhdr));
13666 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13667 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13668 length, LPFC_SLI4_MBX_EMBED);
13669 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13670 wq->queue_id);
13671 mbox->vport = wq->phba->pport;
13672 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13673 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13674 shdr = (union lpfc_sli4_cfg_shdr *)
13675 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13676 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13677 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13678 if (shdr_status || shdr_add_status || rc) {
13679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13680 "2508 WQ_DESTROY mailbox failed with "
13681 "status x%x add_status x%x, mbx status x%x\n",
13682 shdr_status, shdr_add_status, rc);
13683 status = -ENXIO;
13684 }
13685 /* Remove wq from any list */
13686 list_del_init(&wq->list);
8fa38513 13687 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
13688 return status;
13689}
13690
13691/**
13692 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13693 * @rq: The queue structure associated with the queue to destroy.
13694 *
13695 * This function destroys a queue, as detailed in @rq by sending an mailbox
13696 * command, specific to the type of queue, to the HBA.
13697 *
13698 * The @rq struct is used to get the queue ID of the queue to destroy.
13699 *
13700 * On success this function will return a zero. If the queue destroy mailbox
d439d286 13701 * command fails this function will return -ENXIO.
4f774513
JS
13702 **/
13703uint32_t
13704lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13705 struct lpfc_queue *drq)
13706{
13707 LPFC_MBOXQ_t *mbox;
13708 int rc, length, status = 0;
13709 uint32_t shdr_status, shdr_add_status;
13710 union lpfc_sli4_cfg_shdr *shdr;
13711
2e90f4b5 13712 /* sanity check on queue memory */
4f774513
JS
13713 if (!hrq || !drq)
13714 return -ENODEV;
13715 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13716 if (!mbox)
13717 return -ENOMEM;
13718 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 13719 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
13720 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13721 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13722 length, LPFC_SLI4_MBX_EMBED);
13723 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13724 hrq->queue_id);
13725 mbox->vport = hrq->phba->pport;
13726 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13727 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13728 /* The IOCTL status is embedded in the mailbox subheader. */
13729 shdr = (union lpfc_sli4_cfg_shdr *)
13730 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13731 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13732 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13733 if (shdr_status || shdr_add_status || rc) {
13734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13735 "2509 RQ_DESTROY mailbox failed with "
13736 "status x%x add_status x%x, mbx status x%x\n",
13737 shdr_status, shdr_add_status, rc);
13738 if (rc != MBX_TIMEOUT)
13739 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13740 return -ENXIO;
13741 }
13742 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13743 drq->queue_id);
13744 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13745 shdr = (union lpfc_sli4_cfg_shdr *)
13746 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13747 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13748 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13749 if (shdr_status || shdr_add_status || rc) {
13750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13751 "2510 RQ_DESTROY mailbox failed with "
13752 "status x%x add_status x%x, mbx status x%x\n",
13753 shdr_status, shdr_add_status, rc);
13754 status = -ENXIO;
13755 }
13756 list_del_init(&hrq->list);
13757 list_del_init(&drq->list);
8fa38513 13758 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
13759 return status;
13760}
13761
13762/**
13763 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13764 * @phba: The virtual port for which this call being executed.
13765 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13766 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13767 * @xritag: the xritag that ties this io to the SGL pages.
13768 *
13769 * This routine will post the sgl pages for the IO that has the xritag
13770 * that is in the iocbq structure. The xritag is assigned during iocbq
13771 * creation and persists for as long as the driver is loaded.
13772 * if the caller has fewer than 256 scatter gather segments to map then
13773 * pdma_phys_addr1 should be 0.
13774 * If the caller needs to map more than 256 scatter gather segment then
13775 * pdma_phys_addr1 should be a valid physical address.
13776 * physical address for SGLs must be 64 byte aligned.
13777 * If you are going to map 2 SGL's then the first one must have 256 entries
13778 * the second sgl can have between 1 and 256 entries.
13779 *
13780 * Return codes:
13781 * 0 - Success
13782 * -ENXIO, -ENOMEM - Failure
13783 **/
13784int
13785lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13786 dma_addr_t pdma_phys_addr0,
13787 dma_addr_t pdma_phys_addr1,
13788 uint16_t xritag)
13789{
13790 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13791 LPFC_MBOXQ_t *mbox;
13792 int rc;
13793 uint32_t shdr_status, shdr_add_status;
6d368e53 13794 uint32_t mbox_tmo;
4f774513
JS
13795 union lpfc_sli4_cfg_shdr *shdr;
13796
13797 if (xritag == NO_XRI) {
13798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13799 "0364 Invalid param:\n");
13800 return -EINVAL;
13801 }
13802
13803 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13804 if (!mbox)
13805 return -ENOMEM;
13806
13807 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13808 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13809 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 13810 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
13811
13812 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13813 &mbox->u.mqe.un.post_sgl_pages;
13814 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13815 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13816
13817 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13818 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13819 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13820 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13821
13822 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13823 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13824 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13825 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13826 if (!phba->sli4_hba.intr_enable)
13827 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 13828 else {
a183a15f 13829 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
13830 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13831 }
4f774513
JS
13832 /* The IOCTL status is embedded in the mailbox subheader. */
13833 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13834 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13835 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13836 if (rc != MBX_TIMEOUT)
13837 mempool_free(mbox, phba->mbox_mem_pool);
13838 if (shdr_status || shdr_add_status || rc) {
13839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13840 "2511 POST_SGL mailbox failed with "
13841 "status x%x add_status x%x, mbx status x%x\n",
13842 shdr_status, shdr_add_status, rc);
13843 rc = -ENXIO;
13844 }
13845 return 0;
13846}
4f774513 13847
6d368e53 13848/**
88a2cfbb 13849 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
13850 * @phba: pointer to lpfc hba data structure.
13851 *
13852 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
13853 * HBA consistent with the SLI-4 interface spec. This routine
13854 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13855 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 13856 *
88a2cfbb
JS
13857 * Returns
13858 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13859 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13860 **/
6d368e53
JS
13861uint16_t
13862lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13863{
13864 unsigned long xri;
13865
13866 /*
13867 * Fetch the next logical xri. Because this index is logical,
13868 * the driver starts at 0 each time.
13869 */
13870 spin_lock_irq(&phba->hbalock);
13871 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13872 phba->sli4_hba.max_cfg_param.max_xri, 0);
13873 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13874 spin_unlock_irq(&phba->hbalock);
13875 return NO_XRI;
13876 } else {
13877 set_bit(xri, phba->sli4_hba.xri_bmask);
13878 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 13879 }
6d368e53
JS
13880 spin_unlock_irq(&phba->hbalock);
13881 return xri;
13882}
13883
13884/**
13885 * lpfc_sli4_free_xri - Release an xri for reuse.
13886 * @phba: pointer to lpfc hba data structure.
13887 *
13888 * This routine is invoked to release an xri to the pool of
13889 * available rpis maintained by the driver.
13890 **/
13891void
13892__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13893{
13894 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
13895 phba->sli4_hba.max_cfg_param.xri_used--;
13896 }
13897}
13898
13899/**
13900 * lpfc_sli4_free_xri - Release an xri for reuse.
13901 * @phba: pointer to lpfc hba data structure.
13902 *
13903 * This routine is invoked to release an xri to the pool of
13904 * available rpis maintained by the driver.
13905 **/
13906void
13907lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13908{
13909 spin_lock_irq(&phba->hbalock);
13910 __lpfc_sli4_free_xri(phba, xri);
13911 spin_unlock_irq(&phba->hbalock);
13912}
13913
4f774513
JS
13914/**
13915 * lpfc_sli4_next_xritag - Get an xritag for the io
13916 * @phba: Pointer to HBA context object.
13917 *
13918 * This function gets an xritag for the iocb. If there is no unused xritag
13919 * it will return 0xffff.
13920 * The function returns the allocated xritag if successful, else returns zero.
13921 * Zero is not a valid xritag.
13922 * The caller is not required to hold any lock.
13923 **/
13924uint16_t
13925lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13926{
6d368e53 13927 uint16_t xri_index;
4f774513 13928
6d368e53 13929 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
13930 if (xri_index == NO_XRI)
13931 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13932 "2004 Failed to allocate XRI.last XRITAG is %d"
13933 " Max XRI is %d, Used XRI is %d\n",
13934 xri_index,
13935 phba->sli4_hba.max_cfg_param.max_xri,
13936 phba->sli4_hba.max_cfg_param.xri_used);
13937 return xri_index;
4f774513
JS
13938}
13939
13940/**
6d368e53 13941 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
4f774513 13942 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
13943 * @post_sgl_list: pointer to els sgl entry list.
13944 * @count: number of els sgl entries on the list.
4f774513
JS
13945 *
13946 * This routine is invoked to post a block of driver's sgl pages to the
13947 * HBA using non-embedded mailbox command. No Lock is held. This routine
13948 * is only called when the driver is loading and after all IO has been
13949 * stopped.
13950 **/
8a9d2e80
JS
13951static int
13952lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13953 struct list_head *post_sgl_list,
13954 int post_cnt)
4f774513 13955{
8a9d2e80 13956 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
13957 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13958 struct sgl_page_pairs *sgl_pg_pairs;
13959 void *viraddr;
13960 LPFC_MBOXQ_t *mbox;
13961 uint32_t reqlen, alloclen, pg_pairs;
13962 uint32_t mbox_tmo;
8a9d2e80
JS
13963 uint16_t xritag_start = 0;
13964 int rc = 0;
4f774513
JS
13965 uint32_t shdr_status, shdr_add_status;
13966 union lpfc_sli4_cfg_shdr *shdr;
13967
8a9d2e80 13968 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
4f774513 13969 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 13970 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
13971 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13972 "2559 Block sgl registration required DMA "
13973 "size (%d) great than a page\n", reqlen);
13974 return -ENOMEM;
13975 }
13976 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 13977 if (!mbox)
4f774513 13978 return -ENOMEM;
4f774513
JS
13979
13980 /* Allocate DMA memory and set up the non-embedded mailbox command */
13981 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13982 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13983 LPFC_SLI4_MBX_NEMBED);
13984
13985 if (alloclen < reqlen) {
13986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13987 "0285 Allocated DMA memory size (%d) is "
13988 "less than the requested DMA memory "
13989 "size (%d)\n", alloclen, reqlen);
13990 lpfc_sli4_mbox_cmd_free(phba, mbox);
13991 return -ENOMEM;
13992 }
4f774513 13993 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 13994 viraddr = mbox->sge_array->addr[0];
4f774513
JS
13995 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13996 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13997
8a9d2e80
JS
13998 pg_pairs = 0;
13999 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
14000 /* Set up the sge entry */
14001 sgl_pg_pairs->sgl_pg0_addr_lo =
14002 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14003 sgl_pg_pairs->sgl_pg0_addr_hi =
14004 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14005 sgl_pg_pairs->sgl_pg1_addr_lo =
14006 cpu_to_le32(putPaddrLow(0));
14007 sgl_pg_pairs->sgl_pg1_addr_hi =
14008 cpu_to_le32(putPaddrHigh(0));
6d368e53 14009
4f774513
JS
14010 /* Keep the first xritag on the list */
14011 if (pg_pairs == 0)
14012 xritag_start = sglq_entry->sli4_xritag;
14013 sgl_pg_pairs++;
8a9d2e80 14014 pg_pairs++;
4f774513 14015 }
6d368e53
JS
14016
14017 /* Complete initialization and perform endian conversion. */
4f774513 14018 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
8a9d2e80 14019 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
4f774513 14020 sgl->word0 = cpu_to_le32(sgl->word0);
4f774513
JS
14021 if (!phba->sli4_hba.intr_enable)
14022 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14023 else {
a183a15f 14024 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
14025 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14026 }
14027 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14028 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14029 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14030 if (rc != MBX_TIMEOUT)
14031 lpfc_sli4_mbox_cmd_free(phba, mbox);
14032 if (shdr_status || shdr_add_status || rc) {
14033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14034 "2513 POST_SGL_BLOCK mailbox command failed "
14035 "status x%x add_status x%x mbx status x%x\n",
14036 shdr_status, shdr_add_status, rc);
14037 rc = -ENXIO;
14038 }
14039 return rc;
14040}
14041
14042/**
14043 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14044 * @phba: pointer to lpfc hba data structure.
14045 * @sblist: pointer to scsi buffer list.
14046 * @count: number of scsi buffers on the list.
14047 *
14048 * This routine is invoked to post a block of @count scsi sgl pages from a
14049 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14050 * No Lock is held.
14051 *
14052 **/
14053int
8a9d2e80
JS
14054lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14055 struct list_head *sblist,
14056 int count)
4f774513
JS
14057{
14058 struct lpfc_scsi_buf *psb;
14059 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14060 struct sgl_page_pairs *sgl_pg_pairs;
14061 void *viraddr;
14062 LPFC_MBOXQ_t *mbox;
14063 uint32_t reqlen, alloclen, pg_pairs;
14064 uint32_t mbox_tmo;
14065 uint16_t xritag_start = 0;
14066 int rc = 0;
14067 uint32_t shdr_status, shdr_add_status;
14068 dma_addr_t pdma_phys_bpl1;
14069 union lpfc_sli4_cfg_shdr *shdr;
14070
14071 /* Calculate the requested length of the dma memory */
8a9d2e80 14072 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 14073 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 14074 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
14075 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14076 "0217 Block sgl registration required DMA "
14077 "size (%d) great than a page\n", reqlen);
14078 return -ENOMEM;
14079 }
14080 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14081 if (!mbox) {
14082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14083 "0283 Failed to allocate mbox cmd memory\n");
14084 return -ENOMEM;
14085 }
14086
14087 /* Allocate DMA memory and set up the non-embedded mailbox command */
14088 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14089 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14090 LPFC_SLI4_MBX_NEMBED);
14091
14092 if (alloclen < reqlen) {
14093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14094 "2561 Allocated DMA memory size (%d) is "
14095 "less than the requested DMA memory "
14096 "size (%d)\n", alloclen, reqlen);
14097 lpfc_sli4_mbox_cmd_free(phba, mbox);
14098 return -ENOMEM;
14099 }
6d368e53 14100
4f774513 14101 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
14102 viraddr = mbox->sge_array->addr[0];
14103
14104 /* Set up the SGL pages in the non-embedded DMA pages */
14105 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14106 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14107
14108 pg_pairs = 0;
14109 list_for_each_entry(psb, sblist, list) {
14110 /* Set up the sge entry */
14111 sgl_pg_pairs->sgl_pg0_addr_lo =
14112 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14113 sgl_pg_pairs->sgl_pg0_addr_hi =
14114 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14115 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14116 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14117 else
14118 pdma_phys_bpl1 = 0;
14119 sgl_pg_pairs->sgl_pg1_addr_lo =
14120 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14121 sgl_pg_pairs->sgl_pg1_addr_hi =
14122 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14123 /* Keep the first xritag on the list */
14124 if (pg_pairs == 0)
14125 xritag_start = psb->cur_iocbq.sli4_xritag;
14126 sgl_pg_pairs++;
14127 pg_pairs++;
14128 }
14129 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14130 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14131 /* Perform endian conversion if necessary */
14132 sgl->word0 = cpu_to_le32(sgl->word0);
14133
14134 if (!phba->sli4_hba.intr_enable)
14135 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14136 else {
a183a15f 14137 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
14138 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14139 }
14140 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14141 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14143 if (rc != MBX_TIMEOUT)
14144 lpfc_sli4_mbox_cmd_free(phba, mbox);
14145 if (shdr_status || shdr_add_status || rc) {
14146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14147 "2564 POST_SGL_BLOCK mailbox command failed "
14148 "status x%x add_status x%x mbx status x%x\n",
14149 shdr_status, shdr_add_status, rc);
14150 rc = -ENXIO;
14151 }
14152 return rc;
14153}
14154
14155/**
14156 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14157 * @phba: pointer to lpfc_hba struct that the frame was received on
14158 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14159 *
14160 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14161 * valid type of frame that the LPFC driver will handle. This function will
14162 * return a zero if the frame is a valid frame or a non zero value when the
14163 * frame does not pass the check.
14164 **/
14165static int
14166lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14167{
474ffb74
TH
14168 /* make rctl_names static to save stack space */
14169 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
4f774513
JS
14170 char *type_names[] = FC_TYPE_NAMES_INIT;
14171 struct fc_vft_header *fc_vft_hdr;
546fc854 14172 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
14173
14174 switch (fc_hdr->fh_r_ctl) {
14175 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14176 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14177 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14178 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14179 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14180 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14181 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14182 case FC_RCTL_DD_CMD_STATUS: /* command status */
14183 case FC_RCTL_ELS_REQ: /* extended link services request */
14184 case FC_RCTL_ELS_REP: /* extended link services reply */
14185 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14186 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14187 case FC_RCTL_BA_NOP: /* basic link service NOP */
14188 case FC_RCTL_BA_ABTS: /* basic link service abort */
14189 case FC_RCTL_BA_RMC: /* remove connection */
14190 case FC_RCTL_BA_ACC: /* basic accept */
14191 case FC_RCTL_BA_RJT: /* basic reject */
14192 case FC_RCTL_BA_PRMT:
14193 case FC_RCTL_ACK_1: /* acknowledge_1 */
14194 case FC_RCTL_ACK_0: /* acknowledge_0 */
14195 case FC_RCTL_P_RJT: /* port reject */
14196 case FC_RCTL_F_RJT: /* fabric reject */
14197 case FC_RCTL_P_BSY: /* port busy */
14198 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14199 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14200 case FC_RCTL_LCR: /* link credit reset */
14201 case FC_RCTL_END: /* end */
14202 break;
14203 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14204 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14205 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14206 return lpfc_fc_frame_check(phba, fc_hdr);
14207 default:
14208 goto drop;
14209 }
14210 switch (fc_hdr->fh_type) {
14211 case FC_TYPE_BLS:
14212 case FC_TYPE_ELS:
14213 case FC_TYPE_FCP:
14214 case FC_TYPE_CT:
14215 break;
14216 case FC_TYPE_IP:
14217 case FC_TYPE_ILS:
14218 default:
14219 goto drop;
14220 }
546fc854 14221
4f774513 14222 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
88f43a08
JS
14223 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14224 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14225 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14226 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
546fc854
JS
14227 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14228 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
88f43a08
JS
14229 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14230 be32_to_cpu(header[6]));
4f774513
JS
14231 return 0;
14232drop:
14233 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14234 "2539 Dropped frame rctl:%s type:%s\n",
14235 rctl_names[fc_hdr->fh_r_ctl],
14236 type_names[fc_hdr->fh_type]);
14237 return 1;
14238}
14239
14240/**
14241 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14242 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14243 *
14244 * This function processes the FC header to retrieve the VFI from the VF
14245 * header, if one exists. This function will return the VFI if one exists
14246 * or 0 if no VSAN Header exists.
14247 **/
14248static uint32_t
14249lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14250{
14251 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14252
14253 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14254 return 0;
14255 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14256}
14257
14258/**
14259 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14260 * @phba: Pointer to the HBA structure to search for the vport on
14261 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14262 * @fcfi: The FC Fabric ID that the frame came from
14263 *
14264 * This function searches the @phba for a vport that matches the content of the
14265 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14266 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14267 * returns the matching vport pointer or NULL if unable to match frame to a
14268 * vport.
14269 **/
14270static struct lpfc_vport *
14271lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14272 uint16_t fcfi)
14273{
14274 struct lpfc_vport **vports;
14275 struct lpfc_vport *vport = NULL;
14276 int i;
14277 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14278 fc_hdr->fh_d_id[1] << 8 |
14279 fc_hdr->fh_d_id[2]);
939723a4 14280
bf08611b
JS
14281 if (did == Fabric_DID)
14282 return phba->pport;
939723a4
JS
14283 if ((phba->pport->fc_flag & FC_PT2PT) &&
14284 !(phba->link_state == LPFC_HBA_READY))
14285 return phba->pport;
14286
4f774513
JS
14287 vports = lpfc_create_vport_work_array(phba);
14288 if (vports != NULL)
14289 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14290 if (phba->fcf.fcfi == fcfi &&
14291 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14292 vports[i]->fc_myDID == did) {
14293 vport = vports[i];
14294 break;
14295 }
14296 }
14297 lpfc_destroy_vport_work_array(phba, vports);
14298 return vport;
14299}
14300
45ed1190
JS
14301/**
14302 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14303 * @vport: The vport to work on.
14304 *
14305 * This function updates the receive sequence time stamp for this vport. The
14306 * receive sequence time stamp indicates the time that the last frame of the
14307 * the sequence that has been idle for the longest amount of time was received.
14308 * the driver uses this time stamp to indicate if any received sequences have
14309 * timed out.
14310 **/
14311void
14312lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14313{
14314 struct lpfc_dmabuf *h_buf;
14315 struct hbq_dmabuf *dmabuf = NULL;
14316
14317 /* get the oldest sequence on the rcv list */
14318 h_buf = list_get_first(&vport->rcv_buffer_list,
14319 struct lpfc_dmabuf, list);
14320 if (!h_buf)
14321 return;
14322 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14323 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14324}
14325
14326/**
14327 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14328 * @vport: The vport that the received sequences were sent to.
14329 *
14330 * This function cleans up all outstanding received sequences. This is called
14331 * by the driver when a link event or user action invalidates all the received
14332 * sequences.
14333 **/
14334void
14335lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14336{
14337 struct lpfc_dmabuf *h_buf, *hnext;
14338 struct lpfc_dmabuf *d_buf, *dnext;
14339 struct hbq_dmabuf *dmabuf = NULL;
14340
14341 /* start with the oldest sequence on the rcv list */
14342 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14343 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14344 list_del_init(&dmabuf->hbuf.list);
14345 list_for_each_entry_safe(d_buf, dnext,
14346 &dmabuf->dbuf.list, list) {
14347 list_del_init(&d_buf->list);
14348 lpfc_in_buf_free(vport->phba, d_buf);
14349 }
14350 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14351 }
14352}
14353
14354/**
14355 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14356 * @vport: The vport that the received sequences were sent to.
14357 *
14358 * This function determines whether any received sequences have timed out by
14359 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14360 * indicates that there is at least one timed out sequence this routine will
14361 * go through the received sequences one at a time from most inactive to most
14362 * active to determine which ones need to be cleaned up. Once it has determined
14363 * that a sequence needs to be cleaned up it will simply free up the resources
14364 * without sending an abort.
14365 **/
14366void
14367lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14368{
14369 struct lpfc_dmabuf *h_buf, *hnext;
14370 struct lpfc_dmabuf *d_buf, *dnext;
14371 struct hbq_dmabuf *dmabuf = NULL;
14372 unsigned long timeout;
14373 int abort_count = 0;
14374
14375 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14376 vport->rcv_buffer_time_stamp);
14377 if (list_empty(&vport->rcv_buffer_list) ||
14378 time_before(jiffies, timeout))
14379 return;
14380 /* start with the oldest sequence on the rcv list */
14381 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14382 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14383 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14384 dmabuf->time_stamp);
14385 if (time_before(jiffies, timeout))
14386 break;
14387 abort_count++;
14388 list_del_init(&dmabuf->hbuf.list);
14389 list_for_each_entry_safe(d_buf, dnext,
14390 &dmabuf->dbuf.list, list) {
14391 list_del_init(&d_buf->list);
14392 lpfc_in_buf_free(vport->phba, d_buf);
14393 }
14394 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14395 }
14396 if (abort_count)
14397 lpfc_update_rcv_time_stamp(vport);
14398}
14399
4f774513
JS
14400/**
14401 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14402 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14403 *
14404 * This function searches through the existing incomplete sequences that have
14405 * been sent to this @vport. If the frame matches one of the incomplete
14406 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14407 * make up that sequence. If no sequence is found that matches this frame then
14408 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14409 * This function returns a pointer to the first dmabuf in the sequence list that
14410 * the frame was linked to.
14411 **/
14412static struct hbq_dmabuf *
14413lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14414{
14415 struct fc_frame_header *new_hdr;
14416 struct fc_frame_header *temp_hdr;
14417 struct lpfc_dmabuf *d_buf;
14418 struct lpfc_dmabuf *h_buf;
14419 struct hbq_dmabuf *seq_dmabuf = NULL;
14420 struct hbq_dmabuf *temp_dmabuf = NULL;
14421
4d9ab994 14422 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 14423 dmabuf->time_stamp = jiffies;
4f774513
JS
14424 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14425 /* Use the hdr_buf to find the sequence that this frame belongs to */
14426 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14427 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14428 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14429 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14430 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14431 continue;
14432 /* found a pending sequence that matches this frame */
14433 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14434 break;
14435 }
14436 if (!seq_dmabuf) {
14437 /*
14438 * This indicates first frame received for this sequence.
14439 * Queue the buffer on the vport's rcv_buffer_list.
14440 */
14441 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 14442 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
14443 return dmabuf;
14444 }
14445 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
14446 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14447 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
14448 list_del_init(&seq_dmabuf->hbuf.list);
14449 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14450 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 14451 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
14452 return dmabuf;
14453 }
45ed1190
JS
14454 /* move this sequence to the tail to indicate a young sequence */
14455 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14456 seq_dmabuf->time_stamp = jiffies;
14457 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
14458 if (list_empty(&seq_dmabuf->dbuf.list)) {
14459 temp_hdr = dmabuf->hbuf.virt;
14460 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14461 return seq_dmabuf;
14462 }
4f774513
JS
14463 /* find the correct place in the sequence to insert this frame */
14464 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14465 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14466 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14467 /*
14468 * If the frame's sequence count is greater than the frame on
14469 * the list then insert the frame right after this frame
14470 */
eeead811
JS
14471 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14472 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
14473 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14474 return seq_dmabuf;
14475 }
14476 }
14477 return NULL;
14478}
14479
6669f9bb
JS
14480/**
14481 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14482 * @vport: pointer to a vitural port
14483 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14484 *
14485 * This function tries to abort from the partially assembed sequence, described
14486 * by the information from basic abbort @dmabuf. It checks to see whether such
14487 * partially assembled sequence held by the driver. If so, it shall free up all
14488 * the frames from the partially assembled sequence.
14489 *
14490 * Return
14491 * true -- if there is matching partially assembled sequence present and all
14492 * the frames freed with the sequence;
14493 * false -- if there is no matching partially assembled sequence present so
14494 * nothing got aborted in the lower layer driver
14495 **/
14496static bool
14497lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14498 struct hbq_dmabuf *dmabuf)
14499{
14500 struct fc_frame_header *new_hdr;
14501 struct fc_frame_header *temp_hdr;
14502 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14503 struct hbq_dmabuf *seq_dmabuf = NULL;
14504
14505 /* Use the hdr_buf to find the sequence that matches this frame */
14506 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14507 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14508 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14509 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14510 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14511 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14512 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14513 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14514 continue;
14515 /* found a pending sequence that matches this frame */
14516 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14517 break;
14518 }
14519
14520 /* Free up all the frames from the partially assembled sequence */
14521 if (seq_dmabuf) {
14522 list_for_each_entry_safe(d_buf, n_buf,
14523 &seq_dmabuf->dbuf.list, list) {
14524 list_del_init(&d_buf->list);
14525 lpfc_in_buf_free(vport->phba, d_buf);
14526 }
14527 return true;
14528 }
14529 return false;
14530}
14531
6dd9e31c
JS
14532/**
14533 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14534 * @vport: pointer to a vitural port
14535 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14536 *
14537 * This function tries to abort from the assembed sequence from upper level
14538 * protocol, described by the information from basic abbort @dmabuf. It
14539 * checks to see whether such pending context exists at upper level protocol.
14540 * If so, it shall clean up the pending context.
14541 *
14542 * Return
14543 * true -- if there is matching pending context of the sequence cleaned
14544 * at ulp;
14545 * false -- if there is no matching pending context of the sequence present
14546 * at ulp.
14547 **/
14548static bool
14549lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14550{
14551 struct lpfc_hba *phba = vport->phba;
14552 int handled;
14553
14554 /* Accepting abort at ulp with SLI4 only */
14555 if (phba->sli_rev < LPFC_SLI_REV4)
14556 return false;
14557
14558 /* Register all caring upper level protocols to attend abort */
14559 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14560 if (handled)
14561 return true;
14562
14563 return false;
14564}
14565
6669f9bb 14566/**
546fc854 14567 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
14568 * @phba: Pointer to HBA context object.
14569 * @cmd_iocbq: pointer to the command iocbq structure.
14570 * @rsp_iocbq: pointer to the response iocbq structure.
14571 *
546fc854 14572 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
14573 * event. It properly releases the memory allocated to the sequence abort
14574 * accept iocb.
14575 **/
14576static void
546fc854 14577lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
14578 struct lpfc_iocbq *cmd_iocbq,
14579 struct lpfc_iocbq *rsp_iocbq)
14580{
6dd9e31c
JS
14581 struct lpfc_nodelist *ndlp;
14582
14583 if (cmd_iocbq) {
14584 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14585 lpfc_nlp_put(ndlp);
14586 lpfc_nlp_not_used(ndlp);
6669f9bb 14587 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 14588 }
6b5151fd
JS
14589
14590 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14591 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14593 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14594 rsp_iocbq->iocb.ulpStatus,
14595 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
14596}
14597
6d368e53
JS
14598/**
14599 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14600 * @phba: Pointer to HBA context object.
14601 * @xri: xri id in transaction.
14602 *
14603 * This function validates the xri maps to the known range of XRIs allocated an
14604 * used by the driver.
14605 **/
7851fe2c 14606uint16_t
6d368e53
JS
14607lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14608 uint16_t xri)
14609{
14610 int i;
14611
14612 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14613 if (xri == phba->sli4_hba.xri_ids[i])
14614 return i;
14615 }
14616 return NO_XRI;
14617}
14618
6669f9bb 14619/**
546fc854 14620 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
14621 * @phba: Pointer to HBA context object.
14622 * @fc_hdr: pointer to a FC frame header.
14623 *
546fc854 14624 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
14625 * event after aborting the sequence handling.
14626 **/
14627static void
6dd9e31c
JS
14628lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14629 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 14630{
6dd9e31c 14631 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
14632 struct lpfc_iocbq *ctiocb = NULL;
14633 struct lpfc_nodelist *ndlp;
ee0f4fe1 14634 uint16_t oxid, rxid, xri, lxri;
5ffc266e 14635 uint32_t sid, fctl;
6669f9bb 14636 IOCB_t *icmd;
546fc854 14637 int rc;
6669f9bb
JS
14638
14639 if (!lpfc_is_link_up(phba))
14640 return;
14641
14642 sid = sli4_sid_from_fc_hdr(fc_hdr);
14643 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 14644 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 14645
6dd9e31c 14646 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 14647 if (!ndlp) {
6dd9e31c
JS
14648 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14649 if (!ndlp) {
14650 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14651 "1268 Failed to allocate ndlp for "
14652 "oxid:x%x SID:x%x\n", oxid, sid);
14653 return;
14654 }
14655 lpfc_nlp_init(vport, ndlp, sid);
14656 /* Put ndlp onto pport node list */
14657 lpfc_enqueue_node(vport, ndlp);
14658 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14659 /* re-setup ndlp without removing from node list */
14660 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14661 if (!ndlp) {
14662 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14663 "3275 Failed to active ndlp found "
14664 "for oxid:x%x SID:x%x\n", oxid, sid);
14665 return;
14666 }
6669f9bb
JS
14667 }
14668
546fc854 14669 /* Allocate buffer for rsp iocb */
6669f9bb
JS
14670 ctiocb = lpfc_sli_get_iocbq(phba);
14671 if (!ctiocb)
14672 return;
14673
5ffc266e
JS
14674 /* Extract the F_CTL field from FC_HDR */
14675 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14676
6669f9bb 14677 icmd = &ctiocb->iocb;
6669f9bb 14678 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 14679 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
14680 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14681 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14682 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14683
14684 /* Fill in the rest of iocb fields */
14685 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14686 icmd->ulpBdeCount = 0;
14687 icmd->ulpLe = 1;
14688 icmd->ulpClass = CLASS3;
6d368e53 14689 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 14690 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 14691
6669f9bb
JS
14692 ctiocb->iocb_cmpl = NULL;
14693 ctiocb->vport = phba->pport;
546fc854 14694 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 14695 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
14696 ctiocb->sli4_xritag = NO_XRI;
14697
ee0f4fe1
JS
14698 if (fctl & FC_FC_EX_CTX)
14699 /* Exchange responder sent the abort so we
14700 * own the oxid.
14701 */
14702 xri = oxid;
14703 else
14704 xri = rxid;
14705 lxri = lpfc_sli4_xri_inrange(phba, xri);
14706 if (lxri != NO_XRI)
14707 lpfc_set_rrq_active(phba, ndlp, lxri,
14708 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
14709 /* For BA_ABTS from exchange responder, if the logical xri with
14710 * the oxid maps to the FCP XRI range, the port no longer has
14711 * that exchange context, send a BLS_RJT. Override the IOCB for
14712 * a BA_RJT.
14713 */
14714 if ((fctl & FC_FC_EX_CTX) &&
14715 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14716 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14717 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14718 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14719 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14720 }
14721
14722 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14723 * the driver no longer has that exchange, send a BLS_RJT. Override
14724 * the IOCB for a BA_RJT.
546fc854 14725 */
6dd9e31c 14726 if (aborted == false) {
546fc854
JS
14727 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14728 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14729 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14730 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14731 }
6669f9bb 14732
5ffc266e
JS
14733 if (fctl & FC_FC_EX_CTX) {
14734 /* ABTS sent by responder to CT exchange, construction
14735 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14736 * field and RX_ID from ABTS for RX_ID field.
14737 */
546fc854 14738 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
14739 } else {
14740 /* ABTS sent by initiator to CT exchange, construction
14741 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 14742 * XRI_TAG field.
5ffc266e 14743 */
546fc854 14744 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 14745 }
f09c3acc 14746 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 14747 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 14748
546fc854 14749 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
14750 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14751 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14752 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
14753
14754 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14755 if (rc == IOCB_ERROR) {
6dd9e31c
JS
14756 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14757 "2925 Failed to issue CT ABTS RSP x%x on "
14758 "xri x%x, Data x%x\n",
14759 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14760 phba->link_state);
14761 lpfc_nlp_put(ndlp);
14762 ctiocb->context1 = NULL;
546fc854
JS
14763 lpfc_sli_release_iocbq(phba, ctiocb);
14764 }
6669f9bb
JS
14765}
14766
14767/**
14768 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14769 * @vport: Pointer to the vport on which this sequence was received
14770 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14771 *
14772 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14773 * receive sequence is only partially assembed by the driver, it shall abort
14774 * the partially assembled frames for the sequence. Otherwise, if the
14775 * unsolicited receive sequence has been completely assembled and passed to
14776 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14777 * unsolicited sequence has been aborted. After that, it will issue a basic
14778 * accept to accept the abort.
14779 **/
14780void
14781lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14782 struct hbq_dmabuf *dmabuf)
14783{
14784 struct lpfc_hba *phba = vport->phba;
14785 struct fc_frame_header fc_hdr;
5ffc266e 14786 uint32_t fctl;
6dd9e31c 14787 bool aborted;
6669f9bb 14788
6669f9bb
JS
14789 /* Make a copy of fc_hdr before the dmabuf being released */
14790 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 14791 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 14792
5ffc266e 14793 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
14794 /* ABTS by responder to exchange, no cleanup needed */
14795 aborted = true;
5ffc266e 14796 } else {
6dd9e31c
JS
14797 /* ABTS by initiator to exchange, need to do cleanup */
14798 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14799 if (aborted == false)
14800 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 14801 }
6dd9e31c
JS
14802 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14803
14804 /* Respond with BA_ACC or BA_RJT accordingly */
14805 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
14806}
14807
4f774513
JS
14808/**
14809 * lpfc_seq_complete - Indicates if a sequence is complete
14810 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14811 *
14812 * This function checks the sequence, starting with the frame described by
14813 * @dmabuf, to see if all the frames associated with this sequence are present.
14814 * the frames associated with this sequence are linked to the @dmabuf using the
14815 * dbuf list. This function looks for two major things. 1) That the first frame
14816 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14817 * set. 3) That there are no holes in the sequence count. The function will
14818 * return 1 when the sequence is complete, otherwise it will return 0.
14819 **/
14820static int
14821lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14822{
14823 struct fc_frame_header *hdr;
14824 struct lpfc_dmabuf *d_buf;
14825 struct hbq_dmabuf *seq_dmabuf;
14826 uint32_t fctl;
14827 int seq_count = 0;
14828
14829 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14830 /* make sure first fame of sequence has a sequence count of zero */
14831 if (hdr->fh_seq_cnt != seq_count)
14832 return 0;
14833 fctl = (hdr->fh_f_ctl[0] << 16 |
14834 hdr->fh_f_ctl[1] << 8 |
14835 hdr->fh_f_ctl[2]);
14836 /* If last frame of sequence we can return success. */
14837 if (fctl & FC_FC_END_SEQ)
14838 return 1;
14839 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14840 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14841 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14842 /* If there is a hole in the sequence count then fail. */
eeead811 14843 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
14844 return 0;
14845 fctl = (hdr->fh_f_ctl[0] << 16 |
14846 hdr->fh_f_ctl[1] << 8 |
14847 hdr->fh_f_ctl[2]);
14848 /* If last frame of sequence we can return success. */
14849 if (fctl & FC_FC_END_SEQ)
14850 return 1;
14851 }
14852 return 0;
14853}
14854
14855/**
14856 * lpfc_prep_seq - Prep sequence for ULP processing
14857 * @vport: Pointer to the vport on which this sequence was received
14858 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14859 *
14860 * This function takes a sequence, described by a list of frames, and creates
14861 * a list of iocbq structures to describe the sequence. This iocbq list will be
14862 * used to issue to the generic unsolicited sequence handler. This routine
14863 * returns a pointer to the first iocbq in the list. If the function is unable
14864 * to allocate an iocbq then it throw out the received frames that were not
14865 * able to be described and return a pointer to the first iocbq. If unable to
14866 * allocate any iocbqs (including the first) this function will return NULL.
14867 **/
14868static struct lpfc_iocbq *
14869lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14870{
7851fe2c 14871 struct hbq_dmabuf *hbq_buf;
4f774513
JS
14872 struct lpfc_dmabuf *d_buf, *n_buf;
14873 struct lpfc_iocbq *first_iocbq, *iocbq;
14874 struct fc_frame_header *fc_hdr;
14875 uint32_t sid;
7851fe2c 14876 uint32_t len, tot_len;
eeead811 14877 struct ulp_bde64 *pbde;
4f774513
JS
14878
14879 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14880 /* remove from receive buffer list */
14881 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 14882 lpfc_update_rcv_time_stamp(vport);
4f774513 14883 /* get the Remote Port's SID */
6669f9bb 14884 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 14885 tot_len = 0;
4f774513
JS
14886 /* Get an iocbq struct to fill in. */
14887 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14888 if (first_iocbq) {
14889 /* Initialize the first IOCB. */
8fa38513 14890 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 14891 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
939723a4
JS
14892
14893 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14894 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14895 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14896 first_iocbq->iocb.un.rcvels.parmRo =
14897 sli4_did_from_fc_hdr(fc_hdr);
14898 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14899 } else
14900 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
14901 first_iocbq->iocb.ulpContext = NO_XRI;
14902 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14903 be16_to_cpu(fc_hdr->fh_ox_id);
14904 /* iocbq is prepped for internal consumption. Physical vpi. */
14905 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14906 vport->phba->vpi_ids[vport->vpi];
4f774513 14907 /* put the first buffer into the first IOCBq */
48a5a664
JS
14908 tot_len = bf_get(lpfc_rcqe_length,
14909 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14910
4f774513
JS
14911 first_iocbq->context2 = &seq_dmabuf->dbuf;
14912 first_iocbq->context3 = NULL;
14913 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
14914 if (tot_len > LPFC_DATA_BUF_SIZE)
14915 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 14916 LPFC_DATA_BUF_SIZE;
48a5a664
JS
14917 else
14918 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
14919
4f774513 14920 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 14921
7851fe2c 14922 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
14923 }
14924 iocbq = first_iocbq;
14925 /*
14926 * Each IOCBq can have two Buffers assigned, so go through the list
14927 * of buffers for this sequence and save two buffers in each IOCBq
14928 */
14929 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14930 if (!iocbq) {
14931 lpfc_in_buf_free(vport->phba, d_buf);
14932 continue;
14933 }
14934 if (!iocbq->context3) {
14935 iocbq->context3 = d_buf;
14936 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
14937 /* We need to get the size out of the right CQE */
14938 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14939 len = bf_get(lpfc_rcqe_length,
14940 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
14941 pbde = (struct ulp_bde64 *)
14942 &iocbq->iocb.unsli3.sli3Words[4];
14943 if (len > LPFC_DATA_BUF_SIZE)
14944 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14945 else
14946 pbde->tus.f.bdeSize = len;
14947
7851fe2c
JS
14948 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14949 tot_len += len;
4f774513
JS
14950 } else {
14951 iocbq = lpfc_sli_get_iocbq(vport->phba);
14952 if (!iocbq) {
14953 if (first_iocbq) {
14954 first_iocbq->iocb.ulpStatus =
14955 IOSTAT_FCP_RSP_ERROR;
14956 first_iocbq->iocb.un.ulpWord[4] =
14957 IOERR_NO_RESOURCES;
14958 }
14959 lpfc_in_buf_free(vport->phba, d_buf);
14960 continue;
14961 }
48a5a664
JS
14962 /* We need to get the size out of the right CQE */
14963 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14964 len = bf_get(lpfc_rcqe_length,
14965 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
14966 iocbq->context2 = d_buf;
14967 iocbq->context3 = NULL;
14968 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
14969 if (len > LPFC_DATA_BUF_SIZE)
14970 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 14971 LPFC_DATA_BUF_SIZE;
48a5a664
JS
14972 else
14973 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 14974
7851fe2c
JS
14975 tot_len += len;
14976 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14977
4f774513
JS
14978 iocbq->iocb.un.rcvels.remoteID = sid;
14979 list_add_tail(&iocbq->list, &first_iocbq->list);
14980 }
14981 }
14982 return first_iocbq;
14983}
14984
6669f9bb
JS
14985static void
14986lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14987 struct hbq_dmabuf *seq_dmabuf)
14988{
14989 struct fc_frame_header *fc_hdr;
14990 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14991 struct lpfc_hba *phba = vport->phba;
14992
14993 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14994 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14995 if (!iocbq) {
14996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14997 "2707 Ring %d handler: Failed to allocate "
14998 "iocb Rctl x%x Type x%x received\n",
14999 LPFC_ELS_RING,
15000 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15001 return;
15002 }
15003 if (!lpfc_complete_unsol_iocb(phba,
15004 &phba->sli.ring[LPFC_ELS_RING],
15005 iocbq, fc_hdr->fh_r_ctl,
15006 fc_hdr->fh_type))
6d368e53 15007 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
15008 "2540 Ring %d handler: unexpected Rctl "
15009 "x%x Type x%x received\n",
15010 LPFC_ELS_RING,
15011 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15012
15013 /* Free iocb created in lpfc_prep_seq */
15014 list_for_each_entry_safe(curr_iocb, next_iocb,
15015 &iocbq->list, list) {
15016 list_del_init(&curr_iocb->list);
15017 lpfc_sli_release_iocbq(phba, curr_iocb);
15018 }
15019 lpfc_sli_release_iocbq(phba, iocbq);
15020}
15021
4f774513
JS
15022/**
15023 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15024 * @phba: Pointer to HBA context object.
15025 *
15026 * This function is called with no lock held. This function processes all
15027 * the received buffers and gives it to upper layers when a received buffer
15028 * indicates that it is the final frame in the sequence. The interrupt
15029 * service routine processes received buffers at interrupt contexts and adds
15030 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15031 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15032 * appropriate receive function when the final frame in a sequence is received.
15033 **/
4d9ab994
JS
15034void
15035lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15036 struct hbq_dmabuf *dmabuf)
4f774513 15037{
4d9ab994 15038 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
15039 struct fc_frame_header *fc_hdr;
15040 struct lpfc_vport *vport;
15041 uint32_t fcfi;
939723a4 15042 uint32_t did;
4f774513 15043
4f774513 15044 /* Process each received buffer */
4d9ab994
JS
15045 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15046 /* check to see if this a valid type of frame */
15047 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15048 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15049 return;
15050 }
7851fe2c
JS
15051 if ((bf_get(lpfc_cqe_code,
15052 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15053 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15054 &dmabuf->cq_event.cqe.rcqe_cmpl);
15055 else
15056 fcfi = bf_get(lpfc_rcqe_fcf_id,
15057 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 15058
4d9ab994 15059 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
939723a4 15060 if (!vport) {
4d9ab994
JS
15061 /* throw out the frame */
15062 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15063 return;
15064 }
939723a4
JS
15065
15066 /* d_id this frame is directed to */
15067 did = sli4_did_from_fc_hdr(fc_hdr);
15068
15069 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15070 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15071 (did != Fabric_DID)) {
15072 /*
15073 * Throw out the frame if we are not pt2pt.
15074 * The pt2pt protocol allows for discovery frames
15075 * to be received without a registered VPI.
15076 */
15077 if (!(vport->fc_flag & FC_PT2PT) ||
15078 (phba->link_state == LPFC_HBA_READY)) {
15079 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15080 return;
15081 }
15082 }
15083
6669f9bb
JS
15084 /* Handle the basic abort sequence (BA_ABTS) event */
15085 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15086 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15087 return;
15088 }
15089
4d9ab994
JS
15090 /* Link this frame */
15091 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15092 if (!seq_dmabuf) {
15093 /* unable to add frame to vport - throw it out */
15094 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15095 return;
15096 }
15097 /* If not last frame in sequence continue processing frames. */
def9c7a9 15098 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 15099 return;
def9c7a9 15100
6669f9bb
JS
15101 /* Send the complete sequence to the upper layer protocol */
15102 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 15103}
6fb120a7
JS
15104
15105/**
15106 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15107 * @phba: pointer to lpfc hba data structure.
15108 *
15109 * This routine is invoked to post rpi header templates to the
15110 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
15111 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15112 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
15113 *
15114 * This routine does not require any locks. It's usage is expected
15115 * to be driver load or reset recovery when the driver is
15116 * sequential.
15117 *
15118 * Return codes
af901ca1 15119 * 0 - successful
d439d286 15120 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
15121 * When this error occurs, the driver is not guaranteed
15122 * to have any rpi regions posted to the device and
15123 * must either attempt to repost the regions or take a
15124 * fatal error.
15125 **/
15126int
15127lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15128{
15129 struct lpfc_rpi_hdr *rpi_page;
15130 uint32_t rc = 0;
6d368e53
JS
15131 uint16_t lrpi = 0;
15132
15133 /* SLI4 ports that support extents do not require RPI headers. */
15134 if (!phba->sli4_hba.rpi_hdrs_in_use)
15135 goto exit;
15136 if (phba->sli4_hba.extents_in_use)
15137 return -EIO;
6fb120a7 15138
6fb120a7 15139 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
15140 /*
15141 * Assign the rpi headers a physical rpi only if the driver
15142 * has not initialized those resources. A port reset only
15143 * needs the headers posted.
15144 */
15145 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15146 LPFC_RPI_RSRC_RDY)
15147 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15148
6fb120a7
JS
15149 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15150 if (rc != MBX_SUCCESS) {
15151 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15152 "2008 Error %d posting all rpi "
15153 "headers\n", rc);
15154 rc = -EIO;
15155 break;
15156 }
15157 }
15158
6d368e53
JS
15159 exit:
15160 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15161 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
15162 return rc;
15163}
15164
15165/**
15166 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15167 * @phba: pointer to lpfc hba data structure.
15168 * @rpi_page: pointer to the rpi memory region.
15169 *
15170 * This routine is invoked to post a single rpi header to the
15171 * HBA consistent with the SLI-4 interface spec. This memory region
15172 * maps up to 64 rpi context regions.
15173 *
15174 * Return codes
af901ca1 15175 * 0 - successful
d439d286
JS
15176 * -ENOMEM - No available memory
15177 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
15178 **/
15179int
15180lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15181{
15182 LPFC_MBOXQ_t *mboxq;
15183 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15184 uint32_t rc = 0;
6fb120a7
JS
15185 uint32_t shdr_status, shdr_add_status;
15186 union lpfc_sli4_cfg_shdr *shdr;
15187
6d368e53
JS
15188 /* SLI4 ports that support extents do not require RPI headers. */
15189 if (!phba->sli4_hba.rpi_hdrs_in_use)
15190 return rc;
15191 if (phba->sli4_hba.extents_in_use)
15192 return -EIO;
15193
6fb120a7
JS
15194 /* The port is notified of the header region via a mailbox command. */
15195 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15196 if (!mboxq) {
15197 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15198 "2001 Unable to allocate memory for issuing "
15199 "SLI_CONFIG_SPECIAL mailbox command\n");
15200 return -ENOMEM;
15201 }
15202
15203 /* Post all rpi memory regions to the port. */
15204 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
15205 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15206 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15207 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
15208 sizeof(struct lpfc_sli4_cfg_mhdr),
15209 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
15210
15211
15212 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
15213 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15214 rpi_page->start_rpi);
6d368e53
JS
15215 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15216 hdr_tmpl, rpi_page->page_count);
15217
6fb120a7
JS
15218 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15219 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 15220 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
15221 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15222 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15223 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15224 if (rc != MBX_TIMEOUT)
15225 mempool_free(mboxq, phba->mbox_mem_pool);
15226 if (shdr_status || shdr_add_status || rc) {
15227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15228 "2514 POST_RPI_HDR mailbox failed with "
15229 "status x%x add_status x%x, mbx status x%x\n",
15230 shdr_status, shdr_add_status, rc);
15231 rc = -ENXIO;
15232 }
15233 return rc;
15234}
15235
15236/**
15237 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15238 * @phba: pointer to lpfc hba data structure.
15239 *
15240 * This routine is invoked to post rpi header templates to the
15241 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
15242 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15243 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
15244 *
15245 * Returns
af901ca1 15246 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
15247 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15248 **/
15249int
15250lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15251{
6d368e53
JS
15252 unsigned long rpi;
15253 uint16_t max_rpi, rpi_limit;
15254 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 15255 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 15256 unsigned long iflag;
6fb120a7
JS
15257
15258 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6fb120a7
JS
15259 rpi_limit = phba->sli4_hba.next_rpi;
15260
15261 /*
6d368e53
JS
15262 * Fetch the next logical rpi. Because this index is logical,
15263 * the driver starts at 0 each time.
6fb120a7 15264 */
4902b381 15265 spin_lock_irqsave(&phba->hbalock, iflag);
6d368e53
JS
15266 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15267 if (rpi >= rpi_limit)
6fb120a7
JS
15268 rpi = LPFC_RPI_ALLOC_ERROR;
15269 else {
15270 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15271 phba->sli4_hba.max_cfg_param.rpi_used++;
15272 phba->sli4_hba.rpi_count++;
15273 }
15274
15275 /*
15276 * Don't try to allocate more rpi header regions if the device limit
6d368e53 15277 * has been exhausted.
6fb120a7
JS
15278 */
15279 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15280 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 15281 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
15282 return rpi;
15283 }
15284
6d368e53
JS
15285 /*
15286 * RPI header postings are not required for SLI4 ports capable of
15287 * extents.
15288 */
15289 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 15290 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
15291 return rpi;
15292 }
15293
6fb120a7
JS
15294 /*
15295 * If the driver is running low on rpi resources, allocate another
15296 * page now. Note that the next_rpi value is used because
15297 * it represents how many are actually in use whereas max_rpi notes
15298 * how many are supported max by the device.
15299 */
6d368e53 15300 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 15301 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
15302 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15303 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15304 if (!rpi_hdr) {
15305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15306 "2002 Error Could not grow rpi "
15307 "count\n");
15308 } else {
6d368e53
JS
15309 lrpi = rpi_hdr->start_rpi;
15310 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
15311 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15312 }
15313 }
15314
15315 return rpi;
15316}
15317
d7c47992
JS
15318/**
15319 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15320 * @phba: pointer to lpfc hba data structure.
15321 *
15322 * This routine is invoked to release an rpi to the pool of
15323 * available rpis maintained by the driver.
15324 **/
15325void
15326__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15327{
15328 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15329 phba->sli4_hba.rpi_count--;
15330 phba->sli4_hba.max_cfg_param.rpi_used--;
15331 }
15332}
15333
6fb120a7
JS
15334/**
15335 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15336 * @phba: pointer to lpfc hba data structure.
15337 *
15338 * This routine is invoked to release an rpi to the pool of
15339 * available rpis maintained by the driver.
15340 **/
15341void
15342lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15343{
15344 spin_lock_irq(&phba->hbalock);
d7c47992 15345 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
15346 spin_unlock_irq(&phba->hbalock);
15347}
15348
15349/**
15350 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15351 * @phba: pointer to lpfc hba data structure.
15352 *
15353 * This routine is invoked to remove the memory region that
15354 * provided rpi via a bitmask.
15355 **/
15356void
15357lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15358{
15359 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
15360 kfree(phba->sli4_hba.rpi_ids);
15361 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
15362}
15363
15364/**
15365 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15366 * @phba: pointer to lpfc hba data structure.
15367 *
15368 * This routine is invoked to remove the memory region that
15369 * provided rpi via a bitmask.
15370 **/
15371int
6b5151fd
JS
15372lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15373 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
15374{
15375 LPFC_MBOXQ_t *mboxq;
15376 struct lpfc_hba *phba = ndlp->phba;
15377 int rc;
15378
15379 /* The port is notified of the header region via a mailbox command. */
15380 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15381 if (!mboxq)
15382 return -ENOMEM;
15383
15384 /* Post all rpi memory regions to the port. */
15385 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
15386 if (cmpl) {
15387 mboxq->mbox_cmpl = cmpl;
15388 mboxq->context1 = arg;
15389 mboxq->context2 = ndlp;
72859909
JS
15390 } else
15391 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 15392 mboxq->vport = ndlp->vport;
6fb120a7
JS
15393 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15394 if (rc == MBX_NOT_FINISHED) {
15395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15396 "2010 Resume RPI Mailbox failed "
15397 "status %d, mbxStatus x%x\n", rc,
15398 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15399 mempool_free(mboxq, phba->mbox_mem_pool);
15400 return -EIO;
15401 }
15402 return 0;
15403}
15404
15405/**
15406 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 15407 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 15408 *
76a95d75 15409 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
15410 *
15411 * Returns:
15412 * 0 success
15413 * -Evalue otherwise
15414 **/
15415int
76a95d75 15416lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
15417{
15418 LPFC_MBOXQ_t *mboxq;
15419 int rc = 0;
6a9c52cf 15420 int retval = MBX_SUCCESS;
6fb120a7 15421 uint32_t mbox_tmo;
76a95d75 15422 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
15423 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15424 if (!mboxq)
15425 return -ENOMEM;
76a95d75 15426 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 15427 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 15428 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 15429 if (rc != MBX_SUCCESS) {
76a95d75 15430 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
15431 "2022 INIT VPI Mailbox failed "
15432 "status %d, mbxStatus x%x\n", rc,
15433 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 15434 retval = -EIO;
6fb120a7 15435 }
6a9c52cf 15436 if (rc != MBX_TIMEOUT)
76a95d75 15437 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
15438
15439 return retval;
6fb120a7
JS
15440}
15441
15442/**
15443 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15444 * @phba: pointer to lpfc hba data structure.
15445 * @mboxq: Pointer to mailbox object.
15446 *
15447 * This routine is invoked to manually add a single FCF record. The caller
15448 * must pass a completely initialized FCF_Record. This routine takes
15449 * care of the nonembedded mailbox operations.
15450 **/
15451static void
15452lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15453{
15454 void *virt_addr;
15455 union lpfc_sli4_cfg_shdr *shdr;
15456 uint32_t shdr_status, shdr_add_status;
15457
15458 virt_addr = mboxq->sge_array->addr[0];
15459 /* The IOCTL status is embedded in the mailbox subheader. */
15460 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15461 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15462 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15463
15464 if ((shdr_status || shdr_add_status) &&
15465 (shdr_status != STATUS_FCF_IN_USE))
15466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15467 "2558 ADD_FCF_RECORD mailbox failed with "
15468 "status x%x add_status x%x\n",
15469 shdr_status, shdr_add_status);
15470
15471 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15472}
15473
15474/**
15475 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15476 * @phba: pointer to lpfc hba data structure.
15477 * @fcf_record: pointer to the initialized fcf record to add.
15478 *
15479 * This routine is invoked to manually add a single FCF record. The caller
15480 * must pass a completely initialized FCF_Record. This routine takes
15481 * care of the nonembedded mailbox operations.
15482 **/
15483int
15484lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15485{
15486 int rc = 0;
15487 LPFC_MBOXQ_t *mboxq;
15488 uint8_t *bytep;
15489 void *virt_addr;
15490 dma_addr_t phys_addr;
15491 struct lpfc_mbx_sge sge;
15492 uint32_t alloc_len, req_len;
15493 uint32_t fcfindex;
15494
15495 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15496 if (!mboxq) {
15497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15498 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15499 return -ENOMEM;
15500 }
15501
15502 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15503 sizeof(uint32_t);
15504
15505 /* Allocate DMA memory and set up the non-embedded mailbox command */
15506 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15507 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15508 req_len, LPFC_SLI4_MBX_NEMBED);
15509 if (alloc_len < req_len) {
15510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15511 "2523 Allocated DMA memory size (x%x) is "
15512 "less than the requested DMA memory "
15513 "size (x%x)\n", alloc_len, req_len);
15514 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15515 return -ENOMEM;
15516 }
15517
15518 /*
15519 * Get the first SGE entry from the non-embedded DMA memory. This
15520 * routine only uses a single SGE.
15521 */
15522 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15523 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
15524 virt_addr = mboxq->sge_array->addr[0];
15525 /*
15526 * Configure the FCF record for FCFI 0. This is the driver's
15527 * hardcoded default and gets used in nonFIP mode.
15528 */
15529 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15530 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15531 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15532
15533 /*
15534 * Copy the fcf_index and the FCF Record Data. The data starts after
15535 * the FCoE header plus word10. The data copy needs to be endian
15536 * correct.
15537 */
15538 bytep += sizeof(uint32_t);
15539 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15540 mboxq->vport = phba->pport;
15541 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15542 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15543 if (rc == MBX_NOT_FINISHED) {
15544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15545 "2515 ADD_FCF_RECORD mailbox failed with "
15546 "status 0x%x\n", rc);
15547 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15548 rc = -EIO;
15549 } else
15550 rc = 0;
15551
15552 return rc;
15553}
15554
15555/**
15556 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15557 * @phba: pointer to lpfc hba data structure.
15558 * @fcf_record: pointer to the fcf record to write the default data.
15559 * @fcf_index: FCF table entry index.
15560 *
15561 * This routine is invoked to build the driver's default FCF record. The
15562 * values used are hardcoded. This routine handles memory initialization.
15563 *
15564 **/
15565void
15566lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15567 struct fcf_record *fcf_record,
15568 uint16_t fcf_index)
15569{
15570 memset(fcf_record, 0, sizeof(struct fcf_record));
15571 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15572 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15573 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15574 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15575 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15576 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15577 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15578 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15579 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15580 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15581 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15582 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15583 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 15584 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
15585 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15586 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15587 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15588 /* Set the VLAN bit map */
15589 if (phba->valid_vlan) {
15590 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15591 = 1 << (phba->vlan_id % 8);
15592 }
15593}
15594
15595/**
0c9ab6f5 15596 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
15597 * @phba: pointer to lpfc hba data structure.
15598 * @fcf_index: FCF table entry offset.
15599 *
0c9ab6f5
JS
15600 * This routine is invoked to scan the entire FCF table by reading FCF
15601 * record and processing it one at a time starting from the @fcf_index
15602 * for initial FCF discovery or fast FCF failover rediscovery.
15603 *
25985edc 15604 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 15605 * otherwise.
6fb120a7
JS
15606 **/
15607int
0c9ab6f5 15608lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
15609{
15610 int rc = 0, error;
15611 LPFC_MBOXQ_t *mboxq;
6fb120a7 15612
32b9793f 15613 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 15614 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
15615 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15616 if (!mboxq) {
15617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15618 "2000 Failed to allocate mbox for "
15619 "READ_FCF cmd\n");
4d9ab994 15620 error = -ENOMEM;
0c9ab6f5 15621 goto fail_fcf_scan;
6fb120a7 15622 }
ecfd03c6 15623 /* Construct the read FCF record mailbox command */
0c9ab6f5 15624 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
15625 if (rc) {
15626 error = -EINVAL;
0c9ab6f5 15627 goto fail_fcf_scan;
6fb120a7 15628 }
ecfd03c6 15629 /* Issue the mailbox command asynchronously */
6fb120a7 15630 mboxq->vport = phba->pport;
0c9ab6f5 15631 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
15632
15633 spin_lock_irq(&phba->hbalock);
15634 phba->hba_flag |= FCF_TS_INPROG;
15635 spin_unlock_irq(&phba->hbalock);
15636
6fb120a7 15637 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 15638 if (rc == MBX_NOT_FINISHED)
6fb120a7 15639 error = -EIO;
ecfd03c6 15640 else {
38b92ef8
JS
15641 /* Reset eligible FCF count for new scan */
15642 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 15643 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 15644 error = 0;
32b9793f 15645 }
0c9ab6f5 15646fail_fcf_scan:
4d9ab994
JS
15647 if (error) {
15648 if (mboxq)
15649 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 15650 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 15651 spin_lock_irq(&phba->hbalock);
a93ff37a 15652 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
15653 spin_unlock_irq(&phba->hbalock);
15654 }
6fb120a7
JS
15655 return error;
15656}
a0c87cbd 15657
0c9ab6f5 15658/**
a93ff37a 15659 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
15660 * @phba: pointer to lpfc hba data structure.
15661 * @fcf_index: FCF table entry offset.
15662 *
15663 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 15664 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 15665 *
25985edc 15666 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
15667 * otherwise.
15668 **/
15669int
15670lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15671{
15672 int rc = 0, error;
15673 LPFC_MBOXQ_t *mboxq;
15674
15675 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15676 if (!mboxq) {
15677 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15678 "2763 Failed to allocate mbox for "
15679 "READ_FCF cmd\n");
15680 error = -ENOMEM;
15681 goto fail_fcf_read;
15682 }
15683 /* Construct the read FCF record mailbox command */
15684 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15685 if (rc) {
15686 error = -EINVAL;
15687 goto fail_fcf_read;
15688 }
15689 /* Issue the mailbox command asynchronously */
15690 mboxq->vport = phba->pport;
15691 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15692 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15693 if (rc == MBX_NOT_FINISHED)
15694 error = -EIO;
15695 else
15696 error = 0;
15697
15698fail_fcf_read:
15699 if (error && mboxq)
15700 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15701 return error;
15702}
15703
15704/**
15705 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15706 * @phba: pointer to lpfc hba data structure.
15707 * @fcf_index: FCF table entry offset.
15708 *
15709 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 15710 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 15711 *
25985edc 15712 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
15713 * otherwise.
15714 **/
15715int
15716lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15717{
15718 int rc = 0, error;
15719 LPFC_MBOXQ_t *mboxq;
15720
15721 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15722 if (!mboxq) {
15723 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15724 "2758 Failed to allocate mbox for "
15725 "READ_FCF cmd\n");
15726 error = -ENOMEM;
15727 goto fail_fcf_read;
15728 }
15729 /* Construct the read FCF record mailbox command */
15730 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15731 if (rc) {
15732 error = -EINVAL;
15733 goto fail_fcf_read;
15734 }
15735 /* Issue the mailbox command asynchronously */
15736 mboxq->vport = phba->pport;
15737 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15738 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15739 if (rc == MBX_NOT_FINISHED)
15740 error = -EIO;
15741 else
15742 error = 0;
15743
15744fail_fcf_read:
15745 if (error && mboxq)
15746 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15747 return error;
15748}
15749
7d791df7
JS
15750/**
15751 * lpfc_check_next_fcf_pri
15752 * phba pointer to the lpfc_hba struct for this port.
15753 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15754 * routine when the rr_bmask is empty. The FCF indecies are put into the
15755 * rr_bmask based on their priority level. Starting from the highest priority
15756 * to the lowest. The most likely FCF candidate will be in the highest
15757 * priority group. When this routine is called it searches the fcf_pri list for
15758 * next lowest priority group and repopulates the rr_bmask with only those
15759 * fcf_indexes.
15760 * returns:
15761 * 1=success 0=failure
15762 **/
15763int
15764lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15765{
15766 uint16_t next_fcf_pri;
15767 uint16_t last_index;
15768 struct lpfc_fcf_pri *fcf_pri;
15769 int rc;
15770 int ret = 0;
15771
15772 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15773 LPFC_SLI4_FCF_TBL_INDX_MAX);
15774 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15775 "3060 Last IDX %d\n", last_index);
2562669c
JS
15776
15777 /* Verify the priority list has 2 or more entries */
15778 spin_lock_irq(&phba->hbalock);
15779 if (list_empty(&phba->fcf.fcf_pri_list) ||
15780 list_is_singular(&phba->fcf.fcf_pri_list)) {
15781 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
15782 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15783 "3061 Last IDX %d\n", last_index);
15784 return 0; /* Empty rr list */
15785 }
2562669c
JS
15786 spin_unlock_irq(&phba->hbalock);
15787
7d791df7
JS
15788 next_fcf_pri = 0;
15789 /*
15790 * Clear the rr_bmask and set all of the bits that are at this
15791 * priority.
15792 */
15793 memset(phba->fcf.fcf_rr_bmask, 0,
15794 sizeof(*phba->fcf.fcf_rr_bmask));
15795 spin_lock_irq(&phba->hbalock);
15796 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15797 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15798 continue;
15799 /*
15800 * the 1st priority that has not FLOGI failed
15801 * will be the highest.
15802 */
15803 if (!next_fcf_pri)
15804 next_fcf_pri = fcf_pri->fcf_rec.priority;
15805 spin_unlock_irq(&phba->hbalock);
15806 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15807 rc = lpfc_sli4_fcf_rr_index_set(phba,
15808 fcf_pri->fcf_rec.fcf_index);
15809 if (rc)
15810 return 0;
15811 }
15812 spin_lock_irq(&phba->hbalock);
15813 }
15814 /*
15815 * if next_fcf_pri was not set above and the list is not empty then
15816 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 15817 * and start at the beginning.
7d791df7
JS
15818 */
15819 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15820 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15821 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15822 /*
15823 * the 1st priority that has not FLOGI failed
15824 * will be the highest.
15825 */
15826 if (!next_fcf_pri)
15827 next_fcf_pri = fcf_pri->fcf_rec.priority;
15828 spin_unlock_irq(&phba->hbalock);
15829 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15830 rc = lpfc_sli4_fcf_rr_index_set(phba,
15831 fcf_pri->fcf_rec.fcf_index);
15832 if (rc)
15833 return 0;
15834 }
15835 spin_lock_irq(&phba->hbalock);
15836 }
15837 } else
15838 ret = 1;
15839 spin_unlock_irq(&phba->hbalock);
15840
15841 return ret;
15842}
0c9ab6f5
JS
15843/**
15844 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15845 * @phba: pointer to lpfc hba data structure.
15846 *
15847 * This routine is to get the next eligible FCF record index in a round
15848 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 15849 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
15850 * shall be returned, otherwise, the next eligible FCF record's index
15851 * shall be returned.
15852 **/
15853uint16_t
15854lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15855{
15856 uint16_t next_fcf_index;
15857
421c6622 15858initial_priority:
3804dc84 15859 /* Search start from next bit of currently registered FCF index */
421c6622
JS
15860 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15861
7d791df7 15862next_priority:
421c6622
JS
15863 /* Determine the next fcf index to check */
15864 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
15865 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15866 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
15867 next_fcf_index);
15868
0c9ab6f5 15869 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
15870 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15871 /*
15872 * If we have wrapped then we need to clear the bits that
15873 * have been tested so that we can detect when we should
15874 * change the priority level.
15875 */
0c9ab6f5
JS
15876 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15877 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
15878 }
15879
3804dc84
JS
15880
15881 /* Check roundrobin failover list empty condition */
7d791df7
JS
15882 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15883 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15884 /*
15885 * If next fcf index is not found check if there are lower
15886 * Priority level fcf's in the fcf_priority list.
15887 * Set up the rr_bmask with all of the avaiable fcf bits
15888 * at that level and continue the selection process.
15889 */
15890 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 15891 goto initial_priority;
3804dc84
JS
15892 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15893 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
15894 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15895 return LPFC_FCOE_FCF_NEXT_NONE;
15896 else {
15897 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15898 "3063 Only FCF available idx %d, flag %x\n",
15899 next_fcf_index,
15900 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15901 return next_fcf_index;
15902 }
3804dc84
JS
15903 }
15904
7d791df7
JS
15905 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15906 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15907 LPFC_FCF_FLOGI_FAILED)
15908 goto next_priority;
15909
3804dc84 15910 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
15911 "2845 Get next roundrobin failover FCF (x%x)\n",
15912 next_fcf_index);
15913
0c9ab6f5
JS
15914 return next_fcf_index;
15915}
15916
15917/**
15918 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15919 * @phba: pointer to lpfc hba data structure.
15920 *
15921 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 15922 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15923 * does not go beyond the range of the driver allocated bmask dimension
15924 * before setting the bit.
15925 *
15926 * Returns 0 if the index bit successfully set, otherwise, it returns
15927 * -EINVAL.
15928 **/
15929int
15930lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15931{
15932 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15933 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15934 "2610 FCF (x%x) reached driver's book "
15935 "keeping dimension:x%x\n",
0c9ab6f5
JS
15936 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15937 return -EINVAL;
15938 }
15939 /* Set the eligible FCF record index bmask */
15940 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15941
3804dc84 15942 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15943 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
15944 "bmask\n", fcf_index);
15945
0c9ab6f5
JS
15946 return 0;
15947}
15948
15949/**
3804dc84 15950 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
15951 * @phba: pointer to lpfc hba data structure.
15952 *
15953 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 15954 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15955 * does not go beyond the range of the driver allocated bmask dimension
15956 * before clearing the bit.
15957 **/
15958void
15959lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15960{
9a803a74 15961 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
15962 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15963 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15964 "2762 FCF (x%x) reached driver's book "
15965 "keeping dimension:x%x\n",
0c9ab6f5
JS
15966 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15967 return;
15968 }
15969 /* Clear the eligible FCF record index bmask */
7d791df7 15970 spin_lock_irq(&phba->hbalock);
9a803a74
JS
15971 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
15972 list) {
7d791df7
JS
15973 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15974 list_del_init(&fcf_pri->list);
15975 break;
15976 }
15977 }
15978 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 15979 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
15980
15981 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15982 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 15983 "bmask\n", fcf_index);
0c9ab6f5
JS
15984}
15985
ecfd03c6
JS
15986/**
15987 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15988 * @phba: pointer to lpfc hba data structure.
15989 *
15990 * This routine is the completion routine for the rediscover FCF table mailbox
15991 * command. If the mailbox command returned failure, it will try to stop the
15992 * FCF rediscover wait timer.
15993 **/
15994void
15995lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15996{
15997 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15998 uint32_t shdr_status, shdr_add_status;
15999
16000 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16001
16002 shdr_status = bf_get(lpfc_mbox_hdr_status,
16003 &redisc_fcf->header.cfg_shdr.response);
16004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16005 &redisc_fcf->header.cfg_shdr.response);
16006 if (shdr_status || shdr_add_status) {
0c9ab6f5 16007 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
16008 "2746 Requesting for FCF rediscovery failed "
16009 "status x%x add_status x%x\n",
16010 shdr_status, shdr_add_status);
0c9ab6f5 16011 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 16012 spin_lock_irq(&phba->hbalock);
0c9ab6f5 16013 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
16014 spin_unlock_irq(&phba->hbalock);
16015 /*
16016 * CVL event triggered FCF rediscover request failed,
16017 * last resort to re-try current registered FCF entry.
16018 */
16019 lpfc_retry_pport_discovery(phba);
16020 } else {
16021 spin_lock_irq(&phba->hbalock);
0c9ab6f5 16022 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
16023 spin_unlock_irq(&phba->hbalock);
16024 /*
16025 * DEAD FCF event triggered FCF rediscover request
16026 * failed, last resort to fail over as a link down
16027 * to FCF registration.
16028 */
16029 lpfc_sli4_fcf_dead_failthrough(phba);
16030 }
0c9ab6f5
JS
16031 } else {
16032 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 16033 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
16034 /*
16035 * Start FCF rediscovery wait timer for pending FCF
16036 * before rescan FCF record table.
16037 */
16038 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 16039 }
ecfd03c6
JS
16040
16041 mempool_free(mbox, phba->mbox_mem_pool);
16042}
16043
16044/**
3804dc84 16045 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
16046 * @phba: pointer to lpfc hba data structure.
16047 *
16048 * This routine is invoked to request for rediscovery of the entire FCF table
16049 * by the port.
16050 **/
16051int
16052lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16053{
16054 LPFC_MBOXQ_t *mbox;
16055 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16056 int rc, length;
16057
0c9ab6f5
JS
16058 /* Cancel retry delay timers to all vports before FCF rediscover */
16059 lpfc_cancel_all_vport_retry_delay_timer(phba);
16060
ecfd03c6
JS
16061 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16062 if (!mbox) {
16063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16064 "2745 Failed to allocate mbox for "
16065 "requesting FCF rediscover.\n");
16066 return -ENOMEM;
16067 }
16068
16069 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16070 sizeof(struct lpfc_sli4_cfg_mhdr));
16071 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16072 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16073 length, LPFC_SLI4_MBX_EMBED);
16074
16075 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16076 /* Set count to 0 for invalidating the entire FCF database */
16077 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16078
16079 /* Issue the mailbox command asynchronously */
16080 mbox->vport = phba->pport;
16081 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16082 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16083
16084 if (rc == MBX_NOT_FINISHED) {
16085 mempool_free(mbox, phba->mbox_mem_pool);
16086 return -EIO;
16087 }
16088 return 0;
16089}
16090
fc2b989b
JS
16091/**
16092 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16093 * @phba: pointer to lpfc hba data structure.
16094 *
16095 * This function is the failover routine as a last resort to the FCF DEAD
16096 * event when driver failed to perform fast FCF failover.
16097 **/
16098void
16099lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16100{
16101 uint32_t link_state;
16102
16103 /*
16104 * Last resort as FCF DEAD event failover will treat this as
16105 * a link down, but save the link state because we don't want
16106 * it to be changed to Link Down unless it is already down.
16107 */
16108 link_state = phba->link_state;
16109 lpfc_linkdown(phba);
16110 phba->link_state = link_state;
16111
16112 /* Unregister FCF if no devices connected to it */
16113 lpfc_unregister_unused_fcf(phba);
16114}
16115
a0c87cbd 16116/**
026abb87 16117 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 16118 * @phba: pointer to lpfc hba data structure.
026abb87 16119 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 16120 *
026abb87
JS
16121 * This function gets SLI3 port configure region 23 data through memory dump
16122 * mailbox command. When it successfully retrieves data, the size of the data
16123 * will be returned, otherwise, 0 will be returned.
a0c87cbd 16124 **/
026abb87
JS
16125static uint32_t
16126lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
16127{
16128 LPFC_MBOXQ_t *pmb = NULL;
16129 MAILBOX_t *mb;
026abb87 16130 uint32_t offset = 0;
a0c87cbd
JS
16131 int rc;
16132
026abb87
JS
16133 if (!rgn23_data)
16134 return 0;
16135
a0c87cbd
JS
16136 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16137 if (!pmb) {
16138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
16139 "2600 failed to allocate mailbox memory\n");
16140 return 0;
a0c87cbd
JS
16141 }
16142 mb = &pmb->u.mb;
16143
a0c87cbd
JS
16144 do {
16145 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16146 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16147
16148 if (rc != MBX_SUCCESS) {
16149 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
16150 "2601 failed to read config "
16151 "region 23, rc 0x%x Status 0x%x\n",
16152 rc, mb->mbxStatus);
a0c87cbd
JS
16153 mb->un.varDmp.word_cnt = 0;
16154 }
16155 /*
16156 * dump mem may return a zero when finished or we got a
16157 * mailbox error, either way we are done.
16158 */
16159 if (mb->un.varDmp.word_cnt == 0)
16160 break;
16161 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16162 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16163
16164 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
16165 rgn23_data + offset,
16166 mb->un.varDmp.word_cnt);
a0c87cbd
JS
16167 offset += mb->un.varDmp.word_cnt;
16168 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16169
026abb87
JS
16170 mempool_free(pmb, phba->mbox_mem_pool);
16171 return offset;
16172}
16173
16174/**
16175 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16176 * @phba: pointer to lpfc hba data structure.
16177 * @rgn23_data: pointer to configure region 23 data.
16178 *
16179 * This function gets SLI4 port configure region 23 data through memory dump
16180 * mailbox command. When it successfully retrieves data, the size of the data
16181 * will be returned, otherwise, 0 will be returned.
16182 **/
16183static uint32_t
16184lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16185{
16186 LPFC_MBOXQ_t *mboxq = NULL;
16187 struct lpfc_dmabuf *mp = NULL;
16188 struct lpfc_mqe *mqe;
16189 uint32_t data_length = 0;
16190 int rc;
16191
16192 if (!rgn23_data)
16193 return 0;
16194
16195 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16196 if (!mboxq) {
16197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16198 "3105 failed to allocate mailbox memory\n");
16199 return 0;
16200 }
16201
16202 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16203 goto out;
16204 mqe = &mboxq->u.mqe;
16205 mp = (struct lpfc_dmabuf *) mboxq->context1;
16206 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16207 if (rc)
16208 goto out;
16209 data_length = mqe->un.mb_words[5];
16210 if (data_length == 0)
16211 goto out;
16212 if (data_length > DMP_RGN23_SIZE) {
16213 data_length = 0;
16214 goto out;
16215 }
16216 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16217out:
16218 mempool_free(mboxq, phba->mbox_mem_pool);
16219 if (mp) {
16220 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16221 kfree(mp);
16222 }
16223 return data_length;
16224}
16225
16226/**
16227 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16228 * @phba: pointer to lpfc hba data structure.
16229 *
16230 * This function read region 23 and parse TLV for port status to
16231 * decide if the user disaled the port. If the TLV indicates the
16232 * port is disabled, the hba_flag is set accordingly.
16233 **/
16234void
16235lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16236{
16237 uint8_t *rgn23_data = NULL;
16238 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16239 uint32_t offset = 0;
16240
16241 /* Get adapter Region 23 data */
16242 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16243 if (!rgn23_data)
16244 goto out;
16245
16246 if (phba->sli_rev < LPFC_SLI_REV4)
16247 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16248 else {
16249 if_type = bf_get(lpfc_sli_intf_if_type,
16250 &phba->sli4_hba.sli_intf);
16251 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16252 goto out;
16253 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16254 }
a0c87cbd
JS
16255
16256 if (!data_size)
16257 goto out;
16258
16259 /* Check the region signature first */
16260 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16262 "2619 Config region 23 has bad signature\n");
16263 goto out;
16264 }
16265 offset += 4;
16266
16267 /* Check the data structure version */
16268 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16270 "2620 Config region 23 has bad version\n");
16271 goto out;
16272 }
16273 offset += 4;
16274
16275 /* Parse TLV entries in the region */
16276 while (offset < data_size) {
16277 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16278 break;
16279 /*
16280 * If the TLV is not driver specific TLV or driver id is
16281 * not linux driver id, skip the record.
16282 */
16283 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16284 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16285 (rgn23_data[offset + 3] != 0)) {
16286 offset += rgn23_data[offset + 1] * 4 + 4;
16287 continue;
16288 }
16289
16290 /* Driver found a driver specific TLV in the config region */
16291 sub_tlv_len = rgn23_data[offset + 1] * 4;
16292 offset += 4;
16293 tlv_offset = 0;
16294
16295 /*
16296 * Search for configured port state sub-TLV.
16297 */
16298 while ((offset < data_size) &&
16299 (tlv_offset < sub_tlv_len)) {
16300 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16301 offset += 4;
16302 tlv_offset += 4;
16303 break;
16304 }
16305 if (rgn23_data[offset] != PORT_STE_TYPE) {
16306 offset += rgn23_data[offset + 1] * 4 + 4;
16307 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16308 continue;
16309 }
16310
16311 /* This HBA contains PORT_STE configured */
16312 if (!rgn23_data[offset + 2])
16313 phba->hba_flag |= LINK_DISABLED;
16314
16315 goto out;
16316 }
16317 }
026abb87 16318
a0c87cbd 16319out:
a0c87cbd
JS
16320 kfree(rgn23_data);
16321 return;
16322}
695a814e 16323
52d52440
JS
16324/**
16325 * lpfc_wr_object - write an object to the firmware
16326 * @phba: HBA structure that indicates port to create a queue on.
16327 * @dmabuf_list: list of dmabufs to write to the port.
16328 * @size: the total byte value of the objects to write to the port.
16329 * @offset: the current offset to be used to start the transfer.
16330 *
16331 * This routine will create a wr_object mailbox command to send to the port.
16332 * the mailbox command will be constructed using the dma buffers described in
16333 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16334 * BDEs that the imbedded mailbox can support. The @offset variable will be
16335 * used to indicate the starting offset of the transfer and will also return
16336 * the offset after the write object mailbox has completed. @size is used to
16337 * determine the end of the object and whether the eof bit should be set.
16338 *
16339 * Return 0 is successful and offset will contain the the new offset to use
16340 * for the next write.
16341 * Return negative value for error cases.
16342 **/
16343int
16344lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16345 uint32_t size, uint32_t *offset)
16346{
16347 struct lpfc_mbx_wr_object *wr_object;
16348 LPFC_MBOXQ_t *mbox;
16349 int rc = 0, i = 0;
16350 uint32_t shdr_status, shdr_add_status;
16351 uint32_t mbox_tmo;
16352 union lpfc_sli4_cfg_shdr *shdr;
16353 struct lpfc_dmabuf *dmabuf;
16354 uint32_t written = 0;
16355
16356 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16357 if (!mbox)
16358 return -ENOMEM;
16359
16360 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16361 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16362 sizeof(struct lpfc_mbx_wr_object) -
16363 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16364
16365 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16366 wr_object->u.request.write_offset = *offset;
16367 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16368 wr_object->u.request.object_name[0] =
16369 cpu_to_le32(wr_object->u.request.object_name[0]);
16370 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16371 list_for_each_entry(dmabuf, dmabuf_list, list) {
16372 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16373 break;
16374 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16375 wr_object->u.request.bde[i].addrHigh =
16376 putPaddrHigh(dmabuf->phys);
16377 if (written + SLI4_PAGE_SIZE >= size) {
16378 wr_object->u.request.bde[i].tus.f.bdeSize =
16379 (size - written);
16380 written += (size - written);
16381 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16382 } else {
16383 wr_object->u.request.bde[i].tus.f.bdeSize =
16384 SLI4_PAGE_SIZE;
16385 written += SLI4_PAGE_SIZE;
16386 }
16387 i++;
16388 }
16389 wr_object->u.request.bde_count = i;
16390 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16391 if (!phba->sli4_hba.intr_enable)
16392 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16393 else {
a183a15f 16394 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
16395 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16396 }
16397 /* The IOCTL status is embedded in the mailbox subheader. */
16398 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16399 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16400 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16401 if (rc != MBX_TIMEOUT)
16402 mempool_free(mbox, phba->mbox_mem_pool);
16403 if (shdr_status || shdr_add_status || rc) {
16404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16405 "3025 Write Object mailbox failed with "
16406 "status x%x add_status x%x, mbx status x%x\n",
16407 shdr_status, shdr_add_status, rc);
16408 rc = -ENXIO;
16409 } else
16410 *offset += wr_object->u.response.actual_write_length;
16411 return rc;
16412}
16413
695a814e
JS
16414/**
16415 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16416 * @vport: pointer to vport data structure.
16417 *
16418 * This function iterate through the mailboxq and clean up all REG_LOGIN
16419 * and REG_VPI mailbox commands associated with the vport. This function
16420 * is called when driver want to restart discovery of the vport due to
16421 * a Clear Virtual Link event.
16422 **/
16423void
16424lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16425{
16426 struct lpfc_hba *phba = vport->phba;
16427 LPFC_MBOXQ_t *mb, *nextmb;
16428 struct lpfc_dmabuf *mp;
78730cfe 16429 struct lpfc_nodelist *ndlp;
d439d286 16430 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 16431 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 16432 LIST_HEAD(mbox_cmd_list);
63e801ce 16433 uint8_t restart_loop;
695a814e 16434
d439d286 16435 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
16436 spin_lock_irq(&phba->hbalock);
16437 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16438 if (mb->vport != vport)
16439 continue;
16440
16441 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16442 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16443 continue;
16444
d439d286
JS
16445 list_del(&mb->list);
16446 list_add_tail(&mb->list, &mbox_cmd_list);
16447 }
16448 /* Clean up active mailbox command with the vport */
16449 mb = phba->sli.mbox_active;
16450 if (mb && (mb->vport == vport)) {
16451 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16452 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16453 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16454 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16455 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16456 /* Put reference count for delayed processing */
16457 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16458 /* Unregister the RPI when mailbox complete */
16459 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16460 }
16461 }
63e801ce
JS
16462 /* Cleanup any mailbox completions which are not yet processed */
16463 do {
16464 restart_loop = 0;
16465 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16466 /*
16467 * If this mailox is already processed or it is
16468 * for another vport ignore it.
16469 */
16470 if ((mb->vport != vport) ||
16471 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16472 continue;
16473
16474 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16475 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16476 continue;
16477
16478 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16479 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16480 ndlp = (struct lpfc_nodelist *)mb->context2;
16481 /* Unregister the RPI when mailbox complete */
16482 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16483 restart_loop = 1;
16484 spin_unlock_irq(&phba->hbalock);
16485 spin_lock(shost->host_lock);
16486 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16487 spin_unlock(shost->host_lock);
16488 spin_lock_irq(&phba->hbalock);
16489 break;
16490 }
16491 }
16492 } while (restart_loop);
16493
d439d286
JS
16494 spin_unlock_irq(&phba->hbalock);
16495
16496 /* Release the cleaned-up mailbox commands */
16497 while (!list_empty(&mbox_cmd_list)) {
16498 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
16499 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16500 mp = (struct lpfc_dmabuf *) (mb->context1);
16501 if (mp) {
16502 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16503 kfree(mp);
16504 }
78730cfe 16505 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 16506 mb->context2 = NULL;
78730cfe 16507 if (ndlp) {
ec21b3b0 16508 spin_lock(shost->host_lock);
589a52d6 16509 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 16510 spin_unlock(shost->host_lock);
78730cfe 16511 lpfc_nlp_put(ndlp);
78730cfe 16512 }
695a814e 16513 }
695a814e
JS
16514 mempool_free(mb, phba->mbox_mem_pool);
16515 }
d439d286
JS
16516
16517 /* Release the ndlp with the cleaned-up active mailbox command */
16518 if (act_mbx_ndlp) {
16519 spin_lock(shost->host_lock);
16520 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16521 spin_unlock(shost->host_lock);
16522 lpfc_nlp_put(act_mbx_ndlp);
695a814e 16523 }
695a814e
JS
16524}
16525
2a9bf3d0
JS
16526/**
16527 * lpfc_drain_txq - Drain the txq
16528 * @phba: Pointer to HBA context object.
16529 *
16530 * This function attempt to submit IOCBs on the txq
16531 * to the adapter. For SLI4 adapters, the txq contains
16532 * ELS IOCBs that have been deferred because the there
16533 * are no SGLs. This congestion can occur with large
16534 * vport counts during node discovery.
16535 **/
16536
16537uint32_t
16538lpfc_drain_txq(struct lpfc_hba *phba)
16539{
16540 LIST_HEAD(completions);
16541 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16542 struct lpfc_iocbq *piocbq = 0;
16543 unsigned long iflags = 0;
16544 char *fail_msg = NULL;
16545 struct lpfc_sglq *sglq;
16546 union lpfc_wqe wqe;
0e9bb8d7 16547 int txq_cnt = 0;
2a9bf3d0 16548
398d81c9 16549 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
16550 list_for_each_entry(piocbq, &pring->txq, list) {
16551 txq_cnt++;
16552 }
16553
16554 if (txq_cnt > pring->txq_max)
16555 pring->txq_max = txq_cnt;
2a9bf3d0 16556
398d81c9 16557 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 16558
0e9bb8d7 16559 while (!list_empty(&pring->txq)) {
398d81c9 16560 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 16561
19ca7609 16562 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 16563 if (!piocbq) {
398d81c9 16564 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
16565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16566 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 16567 txq_cnt);
a629852a
JS
16568 break;
16569 }
19ca7609 16570 sglq = __lpfc_sli_get_sglq(phba, piocbq);
2a9bf3d0 16571 if (!sglq) {
19ca7609 16572 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 16573 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 16574 break;
2a9bf3d0 16575 }
0e9bb8d7 16576 txq_cnt--;
2a9bf3d0
JS
16577
16578 /* The xri and iocb resources secured,
16579 * attempt to issue request
16580 */
6d368e53 16581 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
16582 piocbq->sli4_xritag = sglq->sli4_xritag;
16583 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16584 fail_msg = "to convert bpl to sgl";
16585 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16586 fail_msg = "to convert iocb to wqe";
16587 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16588 fail_msg = " - Wq is full";
16589 else
16590 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16591
16592 if (fail_msg) {
16593 /* Failed means we can't issue and need to cancel */
16594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16595 "2822 IOCB failed %s iotag 0x%x "
16596 "xri 0x%x\n",
16597 fail_msg,
16598 piocbq->iotag, piocbq->sli4_xritag);
16599 list_add_tail(&piocbq->list, &completions);
16600 }
398d81c9 16601 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
16602 }
16603
2a9bf3d0
JS
16604 /* Cancel all the IOCBs that cannot be issued */
16605 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16606 IOERR_SLI_ABORTED);
16607
0e9bb8d7 16608 return txq_cnt;
2a9bf3d0 16609}