]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix frequency of Release WQE CQEs
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
43140ca6 1
dea3101e
JB
2/*******************************************************************
3 * This file is part of the Emulex Linux Device Driver for *
c44ce173 4 * Fibre Channel Host Bus Adapters. *
d080abe0
JS
5 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
6 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
50611577 7 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 8 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 9 * www.broadcom.com *
c44ce173 10 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
11 * *
12 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
13 * modify it under the terms of version 2 of the GNU General *
14 * Public License as published by the Free Software Foundation. *
15 * This program is distributed in the hope that it will be useful. *
16 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
17 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
18 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
19 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20 * TO BE LEGALLY INVALID. See the GNU General Public License for *
21 * more details, a copy of which can be found in the file COPYING *
22 * included with this package. *
dea3101e
JB
23 *******************************************************************/
24
dea3101e
JB
25#include <linux/blkdev.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
5a0e3ad6 29#include <linux/slab.h>
1c2ba475 30#include <linux/lockdep.h>
dea3101e 31
91886523 32#include <scsi/scsi.h>
dea3101e
JB
33#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
f888ba3c 36#include <scsi/scsi_transport_fc.h>
da0436e9 37#include <scsi/fc/fc_fs.h>
0d878419 38#include <linux/aer.h>
dea3101e 39
895427bd
JS
40#include <linux/nvme-fc-driver.h>
41
da0436e9 42#include "lpfc_hw4.h"
dea3101e
JB
43#include "lpfc_hw.h"
44#include "lpfc_sli.h"
da0436e9 45#include "lpfc_sli4.h"
ea2151b4 46#include "lpfc_nl.h"
dea3101e 47#include "lpfc_disc.h"
dea3101e 48#include "lpfc.h"
895427bd
JS
49#include "lpfc_scsi.h"
50#include "lpfc_nvme.h"
f358dd0c 51#include "lpfc_nvmet.h"
dea3101e
JB
52#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
858c9f6c 55#include "lpfc_debugfs.h"
04c68496 56#include "lpfc_vport.h"
61bda8f7 57#include "lpfc_version.h"
dea3101e
JB
58
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
4f774513
JS
67
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
72 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
6669f9bb
JS
75static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
ae9e28f3
JS
77static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
895427bd 79static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
0558056c 80 struct lpfc_cqe *);
895427bd 81static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
8a9d2e80 82 int);
f485c18d
DK
83static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84 struct lpfc_eqe *eqe, uint32_t qidx);
e8d3c3b1
JS
85static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
86static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
895427bd
JS
87static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
88 struct lpfc_sli_ring *pring,
89 struct lpfc_iocbq *cmdiocb);
0558056c 90
4f774513
JS
91static IOCB_t *
92lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
93{
94 return &iocbq->iocb;
95}
96
97/**
98 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
99 * @q: The Work Queue to operate on.
100 * @wqe: The work Queue Entry to put on the Work queue.
101 *
102 * This routine will copy the contents of @wqe to the next available entry on
103 * the @q. This function will then ring the Work Queue Doorbell to signal the
104 * HBA to start processing the Work Queue Entry. This function returns 0 if
105 * successful. If no entries are available on @q then this function will return
106 * -ENOMEM.
107 * The caller is expected to hold the hbalock when calling this routine.
108 **/
cd22d605 109static int
4f774513
JS
110lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
111{
2e90f4b5 112 union lpfc_wqe *temp_wqe;
4f774513
JS
113 struct lpfc_register doorbell;
114 uint32_t host_index;
027140ea 115 uint32_t idx;
4f774513 116
2e90f4b5
JS
117 /* sanity check on queue memory */
118 if (unlikely(!q))
119 return -ENOMEM;
120 temp_wqe = q->qe[q->host_index].wqe;
121
4f774513 122 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
123 idx = ((q->host_index + 1) % q->entry_count);
124 if (idx == q->hba_index) {
b84daac9 125 q->WQ_overflow++;
cd22d605 126 return -EBUSY;
b84daac9
JS
127 }
128 q->WQ_posted++;
4f774513 129 /* set consumption flag every once in a while */
ff78d8f9 130 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 131 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
04673e38
JS
132 else
133 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
fedd3b7b
JS
134 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
135 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513 136 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
6b3b3bdb
JS
137 /* ensure WQE bcopy flushed before doorbell write */
138 wmb();
4f774513
JS
139
140 /* Update the host index before invoking device */
141 host_index = q->host_index;
027140ea
JS
142
143 q->host_index = idx;
4f774513
JS
144
145 /* Ring Doorbell */
146 doorbell.word0 = 0;
962bc51b
JS
147 if (q->db_format == LPFC_DB_LIST_FORMAT) {
148 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
149 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
150 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
151 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
152 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
153 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
154 } else {
155 return -EINVAL;
156 }
157 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
158
159 return 0;
160}
161
162/**
163 * lpfc_sli4_wq_release - Updates internal hba index for WQ
164 * @q: The Work Queue to operate on.
165 * @index: The index to advance the hba index to.
166 *
167 * This routine will update the HBA index of a queue to reflect consumption of
168 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
169 * an entry the host calls this function to update the queue's internal
170 * pointers. This routine returns the number of entries that were consumed by
171 * the HBA.
172 **/
173static uint32_t
174lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
175{
176 uint32_t released = 0;
177
2e90f4b5
JS
178 /* sanity check on queue memory */
179 if (unlikely(!q))
180 return 0;
181
4f774513
JS
182 if (q->hba_index == index)
183 return 0;
184 do {
185 q->hba_index = ((q->hba_index + 1) % q->entry_count);
186 released++;
187 } while (q->hba_index != index);
188 return released;
189}
190
191/**
192 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
193 * @q: The Mailbox Queue to operate on.
194 * @wqe: The Mailbox Queue Entry to put on the Work queue.
195 *
196 * This routine will copy the contents of @mqe to the next available entry on
197 * the @q. This function will then ring the Work Queue Doorbell to signal the
198 * HBA to start processing the Work Queue Entry. This function returns 0 if
199 * successful. If no entries are available on @q then this function will return
200 * -ENOMEM.
201 * The caller is expected to hold the hbalock when calling this routine.
202 **/
203static uint32_t
204lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
205{
2e90f4b5 206 struct lpfc_mqe *temp_mqe;
4f774513 207 struct lpfc_register doorbell;
4f774513 208
2e90f4b5
JS
209 /* sanity check on queue memory */
210 if (unlikely(!q))
211 return -ENOMEM;
212 temp_mqe = q->qe[q->host_index].mqe;
213
4f774513
JS
214 /* If the host has not yet processed the next entry then we are done */
215 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
216 return -ENOMEM;
217 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
218 /* Save off the mailbox pointer for completion */
219 q->phba->mbox = (MAILBOX_t *)temp_mqe;
220
221 /* Update the host index before invoking device */
4f774513
JS
222 q->host_index = ((q->host_index + 1) % q->entry_count);
223
224 /* Ring Doorbell */
225 doorbell.word0 = 0;
226 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
227 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
228 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
229 return 0;
230}
231
232/**
233 * lpfc_sli4_mq_release - Updates internal hba index for MQ
234 * @q: The Mailbox Queue to operate on.
235 *
236 * This routine will update the HBA index of a queue to reflect consumption of
237 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
238 * an entry the host calls this function to update the queue's internal
239 * pointers. This routine returns the number of entries that were consumed by
240 * the HBA.
241 **/
242static uint32_t
243lpfc_sli4_mq_release(struct lpfc_queue *q)
244{
2e90f4b5
JS
245 /* sanity check on queue memory */
246 if (unlikely(!q))
247 return 0;
248
4f774513
JS
249 /* Clear the mailbox pointer for completion */
250 q->phba->mbox = NULL;
251 q->hba_index = ((q->hba_index + 1) % q->entry_count);
252 return 1;
253}
254
255/**
256 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
257 * @q: The Event Queue to get the first valid EQE from
258 *
259 * This routine will get the first valid Event Queue Entry from @q, update
260 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
261 * the Queue (no more work to do), or the Queue is full of EQEs that have been
262 * processed, but not popped back to the HBA then this routine will return NULL.
263 **/
264static struct lpfc_eqe *
265lpfc_sli4_eq_get(struct lpfc_queue *q)
266{
2e90f4b5 267 struct lpfc_eqe *eqe;
027140ea 268 uint32_t idx;
2e90f4b5
JS
269
270 /* sanity check on queue memory */
271 if (unlikely(!q))
272 return NULL;
273 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
274
275 /* If the next EQE is not valid then we are done */
cb5172ea 276 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
277 return NULL;
278 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
279 idx = ((q->hba_index + 1) % q->entry_count);
280 if (idx == q->host_index)
4f774513
JS
281 return NULL;
282
027140ea 283 q->hba_index = idx;
27f344eb
JS
284
285 /*
286 * insert barrier for instruction interlock : data from the hardware
287 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
288 * upon. Speculative instructions were allowing a bcopy at the start
289 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
290 * after our return, to copy data before the valid bit check above
291 * was done. As such, some of the copied data was stale. The barrier
292 * ensures the check is before any data is copied.
27f344eb
JS
293 */
294 mb();
4f774513
JS
295 return eqe;
296}
297
ba20c853
JS
298/**
299 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
300 * @q: The Event Queue to disable interrupts
301 *
302 **/
303static inline void
304lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
305{
306 struct lpfc_register doorbell;
307
308 doorbell.word0 = 0;
309 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
310 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
311 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
312 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
313 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
314 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
315}
316
4f774513
JS
317/**
318 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
319 * @q: The Event Queue that the host has completed processing for.
320 * @arm: Indicates whether the host wants to arms this CQ.
321 *
322 * This routine will mark all Event Queue Entries on @q, from the last
323 * known completed entry to the last entry that was processed, as completed
324 * by clearing the valid bit for each completion queue entry. Then it will
325 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
326 * The internal host index in the @q will be updated by this routine to indicate
327 * that the host has finished processing the entries. The @arm parameter
328 * indicates that the queue should be rearmed when ringing the doorbell.
329 *
330 * This function will return the number of EQEs that were popped.
331 **/
332uint32_t
333lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
334{
335 uint32_t released = 0;
336 struct lpfc_eqe *temp_eqe;
337 struct lpfc_register doorbell;
338
2e90f4b5
JS
339 /* sanity check on queue memory */
340 if (unlikely(!q))
341 return 0;
342
4f774513
JS
343 /* while there are valid entries */
344 while (q->hba_index != q->host_index) {
345 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 346 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
347 released++;
348 q->host_index = ((q->host_index + 1) % q->entry_count);
349 }
350 if (unlikely(released == 0 && !arm))
351 return 0;
352
353 /* ring doorbell for number popped */
354 doorbell.word0 = 0;
355 if (arm) {
356 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
357 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
358 }
359 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
360 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
361 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
362 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
363 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 364 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
365 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
366 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
367 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
368 return released;
369}
370
371/**
372 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
373 * @q: The Completion Queue to get the first valid CQE from
374 *
375 * This routine will get the first valid Completion Queue Entry from @q, update
376 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
377 * the Queue (no more work to do), or the Queue is full of CQEs that have been
378 * processed, but not popped back to the HBA then this routine will return NULL.
379 **/
380static struct lpfc_cqe *
381lpfc_sli4_cq_get(struct lpfc_queue *q)
382{
383 struct lpfc_cqe *cqe;
027140ea 384 uint32_t idx;
4f774513 385
2e90f4b5
JS
386 /* sanity check on queue memory */
387 if (unlikely(!q))
388 return NULL;
389
4f774513 390 /* If the next CQE is not valid then we are done */
cb5172ea 391 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
392 return NULL;
393 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
394 idx = ((q->hba_index + 1) % q->entry_count);
395 if (idx == q->host_index)
4f774513
JS
396 return NULL;
397
398 cqe = q->qe[q->hba_index].cqe;
027140ea 399 q->hba_index = idx;
27f344eb
JS
400
401 /*
402 * insert barrier for instruction interlock : data from the hardware
403 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
404 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
405 * instructions allowing action on content before valid bit checked,
406 * add barrier here as well. May not be needed as "content" is a
407 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
408 */
409 mb();
4f774513
JS
410 return cqe;
411}
412
413/**
414 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
415 * @q: The Completion Queue that the host has completed processing for.
416 * @arm: Indicates whether the host wants to arms this CQ.
417 *
418 * This routine will mark all Completion queue entries on @q, from the last
419 * known completed entry to the last entry that was processed, as completed
420 * by clearing the valid bit for each completion queue entry. Then it will
421 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
422 * The internal host index in the @q will be updated by this routine to indicate
423 * that the host has finished processing the entries. The @arm parameter
424 * indicates that the queue should be rearmed when ringing the doorbell.
425 *
426 * This function will return the number of CQEs that were released.
427 **/
428uint32_t
429lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
430{
431 uint32_t released = 0;
432 struct lpfc_cqe *temp_qe;
433 struct lpfc_register doorbell;
434
2e90f4b5
JS
435 /* sanity check on queue memory */
436 if (unlikely(!q))
437 return 0;
4f774513
JS
438 /* while there are valid entries */
439 while (q->hba_index != q->host_index) {
440 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 441 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
442 released++;
443 q->host_index = ((q->host_index + 1) % q->entry_count);
444 }
445 if (unlikely(released == 0 && !arm))
446 return 0;
447
448 /* ring doorbell for number popped */
449 doorbell.word0 = 0;
450 if (arm)
451 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
452 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
453 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
454 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
455 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
456 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
457 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
458 return released;
459}
460
461/**
462 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
463 * @q: The Header Receive Queue to operate on.
464 * @wqe: The Receive Queue Entry to put on the Receive queue.
465 *
466 * This routine will copy the contents of @wqe to the next available entry on
467 * the @q. This function will then ring the Receive Queue Doorbell to signal the
468 * HBA to start processing the Receive Queue Entry. This function returns the
469 * index that the rqe was copied to if successful. If no entries are available
470 * on @q then this function will return -ENOMEM.
471 * The caller is expected to hold the hbalock when calling this routine.
472 **/
895427bd 473int
4f774513
JS
474lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
475 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
476{
2e90f4b5
JS
477 struct lpfc_rqe *temp_hrqe;
478 struct lpfc_rqe *temp_drqe;
4f774513 479 struct lpfc_register doorbell;
cbc5de1b
JS
480 int hq_put_index;
481 int dq_put_index;
4f774513 482
2e90f4b5
JS
483 /* sanity check on queue memory */
484 if (unlikely(!hq) || unlikely(!dq))
485 return -ENOMEM;
cbc5de1b
JS
486 hq_put_index = hq->host_index;
487 dq_put_index = dq->host_index;
488 temp_hrqe = hq->qe[hq_put_index].rqe;
489 temp_drqe = dq->qe[dq_put_index].rqe;
2e90f4b5 490
4f774513
JS
491 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
492 return -EINVAL;
cbc5de1b 493 if (hq_put_index != dq_put_index)
4f774513
JS
494 return -EINVAL;
495 /* If the host has not yet processed the next entry then we are done */
cbc5de1b 496 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513
JS
497 return -EBUSY;
498 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
499 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
500
501 /* Update the host index to point to the next slot */
cbc5de1b
JS
502 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
503 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
61f3d4bf 504 hq->RQ_buf_posted++;
4f774513
JS
505
506 /* Ring The Header Receive Queue Doorbell */
73d91e50 507 if (!(hq->host_index % hq->entry_repost)) {
4f774513 508 doorbell.word0 = 0;
962bc51b
JS
509 if (hq->db_format == LPFC_DB_RING_FORMAT) {
510 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
511 hq->entry_repost);
512 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
513 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
514 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
515 hq->entry_repost);
516 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
517 hq->host_index);
518 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
519 } else {
520 return -EINVAL;
521 }
522 writel(doorbell.word0, hq->db_regaddr);
4f774513 523 }
cbc5de1b 524 return hq_put_index;
4f774513
JS
525}
526
527/**
528 * lpfc_sli4_rq_release - Updates internal hba index for RQ
529 * @q: The Header Receive Queue to operate on.
530 *
531 * This routine will update the HBA index of a queue to reflect consumption of
532 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
533 * consumed an entry the host calls this function to update the queue's
534 * internal pointers. This routine returns the number of entries that were
535 * consumed by the HBA.
536 **/
537static uint32_t
538lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
539{
2e90f4b5
JS
540 /* sanity check on queue memory */
541 if (unlikely(!hq) || unlikely(!dq))
542 return 0;
543
4f774513
JS
544 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
545 return 0;
546 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
547 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
548 return 1;
549}
550
e59058c4 551/**
3621a710 552 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
553 * @phba: Pointer to HBA context object.
554 * @pring: Pointer to driver SLI ring object.
555 *
556 * This function returns pointer to next command iocb entry
557 * in the command ring. The caller must hold hbalock to prevent
558 * other threads consume the next command iocb.
559 * SLI-2/SLI-3 provide different sized iocbs.
560 **/
ed957684
JS
561static inline IOCB_t *
562lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
563{
7e56aa25
JS
564 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
565 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
566}
567
e59058c4 568/**
3621a710 569 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
570 * @phba: Pointer to HBA context object.
571 * @pring: Pointer to driver SLI ring object.
572 *
573 * This function returns pointer to next response iocb entry
574 * in the response ring. The caller must hold hbalock to make sure
575 * that no other thread consume the next response iocb.
576 * SLI-2/SLI-3 provide different sized iocbs.
577 **/
ed957684
JS
578static inline IOCB_t *
579lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
580{
7e56aa25
JS
581 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
582 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
583}
584
e59058c4 585/**
3621a710 586 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
587 * @phba: Pointer to HBA context object.
588 *
589 * This function is called with hbalock held. This function
590 * allocates a new driver iocb object from the iocb pool. If the
591 * allocation is successful, it returns pointer to the newly
592 * allocated iocb object else it returns NULL.
593 **/
4f2e66c6 594struct lpfc_iocbq *
2e0fef85 595__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
596{
597 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
598 struct lpfc_iocbq * iocbq = NULL;
599
1c2ba475
JT
600 lockdep_assert_held(&phba->hbalock);
601
0bd4ca25 602 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
603 if (iocbq)
604 phba->iocb_cnt++;
605 if (phba->iocb_cnt > phba->iocb_max)
606 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
607 return iocbq;
608}
609
da0436e9
JS
610/**
611 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
612 * @phba: Pointer to HBA context object.
613 * @xritag: XRI value.
614 *
615 * This function clears the sglq pointer from the array of acive
616 * sglq's. The xritag that is passed in is used to index into the
617 * array. Before the xritag can be used it needs to be adjusted
618 * by subtracting the xribase.
619 *
620 * Returns sglq ponter = success, NULL = Failure.
621 **/
895427bd 622struct lpfc_sglq *
da0436e9
JS
623__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
624{
da0436e9 625 struct lpfc_sglq *sglq;
6d368e53
JS
626
627 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
628 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
629 return sglq;
630}
631
632/**
633 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
634 * @phba: Pointer to HBA context object.
635 * @xritag: XRI value.
636 *
637 * This function returns the sglq pointer from the array of acive
638 * sglq's. The xritag that is passed in is used to index into the
639 * array. Before the xritag can be used it needs to be adjusted
640 * by subtracting the xribase.
641 *
642 * Returns sglq ponter = success, NULL = Failure.
643 **/
0f65ff68 644struct lpfc_sglq *
da0436e9
JS
645__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
646{
da0436e9 647 struct lpfc_sglq *sglq;
6d368e53
JS
648
649 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
650 return sglq;
651}
652
19ca7609 653/**
1151e3ec 654 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
655 * @phba: Pointer to HBA context object.
656 * @xritag: xri used in this exchange.
657 * @rrq: The RRQ to be cleared.
658 *
19ca7609 659 **/
1151e3ec
JS
660void
661lpfc_clr_rrq_active(struct lpfc_hba *phba,
662 uint16_t xritag,
663 struct lpfc_node_rrq *rrq)
19ca7609 664{
1151e3ec 665 struct lpfc_nodelist *ndlp = NULL;
19ca7609 666
1151e3ec
JS
667 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
668 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
669
670 /* The target DID could have been swapped (cable swap)
671 * we should use the ndlp from the findnode if it is
672 * available.
673 */
1151e3ec 674 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
675 ndlp = rrq->ndlp;
676
1151e3ec
JS
677 if (!ndlp)
678 goto out;
679
cff261f6 680 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
681 rrq->send_rrq = 0;
682 rrq->xritag = 0;
683 rrq->rrq_stop_time = 0;
684 }
1151e3ec 685out:
19ca7609
JS
686 mempool_free(rrq, phba->rrq_pool);
687}
688
689/**
690 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
691 * @phba: Pointer to HBA context object.
692 *
693 * This function is called with hbalock held. This function
694 * Checks if stop_time (ratov from setting rrq active) has
695 * been reached, if it has and the send_rrq flag is set then
696 * it will call lpfc_send_rrq. If the send_rrq flag is not set
697 * then it will just call the routine to clear the rrq and
698 * free the rrq resource.
699 * The timer is set to the next rrq that is going to expire before
700 * leaving the routine.
701 *
702 **/
703void
704lpfc_handle_rrq_active(struct lpfc_hba *phba)
705{
706 struct lpfc_node_rrq *rrq;
707 struct lpfc_node_rrq *nextrrq;
708 unsigned long next_time;
709 unsigned long iflags;
1151e3ec 710 LIST_HEAD(send_rrq);
19ca7609
JS
711
712 spin_lock_irqsave(&phba->hbalock, iflags);
713 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 714 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 715 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
716 &phba->active_rrq_list, list) {
717 if (time_after(jiffies, rrq->rrq_stop_time))
718 list_move(&rrq->list, &send_rrq);
719 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
720 next_time = rrq->rrq_stop_time;
721 }
722 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
723 if ((!list_empty(&phba->active_rrq_list)) &&
724 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 725 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
726 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
727 list_del(&rrq->list);
728 if (!rrq->send_rrq)
729 /* this call will free the rrq */
730 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
731 else if (lpfc_send_rrq(phba, rrq)) {
732 /* if we send the rrq then the completion handler
733 * will clear the bit in the xribitmap.
734 */
735 lpfc_clr_rrq_active(phba, rrq->xritag,
736 rrq);
737 }
738 }
19ca7609
JS
739}
740
741/**
742 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
743 * @vport: Pointer to vport context object.
744 * @xri: The xri used in the exchange.
745 * @did: The targets DID for this exchange.
746 *
747 * returns NULL = rrq not found in the phba->active_rrq_list.
748 * rrq = rrq for this xri and target.
749 **/
750struct lpfc_node_rrq *
751lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
752{
753 struct lpfc_hba *phba = vport->phba;
754 struct lpfc_node_rrq *rrq;
755 struct lpfc_node_rrq *nextrrq;
756 unsigned long iflags;
757
758 if (phba->sli_rev != LPFC_SLI_REV4)
759 return NULL;
760 spin_lock_irqsave(&phba->hbalock, iflags);
761 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
762 if (rrq->vport == vport && rrq->xritag == xri &&
763 rrq->nlp_DID == did){
764 list_del(&rrq->list);
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766 return rrq;
767 }
768 }
769 spin_unlock_irqrestore(&phba->hbalock, iflags);
770 return NULL;
771}
772
773/**
774 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
775 * @vport: Pointer to vport context object.
1151e3ec
JS
776 * @ndlp: Pointer to the lpfc_node_list structure.
777 * If ndlp is NULL Remove all active RRQs for this vport from the
778 * phba->active_rrq_list and clear the rrq.
779 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
780 **/
781void
1151e3ec 782lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
783
784{
785 struct lpfc_hba *phba = vport->phba;
786 struct lpfc_node_rrq *rrq;
787 struct lpfc_node_rrq *nextrrq;
788 unsigned long iflags;
1151e3ec 789 LIST_HEAD(rrq_list);
19ca7609
JS
790
791 if (phba->sli_rev != LPFC_SLI_REV4)
792 return;
1151e3ec
JS
793 if (!ndlp) {
794 lpfc_sli4_vport_delete_els_xri_aborted(vport);
795 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 796 }
1151e3ec
JS
797 spin_lock_irqsave(&phba->hbalock, iflags);
798 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
799 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
800 list_move(&rrq->list, &rrq_list);
19ca7609 801 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
802
803 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
804 list_del(&rrq->list);
805 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
806 }
19ca7609
JS
807}
808
19ca7609 809/**
1151e3ec 810 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
811 * @phba: Pointer to HBA context object.
812 * @ndlp: Targets nodelist pointer for this exchange.
813 * @xritag the xri in the bitmap to test.
814 *
815 * This function is called with hbalock held. This function
816 * returns 0 = rrq not active for this xri
817 * 1 = rrq is valid for this xri.
818 **/
1151e3ec
JS
819int
820lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
821 uint16_t xritag)
822{
1c2ba475 823 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
824 if (!ndlp)
825 return 0;
cff261f6
JS
826 if (!ndlp->active_rrqs_xri_bitmap)
827 return 0;
828 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
829 return 1;
830 else
831 return 0;
832}
833
834/**
835 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
836 * @phba: Pointer to HBA context object.
837 * @ndlp: nodelist pointer for this target.
838 * @xritag: xri used in this exchange.
839 * @rxid: Remote Exchange ID.
840 * @send_rrq: Flag used to determine if we should send rrq els cmd.
841 *
842 * This function takes the hbalock.
843 * The active bit is always set in the active rrq xri_bitmap even
844 * if there is no slot avaiable for the other rrq information.
845 *
846 * returns 0 rrq actived for this xri
847 * < 0 No memory or invalid ndlp.
848 **/
849int
850lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 851 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 852{
19ca7609 853 unsigned long iflags;
b42c07c8
JS
854 struct lpfc_node_rrq *rrq;
855 int empty;
856
857 if (!ndlp)
858 return -EINVAL;
859
860 if (!phba->cfg_enable_rrq)
861 return -EINVAL;
19ca7609
JS
862
863 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
864 if (phba->pport->load_flag & FC_UNLOADING) {
865 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
866 goto out;
867 }
868
869 /*
870 * set the active bit even if there is no mem available.
871 */
872 if (NLP_CHK_FREE_REQ(ndlp))
873 goto out;
874
875 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
876 goto out;
877
cff261f6
JS
878 if (!ndlp->active_rrqs_xri_bitmap)
879 goto out;
880
881 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
882 goto out;
883
19ca7609 884 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
885 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
886 if (!rrq) {
887 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
888 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
889 " DID:0x%x Send:%d\n",
890 xritag, rxid, ndlp->nlp_DID, send_rrq);
891 return -EINVAL;
892 }
e5771b4d
JS
893 if (phba->cfg_enable_rrq == 1)
894 rrq->send_rrq = send_rrq;
895 else
896 rrq->send_rrq = 0;
b42c07c8 897 rrq->xritag = xritag;
256ec0d0
JS
898 rrq->rrq_stop_time = jiffies +
899 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
900 rrq->ndlp = ndlp;
901 rrq->nlp_DID = ndlp->nlp_DID;
902 rrq->vport = ndlp->vport;
903 rrq->rxid = rxid;
b42c07c8
JS
904 spin_lock_irqsave(&phba->hbalock, iflags);
905 empty = list_empty(&phba->active_rrq_list);
906 list_add_tail(&rrq->list, &phba->active_rrq_list);
907 phba->hba_flag |= HBA_RRQ_ACTIVE;
908 if (empty)
909 lpfc_worker_wake_up(phba);
910 spin_unlock_irqrestore(&phba->hbalock, iflags);
911 return 0;
912out:
913 spin_unlock_irqrestore(&phba->hbalock, iflags);
914 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
915 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
916 " DID:0x%x Send:%d\n",
917 xritag, rxid, ndlp->nlp_DID, send_rrq);
918 return -EINVAL;
19ca7609
JS
919}
920
da0436e9 921/**
895427bd 922 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 923 * @phba: Pointer to HBA context object.
19ca7609 924 * @piocb: Pointer to the iocbq.
da0436e9 925 *
dafe8cea 926 * This function is called with the ring lock held. This function
6d368e53 927 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
928 * list is not empty then it is successful, it returns pointer to the newly
929 * allocated sglq object else it returns NULL.
930 **/
931static struct lpfc_sglq *
895427bd 932__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 933{
895427bd 934 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 935 struct lpfc_sglq *sglq = NULL;
19ca7609 936 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
937 struct lpfc_scsi_buf *lpfc_cmd;
938 struct lpfc_nodelist *ndlp;
939 int found = 0;
940
1c2ba475
JT
941 lockdep_assert_held(&phba->hbalock);
942
19ca7609
JS
943 if (piocbq->iocb_flag & LPFC_IO_FCP) {
944 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
945 ndlp = lpfc_cmd->rdata->pnode;
be858b65 946 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 947 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 948 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
949 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
950 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
951 ndlp = NULL;
952 else
953 ndlp = piocbq->context_un.ndlp;
954 } else {
19ca7609 955 ndlp = piocbq->context1;
6c7cf486 956 }
19ca7609 957
895427bd
JS
958 spin_lock(&phba->sli4_hba.sgl_list_lock);
959 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
960 start_sglq = sglq;
961 while (!found) {
962 if (!sglq)
d11f54b7 963 break;
895427bd
JS
964 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
965 test_bit(sglq->sli4_lxritag,
966 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
967 /* This xri has an rrq outstanding for this DID.
968 * put it back in the list and get another xri.
969 */
895427bd 970 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 971 sglq = NULL;
895427bd 972 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
973 struct lpfc_sglq, list);
974 if (sglq == start_sglq) {
14041bd1 975 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
976 sglq = NULL;
977 break;
978 } else
979 continue;
980 }
981 sglq->ndlp = ndlp;
982 found = 1;
6d368e53 983 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
984 sglq->state = SGL_ALLOCATED;
985 }
895427bd 986 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
987 return sglq;
988}
989
f358dd0c
JS
990/**
991 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
992 * @phba: Pointer to HBA context object.
993 * @piocb: Pointer to the iocbq.
994 *
995 * This function is called with the sgl_list lock held. This function
996 * gets a new driver sglq object from the sglq list. If the
997 * list is not empty then it is successful, it returns pointer to the newly
998 * allocated sglq object else it returns NULL.
999 **/
1000struct lpfc_sglq *
1001__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1002{
1003 struct list_head *lpfc_nvmet_sgl_list;
1004 struct lpfc_sglq *sglq = NULL;
1005
1006 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1007
1008 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1009
1010 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1011 if (!sglq)
1012 return NULL;
1013 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1014 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1015 return sglq;
1016}
1017
e59058c4 1018/**
3621a710 1019 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1020 * @phba: Pointer to HBA context object.
1021 *
1022 * This function is called with no lock held. This function
1023 * allocates a new driver iocb object from the iocb pool. If the
1024 * allocation is successful, it returns pointer to the newly
1025 * allocated iocb object else it returns NULL.
1026 **/
2e0fef85
JS
1027struct lpfc_iocbq *
1028lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1029{
1030 struct lpfc_iocbq * iocbq = NULL;
1031 unsigned long iflags;
1032
1033 spin_lock_irqsave(&phba->hbalock, iflags);
1034 iocbq = __lpfc_sli_get_iocbq(phba);
1035 spin_unlock_irqrestore(&phba->hbalock, iflags);
1036 return iocbq;
1037}
1038
4f774513
JS
1039/**
1040 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1041 * @phba: Pointer to HBA context object.
1042 * @iocbq: Pointer to driver iocb object.
1043 *
1044 * This function is called with hbalock held to release driver
1045 * iocb object to the iocb pool. The iotag in the iocb object
1046 * does not change for each use of the iocb object. This function
1047 * clears all other fields of the iocb object when it is freed.
1048 * The sqlq structure that holds the xritag and phys and virtual
1049 * mappings for the scatter gather list is retrieved from the
1050 * active array of sglq. The get of the sglq pointer also clears
1051 * the entry in the array. If the status of the IO indiactes that
1052 * this IO was aborted then the sglq entry it put on the
1053 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1054 * IO has good status or fails for any other reason then the sglq
895427bd 1055 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1056 **/
1057static void
1058__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1059{
1060 struct lpfc_sglq *sglq;
1061 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1062 unsigned long iflag = 0;
895427bd 1063 struct lpfc_sli_ring *pring;
4f774513 1064
1c2ba475
JT
1065 lockdep_assert_held(&phba->hbalock);
1066
4f774513
JS
1067 if (iocbq->sli4_xritag == NO_XRI)
1068 sglq = NULL;
1069 else
6d368e53
JS
1070 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1071
0e9bb8d7 1072
4f774513 1073 if (sglq) {
f358dd0c
JS
1074 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1075 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1076 iflag);
1077 sglq->state = SGL_FREED;
1078 sglq->ndlp = NULL;
1079 list_add_tail(&sglq->list,
1080 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1081 spin_unlock_irqrestore(
1082 &phba->sli4_hba.sgl_list_lock, iflag);
1083 goto out;
1084 }
1085
895427bd 1086 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1087 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1088 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1089 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1090 iflag);
4f774513 1091 list_add(&sglq->list,
895427bd 1092 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1093 spin_unlock_irqrestore(
895427bd 1094 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1095 } else {
895427bd
JS
1096 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1097 iflag);
0f65ff68 1098 sglq->state = SGL_FREED;
19ca7609 1099 sglq->ndlp = NULL;
fedd3b7b 1100 list_add_tail(&sglq->list,
895427bd
JS
1101 &phba->sli4_hba.lpfc_els_sgl_list);
1102 spin_unlock_irqrestore(
1103 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1104
1105 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1106 if (!list_empty(&pring->txq))
2a9bf3d0 1107 lpfc_worker_wake_up(phba);
0f65ff68 1108 }
4f774513
JS
1109 }
1110
f358dd0c 1111out:
4f774513
JS
1112 /*
1113 * Clean all volatile data fields, preserve iotag and node struct.
1114 */
1115 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1116 iocbq->sli4_lxritag = NO_XRI;
4f774513 1117 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1118 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1119 LPFC_IO_NVME_LS);
4f774513
JS
1120 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1121}
1122
2a9bf3d0 1123
e59058c4 1124/**
3772a991 1125 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1126 * @phba: Pointer to HBA context object.
1127 * @iocbq: Pointer to driver iocb object.
1128 *
1129 * This function is called with hbalock held to release driver
1130 * iocb object to the iocb pool. The iotag in the iocb object
1131 * does not change for each use of the iocb object. This function
1132 * clears all other fields of the iocb object when it is freed.
1133 **/
a6ababd2 1134static void
3772a991 1135__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1136{
2e0fef85 1137 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1138
1c2ba475 1139 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1140
604a3e30
JB
1141 /*
1142 * Clean all volatile data fields, preserve iotag and node struct.
1143 */
1144 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1145 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1146 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1147}
1148
3772a991
JS
1149/**
1150 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1151 * @phba: Pointer to HBA context object.
1152 * @iocbq: Pointer to driver iocb object.
1153 *
1154 * This function is called with hbalock held to release driver
1155 * iocb object to the iocb pool. The iotag in the iocb object
1156 * does not change for each use of the iocb object. This function
1157 * clears all other fields of the iocb object when it is freed.
1158 **/
1159static void
1160__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1161{
1c2ba475
JT
1162 lockdep_assert_held(&phba->hbalock);
1163
3772a991 1164 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1165 phba->iocb_cnt--;
3772a991
JS
1166}
1167
e59058c4 1168/**
3621a710 1169 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1170 * @phba: Pointer to HBA context object.
1171 * @iocbq: Pointer to driver iocb object.
1172 *
1173 * This function is called with no lock held to release the iocb to
1174 * iocb pool.
1175 **/
2e0fef85
JS
1176void
1177lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1178{
1179 unsigned long iflags;
1180
1181 /*
1182 * Clean all volatile data fields, preserve iotag and node struct.
1183 */
1184 spin_lock_irqsave(&phba->hbalock, iflags);
1185 __lpfc_sli_release_iocbq(phba, iocbq);
1186 spin_unlock_irqrestore(&phba->hbalock, iflags);
1187}
1188
a257bf90
JS
1189/**
1190 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1191 * @phba: Pointer to HBA context object.
1192 * @iocblist: List of IOCBs.
1193 * @ulpstatus: ULP status in IOCB command field.
1194 * @ulpWord4: ULP word-4 in IOCB command field.
1195 *
1196 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1197 * on the list by invoking the complete callback function associated with the
1198 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1199 * fields.
1200 **/
1201void
1202lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1203 uint32_t ulpstatus, uint32_t ulpWord4)
1204{
1205 struct lpfc_iocbq *piocb;
1206
1207 while (!list_empty(iocblist)) {
1208 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1209 if (!piocb->iocb_cmpl)
1210 lpfc_sli_release_iocbq(phba, piocb);
1211 else {
1212 piocb->iocb.ulpStatus = ulpstatus;
1213 piocb->iocb.un.ulpWord[4] = ulpWord4;
1214 (piocb->iocb_cmpl) (phba, piocb, piocb);
1215 }
1216 }
1217 return;
1218}
1219
e59058c4 1220/**
3621a710
JS
1221 * lpfc_sli_iocb_cmd_type - Get the iocb type
1222 * @iocb_cmnd: iocb command code.
e59058c4
JS
1223 *
1224 * This function is called by ring event handler function to get the iocb type.
1225 * This function translates the iocb command to an iocb command type used to
1226 * decide the final disposition of each completed IOCB.
1227 * The function returns
1228 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1229 * LPFC_SOL_IOCB if it is a solicited iocb completion
1230 * LPFC_ABORT_IOCB if it is an abort iocb
1231 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1232 *
1233 * The caller is not required to hold any lock.
1234 **/
dea3101e
JB
1235static lpfc_iocb_type
1236lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1237{
1238 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1239
1240 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1241 return 0;
1242
1243 switch (iocb_cmnd) {
1244 case CMD_XMIT_SEQUENCE_CR:
1245 case CMD_XMIT_SEQUENCE_CX:
1246 case CMD_XMIT_BCAST_CN:
1247 case CMD_XMIT_BCAST_CX:
1248 case CMD_ELS_REQUEST_CR:
1249 case CMD_ELS_REQUEST_CX:
1250 case CMD_CREATE_XRI_CR:
1251 case CMD_CREATE_XRI_CX:
1252 case CMD_GET_RPI_CN:
1253 case CMD_XMIT_ELS_RSP_CX:
1254 case CMD_GET_RPI_CR:
1255 case CMD_FCP_IWRITE_CR:
1256 case CMD_FCP_IWRITE_CX:
1257 case CMD_FCP_IREAD_CR:
1258 case CMD_FCP_IREAD_CX:
1259 case CMD_FCP_ICMND_CR:
1260 case CMD_FCP_ICMND_CX:
f5603511
JS
1261 case CMD_FCP_TSEND_CX:
1262 case CMD_FCP_TRSP_CX:
1263 case CMD_FCP_TRECEIVE_CX:
1264 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1265 case CMD_ADAPTER_MSG:
1266 case CMD_ADAPTER_DUMP:
1267 case CMD_XMIT_SEQUENCE64_CR:
1268 case CMD_XMIT_SEQUENCE64_CX:
1269 case CMD_XMIT_BCAST64_CN:
1270 case CMD_XMIT_BCAST64_CX:
1271 case CMD_ELS_REQUEST64_CR:
1272 case CMD_ELS_REQUEST64_CX:
1273 case CMD_FCP_IWRITE64_CR:
1274 case CMD_FCP_IWRITE64_CX:
1275 case CMD_FCP_IREAD64_CR:
1276 case CMD_FCP_IREAD64_CX:
1277 case CMD_FCP_ICMND64_CR:
1278 case CMD_FCP_ICMND64_CX:
f5603511
JS
1279 case CMD_FCP_TSEND64_CX:
1280 case CMD_FCP_TRSP64_CX:
1281 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1282 case CMD_GEN_REQUEST64_CR:
1283 case CMD_GEN_REQUEST64_CX:
1284 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1285 case DSSCMD_IWRITE64_CR:
1286 case DSSCMD_IWRITE64_CX:
1287 case DSSCMD_IREAD64_CR:
1288 case DSSCMD_IREAD64_CX:
dea3101e
JB
1289 type = LPFC_SOL_IOCB;
1290 break;
1291 case CMD_ABORT_XRI_CN:
1292 case CMD_ABORT_XRI_CX:
1293 case CMD_CLOSE_XRI_CN:
1294 case CMD_CLOSE_XRI_CX:
1295 case CMD_XRI_ABORTED_CX:
1296 case CMD_ABORT_MXRI64_CN:
6669f9bb 1297 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1298 type = LPFC_ABORT_IOCB;
1299 break;
1300 case CMD_RCV_SEQUENCE_CX:
1301 case CMD_RCV_ELS_REQ_CX:
1302 case CMD_RCV_SEQUENCE64_CX:
1303 case CMD_RCV_ELS_REQ64_CX:
57127f15 1304 case CMD_ASYNC_STATUS:
ed957684
JS
1305 case CMD_IOCB_RCV_SEQ64_CX:
1306 case CMD_IOCB_RCV_ELS64_CX:
1307 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1308 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1309 type = LPFC_UNSOL_IOCB;
1310 break;
3163f725
JS
1311 case CMD_IOCB_XMIT_MSEQ64_CR:
1312 case CMD_IOCB_XMIT_MSEQ64_CX:
1313 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1314 case CMD_IOCB_RCV_ELS_LIST64_CX:
1315 case CMD_IOCB_CLOSE_EXTENDED_CN:
1316 case CMD_IOCB_ABORT_EXTENDED_CN:
1317 case CMD_IOCB_RET_HBQE64_CN:
1318 case CMD_IOCB_FCP_IBIDIR64_CR:
1319 case CMD_IOCB_FCP_IBIDIR64_CX:
1320 case CMD_IOCB_FCP_ITASKMGT64_CX:
1321 case CMD_IOCB_LOGENTRY_CN:
1322 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1323 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1324 __func__, iocb_cmnd);
3163f725
JS
1325 type = LPFC_UNKNOWN_IOCB;
1326 break;
dea3101e
JB
1327 default:
1328 type = LPFC_UNKNOWN_IOCB;
1329 break;
1330 }
1331
1332 return type;
1333}
1334
e59058c4 1335/**
3621a710 1336 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1337 * @phba: Pointer to HBA context object.
1338 *
1339 * This function is called from SLI initialization code
1340 * to configure every ring of the HBA's SLI interface. The
1341 * caller is not required to hold any lock. This function issues
1342 * a config_ring mailbox command for each ring.
1343 * This function returns zero if successful else returns a negative
1344 * error code.
1345 **/
dea3101e 1346static int
ed957684 1347lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1348{
1349 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1350 LPFC_MBOXQ_t *pmb;
1351 MAILBOX_t *pmbox;
1352 int i, rc, ret = 0;
dea3101e 1353
ed957684
JS
1354 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1355 if (!pmb)
1356 return -ENOMEM;
04c68496 1357 pmbox = &pmb->u.mb;
ed957684 1358 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1359 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1360 lpfc_config_ring(phba, i, pmb);
1361 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1362 if (rc != MBX_SUCCESS) {
92d7f7b0 1363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1364 "0446 Adapter failed to init (%d), "
dea3101e
JB
1365 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1366 "ring %d\n",
e8b62011
JS
1367 rc, pmbox->mbxCommand,
1368 pmbox->mbxStatus, i);
2e0fef85 1369 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1370 ret = -ENXIO;
1371 break;
dea3101e
JB
1372 }
1373 }
ed957684
JS
1374 mempool_free(pmb, phba->mbox_mem_pool);
1375 return ret;
dea3101e
JB
1376}
1377
e59058c4 1378/**
3621a710 1379 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1380 * @phba: Pointer to HBA context object.
1381 * @pring: Pointer to driver SLI ring object.
1382 * @piocb: Pointer to the driver iocb object.
1383 *
1384 * This function is called with hbalock held. The function adds the
1385 * new iocb to txcmplq of the given ring. This function always returns
1386 * 0. If this function is called for ELS ring, this function checks if
1387 * there is a vport associated with the ELS command. This function also
1388 * starts els_tmofunc timer if this is an ELS command.
1389 **/
dea3101e 1390static int
2e0fef85
JS
1391lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1392 struct lpfc_iocbq *piocb)
dea3101e 1393{
1c2ba475
JT
1394 lockdep_assert_held(&phba->hbalock);
1395
2319f847 1396 BUG_ON(!piocb);
22466da5 1397
dea3101e 1398 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1399 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1400
92d7f7b0
JS
1401 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1402 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1403 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1404 BUG_ON(!piocb->vport);
1405 if (!(piocb->vport->load_flag & FC_UNLOADING))
1406 mod_timer(&piocb->vport->els_tmofunc,
1407 jiffies +
1408 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1409 }
dea3101e 1410
2e0fef85 1411 return 0;
dea3101e
JB
1412}
1413
e59058c4 1414/**
3621a710 1415 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1416 * @phba: Pointer to HBA context object.
1417 * @pring: Pointer to driver SLI ring object.
1418 *
1419 * This function is called with hbalock held to get next
1420 * iocb in txq of the given ring. If there is any iocb in
1421 * the txq, the function returns first iocb in the list after
1422 * removing the iocb from the list, else it returns NULL.
1423 **/
2a9bf3d0 1424struct lpfc_iocbq *
2e0fef85 1425lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1426{
dea3101e
JB
1427 struct lpfc_iocbq *cmd_iocb;
1428
1c2ba475
JT
1429 lockdep_assert_held(&phba->hbalock);
1430
858c9f6c 1431 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1432 return cmd_iocb;
dea3101e
JB
1433}
1434
e59058c4 1435/**
3621a710 1436 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1437 * @phba: Pointer to HBA context object.
1438 * @pring: Pointer to driver SLI ring object.
1439 *
1440 * This function is called with hbalock held and the caller must post the
1441 * iocb without releasing the lock. If the caller releases the lock,
1442 * iocb slot returned by the function is not guaranteed to be available.
1443 * The function returns pointer to the next available iocb slot if there
1444 * is available slot in the ring, else it returns NULL.
1445 * If the get index of the ring is ahead of the put index, the function
1446 * will post an error attention event to the worker thread to take the
1447 * HBA to offline state.
1448 **/
dea3101e
JB
1449static IOCB_t *
1450lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1451{
34b02dcd 1452 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1453 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1454
1455 lockdep_assert_held(&phba->hbalock);
1456
7e56aa25
JS
1457 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1458 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1459 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1460
7e56aa25
JS
1461 if (unlikely(pring->sli.sli3.local_getidx ==
1462 pring->sli.sli3.next_cmdidx)) {
dea3101e 1463
7e56aa25 1464 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1465
7e56aa25 1466 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1467 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1468 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1469 "is bigger than cmd ring %d\n",
e8b62011 1470 pring->ringno,
7e56aa25
JS
1471 pring->sli.sli3.local_getidx,
1472 max_cmd_idx);
dea3101e 1473
2e0fef85 1474 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1475 /*
1476 * All error attention handlers are posted to
1477 * worker thread
1478 */
1479 phba->work_ha |= HA_ERATT;
1480 phba->work_hs = HS_FFER3;
92d7f7b0 1481
5e9d9b82 1482 lpfc_worker_wake_up(phba);
dea3101e
JB
1483
1484 return NULL;
1485 }
1486
7e56aa25 1487 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1488 return NULL;
1489 }
1490
ed957684 1491 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1492}
1493
e59058c4 1494/**
3621a710 1495 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1496 * @phba: Pointer to HBA context object.
1497 * @iocbq: Pointer to driver iocb object.
1498 *
1499 * This function gets an iotag for the iocb. If there is no unused iotag and
1500 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1501 * array and assigns a new iotag.
1502 * The function returns the allocated iotag if successful, else returns zero.
1503 * Zero is not a valid iotag.
1504 * The caller is not required to hold any lock.
1505 **/
604a3e30 1506uint16_t
2e0fef85 1507lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1508{
2e0fef85
JS
1509 struct lpfc_iocbq **new_arr;
1510 struct lpfc_iocbq **old_arr;
604a3e30
JB
1511 size_t new_len;
1512 struct lpfc_sli *psli = &phba->sli;
1513 uint16_t iotag;
dea3101e 1514
2e0fef85 1515 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1516 iotag = psli->last_iotag;
1517 if(++iotag < psli->iocbq_lookup_len) {
1518 psli->last_iotag = iotag;
1519 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1520 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1521 iocbq->iotag = iotag;
1522 return iotag;
2e0fef85 1523 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1524 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1525 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1526 spin_unlock_irq(&phba->hbalock);
1527 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1528 GFP_KERNEL);
1529 if (new_arr) {
2e0fef85 1530 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1531 old_arr = psli->iocbq_lookup;
1532 if (new_len <= psli->iocbq_lookup_len) {
1533 /* highly unprobable case */
1534 kfree(new_arr);
1535 iotag = psli->last_iotag;
1536 if(++iotag < psli->iocbq_lookup_len) {
1537 psli->last_iotag = iotag;
1538 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1539 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1540 iocbq->iotag = iotag;
1541 return iotag;
1542 }
2e0fef85 1543 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1544 return 0;
1545 }
1546 if (psli->iocbq_lookup)
1547 memcpy(new_arr, old_arr,
1548 ((psli->last_iotag + 1) *
311464ec 1549 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1550 psli->iocbq_lookup = new_arr;
1551 psli->iocbq_lookup_len = new_len;
1552 psli->last_iotag = iotag;
1553 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1554 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1555 iocbq->iotag = iotag;
1556 kfree(old_arr);
1557 return iotag;
1558 }
8f6d98d2 1559 } else
2e0fef85 1560 spin_unlock_irq(&phba->hbalock);
dea3101e 1561
bc73905a 1562 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1563 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1564 psli->last_iotag);
dea3101e 1565
604a3e30 1566 return 0;
dea3101e
JB
1567}
1568
e59058c4 1569/**
3621a710 1570 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1571 * @phba: Pointer to HBA context object.
1572 * @pring: Pointer to driver SLI ring object.
1573 * @iocb: Pointer to iocb slot in the ring.
1574 * @nextiocb: Pointer to driver iocb object which need to be
1575 * posted to firmware.
1576 *
1577 * This function is called with hbalock held to post a new iocb to
1578 * the firmware. This function copies the new iocb to ring iocb slot and
1579 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1580 * a completion call back for this iocb else the function will free the
1581 * iocb object.
1582 **/
dea3101e
JB
1583static void
1584lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1585 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1586{
1c2ba475 1587 lockdep_assert_held(&phba->hbalock);
dea3101e 1588 /*
604a3e30 1589 * Set up an iotag
dea3101e 1590 */
604a3e30 1591 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1592
e2a0a9d6 1593
a58cbd52
JS
1594 if (pring->ringno == LPFC_ELS_RING) {
1595 lpfc_debugfs_slow_ring_trc(phba,
1596 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1597 *(((uint32_t *) &nextiocb->iocb) + 4),
1598 *(((uint32_t *) &nextiocb->iocb) + 6),
1599 *(((uint32_t *) &nextiocb->iocb) + 7));
1600 }
1601
dea3101e
JB
1602 /*
1603 * Issue iocb command to adapter
1604 */
92d7f7b0 1605 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1606 wmb();
1607 pring->stats.iocb_cmd++;
1608
1609 /*
1610 * If there is no completion routine to call, we can release the
1611 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1612 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1613 */
1614 if (nextiocb->iocb_cmpl)
1615 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1616 else
2e0fef85 1617 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1618
1619 /*
1620 * Let the HBA know what IOCB slot will be the next one the
1621 * driver will put a command into.
1622 */
7e56aa25
JS
1623 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1624 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1625}
1626
e59058c4 1627/**
3621a710 1628 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1629 * @phba: Pointer to HBA context object.
1630 * @pring: Pointer to driver SLI ring object.
1631 *
1632 * The caller is not required to hold any lock for calling this function.
1633 * This function updates the chip attention bits for the ring to inform firmware
1634 * that there are pending work to be done for this ring and requests an
1635 * interrupt when there is space available in the ring. This function is
1636 * called when the driver is unable to post more iocbs to the ring due
1637 * to unavailability of space in the ring.
1638 **/
dea3101e 1639static void
2e0fef85 1640lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1641{
1642 int ringno = pring->ringno;
1643
1644 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1645
1646 wmb();
1647
1648 /*
1649 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1650 * The HBA will tell us when an IOCB entry is available.
1651 */
1652 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1653 readl(phba->CAregaddr); /* flush */
1654
1655 pring->stats.iocb_cmd_full++;
1656}
1657
e59058c4 1658/**
3621a710 1659 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1660 * @phba: Pointer to HBA context object.
1661 * @pring: Pointer to driver SLI ring object.
1662 *
1663 * This function updates the chip attention register bit for the
1664 * given ring to inform HBA that there is more work to be done
1665 * in this ring. The caller is not required to hold any lock.
1666 **/
dea3101e 1667static void
2e0fef85 1668lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1669{
1670 int ringno = pring->ringno;
1671
1672 /*
1673 * Tell the HBA that there is work to do in this ring.
1674 */
34b02dcd
JS
1675 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1676 wmb();
1677 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1678 readl(phba->CAregaddr); /* flush */
1679 }
dea3101e
JB
1680}
1681
e59058c4 1682/**
3621a710 1683 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1684 * @phba: Pointer to HBA context object.
1685 * @pring: Pointer to driver SLI ring object.
1686 *
1687 * This function is called with hbalock held to post pending iocbs
1688 * in the txq to the firmware. This function is called when driver
1689 * detects space available in the ring.
1690 **/
dea3101e 1691static void
2e0fef85 1692lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1693{
1694 IOCB_t *iocb;
1695 struct lpfc_iocbq *nextiocb;
1696
1c2ba475
JT
1697 lockdep_assert_held(&phba->hbalock);
1698
dea3101e
JB
1699 /*
1700 * Check to see if:
1701 * (a) there is anything on the txq to send
1702 * (b) link is up
1703 * (c) link attention events can be processed (fcp ring only)
1704 * (d) IOCB processing is not blocked by the outstanding mbox command.
1705 */
0e9bb8d7
JS
1706
1707 if (lpfc_is_link_up(phba) &&
1708 (!list_empty(&pring->txq)) &&
895427bd 1709 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1710 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1711
1712 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1713 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1714 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1715
1716 if (iocb)
1717 lpfc_sli_update_ring(phba, pring);
1718 else
1719 lpfc_sli_update_full_ring(phba, pring);
1720 }
1721
1722 return;
1723}
1724
e59058c4 1725/**
3621a710 1726 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1727 * @phba: Pointer to HBA context object.
1728 * @hbqno: HBQ number.
1729 *
1730 * This function is called with hbalock held to get the next
1731 * available slot for the given HBQ. If there is free slot
1732 * available for the HBQ it will return pointer to the next available
1733 * HBQ entry else it will return NULL.
1734 **/
a6ababd2 1735static struct lpfc_hbq_entry *
ed957684
JS
1736lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1737{
1738 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1739
1c2ba475
JT
1740 lockdep_assert_held(&phba->hbalock);
1741
ed957684
JS
1742 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1743 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1744 hbqp->next_hbqPutIdx = 0;
1745
1746 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1747 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1748 uint32_t getidx = le32_to_cpu(raw_index);
1749
1750 hbqp->local_hbqGetIdx = getidx;
1751
1752 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1753 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1754 LOG_SLI | LOG_VPORT,
e8b62011 1755 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1756 "%u is > than hbqp->entry_count %u\n",
e8b62011 1757 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1758 hbqp->entry_count);
1759
1760 phba->link_state = LPFC_HBA_ERROR;
1761 return NULL;
1762 }
1763
1764 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1765 return NULL;
1766 }
1767
51ef4c26
JS
1768 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1769 hbqp->hbqPutIdx;
ed957684
JS
1770}
1771
e59058c4 1772/**
3621a710 1773 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1774 * @phba: Pointer to HBA context object.
1775 *
1776 * This function is called with no lock held to free all the
1777 * hbq buffers while uninitializing the SLI interface. It also
1778 * frees the HBQ buffers returned by the firmware but not yet
1779 * processed by the upper layers.
1780 **/
ed957684
JS
1781void
1782lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1783{
92d7f7b0
JS
1784 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1785 struct hbq_dmabuf *hbq_buf;
3163f725 1786 unsigned long flags;
51ef4c26 1787 int i, hbq_count;
ed957684 1788
51ef4c26 1789 hbq_count = lpfc_sli_hbq_count();
ed957684 1790 /* Return all memory used by all HBQs */
3163f725 1791 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1792 for (i = 0; i < hbq_count; ++i) {
1793 list_for_each_entry_safe(dmabuf, next_dmabuf,
1794 &phba->hbqs[i].hbq_buffer_list, list) {
1795 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1796 list_del(&hbq_buf->dbuf.list);
1797 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1798 }
a8adb832 1799 phba->hbqs[i].buffer_count = 0;
ed957684 1800 }
3163f725
JS
1801
1802 /* Mark the HBQs not in use */
1803 phba->hbq_in_use = 0;
1804 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1805}
1806
e59058c4 1807/**
3621a710 1808 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1809 * @phba: Pointer to HBA context object.
1810 * @hbqno: HBQ number.
1811 * @hbq_buf: Pointer to HBQ buffer.
1812 *
1813 * This function is called with the hbalock held to post a
1814 * hbq buffer to the firmware. If the function finds an empty
1815 * slot in the HBQ, it will post the buffer. The function will return
1816 * pointer to the hbq entry if it successfully post the buffer
1817 * else it will return NULL.
1818 **/
3772a991 1819static int
ed957684 1820lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1821 struct hbq_dmabuf *hbq_buf)
3772a991 1822{
1c2ba475 1823 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1824 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1825}
1826
1827/**
1828 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1829 * @phba: Pointer to HBA context object.
1830 * @hbqno: HBQ number.
1831 * @hbq_buf: Pointer to HBQ buffer.
1832 *
1833 * This function is called with the hbalock held to post a hbq buffer to the
1834 * firmware. If the function finds an empty slot in the HBQ, it will post the
1835 * buffer and place it on the hbq_buffer_list. The function will return zero if
1836 * it successfully post the buffer else it will return an error.
1837 **/
1838static int
1839lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1840 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1841{
1842 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1843 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 1844
1c2ba475 1845 lockdep_assert_held(&phba->hbalock);
ed957684
JS
1846 /* Get next HBQ entry slot to use */
1847 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1848 if (hbqe) {
1849 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1850
92d7f7b0
JS
1851 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1852 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 1853 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 1854 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1855 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1856 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1857 /* Sync SLIM */
ed957684
JS
1858 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1859 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1860 /* flush */
ed957684 1861 readl(phba->hbq_put + hbqno);
51ef4c26 1862 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1863 return 0;
1864 } else
1865 return -ENOMEM;
ed957684
JS
1866}
1867
4f774513
JS
1868/**
1869 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1870 * @phba: Pointer to HBA context object.
1871 * @hbqno: HBQ number.
1872 * @hbq_buf: Pointer to HBQ buffer.
1873 *
1874 * This function is called with the hbalock held to post an RQE to the SLI4
1875 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1876 * the hbq_buffer_list and return zero, otherwise it will return an error.
1877 **/
1878static int
1879lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1880 struct hbq_dmabuf *hbq_buf)
1881{
1882 int rc;
1883 struct lpfc_rqe hrqe;
1884 struct lpfc_rqe drqe;
895427bd
JS
1885 struct lpfc_queue *hrq;
1886 struct lpfc_queue *drq;
1887
1888 if (hbqno != LPFC_ELS_HBQ)
1889 return 1;
1890 hrq = phba->sli4_hba.hdr_rq;
1891 drq = phba->sli4_hba.dat_rq;
4f774513 1892
1c2ba475 1893 lockdep_assert_held(&phba->hbalock);
4f774513
JS
1894 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1895 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1896 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1897 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 1898 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
1899 if (rc < 0)
1900 return rc;
895427bd 1901 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
1902 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1903 return 0;
1904}
1905
e59058c4 1906/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1907static struct lpfc_hbq_init lpfc_els_hbq = {
1908 .rn = 1,
def9c7a9 1909 .entry_count = 256,
92d7f7b0
JS
1910 .mask_count = 0,
1911 .profile = 0,
51ef4c26 1912 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1913 .buffer_count = 0,
a257bf90
JS
1914 .init_count = 40,
1915 .add_count = 40,
92d7f7b0 1916};
ed957684 1917
e59058c4 1918/* Array of HBQs */
78b2d852 1919struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
1920 &lpfc_els_hbq,
1921};
ed957684 1922
e59058c4 1923/**
3621a710 1924 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1925 * @phba: Pointer to HBA context object.
1926 * @hbqno: HBQ number.
1927 * @count: Number of HBQ buffers to be posted.
1928 *
d7c255b2
JS
1929 * This function is called with no lock held to post more hbq buffers to the
1930 * given HBQ. The function returns the number of HBQ buffers successfully
1931 * posted.
e59058c4 1932 **/
311464ec 1933static int
92d7f7b0 1934lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1935{
d7c255b2 1936 uint32_t i, posted = 0;
3163f725 1937 unsigned long flags;
92d7f7b0 1938 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1939 LIST_HEAD(hbq_buf_list);
eafe1df9 1940 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1941 return 0;
51ef4c26 1942
d7c255b2
JS
1943 if ((phba->hbqs[hbqno].buffer_count + count) >
1944 lpfc_hbq_defs[hbqno]->entry_count)
1945 count = lpfc_hbq_defs[hbqno]->entry_count -
1946 phba->hbqs[hbqno].buffer_count;
1947 if (!count)
1948 return 0;
1949 /* Allocate HBQ entries */
1950 for (i = 0; i < count; i++) {
1951 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1952 if (!hbq_buffer)
1953 break;
1954 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1955 }
3163f725
JS
1956 /* Check whether HBQ is still in use */
1957 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1958 if (!phba->hbq_in_use)
d7c255b2
JS
1959 goto err;
1960 while (!list_empty(&hbq_buf_list)) {
1961 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1962 dbuf.list);
1963 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1964 (hbqno << 16));
3772a991 1965 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1966 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1967 posted++;
1968 } else
51ef4c26 1969 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1970 }
3163f725 1971 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1972 return posted;
1973err:
eafe1df9 1974 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1975 while (!list_empty(&hbq_buf_list)) {
1976 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1977 dbuf.list);
1978 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1979 }
1980 return 0;
ed957684
JS
1981}
1982
e59058c4 1983/**
3621a710 1984 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1985 * @phba: Pointer to HBA context object.
1986 * @qno: HBQ number.
1987 *
1988 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1989 * is called with no lock held. The function returns the number of HBQ entries
1990 * successfully allocated.
e59058c4 1991 **/
92d7f7b0
JS
1992int
1993lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1994{
def9c7a9
JS
1995 if (phba->sli_rev == LPFC_SLI_REV4)
1996 return 0;
1997 else
1998 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1999 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 2000}
ed957684 2001
e59058c4 2002/**
3621a710 2003 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2004 * @phba: Pointer to HBA context object.
2005 * @qno: HBQ queue number.
2006 *
2007 * This function is called from SLI initialization code path with
2008 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2009 * function returns the number of HBQ entries successfully allocated.
e59058c4 2010 **/
a6ababd2 2011static int
92d7f7b0
JS
2012lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2013{
def9c7a9
JS
2014 if (phba->sli_rev == LPFC_SLI_REV4)
2015 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2016 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2017 else
2018 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2019 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2020}
2021
3772a991
JS
2022/**
2023 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2024 * @phba: Pointer to HBA context object.
2025 * @hbqno: HBQ number.
2026 *
2027 * This function removes the first hbq buffer on an hbq list and returns a
2028 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2029 **/
2030static struct hbq_dmabuf *
2031lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2032{
2033 struct lpfc_dmabuf *d_buf;
2034
2035 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2036 if (!d_buf)
2037 return NULL;
2038 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2039}
2040
2d7dbc4c
JS
2041/**
2042 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2043 * @phba: Pointer to HBA context object.
2044 * @hbqno: HBQ number.
2045 *
2046 * This function removes the first RQ buffer on an RQ buffer list and returns a
2047 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2048 **/
2049static struct rqb_dmabuf *
2050lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2051{
2052 struct lpfc_dmabuf *h_buf;
2053 struct lpfc_rqb *rqbp;
2054
2055 rqbp = hrq->rqbp;
2056 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2057 struct lpfc_dmabuf, list);
2058 if (!h_buf)
2059 return NULL;
2060 rqbp->buffer_count--;
2061 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2062}
2063
e59058c4 2064/**
3621a710 2065 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2066 * @phba: Pointer to HBA context object.
2067 * @tag: Tag of the hbq buffer.
2068 *
71892418
SH
2069 * This function searches for the hbq buffer associated with the given tag in
2070 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2071 * otherwise it returns NULL.
e59058c4 2072 **/
a6ababd2 2073static struct hbq_dmabuf *
92d7f7b0 2074lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2075{
92d7f7b0
JS
2076 struct lpfc_dmabuf *d_buf;
2077 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2078 uint32_t hbqno;
2079
2080 hbqno = tag >> 16;
a0a74e45 2081 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2082 return NULL;
ed957684 2083
3772a991 2084 spin_lock_irq(&phba->hbalock);
51ef4c26 2085 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2086 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2087 if (hbq_buf->tag == tag) {
3772a991 2088 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2089 return hbq_buf;
ed957684
JS
2090 }
2091 }
3772a991 2092 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2093 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2094 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2095 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2096 return NULL;
ed957684
JS
2097}
2098
e59058c4 2099/**
3621a710 2100 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2101 * @phba: Pointer to HBA context object.
2102 * @hbq_buffer: Pointer to HBQ buffer.
2103 *
2104 * This function is called with hbalock. This function gives back
2105 * the hbq buffer to firmware. If the HBQ does not have space to
2106 * post the buffer, it will free the buffer.
2107 **/
ed957684 2108void
51ef4c26 2109lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2110{
2111 uint32_t hbqno;
2112
51ef4c26
JS
2113 if (hbq_buffer) {
2114 hbqno = hbq_buffer->tag >> 16;
3772a991 2115 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2116 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2117 }
2118}
2119
e59058c4 2120/**
3621a710 2121 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2122 * @mbxCommand: mailbox command code.
2123 *
2124 * This function is called by the mailbox event handler function to verify
2125 * that the completed mailbox command is a legitimate mailbox command. If the
2126 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2127 * and the mailbox event handler will take the HBA offline.
2128 **/
dea3101e
JB
2129static int
2130lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2131{
2132 uint8_t ret;
2133
2134 switch (mbxCommand) {
2135 case MBX_LOAD_SM:
2136 case MBX_READ_NV:
2137 case MBX_WRITE_NV:
a8adb832 2138 case MBX_WRITE_VPARMS:
dea3101e
JB
2139 case MBX_RUN_BIU_DIAG:
2140 case MBX_INIT_LINK:
2141 case MBX_DOWN_LINK:
2142 case MBX_CONFIG_LINK:
2143 case MBX_CONFIG_RING:
2144 case MBX_RESET_RING:
2145 case MBX_READ_CONFIG:
2146 case MBX_READ_RCONFIG:
2147 case MBX_READ_SPARM:
2148 case MBX_READ_STATUS:
2149 case MBX_READ_RPI:
2150 case MBX_READ_XRI:
2151 case MBX_READ_REV:
2152 case MBX_READ_LNK_STAT:
2153 case MBX_REG_LOGIN:
2154 case MBX_UNREG_LOGIN:
dea3101e
JB
2155 case MBX_CLEAR_LA:
2156 case MBX_DUMP_MEMORY:
2157 case MBX_DUMP_CONTEXT:
2158 case MBX_RUN_DIAGS:
2159 case MBX_RESTART:
2160 case MBX_UPDATE_CFG:
2161 case MBX_DOWN_LOAD:
2162 case MBX_DEL_LD_ENTRY:
2163 case MBX_RUN_PROGRAM:
2164 case MBX_SET_MASK:
09372820 2165 case MBX_SET_VARIABLE:
dea3101e 2166 case MBX_UNREG_D_ID:
41415862 2167 case MBX_KILL_BOARD:
dea3101e 2168 case MBX_CONFIG_FARP:
41415862 2169 case MBX_BEACON:
dea3101e
JB
2170 case MBX_LOAD_AREA:
2171 case MBX_RUN_BIU_DIAG64:
2172 case MBX_CONFIG_PORT:
2173 case MBX_READ_SPARM64:
2174 case MBX_READ_RPI64:
2175 case MBX_REG_LOGIN64:
76a95d75 2176 case MBX_READ_TOPOLOGY:
09372820 2177 case MBX_WRITE_WWN:
dea3101e
JB
2178 case MBX_SET_DEBUG:
2179 case MBX_LOAD_EXP_ROM:
57127f15 2180 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2181 case MBX_REG_VPI:
2182 case MBX_UNREG_VPI:
858c9f6c 2183 case MBX_HEARTBEAT:
84774a4d
JS
2184 case MBX_PORT_CAPABILITIES:
2185 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2186 case MBX_SLI4_CONFIG:
2187 case MBX_SLI4_REQ_FTRS:
2188 case MBX_REG_FCFI:
2189 case MBX_UNREG_FCFI:
2190 case MBX_REG_VFI:
2191 case MBX_UNREG_VFI:
2192 case MBX_INIT_VPI:
2193 case MBX_INIT_VFI:
2194 case MBX_RESUME_RPI:
c7495937
JS
2195 case MBX_READ_EVENT_LOG_STATUS:
2196 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2197 case MBX_SECURITY_MGMT:
2198 case MBX_AUTH_PORT:
940eb687 2199 case MBX_ACCESS_VDATA:
dea3101e
JB
2200 ret = mbxCommand;
2201 break;
2202 default:
2203 ret = MBX_SHUTDOWN;
2204 break;
2205 }
2e0fef85 2206 return ret;
dea3101e 2207}
e59058c4
JS
2208
2209/**
3621a710 2210 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2211 * @phba: Pointer to HBA context object.
2212 * @pmboxq: Pointer to mailbox command.
2213 *
2214 * This is completion handler function for mailbox commands issued from
2215 * lpfc_sli_issue_mbox_wait function. This function is called by the
2216 * mailbox event handler function with no lock held. This function
2217 * will wake up thread waiting on the wait queue pointed by context1
2218 * of the mailbox.
2219 **/
04c68496 2220void
2e0fef85 2221lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2222{
2223 wait_queue_head_t *pdone_q;
858c9f6c 2224 unsigned long drvr_flag;
dea3101e
JB
2225
2226 /*
2227 * If pdone_q is empty, the driver thread gave up waiting and
2228 * continued running.
2229 */
7054a606 2230 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2231 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2232 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2233 if (pdone_q)
2234 wake_up_interruptible(pdone_q);
858c9f6c 2235 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2236 return;
2237}
2238
e59058c4
JS
2239
2240/**
3621a710 2241 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2242 * @phba: Pointer to HBA context object.
2243 * @pmb: Pointer to mailbox object.
2244 *
2245 * This function is the default mailbox completion handler. It
2246 * frees the memory resources associated with the completed mailbox
2247 * command. If the completed command is a REG_LOGIN mailbox command,
2248 * this function will issue a UREG_LOGIN to re-claim the RPI.
2249 **/
dea3101e 2250void
2e0fef85 2251lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2252{
d439d286 2253 struct lpfc_vport *vport = pmb->vport;
dea3101e 2254 struct lpfc_dmabuf *mp;
d439d286 2255 struct lpfc_nodelist *ndlp;
5af5eee7 2256 struct Scsi_Host *shost;
04c68496 2257 uint16_t rpi, vpi;
7054a606
JS
2258 int rc;
2259
dea3101e 2260 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2261
dea3101e
JB
2262 if (mp) {
2263 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2264 kfree(mp);
2265 }
7054a606
JS
2266
2267 /*
2268 * If a REG_LOGIN succeeded after node is destroyed or node
2269 * is in re-discovery driver need to cleanup the RPI.
2270 */
2e0fef85 2271 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2272 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2273 !pmb->u.mb.mbxStatus) {
2274 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2275 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2276 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2277 pmb->vport = vport;
92d7f7b0 2278 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2279 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2280 if (rc != MBX_NOT_FINISHED)
2281 return;
2282 }
2283
695a814e
JS
2284 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2285 !(phba->pport->load_flag & FC_UNLOADING) &&
2286 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2287 shost = lpfc_shost_from_vport(vport);
2288 spin_lock_irq(shost->host_lock);
2289 vport->vpi_state |= LPFC_VPI_REGISTERED;
2290 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2291 spin_unlock_irq(shost->host_lock);
695a814e
JS
2292 }
2293
d439d286
JS
2294 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2295 ndlp = (struct lpfc_nodelist *)pmb->context2;
2296 lpfc_nlp_put(ndlp);
2297 pmb->context2 = NULL;
2298 }
2299
dcf2a4e0
JS
2300 /* Check security permission status on INIT_LINK mailbox command */
2301 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2302 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2304 "2860 SLI authentication is required "
2305 "for INIT_LINK but has not done yet\n");
2306
04c68496
JS
2307 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2308 lpfc_sli4_mbox_cmd_free(phba, pmb);
2309 else
2310 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2311}
be6bb941
JS
2312 /**
2313 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2314 * @phba: Pointer to HBA context object.
2315 * @pmb: Pointer to mailbox object.
2316 *
2317 * This function is the unreg rpi mailbox completion handler. It
2318 * frees the memory resources associated with the completed mailbox
2319 * command. An additional refrenece is put on the ndlp to prevent
2320 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2321 * the unreg mailbox command completes, this routine puts the
2322 * reference back.
2323 *
2324 **/
2325void
2326lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2327{
2328 struct lpfc_vport *vport = pmb->vport;
2329 struct lpfc_nodelist *ndlp;
2330
2331 ndlp = pmb->context1;
2332 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2333 if (phba->sli_rev == LPFC_SLI_REV4 &&
2334 (bf_get(lpfc_sli_intf_if_type,
2335 &phba->sli4_hba.sli_intf) ==
2336 LPFC_SLI_INTF_IF_TYPE_2)) {
2337 if (ndlp) {
2338 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2339 "0010 UNREG_LOGIN vpi:%x "
2340 "rpi:%x DID:%x map:%x %p\n",
2341 vport->vpi, ndlp->nlp_rpi,
2342 ndlp->nlp_DID,
2343 ndlp->nlp_usg_map, ndlp);
7c5e518c 2344 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2345 lpfc_nlp_put(ndlp);
2346 }
2347 }
2348 }
2349
2350 mempool_free(pmb, phba->mbox_mem_pool);
2351}
dea3101e 2352
e59058c4 2353/**
3621a710 2354 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2355 * @phba: Pointer to HBA context object.
2356 *
2357 * This function is called with no lock held. This function processes all
2358 * the completed mailbox commands and gives it to upper layers. The interrupt
2359 * service routine processes mailbox completion interrupt and adds completed
2360 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2361 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2362 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2363 * function returns the mailbox commands to the upper layer by calling the
2364 * completion handler function of each mailbox.
2365 **/
dea3101e 2366int
2e0fef85 2367lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2368{
92d7f7b0 2369 MAILBOX_t *pmbox;
dea3101e 2370 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2371 int rc;
2372 LIST_HEAD(cmplq);
dea3101e
JB
2373
2374 phba->sli.slistat.mbox_event++;
2375
92d7f7b0
JS
2376 /* Get all completed mailboxe buffers into the cmplq */
2377 spin_lock_irq(&phba->hbalock);
2378 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2379 spin_unlock_irq(&phba->hbalock);
dea3101e 2380
92d7f7b0
JS
2381 /* Get a Mailbox buffer to setup mailbox commands for callback */
2382 do {
2383 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2384 if (pmb == NULL)
2385 break;
2e0fef85 2386
04c68496 2387 pmbox = &pmb->u.mb;
dea3101e 2388
858c9f6c
JS
2389 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2390 if (pmb->vport) {
2391 lpfc_debugfs_disc_trc(pmb->vport,
2392 LPFC_DISC_TRC_MBOX_VPORT,
2393 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2394 (uint32_t)pmbox->mbxCommand,
2395 pmbox->un.varWords[0],
2396 pmbox->un.varWords[1]);
2397 }
2398 else {
2399 lpfc_debugfs_disc_trc(phba->pport,
2400 LPFC_DISC_TRC_MBOX,
2401 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2402 (uint32_t)pmbox->mbxCommand,
2403 pmbox->un.varWords[0],
2404 pmbox->un.varWords[1]);
2405 }
2406 }
2407
dea3101e
JB
2408 /*
2409 * It is a fatal error if unknown mbox command completion.
2410 */
2411 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2412 MBX_SHUTDOWN) {
af901ca1 2413 /* Unknown mailbox command compl */
92d7f7b0 2414 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2415 "(%d):0323 Unknown Mailbox command "
a183a15f 2416 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2417 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2418 pmbox->mbxCommand,
a183a15f
JS
2419 lpfc_sli_config_mbox_subsys_get(phba,
2420 pmb),
2421 lpfc_sli_config_mbox_opcode_get(phba,
2422 pmb));
2e0fef85 2423 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2424 phba->work_hs = HS_FFER3;
2425 lpfc_handle_eratt(phba);
92d7f7b0 2426 continue;
dea3101e
JB
2427 }
2428
dea3101e
JB
2429 if (pmbox->mbxStatus) {
2430 phba->sli.slistat.mbox_stat_err++;
2431 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2432 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2433 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2434 LOG_MBOX | LOG_SLI,
2435 "(%d):0305 Mbox cmd cmpl "
2436 "error - RETRYing Data: x%x "
2437 "(x%x/x%x) x%x x%x x%x\n",
2438 pmb->vport ? pmb->vport->vpi : 0,
2439 pmbox->mbxCommand,
2440 lpfc_sli_config_mbox_subsys_get(phba,
2441 pmb),
2442 lpfc_sli_config_mbox_opcode_get(phba,
2443 pmb),
2444 pmbox->mbxStatus,
2445 pmbox->un.varWords[0],
2446 pmb->vport->port_state);
dea3101e
JB
2447 pmbox->mbxStatus = 0;
2448 pmbox->mbxOwner = OWN_HOST;
dea3101e 2449 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2450 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2451 continue;
dea3101e
JB
2452 }
2453 }
2454
2455 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2456 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2457 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2458 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2459 "x%x x%x x%x\n",
92d7f7b0 2460 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2461 pmbox->mbxCommand,
a183a15f
JS
2462 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2463 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2464 pmb->mbox_cmpl,
2465 *((uint32_t *) pmbox),
2466 pmbox->un.varWords[0],
2467 pmbox->un.varWords[1],
2468 pmbox->un.varWords[2],
2469 pmbox->un.varWords[3],
2470 pmbox->un.varWords[4],
2471 pmbox->un.varWords[5],
2472 pmbox->un.varWords[6],
e74c03c8
JS
2473 pmbox->un.varWords[7],
2474 pmbox->un.varWords[8],
2475 pmbox->un.varWords[9],
2476 pmbox->un.varWords[10]);
dea3101e 2477
92d7f7b0 2478 if (pmb->mbox_cmpl)
dea3101e 2479 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2480 } while (1);
2481 return 0;
2482}
dea3101e 2483
e59058c4 2484/**
3621a710 2485 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2486 * @phba: Pointer to HBA context object.
2487 * @pring: Pointer to driver SLI ring object.
2488 * @tag: buffer tag.
2489 *
2490 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2491 * is set in the tag the buffer is posted for a particular exchange,
2492 * the function will return the buffer without replacing the buffer.
2493 * If the buffer is for unsolicited ELS or CT traffic, this function
2494 * returns the buffer and also posts another buffer to the firmware.
2495 **/
76bb24ef
JS
2496static struct lpfc_dmabuf *
2497lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2498 struct lpfc_sli_ring *pring,
2499 uint32_t tag)
76bb24ef 2500{
9f1e1b50
JS
2501 struct hbq_dmabuf *hbq_entry;
2502
76bb24ef
JS
2503 if (tag & QUE_BUFTAG_BIT)
2504 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2505 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2506 if (!hbq_entry)
2507 return NULL;
2508 return &hbq_entry->dbuf;
76bb24ef 2509}
57127f15 2510
3772a991
JS
2511/**
2512 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2513 * @phba: Pointer to HBA context object.
2514 * @pring: Pointer to driver SLI ring object.
2515 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2516 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2517 * @fch_type: the type for the first frame of the sequence.
2518 *
2519 * This function is called with no lock held. This function uses the r_ctl and
2520 * type of the received sequence to find the correct callback function to call
2521 * to process the sequence.
2522 **/
2523static int
2524lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2525 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2526 uint32_t fch_type)
2527{
2528 int i;
2529
f358dd0c
JS
2530 switch (fch_type) {
2531 case FC_TYPE_NVME:
d613b6a7 2532 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2533 return 1;
2534 default:
2535 break;
2536 }
2537
3772a991
JS
2538 /* unSolicited Responses */
2539 if (pring->prt[0].profile) {
2540 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2541 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2542 saveq);
2543 return 1;
2544 }
2545 /* We must search, based on rctl / type
2546 for the right routine */
2547 for (i = 0; i < pring->num_mask; i++) {
2548 if ((pring->prt[i].rctl == fch_r_ctl) &&
2549 (pring->prt[i].type == fch_type)) {
2550 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2551 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2552 (phba, pring, saveq);
2553 return 1;
2554 }
2555 }
2556 return 0;
2557}
e59058c4
JS
2558
2559/**
3621a710 2560 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2561 * @phba: Pointer to HBA context object.
2562 * @pring: Pointer to driver SLI ring object.
2563 * @saveq: Pointer to the unsolicited iocb.
2564 *
2565 * This function is called with no lock held by the ring event handler
2566 * when there is an unsolicited iocb posted to the response ring by the
2567 * firmware. This function gets the buffer associated with the iocbs
2568 * and calls the event handler for the ring. This function handles both
2569 * qring buffers and hbq buffers.
2570 * When the function returns 1 the caller can free the iocb object otherwise
2571 * upper layer functions will free the iocb objects.
2572 **/
dea3101e
JB
2573static int
2574lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2575 struct lpfc_iocbq *saveq)
2576{
2577 IOCB_t * irsp;
2578 WORD5 * w5p;
2579 uint32_t Rctl, Type;
76bb24ef 2580 struct lpfc_iocbq *iocbq;
3163f725 2581 struct lpfc_dmabuf *dmzbuf;
dea3101e 2582
dea3101e 2583 irsp = &(saveq->iocb);
57127f15
JS
2584
2585 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2586 if (pring->lpfc_sli_rcv_async_status)
2587 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2588 else
2589 lpfc_printf_log(phba,
2590 KERN_WARNING,
2591 LOG_SLI,
2592 "0316 Ring %d handler: unexpected "
2593 "ASYNC_STATUS iocb received evt_code "
2594 "0x%x\n",
2595 pring->ringno,
2596 irsp->un.asyncstat.evt_code);
2597 return 1;
2598 }
2599
3163f725
JS
2600 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2601 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2602 if (irsp->ulpBdeCount > 0) {
2603 dmzbuf = lpfc_sli_get_buff(phba, pring,
2604 irsp->un.ulpWord[3]);
2605 lpfc_in_buf_free(phba, dmzbuf);
2606 }
2607
2608 if (irsp->ulpBdeCount > 1) {
2609 dmzbuf = lpfc_sli_get_buff(phba, pring,
2610 irsp->unsli3.sli3Words[3]);
2611 lpfc_in_buf_free(phba, dmzbuf);
2612 }
2613
2614 if (irsp->ulpBdeCount > 2) {
2615 dmzbuf = lpfc_sli_get_buff(phba, pring,
2616 irsp->unsli3.sli3Words[7]);
2617 lpfc_in_buf_free(phba, dmzbuf);
2618 }
2619
2620 return 1;
2621 }
2622
92d7f7b0 2623 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2624 if (irsp->ulpBdeCount != 0) {
2625 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2626 irsp->un.ulpWord[3]);
2627 if (!saveq->context2)
2628 lpfc_printf_log(phba,
2629 KERN_ERR,
2630 LOG_SLI,
2631 "0341 Ring %d Cannot find buffer for "
2632 "an unsolicited iocb. tag 0x%x\n",
2633 pring->ringno,
2634 irsp->un.ulpWord[3]);
76bb24ef
JS
2635 }
2636 if (irsp->ulpBdeCount == 2) {
2637 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2638 irsp->unsli3.sli3Words[7]);
2639 if (!saveq->context3)
2640 lpfc_printf_log(phba,
2641 KERN_ERR,
2642 LOG_SLI,
2643 "0342 Ring %d Cannot find buffer for an"
2644 " unsolicited iocb. tag 0x%x\n",
2645 pring->ringno,
2646 irsp->unsli3.sli3Words[7]);
2647 }
2648 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2649 irsp = &(iocbq->iocb);
76bb24ef
JS
2650 if (irsp->ulpBdeCount != 0) {
2651 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2652 irsp->un.ulpWord[3]);
9c2face6 2653 if (!iocbq->context2)
76bb24ef
JS
2654 lpfc_printf_log(phba,
2655 KERN_ERR,
2656 LOG_SLI,
2657 "0343 Ring %d Cannot find "
2658 "buffer for an unsolicited iocb"
2659 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2660 irsp->un.ulpWord[3]);
76bb24ef
JS
2661 }
2662 if (irsp->ulpBdeCount == 2) {
2663 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2664 irsp->unsli3.sli3Words[7]);
9c2face6 2665 if (!iocbq->context3)
76bb24ef
JS
2666 lpfc_printf_log(phba,
2667 KERN_ERR,
2668 LOG_SLI,
2669 "0344 Ring %d Cannot find "
2670 "buffer for an unsolicited "
2671 "iocb. tag 0x%x\n",
2672 pring->ringno,
2673 irsp->unsli3.sli3Words[7]);
2674 }
2675 }
92d7f7b0 2676 }
9c2face6
JS
2677 if (irsp->ulpBdeCount != 0 &&
2678 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2679 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2680 int found = 0;
2681
2682 /* search continue save q for same XRI */
2683 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2684 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2685 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2686 list_add_tail(&saveq->list, &iocbq->list);
2687 found = 1;
2688 break;
2689 }
2690 }
2691 if (!found)
2692 list_add_tail(&saveq->clist,
2693 &pring->iocb_continue_saveq);
2694 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2695 list_del_init(&iocbq->clist);
2696 saveq = iocbq;
2697 irsp = &(saveq->iocb);
2698 } else
2699 return 0;
2700 }
2701 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2702 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2703 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2704 Rctl = FC_RCTL_ELS_REQ;
2705 Type = FC_TYPE_ELS;
9c2face6
JS
2706 } else {
2707 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2708 Rctl = w5p->hcsw.Rctl;
2709 Type = w5p->hcsw.Type;
2710
2711 /* Firmware Workaround */
2712 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2713 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2714 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2715 Rctl = FC_RCTL_ELS_REQ;
2716 Type = FC_TYPE_ELS;
9c2face6
JS
2717 w5p->hcsw.Rctl = Rctl;
2718 w5p->hcsw.Type = Type;
2719 }
2720 }
92d7f7b0 2721
3772a991 2722 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2723 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2724 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2725 "Type x%x received\n",
e8b62011 2726 pring->ringno, Rctl, Type);
3772a991 2727
92d7f7b0 2728 return 1;
dea3101e
JB
2729}
2730
e59058c4 2731/**
3621a710 2732 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2733 * @phba: Pointer to HBA context object.
2734 * @pring: Pointer to driver SLI ring object.
2735 * @prspiocb: Pointer to response iocb object.
2736 *
2737 * This function looks up the iocb_lookup table to get the command iocb
2738 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2739 * response iocb. This function is called with the hbalock held
2740 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2741 * This function returns the command iocb object if it finds the command
2742 * iocb else returns NULL.
2743 **/
dea3101e 2744static struct lpfc_iocbq *
2e0fef85
JS
2745lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2746 struct lpfc_sli_ring *pring,
2747 struct lpfc_iocbq *prspiocb)
dea3101e 2748{
dea3101e
JB
2749 struct lpfc_iocbq *cmd_iocb = NULL;
2750 uint16_t iotag;
1c2ba475 2751 lockdep_assert_held(&phba->hbalock);
dea3101e 2752
604a3e30
JB
2753 iotag = prspiocb->iocb.ulpIoTag;
2754
2755 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2756 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2757 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2758 /* remove from txcmpl queue list */
2759 list_del_init(&cmd_iocb->list);
4f2e66c6 2760 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2761 return cmd_iocb;
2a9bf3d0 2762 }
dea3101e
JB
2763 }
2764
dea3101e 2765 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2766 "0317 iotag x%x is out of "
604a3e30 2767 "range: max iotag x%x wd0 x%x\n",
e8b62011 2768 iotag, phba->sli.last_iotag,
604a3e30 2769 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2770 return NULL;
2771}
2772
3772a991
JS
2773/**
2774 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2775 * @phba: Pointer to HBA context object.
2776 * @pring: Pointer to driver SLI ring object.
2777 * @iotag: IOCB tag.
2778 *
2779 * This function looks up the iocb_lookup table to get the command iocb
2780 * corresponding to the given iotag. This function is called with the
2781 * hbalock held.
2782 * This function returns the command iocb object if it finds the command
2783 * iocb else returns NULL.
2784 **/
2785static struct lpfc_iocbq *
2786lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2787 struct lpfc_sli_ring *pring, uint16_t iotag)
2788{
895427bd 2789 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2790
1c2ba475 2791 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2792 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2793 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2794 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2795 /* remove from txcmpl queue list */
2796 list_del_init(&cmd_iocb->list);
2797 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2798 return cmd_iocb;
2a9bf3d0 2799 }
3772a991 2800 }
89533e9b 2801
3772a991 2802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2803 "0372 iotag x%x lookup error: max iotag (x%x) "
2804 "iocb_flag x%x\n",
2805 iotag, phba->sli.last_iotag,
2806 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
2807 return NULL;
2808}
2809
e59058c4 2810/**
3621a710 2811 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2812 * @phba: Pointer to HBA context object.
2813 * @pring: Pointer to driver SLI ring object.
2814 * @saveq: Pointer to the response iocb to be processed.
2815 *
2816 * This function is called by the ring event handler for non-fcp
2817 * rings when there is a new response iocb in the response ring.
2818 * The caller is not required to hold any locks. This function
2819 * gets the command iocb associated with the response iocb and
2820 * calls the completion handler for the command iocb. If there
2821 * is no completion handler, the function will free the resources
2822 * associated with command iocb. If the response iocb is for
2823 * an already aborted command iocb, the status of the completion
2824 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2825 * This function always returns 1.
2826 **/
dea3101e 2827static int
2e0fef85 2828lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2829 struct lpfc_iocbq *saveq)
2830{
2e0fef85 2831 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2832 int rc = 1;
2833 unsigned long iflag;
2834
2835 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
2836 if (phba->sli_rev == LPFC_SLI_REV4)
2837 spin_lock_irqsave(&pring->ring_lock, iflag);
2838 else
2839 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2840 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
2841 if (phba->sli_rev == LPFC_SLI_REV4)
2842 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2843 else
2844 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 2845
dea3101e
JB
2846 if (cmdiocbp) {
2847 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2848 /*
2849 * If an ELS command failed send an event to mgmt
2850 * application.
2851 */
2852 if (saveq->iocb.ulpStatus &&
2853 (pring->ringno == LPFC_ELS_RING) &&
2854 (cmdiocbp->iocb.ulpCommand ==
2855 CMD_ELS_REQUEST64_CR))
2856 lpfc_send_els_failure_event(phba,
2857 cmdiocbp, saveq);
2858
dea3101e
JB
2859 /*
2860 * Post all ELS completions to the worker thread.
2861 * All other are passed to the completion callback.
2862 */
2863 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2864 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2865 (cmdiocbp->iocb_flag &
2866 LPFC_DRIVER_ABORTED)) {
2867 spin_lock_irqsave(&phba->hbalock,
2868 iflag);
07951076
JS
2869 cmdiocbp->iocb_flag &=
2870 ~LPFC_DRIVER_ABORTED;
341af102
JS
2871 spin_unlock_irqrestore(&phba->hbalock,
2872 iflag);
07951076
JS
2873 saveq->iocb.ulpStatus =
2874 IOSTAT_LOCAL_REJECT;
2875 saveq->iocb.un.ulpWord[4] =
2876 IOERR_SLI_ABORTED;
0ff10d46
JS
2877
2878 /* Firmware could still be in progress
2879 * of DMAing payload, so don't free data
2880 * buffer till after a hbeat.
2881 */
341af102
JS
2882 spin_lock_irqsave(&phba->hbalock,
2883 iflag);
0ff10d46 2884 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2885 spin_unlock_irqrestore(&phba->hbalock,
2886 iflag);
2887 }
0f65ff68
JS
2888 if (phba->sli_rev == LPFC_SLI_REV4) {
2889 if (saveq->iocb_flag &
2890 LPFC_EXCHANGE_BUSY) {
2891 /* Set cmdiocb flag for the
2892 * exchange busy so sgl (xri)
2893 * will not be released until
2894 * the abort xri is received
2895 * from hba.
2896 */
2897 spin_lock_irqsave(
2898 &phba->hbalock, iflag);
2899 cmdiocbp->iocb_flag |=
2900 LPFC_EXCHANGE_BUSY;
2901 spin_unlock_irqrestore(
2902 &phba->hbalock, iflag);
2903 }
2904 if (cmdiocbp->iocb_flag &
2905 LPFC_DRIVER_ABORTED) {
2906 /*
2907 * Clear LPFC_DRIVER_ABORTED
2908 * bit in case it was driver
2909 * initiated abort.
2910 */
2911 spin_lock_irqsave(
2912 &phba->hbalock, iflag);
2913 cmdiocbp->iocb_flag &=
2914 ~LPFC_DRIVER_ABORTED;
2915 spin_unlock_irqrestore(
2916 &phba->hbalock, iflag);
2917 cmdiocbp->iocb.ulpStatus =
2918 IOSTAT_LOCAL_REJECT;
2919 cmdiocbp->iocb.un.ulpWord[4] =
2920 IOERR_ABORT_REQUESTED;
2921 /*
2922 * For SLI4, irsiocb contains
2923 * NO_XRI in sli_xritag, it
2924 * shall not affect releasing
2925 * sgl (xri) process.
2926 */
2927 saveq->iocb.ulpStatus =
2928 IOSTAT_LOCAL_REJECT;
2929 saveq->iocb.un.ulpWord[4] =
2930 IOERR_SLI_ABORTED;
2931 spin_lock_irqsave(
2932 &phba->hbalock, iflag);
2933 saveq->iocb_flag |=
2934 LPFC_DELAY_MEM_FREE;
2935 spin_unlock_irqrestore(
2936 &phba->hbalock, iflag);
2937 }
07951076 2938 }
dea3101e 2939 }
2e0fef85 2940 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2941 } else
2942 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2943 } else {
2944 /*
2945 * Unknown initiating command based on the response iotag.
2946 * This could be the case on the ELS ring because of
2947 * lpfc_els_abort().
2948 */
2949 if (pring->ringno != LPFC_ELS_RING) {
2950 /*
2951 * Ring <ringno> handler: unexpected completion IoTag
2952 * <IoTag>
2953 */
a257bf90 2954 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2955 "0322 Ring %d handler: "
2956 "unexpected completion IoTag x%x "
2957 "Data: x%x x%x x%x x%x\n",
2958 pring->ringno,
2959 saveq->iocb.ulpIoTag,
2960 saveq->iocb.ulpStatus,
2961 saveq->iocb.un.ulpWord[4],
2962 saveq->iocb.ulpCommand,
2963 saveq->iocb.ulpContext);
dea3101e
JB
2964 }
2965 }
68876920 2966
dea3101e
JB
2967 return rc;
2968}
2969
e59058c4 2970/**
3621a710 2971 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2972 * @phba: Pointer to HBA context object.
2973 * @pring: Pointer to driver SLI ring object.
2974 *
2975 * This function is called from the iocb ring event handlers when
2976 * put pointer is ahead of the get pointer for a ring. This function signal
2977 * an error attention condition to the worker thread and the worker
2978 * thread will transition the HBA to offline state.
2979 **/
2e0fef85
JS
2980static void
2981lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2982{
34b02dcd 2983 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2984 /*
025dfdaf 2985 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2986 * rsp ring <portRspMax>
2987 */
2988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2989 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2990 "is bigger than rsp ring %d\n",
e8b62011 2991 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2992 pring->sli.sli3.numRiocb);
875fbdfe 2993
2e0fef85 2994 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2995
2996 /*
2997 * All error attention handlers are posted to
2998 * worker thread
2999 */
3000 phba->work_ha |= HA_ERATT;
3001 phba->work_hs = HS_FFER3;
92d7f7b0 3002
5e9d9b82 3003 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3004
3005 return;
3006}
3007
9399627f 3008/**
3621a710 3009 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3010 * @ptr: Pointer to address of HBA context object.
3011 *
3012 * This function is invoked by the Error Attention polling timer when the
3013 * timer times out. It will check the SLI Error Attention register for
3014 * possible attention events. If so, it will post an Error Attention event
3015 * and wake up worker thread to process it. Otherwise, it will set up the
3016 * Error Attention polling timer for the next poll.
3017 **/
f22eb4d3 3018void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3019{
3020 struct lpfc_hba *phba;
eb016566 3021 uint32_t eratt = 0;
aa6fbb75 3022 uint64_t sli_intr, cnt;
9399627f 3023
f22eb4d3 3024 phba = from_timer(phba, t, eratt_poll);
9399627f 3025
aa6fbb75
JS
3026 /* Here we will also keep track of interrupts per sec of the hba */
3027 sli_intr = phba->sli.slistat.sli_intr;
3028
3029 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3030 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3031 sli_intr);
3032 else
3033 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3034
65791f1f
JS
3035 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3036 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3037 phba->sli.slistat.sli_ips = cnt;
3038
3039 phba->sli.slistat.sli_prev_intr = sli_intr;
3040
9399627f
JS
3041 /* Check chip HA register for error event */
3042 eratt = lpfc_sli_check_eratt(phba);
3043
3044 if (eratt)
3045 /* Tell the worker thread there is work to do */
3046 lpfc_worker_wake_up(phba);
3047 else
3048 /* Restart the timer for next eratt poll */
256ec0d0
JS
3049 mod_timer(&phba->eratt_poll,
3050 jiffies +
65791f1f 3051 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3052 return;
3053}
3054
875fbdfe 3055
e59058c4 3056/**
3621a710 3057 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3058 * @phba: Pointer to HBA context object.
3059 * @pring: Pointer to driver SLI ring object.
3060 * @mask: Host attention register mask for this ring.
3061 *
3062 * This function is called from the interrupt context when there is a ring
3063 * event for the fcp ring. The caller does not hold any lock.
3064 * The function processes each response iocb in the response ring until it
25985edc 3065 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3066 * LE bit set. The function will call the completion handler of the command iocb
3067 * if the response iocb indicates a completion for a command iocb or it is
3068 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3069 * function if this is an unsolicited iocb.
dea3101e 3070 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3071 * to check it explicitly.
3072 */
3073int
2e0fef85
JS
3074lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3075 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3076{
34b02dcd 3077 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3078 IOCB_t *irsp = NULL;
87f6eaff 3079 IOCB_t *entry = NULL;
dea3101e
JB
3080 struct lpfc_iocbq *cmdiocbq = NULL;
3081 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3082 uint32_t status;
3083 uint32_t portRspPut, portRspMax;
3084 int rc = 1;
3085 lpfc_iocb_type type;
3086 unsigned long iflag;
3087 uint32_t rsp_cmpl = 0;
dea3101e 3088
2e0fef85 3089 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3090 pring->stats.iocb_event++;
3091
dea3101e
JB
3092 /*
3093 * The next available response entry should never exceed the maximum
3094 * entries. If it does, treat it as an adapter hardware error.
3095 */
7e56aa25 3096 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3097 portRspPut = le32_to_cpu(pgp->rspPutInx);
3098 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3099 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3100 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3101 return 1;
3102 }
45ed1190
JS
3103 if (phba->fcp_ring_in_use) {
3104 spin_unlock_irqrestore(&phba->hbalock, iflag);
3105 return 1;
3106 } else
3107 phba->fcp_ring_in_use = 1;
dea3101e
JB
3108
3109 rmb();
7e56aa25 3110 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3111 /*
3112 * Fetch an entry off the ring and copy it into a local data
3113 * structure. The copy involves a byte-swap since the
3114 * network byte order and pci byte orders are different.
3115 */
ed957684 3116 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3117 phba->last_completion_time = jiffies;
875fbdfe 3118
7e56aa25
JS
3119 if (++pring->sli.sli3.rspidx >= portRspMax)
3120 pring->sli.sli3.rspidx = 0;
875fbdfe 3121
87f6eaff
JSEC
3122 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3123 (uint32_t *) &rspiocbq.iocb,
ed957684 3124 phba->iocb_rsp_size);
a4bc3379 3125 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3126 irsp = &rspiocbq.iocb;
3127
dea3101e
JB
3128 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3129 pring->stats.iocb_rsp++;
3130 rsp_cmpl++;
3131
3132 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3133 /*
3134 * If resource errors reported from HBA, reduce
3135 * queuedepths of the SCSI device.
3136 */
3137 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3138 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3139 IOERR_NO_RESOURCES)) {
92d7f7b0 3140 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3141 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3142 spin_lock_irqsave(&phba->hbalock, iflag);
3143 }
3144
dea3101e
JB
3145 /* Rsp ring <ringno> error: IOCB */
3146 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3147 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3148 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3149 pring->ringno,
92d7f7b0
JS
3150 irsp->un.ulpWord[0],
3151 irsp->un.ulpWord[1],
3152 irsp->un.ulpWord[2],
3153 irsp->un.ulpWord[3],
3154 irsp->un.ulpWord[4],
3155 irsp->un.ulpWord[5],
d7c255b2
JS
3156 *(uint32_t *)&irsp->un1,
3157 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3158 }
3159
3160 switch (type) {
3161 case LPFC_ABORT_IOCB:
3162 case LPFC_SOL_IOCB:
3163 /*
3164 * Idle exchange closed via ABTS from port. No iocb
3165 * resources need to be recovered.
3166 */
3167 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3168 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3169 "0333 IOCB cmd 0x%x"
dca9479b 3170 " processed. Skipping"
92d7f7b0 3171 " completion\n",
dca9479b 3172 irsp->ulpCommand);
dea3101e
JB
3173 break;
3174 }
3175
604a3e30
JB
3176 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3177 &rspiocbq);
0f65ff68
JS
3178 if (unlikely(!cmdiocbq))
3179 break;
3180 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3181 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3182 if (cmdiocbq->iocb_cmpl) {
3183 spin_unlock_irqrestore(&phba->hbalock, iflag);
3184 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3185 &rspiocbq);
3186 spin_lock_irqsave(&phba->hbalock, iflag);
3187 }
dea3101e 3188 break;
a4bc3379 3189 case LPFC_UNSOL_IOCB:
2e0fef85 3190 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3191 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3192 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3193 break;
dea3101e
JB
3194 default:
3195 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3196 char adaptermsg[LPFC_MAX_ADPTMSG];
3197 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3198 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3199 MAX_MSG_DATA);
898eb71c
JP
3200 dev_warn(&((phba->pcidev)->dev),
3201 "lpfc%d: %s\n",
dea3101e
JB
3202 phba->brd_no, adaptermsg);
3203 } else {
3204 /* Unknown IOCB command */
3205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3206 "0334 Unknown IOCB command "
92d7f7b0 3207 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3208 type, irsp->ulpCommand,
92d7f7b0
JS
3209 irsp->ulpStatus,
3210 irsp->ulpIoTag,
3211 irsp->ulpContext);
dea3101e
JB
3212 }
3213 break;
3214 }
3215
3216 /*
3217 * The response IOCB has been processed. Update the ring
3218 * pointer in SLIM. If the port response put pointer has not
3219 * been updated, sync the pgp->rspPutInx and fetch the new port
3220 * response put pointer.
3221 */
7e56aa25
JS
3222 writel(pring->sli.sli3.rspidx,
3223 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3224
7e56aa25 3225 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3226 portRspPut = le32_to_cpu(pgp->rspPutInx);
3227 }
3228
3229 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3230 pring->stats.iocb_rsp_full++;
3231 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3232 writel(status, phba->CAregaddr);
3233 readl(phba->CAregaddr);
3234 }
3235 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3236 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3237 pring->stats.iocb_cmd_empty++;
3238
3239 /* Force update of the local copy of cmdGetInx */
7e56aa25 3240 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3241 lpfc_sli_resume_iocb(phba, pring);
3242
3243 if ((pring->lpfc_sli_cmd_available))
3244 (pring->lpfc_sli_cmd_available) (phba, pring);
3245
3246 }
3247
45ed1190 3248 phba->fcp_ring_in_use = 0;
2e0fef85 3249 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3250 return rc;
3251}
3252
e59058c4 3253/**
3772a991
JS
3254 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3255 * @phba: Pointer to HBA context object.
3256 * @pring: Pointer to driver SLI ring object.
3257 * @rspiocbp: Pointer to driver response IOCB object.
3258 *
3259 * This function is called from the worker thread when there is a slow-path
3260 * response IOCB to process. This function chains all the response iocbs until
3261 * seeing the iocb with the LE bit set. The function will call
3262 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3263 * completion of a command iocb. The function will call the
3264 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3265 * The function frees the resources or calls the completion handler if this
3266 * iocb is an abort completion. The function returns NULL when the response
3267 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3268 * this function shall chain the iocb on to the iocb_continueq and return the
3269 * response iocb passed in.
3270 **/
3271static struct lpfc_iocbq *
3272lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3273 struct lpfc_iocbq *rspiocbp)
3274{
3275 struct lpfc_iocbq *saveq;
3276 struct lpfc_iocbq *cmdiocbp;
3277 struct lpfc_iocbq *next_iocb;
3278 IOCB_t *irsp = NULL;
3279 uint32_t free_saveq;
3280 uint8_t iocb_cmd_type;
3281 lpfc_iocb_type type;
3282 unsigned long iflag;
3283 int rc;
3284
3285 spin_lock_irqsave(&phba->hbalock, iflag);
3286 /* First add the response iocb to the countinueq list */
3287 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3288 pring->iocb_continueq_cnt++;
3289
70f23fd6 3290 /* Now, determine whether the list is completed for processing */
3772a991
JS
3291 irsp = &rspiocbp->iocb;
3292 if (irsp->ulpLe) {
3293 /*
3294 * By default, the driver expects to free all resources
3295 * associated with this iocb completion.
3296 */
3297 free_saveq = 1;
3298 saveq = list_get_first(&pring->iocb_continueq,
3299 struct lpfc_iocbq, list);
3300 irsp = &(saveq->iocb);
3301 list_del_init(&pring->iocb_continueq);
3302 pring->iocb_continueq_cnt = 0;
3303
3304 pring->stats.iocb_rsp++;
3305
3306 /*
3307 * If resource errors reported from HBA, reduce
3308 * queuedepths of the SCSI device.
3309 */
3310 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3311 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3312 IOERR_NO_RESOURCES)) {
3772a991
JS
3313 spin_unlock_irqrestore(&phba->hbalock, iflag);
3314 phba->lpfc_rampdown_queue_depth(phba);
3315 spin_lock_irqsave(&phba->hbalock, iflag);
3316 }
3317
3318 if (irsp->ulpStatus) {
3319 /* Rsp ring <ringno> error: IOCB */
3320 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3321 "0328 Rsp Ring %d error: "
3322 "IOCB Data: "
3323 "x%x x%x x%x x%x "
3324 "x%x x%x x%x x%x "
3325 "x%x x%x x%x x%x "
3326 "x%x x%x x%x x%x\n",
3327 pring->ringno,
3328 irsp->un.ulpWord[0],
3329 irsp->un.ulpWord[1],
3330 irsp->un.ulpWord[2],
3331 irsp->un.ulpWord[3],
3332 irsp->un.ulpWord[4],
3333 irsp->un.ulpWord[5],
3334 *(((uint32_t *) irsp) + 6),
3335 *(((uint32_t *) irsp) + 7),
3336 *(((uint32_t *) irsp) + 8),
3337 *(((uint32_t *) irsp) + 9),
3338 *(((uint32_t *) irsp) + 10),
3339 *(((uint32_t *) irsp) + 11),
3340 *(((uint32_t *) irsp) + 12),
3341 *(((uint32_t *) irsp) + 13),
3342 *(((uint32_t *) irsp) + 14),
3343 *(((uint32_t *) irsp) + 15));
3344 }
3345
3346 /*
3347 * Fetch the IOCB command type and call the correct completion
3348 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3349 * get freed back to the lpfc_iocb_list by the discovery
3350 * kernel thread.
3351 */
3352 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3353 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3354 switch (type) {
3355 case LPFC_SOL_IOCB:
3356 spin_unlock_irqrestore(&phba->hbalock, iflag);
3357 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3358 spin_lock_irqsave(&phba->hbalock, iflag);
3359 break;
3360
3361 case LPFC_UNSOL_IOCB:
3362 spin_unlock_irqrestore(&phba->hbalock, iflag);
3363 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3364 spin_lock_irqsave(&phba->hbalock, iflag);
3365 if (!rc)
3366 free_saveq = 0;
3367 break;
3368
3369 case LPFC_ABORT_IOCB:
3370 cmdiocbp = NULL;
3371 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3372 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3373 saveq);
3374 if (cmdiocbp) {
3375 /* Call the specified completion routine */
3376 if (cmdiocbp->iocb_cmpl) {
3377 spin_unlock_irqrestore(&phba->hbalock,
3378 iflag);
3379 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3380 saveq);
3381 spin_lock_irqsave(&phba->hbalock,
3382 iflag);
3383 } else
3384 __lpfc_sli_release_iocbq(phba,
3385 cmdiocbp);
3386 }
3387 break;
3388
3389 case LPFC_UNKNOWN_IOCB:
3390 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3391 char adaptermsg[LPFC_MAX_ADPTMSG];
3392 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3393 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3394 MAX_MSG_DATA);
3395 dev_warn(&((phba->pcidev)->dev),
3396 "lpfc%d: %s\n",
3397 phba->brd_no, adaptermsg);
3398 } else {
3399 /* Unknown IOCB command */
3400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3401 "0335 Unknown IOCB "
3402 "command Data: x%x "
3403 "x%x x%x x%x\n",
3404 irsp->ulpCommand,
3405 irsp->ulpStatus,
3406 irsp->ulpIoTag,
3407 irsp->ulpContext);
3408 }
3409 break;
3410 }
3411
3412 if (free_saveq) {
3413 list_for_each_entry_safe(rspiocbp, next_iocb,
3414 &saveq->list, list) {
61f35bff 3415 list_del_init(&rspiocbp->list);
3772a991
JS
3416 __lpfc_sli_release_iocbq(phba, rspiocbp);
3417 }
3418 __lpfc_sli_release_iocbq(phba, saveq);
3419 }
3420 rspiocbp = NULL;
3421 }
3422 spin_unlock_irqrestore(&phba->hbalock, iflag);
3423 return rspiocbp;
3424}
3425
3426/**
3427 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3428 * @phba: Pointer to HBA context object.
3429 * @pring: Pointer to driver SLI ring object.
3430 * @mask: Host attention register mask for this ring.
3431 *
3772a991
JS
3432 * This routine wraps the actual slow_ring event process routine from the
3433 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3434 **/
3772a991 3435void
2e0fef85
JS
3436lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3437 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3438{
3439 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3440}
3441
3442/**
3443 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3444 * @phba: Pointer to HBA context object.
3445 * @pring: Pointer to driver SLI ring object.
3446 * @mask: Host attention register mask for this ring.
3447 *
3448 * This function is called from the worker thread when there is a ring event
3449 * for non-fcp rings. The caller does not hold any lock. The function will
3450 * remove each response iocb in the response ring and calls the handle
3451 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3452 **/
3453static void
3454lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3455 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3456{
34b02dcd 3457 struct lpfc_pgp *pgp;
dea3101e
JB
3458 IOCB_t *entry;
3459 IOCB_t *irsp = NULL;
3460 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3461 uint32_t portRspPut, portRspMax;
dea3101e 3462 unsigned long iflag;
3772a991 3463 uint32_t status;
dea3101e 3464
34b02dcd 3465 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3466 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3467 pring->stats.iocb_event++;
3468
dea3101e
JB
3469 /*
3470 * The next available response entry should never exceed the maximum
3471 * entries. If it does, treat it as an adapter hardware error.
3472 */
7e56aa25 3473 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3474 portRspPut = le32_to_cpu(pgp->rspPutInx);
3475 if (portRspPut >= portRspMax) {
3476 /*
025dfdaf 3477 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3478 * rsp ring <portRspMax>
3479 */
ed957684 3480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3481 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3482 "is bigger than rsp ring %d\n",
e8b62011 3483 pring->ringno, portRspPut, portRspMax);
dea3101e 3484
2e0fef85
JS
3485 phba->link_state = LPFC_HBA_ERROR;
3486 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3487
3488 phba->work_hs = HS_FFER3;
3489 lpfc_handle_eratt(phba);
3490
3772a991 3491 return;
dea3101e
JB
3492 }
3493
3494 rmb();
7e56aa25 3495 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3496 /*
3497 * Build a completion list and call the appropriate handler.
3498 * The process is to get the next available response iocb, get
3499 * a free iocb from the list, copy the response data into the
3500 * free iocb, insert to the continuation list, and update the
3501 * next response index to slim. This process makes response
3502 * iocb's in the ring available to DMA as fast as possible but
3503 * pays a penalty for a copy operation. Since the iocb is
3504 * only 32 bytes, this penalty is considered small relative to
3505 * the PCI reads for register values and a slim write. When
3506 * the ulpLe field is set, the entire Command has been
3507 * received.
3508 */
ed957684
JS
3509 entry = lpfc_resp_iocb(phba, pring);
3510
858c9f6c 3511 phba->last_completion_time = jiffies;
2e0fef85 3512 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3513 if (rspiocbp == NULL) {
3514 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3515 "completion.\n", __func__);
dea3101e
JB
3516 break;
3517 }
3518
ed957684
JS
3519 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3520 phba->iocb_rsp_size);
dea3101e
JB
3521 irsp = &rspiocbp->iocb;
3522
7e56aa25
JS
3523 if (++pring->sli.sli3.rspidx >= portRspMax)
3524 pring->sli.sli3.rspidx = 0;
dea3101e 3525
a58cbd52
JS
3526 if (pring->ringno == LPFC_ELS_RING) {
3527 lpfc_debugfs_slow_ring_trc(phba,
3528 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3529 *(((uint32_t *) irsp) + 4),
3530 *(((uint32_t *) irsp) + 6),
3531 *(((uint32_t *) irsp) + 7));
3532 }
3533
7e56aa25
JS
3534 writel(pring->sli.sli3.rspidx,
3535 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3536
3772a991
JS
3537 spin_unlock_irqrestore(&phba->hbalock, iflag);
3538 /* Handle the response IOCB */
3539 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3540 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3541
3542 /*
3543 * If the port response put pointer has not been updated, sync
3544 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3545 * response put pointer.
3546 */
7e56aa25 3547 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3548 portRspPut = le32_to_cpu(pgp->rspPutInx);
3549 }
7e56aa25 3550 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3551
92d7f7b0 3552 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3553 /* At least one response entry has been freed */
3554 pring->stats.iocb_rsp_full++;
3555 /* SET RxRE_RSP in Chip Att register */
3556 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3557 writel(status, phba->CAregaddr);
3558 readl(phba->CAregaddr); /* flush */
3559 }
3560 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3561 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3562 pring->stats.iocb_cmd_empty++;
3563
3564 /* Force update of the local copy of cmdGetInx */
7e56aa25 3565 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3566 lpfc_sli_resume_iocb(phba, pring);
3567
3568 if ((pring->lpfc_sli_cmd_available))
3569 (pring->lpfc_sli_cmd_available) (phba, pring);
3570
3571 }
3572
2e0fef85 3573 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3574 return;
dea3101e
JB
3575}
3576
4f774513
JS
3577/**
3578 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3579 * @phba: Pointer to HBA context object.
3580 * @pring: Pointer to driver SLI ring object.
3581 * @mask: Host attention register mask for this ring.
3582 *
3583 * This function is called from the worker thread when there is a pending
3584 * ELS response iocb on the driver internal slow-path response iocb worker
3585 * queue. The caller does not hold any lock. The function will remove each
3586 * response iocb from the response worker queue and calls the handle
3587 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3588 **/
3589static void
3590lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3591 struct lpfc_sli_ring *pring, uint32_t mask)
3592{
3593 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3594 struct hbq_dmabuf *dmabuf;
3595 struct lpfc_cq_event *cq_event;
4f774513
JS
3596 unsigned long iflag;
3597
45ed1190
JS
3598 spin_lock_irqsave(&phba->hbalock, iflag);
3599 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3600 spin_unlock_irqrestore(&phba->hbalock, iflag);
3601 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3602 /* Get the response iocb from the head of work queue */
3603 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3604 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3605 cq_event, struct lpfc_cq_event, list);
4f774513 3606 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3607
3608 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3609 case CQE_CODE_COMPL_WQE:
3610 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3611 cq_event);
45ed1190
JS
3612 /* Translate ELS WCQE to response IOCBQ */
3613 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3614 irspiocbq);
3615 if (irspiocbq)
3616 lpfc_sli_sp_handle_rspiocb(phba, pring,
3617 irspiocbq);
4d9ab994
JS
3618 break;
3619 case CQE_CODE_RECEIVE:
7851fe2c 3620 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3621 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3622 cq_event);
3623 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3624 break;
3625 default:
3626 break;
3627 }
4f774513
JS
3628 }
3629}
3630
e59058c4 3631/**
3621a710 3632 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3633 * @phba: Pointer to HBA context object.
3634 * @pring: Pointer to driver SLI ring object.
3635 *
3636 * This function aborts all iocbs in the given ring and frees all the iocb
3637 * objects in txq. This function issues an abort iocb for all the iocb commands
3638 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3639 * the return of this function. The caller is not required to hold any locks.
3640 **/
2e0fef85 3641void
dea3101e
JB
3642lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3643{
2534ba75 3644 LIST_HEAD(completions);
dea3101e 3645 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3646
92d7f7b0
JS
3647 if (pring->ringno == LPFC_ELS_RING) {
3648 lpfc_fabric_abort_hba(phba);
3649 }
3650
dea3101e
JB
3651 /* Error everything on txq and txcmplq
3652 * First do the txq.
3653 */
db55fba8
JS
3654 if (phba->sli_rev >= LPFC_SLI_REV4) {
3655 spin_lock_irq(&pring->ring_lock);
3656 list_splice_init(&pring->txq, &completions);
3657 pring->txq_cnt = 0;
3658 spin_unlock_irq(&pring->ring_lock);
dea3101e 3659
db55fba8
JS
3660 spin_lock_irq(&phba->hbalock);
3661 /* Next issue ABTS for everything on the txcmplq */
3662 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3663 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3664 spin_unlock_irq(&phba->hbalock);
3665 } else {
3666 spin_lock_irq(&phba->hbalock);
3667 list_splice_init(&pring->txq, &completions);
3668 pring->txq_cnt = 0;
dea3101e 3669
db55fba8
JS
3670 /* Next issue ABTS for everything on the txcmplq */
3671 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3672 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3673 spin_unlock_irq(&phba->hbalock);
3674 }
dea3101e 3675
a257bf90
JS
3676 /* Cancel all the IOCBs from the completions list */
3677 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3678 IOERR_SLI_ABORTED);
dea3101e
JB
3679}
3680
895427bd
JS
3681/**
3682 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3683 * @phba: Pointer to HBA context object.
3684 * @pring: Pointer to driver SLI ring object.
3685 *
3686 * This function aborts all iocbs in the given ring and frees all the iocb
3687 * objects in txq. This function issues an abort iocb for all the iocb commands
3688 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3689 * the return of this function. The caller is not required to hold any locks.
3690 **/
3691void
3692lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3693{
3694 LIST_HEAD(completions);
3695 struct lpfc_iocbq *iocb, *next_iocb;
3696
3697 if (pring->ringno == LPFC_ELS_RING)
3698 lpfc_fabric_abort_hba(phba);
3699
3700 spin_lock_irq(&phba->hbalock);
3701 /* Next issue ABTS for everything on the txcmplq */
3702 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3703 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3704 spin_unlock_irq(&phba->hbalock);
3705}
3706
3707
db55fba8
JS
3708/**
3709 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3710 * @phba: Pointer to HBA context object.
3711 * @pring: Pointer to driver SLI ring object.
3712 *
3713 * This function aborts all iocbs in FCP rings and frees all the iocb
3714 * objects in txq. This function issues an abort iocb for all the iocb commands
3715 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3716 * the return of this function. The caller is not required to hold any locks.
3717 **/
3718void
3719lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3720{
3721 struct lpfc_sli *psli = &phba->sli;
3722 struct lpfc_sli_ring *pring;
3723 uint32_t i;
3724
3725 /* Look on all the FCP Rings for the iotag */
3726 if (phba->sli_rev >= LPFC_SLI_REV4) {
3727 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3728 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3729 lpfc_sli_abort_iocb_ring(phba, pring);
3730 }
3731 } else {
895427bd 3732 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3733 lpfc_sli_abort_iocb_ring(phba, pring);
3734 }
3735}
3736
895427bd
JS
3737/**
3738 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3739 * @phba: Pointer to HBA context object.
3740 *
3741 * This function aborts all wqes in NVME rings. This function issues an
3742 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3743 * the txcmplq is not guaranteed to complete before the return of this
3744 * function. The caller is not required to hold any locks.
3745 **/
3746void
3747lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3748{
3749 struct lpfc_sli_ring *pring;
3750 uint32_t i;
3751
3752 if (phba->sli_rev < LPFC_SLI_REV4)
3753 return;
3754
3755 /* Abort all IO on each NVME ring. */
3756 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3757 pring = phba->sli4_hba.nvme_wq[i]->pring;
3758 lpfc_sli_abort_wqe_ring(phba, pring);
3759 }
3760}
3761
db55fba8 3762
a8e497d5 3763/**
3621a710 3764 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3765 * @phba: Pointer to HBA context object.
3766 *
3767 * This function flushes all iocbs in the fcp ring and frees all the iocb
3768 * objects in txq and txcmplq. This function will not issue abort iocbs
3769 * for all the iocb commands in txcmplq, they will just be returned with
3770 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3771 * slot has been permanently disabled.
3772 **/
3773void
3774lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3775{
3776 LIST_HEAD(txq);
3777 LIST_HEAD(txcmplq);
a8e497d5
JS
3778 struct lpfc_sli *psli = &phba->sli;
3779 struct lpfc_sli_ring *pring;
db55fba8 3780 uint32_t i;
a8e497d5
JS
3781
3782 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3783 /* Indicate the I/O queues are flushed */
3784 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3785 spin_unlock_irq(&phba->hbalock);
3786
db55fba8
JS
3787 /* Look on all the FCP Rings for the iotag */
3788 if (phba->sli_rev >= LPFC_SLI_REV4) {
3789 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3790 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3791
3792 spin_lock_irq(&pring->ring_lock);
3793 /* Retrieve everything on txq */
3794 list_splice_init(&pring->txq, &txq);
3795 /* Retrieve everything on the txcmplq */
3796 list_splice_init(&pring->txcmplq, &txcmplq);
3797 pring->txq_cnt = 0;
3798 pring->txcmplq_cnt = 0;
3799 spin_unlock_irq(&pring->ring_lock);
3800
3801 /* Flush the txq */
3802 lpfc_sli_cancel_iocbs(phba, &txq,
3803 IOSTAT_LOCAL_REJECT,
3804 IOERR_SLI_DOWN);
3805 /* Flush the txcmpq */
3806 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3807 IOSTAT_LOCAL_REJECT,
3808 IOERR_SLI_DOWN);
3809 }
3810 } else {
895427bd 3811 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 3812
db55fba8
JS
3813 spin_lock_irq(&phba->hbalock);
3814 /* Retrieve everything on txq */
3815 list_splice_init(&pring->txq, &txq);
3816 /* Retrieve everything on the txcmplq */
3817 list_splice_init(&pring->txcmplq, &txcmplq);
3818 pring->txq_cnt = 0;
3819 pring->txcmplq_cnt = 0;
3820 spin_unlock_irq(&phba->hbalock);
3821
3822 /* Flush the txq */
3823 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3824 IOERR_SLI_DOWN);
3825 /* Flush the txcmpq */
3826 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3827 IOERR_SLI_DOWN);
3828 }
a8e497d5
JS
3829}
3830
895427bd
JS
3831/**
3832 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3833 * @phba: Pointer to HBA context object.
3834 *
3835 * This function flushes all wqes in the nvme rings and frees all resources
3836 * in the txcmplq. This function does not issue abort wqes for the IO
3837 * commands in txcmplq, they will just be returned with
3838 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3839 * slot has been permanently disabled.
3840 **/
3841void
3842lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3843{
3844 LIST_HEAD(txcmplq);
3845 struct lpfc_sli_ring *pring;
3846 uint32_t i;
3847
3848 if (phba->sli_rev < LPFC_SLI_REV4)
3849 return;
3850
3851 /* Hint to other driver operations that a flush is in progress. */
3852 spin_lock_irq(&phba->hbalock);
3853 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3854 spin_unlock_irq(&phba->hbalock);
3855
3856 /* Cycle through all NVME rings and complete each IO with
3857 * a local driver reason code. This is a flush so no
3858 * abort exchange to FW.
3859 */
3860 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3861 pring = phba->sli4_hba.nvme_wq[i]->pring;
3862
3863 /* Retrieve everything on the txcmplq */
3864 spin_lock_irq(&pring->ring_lock);
3865 list_splice_init(&pring->txcmplq, &txcmplq);
3866 pring->txcmplq_cnt = 0;
3867 spin_unlock_irq(&pring->ring_lock);
3868
3869 /* Flush the txcmpq &&&PAE */
3870 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3871 IOSTAT_LOCAL_REJECT,
3872 IOERR_SLI_DOWN);
3873 }
3874}
3875
e59058c4 3876/**
3772a991 3877 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3878 * @phba: Pointer to HBA context object.
3879 * @mask: Bit mask to be checked.
3880 *
3881 * This function reads the host status register and compares
3882 * with the provided bit mask to check if HBA completed
3883 * the restart. This function will wait in a loop for the
3884 * HBA to complete restart. If the HBA does not restart within
3885 * 15 iterations, the function will reset the HBA again. The
3886 * function returns 1 when HBA fail to restart otherwise returns
3887 * zero.
3888 **/
3772a991
JS
3889static int
3890lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3891{
41415862
JW
3892 uint32_t status;
3893 int i = 0;
3894 int retval = 0;
dea3101e 3895
41415862 3896 /* Read the HBA Host Status Register */
9940b97b
JS
3897 if (lpfc_readl(phba->HSregaddr, &status))
3898 return 1;
dea3101e 3899
41415862
JW
3900 /*
3901 * Check status register every 100ms for 5 retries, then every
3902 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3903 * every 2.5 sec for 4.
3904 * Break our of the loop if errors occurred during init.
3905 */
3906 while (((status & mask) != mask) &&
3907 !(status & HS_FFERM) &&
3908 i++ < 20) {
dea3101e 3909
41415862
JW
3910 if (i <= 5)
3911 msleep(10);
3912 else if (i <= 10)
3913 msleep(500);
3914 else
3915 msleep(2500);
dea3101e 3916
41415862 3917 if (i == 15) {
2e0fef85 3918 /* Do post */
92d7f7b0 3919 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3920 lpfc_sli_brdrestart(phba);
3921 }
3922 /* Read the HBA Host Status Register */
9940b97b
JS
3923 if (lpfc_readl(phba->HSregaddr, &status)) {
3924 retval = 1;
3925 break;
3926 }
41415862 3927 }
dea3101e 3928
41415862
JW
3929 /* Check to see if any errors occurred during init */
3930 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3932 "2751 Adapter failed to restart, "
3933 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3934 status,
3935 readl(phba->MBslimaddr + 0xa8),
3936 readl(phba->MBslimaddr + 0xac));
2e0fef85 3937 phba->link_state = LPFC_HBA_ERROR;
41415862 3938 retval = 1;
dea3101e 3939 }
dea3101e 3940
41415862
JW
3941 return retval;
3942}
dea3101e 3943
da0436e9
JS
3944/**
3945 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3946 * @phba: Pointer to HBA context object.
3947 * @mask: Bit mask to be checked.
3948 *
3949 * This function checks the host status register to check if HBA is
3950 * ready. This function will wait in a loop for the HBA to be ready
3951 * If the HBA is not ready , the function will will reset the HBA PCI
3952 * function again. The function returns 1 when HBA fail to be ready
3953 * otherwise returns zero.
3954 **/
3955static int
3956lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3957{
3958 uint32_t status;
3959 int retval = 0;
3960
3961 /* Read the HBA Host Status Register */
3962 status = lpfc_sli4_post_status_check(phba);
3963
3964 if (status) {
3965 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3966 lpfc_sli_brdrestart(phba);
3967 status = lpfc_sli4_post_status_check(phba);
3968 }
3969
3970 /* Check to see if any errors occurred during init */
3971 if (status) {
3972 phba->link_state = LPFC_HBA_ERROR;
3973 retval = 1;
3974 } else
3975 phba->sli4_hba.intr_enable = 0;
3976
3977 return retval;
3978}
3979
3980/**
3981 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3982 * @phba: Pointer to HBA context object.
3983 * @mask: Bit mask to be checked.
3984 *
3985 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3986 * from the API jump table function pointer from the lpfc_hba struct.
3987 **/
3988int
3989lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3990{
3991 return phba->lpfc_sli_brdready(phba, mask);
3992}
3993
9290831f
JS
3994#define BARRIER_TEST_PATTERN (0xdeadbeef)
3995
e59058c4 3996/**
3621a710 3997 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3998 * @phba: Pointer to HBA context object.
3999 *
1b51197d
JS
4000 * This function is called before resetting an HBA. This function is called
4001 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 4002 **/
2e0fef85 4003void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4004{
65a29c16
JS
4005 uint32_t __iomem *resp_buf;
4006 uint32_t __iomem *mbox_buf;
9290831f 4007 volatile uint32_t mbox;
9940b97b 4008 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4009 int i;
4010 uint8_t hdrtype;
4011
1c2ba475
JT
4012 lockdep_assert_held(&phba->hbalock);
4013
9290831f
JS
4014 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4015 if (hdrtype != 0x80 ||
4016 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4017 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4018 return;
4019
4020 /*
4021 * Tell the other part of the chip to suspend temporarily all
4022 * its DMA activity.
4023 */
65a29c16 4024 resp_buf = phba->MBslimaddr;
9290831f
JS
4025
4026 /* Disable the error attention */
9940b97b
JS
4027 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4028 return;
9290831f
JS
4029 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4030 readl(phba->HCregaddr); /* flush */
2e0fef85 4031 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4032
9940b97b
JS
4033 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4034 return;
4035 if (ha_copy & HA_ERATT) {
9290831f
JS
4036 /* Clear Chip error bit */
4037 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4038 phba->pport->stopped = 1;
9290831f
JS
4039 }
4040
4041 mbox = 0;
4042 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4043 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4044
4045 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4046 mbox_buf = phba->MBslimaddr;
9290831f
JS
4047 writel(mbox, mbox_buf);
4048
9940b97b
JS
4049 for (i = 0; i < 50; i++) {
4050 if (lpfc_readl((resp_buf + 1), &resp_data))
4051 return;
4052 if (resp_data != ~(BARRIER_TEST_PATTERN))
4053 mdelay(1);
4054 else
4055 break;
4056 }
4057 resp_data = 0;
4058 if (lpfc_readl((resp_buf + 1), &resp_data))
4059 return;
4060 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4061 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4062 phba->pport->stopped)
9290831f
JS
4063 goto restore_hc;
4064 else
4065 goto clear_errat;
4066 }
4067
4068 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4069 resp_data = 0;
4070 for (i = 0; i < 500; i++) {
4071 if (lpfc_readl(resp_buf, &resp_data))
4072 return;
4073 if (resp_data != mbox)
4074 mdelay(1);
4075 else
4076 break;
4077 }
9290831f
JS
4078
4079clear_errat:
4080
9940b97b
JS
4081 while (++i < 500) {
4082 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4083 return;
4084 if (!(ha_copy & HA_ERATT))
4085 mdelay(1);
4086 else
4087 break;
4088 }
9290831f
JS
4089
4090 if (readl(phba->HAregaddr) & HA_ERATT) {
4091 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4092 phba->pport->stopped = 1;
9290831f
JS
4093 }
4094
4095restore_hc:
2e0fef85 4096 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4097 writel(hc_copy, phba->HCregaddr);
4098 readl(phba->HCregaddr); /* flush */
4099}
4100
e59058c4 4101/**
3621a710 4102 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4103 * @phba: Pointer to HBA context object.
4104 *
4105 * This function issues a kill_board mailbox command and waits for
4106 * the error attention interrupt. This function is called for stopping
4107 * the firmware processing. The caller is not required to hold any
4108 * locks. This function calls lpfc_hba_down_post function to free
4109 * any pending commands after the kill. The function will return 1 when it
4110 * fails to kill the board else will return 0.
4111 **/
41415862 4112int
2e0fef85 4113lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4114{
4115 struct lpfc_sli *psli;
4116 LPFC_MBOXQ_t *pmb;
4117 uint32_t status;
4118 uint32_t ha_copy;
4119 int retval;
4120 int i = 0;
dea3101e 4121
41415862 4122 psli = &phba->sli;
dea3101e 4123
41415862 4124 /* Kill HBA */
ed957684 4125 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4126 "0329 Kill HBA Data: x%x x%x\n",
4127 phba->pport->port_state, psli->sli_flag);
41415862 4128
98c9ea5c
JS
4129 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4130 if (!pmb)
41415862 4131 return 1;
41415862
JW
4132
4133 /* Disable the error attention */
2e0fef85 4134 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4135 if (lpfc_readl(phba->HCregaddr, &status)) {
4136 spin_unlock_irq(&phba->hbalock);
4137 mempool_free(pmb, phba->mbox_mem_pool);
4138 return 1;
4139 }
41415862
JW
4140 status &= ~HC_ERINT_ENA;
4141 writel(status, phba->HCregaddr);
4142 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4143 phba->link_flag |= LS_IGNORE_ERATT;
4144 spin_unlock_irq(&phba->hbalock);
41415862
JW
4145
4146 lpfc_kill_board(phba, pmb);
4147 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4148 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4149
4150 if (retval != MBX_SUCCESS) {
4151 if (retval != MBX_BUSY)
4152 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4154 "2752 KILL_BOARD command failed retval %d\n",
4155 retval);
2e0fef85
JS
4156 spin_lock_irq(&phba->hbalock);
4157 phba->link_flag &= ~LS_IGNORE_ERATT;
4158 spin_unlock_irq(&phba->hbalock);
41415862
JW
4159 return 1;
4160 }
4161
f4b4c68f
JS
4162 spin_lock_irq(&phba->hbalock);
4163 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4164 spin_unlock_irq(&phba->hbalock);
9290831f 4165
41415862
JW
4166 mempool_free(pmb, phba->mbox_mem_pool);
4167
4168 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4169 * attention every 100ms for 3 seconds. If we don't get ERATT after
4170 * 3 seconds we still set HBA_ERROR state because the status of the
4171 * board is now undefined.
4172 */
9940b97b
JS
4173 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4174 return 1;
41415862
JW
4175 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4176 mdelay(100);
9940b97b
JS
4177 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4178 return 1;
41415862
JW
4179 }
4180
4181 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4182 if (ha_copy & HA_ERATT) {
4183 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4184 phba->pport->stopped = 1;
9290831f 4185 }
2e0fef85 4186 spin_lock_irq(&phba->hbalock);
41415862 4187 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4188 psli->mbox_active = NULL;
2e0fef85
JS
4189 phba->link_flag &= ~LS_IGNORE_ERATT;
4190 spin_unlock_irq(&phba->hbalock);
41415862 4191
41415862 4192 lpfc_hba_down_post(phba);
2e0fef85 4193 phba->link_state = LPFC_HBA_ERROR;
41415862 4194
2e0fef85 4195 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4196}
4197
e59058c4 4198/**
3772a991 4199 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4200 * @phba: Pointer to HBA context object.
4201 *
4202 * This function resets the HBA by writing HC_INITFF to the control
4203 * register. After the HBA resets, this function resets all the iocb ring
4204 * indices. This function disables PCI layer parity checking during
4205 * the reset.
4206 * This function returns 0 always.
4207 * The caller is not required to hold any locks.
4208 **/
41415862 4209int
2e0fef85 4210lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4211{
41415862 4212 struct lpfc_sli *psli;
dea3101e 4213 struct lpfc_sli_ring *pring;
41415862 4214 uint16_t cfg_value;
dea3101e 4215 int i;
dea3101e 4216
41415862 4217 psli = &phba->sli;
dea3101e 4218
41415862
JW
4219 /* Reset HBA */
4220 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4221 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4222 (phba->pport) ? phba->pport->port_state : 0,
4223 psli->sli_flag);
dea3101e
JB
4224
4225 /* perform board reset */
4226 phba->fc_eventTag = 0;
4d9ab994 4227 phba->link_events = 0;
4492b739
JS
4228 if (phba->pport) {
4229 phba->pport->fc_myDID = 0;
4230 phba->pport->fc_prevDID = 0;
4231 }
dea3101e 4232
41415862
JW
4233 /* Turn off parity checking and serr during the physical reset */
4234 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4235 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4236 (cfg_value &
4237 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4238
3772a991
JS
4239 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4240
41415862
JW
4241 /* Now toggle INITFF bit in the Host Control Register */
4242 writel(HC_INITFF, phba->HCregaddr);
4243 mdelay(1);
4244 readl(phba->HCregaddr); /* flush */
4245 writel(0, phba->HCregaddr);
4246 readl(phba->HCregaddr); /* flush */
4247
4248 /* Restore PCI cmd register */
4249 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4250
4251 /* Initialize relevant SLI info */
41415862 4252 for (i = 0; i < psli->num_rings; i++) {
895427bd 4253 pring = &psli->sli3_ring[i];
dea3101e 4254 pring->flag = 0;
7e56aa25
JS
4255 pring->sli.sli3.rspidx = 0;
4256 pring->sli.sli3.next_cmdidx = 0;
4257 pring->sli.sli3.local_getidx = 0;
4258 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4259 pring->missbufcnt = 0;
4260 }
dea3101e 4261
2e0fef85 4262 phba->link_state = LPFC_WARM_START;
41415862
JW
4263 return 0;
4264}
4265
e59058c4 4266/**
da0436e9
JS
4267 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4268 * @phba: Pointer to HBA context object.
4269 *
4270 * This function resets a SLI4 HBA. This function disables PCI layer parity
4271 * checking during resets the device. The caller is not required to hold
4272 * any locks.
4273 *
4274 * This function returns 0 always.
4275 **/
4276int
4277lpfc_sli4_brdreset(struct lpfc_hba *phba)
4278{
4279 struct lpfc_sli *psli = &phba->sli;
4280 uint16_t cfg_value;
0293635e 4281 int rc = 0;
da0436e9
JS
4282
4283 /* Reset HBA */
4284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4285 "0295 Reset HBA Data: x%x x%x x%x\n",
4286 phba->pport->port_state, psli->sli_flag,
4287 phba->hba_flag);
da0436e9
JS
4288
4289 /* perform board reset */
4290 phba->fc_eventTag = 0;
4d9ab994 4291 phba->link_events = 0;
da0436e9
JS
4292 phba->pport->fc_myDID = 0;
4293 phba->pport->fc_prevDID = 0;
4294
da0436e9
JS
4295 spin_lock_irq(&phba->hbalock);
4296 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4297 phba->fcf.fcf_flag = 0;
da0436e9
JS
4298 spin_unlock_irq(&phba->hbalock);
4299
0293635e
JS
4300 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4301 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4302 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4303 return rc;
4304 }
4305
da0436e9
JS
4306 /* Now physically reset the device */
4307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4308 "0389 Performing PCI function reset!\n");
be858b65
JS
4309
4310 /* Turn off parity checking and serr during the physical reset */
4311 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4312 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4313 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4314
88318816 4315 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4316 rc = lpfc_pci_function_reset(phba);
da0436e9 4317
be858b65
JS
4318 /* Restore PCI cmd register */
4319 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4320
27b01b82 4321 return rc;
da0436e9
JS
4322}
4323
4324/**
4325 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4326 * @phba: Pointer to HBA context object.
4327 *
4328 * This function is called in the SLI initialization code path to
4329 * restart the HBA. The caller is not required to hold any lock.
4330 * This function writes MBX_RESTART mailbox command to the SLIM and
4331 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4332 * function to free any pending commands. The function enables
4333 * POST only during the first initialization. The function returns zero.
4334 * The function does not guarantee completion of MBX_RESTART mailbox
4335 * command before the return of this function.
4336 **/
da0436e9
JS
4337static int
4338lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4339{
4340 MAILBOX_t *mb;
4341 struct lpfc_sli *psli;
41415862
JW
4342 volatile uint32_t word0;
4343 void __iomem *to_slim;
0d878419 4344 uint32_t hba_aer_enabled;
41415862 4345
2e0fef85 4346 spin_lock_irq(&phba->hbalock);
41415862 4347
0d878419
JS
4348 /* Take PCIe device Advanced Error Reporting (AER) state */
4349 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4350
41415862
JW
4351 psli = &phba->sli;
4352
4353 /* Restart HBA */
4354 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4355 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4356 (phba->pport) ? phba->pport->port_state : 0,
4357 psli->sli_flag);
41415862
JW
4358
4359 word0 = 0;
4360 mb = (MAILBOX_t *) &word0;
4361 mb->mbxCommand = MBX_RESTART;
4362 mb->mbxHc = 1;
4363
9290831f
JS
4364 lpfc_reset_barrier(phba);
4365
41415862
JW
4366 to_slim = phba->MBslimaddr;
4367 writel(*(uint32_t *) mb, to_slim);
4368 readl(to_slim); /* flush */
4369
4370 /* Only skip post after fc_ffinit is completed */
4492b739 4371 if (phba->pport && phba->pport->port_state)
41415862 4372 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4373 else
41415862 4374 word0 = 0; /* This is really setting up word1 */
65a29c16 4375 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4376 writel(*(uint32_t *) mb, to_slim);
4377 readl(to_slim); /* flush */
dea3101e 4378
41415862 4379 lpfc_sli_brdreset(phba);
4492b739
JS
4380 if (phba->pport)
4381 phba->pport->stopped = 0;
2e0fef85 4382 phba->link_state = LPFC_INIT_START;
da0436e9 4383 phba->hba_flag = 0;
2e0fef85 4384 spin_unlock_irq(&phba->hbalock);
41415862 4385
64ba8818
JS
4386 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4387 psli->stats_start = get_seconds();
4388
eaf15d5b
JS
4389 /* Give the INITFF and Post time to settle. */
4390 mdelay(100);
41415862 4391
0d878419
JS
4392 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4393 if (hba_aer_enabled)
4394 pci_disable_pcie_error_reporting(phba->pcidev);
4395
41415862 4396 lpfc_hba_down_post(phba);
dea3101e
JB
4397
4398 return 0;
4399}
4400
da0436e9
JS
4401/**
4402 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4403 * @phba: Pointer to HBA context object.
4404 *
4405 * This function is called in the SLI initialization code path to restart
4406 * a SLI4 HBA. The caller is not required to hold any lock.
4407 * At the end of the function, it calls lpfc_hba_down_post function to
4408 * free any pending commands.
4409 **/
4410static int
4411lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4412{
4413 struct lpfc_sli *psli = &phba->sli;
75baf696 4414 uint32_t hba_aer_enabled;
27b01b82 4415 int rc;
da0436e9
JS
4416
4417 /* Restart HBA */
4418 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4419 "0296 Restart HBA Data: x%x x%x\n",
4420 phba->pport->port_state, psli->sli_flag);
4421
75baf696
JS
4422 /* Take PCIe device Advanced Error Reporting (AER) state */
4423 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4424
27b01b82 4425 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4426
4427 spin_lock_irq(&phba->hbalock);
4428 phba->pport->stopped = 0;
4429 phba->link_state = LPFC_INIT_START;
4430 phba->hba_flag = 0;
4431 spin_unlock_irq(&phba->hbalock);
4432
4433 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4434 psli->stats_start = get_seconds();
4435
75baf696
JS
4436 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4437 if (hba_aer_enabled)
4438 pci_disable_pcie_error_reporting(phba->pcidev);
4439
da0436e9 4440 lpfc_hba_down_post(phba);
569dbe84 4441 lpfc_sli4_queue_destroy(phba);
da0436e9 4442
27b01b82 4443 return rc;
da0436e9
JS
4444}
4445
4446/**
4447 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4448 * @phba: Pointer to HBA context object.
4449 *
4450 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4451 * API jump table function pointer from the lpfc_hba struct.
4452**/
4453int
4454lpfc_sli_brdrestart(struct lpfc_hba *phba)
4455{
4456 return phba->lpfc_sli_brdrestart(phba);
4457}
4458
e59058c4 4459/**
3621a710 4460 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4461 * @phba: Pointer to HBA context object.
4462 *
4463 * This function is called after a HBA restart to wait for successful
4464 * restart of the HBA. Successful restart of the HBA is indicated by
4465 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4466 * iteration, the function will restart the HBA again. The function returns
4467 * zero if HBA successfully restarted else returns negative error code.
4468 **/
4492b739 4469int
dea3101e
JB
4470lpfc_sli_chipset_init(struct lpfc_hba *phba)
4471{
4472 uint32_t status, i = 0;
4473
4474 /* Read the HBA Host Status Register */
9940b97b
JS
4475 if (lpfc_readl(phba->HSregaddr, &status))
4476 return -EIO;
dea3101e
JB
4477
4478 /* Check status register to see what current state is */
4479 i = 0;
4480 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4481
dcf2a4e0
JS
4482 /* Check every 10ms for 10 retries, then every 100ms for 90
4483 * retries, then every 1 sec for 50 retires for a total of
4484 * ~60 seconds before reset the board again and check every
4485 * 1 sec for 50 retries. The up to 60 seconds before the
4486 * board ready is required by the Falcon FIPS zeroization
4487 * complete, and any reset the board in between shall cause
4488 * restart of zeroization, further delay the board ready.
dea3101e 4489 */
dcf2a4e0 4490 if (i++ >= 200) {
dea3101e
JB
4491 /* Adapter failed to init, timeout, status reg
4492 <status> */
ed957684 4493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4494 "0436 Adapter failed to init, "
09372820
JS
4495 "timeout, status reg x%x, "
4496 "FW Data: A8 x%x AC x%x\n", status,
4497 readl(phba->MBslimaddr + 0xa8),
4498 readl(phba->MBslimaddr + 0xac));
2e0fef85 4499 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4500 return -ETIMEDOUT;
4501 }
4502
4503 /* Check to see if any errors occurred during init */
4504 if (status & HS_FFERM) {
4505 /* ERROR: During chipset initialization */
4506 /* Adapter failed to init, chipset, status reg
4507 <status> */
ed957684 4508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4509 "0437 Adapter failed to init, "
09372820
JS
4510 "chipset, status reg x%x, "
4511 "FW Data: A8 x%x AC x%x\n", status,
4512 readl(phba->MBslimaddr + 0xa8),
4513 readl(phba->MBslimaddr + 0xac));
2e0fef85 4514 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4515 return -EIO;
4516 }
4517
dcf2a4e0 4518 if (i <= 10)
dea3101e 4519 msleep(10);
dcf2a4e0
JS
4520 else if (i <= 100)
4521 msleep(100);
4522 else
4523 msleep(1000);
dea3101e 4524
dcf2a4e0
JS
4525 if (i == 150) {
4526 /* Do post */
92d7f7b0 4527 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4528 lpfc_sli_brdrestart(phba);
dea3101e
JB
4529 }
4530 /* Read the HBA Host Status Register */
9940b97b
JS
4531 if (lpfc_readl(phba->HSregaddr, &status))
4532 return -EIO;
dea3101e
JB
4533 }
4534
4535 /* Check to see if any errors occurred during init */
4536 if (status & HS_FFERM) {
4537 /* ERROR: During chipset initialization */
4538 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4540 "0438 Adapter failed to init, chipset, "
09372820
JS
4541 "status reg x%x, "
4542 "FW Data: A8 x%x AC x%x\n", status,
4543 readl(phba->MBslimaddr + 0xa8),
4544 readl(phba->MBslimaddr + 0xac));
2e0fef85 4545 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4546 return -EIO;
4547 }
4548
4549 /* Clear all interrupt enable conditions */
4550 writel(0, phba->HCregaddr);
4551 readl(phba->HCregaddr); /* flush */
4552
4553 /* setup host attn register */
4554 writel(0xffffffff, phba->HAregaddr);
4555 readl(phba->HAregaddr); /* flush */
4556 return 0;
4557}
4558
e59058c4 4559/**
3621a710 4560 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4561 *
4562 * This function calculates and returns the number of HBQs required to be
4563 * configured.
4564 **/
78b2d852 4565int
ed957684
JS
4566lpfc_sli_hbq_count(void)
4567{
92d7f7b0 4568 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4569}
4570
e59058c4 4571/**
3621a710 4572 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4573 *
4574 * This function adds the number of hbq entries in every HBQ to get
4575 * the total number of hbq entries required for the HBA and returns
4576 * the total count.
4577 **/
ed957684
JS
4578static int
4579lpfc_sli_hbq_entry_count(void)
4580{
4581 int hbq_count = lpfc_sli_hbq_count();
4582 int count = 0;
4583 int i;
4584
4585 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4586 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4587 return count;
4588}
4589
e59058c4 4590/**
3621a710 4591 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4592 *
4593 * This function calculates amount of memory required for all hbq entries
4594 * to be configured and returns the total memory required.
4595 **/
dea3101e 4596int
ed957684
JS
4597lpfc_sli_hbq_size(void)
4598{
4599 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4600}
4601
e59058c4 4602/**
3621a710 4603 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4604 * @phba: Pointer to HBA context object.
4605 *
4606 * This function is called during the SLI initialization to configure
4607 * all the HBQs and post buffers to the HBQ. The caller is not
4608 * required to hold any locks. This function will return zero if successful
4609 * else it will return negative error code.
4610 **/
ed957684
JS
4611static int
4612lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4613{
4614 int hbq_count = lpfc_sli_hbq_count();
4615 LPFC_MBOXQ_t *pmb;
4616 MAILBOX_t *pmbox;
4617 uint32_t hbqno;
4618 uint32_t hbq_entry_index;
ed957684 4619
92d7f7b0
JS
4620 /* Get a Mailbox buffer to setup mailbox
4621 * commands for HBA initialization
4622 */
ed957684
JS
4623 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4624
4625 if (!pmb)
4626 return -ENOMEM;
4627
04c68496 4628 pmbox = &pmb->u.mb;
ed957684
JS
4629
4630 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4631 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4632 phba->hbq_in_use = 1;
ed957684
JS
4633
4634 hbq_entry_index = 0;
4635 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4636 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4637 phba->hbqs[hbqno].hbqPutIdx = 0;
4638 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4639 phba->hbqs[hbqno].entry_count =
92d7f7b0 4640 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4641 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4642 hbq_entry_index, pmb);
ed957684
JS
4643 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4644
4645 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4646 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4647 mbxStatus <status>, ring <num> */
4648
4649 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4650 LOG_SLI | LOG_VPORT,
e8b62011 4651 "1805 Adapter failed to init. "
ed957684 4652 "Data: x%x x%x x%x\n",
e8b62011 4653 pmbox->mbxCommand,
ed957684
JS
4654 pmbox->mbxStatus, hbqno);
4655
4656 phba->link_state = LPFC_HBA_ERROR;
4657 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4658 return -ENXIO;
ed957684
JS
4659 }
4660 }
4661 phba->hbq_count = hbq_count;
4662
ed957684
JS
4663 mempool_free(pmb, phba->mbox_mem_pool);
4664
92d7f7b0 4665 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4666 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4667 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4668 return 0;
4669}
4670
4f774513
JS
4671/**
4672 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4673 * @phba: Pointer to HBA context object.
4674 *
4675 * This function is called during the SLI initialization to configure
4676 * all the HBQs and post buffers to the HBQ. The caller is not
4677 * required to hold any locks. This function will return zero if successful
4678 * else it will return negative error code.
4679 **/
4680static int
4681lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4682{
4683 phba->hbq_in_use = 1;
895427bd
JS
4684 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4685 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4686 phba->hbq_count = 1;
895427bd 4687 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4688 /* Initially populate or replenish the HBQs */
4f774513
JS
4689 return 0;
4690}
4691
e59058c4 4692/**
3621a710 4693 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4694 * @phba: Pointer to HBA context object.
4695 * @sli_mode: sli mode - 2/3
4696 *
183b8021 4697 * This function is called by the sli initialization code path
e59058c4
JS
4698 * to issue config_port mailbox command. This function restarts the
4699 * HBA firmware and issues a config_port mailbox command to configure
4700 * the SLI interface in the sli mode specified by sli_mode
4701 * variable. The caller is not required to hold any locks.
4702 * The function returns 0 if successful, else returns negative error
4703 * code.
4704 **/
9399627f
JS
4705int
4706lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4707{
4708 LPFC_MBOXQ_t *pmb;
4709 uint32_t resetcount = 0, rc = 0, done = 0;
4710
4711 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4712 if (!pmb) {
2e0fef85 4713 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4714 return -ENOMEM;
4715 }
4716
ed957684 4717 phba->sli_rev = sli_mode;
dea3101e 4718 while (resetcount < 2 && !done) {
2e0fef85 4719 spin_lock_irq(&phba->hbalock);
1c067a42 4720 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4721 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4722 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4723 lpfc_sli_brdrestart(phba);
dea3101e
JB
4724 rc = lpfc_sli_chipset_init(phba);
4725 if (rc)
4726 break;
4727
2e0fef85 4728 spin_lock_irq(&phba->hbalock);
1c067a42 4729 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4730 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4731 resetcount++;
4732
ed957684
JS
4733 /* Call pre CONFIG_PORT mailbox command initialization. A
4734 * value of 0 means the call was successful. Any other
4735 * nonzero value is a failure, but if ERESTART is returned,
4736 * the driver may reset the HBA and try again.
4737 */
dea3101e
JB
4738 rc = lpfc_config_port_prep(phba);
4739 if (rc == -ERESTART) {
ed957684 4740 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4741 continue;
34b02dcd 4742 } else if (rc)
dea3101e 4743 break;
6d368e53 4744
2e0fef85 4745 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4746 lpfc_config_port(phba, pmb);
4747 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4748 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4749 LPFC_SLI3_HBQ_ENABLED |
4750 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4751 LPFC_SLI3_BG_ENABLED |
4752 LPFC_SLI3_DSS_ENABLED);
ed957684 4753 if (rc != MBX_SUCCESS) {
dea3101e 4754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4755 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4756 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4757 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4758 spin_lock_irq(&phba->hbalock);
04c68496 4759 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4760 spin_unlock_irq(&phba->hbalock);
4761 rc = -ENXIO;
04c68496
JS
4762 } else {
4763 /* Allow asynchronous mailbox command to go through */
4764 spin_lock_irq(&phba->hbalock);
4765 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4766 spin_unlock_irq(&phba->hbalock);
ed957684 4767 done = 1;
cb69f7de
JS
4768
4769 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4770 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4771 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4772 "3110 Port did not grant ASABT\n");
04c68496 4773 }
dea3101e 4774 }
ed957684
JS
4775 if (!done) {
4776 rc = -EINVAL;
4777 goto do_prep_failed;
4778 }
04c68496
JS
4779 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4780 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4781 rc = -ENXIO;
4782 goto do_prep_failed;
4783 }
04c68496 4784 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4785 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4786 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4787 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4788 phba->max_vpi : phba->max_vports;
4789
34b02dcd
JS
4790 } else
4791 phba->max_vpi = 0;
bc73905a
JS
4792 phba->fips_level = 0;
4793 phba->fips_spec_rev = 0;
4794 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4795 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4796 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4797 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4798 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4799 "2850 Security Crypto Active. FIPS x%d "
4800 "(Spec Rev: x%d)",
4801 phba->fips_level, phba->fips_spec_rev);
4802 }
4803 if (pmb->u.mb.un.varCfgPort.sec_err) {
4804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4805 "2856 Config Port Security Crypto "
4806 "Error: x%x ",
4807 pmb->u.mb.un.varCfgPort.sec_err);
4808 }
04c68496 4809 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4810 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4811 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4812 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4813
4814 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4815 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4816
4817 if (phba->cfg_enable_bg) {
04c68496 4818 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4819 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4820 else
4821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4822 "0443 Adapter did not grant "
4823 "BlockGuard\n");
4824 }
34b02dcd 4825 } else {
8f34f4ce 4826 phba->hbq_get = NULL;
34b02dcd 4827 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4828 phba->max_vpi = 0;
ed957684 4829 }
92d7f7b0 4830do_prep_failed:
ed957684
JS
4831 mempool_free(pmb, phba->mbox_mem_pool);
4832 return rc;
4833}
4834
e59058c4
JS
4835
4836/**
183b8021 4837 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
4838 * @phba: Pointer to HBA context object.
4839 *
183b8021
MY
4840 * This function is the main SLI initialization function. This function
4841 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
4842 * error attention handler code. Caller is not required to hold any
4843 * locks. This function issues config_port mailbox command to configure
4844 * the SLI, setup iocb rings and HBQ rings. In the end the function
4845 * calls the config_port_post function to issue init_link mailbox
4846 * command and to start the discovery. The function will return zero
4847 * if successful, else it will return negative error code.
4848 **/
ed957684
JS
4849int
4850lpfc_sli_hba_setup(struct lpfc_hba *phba)
4851{
4852 uint32_t rc;
6d368e53
JS
4853 int mode = 3, i;
4854 int longs;
ed957684 4855
12247e81 4856 switch (phba->cfg_sli_mode) {
ed957684 4857 case 2:
78b2d852 4858 if (phba->cfg_enable_npiv) {
92d7f7b0 4859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 4860 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 4861 "parameter (%d) to auto (0).\n",
12247e81 4862 phba->cfg_sli_mode);
92d7f7b0
JS
4863 break;
4864 }
ed957684
JS
4865 mode = 2;
4866 break;
4867 case 0:
4868 case 3:
4869 break;
4870 default:
92d7f7b0 4871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
4872 "1819 Unrecognized sli_mode parameter: %d.\n",
4873 phba->cfg_sli_mode);
ed957684
JS
4874
4875 break;
4876 }
b5c53958 4877 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 4878
9399627f
JS
4879 rc = lpfc_sli_config_port(phba, mode);
4880
12247e81 4881 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 4882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4883 "1820 Unable to select SLI-3. "
4884 "Not supported by adapter.\n");
ed957684 4885 if (rc && mode != 2)
9399627f 4886 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
4887 else if (rc && mode == 2)
4888 rc = lpfc_sli_config_port(phba, 3);
ed957684 4889 if (rc)
dea3101e
JB
4890 goto lpfc_sli_hba_setup_error;
4891
0d878419
JS
4892 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4893 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4894 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4895 if (!rc) {
4896 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4897 "2709 This device supports "
4898 "Advanced Error Reporting (AER)\n");
4899 spin_lock_irq(&phba->hbalock);
4900 phba->hba_flag |= HBA_AER_ENABLED;
4901 spin_unlock_irq(&phba->hbalock);
4902 } else {
4903 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4904 "2708 This device does not support "
b069d7eb
JS
4905 "Advanced Error Reporting (AER): %d\n",
4906 rc);
0d878419
JS
4907 phba->cfg_aer_support = 0;
4908 }
4909 }
4910
ed957684
JS
4911 if (phba->sli_rev == 3) {
4912 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4913 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4914 } else {
4915 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4916 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4917 phba->sli3_options = 0;
ed957684
JS
4918 }
4919
4920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4921 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4922 phba->sli_rev, phba->max_vpi);
ed957684 4923 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4924
4925 if (rc)
4926 goto lpfc_sli_hba_setup_error;
4927
6d368e53
JS
4928 /* Initialize VPIs. */
4929 if (phba->sli_rev == LPFC_SLI_REV3) {
4930 /*
4931 * The VPI bitmask and physical ID array are allocated
4932 * and initialized once only - at driver load. A port
4933 * reset doesn't need to reinitialize this memory.
4934 */
4935 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4936 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4937 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4938 GFP_KERNEL);
4939 if (!phba->vpi_bmask) {
4940 rc = -ENOMEM;
4941 goto lpfc_sli_hba_setup_error;
4942 }
4943
4944 phba->vpi_ids = kzalloc(
4945 (phba->max_vpi+1) * sizeof(uint16_t),
4946 GFP_KERNEL);
4947 if (!phba->vpi_ids) {
4948 kfree(phba->vpi_bmask);
4949 rc = -ENOMEM;
4950 goto lpfc_sli_hba_setup_error;
4951 }
4952 for (i = 0; i < phba->max_vpi; i++)
4953 phba->vpi_ids[i] = i;
4954 }
4955 }
4956
9399627f 4957 /* Init HBQs */
ed957684
JS
4958 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4959 rc = lpfc_sli_hbq_setup(phba);
4960 if (rc)
4961 goto lpfc_sli_hba_setup_error;
4962 }
04c68496 4963 spin_lock_irq(&phba->hbalock);
dea3101e 4964 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4965 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4966
4967 rc = lpfc_config_port_post(phba);
4968 if (rc)
4969 goto lpfc_sli_hba_setup_error;
4970
ed957684
JS
4971 return rc;
4972
92d7f7b0 4973lpfc_sli_hba_setup_error:
2e0fef85 4974 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4975 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4976 "0445 Firmware initialization failed\n");
dea3101e
JB
4977 return rc;
4978}
4979
e59058c4 4980/**
da0436e9
JS
4981 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4982 * @phba: Pointer to HBA context object.
4983 * @mboxq: mailbox pointer.
4984 * This function issue a dump mailbox command to read config region
4985 * 23 and parse the records in the region and populate driver
4986 * data structure.
e59058c4 4987 **/
da0436e9 4988static int
ff78d8f9 4989lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4990{
ff78d8f9 4991 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4992 struct lpfc_dmabuf *mp;
4993 struct lpfc_mqe *mqe;
4994 uint32_t data_length;
4995 int rc;
dea3101e 4996
da0436e9
JS
4997 /* Program the default value of vlan_id and fc_map */
4998 phba->valid_vlan = 0;
4999 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5000 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5001 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 5002
ff78d8f9
JS
5003 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5004 if (!mboxq)
da0436e9
JS
5005 return -ENOMEM;
5006
ff78d8f9
JS
5007 mqe = &mboxq->u.mqe;
5008 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5009 rc = -ENOMEM;
5010 goto out_free_mboxq;
5011 }
5012
da0436e9
JS
5013 mp = (struct lpfc_dmabuf *) mboxq->context1;
5014 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5015
5016 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5017 "(%d):2571 Mailbox cmd x%x Status x%x "
5018 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5019 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5020 "CQ: x%x x%x x%x x%x\n",
5021 mboxq->vport ? mboxq->vport->vpi : 0,
5022 bf_get(lpfc_mqe_command, mqe),
5023 bf_get(lpfc_mqe_status, mqe),
5024 mqe->un.mb_words[0], mqe->un.mb_words[1],
5025 mqe->un.mb_words[2], mqe->un.mb_words[3],
5026 mqe->un.mb_words[4], mqe->un.mb_words[5],
5027 mqe->un.mb_words[6], mqe->un.mb_words[7],
5028 mqe->un.mb_words[8], mqe->un.mb_words[9],
5029 mqe->un.mb_words[10], mqe->un.mb_words[11],
5030 mqe->un.mb_words[12], mqe->un.mb_words[13],
5031 mqe->un.mb_words[14], mqe->un.mb_words[15],
5032 mqe->un.mb_words[16], mqe->un.mb_words[50],
5033 mboxq->mcqe.word0,
5034 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5035 mboxq->mcqe.trailer);
5036
5037 if (rc) {
5038 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5039 kfree(mp);
ff78d8f9
JS
5040 rc = -EIO;
5041 goto out_free_mboxq;
da0436e9
JS
5042 }
5043 data_length = mqe->un.mb_words[5];
a0c87cbd 5044 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5045 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5046 kfree(mp);
ff78d8f9
JS
5047 rc = -EIO;
5048 goto out_free_mboxq;
d11e31dd 5049 }
dea3101e 5050
da0436e9
JS
5051 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5052 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5053 kfree(mp);
ff78d8f9
JS
5054 rc = 0;
5055
5056out_free_mboxq:
5057 mempool_free(mboxq, phba->mbox_mem_pool);
5058 return rc;
da0436e9 5059}
e59058c4
JS
5060
5061/**
da0436e9
JS
5062 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5063 * @phba: pointer to lpfc hba data structure.
5064 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5065 * @vpd: pointer to the memory to hold resulting port vpd data.
5066 * @vpd_size: On input, the number of bytes allocated to @vpd.
5067 * On output, the number of data bytes in @vpd.
e59058c4 5068 *
da0436e9
JS
5069 * This routine executes a READ_REV SLI4 mailbox command. In
5070 * addition, this routine gets the port vpd data.
5071 *
5072 * Return codes
af901ca1 5073 * 0 - successful
d439d286 5074 * -ENOMEM - could not allocated memory.
e59058c4 5075 **/
da0436e9
JS
5076static int
5077lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5078 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5079{
da0436e9
JS
5080 int rc = 0;
5081 uint32_t dma_size;
5082 struct lpfc_dmabuf *dmabuf;
5083 struct lpfc_mqe *mqe;
dea3101e 5084
da0436e9
JS
5085 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5086 if (!dmabuf)
5087 return -ENOMEM;
5088
5089 /*
5090 * Get a DMA buffer for the vpd data resulting from the READ_REV
5091 * mailbox command.
a257bf90 5092 */
da0436e9 5093 dma_size = *vpd_size;
1aee383d
JP
5094 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5095 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5096 if (!dmabuf->virt) {
5097 kfree(dmabuf);
5098 return -ENOMEM;
a257bf90
JS
5099 }
5100
da0436e9
JS
5101 /*
5102 * The SLI4 implementation of READ_REV conflicts at word1,
5103 * bits 31:16 and SLI4 adds vpd functionality not present
5104 * in SLI3. This code corrects the conflicts.
1dcb58e5 5105 */
da0436e9
JS
5106 lpfc_read_rev(phba, mboxq);
5107 mqe = &mboxq->u.mqe;
5108 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5109 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5110 mqe->un.read_rev.word1 &= 0x0000FFFF;
5111 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5112 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5113
5114 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5115 if (rc) {
5116 dma_free_coherent(&phba->pcidev->dev, dma_size,
5117 dmabuf->virt, dmabuf->phys);
def9c7a9 5118 kfree(dmabuf);
da0436e9
JS
5119 return -EIO;
5120 }
1dcb58e5 5121
da0436e9
JS
5122 /*
5123 * The available vpd length cannot be bigger than the
5124 * DMA buffer passed to the port. Catch the less than
5125 * case and update the caller's size.
5126 */
5127 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5128 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5129
d7c47992
JS
5130 memcpy(vpd, dmabuf->virt, *vpd_size);
5131
da0436e9
JS
5132 dma_free_coherent(&phba->pcidev->dev, dma_size,
5133 dmabuf->virt, dmabuf->phys);
5134 kfree(dmabuf);
5135 return 0;
dea3101e
JB
5136}
5137
cd1c8301
JS
5138/**
5139 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5140 * @phba: pointer to lpfc hba data structure.
5141 *
5142 * This routine retrieves SLI4 device physical port name this PCI function
5143 * is attached to.
5144 *
5145 * Return codes
4907cb7b 5146 * 0 - successful
cd1c8301
JS
5147 * otherwise - failed to retrieve physical port name
5148 **/
5149static int
5150lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5151{
5152 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5153 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5154 struct lpfc_controller_attribute *cntl_attr;
5155 struct lpfc_mbx_get_port_name *get_port_name;
5156 void *virtaddr = NULL;
5157 uint32_t alloclen, reqlen;
5158 uint32_t shdr_status, shdr_add_status;
5159 union lpfc_sli4_cfg_shdr *shdr;
5160 char cport_name = 0;
5161 int rc;
5162
5163 /* We assume nothing at this point */
5164 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5165 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5166
5167 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5168 if (!mboxq)
5169 return -ENOMEM;
cd1c8301 5170 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5171 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5172 lpfc_sli4_read_config(phba);
5173 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5174 goto retrieve_ppname;
cd1c8301
JS
5175
5176 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5177 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5178 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5179 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5180 LPFC_SLI4_MBX_NEMBED);
5181 if (alloclen < reqlen) {
5182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5183 "3084 Allocated DMA memory size (%d) is "
5184 "less than the requested DMA memory size "
5185 "(%d)\n", alloclen, reqlen);
5186 rc = -ENOMEM;
5187 goto out_free_mboxq;
5188 }
5189 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5190 virtaddr = mboxq->sge_array->addr[0];
5191 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5192 shdr = &mbx_cntl_attr->cfg_shdr;
5193 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5194 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5195 if (shdr_status || shdr_add_status || rc) {
5196 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5197 "3085 Mailbox x%x (x%x/x%x) failed, "
5198 "rc:x%x, status:x%x, add_status:x%x\n",
5199 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5200 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5201 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5202 rc, shdr_status, shdr_add_status);
5203 rc = -ENXIO;
5204 goto out_free_mboxq;
5205 }
5206 cntl_attr = &mbx_cntl_attr->cntl_attr;
5207 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5208 phba->sli4_hba.lnk_info.lnk_tp =
5209 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5210 phba->sli4_hba.lnk_info.lnk_no =
5211 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5212 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5213 "3086 lnk_type:%d, lnk_numb:%d\n",
5214 phba->sli4_hba.lnk_info.lnk_tp,
5215 phba->sli4_hba.lnk_info.lnk_no);
5216
5217retrieve_ppname:
5218 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5219 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5220 sizeof(struct lpfc_mbx_get_port_name) -
5221 sizeof(struct lpfc_sli4_cfg_mhdr),
5222 LPFC_SLI4_MBX_EMBED);
5223 get_port_name = &mboxq->u.mqe.un.get_port_name;
5224 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5225 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5226 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5227 phba->sli4_hba.lnk_info.lnk_tp);
5228 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5229 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5231 if (shdr_status || shdr_add_status || rc) {
5232 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5233 "3087 Mailbox x%x (x%x/x%x) failed: "
5234 "rc:x%x, status:x%x, add_status:x%x\n",
5235 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5236 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5237 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5238 rc, shdr_status, shdr_add_status);
5239 rc = -ENXIO;
5240 goto out_free_mboxq;
5241 }
5242 switch (phba->sli4_hba.lnk_info.lnk_no) {
5243 case LPFC_LINK_NUMBER_0:
5244 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5245 &get_port_name->u.response);
5246 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5247 break;
5248 case LPFC_LINK_NUMBER_1:
5249 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5250 &get_port_name->u.response);
5251 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5252 break;
5253 case LPFC_LINK_NUMBER_2:
5254 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5255 &get_port_name->u.response);
5256 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5257 break;
5258 case LPFC_LINK_NUMBER_3:
5259 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5260 &get_port_name->u.response);
5261 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5262 break;
5263 default:
5264 break;
5265 }
5266
5267 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5268 phba->Port[0] = cport_name;
5269 phba->Port[1] = '\0';
5270 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5271 "3091 SLI get port name: %s\n", phba->Port);
5272 }
5273
5274out_free_mboxq:
5275 if (rc != MBX_TIMEOUT) {
5276 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5277 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5278 else
5279 mempool_free(mboxq, phba->mbox_mem_pool);
5280 }
5281 return rc;
5282}
5283
e59058c4 5284/**
da0436e9
JS
5285 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5286 * @phba: pointer to lpfc hba data structure.
e59058c4 5287 *
da0436e9
JS
5288 * This routine is called to explicitly arm the SLI4 device's completion and
5289 * event queues
5290 **/
5291static void
5292lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5293{
895427bd 5294 int qidx;
da0436e9
JS
5295
5296 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5297 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
895427bd
JS
5298 if (phba->sli4_hba.nvmels_cq)
5299 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5300 LPFC_QUEUE_REARM);
5301
5302 if (phba->sli4_hba.fcp_cq)
5303 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5304 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5305 LPFC_QUEUE_REARM);
5306
5307 if (phba->sli4_hba.nvme_cq)
5308 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5309 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5310 LPFC_QUEUE_REARM);
1ba981fd 5311
f38fa0bb 5312 if (phba->cfg_fof)
1ba981fd
JS
5313 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5314
895427bd
JS
5315 if (phba->sli4_hba.hba_eq)
5316 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5317 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5318 LPFC_QUEUE_REARM);
1ba981fd 5319
2d7dbc4c
JS
5320 if (phba->nvmet_support) {
5321 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5322 lpfc_sli4_cq_release(
5323 phba->sli4_hba.nvmet_cqset[qidx],
5324 LPFC_QUEUE_REARM);
5325 }
2e90f4b5 5326 }
1ba981fd
JS
5327
5328 if (phba->cfg_fof)
5329 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5330}
5331
6d368e53
JS
5332/**
5333 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5334 * @phba: Pointer to HBA context object.
5335 * @type: The resource extent type.
b76f2dc9
JS
5336 * @extnt_count: buffer to hold port available extent count.
5337 * @extnt_size: buffer to hold element count per extent.
6d368e53 5338 *
b76f2dc9
JS
5339 * This function calls the port and retrievs the number of available
5340 * extents and their size for a particular extent type.
5341 *
5342 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5343 **/
b76f2dc9 5344int
6d368e53
JS
5345lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5346 uint16_t *extnt_count, uint16_t *extnt_size)
5347{
5348 int rc = 0;
5349 uint32_t length;
5350 uint32_t mbox_tmo;
5351 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5352 LPFC_MBOXQ_t *mbox;
5353
5354 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5355 if (!mbox)
5356 return -ENOMEM;
5357
5358 /* Find out how many extents are available for this resource type */
5359 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5360 sizeof(struct lpfc_sli4_cfg_mhdr));
5361 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5362 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5363 length, LPFC_SLI4_MBX_EMBED);
5364
5365 /* Send an extents count of 0 - the GET doesn't use it. */
5366 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5367 LPFC_SLI4_MBX_EMBED);
5368 if (unlikely(rc)) {
5369 rc = -EIO;
5370 goto err_exit;
5371 }
5372
5373 if (!phba->sli4_hba.intr_enable)
5374 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5375 else {
a183a15f 5376 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5377 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5378 }
5379 if (unlikely(rc)) {
5380 rc = -EIO;
5381 goto err_exit;
5382 }
5383
5384 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5385 if (bf_get(lpfc_mbox_hdr_status,
5386 &rsrc_info->header.cfg_shdr.response)) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5388 "2930 Failed to get resource extents "
5389 "Status 0x%x Add'l Status 0x%x\n",
5390 bf_get(lpfc_mbox_hdr_status,
5391 &rsrc_info->header.cfg_shdr.response),
5392 bf_get(lpfc_mbox_hdr_add_status,
5393 &rsrc_info->header.cfg_shdr.response));
5394 rc = -EIO;
5395 goto err_exit;
5396 }
5397
5398 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5399 &rsrc_info->u.rsp);
5400 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5401 &rsrc_info->u.rsp);
8a9d2e80
JS
5402
5403 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5404 "3162 Retrieved extents type-%d from port: count:%d, "
5405 "size:%d\n", type, *extnt_count, *extnt_size);
5406
5407err_exit:
6d368e53
JS
5408 mempool_free(mbox, phba->mbox_mem_pool);
5409 return rc;
5410}
5411
5412/**
5413 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5414 * @phba: Pointer to HBA context object.
5415 * @type: The extent type to check.
5416 *
5417 * This function reads the current available extents from the port and checks
5418 * if the extent count or extent size has changed since the last access.
5419 * Callers use this routine post port reset to understand if there is a
5420 * extent reprovisioning requirement.
5421 *
5422 * Returns:
5423 * -Error: error indicates problem.
5424 * 1: Extent count or size has changed.
5425 * 0: No changes.
5426 **/
5427static int
5428lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5429{
5430 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5431 uint16_t size_diff, rsrc_ext_size;
5432 int rc = 0;
5433 struct lpfc_rsrc_blks *rsrc_entry;
5434 struct list_head *rsrc_blk_list = NULL;
5435
5436 size_diff = 0;
5437 curr_ext_cnt = 0;
5438 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5439 &rsrc_ext_cnt,
5440 &rsrc_ext_size);
5441 if (unlikely(rc))
5442 return -EIO;
5443
5444 switch (type) {
5445 case LPFC_RSC_TYPE_FCOE_RPI:
5446 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5447 break;
5448 case LPFC_RSC_TYPE_FCOE_VPI:
5449 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5450 break;
5451 case LPFC_RSC_TYPE_FCOE_XRI:
5452 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5453 break;
5454 case LPFC_RSC_TYPE_FCOE_VFI:
5455 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5456 break;
5457 default:
5458 break;
5459 }
5460
5461 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5462 curr_ext_cnt++;
5463 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5464 size_diff++;
5465 }
5466
5467 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5468 rc = 1;
5469
5470 return rc;
5471}
5472
5473/**
5474 * lpfc_sli4_cfg_post_extnts -
5475 * @phba: Pointer to HBA context object.
5476 * @extnt_cnt - number of available extents.
5477 * @type - the extent type (rpi, xri, vfi, vpi).
5478 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5479 * @mbox - pointer to the caller's allocated mailbox structure.
5480 *
5481 * This function executes the extents allocation request. It also
5482 * takes care of the amount of memory needed to allocate or get the
5483 * allocated extents. It is the caller's responsibility to evaluate
5484 * the response.
5485 *
5486 * Returns:
5487 * -Error: Error value describes the condition found.
5488 * 0: if successful
5489 **/
5490static int
8a9d2e80 5491lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5492 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5493{
5494 int rc = 0;
5495 uint32_t req_len;
5496 uint32_t emb_len;
5497 uint32_t alloc_len, mbox_tmo;
5498
5499 /* Calculate the total requested length of the dma memory */
8a9d2e80 5500 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5501
5502 /*
5503 * Calculate the size of an embedded mailbox. The uint32_t
5504 * accounts for extents-specific word.
5505 */
5506 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5507 sizeof(uint32_t);
5508
5509 /*
5510 * Presume the allocation and response will fit into an embedded
5511 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5512 */
5513 *emb = LPFC_SLI4_MBX_EMBED;
5514 if (req_len > emb_len) {
8a9d2e80 5515 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5516 sizeof(union lpfc_sli4_cfg_shdr) +
5517 sizeof(uint32_t);
5518 *emb = LPFC_SLI4_MBX_NEMBED;
5519 }
5520
5521 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5522 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5523 req_len, *emb);
5524 if (alloc_len < req_len) {
5525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5526 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5527 "less than the requested DMA memory "
5528 "size (x%x)\n", alloc_len, req_len);
5529 return -ENOMEM;
5530 }
8a9d2e80 5531 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5532 if (unlikely(rc))
5533 return -EIO;
5534
5535 if (!phba->sli4_hba.intr_enable)
5536 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5537 else {
a183a15f 5538 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5539 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5540 }
5541
5542 if (unlikely(rc))
5543 rc = -EIO;
5544 return rc;
5545}
5546
5547/**
5548 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5549 * @phba: Pointer to HBA context object.
5550 * @type: The resource extent type to allocate.
5551 *
5552 * This function allocates the number of elements for the specified
5553 * resource type.
5554 **/
5555static int
5556lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5557{
5558 bool emb = false;
5559 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5560 uint16_t rsrc_id, rsrc_start, j, k;
5561 uint16_t *ids;
5562 int i, rc;
5563 unsigned long longs;
5564 unsigned long *bmask;
5565 struct lpfc_rsrc_blks *rsrc_blks;
5566 LPFC_MBOXQ_t *mbox;
5567 uint32_t length;
5568 struct lpfc_id_range *id_array = NULL;
5569 void *virtaddr = NULL;
5570 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5571 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5572 struct list_head *ext_blk_list;
5573
5574 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5575 &rsrc_cnt,
5576 &rsrc_size);
5577 if (unlikely(rc))
5578 return -EIO;
5579
5580 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5581 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5582 "3009 No available Resource Extents "
5583 "for resource type 0x%x: Count: 0x%x, "
5584 "Size 0x%x\n", type, rsrc_cnt,
5585 rsrc_size);
5586 return -ENOMEM;
5587 }
5588
8a9d2e80
JS
5589 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5590 "2903 Post resource extents type-0x%x: "
5591 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5592
5593 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5594 if (!mbox)
5595 return -ENOMEM;
5596
8a9d2e80 5597 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5598 if (unlikely(rc)) {
5599 rc = -EIO;
5600 goto err_exit;
5601 }
5602
5603 /*
5604 * Figure out where the response is located. Then get local pointers
5605 * to the response data. The port does not guarantee to respond to
5606 * all extents counts request so update the local variable with the
5607 * allocated count from the port.
5608 */
5609 if (emb == LPFC_SLI4_MBX_EMBED) {
5610 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5611 id_array = &rsrc_ext->u.rsp.id[0];
5612 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5613 } else {
5614 virtaddr = mbox->sge_array->addr[0];
5615 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5616 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5617 id_array = &n_rsrc->id;
5618 }
5619
5620 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5621 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5622
5623 /*
5624 * Based on the resource size and count, correct the base and max
5625 * resource values.
5626 */
5627 length = sizeof(struct lpfc_rsrc_blks);
5628 switch (type) {
5629 case LPFC_RSC_TYPE_FCOE_RPI:
5630 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5631 sizeof(unsigned long),
5632 GFP_KERNEL);
5633 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5634 rc = -ENOMEM;
5635 goto err_exit;
5636 }
5637 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5638 sizeof(uint16_t),
5639 GFP_KERNEL);
5640 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5641 kfree(phba->sli4_hba.rpi_bmask);
5642 rc = -ENOMEM;
5643 goto err_exit;
5644 }
5645
5646 /*
5647 * The next_rpi was initialized with the maximum available
5648 * count but the port may allocate a smaller number. Catch
5649 * that case and update the next_rpi.
5650 */
5651 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5652
5653 /* Initialize local ptrs for common extent processing later. */
5654 bmask = phba->sli4_hba.rpi_bmask;
5655 ids = phba->sli4_hba.rpi_ids;
5656 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5657 break;
5658 case LPFC_RSC_TYPE_FCOE_VPI:
5659 phba->vpi_bmask = kzalloc(longs *
5660 sizeof(unsigned long),
5661 GFP_KERNEL);
5662 if (unlikely(!phba->vpi_bmask)) {
5663 rc = -ENOMEM;
5664 goto err_exit;
5665 }
5666 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5667 sizeof(uint16_t),
5668 GFP_KERNEL);
5669 if (unlikely(!phba->vpi_ids)) {
5670 kfree(phba->vpi_bmask);
5671 rc = -ENOMEM;
5672 goto err_exit;
5673 }
5674
5675 /* Initialize local ptrs for common extent processing later. */
5676 bmask = phba->vpi_bmask;
5677 ids = phba->vpi_ids;
5678 ext_blk_list = &phba->lpfc_vpi_blk_list;
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_XRI:
5681 phba->sli4_hba.xri_bmask = kzalloc(longs *
5682 sizeof(unsigned long),
5683 GFP_KERNEL);
5684 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5685 rc = -ENOMEM;
5686 goto err_exit;
5687 }
8a9d2e80 5688 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5689 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5690 sizeof(uint16_t),
5691 GFP_KERNEL);
5692 if (unlikely(!phba->sli4_hba.xri_ids)) {
5693 kfree(phba->sli4_hba.xri_bmask);
5694 rc = -ENOMEM;
5695 goto err_exit;
5696 }
5697
5698 /* Initialize local ptrs for common extent processing later. */
5699 bmask = phba->sli4_hba.xri_bmask;
5700 ids = phba->sli4_hba.xri_ids;
5701 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5702 break;
5703 case LPFC_RSC_TYPE_FCOE_VFI:
5704 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5705 sizeof(unsigned long),
5706 GFP_KERNEL);
5707 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5708 rc = -ENOMEM;
5709 goto err_exit;
5710 }
5711 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5712 sizeof(uint16_t),
5713 GFP_KERNEL);
5714 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5715 kfree(phba->sli4_hba.vfi_bmask);
5716 rc = -ENOMEM;
5717 goto err_exit;
5718 }
5719
5720 /* Initialize local ptrs for common extent processing later. */
5721 bmask = phba->sli4_hba.vfi_bmask;
5722 ids = phba->sli4_hba.vfi_ids;
5723 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5724 break;
5725 default:
5726 /* Unsupported Opcode. Fail call. */
5727 id_array = NULL;
5728 bmask = NULL;
5729 ids = NULL;
5730 ext_blk_list = NULL;
5731 goto err_exit;
5732 }
5733
5734 /*
5735 * Complete initializing the extent configuration with the
5736 * allocated ids assigned to this function. The bitmask serves
5737 * as an index into the array and manages the available ids. The
5738 * array just stores the ids communicated to the port via the wqes.
5739 */
5740 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5741 if ((i % 2) == 0)
5742 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5743 &id_array[k]);
5744 else
5745 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5746 &id_array[k]);
5747
5748 rsrc_blks = kzalloc(length, GFP_KERNEL);
5749 if (unlikely(!rsrc_blks)) {
5750 rc = -ENOMEM;
5751 kfree(bmask);
5752 kfree(ids);
5753 goto err_exit;
5754 }
5755 rsrc_blks->rsrc_start = rsrc_id;
5756 rsrc_blks->rsrc_size = rsrc_size;
5757 list_add_tail(&rsrc_blks->list, ext_blk_list);
5758 rsrc_start = rsrc_id;
895427bd 5759 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5760 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5761 lpfc_sli4_get_iocb_cnt(phba);
5762 phba->sli4_hba.nvme_xri_start =
5763 phba->sli4_hba.scsi_xri_start +
5764 phba->sli4_hba.scsi_xri_max;
5765 }
6d368e53
JS
5766
5767 while (rsrc_id < (rsrc_start + rsrc_size)) {
5768 ids[j] = rsrc_id;
5769 rsrc_id++;
5770 j++;
5771 }
5772 /* Entire word processed. Get next word.*/
5773 if ((i % 2) == 1)
5774 k++;
5775 }
5776 err_exit:
5777 lpfc_sli4_mbox_cmd_free(phba, mbox);
5778 return rc;
5779}
5780
895427bd
JS
5781
5782
6d368e53
JS
5783/**
5784 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5785 * @phba: Pointer to HBA context object.
5786 * @type: the extent's type.
5787 *
5788 * This function deallocates all extents of a particular resource type.
5789 * SLI4 does not allow for deallocating a particular extent range. It
5790 * is the caller's responsibility to release all kernel memory resources.
5791 **/
5792static int
5793lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5794{
5795 int rc;
5796 uint32_t length, mbox_tmo = 0;
5797 LPFC_MBOXQ_t *mbox;
5798 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5799 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5800
5801 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5802 if (!mbox)
5803 return -ENOMEM;
5804
5805 /*
5806 * This function sends an embedded mailbox because it only sends the
5807 * the resource type. All extents of this type are released by the
5808 * port.
5809 */
5810 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5811 sizeof(struct lpfc_sli4_cfg_mhdr));
5812 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5813 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5814 length, LPFC_SLI4_MBX_EMBED);
5815
5816 /* Send an extents count of 0 - the dealloc doesn't use it. */
5817 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5818 LPFC_SLI4_MBX_EMBED);
5819 if (unlikely(rc)) {
5820 rc = -EIO;
5821 goto out_free_mbox;
5822 }
5823 if (!phba->sli4_hba.intr_enable)
5824 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5825 else {
a183a15f 5826 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5827 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5828 }
5829 if (unlikely(rc)) {
5830 rc = -EIO;
5831 goto out_free_mbox;
5832 }
5833
5834 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5835 if (bf_get(lpfc_mbox_hdr_status,
5836 &dealloc_rsrc->header.cfg_shdr.response)) {
5837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5838 "2919 Failed to release resource extents "
5839 "for type %d - Status 0x%x Add'l Status 0x%x. "
5840 "Resource memory not released.\n",
5841 type,
5842 bf_get(lpfc_mbox_hdr_status,
5843 &dealloc_rsrc->header.cfg_shdr.response),
5844 bf_get(lpfc_mbox_hdr_add_status,
5845 &dealloc_rsrc->header.cfg_shdr.response));
5846 rc = -EIO;
5847 goto out_free_mbox;
5848 }
5849
5850 /* Release kernel memory resources for the specific type. */
5851 switch (type) {
5852 case LPFC_RSC_TYPE_FCOE_VPI:
5853 kfree(phba->vpi_bmask);
5854 kfree(phba->vpi_ids);
5855 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5856 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5857 &phba->lpfc_vpi_blk_list, list) {
5858 list_del_init(&rsrc_blk->list);
5859 kfree(rsrc_blk);
5860 }
16a3a208 5861 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5862 break;
5863 case LPFC_RSC_TYPE_FCOE_XRI:
5864 kfree(phba->sli4_hba.xri_bmask);
5865 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5866 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5867 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5868 list_del_init(&rsrc_blk->list);
5869 kfree(rsrc_blk);
5870 }
5871 break;
5872 case LPFC_RSC_TYPE_FCOE_VFI:
5873 kfree(phba->sli4_hba.vfi_bmask);
5874 kfree(phba->sli4_hba.vfi_ids);
5875 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5876 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5877 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5878 list_del_init(&rsrc_blk->list);
5879 kfree(rsrc_blk);
5880 }
5881 break;
5882 case LPFC_RSC_TYPE_FCOE_RPI:
5883 /* RPI bitmask and physical id array are cleaned up earlier. */
5884 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5885 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5886 list_del_init(&rsrc_blk->list);
5887 kfree(rsrc_blk);
5888 }
5889 break;
5890 default:
5891 break;
5892 }
5893
5894 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5895
5896 out_free_mbox:
5897 mempool_free(mbox, phba->mbox_mem_pool);
5898 return rc;
5899}
5900
bd4b3e5c 5901static void
7bdedb34
JS
5902lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5903 uint32_t feature)
65791f1f 5904{
65791f1f 5905 uint32_t len;
65791f1f 5906
65791f1f
JS
5907 len = sizeof(struct lpfc_mbx_set_feature) -
5908 sizeof(struct lpfc_sli4_cfg_mhdr);
5909 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5910 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5911 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
5912
5913 switch (feature) {
5914 case LPFC_SET_UE_RECOVERY:
5915 bf_set(lpfc_mbx_set_feature_UER,
5916 &mbox->u.mqe.un.set_feature, 1);
5917 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5918 mbox->u.mqe.un.set_feature.param_len = 8;
5919 break;
5920 case LPFC_SET_MDS_DIAGS:
5921 bf_set(lpfc_mbx_set_feature_mds,
5922 &mbox->u.mqe.un.set_feature, 1);
5923 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 5924 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
5925 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5926 mbox->u.mqe.un.set_feature.param_len = 8;
5927 break;
65791f1f 5928 }
7bdedb34
JS
5929
5930 return;
65791f1f
JS
5931}
5932
6d368e53
JS
5933/**
5934 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5935 * @phba: Pointer to HBA context object.
5936 *
5937 * This function allocates all SLI4 resource identifiers.
5938 **/
5939int
5940lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5941{
5942 int i, rc, error = 0;
5943 uint16_t count, base;
5944 unsigned long longs;
5945
ff78d8f9
JS
5946 if (!phba->sli4_hba.rpi_hdrs_in_use)
5947 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5948 if (phba->sli4_hba.extents_in_use) {
5949 /*
5950 * The port supports resource extents. The XRI, VPI, VFI, RPI
5951 * resource extent count must be read and allocated before
5952 * provisioning the resource id arrays.
5953 */
5954 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5955 LPFC_IDX_RSRC_RDY) {
5956 /*
5957 * Extent-based resources are set - the driver could
5958 * be in a port reset. Figure out if any corrective
5959 * actions need to be taken.
5960 */
5961 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5962 LPFC_RSC_TYPE_FCOE_VFI);
5963 if (rc != 0)
5964 error++;
5965 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5966 LPFC_RSC_TYPE_FCOE_VPI);
5967 if (rc != 0)
5968 error++;
5969 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5970 LPFC_RSC_TYPE_FCOE_XRI);
5971 if (rc != 0)
5972 error++;
5973 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5974 LPFC_RSC_TYPE_FCOE_RPI);
5975 if (rc != 0)
5976 error++;
5977
5978 /*
5979 * It's possible that the number of resources
5980 * provided to this port instance changed between
5981 * resets. Detect this condition and reallocate
5982 * resources. Otherwise, there is no action.
5983 */
5984 if (error) {
5985 lpfc_printf_log(phba, KERN_INFO,
5986 LOG_MBOX | LOG_INIT,
5987 "2931 Detected extent resource "
5988 "change. Reallocating all "
5989 "extents.\n");
5990 rc = lpfc_sli4_dealloc_extent(phba,
5991 LPFC_RSC_TYPE_FCOE_VFI);
5992 rc = lpfc_sli4_dealloc_extent(phba,
5993 LPFC_RSC_TYPE_FCOE_VPI);
5994 rc = lpfc_sli4_dealloc_extent(phba,
5995 LPFC_RSC_TYPE_FCOE_XRI);
5996 rc = lpfc_sli4_dealloc_extent(phba,
5997 LPFC_RSC_TYPE_FCOE_RPI);
5998 } else
5999 return 0;
6000 }
6001
6002 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6003 if (unlikely(rc))
6004 goto err_exit;
6005
6006 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6007 if (unlikely(rc))
6008 goto err_exit;
6009
6010 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6011 if (unlikely(rc))
6012 goto err_exit;
6013
6014 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6015 if (unlikely(rc))
6016 goto err_exit;
6017 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6018 LPFC_IDX_RSRC_RDY);
6019 return rc;
6020 } else {
6021 /*
6022 * The port does not support resource extents. The XRI, VPI,
6023 * VFI, RPI resource ids were determined from READ_CONFIG.
6024 * Just allocate the bitmasks and provision the resource id
6025 * arrays. If a port reset is active, the resources don't
6026 * need any action - just exit.
6027 */
6028 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6029 LPFC_IDX_RSRC_RDY) {
6030 lpfc_sli4_dealloc_resource_identifiers(phba);
6031 lpfc_sli4_remove_rpis(phba);
6032 }
6d368e53
JS
6033 /* RPIs. */
6034 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6035 if (count <= 0) {
6036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6037 "3279 Invalid provisioning of "
6038 "rpi:%d\n", count);
6039 rc = -EINVAL;
6040 goto err_exit;
6041 }
6d368e53
JS
6042 base = phba->sli4_hba.max_cfg_param.rpi_base;
6043 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6044 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6045 sizeof(unsigned long),
6046 GFP_KERNEL);
6047 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6048 rc = -ENOMEM;
6049 goto err_exit;
6050 }
6051 phba->sli4_hba.rpi_ids = kzalloc(count *
6052 sizeof(uint16_t),
6053 GFP_KERNEL);
6054 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6055 rc = -ENOMEM;
6056 goto free_rpi_bmask;
6057 }
6058
6059 for (i = 0; i < count; i++)
6060 phba->sli4_hba.rpi_ids[i] = base + i;
6061
6062 /* VPIs. */
6063 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6064 if (count <= 0) {
6065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6066 "3280 Invalid provisioning of "
6067 "vpi:%d\n", count);
6068 rc = -EINVAL;
6069 goto free_rpi_ids;
6070 }
6d368e53
JS
6071 base = phba->sli4_hba.max_cfg_param.vpi_base;
6072 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6073 phba->vpi_bmask = kzalloc(longs *
6074 sizeof(unsigned long),
6075 GFP_KERNEL);
6076 if (unlikely(!phba->vpi_bmask)) {
6077 rc = -ENOMEM;
6078 goto free_rpi_ids;
6079 }
6080 phba->vpi_ids = kzalloc(count *
6081 sizeof(uint16_t),
6082 GFP_KERNEL);
6083 if (unlikely(!phba->vpi_ids)) {
6084 rc = -ENOMEM;
6085 goto free_vpi_bmask;
6086 }
6087
6088 for (i = 0; i < count; i++)
6089 phba->vpi_ids[i] = base + i;
6090
6091 /* XRIs. */
6092 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6093 if (count <= 0) {
6094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6095 "3281 Invalid provisioning of "
6096 "xri:%d\n", count);
6097 rc = -EINVAL;
6098 goto free_vpi_ids;
6099 }
6d368e53
JS
6100 base = phba->sli4_hba.max_cfg_param.xri_base;
6101 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6102 phba->sli4_hba.xri_bmask = kzalloc(longs *
6103 sizeof(unsigned long),
6104 GFP_KERNEL);
6105 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6106 rc = -ENOMEM;
6107 goto free_vpi_ids;
6108 }
41899be7 6109 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6110 phba->sli4_hba.xri_ids = kzalloc(count *
6111 sizeof(uint16_t),
6112 GFP_KERNEL);
6113 if (unlikely(!phba->sli4_hba.xri_ids)) {
6114 rc = -ENOMEM;
6115 goto free_xri_bmask;
6116 }
6117
6118 for (i = 0; i < count; i++)
6119 phba->sli4_hba.xri_ids[i] = base + i;
6120
6121 /* VFIs. */
6122 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6123 if (count <= 0) {
6124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6125 "3282 Invalid provisioning of "
6126 "vfi:%d\n", count);
6127 rc = -EINVAL;
6128 goto free_xri_ids;
6129 }
6d368e53
JS
6130 base = phba->sli4_hba.max_cfg_param.vfi_base;
6131 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6132 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6133 sizeof(unsigned long),
6134 GFP_KERNEL);
6135 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6136 rc = -ENOMEM;
6137 goto free_xri_ids;
6138 }
6139 phba->sli4_hba.vfi_ids = kzalloc(count *
6140 sizeof(uint16_t),
6141 GFP_KERNEL);
6142 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6143 rc = -ENOMEM;
6144 goto free_vfi_bmask;
6145 }
6146
6147 for (i = 0; i < count; i++)
6148 phba->sli4_hba.vfi_ids[i] = base + i;
6149
6150 /*
6151 * Mark all resources ready. An HBA reset doesn't need
6152 * to reset the initialization.
6153 */
6154 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6155 LPFC_IDX_RSRC_RDY);
6156 return 0;
6157 }
6158
6159 free_vfi_bmask:
6160 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6161 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6162 free_xri_ids:
6163 kfree(phba->sli4_hba.xri_ids);
cd60be49 6164 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6165 free_xri_bmask:
6166 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6167 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6168 free_vpi_ids:
6169 kfree(phba->vpi_ids);
cd60be49 6170 phba->vpi_ids = NULL;
6d368e53
JS
6171 free_vpi_bmask:
6172 kfree(phba->vpi_bmask);
cd60be49 6173 phba->vpi_bmask = NULL;
6d368e53
JS
6174 free_rpi_ids:
6175 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6176 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6177 free_rpi_bmask:
6178 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6179 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6180 err_exit:
6181 return rc;
6182}
6183
6184/**
6185 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6186 * @phba: Pointer to HBA context object.
6187 *
6188 * This function allocates the number of elements for the specified
6189 * resource type.
6190 **/
6191int
6192lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6193{
6194 if (phba->sli4_hba.extents_in_use) {
6195 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6196 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6197 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6198 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6199 } else {
6200 kfree(phba->vpi_bmask);
16a3a208 6201 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6202 kfree(phba->vpi_ids);
6203 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6204 kfree(phba->sli4_hba.xri_bmask);
6205 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6206 kfree(phba->sli4_hba.vfi_bmask);
6207 kfree(phba->sli4_hba.vfi_ids);
6208 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6209 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6210 }
6211
6212 return 0;
6213}
6214
b76f2dc9
JS
6215/**
6216 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6217 * @phba: Pointer to HBA context object.
6218 * @type: The resource extent type.
6219 * @extnt_count: buffer to hold port extent count response
6220 * @extnt_size: buffer to hold port extent size response.
6221 *
6222 * This function calls the port to read the host allocated extents
6223 * for a particular type.
6224 **/
6225int
6226lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6227 uint16_t *extnt_cnt, uint16_t *extnt_size)
6228{
6229 bool emb;
6230 int rc = 0;
6231 uint16_t curr_blks = 0;
6232 uint32_t req_len, emb_len;
6233 uint32_t alloc_len, mbox_tmo;
6234 struct list_head *blk_list_head;
6235 struct lpfc_rsrc_blks *rsrc_blk;
6236 LPFC_MBOXQ_t *mbox;
6237 void *virtaddr = NULL;
6238 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6239 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6240 union lpfc_sli4_cfg_shdr *shdr;
6241
6242 switch (type) {
6243 case LPFC_RSC_TYPE_FCOE_VPI:
6244 blk_list_head = &phba->lpfc_vpi_blk_list;
6245 break;
6246 case LPFC_RSC_TYPE_FCOE_XRI:
6247 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6248 break;
6249 case LPFC_RSC_TYPE_FCOE_VFI:
6250 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6251 break;
6252 case LPFC_RSC_TYPE_FCOE_RPI:
6253 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6254 break;
6255 default:
6256 return -EIO;
6257 }
6258
6259 /* Count the number of extents currently allocatd for this type. */
6260 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6261 if (curr_blks == 0) {
6262 /*
6263 * The GET_ALLOCATED mailbox does not return the size,
6264 * just the count. The size should be just the size
6265 * stored in the current allocated block and all sizes
6266 * for an extent type are the same so set the return
6267 * value now.
6268 */
6269 *extnt_size = rsrc_blk->rsrc_size;
6270 }
6271 curr_blks++;
6272 }
6273
b76f2dc9
JS
6274 /*
6275 * Calculate the size of an embedded mailbox. The uint32_t
6276 * accounts for extents-specific word.
6277 */
6278 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6279 sizeof(uint32_t);
6280
6281 /*
6282 * Presume the allocation and response will fit into an embedded
6283 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6284 */
6285 emb = LPFC_SLI4_MBX_EMBED;
6286 req_len = emb_len;
6287 if (req_len > emb_len) {
6288 req_len = curr_blks * sizeof(uint16_t) +
6289 sizeof(union lpfc_sli4_cfg_shdr) +
6290 sizeof(uint32_t);
6291 emb = LPFC_SLI4_MBX_NEMBED;
6292 }
6293
6294 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6295 if (!mbox)
6296 return -ENOMEM;
6297 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6298
6299 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6300 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6301 req_len, emb);
6302 if (alloc_len < req_len) {
6303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6304 "2983 Allocated DMA memory size (x%x) is "
6305 "less than the requested DMA memory "
6306 "size (x%x)\n", alloc_len, req_len);
6307 rc = -ENOMEM;
6308 goto err_exit;
6309 }
6310 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6311 if (unlikely(rc)) {
6312 rc = -EIO;
6313 goto err_exit;
6314 }
6315
6316 if (!phba->sli4_hba.intr_enable)
6317 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6318 else {
a183a15f 6319 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6320 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6321 }
6322
6323 if (unlikely(rc)) {
6324 rc = -EIO;
6325 goto err_exit;
6326 }
6327
6328 /*
6329 * Figure out where the response is located. Then get local pointers
6330 * to the response data. The port does not guarantee to respond to
6331 * all extents counts request so update the local variable with the
6332 * allocated count from the port.
6333 */
6334 if (emb == LPFC_SLI4_MBX_EMBED) {
6335 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6336 shdr = &rsrc_ext->header.cfg_shdr;
6337 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6338 } else {
6339 virtaddr = mbox->sge_array->addr[0];
6340 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6341 shdr = &n_rsrc->cfg_shdr;
6342 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6343 }
6344
6345 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6346 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6347 "2984 Failed to read allocated resources "
6348 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6349 type,
6350 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6351 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6352 rc = -EIO;
6353 goto err_exit;
6354 }
6355 err_exit:
6356 lpfc_sli4_mbox_cmd_free(phba, mbox);
6357 return rc;
6358}
6359
8a9d2e80 6360/**
0ef69968 6361 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6362 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6363 * @pring: Pointer to driver SLI ring object.
6364 * @sgl_list: linked link of sgl buffers to post
6365 * @cnt: number of linked list buffers
8a9d2e80 6366 *
895427bd 6367 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6368 * repost them to the port by using SGL block post. This is needed after a
6369 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6370 * of buffer sgls which contains contiguous xris and uses the non-embedded
6371 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6372 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6373 * mailbox command for posting.
6374 *
6375 * Returns: 0 = success, non-zero failure.
6376 **/
6377static int
895427bd
JS
6378lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6379 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6380{
6381 struct lpfc_sglq *sglq_entry = NULL;
6382 struct lpfc_sglq *sglq_entry_next = NULL;
6383 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6384 int status, total_cnt;
6385 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6386 int last_xritag = NO_XRI;
6387 LIST_HEAD(prep_sgl_list);
6388 LIST_HEAD(blck_sgl_list);
6389 LIST_HEAD(allc_sgl_list);
6390 LIST_HEAD(post_sgl_list);
6391 LIST_HEAD(free_sgl_list);
6392
38c20673 6393 spin_lock_irq(&phba->hbalock);
895427bd
JS
6394 spin_lock(&phba->sli4_hba.sgl_list_lock);
6395 list_splice_init(sgl_list, &allc_sgl_list);
6396 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6397 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6398
895427bd 6399 total_cnt = cnt;
8a9d2e80
JS
6400 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6401 &allc_sgl_list, list) {
6402 list_del_init(&sglq_entry->list);
6403 block_cnt++;
6404 if ((last_xritag != NO_XRI) &&
6405 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6406 /* a hole in xri block, form a sgl posting block */
6407 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6408 post_cnt = block_cnt - 1;
6409 /* prepare list for next posting block */
6410 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6411 block_cnt = 1;
6412 } else {
6413 /* prepare list for next posting block */
6414 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6415 /* enough sgls for non-embed sgl mbox command */
6416 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6417 list_splice_init(&prep_sgl_list,
6418 &blck_sgl_list);
6419 post_cnt = block_cnt;
6420 block_cnt = 0;
6421 }
6422 }
6423 num_posted++;
6424
6425 /* keep track of last sgl's xritag */
6426 last_xritag = sglq_entry->sli4_xritag;
6427
895427bd
JS
6428 /* end of repost sgl list condition for buffers */
6429 if (num_posted == total_cnt) {
8a9d2e80
JS
6430 if (post_cnt == 0) {
6431 list_splice_init(&prep_sgl_list,
6432 &blck_sgl_list);
6433 post_cnt = block_cnt;
6434 } else if (block_cnt == 1) {
6435 status = lpfc_sli4_post_sgl(phba,
6436 sglq_entry->phys, 0,
6437 sglq_entry->sli4_xritag);
6438 if (!status) {
6439 /* successful, put sgl to posted list */
6440 list_add_tail(&sglq_entry->list,
6441 &post_sgl_list);
6442 } else {
6443 /* Failure, put sgl to free list */
6444 lpfc_printf_log(phba, KERN_WARNING,
6445 LOG_SLI,
895427bd 6446 "3159 Failed to post "
8a9d2e80
JS
6447 "sgl, xritag:x%x\n",
6448 sglq_entry->sli4_xritag);
6449 list_add_tail(&sglq_entry->list,
6450 &free_sgl_list);
711ea882 6451 total_cnt--;
8a9d2e80
JS
6452 }
6453 }
6454 }
6455
6456 /* continue until a nembed page worth of sgls */
6457 if (post_cnt == 0)
6458 continue;
6459
895427bd
JS
6460 /* post the buffer list sgls as a block */
6461 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6462 post_cnt);
8a9d2e80
JS
6463
6464 if (!status) {
6465 /* success, put sgl list to posted sgl list */
6466 list_splice_init(&blck_sgl_list, &post_sgl_list);
6467 } else {
6468 /* Failure, put sgl list to free sgl list */
6469 sglq_entry_first = list_first_entry(&blck_sgl_list,
6470 struct lpfc_sglq,
6471 list);
6472 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6473 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6474 "xritag:x%x-x%x\n",
6475 sglq_entry_first->sli4_xritag,
6476 (sglq_entry_first->sli4_xritag +
6477 post_cnt - 1));
6478 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6479 total_cnt -= post_cnt;
8a9d2e80
JS
6480 }
6481
6482 /* don't reset xirtag due to hole in xri block */
6483 if (block_cnt == 0)
6484 last_xritag = NO_XRI;
6485
895427bd 6486 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6487 post_cnt = 0;
6488 }
6489
895427bd 6490 /* free the sgls failed to post */
8a9d2e80
JS
6491 lpfc_free_sgl_list(phba, &free_sgl_list);
6492
895427bd 6493 /* push sgls posted to the available list */
8a9d2e80 6494 if (!list_empty(&post_sgl_list)) {
38c20673 6495 spin_lock_irq(&phba->hbalock);
895427bd
JS
6496 spin_lock(&phba->sli4_hba.sgl_list_lock);
6497 list_splice_init(&post_sgl_list, sgl_list);
6498 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6499 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6500 } else {
6501 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6502 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6503 return -EIO;
6504 }
895427bd
JS
6505
6506 /* return the number of XRIs actually posted */
6507 return total_cnt;
8a9d2e80
JS
6508}
6509
61bda8f7
JS
6510void
6511lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6512{
6513 uint32_t len;
6514
6515 len = sizeof(struct lpfc_mbx_set_host_data) -
6516 sizeof(struct lpfc_sli4_cfg_mhdr);
6517 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6518 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6519 LPFC_SLI4_MBX_EMBED);
6520
6521 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6522 mbox->u.mqe.un.set_host_data.param_len =
6523 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6524 snprintf(mbox->u.mqe.un.set_host_data.data,
6525 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6526 "Linux %s v"LPFC_DRIVER_VERSION,
6527 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6528}
6529
a8cf5dfe 6530int
6c621a22 6531lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 6532 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
6533{
6534 int rc, i;
6535 struct lpfc_rqe hrqe;
6536 struct lpfc_rqe drqe;
6537 struct lpfc_rqb *rqbp;
6538 struct rqb_dmabuf *rqb_buffer;
6539 LIST_HEAD(rqb_buf_list);
6540
6541 rqbp = hrq->rqbp;
6542 for (i = 0; i < count; i++) {
6543 /* IF RQ is already full, don't bother */
6544 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6545 break;
6546 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6547 if (!rqb_buffer)
6548 break;
6549 rqb_buffer->hrq = hrq;
6550 rqb_buffer->drq = drq;
a8cf5dfe 6551 rqb_buffer->idx = idx;
6c621a22
JS
6552 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6553 }
6554 while (!list_empty(&rqb_buf_list)) {
6555 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6556 hbuf.list);
6557
6558 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6559 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6560 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6561 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6562 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6563 if (rc < 0) {
6564 rqbp->rqb_free_buffer(phba, rqb_buffer);
6565 } else {
6566 list_add_tail(&rqb_buffer->hbuf.list,
6567 &rqbp->rqb_buffer_list);
6568 rqbp->buffer_count++;
6569 }
6570 }
6571 return 1;
6572}
6573
da0436e9 6574/**
183b8021 6575 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
6576 * @phba: Pointer to HBA context object.
6577 *
183b8021
MY
6578 * This function is the main SLI4 device initialization PCI function. This
6579 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
6580 * HBA error attention handler code. Caller is not required to hold any
6581 * locks.
6582 **/
6583int
6584lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6585{
6c621a22 6586 int rc, i, cnt;
da0436e9
JS
6587 LPFC_MBOXQ_t *mboxq;
6588 struct lpfc_mqe *mqe;
6589 uint8_t *vpd;
6590 uint32_t vpd_size;
6591 uint32_t ftr_rsp = 0;
6592 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6593 struct lpfc_vport *vport = phba->pport;
6594 struct lpfc_dmabuf *mp;
2d7dbc4c 6595 struct lpfc_rqb *rqbp;
da0436e9
JS
6596
6597 /* Perform a PCI function reset to start from clean */
6598 rc = lpfc_pci_function_reset(phba);
6599 if (unlikely(rc))
6600 return -ENODEV;
6601
6602 /* Check the HBA Host Status Register for readyness */
6603 rc = lpfc_sli4_post_status_check(phba);
6604 if (unlikely(rc))
6605 return -ENODEV;
6606 else {
6607 spin_lock_irq(&phba->hbalock);
6608 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6609 spin_unlock_irq(&phba->hbalock);
6610 }
6611
6612 /*
6613 * Allocate a single mailbox container for initializing the
6614 * port.
6615 */
6616 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6617 if (!mboxq)
6618 return -ENOMEM;
6619
da0436e9 6620 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6621 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6622 vpd = kzalloc(vpd_size, GFP_KERNEL);
6623 if (!vpd) {
6624 rc = -ENOMEM;
6625 goto out_free_mbox;
6626 }
6627
6628 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6629 if (unlikely(rc)) {
6630 kfree(vpd);
6631 goto out_free_mbox;
6632 }
572709e2 6633
da0436e9 6634 mqe = &mboxq->u.mqe;
f1126688 6635 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6636 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6637 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6638 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6639 } else {
76a95d75 6640 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6641 }
45ed1190
JS
6642
6643 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6644 LPFC_DCBX_CEE_MODE)
6645 phba->hba_flag |= HBA_FIP_SUPPORT;
6646 else
6647 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6648
4f2e66c6
JS
6649 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6650
c31098ce 6651 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6653 "0376 READ_REV Error. SLI Level %d "
6654 "FCoE enabled %d\n",
76a95d75 6655 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6656 rc = -EIO;
76a95d75
JS
6657 kfree(vpd);
6658 goto out_free_mbox;
da0436e9 6659 }
cd1c8301 6660
ff78d8f9
JS
6661 /*
6662 * Continue initialization with default values even if driver failed
6663 * to read FCoE param config regions, only read parameters if the
6664 * board is FCoE
6665 */
6666 if (phba->hba_flag & HBA_FCOE_MODE &&
6667 lpfc_sli4_read_fcoe_params(phba))
6668 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6669 "2570 Failed to read FCoE parameters\n");
6670
cd1c8301
JS
6671 /*
6672 * Retrieve sli4 device physical port name, failure of doing it
6673 * is considered as non-fatal.
6674 */
6675 rc = lpfc_sli4_retrieve_pport_name(phba);
6676 if (!rc)
6677 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6678 "3080 Successful retrieving SLI4 device "
6679 "physical port name: %s.\n", phba->Port);
6680
da0436e9
JS
6681 /*
6682 * Evaluate the read rev and vpd data. Populate the driver
6683 * state with the results. If this routine fails, the failure
6684 * is not fatal as the driver will use generic values.
6685 */
6686 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6687 if (unlikely(!rc)) {
6688 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6689 "0377 Error %d parsing vpd. "
6690 "Using defaults.\n", rc);
6691 rc = 0;
6692 }
76a95d75 6693 kfree(vpd);
da0436e9 6694
f1126688
JS
6695 /* Save information as VPD data */
6696 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6697 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6698 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6699 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6700 &mqe->un.read_rev);
6701 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6702 &mqe->un.read_rev);
6703 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6704 &mqe->un.read_rev);
6705 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6706 &mqe->un.read_rev);
6707 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6708 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6709 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6710 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6711 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6712 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6713 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6714 "(%d):0380 READ_REV Status x%x "
6715 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6716 mboxq->vport ? mboxq->vport->vpi : 0,
6717 bf_get(lpfc_mqe_status, mqe),
6718 phba->vpd.rev.opFwName,
6719 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6720 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6721
572709e2
JS
6722 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6723 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6724 if (phba->pport->cfg_lun_queue_depth > rc) {
6725 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6726 "3362 LUN queue depth changed from %d to %d\n",
6727 phba->pport->cfg_lun_queue_depth, rc);
6728 phba->pport->cfg_lun_queue_depth = rc;
6729 }
6730
65791f1f 6731 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6732 LPFC_SLI_INTF_IF_TYPE_0) {
6733 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6734 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6735 if (rc == MBX_SUCCESS) {
6736 phba->hba_flag |= HBA_RECOVERABLE_UE;
6737 /* Set 1Sec interval to detect UE */
6738 phba->eratt_poll_interval = 1;
6739 phba->sli4_hba.ue_to_sr = bf_get(
6740 lpfc_mbx_set_feature_UESR,
6741 &mboxq->u.mqe.un.set_feature);
6742 phba->sli4_hba.ue_to_rp = bf_get(
6743 lpfc_mbx_set_feature_UERP,
6744 &mboxq->u.mqe.un.set_feature);
6745 }
6746 }
6747
6748 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6749 /* Enable MDS Diagnostics only if the SLI Port supports it */
6750 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6751 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6752 if (rc != MBX_SUCCESS)
6753 phba->mds_diags_support = 0;
6754 }
572709e2 6755
da0436e9
JS
6756 /*
6757 * Discover the port's supported feature set and match it against the
6758 * hosts requests.
6759 */
6760 lpfc_request_features(phba, mboxq);
6761 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6762 if (unlikely(rc)) {
6763 rc = -EIO;
76a95d75 6764 goto out_free_mbox;
da0436e9
JS
6765 }
6766
6767 /*
6768 * The port must support FCP initiator mode as this is the
6769 * only mode running in the host.
6770 */
6771 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6772 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6773 "0378 No support for fcpi mode.\n");
6774 ftr_rsp++;
6775 }
fedd3b7b
JS
6776 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6777 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6778 else
6779 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6780 /*
6781 * If the port cannot support the host's requested features
6782 * then turn off the global config parameters to disable the
6783 * feature in the driver. This is not a fatal error.
6784 */
bf08611b
JS
6785 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6786 if (phba->cfg_enable_bg) {
6787 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6788 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6789 else
6790 ftr_rsp++;
6791 }
da0436e9
JS
6792
6793 if (phba->max_vpi && phba->cfg_enable_npiv &&
6794 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6795 ftr_rsp++;
6796
6797 if (ftr_rsp) {
6798 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6799 "0379 Feature Mismatch Data: x%08x %08x "
6800 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6801 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6802 phba->cfg_enable_npiv, phba->max_vpi);
6803 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6804 phba->cfg_enable_bg = 0;
6805 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6806 phba->cfg_enable_npiv = 0;
6807 }
6808
6809 /* These SLI3 features are assumed in SLI4 */
6810 spin_lock_irq(&phba->hbalock);
6811 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6812 spin_unlock_irq(&phba->hbalock);
6813
6d368e53
JS
6814 /*
6815 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6816 * calls depends on these resources to complete port setup.
6817 */
6818 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6819 if (rc) {
6820 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6821 "2920 Failed to alloc Resource IDs "
6822 "rc = x%x\n", rc);
6823 goto out_free_mbox;
6824 }
6825
61bda8f7
JS
6826 lpfc_set_host_data(phba, mboxq);
6827
6828 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6829 if (rc) {
6830 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6831 "2134 Failed to set host os driver version %x",
6832 rc);
6833 }
6834
da0436e9 6835 /* Read the port's service parameters. */
9f1177a3
JS
6836 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6837 if (rc) {
6838 phba->link_state = LPFC_HBA_ERROR;
6839 rc = -ENOMEM;
76a95d75 6840 goto out_free_mbox;
9f1177a3
JS
6841 }
6842
da0436e9
JS
6843 mboxq->vport = vport;
6844 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6845 mp = (struct lpfc_dmabuf *) mboxq->context1;
6846 if (rc == MBX_SUCCESS) {
6847 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6848 rc = 0;
6849 }
6850
6851 /*
6852 * This memory was allocated by the lpfc_read_sparam routine. Release
6853 * it to the mbuf pool.
6854 */
6855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6856 kfree(mp);
6857 mboxq->context1 = NULL;
6858 if (unlikely(rc)) {
6859 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6860 "0382 READ_SPARAM command failed "
6861 "status %d, mbxStatus x%x\n",
6862 rc, bf_get(lpfc_mqe_status, mqe));
6863 phba->link_state = LPFC_HBA_ERROR;
6864 rc = -EIO;
76a95d75 6865 goto out_free_mbox;
da0436e9
JS
6866 }
6867
0558056c 6868 lpfc_update_vport_wwn(vport);
da0436e9
JS
6869
6870 /* Update the fc_host data structures with new wwn. */
6871 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6872 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6873
895427bd
JS
6874 /* Create all the SLI4 queues */
6875 rc = lpfc_sli4_queue_create(phba);
6876 if (rc) {
6877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6878 "3089 Failed to allocate queues\n");
6879 rc = -ENODEV;
6880 goto out_free_mbox;
6881 }
6882 /* Set up all the queues to the device */
6883 rc = lpfc_sli4_queue_setup(phba);
6884 if (unlikely(rc)) {
6885 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6886 "0381 Error %d during queue setup.\n ", rc);
6887 goto out_stop_timers;
6888 }
6889 /* Initialize the driver internal SLI layer lists. */
6890 lpfc_sli4_setup(phba);
6891 lpfc_sli4_queue_init(phba);
6892
6893 /* update host els xri-sgl sizes and mappings */
6894 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
6895 if (unlikely(rc)) {
6896 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6897 "1400 Failed to update xri-sgl size and "
6898 "mapping: %d\n", rc);
895427bd 6899 goto out_destroy_queue;
da0436e9
JS
6900 }
6901
8a9d2e80 6902 /* register the els sgl pool to the port */
895427bd
JS
6903 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6904 phba->sli4_hba.els_xri_cnt);
6905 if (unlikely(rc < 0)) {
8a9d2e80
JS
6906 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6907 "0582 Error %d during els sgl post "
6908 "operation\n", rc);
6909 rc = -ENODEV;
895427bd 6910 goto out_destroy_queue;
8a9d2e80 6911 }
895427bd 6912 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 6913
f358dd0c
JS
6914 if (phba->nvmet_support) {
6915 /* update host nvmet xri-sgl sizes and mappings */
6916 rc = lpfc_sli4_nvmet_sgl_update(phba);
6917 if (unlikely(rc)) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "6308 Failed to update nvmet-sgl size "
6920 "and mapping: %d\n", rc);
6921 goto out_destroy_queue;
6922 }
6923
6924 /* register the nvmet sgl pool to the port */
6925 rc = lpfc_sli4_repost_sgl_list(
6926 phba,
6927 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6928 phba->sli4_hba.nvmet_xri_cnt);
6929 if (unlikely(rc < 0)) {
6930 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6931 "3117 Error %d during nvmet "
6932 "sgl post\n", rc);
6933 rc = -ENODEV;
6934 goto out_destroy_queue;
6935 }
6936 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
6937
6938 cnt = phba->cfg_iocb_cnt * 1024;
6939 /* We need 1 iocbq for every SGL, for IO processing */
6940 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 6941 } else {
895427bd
JS
6942 /* update host scsi xri-sgl sizes and mappings */
6943 rc = lpfc_sli4_scsi_sgl_update(phba);
6944 if (unlikely(rc)) {
6945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6946 "6309 Failed to update scsi-sgl size "
6947 "and mapping: %d\n", rc);
6948 goto out_destroy_queue;
6949 }
6950
6951 /* update host nvme xri-sgl sizes and mappings */
6952 rc = lpfc_sli4_nvme_sgl_update(phba);
6953 if (unlikely(rc)) {
6954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6955 "6082 Failed to update nvme-sgl size "
6956 "and mapping: %d\n", rc);
6957 goto out_destroy_queue;
6958 }
6c621a22
JS
6959
6960 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
6961 }
6962
6963 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
6964 /* Initialize and populate the iocb list per host */
6965 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 6966 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
6967 phba->cfg_iocb_cnt, cnt);
6968 rc = lpfc_init_iocb_list(phba, cnt);
6969 if (rc) {
6970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 6971 "1413 Failed to init iocb list.\n");
6c621a22
JS
6972 goto out_destroy_queue;
6973 }
895427bd
JS
6974 }
6975
11e644e2
JS
6976 if (phba->nvmet_support)
6977 lpfc_nvmet_create_targetport(phba);
6978
2d7dbc4c 6979 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
6980 /* Post initial buffers to all RQs created */
6981 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6982 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6983 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6984 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6985 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 6986 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
6987 rqbp->buffer_count = 0;
6988
2d7dbc4c
JS
6989 lpfc_post_rq_buffer(
6990 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6991 phba->sli4_hba.nvmet_mrq_data[i],
a8cf5dfe 6992 LPFC_NVMET_RQE_DEF_COUNT, i);
2d7dbc4c
JS
6993 }
6994 }
6995
895427bd
JS
6996 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6997 /* register the allocated scsi sgl pool to the port */
6998 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6999 if (unlikely(rc)) {
7000 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7001 "0383 Error %d during scsi sgl post "
7002 "operation\n", rc);
7003 /* Some Scsi buffers were moved to abort scsi list */
7004 /* A pci function reset will repost them */
7005 rc = -ENODEV;
7006 goto out_destroy_queue;
7007 }
da0436e9
JS
7008 }
7009
01649561
JS
7010 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7011 (phba->nvmet_support == 0)) {
7012
7013 /* register the allocated nvme sgl pool to the port */
7014 rc = lpfc_repost_nvme_sgl_list(phba);
7015 if (unlikely(rc)) {
7016 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7017 "6116 Error %d during nvme sgl post "
7018 "operation\n", rc);
7019 /* Some NVME buffers were moved to abort nvme list */
7020 /* A pci function reset will repost them */
7021 rc = -ENODEV;
7022 goto out_destroy_queue;
7023 }
da0436e9
JS
7024 }
7025
7026 /* Post the rpi header region to the device. */
7027 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7028 if (unlikely(rc)) {
7029 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7030 "0393 Error %d during rpi post operation\n",
7031 rc);
7032 rc = -ENODEV;
895427bd 7033 goto out_destroy_queue;
da0436e9 7034 }
97f2ecf1 7035 lpfc_sli4_node_prep(phba);
da0436e9 7036
895427bd 7037 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7038 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7039 /*
7040 * The FC Port needs to register FCFI (index 0)
7041 */
7042 lpfc_reg_fcfi(phba, mboxq);
7043 mboxq->vport = phba->pport;
7044 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7045 if (rc != MBX_SUCCESS)
7046 goto out_unset_queue;
7047 rc = 0;
7048 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7049 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7050 } else {
7051 /* We are a NVME Target mode with MRQ > 1 */
7052
7053 /* First register the FCFI */
7054 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7055 mboxq->vport = phba->pport;
7056 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7057 if (rc != MBX_SUCCESS)
7058 goto out_unset_queue;
7059 rc = 0;
7060 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7061 &mboxq->u.mqe.un.reg_fcfi_mrq);
7062
7063 /* Next register the MRQs */
7064 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7065 mboxq->vport = phba->pport;
7066 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7067 if (rc != MBX_SUCCESS)
7068 goto out_unset_queue;
7069 rc = 0;
895427bd
JS
7070 }
7071 /* Check if the port is configured to be disabled */
7072 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7073 }
7074
7075 /* Arm the CQs and then EQs on device */
7076 lpfc_sli4_arm_cqeq_intr(phba);
7077
7078 /* Indicate device interrupt mode */
7079 phba->sli4_hba.intr_enable = 1;
7080
7081 /* Allow asynchronous mailbox command to go through */
7082 spin_lock_irq(&phba->hbalock);
7083 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7084 spin_unlock_irq(&phba->hbalock);
7085
7086 /* Post receive buffers to the device */
7087 lpfc_sli4_rb_setup(phba);
7088
fc2b989b
JS
7089 /* Reset HBA FCF states after HBA reset */
7090 phba->fcf.fcf_flag = 0;
7091 phba->fcf.current_rec.flag = 0;
7092
da0436e9 7093 /* Start the ELS watchdog timer */
8fa38513 7094 mod_timer(&vport->els_tmofunc,
256ec0d0 7095 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7096
7097 /* Start heart beat timer */
7098 mod_timer(&phba->hb_tmofunc,
256ec0d0 7099 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7100 phba->hb_outstanding = 0;
7101 phba->last_completion_time = jiffies;
7102
7103 /* Start error attention (ERATT) polling timer */
256ec0d0 7104 mod_timer(&phba->eratt_poll,
65791f1f 7105 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7106
75baf696
JS
7107 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7108 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7109 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7110 if (!rc) {
7111 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7112 "2829 This device supports "
7113 "Advanced Error Reporting (AER)\n");
7114 spin_lock_irq(&phba->hbalock);
7115 phba->hba_flag |= HBA_AER_ENABLED;
7116 spin_unlock_irq(&phba->hbalock);
7117 } else {
7118 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7119 "2830 This device does not support "
7120 "Advanced Error Reporting (AER)\n");
7121 phba->cfg_aer_support = 0;
7122 }
0a96e975 7123 rc = 0;
75baf696
JS
7124 }
7125
da0436e9
JS
7126 /*
7127 * The port is ready, set the host's link state to LINK_DOWN
7128 * in preparation for link interrupts.
7129 */
da0436e9
JS
7130 spin_lock_irq(&phba->hbalock);
7131 phba->link_state = LPFC_LINK_DOWN;
7132 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7133 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7134 (phba->hba_flag & LINK_DISABLED)) {
7135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7136 "3103 Adapter Link is disabled.\n");
7137 lpfc_down_link(phba, mboxq);
7138 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7139 if (rc != MBX_SUCCESS) {
7140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7141 "3104 Adapter failed to issue "
7142 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7143 goto out_unset_queue;
7144 }
7145 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7146 /* don't perform init_link on SLI4 FC port loopback test */
7147 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7148 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7149 if (rc)
7150 goto out_unset_queue;
7151 }
5350d872
JS
7152 }
7153 mempool_free(mboxq, phba->mbox_mem_pool);
7154 return rc;
76a95d75 7155out_unset_queue:
da0436e9 7156 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7157 lpfc_sli4_queue_unset(phba);
7158out_destroy_queue:
6c621a22 7159 lpfc_free_iocb_list(phba);
5350d872 7160 lpfc_sli4_queue_destroy(phba);
da0436e9 7161out_stop_timers:
5350d872 7162 lpfc_stop_hba_timers(phba);
da0436e9
JS
7163out_free_mbox:
7164 mempool_free(mboxq, phba->mbox_mem_pool);
7165 return rc;
7166}
7167
7168/**
7169 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7170 * @ptr: context object - pointer to hba structure.
7171 *
7172 * This is the callback function for mailbox timer. The mailbox
7173 * timer is armed when a new mailbox command is issued and the timer
7174 * is deleted when the mailbox complete. The function is called by
7175 * the kernel timer code when a mailbox does not complete within
7176 * expected time. This function wakes up the worker thread to
7177 * process the mailbox timeout and returns. All the processing is
7178 * done by the worker thread function lpfc_mbox_timeout_handler.
7179 **/
7180void
f22eb4d3 7181lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7182{
f22eb4d3 7183 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7184 unsigned long iflag;
7185 uint32_t tmo_posted;
7186
7187 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7188 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7189 if (!tmo_posted)
7190 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7191 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7192
7193 if (!tmo_posted)
7194 lpfc_worker_wake_up(phba);
7195 return;
7196}
7197
e8d3c3b1
JS
7198/**
7199 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7200 * are pending
7201 * @phba: Pointer to HBA context object.
7202 *
7203 * This function checks if any mailbox completions are present on the mailbox
7204 * completion queue.
7205 **/
3bb11fc5 7206static bool
e8d3c3b1
JS
7207lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7208{
7209
7210 uint32_t idx;
7211 struct lpfc_queue *mcq;
7212 struct lpfc_mcqe *mcqe;
7213 bool pending_completions = false;
7214
7215 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7216 return false;
7217
7218 /* Check for completions on mailbox completion queue */
7219
7220 mcq = phba->sli4_hba.mbx_cq;
7221 idx = mcq->hba_index;
7222 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7223 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7224 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7225 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7226 pending_completions = true;
7227 break;
7228 }
7229 idx = (idx + 1) % mcq->entry_count;
7230 if (mcq->hba_index == idx)
7231 break;
7232 }
7233 return pending_completions;
7234
7235}
7236
7237/**
7238 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7239 * that were missed.
7240 * @phba: Pointer to HBA context object.
7241 *
7242 * For sli4, it is possible to miss an interrupt. As such mbox completions
7243 * maybe missed causing erroneous mailbox timeouts to occur. This function
7244 * checks to see if mbox completions are on the mailbox completion queue
7245 * and will process all the completions associated with the eq for the
7246 * mailbox completion queue.
7247 **/
7248bool
7249lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7250{
7251
7252 uint32_t eqidx;
7253 struct lpfc_queue *fpeq = NULL;
7254 struct lpfc_eqe *eqe;
7255 bool mbox_pending;
7256
7257 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7258 return false;
7259
7260 /* Find the eq associated with the mcq */
7261
7262 if (phba->sli4_hba.hba_eq)
895427bd 7263 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
e8d3c3b1
JS
7264 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7265 phba->sli4_hba.mbx_cq->assoc_qid) {
7266 fpeq = phba->sli4_hba.hba_eq[eqidx];
7267 break;
7268 }
7269 if (!fpeq)
7270 return false;
7271
7272 /* Turn off interrupts from this EQ */
7273
7274 lpfc_sli4_eq_clr_intr(fpeq);
7275
7276 /* Check to see if a mbox completion is pending */
7277
7278 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7279
7280 /*
7281 * If a mbox completion is pending, process all the events on EQ
7282 * associated with the mbox completion queue (this could include
7283 * mailbox commands, async events, els commands, receive queue data
7284 * and fcp commands)
7285 */
7286
7287 if (mbox_pending)
7288 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7289 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7290 fpeq->EQ_processed++;
7291 }
7292
7293 /* Always clear and re-arm the EQ */
7294
7295 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7296
7297 return mbox_pending;
7298
7299}
da0436e9
JS
7300
7301/**
7302 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7303 * @phba: Pointer to HBA context object.
7304 *
7305 * This function is called from worker thread when a mailbox command times out.
7306 * The caller is not required to hold any locks. This function will reset the
7307 * HBA and recover all the pending commands.
7308 **/
7309void
7310lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7311{
7312 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7313 MAILBOX_t *mb = NULL;
7314
da0436e9 7315 struct lpfc_sli *psli = &phba->sli;
da0436e9 7316
e8d3c3b1
JS
7317 /* If the mailbox completed, process the completion and return */
7318 if (lpfc_sli4_process_missed_mbox_completions(phba))
7319 return;
7320
eb016566
JS
7321 if (pmbox != NULL)
7322 mb = &pmbox->u.mb;
da0436e9
JS
7323 /* Check the pmbox pointer first. There is a race condition
7324 * between the mbox timeout handler getting executed in the
7325 * worklist and the mailbox actually completing. When this
7326 * race condition occurs, the mbox_active will be NULL.
7327 */
7328 spin_lock_irq(&phba->hbalock);
7329 if (pmbox == NULL) {
7330 lpfc_printf_log(phba, KERN_WARNING,
7331 LOG_MBOX | LOG_SLI,
7332 "0353 Active Mailbox cleared - mailbox timeout "
7333 "exiting\n");
7334 spin_unlock_irq(&phba->hbalock);
7335 return;
7336 }
7337
7338 /* Mbox cmd <mbxCommand> timeout */
7339 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7340 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7341 mb->mbxCommand,
7342 phba->pport->port_state,
7343 phba->sli.sli_flag,
7344 phba->sli.mbox_active);
7345 spin_unlock_irq(&phba->hbalock);
7346
7347 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7348 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7349 * it to fail all outstanding SCSI IO.
da0436e9
JS
7350 */
7351 spin_lock_irq(&phba->pport->work_port_lock);
7352 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7353 spin_unlock_irq(&phba->pport->work_port_lock);
7354 spin_lock_irq(&phba->hbalock);
7355 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7356 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7357 spin_unlock_irq(&phba->hbalock);
7358
db55fba8 7359 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7360
7361 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7362 "0345 Resetting board due to mailbox timeout\n");
7363
7364 /* Reset the HBA device */
7365 lpfc_reset_hba(phba);
7366}
7367
7368/**
7369 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7370 * @phba: Pointer to HBA context object.
7371 * @pmbox: Pointer to mailbox object.
7372 * @flag: Flag indicating how the mailbox need to be processed.
7373 *
7374 * This function is called by discovery code and HBA management code
7375 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7376 * function gets the hbalock to protect the data structures.
7377 * The mailbox command can be submitted in polling mode, in which case
7378 * this function will wait in a polling loop for the completion of the
7379 * mailbox.
7380 * If the mailbox is submitted in no_wait mode (not polling) the
7381 * function will submit the command and returns immediately without waiting
7382 * for the mailbox completion. The no_wait is supported only when HBA
7383 * is in SLI2/SLI3 mode - interrupts are enabled.
7384 * The SLI interface allows only one mailbox pending at a time. If the
7385 * mailbox is issued in polling mode and there is already a mailbox
7386 * pending, then the function will return an error. If the mailbox is issued
7387 * in NO_WAIT mode and there is a mailbox pending already, the function
7388 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7389 * The sli layer owns the mailbox object until the completion of mailbox
7390 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7391 * return codes the caller owns the mailbox command after the return of
7392 * the function.
e59058c4 7393 **/
3772a991
JS
7394static int
7395lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7396 uint32_t flag)
dea3101e 7397{
bf07bdea 7398 MAILBOX_t *mbx;
2e0fef85 7399 struct lpfc_sli *psli = &phba->sli;
dea3101e 7400 uint32_t status, evtctr;
9940b97b 7401 uint32_t ha_copy, hc_copy;
dea3101e 7402 int i;
09372820 7403 unsigned long timeout;
dea3101e 7404 unsigned long drvr_flag = 0;
34b02dcd 7405 uint32_t word0, ldata;
dea3101e 7406 void __iomem *to_slim;
58da1ffb
JS
7407 int processing_queue = 0;
7408
7409 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7410 if (!pmbox) {
8568a4d2 7411 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7412 /* processing mbox queue from intr_handler */
3772a991
JS
7413 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7414 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7415 return MBX_SUCCESS;
7416 }
58da1ffb 7417 processing_queue = 1;
58da1ffb
JS
7418 pmbox = lpfc_mbox_get(phba);
7419 if (!pmbox) {
7420 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7421 return MBX_SUCCESS;
7422 }
7423 }
dea3101e 7424
ed957684 7425 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7426 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7427 if(!pmbox->vport) {
58da1ffb 7428 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7429 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7430 LOG_MBOX | LOG_VPORT,
e8b62011 7431 "1806 Mbox x%x failed. No vport\n",
3772a991 7432 pmbox->u.mb.mbxCommand);
ed957684 7433 dump_stack();
58da1ffb 7434 goto out_not_finished;
ed957684
JS
7435 }
7436 }
7437
8d63f375 7438 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7439 if (unlikely(pci_channel_offline(phba->pcidev))) {
7440 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7441 goto out_not_finished;
7442 }
8d63f375 7443
a257bf90
JS
7444 /* If HBA has a deferred error attention, fail the iocb. */
7445 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7446 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7447 goto out_not_finished;
7448 }
7449
dea3101e 7450 psli = &phba->sli;
92d7f7b0 7451
bf07bdea 7452 mbx = &pmbox->u.mb;
dea3101e
JB
7453 status = MBX_SUCCESS;
7454
2e0fef85
JS
7455 if (phba->link_state == LPFC_HBA_ERROR) {
7456 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7457
7458 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7459 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7460 "(%d):0311 Mailbox command x%x cannot "
7461 "issue Data: x%x x%x\n",
7462 pmbox->vport ? pmbox->vport->vpi : 0,
7463 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7464 goto out_not_finished;
41415862
JW
7465 }
7466
bf07bdea 7467 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7468 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7469 !(hc_copy & HC_MBINT_ENA)) {
7470 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7472 "(%d):2528 Mailbox command x%x cannot "
7473 "issue Data: x%x x%x\n",
7474 pmbox->vport ? pmbox->vport->vpi : 0,
7475 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7476 goto out_not_finished;
7477 }
9290831f
JS
7478 }
7479
dea3101e
JB
7480 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7481 /* Polling for a mbox command when another one is already active
7482 * is not allowed in SLI. Also, the driver must have established
7483 * SLI2 mode to queue and process multiple mbox commands.
7484 */
7485
7486 if (flag & MBX_POLL) {
2e0fef85 7487 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7488
7489 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7490 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7491 "(%d):2529 Mailbox command x%x "
7492 "cannot issue Data: x%x x%x\n",
7493 pmbox->vport ? pmbox->vport->vpi : 0,
7494 pmbox->u.mb.mbxCommand,
7495 psli->sli_flag, flag);
58da1ffb 7496 goto out_not_finished;
dea3101e
JB
7497 }
7498
3772a991 7499 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7500 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7501 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7503 "(%d):2530 Mailbox command x%x "
7504 "cannot issue Data: x%x x%x\n",
7505 pmbox->vport ? pmbox->vport->vpi : 0,
7506 pmbox->u.mb.mbxCommand,
7507 psli->sli_flag, flag);
58da1ffb 7508 goto out_not_finished;
dea3101e
JB
7509 }
7510
dea3101e
JB
7511 /* Another mailbox command is still being processed, queue this
7512 * command to be processed later.
7513 */
7514 lpfc_mbox_put(phba, pmbox);
7515
7516 /* Mbox cmd issue - BUSY */
ed957684 7517 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7518 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7519 "x%x x%x x%x x%x\n",
92d7f7b0 7520 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
7521 mbx->mbxCommand,
7522 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7523 psli->sli_flag, flag);
dea3101e
JB
7524
7525 psli->slistat.mbox_busy++;
2e0fef85 7526 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7527
858c9f6c
JS
7528 if (pmbox->vport) {
7529 lpfc_debugfs_disc_trc(pmbox->vport,
7530 LPFC_DISC_TRC_MBOX_VPORT,
7531 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7532 (uint32_t)mbx->mbxCommand,
7533 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7534 }
7535 else {
7536 lpfc_debugfs_disc_trc(phba->pport,
7537 LPFC_DISC_TRC_MBOX,
7538 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7539 (uint32_t)mbx->mbxCommand,
7540 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7541 }
7542
2e0fef85 7543 return MBX_BUSY;
dea3101e
JB
7544 }
7545
dea3101e
JB
7546 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7547
7548 /* If we are not polling, we MUST be in SLI2 mode */
7549 if (flag != MBX_POLL) {
3772a991 7550 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7551 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7552 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7553 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7554 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7555 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7556 "(%d):2531 Mailbox command x%x "
7557 "cannot issue Data: x%x x%x\n",
7558 pmbox->vport ? pmbox->vport->vpi : 0,
7559 pmbox->u.mb.mbxCommand,
7560 psli->sli_flag, flag);
58da1ffb 7561 goto out_not_finished;
dea3101e
JB
7562 }
7563 /* timeout active mbox command */
256ec0d0
JS
7564 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7565 1000);
7566 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7567 }
7568
7569 /* Mailbox cmd <cmd> issue */
ed957684 7570 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7571 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7572 "x%x\n",
e8b62011 7573 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
7574 mbx->mbxCommand,
7575 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7576 psli->sli_flag, flag);
dea3101e 7577
bf07bdea 7578 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7579 if (pmbox->vport) {
7580 lpfc_debugfs_disc_trc(pmbox->vport,
7581 LPFC_DISC_TRC_MBOX_VPORT,
7582 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7583 (uint32_t)mbx->mbxCommand,
7584 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7585 }
7586 else {
7587 lpfc_debugfs_disc_trc(phba->pport,
7588 LPFC_DISC_TRC_MBOX,
7589 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7590 (uint32_t)mbx->mbxCommand,
7591 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7592 }
7593 }
7594
dea3101e
JB
7595 psli->slistat.mbox_cmd++;
7596 evtctr = psli->slistat.mbox_event;
7597
7598 /* next set own bit for the adapter and copy over command word */
bf07bdea 7599 mbx->mbxOwner = OWN_CHIP;
dea3101e 7600
3772a991 7601 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7602 /* Populate mbox extension offset word. */
7603 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7604 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7605 = (uint8_t *)phba->mbox_ext
7606 - (uint8_t *)phba->mbox;
7607 }
7608
7609 /* Copy the mailbox extension data */
7610 if (pmbox->in_ext_byte_len && pmbox->context2) {
7611 lpfc_sli_pcimem_bcopy(pmbox->context2,
7612 (uint8_t *)phba->mbox_ext,
7613 pmbox->in_ext_byte_len);
7614 }
7615 /* Copy command data to host SLIM area */
bf07bdea 7616 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7617 } else {
7a470277
JS
7618 /* Populate mbox extension offset word. */
7619 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7620 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7621 = MAILBOX_HBA_EXT_OFFSET;
7622
7623 /* Copy the mailbox extension data */
895427bd 7624 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7625 lpfc_memcpy_to_slim(phba->MBslimaddr +
7626 MAILBOX_HBA_EXT_OFFSET,
7627 pmbox->context2, pmbox->in_ext_byte_len);
7628
895427bd 7629 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7630 /* copy command data into host mbox for cmpl */
895427bd
JS
7631 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7632 MAILBOX_CMD_SIZE);
dea3101e
JB
7633
7634 /* First copy mbox command data to HBA SLIM, skip past first
7635 word */
7636 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7637 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7638 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7639
7640 /* Next copy over first word, with mbxOwner set */
bf07bdea 7641 ldata = *((uint32_t *)mbx);
dea3101e
JB
7642 to_slim = phba->MBslimaddr;
7643 writel(ldata, to_slim);
7644 readl(to_slim); /* flush */
7645
895427bd 7646 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7647 /* switch over to host mailbox */
3772a991 7648 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7649 }
7650
7651 wmb();
dea3101e
JB
7652
7653 switch (flag) {
7654 case MBX_NOWAIT:
09372820 7655 /* Set up reference to mailbox command */
dea3101e 7656 psli->mbox_active = pmbox;
09372820
JS
7657 /* Interrupt board to do it */
7658 writel(CA_MBATT, phba->CAregaddr);
7659 readl(phba->CAregaddr); /* flush */
7660 /* Don't wait for it to finish, just return */
dea3101e
JB
7661 break;
7662
7663 case MBX_POLL:
09372820 7664 /* Set up null reference to mailbox command */
dea3101e 7665 psli->mbox_active = NULL;
09372820
JS
7666 /* Interrupt board to do it */
7667 writel(CA_MBATT, phba->CAregaddr);
7668 readl(phba->CAregaddr); /* flush */
7669
3772a991 7670 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7671 /* First read mbox status word */
34b02dcd 7672 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7673 word0 = le32_to_cpu(word0);
7674 } else {
7675 /* First read mbox status word */
9940b97b
JS
7676 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7677 spin_unlock_irqrestore(&phba->hbalock,
7678 drvr_flag);
7679 goto out_not_finished;
7680 }
dea3101e
JB
7681 }
7682
7683 /* Read the HBA Host Attention Register */
9940b97b
JS
7684 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7685 spin_unlock_irqrestore(&phba->hbalock,
7686 drvr_flag);
7687 goto out_not_finished;
7688 }
a183a15f
JS
7689 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7690 1000) + jiffies;
09372820 7691 i = 0;
dea3101e 7692 /* Wait for command to complete */
41415862
JW
7693 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7694 (!(ha_copy & HA_MBATT) &&
2e0fef85 7695 (phba->link_state > LPFC_WARM_START))) {
09372820 7696 if (time_after(jiffies, timeout)) {
dea3101e 7697 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7698 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7699 drvr_flag);
58da1ffb 7700 goto out_not_finished;
dea3101e
JB
7701 }
7702
7703 /* Check if we took a mbox interrupt while we were
7704 polling */
7705 if (((word0 & OWN_CHIP) != OWN_CHIP)
7706 && (evtctr != psli->slistat.mbox_event))
7707 break;
7708
09372820
JS
7709 if (i++ > 10) {
7710 spin_unlock_irqrestore(&phba->hbalock,
7711 drvr_flag);
7712 msleep(1);
7713 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7714 }
dea3101e 7715
3772a991 7716 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7717 /* First copy command data */
34b02dcd 7718 word0 = *((uint32_t *)phba->mbox);
dea3101e 7719 word0 = le32_to_cpu(word0);
bf07bdea 7720 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7721 MAILBOX_t *slimmb;
34b02dcd 7722 uint32_t slimword0;
dea3101e
JB
7723 /* Check real SLIM for any errors */
7724 slimword0 = readl(phba->MBslimaddr);
7725 slimmb = (MAILBOX_t *) & slimword0;
7726 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7727 && slimmb->mbxStatus) {
7728 psli->sli_flag &=
3772a991 7729 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7730 word0 = slimword0;
7731 }
7732 }
7733 } else {
7734 /* First copy command data */
7735 word0 = readl(phba->MBslimaddr);
7736 }
7737 /* Read the HBA Host Attention Register */
9940b97b
JS
7738 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7739 spin_unlock_irqrestore(&phba->hbalock,
7740 drvr_flag);
7741 goto out_not_finished;
7742 }
dea3101e
JB
7743 }
7744
3772a991 7745 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7746 /* copy results back to user */
2ea259ee
JS
7747 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7748 MAILBOX_CMD_SIZE);
7a470277
JS
7749 /* Copy the mailbox extension data */
7750 if (pmbox->out_ext_byte_len && pmbox->context2) {
7751 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7752 pmbox->context2,
7753 pmbox->out_ext_byte_len);
7754 }
dea3101e
JB
7755 } else {
7756 /* First copy command data */
bf07bdea 7757 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 7758 MAILBOX_CMD_SIZE);
7a470277
JS
7759 /* Copy the mailbox extension data */
7760 if (pmbox->out_ext_byte_len && pmbox->context2) {
7761 lpfc_memcpy_from_slim(pmbox->context2,
7762 phba->MBslimaddr +
7763 MAILBOX_HBA_EXT_OFFSET,
7764 pmbox->out_ext_byte_len);
dea3101e
JB
7765 }
7766 }
7767
7768 writel(HA_MBATT, phba->HAregaddr);
7769 readl(phba->HAregaddr); /* flush */
7770
7771 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7772 status = mbx->mbxStatus;
dea3101e
JB
7773 }
7774
2e0fef85
JS
7775 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7776 return status;
58da1ffb
JS
7777
7778out_not_finished:
7779 if (processing_queue) {
da0436e9 7780 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7781 lpfc_mbox_cmpl_put(phba, pmbox);
7782 }
7783 return MBX_NOT_FINISHED;
dea3101e
JB
7784}
7785
f1126688
JS
7786/**
7787 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7788 * @phba: Pointer to HBA context object.
7789 *
7790 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7791 * the driver internal pending mailbox queue. It will then try to wait out the
7792 * possible outstanding mailbox command before return.
7793 *
7794 * Returns:
7795 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7796 * the outstanding mailbox command timed out.
7797 **/
7798static int
7799lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7800{
7801 struct lpfc_sli *psli = &phba->sli;
f1126688 7802 int rc = 0;
a183a15f 7803 unsigned long timeout = 0;
f1126688
JS
7804
7805 /* Mark the asynchronous mailbox command posting as blocked */
7806 spin_lock_irq(&phba->hbalock);
7807 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7808 /* Determine how long we might wait for the active mailbox
7809 * command to be gracefully completed by firmware.
7810 */
a183a15f
JS
7811 if (phba->sli.mbox_active)
7812 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7813 phba->sli.mbox_active) *
7814 1000) + jiffies;
7815 spin_unlock_irq(&phba->hbalock);
7816
e8d3c3b1
JS
7817 /* Make sure the mailbox is really active */
7818 if (timeout)
7819 lpfc_sli4_process_missed_mbox_completions(phba);
7820
f1126688
JS
7821 /* Wait for the outstnading mailbox command to complete */
7822 while (phba->sli.mbox_active) {
7823 /* Check active mailbox complete status every 2ms */
7824 msleep(2);
7825 if (time_after(jiffies, timeout)) {
7826 /* Timeout, marked the outstanding cmd not complete */
7827 rc = 1;
7828 break;
7829 }
7830 }
7831
7832 /* Can not cleanly block async mailbox command, fails it */
7833 if (rc) {
7834 spin_lock_irq(&phba->hbalock);
7835 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7836 spin_unlock_irq(&phba->hbalock);
7837 }
7838 return rc;
7839}
7840
7841/**
7842 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7843 * @phba: Pointer to HBA context object.
7844 *
7845 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7846 * commands from the driver internal pending mailbox queue. It makes sure
7847 * that there is no outstanding mailbox command before resuming posting
7848 * asynchronous mailbox commands. If, for any reason, there is outstanding
7849 * mailbox command, it will try to wait it out before resuming asynchronous
7850 * mailbox command posting.
7851 **/
7852static void
7853lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7854{
7855 struct lpfc_sli *psli = &phba->sli;
7856
7857 spin_lock_irq(&phba->hbalock);
7858 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7859 /* Asynchronous mailbox posting is not blocked, do nothing */
7860 spin_unlock_irq(&phba->hbalock);
7861 return;
7862 }
7863
7864 /* Outstanding synchronous mailbox command is guaranteed to be done,
7865 * successful or timeout, after timing-out the outstanding mailbox
7866 * command shall always be removed, so just unblock posting async
7867 * mailbox command and resume
7868 */
7869 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7870 spin_unlock_irq(&phba->hbalock);
7871
7872 /* wake up worker thread to post asynchronlous mailbox command */
7873 lpfc_worker_wake_up(phba);
7874}
7875
2d843edc
JS
7876/**
7877 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7878 * @phba: Pointer to HBA context object.
7879 * @mboxq: Pointer to mailbox object.
7880 *
7881 * The function waits for the bootstrap mailbox register ready bit from
7882 * port for twice the regular mailbox command timeout value.
7883 *
7884 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7885 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7886 **/
7887static int
7888lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7889{
7890 uint32_t db_ready;
7891 unsigned long timeout;
7892 struct lpfc_register bmbx_reg;
7893
7894 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7895 * 1000) + jiffies;
7896
7897 do {
7898 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7899 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7900 if (!db_ready)
7901 msleep(2);
7902
7903 if (time_after(jiffies, timeout))
7904 return MBXERR_ERROR;
7905 } while (!db_ready);
7906
7907 return 0;
7908}
7909
da0436e9
JS
7910/**
7911 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7912 * @phba: Pointer to HBA context object.
7913 * @mboxq: Pointer to mailbox object.
7914 *
7915 * The function posts a mailbox to the port. The mailbox is expected
7916 * to be comletely filled in and ready for the port to operate on it.
7917 * This routine executes a synchronous completion operation on the
7918 * mailbox by polling for its completion.
7919 *
7920 * The caller must not be holding any locks when calling this routine.
7921 *
7922 * Returns:
7923 * MBX_SUCCESS - mailbox posted successfully
7924 * Any of the MBX error values.
7925 **/
7926static int
7927lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7928{
7929 int rc = MBX_SUCCESS;
7930 unsigned long iflag;
da0436e9
JS
7931 uint32_t mcqe_status;
7932 uint32_t mbx_cmnd;
da0436e9
JS
7933 struct lpfc_sli *psli = &phba->sli;
7934 struct lpfc_mqe *mb = &mboxq->u.mqe;
7935 struct lpfc_bmbx_create *mbox_rgn;
7936 struct dma_address *dma_address;
da0436e9
JS
7937
7938 /*
7939 * Only one mailbox can be active to the bootstrap mailbox region
7940 * at a time and there is no queueing provided.
7941 */
7942 spin_lock_irqsave(&phba->hbalock, iflag);
7943 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7944 spin_unlock_irqrestore(&phba->hbalock, iflag);
7945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7946 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7947 "cannot issue Data: x%x x%x\n",
7948 mboxq->vport ? mboxq->vport->vpi : 0,
7949 mboxq->u.mb.mbxCommand,
a183a15f
JS
7950 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7951 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7952 psli->sli_flag, MBX_POLL);
7953 return MBXERR_ERROR;
7954 }
7955 /* The server grabs the token and owns it until release */
7956 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7957 phba->sli.mbox_active = mboxq;
7958 spin_unlock_irqrestore(&phba->hbalock, iflag);
7959
2d843edc
JS
7960 /* wait for bootstrap mbox register for readyness */
7961 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7962 if (rc)
7963 goto exit;
7964
da0436e9
JS
7965 /*
7966 * Initialize the bootstrap memory region to avoid stale data areas
7967 * in the mailbox post. Then copy the caller's mailbox contents to
7968 * the bmbx mailbox region.
7969 */
7970 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7971 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7972 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7973 sizeof(struct lpfc_mqe));
7974
7975 /* Post the high mailbox dma address to the port and wait for ready. */
7976 dma_address = &phba->sli4_hba.bmbx.dma_address;
7977 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7978
2d843edc
JS
7979 /* wait for bootstrap mbox register for hi-address write done */
7980 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7981 if (rc)
7982 goto exit;
da0436e9
JS
7983
7984 /* Post the low mailbox dma address to the port. */
7985 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7986
2d843edc
JS
7987 /* wait for bootstrap mbox register for low address write done */
7988 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7989 if (rc)
7990 goto exit;
da0436e9
JS
7991
7992 /*
7993 * Read the CQ to ensure the mailbox has completed.
7994 * If so, update the mailbox status so that the upper layers
7995 * can complete the request normally.
7996 */
7997 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7998 sizeof(struct lpfc_mqe));
7999 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8000 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8001 sizeof(struct lpfc_mcqe));
8002 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
8003 /*
8004 * When the CQE status indicates a failure and the mailbox status
8005 * indicates success then copy the CQE status into the mailbox status
8006 * (and prefix it with x4000).
8007 */
da0436e9 8008 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8009 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8010 bf_set(lpfc_mqe_status, mb,
8011 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8012 rc = MBXERR_ERROR;
d7c47992
JS
8013 } else
8014 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8015
8016 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8017 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8018 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8019 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8020 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8021 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8022 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8023 bf_get(lpfc_mqe_status, mb),
8024 mb->un.mb_words[0], mb->un.mb_words[1],
8025 mb->un.mb_words[2], mb->un.mb_words[3],
8026 mb->un.mb_words[4], mb->un.mb_words[5],
8027 mb->un.mb_words[6], mb->un.mb_words[7],
8028 mb->un.mb_words[8], mb->un.mb_words[9],
8029 mb->un.mb_words[10], mb->un.mb_words[11],
8030 mb->un.mb_words[12], mboxq->mcqe.word0,
8031 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8032 mboxq->mcqe.trailer);
8033exit:
8034 /* We are holding the token, no needed for lock when release */
8035 spin_lock_irqsave(&phba->hbalock, iflag);
8036 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8037 phba->sli.mbox_active = NULL;
8038 spin_unlock_irqrestore(&phba->hbalock, iflag);
8039 return rc;
8040}
8041
8042/**
8043 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8044 * @phba: Pointer to HBA context object.
8045 * @pmbox: Pointer to mailbox object.
8046 * @flag: Flag indicating how the mailbox need to be processed.
8047 *
8048 * This function is called by discovery code and HBA management code to submit
8049 * a mailbox command to firmware with SLI-4 interface spec.
8050 *
8051 * Return codes the caller owns the mailbox command after the return of the
8052 * function.
8053 **/
8054static int
8055lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8056 uint32_t flag)
8057{
8058 struct lpfc_sli *psli = &phba->sli;
8059 unsigned long iflags;
8060 int rc;
8061
b76f2dc9
JS
8062 /* dump from issue mailbox command if setup */
8063 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8064
8fa38513
JS
8065 rc = lpfc_mbox_dev_check(phba);
8066 if (unlikely(rc)) {
8067 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8068 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8069 "cannot issue Data: x%x x%x\n",
8070 mboxq->vport ? mboxq->vport->vpi : 0,
8071 mboxq->u.mb.mbxCommand,
a183a15f
JS
8072 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8073 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8074 psli->sli_flag, flag);
8075 goto out_not_finished;
8076 }
8077
da0436e9
JS
8078 /* Detect polling mode and jump to a handler */
8079 if (!phba->sli4_hba.intr_enable) {
8080 if (flag == MBX_POLL)
8081 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8082 else
8083 rc = -EIO;
8084 if (rc != MBX_SUCCESS)
0558056c 8085 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8086 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8087 "(x%x/x%x) failure: "
8088 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8089 "Data: x%x x%x\n,",
da0436e9
JS
8090 mboxq->vport ? mboxq->vport->vpi : 0,
8091 mboxq->u.mb.mbxCommand,
a183a15f
JS
8092 lpfc_sli_config_mbox_subsys_get(phba,
8093 mboxq),
8094 lpfc_sli_config_mbox_opcode_get(phba,
8095 mboxq),
cc459f19
JS
8096 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8097 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8098 bf_get(lpfc_mcqe_ext_status,
8099 &mboxq->mcqe),
da0436e9
JS
8100 psli->sli_flag, flag);
8101 return rc;
8102 } else if (flag == MBX_POLL) {
f1126688
JS
8103 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8104 "(%d):2542 Try to issue mailbox command "
a183a15f 8105 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 8106 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8107 mboxq->vport ? mboxq->vport->vpi : 0,
8108 mboxq->u.mb.mbxCommand,
a183a15f
JS
8109 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8110 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8111 psli->sli_flag, flag);
f1126688
JS
8112 /* Try to block the asynchronous mailbox posting */
8113 rc = lpfc_sli4_async_mbox_block(phba);
8114 if (!rc) {
8115 /* Successfully blocked, now issue sync mbox cmd */
8116 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8117 if (rc != MBX_SUCCESS)
cc459f19 8118 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8119 LOG_MBOX | LOG_SLI,
cc459f19
JS
8120 "(%d):2597 Sync Mailbox command "
8121 "x%x (x%x/x%x) failure: "
8122 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8123 "Data: x%x x%x\n,",
8124 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8125 mboxq->u.mb.mbxCommand,
8126 lpfc_sli_config_mbox_subsys_get(phba,
8127 mboxq),
8128 lpfc_sli_config_mbox_opcode_get(phba,
8129 mboxq),
cc459f19
JS
8130 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8131 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8132 bf_get(lpfc_mcqe_ext_status,
8133 &mboxq->mcqe),
a183a15f 8134 psli->sli_flag, flag);
f1126688
JS
8135 /* Unblock the async mailbox posting afterward */
8136 lpfc_sli4_async_mbox_unblock(phba);
8137 }
8138 return rc;
da0436e9
JS
8139 }
8140
8141 /* Now, interrupt mode asynchrous mailbox command */
8142 rc = lpfc_mbox_cmd_check(phba, mboxq);
8143 if (rc) {
8144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8145 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8146 "cannot issue Data: x%x x%x\n",
8147 mboxq->vport ? mboxq->vport->vpi : 0,
8148 mboxq->u.mb.mbxCommand,
a183a15f
JS
8149 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8150 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8151 psli->sli_flag, flag);
8152 goto out_not_finished;
8153 }
da0436e9
JS
8154
8155 /* Put the mailbox command to the driver internal FIFO */
8156 psli->slistat.mbox_busy++;
8157 spin_lock_irqsave(&phba->hbalock, iflags);
8158 lpfc_mbox_put(phba, mboxq);
8159 spin_unlock_irqrestore(&phba->hbalock, iflags);
8160 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8161 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8162 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8163 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8164 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8165 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8166 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8167 phba->pport->port_state,
8168 psli->sli_flag, MBX_NOWAIT);
8169 /* Wake up worker thread to transport mailbox command from head */
8170 lpfc_worker_wake_up(phba);
8171
8172 return MBX_BUSY;
8173
8174out_not_finished:
8175 return MBX_NOT_FINISHED;
8176}
8177
8178/**
8179 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8180 * @phba: Pointer to HBA context object.
8181 *
8182 * This function is called by worker thread to send a mailbox command to
8183 * SLI4 HBA firmware.
8184 *
8185 **/
8186int
8187lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8188{
8189 struct lpfc_sli *psli = &phba->sli;
8190 LPFC_MBOXQ_t *mboxq;
8191 int rc = MBX_SUCCESS;
8192 unsigned long iflags;
8193 struct lpfc_mqe *mqe;
8194 uint32_t mbx_cmnd;
8195
8196 /* Check interrupt mode before post async mailbox command */
8197 if (unlikely(!phba->sli4_hba.intr_enable))
8198 return MBX_NOT_FINISHED;
8199
8200 /* Check for mailbox command service token */
8201 spin_lock_irqsave(&phba->hbalock, iflags);
8202 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8203 spin_unlock_irqrestore(&phba->hbalock, iflags);
8204 return MBX_NOT_FINISHED;
8205 }
8206 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8207 spin_unlock_irqrestore(&phba->hbalock, iflags);
8208 return MBX_NOT_FINISHED;
8209 }
8210 if (unlikely(phba->sli.mbox_active)) {
8211 spin_unlock_irqrestore(&phba->hbalock, iflags);
8212 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8213 "0384 There is pending active mailbox cmd\n");
8214 return MBX_NOT_FINISHED;
8215 }
8216 /* Take the mailbox command service token */
8217 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8218
8219 /* Get the next mailbox command from head of queue */
8220 mboxq = lpfc_mbox_get(phba);
8221
8222 /* If no more mailbox command waiting for post, we're done */
8223 if (!mboxq) {
8224 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8225 spin_unlock_irqrestore(&phba->hbalock, iflags);
8226 return MBX_SUCCESS;
8227 }
8228 phba->sli.mbox_active = mboxq;
8229 spin_unlock_irqrestore(&phba->hbalock, iflags);
8230
8231 /* Check device readiness for posting mailbox command */
8232 rc = lpfc_mbox_dev_check(phba);
8233 if (unlikely(rc))
8234 /* Driver clean routine will clean up pending mailbox */
8235 goto out_not_finished;
8236
8237 /* Prepare the mbox command to be posted */
8238 mqe = &mboxq->u.mqe;
8239 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8240
8241 /* Start timer for the mbox_tmo and log some mailbox post messages */
8242 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8243 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8244
8245 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8246 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8247 "x%x x%x\n",
8248 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8249 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8250 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8251 phba->pport->port_state, psli->sli_flag);
8252
8253 if (mbx_cmnd != MBX_HEARTBEAT) {
8254 if (mboxq->vport) {
8255 lpfc_debugfs_disc_trc(mboxq->vport,
8256 LPFC_DISC_TRC_MBOX_VPORT,
8257 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8258 mbx_cmnd, mqe->un.mb_words[0],
8259 mqe->un.mb_words[1]);
8260 } else {
8261 lpfc_debugfs_disc_trc(phba->pport,
8262 LPFC_DISC_TRC_MBOX,
8263 "MBOX Send: cmd:x%x mb:x%x x%x",
8264 mbx_cmnd, mqe->un.mb_words[0],
8265 mqe->un.mb_words[1]);
8266 }
8267 }
8268 psli->slistat.mbox_cmd++;
8269
8270 /* Post the mailbox command to the port */
8271 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8272 if (rc != MBX_SUCCESS) {
8273 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8274 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8275 "cannot issue Data: x%x x%x\n",
8276 mboxq->vport ? mboxq->vport->vpi : 0,
8277 mboxq->u.mb.mbxCommand,
a183a15f
JS
8278 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8279 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8280 psli->sli_flag, MBX_NOWAIT);
8281 goto out_not_finished;
8282 }
8283
8284 return rc;
8285
8286out_not_finished:
8287 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8288 if (phba->sli.mbox_active) {
8289 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8290 __lpfc_mbox_cmpl_put(phba, mboxq);
8291 /* Release the token */
8292 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8293 phba->sli.mbox_active = NULL;
8294 }
da0436e9
JS
8295 spin_unlock_irqrestore(&phba->hbalock, iflags);
8296
8297 return MBX_NOT_FINISHED;
8298}
8299
8300/**
8301 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8302 * @phba: Pointer to HBA context object.
8303 * @pmbox: Pointer to mailbox object.
8304 * @flag: Flag indicating how the mailbox need to be processed.
8305 *
8306 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8307 * the API jump table function pointer from the lpfc_hba struct.
8308 *
8309 * Return codes the caller owns the mailbox command after the return of the
8310 * function.
8311 **/
8312int
8313lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8314{
8315 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8316}
8317
8318/**
25985edc 8319 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8320 * @phba: The hba struct for which this call is being executed.
8321 * @dev_grp: The HBA PCI-Device group number.
8322 *
8323 * This routine sets up the mbox interface API function jump table in @phba
8324 * struct.
8325 * Returns: 0 - success, -ENODEV - failure.
8326 **/
8327int
8328lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8329{
8330
8331 switch (dev_grp) {
8332 case LPFC_PCI_DEV_LP:
8333 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8334 phba->lpfc_sli_handle_slow_ring_event =
8335 lpfc_sli_handle_slow_ring_event_s3;
8336 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8337 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8338 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8339 break;
8340 case LPFC_PCI_DEV_OC:
8341 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8342 phba->lpfc_sli_handle_slow_ring_event =
8343 lpfc_sli_handle_slow_ring_event_s4;
8344 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8345 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8346 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8347 break;
8348 default:
8349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8350 "1420 Invalid HBA PCI-device group: 0x%x\n",
8351 dev_grp);
8352 return -ENODEV;
8353 break;
8354 }
8355 return 0;
8356}
8357
e59058c4 8358/**
3621a710 8359 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8360 * @phba: Pointer to HBA context object.
8361 * @pring: Pointer to driver SLI ring object.
8362 * @piocb: Pointer to address of newly added command iocb.
8363 *
8364 * This function is called with hbalock held to add a command
8365 * iocb to the txq when SLI layer cannot submit the command iocb
8366 * to the ring.
8367 **/
2a9bf3d0 8368void
92d7f7b0 8369__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8370 struct lpfc_iocbq *piocb)
dea3101e 8371{
1c2ba475 8372 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8373 /* Insert the caller's iocb in the txq tail for later processing. */
8374 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8375}
8376
e59058c4 8377/**
3621a710 8378 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8379 * @phba: Pointer to HBA context object.
8380 * @pring: Pointer to driver SLI ring object.
8381 * @piocb: Pointer to address of newly added command iocb.
8382 *
8383 * This function is called with hbalock held before a new
8384 * iocb is submitted to the firmware. This function checks
8385 * txq to flush the iocbs in txq to Firmware before
8386 * submitting new iocbs to the Firmware.
8387 * If there are iocbs in the txq which need to be submitted
8388 * to firmware, lpfc_sli_next_iocb returns the first element
8389 * of the txq after dequeuing it from txq.
8390 * If there is no iocb in the txq then the function will return
8391 * *piocb and *piocb is set to NULL. Caller needs to check
8392 * *piocb to find if there are more commands in the txq.
8393 **/
dea3101e
JB
8394static struct lpfc_iocbq *
8395lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8396 struct lpfc_iocbq **piocb)
dea3101e
JB
8397{
8398 struct lpfc_iocbq * nextiocb;
8399
1c2ba475
JT
8400 lockdep_assert_held(&phba->hbalock);
8401
dea3101e
JB
8402 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8403 if (!nextiocb) {
8404 nextiocb = *piocb;
8405 *piocb = NULL;
8406 }
8407
8408 return nextiocb;
8409}
8410
e59058c4 8411/**
3772a991 8412 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8413 * @phba: Pointer to HBA context object.
3772a991 8414 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8415 * @piocb: Pointer to command iocb.
8416 * @flag: Flag indicating if this command can be put into txq.
8417 *
3772a991
JS
8418 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8419 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8420 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8421 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8422 * this function allows only iocbs for posting buffers. This function finds
8423 * next available slot in the command ring and posts the command to the
8424 * available slot and writes the port attention register to request HBA start
8425 * processing new iocb. If there is no slot available in the ring and
8426 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8427 * the function returns IOCB_BUSY.
e59058c4 8428 *
3772a991
JS
8429 * This function is called with hbalock held. The function will return success
8430 * after it successfully submit the iocb to firmware or after adding to the
8431 * txq.
e59058c4 8432 **/
98c9ea5c 8433static int
3772a991 8434__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8435 struct lpfc_iocbq *piocb, uint32_t flag)
8436{
8437 struct lpfc_iocbq *nextiocb;
8438 IOCB_t *iocb;
895427bd 8439 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8440
1c2ba475
JT
8441 lockdep_assert_held(&phba->hbalock);
8442
92d7f7b0
JS
8443 if (piocb->iocb_cmpl && (!piocb->vport) &&
8444 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8445 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8446 lpfc_printf_log(phba, KERN_ERR,
8447 LOG_SLI | LOG_VPORT,
e8b62011 8448 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8449 piocb->iocb.ulpCommand);
8450 dump_stack();
8451 return IOCB_ERROR;
8452 }
8453
8454
8d63f375
LV
8455 /* If the PCI channel is in offline state, do not post iocbs. */
8456 if (unlikely(pci_channel_offline(phba->pcidev)))
8457 return IOCB_ERROR;
8458
a257bf90
JS
8459 /* If HBA has a deferred error attention, fail the iocb. */
8460 if (unlikely(phba->hba_flag & DEFER_ERATT))
8461 return IOCB_ERROR;
8462
dea3101e
JB
8463 /*
8464 * We should never get an IOCB if we are in a < LINK_DOWN state
8465 */
2e0fef85 8466 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8467 return IOCB_ERROR;
8468
8469 /*
8470 * Check to see if we are blocking IOCB processing because of a
0b727fea 8471 * outstanding event.
dea3101e 8472 */
0b727fea 8473 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8474 goto iocb_busy;
8475
2e0fef85 8476 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8477 /*
2680eeaa 8478 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8479 * can be issued if the link is not up.
8480 */
8481 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8482 case CMD_GEN_REQUEST64_CR:
8483 case CMD_GEN_REQUEST64_CX:
8484 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8485 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8486 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8487 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8488 MENLO_TRANSPORT_TYPE))
8489
8490 goto iocb_busy;
8491 break;
dea3101e
JB
8492 case CMD_QUE_RING_BUF_CN:
8493 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8494 /*
8495 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8496 * completion, iocb_cmpl MUST be 0.
8497 */
8498 if (piocb->iocb_cmpl)
8499 piocb->iocb_cmpl = NULL;
8500 /*FALLTHROUGH*/
8501 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8502 case CMD_CLOSE_XRI_CN:
8503 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8504 break;
8505 default:
8506 goto iocb_busy;
8507 }
8508
8509 /*
8510 * For FCP commands, we must be in a state where we can process link
8511 * attention events.
8512 */
895427bd 8513 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8514 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8515 goto iocb_busy;
92d7f7b0 8516 }
dea3101e 8517
dea3101e
JB
8518 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8519 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8520 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8521
8522 if (iocb)
8523 lpfc_sli_update_ring(phba, pring);
8524 else
8525 lpfc_sli_update_full_ring(phba, pring);
8526
8527 if (!piocb)
8528 return IOCB_SUCCESS;
8529
8530 goto out_busy;
8531
8532 iocb_busy:
8533 pring->stats.iocb_cmd_delay++;
8534
8535 out_busy:
8536
8537 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8538 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8539 return IOCB_SUCCESS;
8540 }
8541
8542 return IOCB_BUSY;
8543}
8544
3772a991 8545/**
4f774513
JS
8546 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8547 * @phba: Pointer to HBA context object.
8548 * @piocb: Pointer to command iocb.
8549 * @sglq: Pointer to the scatter gather queue object.
8550 *
8551 * This routine converts the bpl or bde that is in the IOCB
8552 * to a sgl list for the sli4 hardware. The physical address
8553 * of the bpl/bde is converted back to a virtual address.
8554 * If the IOCB contains a BPL then the list of BDE's is
8555 * converted to sli4_sge's. If the IOCB contains a single
8556 * BDE then it is converted to a single sli_sge.
8557 * The IOCB is still in cpu endianess so the contents of
8558 * the bpl can be used without byte swapping.
8559 *
8560 * Returns valid XRI = Success, NO_XRI = Failure.
8561**/
8562static uint16_t
8563lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8564 struct lpfc_sglq *sglq)
3772a991 8565{
4f774513
JS
8566 uint16_t xritag = NO_XRI;
8567 struct ulp_bde64 *bpl = NULL;
8568 struct ulp_bde64 bde;
8569 struct sli4_sge *sgl = NULL;
1b51197d 8570 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8571 IOCB_t *icmd;
8572 int numBdes = 0;
8573 int i = 0;
63e801ce
JS
8574 uint32_t offset = 0; /* accumulated offset in the sg request list */
8575 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8576
4f774513
JS
8577 if (!piocbq || !sglq)
8578 return xritag;
8579
8580 sgl = (struct sli4_sge *)sglq->sgl;
8581 icmd = &piocbq->iocb;
6b5151fd
JS
8582 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8583 return sglq->sli4_xritag;
4f774513
JS
8584 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8585 numBdes = icmd->un.genreq64.bdl.bdeSize /
8586 sizeof(struct ulp_bde64);
8587 /* The addrHigh and addrLow fields within the IOCB
8588 * have not been byteswapped yet so there is no
8589 * need to swap them back.
8590 */
1b51197d
JS
8591 if (piocbq->context3)
8592 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8593 else
8594 return xritag;
4f774513 8595
1b51197d 8596 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8597 if (!bpl)
8598 return xritag;
8599
8600 for (i = 0; i < numBdes; i++) {
8601 /* Should already be byte swapped. */
28baac74
JS
8602 sgl->addr_hi = bpl->addrHigh;
8603 sgl->addr_lo = bpl->addrLow;
8604
0558056c 8605 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8606 if ((i+1) == numBdes)
8607 bf_set(lpfc_sli4_sge_last, sgl, 1);
8608 else
8609 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8610 /* swap the size field back to the cpu so we
8611 * can assign it to the sgl.
8612 */
8613 bde.tus.w = le32_to_cpu(bpl->tus.w);
8614 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8615 /* The offsets in the sgl need to be accumulated
8616 * separately for the request and reply lists.
8617 * The request is always first, the reply follows.
8618 */
8619 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8620 /* add up the reply sg entries */
8621 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8622 inbound++;
8623 /* first inbound? reset the offset */
8624 if (inbound == 1)
8625 offset = 0;
8626 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8627 bf_set(lpfc_sli4_sge_type, sgl,
8628 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8629 offset += bde.tus.f.bdeSize;
8630 }
546fc854 8631 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8632 bpl++;
8633 sgl++;
8634 }
8635 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8636 /* The addrHigh and addrLow fields of the BDE have not
8637 * been byteswapped yet so they need to be swapped
8638 * before putting them in the sgl.
8639 */
8640 sgl->addr_hi =
8641 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8642 sgl->addr_lo =
8643 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8644 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8645 bf_set(lpfc_sli4_sge_last, sgl, 1);
8646 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8647 sgl->sge_len =
8648 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8649 }
8650 return sglq->sli4_xritag;
3772a991 8651}
92d7f7b0 8652
e59058c4 8653/**
4f774513 8654 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8655 * @phba: Pointer to HBA context object.
4f774513
JS
8656 * @piocb: Pointer to command iocb.
8657 * @wqe: Pointer to the work queue entry.
e59058c4 8658 *
4f774513
JS
8659 * This routine converts the iocb command to its Work Queue Entry
8660 * equivalent. The wqe pointer should not have any fields set when
8661 * this routine is called because it will memcpy over them.
8662 * This routine does not set the CQ_ID or the WQEC bits in the
8663 * wqe.
e59058c4 8664 *
4f774513 8665 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8666 **/
cf5bf97e 8667static int
4f774513
JS
8668lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8669 union lpfc_wqe *wqe)
cf5bf97e 8670{
5ffc266e 8671 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8672 uint8_t ct = 0;
8673 uint32_t fip;
8674 uint32_t abort_tag;
8675 uint8_t command_type = ELS_COMMAND_NON_FIP;
8676 uint8_t cmnd;
8677 uint16_t xritag;
dcf2a4e0
JS
8678 uint16_t abrt_iotag;
8679 struct lpfc_iocbq *abrtiocbq;
4f774513 8680 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8681 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8682 int numBdes, i;
8683 struct ulp_bde64 bde;
c31098ce 8684 struct lpfc_nodelist *ndlp;
ff78d8f9 8685 uint32_t *pcmd;
1b51197d 8686 uint32_t if_type;
4f774513 8687
45ed1190 8688 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8689 /* The fcp commands will set command type */
0c287589 8690 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8691 command_type = FCP_COMMAND;
c868595d 8692 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8693 command_type = ELS_COMMAND_FIP;
8694 else
8695 command_type = ELS_COMMAND_NON_FIP;
8696
b5c53958
JS
8697 if (phba->fcp_embed_io)
8698 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8699 /* Some of the fields are in the right position already */
8700 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
8701 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8702 /* The ct field has moved so reset */
8703 wqe->generic.wqe_com.word7 = 0;
8704 wqe->generic.wqe_com.word10 = 0;
8705 }
b5c53958
JS
8706
8707 abort_tag = (uint32_t) iocbq->iotag;
8708 xritag = iocbq->sli4_xritag;
4f774513
JS
8709 /* words0-2 bpl convert bde */
8710 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8711 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8712 sizeof(struct ulp_bde64);
4f774513
JS
8713 bpl = (struct ulp_bde64 *)
8714 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8715 if (!bpl)
8716 return IOCB_ERROR;
cf5bf97e 8717
4f774513
JS
8718 /* Should already be byte swapped. */
8719 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8720 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8721 /* swap the size field back to the cpu so we
8722 * can assign it to the sgl.
8723 */
8724 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8725 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8726 total_len = 0;
8727 for (i = 0; i < numBdes; i++) {
8728 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8729 total_len += bde.tus.f.bdeSize;
8730 }
4f774513 8731 } else
5ffc266e 8732 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8733
4f774513
JS
8734 iocbq->iocb.ulpIoTag = iocbq->iotag;
8735 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8736
4f774513
JS
8737 switch (iocbq->iocb.ulpCommand) {
8738 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8739 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8740 ndlp = iocbq->context_un.ndlp;
8741 else
8742 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8743 if (!iocbq->iocb.ulpLe) {
8744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8745 "2007 Only Limited Edition cmd Format"
8746 " supported 0x%x\n",
8747 iocbq->iocb.ulpCommand);
8748 return IOCB_ERROR;
8749 }
ff78d8f9 8750
5ffc266e 8751 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8752 /* Els_reguest64 has a TMO */
8753 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8754 iocbq->iocb.ulpTimeout);
8755 /* Need a VF for word 4 set the vf bit*/
8756 bf_set(els_req64_vf, &wqe->els_req, 0);
8757 /* And a VFID for word 12 */
8758 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8759 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8760 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8761 iocbq->iocb.ulpContext);
8762 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8763 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8764 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8765 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8766 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8767 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8768 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8769 iocbq->context2)->virt);
1b51197d
JS
8770 if_type = bf_get(lpfc_sli_intf_if_type,
8771 &phba->sli4_hba.sli_intf);
8772 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8773 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8774 *pcmd == ELS_CMD_SCR ||
6b5151fd 8775 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8776 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8777 *pcmd == ELS_CMD_PLOGI)) {
8778 bf_set(els_req64_sp, &wqe->els_req, 1);
8779 bf_set(els_req64_sid, &wqe->els_req,
8780 iocbq->vport->fc_myDID);
939723a4
JS
8781 if ((*pcmd == ELS_CMD_FLOGI) &&
8782 !(phba->fc_topology ==
8783 LPFC_TOPOLOGY_LOOP))
8784 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8785 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8786 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8787 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8788 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8789 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8790 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8791 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8792 }
c868595d 8793 }
6d368e53
JS
8794 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8795 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8796 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8797 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8798 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8799 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8800 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8801 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8802 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8803 break;
5ffc266e 8804 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8805 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8806 iocbq->iocb.un.ulpWord[3]);
8807 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8808 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8809 /* The entire sequence is transmitted for this IOCB */
8810 xmit_len = total_len;
8811 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8812 if (phba->link_flag & LS_LOOPBACK_MODE)
8813 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8814 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8815 /* word3 iocb=io_tag32 wqe=reserved */
8816 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8817 /* word4 relative_offset memcpy */
8818 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8819 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8820 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8821 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8822 LPFC_WQE_IOD_WRITE);
8823 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8824 LPFC_WQE_LENLOC_WORD12);
8825 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8826 wqe->xmit_sequence.xmit_len = xmit_len;
8827 command_type = OTHER_COMMAND;
7851fe2c 8828 break;
4f774513 8829 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8830 /* word3 iocb=iotag32 wqe=seq_payload_len */
8831 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8832 /* word4 iocb=rsvd wqe=rsvd */
8833 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8834 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8835 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8836 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8837 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8838 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8839 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8840 LPFC_WQE_LENLOC_WORD3);
8841 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8842 break;
4f774513
JS
8843 case CMD_FCP_IWRITE64_CR:
8844 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8845 /* word3 iocb=iotag wqe=payload_offset_len */
8846 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8847 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8848 xmit_len + sizeof(struct fcp_rsp));
8849 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8850 0);
f0d9bccc
JS
8851 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8852 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8853 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8854 iocbq->iocb.ulpFCP2Rcvy);
8855 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8856 /* Always open the exchange */
f0d9bccc
JS
8857 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8858 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8859 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8860 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8861 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
8862 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8863 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
8864 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8865 if (iocbq->priority) {
8866 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8867 (iocbq->priority << 1));
8868 } else {
1ba981fd
JS
8869 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8870 (phba->cfg_XLanePriority << 1));
8871 }
8872 }
b5c53958
JS
8873 /* Note, word 10 is already initialized to 0 */
8874
8875 if (phba->fcp_embed_io) {
8876 struct lpfc_scsi_buf *lpfc_cmd;
8877 struct sli4_sge *sgl;
8878 union lpfc_wqe128 *wqe128;
8879 struct fcp_cmnd *fcp_cmnd;
8880 uint32_t *ptr;
8881
8882 /* 128 byte wqe support here */
8883 wqe128 = (union lpfc_wqe128 *)wqe;
8884
8885 lpfc_cmd = iocbq->context1;
8886 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8887 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8888
8889 /* Word 0-2 - FCP_CMND */
8890 wqe128->generic.bde.tus.f.bdeFlags =
8891 BUFF_TYPE_BDE_IMMED;
8892 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8893 wqe128->generic.bde.addrHigh = 0;
8894 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8895
8896 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8897
8898 /* Word 22-29 FCP CMND Payload */
8899 ptr = &wqe128->words[22];
8900 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8901 }
7851fe2c 8902 break;
4f774513 8903 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8904 /* word3 iocb=iotag wqe=payload_offset_len */
8905 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8906 bf_set(payload_offset_len, &wqe->fcp_iread,
8907 xmit_len + sizeof(struct fcp_rsp));
8908 bf_set(cmd_buff_len, &wqe->fcp_iread,
8909 0);
f0d9bccc
JS
8910 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8911 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8912 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8913 iocbq->iocb.ulpFCP2Rcvy);
8914 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 8915 /* Always open the exchange */
f0d9bccc
JS
8916 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8917 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8918 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8919 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8920 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
8921 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8922 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
8923 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8924 if (iocbq->priority) {
8925 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8926 (iocbq->priority << 1));
8927 } else {
1ba981fd
JS
8928 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8929 (phba->cfg_XLanePriority << 1));
8930 }
8931 }
b5c53958
JS
8932 /* Note, word 10 is already initialized to 0 */
8933
8934 if (phba->fcp_embed_io) {
8935 struct lpfc_scsi_buf *lpfc_cmd;
8936 struct sli4_sge *sgl;
8937 union lpfc_wqe128 *wqe128;
8938 struct fcp_cmnd *fcp_cmnd;
8939 uint32_t *ptr;
8940
8941 /* 128 byte wqe support here */
8942 wqe128 = (union lpfc_wqe128 *)wqe;
8943
8944 lpfc_cmd = iocbq->context1;
8945 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8946 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8947
8948 /* Word 0-2 - FCP_CMND */
8949 wqe128->generic.bde.tus.f.bdeFlags =
8950 BUFF_TYPE_BDE_IMMED;
8951 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8952 wqe128->generic.bde.addrHigh = 0;
8953 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8954
8955 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8956
8957 /* Word 22-29 FCP CMND Payload */
8958 ptr = &wqe128->words[22];
8959 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8960 }
7851fe2c 8961 break;
4f774513 8962 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
8963 /* word3 iocb=iotag wqe=payload_offset_len */
8964 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8965 bf_set(payload_offset_len, &wqe->fcp_icmd,
8966 xmit_len + sizeof(struct fcp_rsp));
8967 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8968 0);
f0d9bccc 8969 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 8970 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8971 /* Always open the exchange */
f0d9bccc
JS
8972 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8973 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8974 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8975 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8976 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
8977 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8978 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
8979 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8980 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
8981 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8982 if (iocbq->priority) {
8983 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8984 (iocbq->priority << 1));
8985 } else {
1ba981fd
JS
8986 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8987 (phba->cfg_XLanePriority << 1));
8988 }
8989 }
b5c53958
JS
8990 /* Note, word 10 is already initialized to 0 */
8991
8992 if (phba->fcp_embed_io) {
8993 struct lpfc_scsi_buf *lpfc_cmd;
8994 struct sli4_sge *sgl;
8995 union lpfc_wqe128 *wqe128;
8996 struct fcp_cmnd *fcp_cmnd;
8997 uint32_t *ptr;
8998
8999 /* 128 byte wqe support here */
9000 wqe128 = (union lpfc_wqe128 *)wqe;
9001
9002 lpfc_cmd = iocbq->context1;
9003 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9004 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9005
9006 /* Word 0-2 - FCP_CMND */
9007 wqe128->generic.bde.tus.f.bdeFlags =
9008 BUFF_TYPE_BDE_IMMED;
9009 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9010 wqe128->generic.bde.addrHigh = 0;
9011 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9012
9013 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9014
9015 /* Word 22-29 FCP CMND Payload */
9016 ptr = &wqe128->words[22];
9017 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9018 }
7851fe2c 9019 break;
4f774513 9020 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9021 /* For this command calculate the xmit length of the
9022 * request bde.
9023 */
9024 xmit_len = 0;
9025 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9026 sizeof(struct ulp_bde64);
9027 for (i = 0; i < numBdes; i++) {
63e801ce 9028 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9029 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9030 break;
63e801ce
JS
9031 xmit_len += bde.tus.f.bdeSize;
9032 }
f0d9bccc
JS
9033 /* word3 iocb=IO_TAG wqe=request_payload_len */
9034 wqe->gen_req.request_payload_len = xmit_len;
9035 /* word4 iocb=parameter wqe=relative_offset memcpy */
9036 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9037 /* word6 context tag copied in memcpy */
9038 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9039 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9041 "2015 Invalid CT %x command 0x%x\n",
9042 ct, iocbq->iocb.ulpCommand);
9043 return IOCB_ERROR;
9044 }
f0d9bccc
JS
9045 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9046 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9047 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9048 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9049 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9050 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9051 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9052 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9053 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9054 command_type = OTHER_COMMAND;
7851fe2c 9055 break;
4f774513 9056 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9057 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9058 /* words0-2 BDE memcpy */
f0d9bccc
JS
9059 /* word3 iocb=iotag32 wqe=response_payload_len */
9060 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9061 /* word4 */
9062 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9063 /* word5 iocb=rsvd wge=did */
9064 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9065 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9066
9067 if_type = bf_get(lpfc_sli_intf_if_type,
9068 &phba->sli4_hba.sli_intf);
9069 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9070 if (iocbq->vport->fc_flag & FC_PT2PT) {
9071 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9072 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9073 iocbq->vport->fc_myDID);
9074 if (iocbq->vport->fc_myDID == Fabric_DID) {
9075 bf_set(wqe_els_did,
9076 &wqe->xmit_els_rsp.wqe_dest, 0);
9077 }
9078 }
9079 }
f0d9bccc
JS
9080 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9081 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9082 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9083 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9084 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9085 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9086 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9087 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9088 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9089 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9090 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9091 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9092 LPFC_WQE_LENLOC_WORD3);
9093 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9094 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9095 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9096 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9097 iocbq->context2)->virt);
9098 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9099 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9100 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9101 iocbq->vport->fc_myDID);
939723a4
JS
9102 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9103 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9104 phba->vpi_ids[phba->pport->vpi]);
9105 }
4f774513 9106 command_type = OTHER_COMMAND;
7851fe2c 9107 break;
4f774513
JS
9108 case CMD_CLOSE_XRI_CN:
9109 case CMD_ABORT_XRI_CN:
9110 case CMD_ABORT_XRI_CX:
9111 /* words 0-2 memcpy should be 0 rserved */
9112 /* port will send abts */
dcf2a4e0
JS
9113 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9114 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9115 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9116 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9117 } else
9118 fip = 0;
9119
9120 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9121 /*
dcf2a4e0
JS
9122 * The link is down, or the command was ELS_FIP
9123 * so the fw does not need to send abts
4f774513
JS
9124 * on the wire.
9125 */
9126 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9127 else
9128 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9129 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9130 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9131 wqe->abort_cmd.rsrvd5 = 0;
9132 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9133 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9134 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9135 /*
9136 * The abort handler will send us CMD_ABORT_XRI_CN or
9137 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9138 */
f0d9bccc
JS
9139 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9140 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9141 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9142 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9143 cmnd = CMD_ABORT_XRI_CX;
9144 command_type = OTHER_COMMAND;
9145 xritag = 0;
7851fe2c 9146 break;
6669f9bb 9147 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9148 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9149 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9150 * we re-construct this WQE here based on information in
9151 * iocbq from scratch.
9152 */
9153 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9154 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9155 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9156 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9157 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9158 LPFC_ABTS_UNSOL_INT) {
9159 /* ABTS sent by initiator to CT exchange, the
9160 * RX_ID field will be filled with the newly
9161 * allocated responder XRI.
9162 */
9163 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9164 iocbq->sli4_xritag);
9165 } else {
9166 /* ABTS sent by responder to CT exchange, the
9167 * RX_ID field will be filled with the responder
9168 * RX_ID from ABTS.
9169 */
9170 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9171 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9172 }
6669f9bb
JS
9173 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9174 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9175
9176 /* Use CT=VPI */
9177 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9178 ndlp->nlp_DID);
9179 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9180 iocbq->iocb.ulpContext);
9181 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9182 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9183 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9184 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9185 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9186 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9187 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9188 command_type = OTHER_COMMAND;
546fc854
JS
9189 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9190 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9191 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9192 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9193 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9194 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9195 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9196 }
9197
7851fe2c 9198 break;
ae9e28f3
JS
9199 case CMD_SEND_FRAME:
9200 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9201 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9202 return 0;
4f774513
JS
9203 case CMD_XRI_ABORTED_CX:
9204 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9205 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9206 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9207 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9208 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9209 default:
9210 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9211 "2014 Invalid command 0x%x\n",
9212 iocbq->iocb.ulpCommand);
9213 return IOCB_ERROR;
7851fe2c 9214 break;
4f774513 9215 }
6d368e53 9216
8012cc38
JS
9217 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9218 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9219 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9220 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9221 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9222 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9223 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9224 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9225 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9226 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9227 wqe->generic.wqe_com.abort_tag = abort_tag;
9228 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9229 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9230 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9231 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9232 return 0;
9233}
9234
9235/**
9236 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9237 * @phba: Pointer to HBA context object.
9238 * @ring_number: SLI ring number to issue iocb on.
9239 * @piocb: Pointer to command iocb.
9240 * @flag: Flag indicating if this command can be put into txq.
9241 *
9242 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9243 * an iocb command to an HBA with SLI-4 interface spec.
9244 *
9245 * This function is called with hbalock held. The function will return success
9246 * after it successfully submit the iocb to firmware or after adding to the
9247 * txq.
9248 **/
9249static int
9250__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9251 struct lpfc_iocbq *piocb, uint32_t flag)
9252{
9253 struct lpfc_sglq *sglq;
b5c53958
JS
9254 union lpfc_wqe *wqe;
9255 union lpfc_wqe128 wqe128;
1ba981fd 9256 struct lpfc_queue *wq;
895427bd 9257 struct lpfc_sli_ring *pring;
4f774513 9258
895427bd
JS
9259 /* Get the WQ */
9260 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9261 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9262 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9263 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9264 else
9265 wq = phba->sli4_hba.oas_wq;
9266 } else {
9267 wq = phba->sli4_hba.els_wq;
9268 }
9269
9270 /* Get corresponding ring */
9271 pring = wq->pring;
1c2ba475 9272
b5c53958
JS
9273 /*
9274 * The WQE can be either 64 or 128 bytes,
9275 * so allocate space on the stack assuming the largest.
9276 */
9277 wqe = (union lpfc_wqe *)&wqe128;
9278
895427bd
JS
9279 lockdep_assert_held(&phba->hbalock);
9280
4f774513
JS
9281 if (piocb->sli4_xritag == NO_XRI) {
9282 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9283 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9284 sglq = NULL;
9285 else {
0e9bb8d7 9286 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9287 if (!(flag & SLI_IOCB_RET_IOCB)) {
9288 __lpfc_sli_ringtx_put(phba,
9289 pring, piocb);
9290 return IOCB_SUCCESS;
9291 } else {
9292 return IOCB_BUSY;
9293 }
9294 } else {
895427bd 9295 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9296 if (!sglq) {
9297 if (!(flag & SLI_IOCB_RET_IOCB)) {
9298 __lpfc_sli_ringtx_put(phba,
9299 pring,
9300 piocb);
9301 return IOCB_SUCCESS;
9302 } else
9303 return IOCB_BUSY;
9304 }
9305 }
4f774513 9306 }
2ea259ee 9307 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9308 /* These IO's already have an XRI and a mapped sgl. */
9309 sglq = NULL;
2ea259ee 9310 else {
6d368e53
JS
9311 /*
9312 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9313 * sglq is on the active list
9314 */
edccdc17 9315 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9316 if (!sglq)
9317 return IOCB_ERROR;
9318 }
9319
9320 if (sglq) {
6d368e53 9321 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9322 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9323 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9324 return IOCB_ERROR;
9325 }
9326
b5c53958 9327 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
4f774513
JS
9328 return IOCB_ERROR;
9329
895427bd
JS
9330 if (lpfc_sli4_wq_put(wq, wqe))
9331 return IOCB_ERROR;
4f774513
JS
9332 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9333
9334 return 0;
9335}
9336
9337/**
9338 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9339 *
9340 * This routine wraps the actual lockless version for issusing IOCB function
9341 * pointer from the lpfc_hba struct.
9342 *
9343 * Return codes:
b5c53958
JS
9344 * IOCB_ERROR - Error
9345 * IOCB_SUCCESS - Success
9346 * IOCB_BUSY - Busy
4f774513 9347 **/
2a9bf3d0 9348int
4f774513
JS
9349__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9350 struct lpfc_iocbq *piocb, uint32_t flag)
9351{
9352 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9353}
9354
9355/**
25985edc 9356 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9357 * @phba: The hba struct for which this call is being executed.
9358 * @dev_grp: The HBA PCI-Device group number.
9359 *
9360 * This routine sets up the SLI interface API function jump table in @phba
9361 * struct.
9362 * Returns: 0 - success, -ENODEV - failure.
9363 **/
9364int
9365lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9366{
9367
9368 switch (dev_grp) {
9369 case LPFC_PCI_DEV_LP:
9370 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9371 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9372 break;
9373 case LPFC_PCI_DEV_OC:
9374 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9375 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9376 break;
9377 default:
9378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9379 "1419 Invalid HBA PCI-device group: 0x%x\n",
9380 dev_grp);
9381 return -ENODEV;
9382 break;
9383 }
9384 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9385 return 0;
9386}
9387
a1efe163 9388/**
895427bd 9389 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9390 * @phba: Pointer to HBA context object.
a1efe163
JS
9391 * @piocb: Pointer to command iocb.
9392 *
895427bd
JS
9393 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9394 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9395 * Since ABORTS must go on the same WQ of the command they are
895427bd 9396 * aborting, we use command's hba_wqidx.
a1efe163 9397 */
895427bd
JS
9398struct lpfc_sli_ring *
9399lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9400{
895427bd 9401 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9402 if (!(phba->cfg_fof) ||
895427bd 9403 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9404 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9405 return NULL;
8b0dff14 9406 /*
895427bd 9407 * for abort iocb hba_wqidx should already
8b0dff14
JS
9408 * be setup based on what work queue we used.
9409 */
8e036a94 9410 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9411 piocb->hba_wqidx =
8b0dff14
JS
9412 lpfc_sli4_scmd_to_wqidx_distr(phba,
9413 piocb->context1);
8e036a94
DK
9414 piocb->hba_wqidx = piocb->hba_wqidx %
9415 phba->cfg_fcp_io_channel;
9416 }
895427bd 9417 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9418 } else {
9419 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9420 return NULL;
9421 piocb->hba_wqidx = 0;
9422 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9423 }
895427bd
JS
9424 } else {
9425 if (unlikely(!phba->sli4_hba.els_wq))
9426 return NULL;
9427 piocb->hba_wqidx = 0;
9428 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9429 }
9bd2bff5
JS
9430}
9431
4f774513
JS
9432/**
9433 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9434 * @phba: Pointer to HBA context object.
9435 * @pring: Pointer to driver SLI ring object.
9436 * @piocb: Pointer to command iocb.
9437 * @flag: Flag indicating if this command can be put into txq.
9438 *
9439 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9440 * function. This function gets the hbalock and calls
9441 * __lpfc_sli_issue_iocb function and will return the error returned
9442 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9443 * functions which do not hold hbalock.
9444 **/
9445int
9446lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9447 struct lpfc_iocbq *piocb, uint32_t flag)
9448{
895427bd 9449 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9450 struct lpfc_sli_ring *pring;
ba20c853
JS
9451 struct lpfc_queue *fpeq;
9452 struct lpfc_eqe *eqe;
4f774513 9453 unsigned long iflags;
2a76a283 9454 int rc, idx;
4f774513 9455
7e56aa25 9456 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9457 pring = lpfc_sli4_calc_ring(phba, piocb);
9458 if (unlikely(pring == NULL))
9bd2bff5 9459 return IOCB_ERROR;
ba20c853 9460
9bd2bff5
JS
9461 spin_lock_irqsave(&pring->ring_lock, iflags);
9462 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9463 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9464
9bd2bff5 9465 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9466 idx = piocb->hba_wqidx;
9467 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9468
895427bd 9469 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9470
9bd2bff5
JS
9471 /* Get associated EQ with this index */
9472 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9473
9bd2bff5
JS
9474 /* Turn off interrupts from this EQ */
9475 lpfc_sli4_eq_clr_intr(fpeq);
ba20c853 9476
9bd2bff5
JS
9477 /*
9478 * Process all the events on FCP EQ
9479 */
9480 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9481 lpfc_sli4_hba_handle_eqe(phba,
9482 eqe, idx);
9483 fpeq->EQ_processed++;
ba20c853 9484 }
ba20c853 9485
9bd2bff5
JS
9486 /* Always clear and re-arm the EQ */
9487 lpfc_sli4_eq_release(fpeq,
9488 LPFC_QUEUE_REARM);
9489 }
895427bd 9490 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9491 }
7e56aa25
JS
9492 } else {
9493 /* For now, SLI2/3 will still use hbalock */
9494 spin_lock_irqsave(&phba->hbalock, iflags);
9495 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9496 spin_unlock_irqrestore(&phba->hbalock, iflags);
9497 }
4f774513
JS
9498 return rc;
9499}
9500
9501/**
9502 * lpfc_extra_ring_setup - Extra ring setup function
9503 * @phba: Pointer to HBA context object.
9504 *
9505 * This function is called while driver attaches with the
9506 * HBA to setup the extra ring. The extra ring is used
9507 * only when driver needs to support target mode functionality
9508 * or IP over FC functionalities.
9509 *
895427bd 9510 * This function is called with no lock held. SLI3 only.
4f774513
JS
9511 **/
9512static int
9513lpfc_extra_ring_setup( struct lpfc_hba *phba)
9514{
9515 struct lpfc_sli *psli;
9516 struct lpfc_sli_ring *pring;
9517
9518 psli = &phba->sli;
9519
9520 /* Adjust cmd/rsp ring iocb entries more evenly */
9521
9522 /* Take some away from the FCP ring */
895427bd 9523 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9524 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9525 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9526 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9527 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9528
a4bc3379 9529 /* and give them to the extra ring */
895427bd 9530 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9531
7e56aa25
JS
9532 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9533 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9534 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9535 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9536
9537 /* Setup default profile for this ring */
9538 pring->iotag_max = 4096;
9539 pring->num_mask = 1;
9540 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9541 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9542 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9543 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9544 return 0;
9545}
9546
cb69f7de
JS
9547/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9548 * @phba: Pointer to HBA context object.
9549 * @iocbq: Pointer to iocb object.
9550 *
9551 * The async_event handler calls this routine when it receives
9552 * an ASYNC_STATUS_CN event from the port. The port generates
9553 * this event when an Abort Sequence request to an rport fails
9554 * twice in succession. The abort could be originated by the
9555 * driver or by the port. The ABTS could have been for an ELS
9556 * or FCP IO. The port only generates this event when an ABTS
9557 * fails to complete after one retry.
9558 */
9559static void
9560lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9561 struct lpfc_iocbq *iocbq)
9562{
9563 struct lpfc_nodelist *ndlp = NULL;
9564 uint16_t rpi = 0, vpi = 0;
9565 struct lpfc_vport *vport = NULL;
9566
9567 /* The rpi in the ulpContext is vport-sensitive. */
9568 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9569 rpi = iocbq->iocb.ulpContext;
9570
9571 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9572 "3092 Port generated ABTS async event "
9573 "on vpi %d rpi %d status 0x%x\n",
9574 vpi, rpi, iocbq->iocb.ulpStatus);
9575
9576 vport = lpfc_find_vport_by_vpid(phba, vpi);
9577 if (!vport)
9578 goto err_exit;
9579 ndlp = lpfc_findnode_rpi(vport, rpi);
9580 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9581 goto err_exit;
9582
9583 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9584 lpfc_sli_abts_recover_port(vport, ndlp);
9585 return;
9586
9587 err_exit:
9588 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9589 "3095 Event Context not found, no "
9590 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9591 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9592 vpi, rpi);
9593}
9594
9595/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9596 * @phba: pointer to HBA context object.
9597 * @ndlp: nodelist pointer for the impacted rport.
9598 * @axri: pointer to the wcqe containing the failed exchange.
9599 *
9600 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9601 * port. The port generates this event when an abort exchange request to an
9602 * rport fails twice in succession with no reply. The abort could be originated
9603 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9604 */
9605void
9606lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9607 struct lpfc_nodelist *ndlp,
9608 struct sli4_wcqe_xri_aborted *axri)
9609{
9610 struct lpfc_vport *vport;
5c1db2ac 9611 uint32_t ext_status = 0;
cb69f7de 9612
6b5151fd 9613 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9614 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9615 "3115 Node Context not found, driver "
9616 "ignoring abts err event\n");
6b5151fd
JS
9617 return;
9618 }
9619
cb69f7de
JS
9620 vport = ndlp->vport;
9621 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9622 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9623 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9624 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9625 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9626 bf_get(lpfc_wcqe_xa_status, axri),
9627 axri->parameter);
cb69f7de 9628
5c1db2ac
JS
9629 /*
9630 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9631 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9632 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9633 */
e3d2b802 9634 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9635 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9636 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9637 lpfc_sli_abts_recover_port(vport, ndlp);
9638}
9639
e59058c4 9640/**
3621a710 9641 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9642 * @phba: Pointer to HBA context object.
9643 * @pring: Pointer to driver SLI ring object.
9644 * @iocbq: Pointer to iocb object.
9645 *
9646 * This function is called by the slow ring event handler
9647 * function when there is an ASYNC event iocb in the ring.
9648 * This function is called with no lock held.
9649 * Currently this function handles only temperature related
9650 * ASYNC events. The function decodes the temperature sensor
9651 * event message and posts events for the management applications.
9652 **/
98c9ea5c 9653static void
57127f15
JS
9654lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9655 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9656{
9657 IOCB_t *icmd;
9658 uint16_t evt_code;
57127f15
JS
9659 struct temp_event temp_event_data;
9660 struct Scsi_Host *shost;
a257bf90 9661 uint32_t *iocb_w;
57127f15
JS
9662
9663 icmd = &iocbq->iocb;
9664 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9665
cb69f7de
JS
9666 switch (evt_code) {
9667 case ASYNC_TEMP_WARN:
9668 case ASYNC_TEMP_SAFE:
9669 temp_event_data.data = (uint32_t) icmd->ulpContext;
9670 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9671 if (evt_code == ASYNC_TEMP_WARN) {
9672 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9673 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9674 "0347 Adapter is very hot, please take "
9675 "corrective action. temperature : %d Celsius\n",
9676 (uint32_t) icmd->ulpContext);
9677 } else {
9678 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9679 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9680 "0340 Adapter temperature is OK now. "
9681 "temperature : %d Celsius\n",
9682 (uint32_t) icmd->ulpContext);
9683 }
9684
9685 /* Send temperature change event to applications */
9686 shost = lpfc_shost_from_vport(phba->pport);
9687 fc_host_post_vendor_event(shost, fc_get_event_number(),
9688 sizeof(temp_event_data), (char *) &temp_event_data,
9689 LPFC_NL_VENDOR_ID);
9690 break;
9691 case ASYNC_STATUS_CN:
9692 lpfc_sli_abts_err_handler(phba, iocbq);
9693 break;
9694 default:
a257bf90 9695 iocb_w = (uint32_t *) icmd;
cb69f7de 9696 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9697 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9698 " evt_code 0x%x\n"
a257bf90
JS
9699 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9700 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9701 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9702 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9703 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9704 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9705 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9706 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9707 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9708
cb69f7de 9709 break;
57127f15 9710 }
57127f15
JS
9711}
9712
9713
e59058c4 9714/**
895427bd 9715 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9716 * @phba: Pointer to HBA context object.
9717 *
9718 * lpfc_sli_setup sets up rings of the SLI interface with
9719 * number of iocbs per ring and iotags. This function is
9720 * called while driver attach to the HBA and before the
9721 * interrupts are enabled. So there is no need for locking.
9722 *
9723 * This function always returns 0.
9724 **/
dea3101e 9725int
895427bd
JS
9726lpfc_sli4_setup(struct lpfc_hba *phba)
9727{
9728 struct lpfc_sli_ring *pring;
9729
9730 pring = phba->sli4_hba.els_wq->pring;
9731 pring->num_mask = LPFC_MAX_RING_MASK;
9732 pring->prt[0].profile = 0; /* Mask 0 */
9733 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9734 pring->prt[0].type = FC_TYPE_ELS;
9735 pring->prt[0].lpfc_sli_rcv_unsol_event =
9736 lpfc_els_unsol_event;
9737 pring->prt[1].profile = 0; /* Mask 1 */
9738 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9739 pring->prt[1].type = FC_TYPE_ELS;
9740 pring->prt[1].lpfc_sli_rcv_unsol_event =
9741 lpfc_els_unsol_event;
9742 pring->prt[2].profile = 0; /* Mask 2 */
9743 /* NameServer Inquiry */
9744 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9745 /* NameServer */
9746 pring->prt[2].type = FC_TYPE_CT;
9747 pring->prt[2].lpfc_sli_rcv_unsol_event =
9748 lpfc_ct_unsol_event;
9749 pring->prt[3].profile = 0; /* Mask 3 */
9750 /* NameServer response */
9751 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9752 /* NameServer */
9753 pring->prt[3].type = FC_TYPE_CT;
9754 pring->prt[3].lpfc_sli_rcv_unsol_event =
9755 lpfc_ct_unsol_event;
9756 return 0;
9757}
9758
9759/**
9760 * lpfc_sli_setup - SLI ring setup function
9761 * @phba: Pointer to HBA context object.
9762 *
9763 * lpfc_sli_setup sets up rings of the SLI interface with
9764 * number of iocbs per ring and iotags. This function is
9765 * called while driver attach to the HBA and before the
9766 * interrupts are enabled. So there is no need for locking.
9767 *
9768 * This function always returns 0. SLI3 only.
9769 **/
9770int
dea3101e
JB
9771lpfc_sli_setup(struct lpfc_hba *phba)
9772{
ed957684 9773 int i, totiocbsize = 0;
dea3101e
JB
9774 struct lpfc_sli *psli = &phba->sli;
9775 struct lpfc_sli_ring *pring;
9776
2a76a283 9777 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 9778 psli->sli_flag = 0;
dea3101e 9779
604a3e30
JB
9780 psli->iocbq_lookup = NULL;
9781 psli->iocbq_lookup_len = 0;
9782 psli->last_iotag = 0;
9783
dea3101e 9784 for (i = 0; i < psli->num_rings; i++) {
895427bd 9785 pring = &psli->sli3_ring[i];
dea3101e
JB
9786 switch (i) {
9787 case LPFC_FCP_RING: /* ring 0 - FCP */
9788 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9789 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9790 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9791 pring->sli.sli3.numCiocb +=
9792 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9793 pring->sli.sli3.numRiocb +=
9794 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9795 pring->sli.sli3.numCiocb +=
9796 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9797 pring->sli.sli3.numRiocb +=
9798 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9799 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9800 SLI3_IOCB_CMD_SIZE :
9801 SLI2_IOCB_CMD_SIZE;
7e56aa25 9802 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9803 SLI3_IOCB_RSP_SIZE :
9804 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9805 pring->iotag_ctr = 0;
9806 pring->iotag_max =
92d7f7b0 9807 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
9808 pring->fast_iotag = pring->iotag_max;
9809 pring->num_mask = 0;
9810 break;
a4bc3379 9811 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 9812 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9813 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9814 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9815 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9816 SLI3_IOCB_CMD_SIZE :
9817 SLI2_IOCB_CMD_SIZE;
7e56aa25 9818 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9819 SLI3_IOCB_RSP_SIZE :
9820 SLI2_IOCB_RSP_SIZE;
2e0fef85 9821 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9822 pring->num_mask = 0;
9823 break;
9824 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9825 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9826 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9827 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9828 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9829 SLI3_IOCB_CMD_SIZE :
9830 SLI2_IOCB_CMD_SIZE;
7e56aa25 9831 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9832 SLI3_IOCB_RSP_SIZE :
9833 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9834 pring->fast_iotag = 0;
9835 pring->iotag_ctr = 0;
9836 pring->iotag_max = 4096;
57127f15
JS
9837 pring->lpfc_sli_rcv_async_status =
9838 lpfc_sli_async_event_handler;
6669f9bb 9839 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9840 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9841 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9842 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9843 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9844 lpfc_els_unsol_event;
dea3101e 9845 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9846 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9847 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9848 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9849 lpfc_els_unsol_event;
dea3101e
JB
9850 pring->prt[2].profile = 0; /* Mask 2 */
9851 /* NameServer Inquiry */
6a9c52cf 9852 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9853 /* NameServer */
6a9c52cf 9854 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9855 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9856 lpfc_ct_unsol_event;
dea3101e
JB
9857 pring->prt[3].profile = 0; /* Mask 3 */
9858 /* NameServer response */
6a9c52cf 9859 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9860 /* NameServer */
6a9c52cf 9861 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9862 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9863 lpfc_ct_unsol_event;
dea3101e
JB
9864 break;
9865 }
7e56aa25
JS
9866 totiocbsize += (pring->sli.sli3.numCiocb *
9867 pring->sli.sli3.sizeCiocb) +
9868 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 9869 }
ed957684 9870 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 9871 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
9872 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9873 "SLI2 SLIM Data: x%x x%lx\n",
9874 phba->brd_no, totiocbsize,
9875 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 9876 }
cf5bf97e
JW
9877 if (phba->cfg_multi_ring_support == 2)
9878 lpfc_extra_ring_setup(phba);
dea3101e
JB
9879
9880 return 0;
9881}
9882
e59058c4 9883/**
895427bd 9884 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
9885 * @phba: Pointer to HBA context object.
9886 *
895427bd 9887 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
9888 * ring. This function also initializes ring indices of each ring.
9889 * This function is called during the initialization of the SLI
9890 * interface of an HBA.
9891 * This function is called with no lock held and always returns
9892 * 1.
9893 **/
895427bd
JS
9894void
9895lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
9896{
9897 struct lpfc_sli *psli;
9898 struct lpfc_sli_ring *pring;
604a3e30 9899 int i;
dea3101e
JB
9900
9901 psli = &phba->sli;
2e0fef85 9902 spin_lock_irq(&phba->hbalock);
dea3101e 9903 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9904 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 9905 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
9906 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9907 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 9908 pring->flag = 0;
895427bd 9909 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
9910 INIT_LIST_HEAD(&pring->txq);
9911 INIT_LIST_HEAD(&pring->txcmplq);
9912 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 9913 spin_lock_init(&pring->ring_lock);
dea3101e 9914 }
895427bd
JS
9915 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9916 pring = phba->sli4_hba.nvme_wq[i]->pring;
9917 pring->flag = 0;
9918 pring->ringno = LPFC_FCP_RING;
9919 INIT_LIST_HEAD(&pring->txq);
9920 INIT_LIST_HEAD(&pring->txcmplq);
9921 INIT_LIST_HEAD(&pring->iocb_continueq);
9922 spin_lock_init(&pring->ring_lock);
9923 }
9924 pring = phba->sli4_hba.els_wq->pring;
9925 pring->flag = 0;
9926 pring->ringno = LPFC_ELS_RING;
9927 INIT_LIST_HEAD(&pring->txq);
9928 INIT_LIST_HEAD(&pring->txcmplq);
9929 INIT_LIST_HEAD(&pring->iocb_continueq);
9930 spin_lock_init(&pring->ring_lock);
dea3101e 9931
895427bd
JS
9932 if (phba->cfg_nvme_io_channel) {
9933 pring = phba->sli4_hba.nvmels_wq->pring;
9934 pring->flag = 0;
9935 pring->ringno = LPFC_ELS_RING;
9936 INIT_LIST_HEAD(&pring->txq);
9937 INIT_LIST_HEAD(&pring->txcmplq);
9938 INIT_LIST_HEAD(&pring->iocb_continueq);
9939 spin_lock_init(&pring->ring_lock);
9940 }
9941
9942 if (phba->cfg_fof) {
9943 pring = phba->sli4_hba.oas_wq->pring;
9944 pring->flag = 0;
9945 pring->ringno = LPFC_FCP_RING;
9946 INIT_LIST_HEAD(&pring->txq);
9947 INIT_LIST_HEAD(&pring->txcmplq);
9948 INIT_LIST_HEAD(&pring->iocb_continueq);
9949 spin_lock_init(&pring->ring_lock);
9950 }
9951
9952 spin_unlock_irq(&phba->hbalock);
9953}
9954
9955/**
9956 * lpfc_sli_queue_init - Queue initialization function
9957 * @phba: Pointer to HBA context object.
9958 *
9959 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9960 * ring. This function also initializes ring indices of each ring.
9961 * This function is called during the initialization of the SLI
9962 * interface of an HBA.
9963 * This function is called with no lock held and always returns
9964 * 1.
9965 **/
9966void
9967lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e
JB
9968{
9969 struct lpfc_sli *psli;
9970 struct lpfc_sli_ring *pring;
604a3e30 9971 int i;
dea3101e
JB
9972
9973 psli = &phba->sli;
2e0fef85 9974 spin_lock_irq(&phba->hbalock);
dea3101e 9975 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9976 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
9977 /* Initialize list headers for txq and txcmplq as double linked lists */
9978 for (i = 0; i < psli->num_rings; i++) {
895427bd 9979 pring = &psli->sli3_ring[i];
dea3101e 9980 pring->ringno = i;
7e56aa25
JS
9981 pring->sli.sli3.next_cmdidx = 0;
9982 pring->sli.sli3.local_getidx = 0;
9983 pring->sli.sli3.cmdidx = 0;
dea3101e 9984 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 9985 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 9986 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
9987 pring->flag = 0;
9988 INIT_LIST_HEAD(&pring->txq);
9989 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 9990 spin_lock_init(&pring->ring_lock);
dea3101e 9991 }
2e0fef85 9992 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9993}
9994
04c68496
JS
9995/**
9996 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9997 * @phba: Pointer to HBA context object.
9998 *
9999 * This routine flushes the mailbox command subsystem. It will unconditionally
10000 * flush all the mailbox commands in the three possible stages in the mailbox
10001 * command sub-system: pending mailbox command queue; the outstanding mailbox
10002 * command; and completed mailbox command queue. It is caller's responsibility
10003 * to make sure that the driver is in the proper state to flush the mailbox
10004 * command sub-system. Namely, the posting of mailbox commands into the
10005 * pending mailbox command queue from the various clients must be stopped;
10006 * either the HBA is in a state that it will never works on the outstanding
10007 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10008 * mailbox command has been completed.
10009 **/
10010static void
10011lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10012{
10013 LIST_HEAD(completions);
10014 struct lpfc_sli *psli = &phba->sli;
10015 LPFC_MBOXQ_t *pmb;
10016 unsigned long iflag;
10017
10018 /* Flush all the mailbox commands in the mbox system */
10019 spin_lock_irqsave(&phba->hbalock, iflag);
10020 /* The pending mailbox command queue */
10021 list_splice_init(&phba->sli.mboxq, &completions);
10022 /* The outstanding active mailbox command */
10023 if (psli->mbox_active) {
10024 list_add_tail(&psli->mbox_active->list, &completions);
10025 psli->mbox_active = NULL;
10026 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10027 }
10028 /* The completed mailbox command queue */
10029 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10030 spin_unlock_irqrestore(&phba->hbalock, iflag);
10031
10032 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10033 while (!list_empty(&completions)) {
10034 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10035 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10036 if (pmb->mbox_cmpl)
10037 pmb->mbox_cmpl(phba, pmb);
10038 }
10039}
10040
e59058c4 10041/**
3621a710 10042 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10043 * @vport: Pointer to virtual port object.
10044 *
10045 * lpfc_sli_host_down is called to clean up the resources
10046 * associated with a vport before destroying virtual
10047 * port data structures.
10048 * This function does following operations:
10049 * - Free discovery resources associated with this virtual
10050 * port.
10051 * - Free iocbs associated with this virtual port in
10052 * the txq.
10053 * - Send abort for all iocb commands associated with this
10054 * vport in txcmplq.
10055 *
10056 * This function is called with no lock held and always returns 1.
10057 **/
92d7f7b0
JS
10058int
10059lpfc_sli_host_down(struct lpfc_vport *vport)
10060{
858c9f6c 10061 LIST_HEAD(completions);
92d7f7b0
JS
10062 struct lpfc_hba *phba = vport->phba;
10063 struct lpfc_sli *psli = &phba->sli;
895427bd 10064 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10065 struct lpfc_sli_ring *pring;
10066 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10067 int i;
10068 unsigned long flags = 0;
10069 uint16_t prev_pring_flag;
10070
10071 lpfc_cleanup_discovery_resources(vport);
10072
10073 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10074
895427bd
JS
10075 /*
10076 * Error everything on the txq since these iocbs
10077 * have not been given to the FW yet.
10078 * Also issue ABTS for everything on the txcmplq
10079 */
10080 if (phba->sli_rev != LPFC_SLI_REV4) {
10081 for (i = 0; i < psli->num_rings; i++) {
10082 pring = &psli->sli3_ring[i];
10083 prev_pring_flag = pring->flag;
10084 /* Only slow rings */
10085 if (pring->ringno == LPFC_ELS_RING) {
10086 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10087 /* Set the lpfc data pending flag */
10088 set_bit(LPFC_DATA_READY, &phba->data_flags);
10089 }
10090 list_for_each_entry_safe(iocb, next_iocb,
10091 &pring->txq, list) {
10092 if (iocb->vport != vport)
10093 continue;
10094 list_move_tail(&iocb->list, &completions);
10095 }
10096 list_for_each_entry_safe(iocb, next_iocb,
10097 &pring->txcmplq, list) {
10098 if (iocb->vport != vport)
10099 continue;
10100 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10101 }
10102 pring->flag = prev_pring_flag;
10103 }
10104 } else {
10105 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10106 pring = qp->pring;
10107 if (!pring)
92d7f7b0 10108 continue;
895427bd
JS
10109 if (pring == phba->sli4_hba.els_wq->pring) {
10110 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10111 /* Set the lpfc data pending flag */
10112 set_bit(LPFC_DATA_READY, &phba->data_flags);
10113 }
10114 prev_pring_flag = pring->flag;
10115 spin_lock_irq(&pring->ring_lock);
10116 list_for_each_entry_safe(iocb, next_iocb,
10117 &pring->txq, list) {
10118 if (iocb->vport != vport)
10119 continue;
10120 list_move_tail(&iocb->list, &completions);
10121 }
10122 spin_unlock_irq(&pring->ring_lock);
10123 list_for_each_entry_safe(iocb, next_iocb,
10124 &pring->txcmplq, list) {
10125 if (iocb->vport != vport)
10126 continue;
10127 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10128 }
10129 pring->flag = prev_pring_flag;
92d7f7b0 10130 }
92d7f7b0 10131 }
92d7f7b0
JS
10132 spin_unlock_irqrestore(&phba->hbalock, flags);
10133
a257bf90
JS
10134 /* Cancel all the IOCBs from the completions list */
10135 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10136 IOERR_SLI_DOWN);
92d7f7b0
JS
10137 return 1;
10138}
10139
e59058c4 10140/**
3621a710 10141 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10142 * @phba: Pointer to HBA context object.
10143 *
10144 * This function cleans up all iocb, buffers, mailbox commands
10145 * while shutting down the HBA. This function is called with no
10146 * lock held and always returns 1.
10147 * This function does the following to cleanup driver resources:
10148 * - Free discovery resources for each virtual port
10149 * - Cleanup any pending fabric iocbs
10150 * - Iterate through the iocb txq and free each entry
10151 * in the list.
10152 * - Free up any buffer posted to the HBA
10153 * - Free mailbox commands in the mailbox queue.
10154 **/
dea3101e 10155int
2e0fef85 10156lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10157{
2534ba75 10158 LIST_HEAD(completions);
2e0fef85 10159 struct lpfc_sli *psli = &phba->sli;
895427bd 10160 struct lpfc_queue *qp = NULL;
dea3101e 10161 struct lpfc_sli_ring *pring;
0ff10d46 10162 struct lpfc_dmabuf *buf_ptr;
dea3101e 10163 unsigned long flags = 0;
04c68496
JS
10164 int i;
10165
10166 /* Shutdown the mailbox command sub-system */
618a5230 10167 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10168
dea3101e
JB
10169 lpfc_hba_down_prep(phba);
10170
92d7f7b0
JS
10171 lpfc_fabric_abort_hba(phba);
10172
2e0fef85 10173 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10174
895427bd
JS
10175 /*
10176 * Error everything on the txq since these iocbs
10177 * have not been given to the FW yet.
10178 */
10179 if (phba->sli_rev != LPFC_SLI_REV4) {
10180 for (i = 0; i < psli->num_rings; i++) {
10181 pring = &psli->sli3_ring[i];
10182 /* Only slow rings */
10183 if (pring->ringno == LPFC_ELS_RING) {
10184 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10185 /* Set the lpfc data pending flag */
10186 set_bit(LPFC_DATA_READY, &phba->data_flags);
10187 }
10188 list_splice_init(&pring->txq, &completions);
10189 }
10190 } else {
10191 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10192 pring = qp->pring;
10193 if (!pring)
10194 continue;
10195 spin_lock_irq(&pring->ring_lock);
10196 list_splice_init(&pring->txq, &completions);
10197 spin_unlock_irq(&pring->ring_lock);
10198 if (pring == phba->sli4_hba.els_wq->pring) {
10199 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10200 /* Set the lpfc data pending flag */
10201 set_bit(LPFC_DATA_READY, &phba->data_flags);
10202 }
10203 }
2534ba75 10204 }
2e0fef85 10205 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10206
a257bf90
JS
10207 /* Cancel all the IOCBs from the completions list */
10208 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10209 IOERR_SLI_DOWN);
dea3101e 10210
0ff10d46
JS
10211 spin_lock_irqsave(&phba->hbalock, flags);
10212 list_splice_init(&phba->elsbuf, &completions);
10213 phba->elsbuf_cnt = 0;
10214 phba->elsbuf_prev_cnt = 0;
10215 spin_unlock_irqrestore(&phba->hbalock, flags);
10216
10217 while (!list_empty(&completions)) {
10218 list_remove_head(&completions, buf_ptr,
10219 struct lpfc_dmabuf, list);
10220 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10221 kfree(buf_ptr);
10222 }
10223
dea3101e
JB
10224 /* Return any active mbox cmds */
10225 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10226
da0436e9 10227 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10228 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10229 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10230
da0436e9
JS
10231 return 1;
10232}
10233
e59058c4 10234/**
3621a710 10235 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10236 * @srcp: Source memory pointer.
10237 * @destp: Destination memory pointer.
10238 * @cnt: Number of words required to be copied.
10239 *
10240 * This function is used for copying data between driver memory
10241 * and the SLI memory. This function also changes the endianness
10242 * of each word if native endianness is different from SLI
10243 * endianness. This function can be called with or without
10244 * lock.
10245 **/
dea3101e
JB
10246void
10247lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10248{
10249 uint32_t *src = srcp;
10250 uint32_t *dest = destp;
10251 uint32_t ldata;
10252 int i;
10253
10254 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10255 ldata = *src;
10256 ldata = le32_to_cpu(ldata);
10257 *dest = ldata;
10258 src++;
10259 dest++;
10260 }
10261}
10262
e59058c4 10263
a0c87cbd
JS
10264/**
10265 * lpfc_sli_bemem_bcopy - SLI memory copy function
10266 * @srcp: Source memory pointer.
10267 * @destp: Destination memory pointer.
10268 * @cnt: Number of words required to be copied.
10269 *
10270 * This function is used for copying data between a data structure
10271 * with big endian representation to local endianness.
10272 * This function can be called with or without lock.
10273 **/
10274void
10275lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10276{
10277 uint32_t *src = srcp;
10278 uint32_t *dest = destp;
10279 uint32_t ldata;
10280 int i;
10281
10282 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10283 ldata = *src;
10284 ldata = be32_to_cpu(ldata);
10285 *dest = ldata;
10286 src++;
10287 dest++;
10288 }
10289}
10290
e59058c4 10291/**
3621a710 10292 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10293 * @phba: Pointer to HBA context object.
10294 * @pring: Pointer to driver SLI ring object.
10295 * @mp: Pointer to driver buffer object.
10296 *
10297 * This function is called with no lock held.
10298 * It always return zero after adding the buffer to the postbufq
10299 * buffer list.
10300 **/
dea3101e 10301int
2e0fef85
JS
10302lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10303 struct lpfc_dmabuf *mp)
dea3101e
JB
10304{
10305 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10306 later */
2e0fef85 10307 spin_lock_irq(&phba->hbalock);
dea3101e 10308 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10309 pring->postbufq_cnt++;
2e0fef85 10310 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10311 return 0;
10312}
10313
e59058c4 10314/**
3621a710 10315 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10316 * @phba: Pointer to HBA context object.
10317 *
10318 * When HBQ is enabled, buffers are searched based on tags. This function
10319 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10320 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10321 * does not conflict with tags of buffer posted for unsolicited events.
10322 * The function returns the allocated tag. The function is called with
10323 * no locks held.
10324 **/
76bb24ef
JS
10325uint32_t
10326lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10327{
10328 spin_lock_irq(&phba->hbalock);
10329 phba->buffer_tag_count++;
10330 /*
10331 * Always set the QUE_BUFTAG_BIT to distiguish between
10332 * a tag assigned by HBQ.
10333 */
10334 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10335 spin_unlock_irq(&phba->hbalock);
10336 return phba->buffer_tag_count;
10337}
10338
e59058c4 10339/**
3621a710 10340 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10341 * @phba: Pointer to HBA context object.
10342 * @pring: Pointer to driver SLI ring object.
10343 * @tag: Buffer tag.
10344 *
10345 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10346 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10347 * iocb is posted to the response ring with the tag of the buffer.
10348 * This function searches the pring->postbufq list using the tag
10349 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10350 * iocb. If the buffer is found then lpfc_dmabuf object of the
10351 * buffer is returned to the caller else NULL is returned.
10352 * This function is called with no lock held.
10353 **/
76bb24ef
JS
10354struct lpfc_dmabuf *
10355lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10356 uint32_t tag)
10357{
10358 struct lpfc_dmabuf *mp, *next_mp;
10359 struct list_head *slp = &pring->postbufq;
10360
25985edc 10361 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10362 spin_lock_irq(&phba->hbalock);
10363 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10364 if (mp->buffer_tag == tag) {
10365 list_del_init(&mp->list);
10366 pring->postbufq_cnt--;
10367 spin_unlock_irq(&phba->hbalock);
10368 return mp;
10369 }
10370 }
10371
10372 spin_unlock_irq(&phba->hbalock);
10373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10374 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10375 "ring %d Data x%lx x%p x%p x%x\n",
10376 pring->ringno, (unsigned long) tag,
10377 slp->next, slp->prev, pring->postbufq_cnt);
10378
10379 return NULL;
10380}
dea3101e 10381
e59058c4 10382/**
3621a710 10383 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10384 * @phba: Pointer to HBA context object.
10385 * @pring: Pointer to driver SLI ring object.
10386 * @phys: DMA address of the buffer.
10387 *
10388 * This function searches the buffer list using the dma_address
10389 * of unsolicited event to find the driver's lpfc_dmabuf object
10390 * corresponding to the dma_address. The function returns the
10391 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10392 * This function is called by the ct and els unsolicited event
10393 * handlers to get the buffer associated with the unsolicited
10394 * event.
10395 *
10396 * This function is called with no lock held.
10397 **/
dea3101e
JB
10398struct lpfc_dmabuf *
10399lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10400 dma_addr_t phys)
10401{
10402 struct lpfc_dmabuf *mp, *next_mp;
10403 struct list_head *slp = &pring->postbufq;
10404
25985edc 10405 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10406 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10407 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10408 if (mp->phys == phys) {
10409 list_del_init(&mp->list);
10410 pring->postbufq_cnt--;
2e0fef85 10411 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10412 return mp;
10413 }
10414 }
10415
2e0fef85 10416 spin_unlock_irq(&phba->hbalock);
dea3101e 10417 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10418 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10419 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10420 pring->ringno, (unsigned long long)phys,
dea3101e
JB
10421 slp->next, slp->prev, pring->postbufq_cnt);
10422 return NULL;
10423}
10424
e59058c4 10425/**
3621a710 10426 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10427 * @phba: Pointer to HBA context object.
10428 * @cmdiocb: Pointer to driver command iocb object.
10429 * @rspiocb: Pointer to driver response iocb object.
10430 *
10431 * This function is the completion handler for the abort iocbs for
10432 * ELS commands. This function is called from the ELS ring event
10433 * handler with no lock held. This function frees memory resources
10434 * associated with the abort iocb.
10435 **/
dea3101e 10436static void
2e0fef85
JS
10437lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10438 struct lpfc_iocbq *rspiocb)
dea3101e 10439{
2e0fef85 10440 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10441 uint16_t abort_iotag, abort_context;
ff78d8f9 10442 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10443
10444 if (irsp->ulpStatus) {
ff78d8f9
JS
10445
10446 /*
10447 * Assume that the port already completed and returned, or
10448 * will return the iocb. Just Log the message.
10449 */
2680eeaa
JS
10450 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10451 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10452
2e0fef85 10453 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10454 if (phba->sli_rev < LPFC_SLI_REV4) {
10455 if (abort_iotag != 0 &&
10456 abort_iotag <= phba->sli.last_iotag)
10457 abort_iocb =
10458 phba->sli.iocbq_lookup[abort_iotag];
10459 } else
10460 /* For sli4 the abort_tag is the XRI,
10461 * so the abort routine puts the iotag of the iocb
10462 * being aborted in the context field of the abort
10463 * IOCB.
10464 */
10465 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10466
2a9bf3d0
JS
10467 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10468 "0327 Cannot abort els iocb %p "
10469 "with tag %x context %x, abort status %x, "
10470 "abort code %x\n",
10471 abort_iocb, abort_iotag, abort_context,
10472 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10473
ff78d8f9 10474 spin_unlock_irq(&phba->hbalock);
2680eeaa 10475 }
604a3e30 10476 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
10477 return;
10478}
10479
e59058c4 10480/**
3621a710 10481 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10482 * @phba: Pointer to HBA context object.
10483 * @cmdiocb: Pointer to driver command iocb object.
10484 * @rspiocb: Pointer to driver response iocb object.
10485 *
10486 * The function is called from SLI ring event handler with no
10487 * lock held. This function is the completion handler for ELS commands
10488 * which are aborted. The function frees memory resources used for
10489 * the aborted ELS commands.
10490 **/
92d7f7b0
JS
10491static void
10492lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10493 struct lpfc_iocbq *rspiocb)
10494{
10495 IOCB_t *irsp = &rspiocb->iocb;
10496
10497 /* ELS cmd tag <ulpIoTag> completes */
10498 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10499 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10500 "x%x x%x x%x\n",
e8b62011 10501 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10502 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10503 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10504 lpfc_ct_free_iocb(phba, cmdiocb);
10505 else
10506 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10507 return;
10508}
10509
e59058c4 10510/**
5af5eee7 10511 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10512 * @phba: Pointer to HBA context object.
10513 * @pring: Pointer to driver SLI ring object.
10514 * @cmdiocb: Pointer to driver command iocb object.
10515 *
5af5eee7
JS
10516 * This function issues an abort iocb for the provided command iocb down to
10517 * the port. Other than the case the outstanding command iocb is an abort
10518 * request, this function issues abort out unconditionally. This function is
10519 * called with hbalock held. The function returns 0 when it fails due to
10520 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10521 **/
5af5eee7
JS
10522static int
10523lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10524 struct lpfc_iocbq *cmdiocb)
dea3101e 10525{
2e0fef85 10526 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10527 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
10528 IOCB_t *icmd = NULL;
10529 IOCB_t *iabt = NULL;
5af5eee7 10530 int retval;
7e56aa25 10531 unsigned long iflags;
07951076 10532
1c2ba475
JT
10533 lockdep_assert_held(&phba->hbalock);
10534
92d7f7b0
JS
10535 /*
10536 * There are certain command types we don't want to abort. And we
10537 * don't want to abort commands that are already in the process of
10538 * being aborted.
07951076
JS
10539 */
10540 icmd = &cmdiocb->iocb;
2e0fef85 10541 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10542 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10543 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10544 return 0;
10545
dea3101e 10546 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10547 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
10548 if (abtsiocbp == NULL)
10549 return 0;
dea3101e 10550
07951076 10551 /* This signals the response to set the correct status
341af102 10552 * before calling the completion handler
07951076
JS
10553 */
10554 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10555
dea3101e 10556 iabt = &abtsiocbp->iocb;
07951076
JS
10557 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10558 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10559 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10560 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10561 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10562 }
da0436e9
JS
10563 else
10564 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10565 iabt->ulpLe = 1;
10566 iabt->ulpClass = icmd->ulpClass;
dea3101e 10567
5ffc266e 10568 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10569 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10570 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10571 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10572 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10573 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10574
2e0fef85 10575 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10576 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10577 else
10578 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10579
07951076 10580 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10581 abtsiocbp->vport = vport;
5b8bd0c9 10582
e8b62011
JS
10583 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10584 "0339 Abort xri x%x, original iotag x%x, "
10585 "abort cmd iotag x%x\n",
2a9bf3d0 10586 iabt->un.acxri.abortIoTag,
e8b62011 10587 iabt->un.acxri.abortContextTag,
2a9bf3d0 10588 abtsiocbp->iotag);
7e56aa25
JS
10589
10590 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10591 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10592 if (unlikely(pring == NULL))
9bd2bff5 10593 return 0;
7e56aa25
JS
10594 /* Note: both hbalock and ring_lock need to be set here */
10595 spin_lock_irqsave(&pring->ring_lock, iflags);
10596 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10597 abtsiocbp, 0);
10598 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10599 } else {
10600 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10601 abtsiocbp, 0);
10602 }
dea3101e 10603
d7c255b2
JS
10604 if (retval)
10605 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10606
10607 /*
10608 * Caller to this routine should check for IOCB_ERROR
10609 * and handle it properly. This routine no longer removes
10610 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10611 */
10612 return retval;
10613}
10614
10615/**
10616 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10617 * @phba: Pointer to HBA context object.
10618 * @pring: Pointer to driver SLI ring object.
10619 * @cmdiocb: Pointer to driver command iocb object.
10620 *
10621 * This function issues an abort iocb for the provided command iocb. In case
10622 * of unloading, the abort iocb will not be issued to commands on the ELS
10623 * ring. Instead, the callback function shall be changed to those commands
10624 * so that nothing happens when them finishes. This function is called with
10625 * hbalock held. The function returns 0 when the command iocb is an abort
10626 * request.
10627 **/
10628int
10629lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10630 struct lpfc_iocbq *cmdiocb)
10631{
10632 struct lpfc_vport *vport = cmdiocb->vport;
10633 int retval = IOCB_ERROR;
10634 IOCB_t *icmd = NULL;
10635
1c2ba475
JT
10636 lockdep_assert_held(&phba->hbalock);
10637
5af5eee7
JS
10638 /*
10639 * There are certain command types we don't want to abort. And we
10640 * don't want to abort commands that are already in the process of
10641 * being aborted.
10642 */
10643 icmd = &cmdiocb->iocb;
10644 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10645 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10646 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10647 return 0;
10648
1234a6d5
DK
10649 if (!pring) {
10650 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10651 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10652 else
10653 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10654 goto abort_iotag_exit;
10655 }
10656
5af5eee7
JS
10657 /*
10658 * If we're unloading, don't abort iocb on the ELS ring, but change
10659 * the callback so that nothing happens when it finishes.
10660 */
10661 if ((vport->load_flag & FC_UNLOADING) &&
10662 (pring->ringno == LPFC_ELS_RING)) {
10663 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10664 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10665 else
10666 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10667 goto abort_iotag_exit;
10668 }
10669
10670 /* Now, we try to issue the abort to the cmdiocb out */
10671 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10672
07951076 10673abort_iotag_exit:
2e0fef85
JS
10674 /*
10675 * Caller to this routine should check for IOCB_ERROR
10676 * and handle it properly. This routine no longer removes
10677 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10678 */
2e0fef85 10679 return retval;
dea3101e
JB
10680}
10681
895427bd
JS
10682/**
10683 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10684 * @phba: Pointer to HBA context object.
10685 * @pring: Pointer to driver SLI ring object.
10686 * @cmdiocb: Pointer to driver command iocb object.
10687 *
10688 * This function issues an abort iocb for the provided command iocb down to
10689 * the port. Other than the case the outstanding command iocb is an abort
10690 * request, this function issues abort out unconditionally. This function is
10691 * called with hbalock held. The function returns 0 when it fails due to
10692 * memory allocation failure or when the command iocb is an abort request.
10693 **/
10694static int
10695lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10696 struct lpfc_iocbq *cmdiocb)
10697{
10698 struct lpfc_vport *vport = cmdiocb->vport;
10699 struct lpfc_iocbq *abtsiocbp;
10700 union lpfc_wqe *abts_wqe;
10701 int retval;
10702
10703 /*
10704 * There are certain command types we don't want to abort. And we
10705 * don't want to abort commands that are already in the process of
10706 * being aborted.
10707 */
10708 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10709 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10710 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10711 return 0;
10712
10713 /* issue ABTS for this io based on iotag */
10714 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10715 if (abtsiocbp == NULL)
10716 return 0;
10717
10718 /* This signals the response to set the correct status
10719 * before calling the completion handler
10720 */
10721 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10722
10723 /* Complete prepping the abort wqe and issue to the FW. */
10724 abts_wqe = &abtsiocbp->wqe;
10725 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10726 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10727
10728 /* Explicitly set reserved fields to zero.*/
10729 abts_wqe->abort_cmd.rsrvd4 = 0;
10730 abts_wqe->abort_cmd.rsrvd5 = 0;
10731
10732 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10733 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10734 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10735
10736 /* word 7 */
10737 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10738 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10739 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10740 cmdiocb->iocb.ulpClass);
10741
10742 /* word 8 - tell the FW to abort the IO associated with this
10743 * outstanding exchange ID.
10744 */
10745 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10746
10747 /* word 9 - this is the iotag for the abts_wqe completion. */
10748 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10749 abtsiocbp->iotag);
10750
10751 /* word 10 */
10752 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10753 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10754 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10755
10756 /* word 11 */
10757 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10758 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10759 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10760
10761 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10762 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10763 abtsiocbp->vport = vport;
01649561 10764 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 10765 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 10766 if (retval) {
895427bd
JS
10767 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10768 "6147 Failed abts issue_wqe with status x%x "
10769 "for oxid x%x\n",
10770 retval, cmdiocb->sli4_xritag);
10771 lpfc_sli_release_iocbq(phba, abtsiocbp);
10772 return retval;
10773 }
10774
10775 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10776 "6148 Drv Abort NVME Request Issued for "
10777 "ox_id x%x on reqtag x%x\n",
10778 cmdiocb->sli4_xritag,
10779 abtsiocbp->iotag);
10780
10781 return retval;
10782}
10783
5af5eee7
JS
10784/**
10785 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10786 * @phba: pointer to lpfc HBA data structure.
10787 *
10788 * This routine will abort all pending and outstanding iocbs to an HBA.
10789 **/
10790void
10791lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10792{
10793 struct lpfc_sli *psli = &phba->sli;
10794 struct lpfc_sli_ring *pring;
895427bd 10795 struct lpfc_queue *qp = NULL;
5af5eee7
JS
10796 int i;
10797
895427bd
JS
10798 if (phba->sli_rev != LPFC_SLI_REV4) {
10799 for (i = 0; i < psli->num_rings; i++) {
10800 pring = &psli->sli3_ring[i];
10801 lpfc_sli_abort_iocb_ring(phba, pring);
10802 }
10803 return;
10804 }
10805 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10806 pring = qp->pring;
10807 if (!pring)
10808 continue;
db55fba8 10809 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
10810 }
10811}
10812
e59058c4 10813/**
3621a710 10814 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
10815 * @iocbq: Pointer to driver iocb object.
10816 * @vport: Pointer to driver virtual port object.
10817 * @tgt_id: SCSI ID of the target.
10818 * @lun_id: LUN ID of the scsi device.
10819 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10820 *
3621a710 10821 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
10822 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10823 * 0 if the filtering criteria is met for the given iocb and will return
10824 * 1 if the filtering criteria is not met.
10825 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10826 * given iocb is for the SCSI device specified by vport, tgt_id and
10827 * lun_id parameter.
10828 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10829 * given iocb is for the SCSI target specified by vport and tgt_id
10830 * parameters.
10831 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10832 * given iocb is for the SCSI host associated with the given vport.
10833 * This function is called with no locks held.
10834 **/
dea3101e 10835static int
51ef4c26
JS
10836lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10837 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 10838 lpfc_ctx_cmd ctx_cmd)
dea3101e 10839{
0bd4ca25 10840 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
10841 int rc = 1;
10842
0bd4ca25
JSEC
10843 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10844 return rc;
10845
51ef4c26
JS
10846 if (iocbq->vport != vport)
10847 return rc;
10848
0bd4ca25 10849 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 10850
495a714c 10851 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
10852 return rc;
10853
10854 switch (ctx_cmd) {
10855 case LPFC_CTX_LUN:
495a714c
JS
10856 if ((lpfc_cmd->rdata->pnode) &&
10857 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10858 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
10859 rc = 0;
10860 break;
10861 case LPFC_CTX_TGT:
495a714c
JS
10862 if ((lpfc_cmd->rdata->pnode) &&
10863 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
10864 rc = 0;
10865 break;
dea3101e
JB
10866 case LPFC_CTX_HOST:
10867 rc = 0;
10868 break;
10869 default:
10870 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 10871 __func__, ctx_cmd);
dea3101e
JB
10872 break;
10873 }
10874
10875 return rc;
10876}
10877
e59058c4 10878/**
3621a710 10879 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
10880 * @vport: Pointer to virtual port.
10881 * @tgt_id: SCSI ID of the target.
10882 * @lun_id: LUN ID of the scsi device.
10883 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10884 *
10885 * This function returns number of FCP commands pending for the vport.
10886 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10887 * commands pending on the vport associated with SCSI device specified
10888 * by tgt_id and lun_id parameters.
10889 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10890 * commands pending on the vport associated with SCSI target specified
10891 * by tgt_id parameter.
10892 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10893 * commands pending on the vport.
10894 * This function returns the number of iocbs which satisfy the filter.
10895 * This function is called without any lock held.
10896 **/
dea3101e 10897int
51ef4c26
JS
10898lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10899 lpfc_ctx_cmd ctx_cmd)
dea3101e 10900{
51ef4c26 10901 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10902 struct lpfc_iocbq *iocbq;
10903 int sum, i;
dea3101e 10904
31979008 10905 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
10906 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10907 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10908
51ef4c26
JS
10909 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10910 ctx_cmd) == 0)
0bd4ca25 10911 sum++;
dea3101e 10912 }
31979008 10913 spin_unlock_irq(&phba->hbalock);
0bd4ca25 10914
dea3101e
JB
10915 return sum;
10916}
10917
e59058c4 10918/**
3621a710 10919 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
10920 * @phba: Pointer to HBA context object
10921 * @cmdiocb: Pointer to command iocb object.
10922 * @rspiocb: Pointer to response iocb object.
10923 *
10924 * This function is called when an aborted FCP iocb completes. This
10925 * function is called by the ring event handler with no lock held.
10926 * This function frees the iocb.
10927 **/
5eb95af0 10928void
2e0fef85
JS
10929lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10930 struct lpfc_iocbq *rspiocb)
5eb95af0 10931{
cb69f7de 10932 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 10933 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
10934 "original iotag x%x, abort cmd iotag x%x "
10935 "status 0x%x, reason 0x%x\n",
10936 cmdiocb->iocb.un.acxri.abortContextTag,
10937 cmdiocb->iocb.un.acxri.abortIoTag,
10938 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10939 rspiocb->iocb.un.ulpWord[4]);
604a3e30 10940 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
10941 return;
10942}
10943
e59058c4 10944/**
3621a710 10945 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
10946 * @vport: Pointer to virtual port.
10947 * @pring: Pointer to driver SLI ring object.
10948 * @tgt_id: SCSI ID of the target.
10949 * @lun_id: LUN ID of the scsi device.
10950 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10951 *
10952 * This function sends an abort command for every SCSI command
10953 * associated with the given virtual port pending on the ring
10954 * filtered by lpfc_sli_validate_fcp_iocb function.
10955 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10956 * FCP iocbs associated with lun specified by tgt_id and lun_id
10957 * parameters
10958 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10959 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10960 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10961 * FCP iocbs associated with virtual port.
10962 * This function returns number of iocbs it failed to abort.
10963 * This function is called with no locks held.
10964 **/
dea3101e 10965int
51ef4c26
JS
10966lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10967 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 10968{
51ef4c26 10969 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10970 struct lpfc_iocbq *iocbq;
10971 struct lpfc_iocbq *abtsiocb;
ecbb227e 10972 struct lpfc_sli_ring *pring_s4;
dea3101e 10973 IOCB_t *cmd = NULL;
dea3101e 10974 int errcnt = 0, ret_val = 0;
0bd4ca25 10975 int i;
dea3101e 10976
0bd4ca25
JSEC
10977 for (i = 1; i <= phba->sli.last_iotag; i++) {
10978 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10979
51ef4c26 10980 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 10981 abort_cmd) != 0)
dea3101e
JB
10982 continue;
10983
afbd8d88
JS
10984 /*
10985 * If the iocbq is already being aborted, don't take a second
10986 * action, but do count it.
10987 */
10988 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10989 continue;
10990
dea3101e 10991 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 10992 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
10993 if (abtsiocb == NULL) {
10994 errcnt++;
10995 continue;
10996 }
dea3101e 10997
afbd8d88
JS
10998 /* indicate the IO is being aborted by the driver. */
10999 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11000
0bd4ca25 11001 cmd = &iocbq->iocb;
dea3101e
JB
11002 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11003 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11004 if (phba->sli_rev == LPFC_SLI_REV4)
11005 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11006 else
11007 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
11008 abtsiocb->iocb.ulpLe = 1;
11009 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11010 abtsiocb->vport = vport;
dea3101e 11011
5ffc266e 11012 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11013 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11014 if (iocbq->iocb_flag & LPFC_IO_FCP)
11015 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11016 if (iocbq->iocb_flag & LPFC_IO_FOF)
11017 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11018
2e0fef85 11019 if (lpfc_is_link_up(phba))
dea3101e
JB
11020 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11021 else
11022 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11023
5eb95af0
JSEC
11024 /* Setup callback routine and issue the command. */
11025 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11026 if (phba->sli_rev == LPFC_SLI_REV4) {
11027 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11028 if (!pring_s4)
11029 continue;
11030 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11031 abtsiocb, 0);
11032 } else
11033 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11034 abtsiocb, 0);
dea3101e 11035 if (ret_val == IOCB_ERROR) {
604a3e30 11036 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
11037 errcnt++;
11038 continue;
11039 }
11040 }
11041
11042 return errcnt;
11043}
11044
98912dda
JS
11045/**
11046 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11047 * @vport: Pointer to virtual port.
11048 * @pring: Pointer to driver SLI ring object.
11049 * @tgt_id: SCSI ID of the target.
11050 * @lun_id: LUN ID of the scsi device.
11051 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11052 *
11053 * This function sends an abort command for every SCSI command
11054 * associated with the given virtual port pending on the ring
11055 * filtered by lpfc_sli_validate_fcp_iocb function.
11056 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11057 * FCP iocbs associated with lun specified by tgt_id and lun_id
11058 * parameters
11059 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11060 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11061 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11062 * FCP iocbs associated with virtual port.
11063 * This function returns number of iocbs it aborted .
11064 * This function is called with no locks held right after a taskmgmt
11065 * command is sent.
11066 **/
11067int
11068lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11069 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11070{
11071 struct lpfc_hba *phba = vport->phba;
8c50d25c 11072 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11073 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11074 struct lpfc_nodelist *ndlp;
98912dda
JS
11075 struct lpfc_iocbq *iocbq;
11076 IOCB_t *icmd;
11077 int sum, i, ret_val;
11078 unsigned long iflags;
11079 struct lpfc_sli_ring *pring_s4;
98912dda
JS
11080
11081 spin_lock_irq(&phba->hbalock);
11082
11083 /* all I/Os are in process of being flushed */
11084 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11085 spin_unlock_irq(&phba->hbalock);
11086 return 0;
11087 }
11088 sum = 0;
11089
11090 for (i = 1; i <= phba->sli.last_iotag; i++) {
11091 iocbq = phba->sli.iocbq_lookup[i];
11092
11093 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11094 cmd) != 0)
11095 continue;
11096
11097 /*
11098 * If the iocbq is already being aborted, don't take a second
11099 * action, but do count it.
11100 */
11101 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11102 continue;
11103
11104 /* issue ABTS for this IOCB based on iotag */
11105 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11106 if (abtsiocbq == NULL)
11107 continue;
11108
11109 icmd = &iocbq->iocb;
11110 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11111 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11112 if (phba->sli_rev == LPFC_SLI_REV4)
11113 abtsiocbq->iocb.un.acxri.abortIoTag =
11114 iocbq->sli4_xritag;
11115 else
11116 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11117 abtsiocbq->iocb.ulpLe = 1;
11118 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11119 abtsiocbq->vport = vport;
11120
11121 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11122 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11123 if (iocbq->iocb_flag & LPFC_IO_FCP)
11124 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11125 if (iocbq->iocb_flag & LPFC_IO_FOF)
11126 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11127
8c50d25c
JS
11128 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11129 ndlp = lpfc_cmd->rdata->pnode;
11130
11131 if (lpfc_is_link_up(phba) &&
11132 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11133 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11134 else
11135 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11136
11137 /* Setup callback routine and issue the command. */
11138 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11139
11140 /*
11141 * Indicate the IO is being aborted by the driver and set
11142 * the caller's flag into the aborted IO.
11143 */
11144 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11145
11146 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11147 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11148 if (pring_s4 == NULL)
11149 continue;
98912dda
JS
11150 /* Note: both hbalock and ring_lock must be set here */
11151 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11152 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11153 abtsiocbq, 0);
11154 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11155 } else {
11156 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11157 abtsiocbq, 0);
11158 }
11159
11160
11161 if (ret_val == IOCB_ERROR)
11162 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11163 else
11164 sum++;
11165 }
11166 spin_unlock_irq(&phba->hbalock);
11167 return sum;
11168}
11169
e59058c4 11170/**
3621a710 11171 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11172 * @phba: Pointer to HBA context object.
11173 * @cmdiocbq: Pointer to command iocb.
11174 * @rspiocbq: Pointer to response iocb.
11175 *
11176 * This function is the completion handler for iocbs issued using
11177 * lpfc_sli_issue_iocb_wait function. This function is called by the
11178 * ring event handler function without any lock held. This function
11179 * can be called from both worker thread context and interrupt
11180 * context. This function also can be called from other thread which
11181 * cleans up the SLI layer objects.
11182 * This function copy the contents of the response iocb to the
11183 * response iocb memory object provided by the caller of
11184 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11185 * sleeps for the iocb completion.
11186 **/
68876920
JSEC
11187static void
11188lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11189 struct lpfc_iocbq *cmdiocbq,
11190 struct lpfc_iocbq *rspiocbq)
dea3101e 11191{
68876920
JSEC
11192 wait_queue_head_t *pdone_q;
11193 unsigned long iflags;
0f65ff68 11194 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11195
2e0fef85 11196 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11197 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11198
11199 /*
11200 * A time out has occurred for the iocb. If a time out
11201 * completion handler has been supplied, call it. Otherwise,
11202 * just free the iocbq.
11203 */
11204
11205 spin_unlock_irqrestore(&phba->hbalock, iflags);
11206 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11207 cmdiocbq->wait_iocb_cmpl = NULL;
11208 if (cmdiocbq->iocb_cmpl)
11209 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11210 else
11211 lpfc_sli_release_iocbq(phba, cmdiocbq);
11212 return;
11213 }
11214
68876920
JSEC
11215 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11216 if (cmdiocbq->context2 && rspiocbq)
11217 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11218 &rspiocbq->iocb, sizeof(IOCB_t));
11219
0f65ff68
JS
11220 /* Set the exchange busy flag for task management commands */
11221 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11222 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11223 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11224 cur_iocbq);
11225 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11226 }
11227
68876920 11228 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11229 if (pdone_q)
11230 wake_up(pdone_q);
858c9f6c 11231 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11232 return;
11233}
11234
d11e31dd
JS
11235/**
11236 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11237 * @phba: Pointer to HBA context object..
11238 * @piocbq: Pointer to command iocb.
11239 * @flag: Flag to test.
11240 *
11241 * This routine grabs the hbalock and then test the iocb_flag to
11242 * see if the passed in flag is set.
11243 * Returns:
11244 * 1 if flag is set.
11245 * 0 if flag is not set.
11246 **/
11247static int
11248lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11249 struct lpfc_iocbq *piocbq, uint32_t flag)
11250{
11251 unsigned long iflags;
11252 int ret;
11253
11254 spin_lock_irqsave(&phba->hbalock, iflags);
11255 ret = piocbq->iocb_flag & flag;
11256 spin_unlock_irqrestore(&phba->hbalock, iflags);
11257 return ret;
11258
11259}
11260
e59058c4 11261/**
3621a710 11262 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11263 * @phba: Pointer to HBA context object..
11264 * @pring: Pointer to sli ring.
11265 * @piocb: Pointer to command iocb.
11266 * @prspiocbq: Pointer to response iocb.
11267 * @timeout: Timeout in number of seconds.
11268 *
11269 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11270 * iocb to complete. The iocb_cmpl field of the shall be used
11271 * to handle iocbs which time out. If the field is NULL, the
11272 * function shall free the iocbq structure. If more clean up is
11273 * needed, the caller is expected to provide a completion function
11274 * that will provide the needed clean up. If the iocb command is
11275 * not completed within timeout seconds, the function will either
11276 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11277 * completion function set in the iocb_cmpl field and then return
11278 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11279 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11280 * The function waits for the iocb completion using an
11281 * non-interruptible wait.
11282 * This function will sleep while waiting for iocb completion.
11283 * So, this function should not be called from any context which
11284 * does not allow sleeping. Due to the same reason, this function
11285 * cannot be called with interrupt disabled.
11286 * This function assumes that the iocb completions occur while
11287 * this function sleep. So, this function cannot be called from
11288 * the thread which process iocb completion for this ring.
11289 * This function clears the iocb_flag of the iocb object before
11290 * issuing the iocb and the iocb completion handler sets this
11291 * flag and wakes this thread when the iocb completes.
11292 * The contents of the response iocb will be copied to prspiocbq
11293 * by the completion handler when the command completes.
11294 * This function returns IOCB_SUCCESS when success.
11295 * This function is called with no lock held.
11296 **/
dea3101e 11297int
2e0fef85 11298lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11299 uint32_t ring_number,
2e0fef85
JS
11300 struct lpfc_iocbq *piocb,
11301 struct lpfc_iocbq *prspiocbq,
68876920 11302 uint32_t timeout)
dea3101e 11303{
7259f0d0 11304 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11305 long timeleft, timeout_req = 0;
11306 int retval = IOCB_SUCCESS;
875fbdfe 11307 uint32_t creg_val;
0e9bb8d7
JS
11308 struct lpfc_iocbq *iocb;
11309 int txq_cnt = 0;
11310 int txcmplq_cnt = 0;
895427bd 11311 struct lpfc_sli_ring *pring;
5a0916b4
JS
11312 unsigned long iflags;
11313 bool iocb_completed = true;
11314
895427bd
JS
11315 if (phba->sli_rev >= LPFC_SLI_REV4)
11316 pring = lpfc_sli4_calc_ring(phba, piocb);
11317 else
11318 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11319 /*
68876920
JSEC
11320 * If the caller has provided a response iocbq buffer, then context2
11321 * is NULL or its an error.
dea3101e 11322 */
68876920
JSEC
11323 if (prspiocbq) {
11324 if (piocb->context2)
11325 return IOCB_ERROR;
11326 piocb->context2 = prspiocbq;
dea3101e
JB
11327 }
11328
5a0916b4 11329 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11330 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11331 piocb->context_un.wait_queue = &done_q;
5a0916b4 11332 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11333
875fbdfe 11334 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11335 if (lpfc_readl(phba->HCregaddr, &creg_val))
11336 return IOCB_ERROR;
875fbdfe
JSEC
11337 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11338 writel(creg_val, phba->HCregaddr);
11339 readl(phba->HCregaddr); /* flush */
11340 }
11341
2a9bf3d0
JS
11342 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11343 SLI_IOCB_RET_IOCB);
68876920 11344 if (retval == IOCB_SUCCESS) {
256ec0d0 11345 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11346 timeleft = wait_event_timeout(done_q,
d11e31dd 11347 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11348 timeout_req);
5a0916b4
JS
11349 spin_lock_irqsave(&phba->hbalock, iflags);
11350 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11351
11352 /*
11353 * IOCB timed out. Inform the wake iocb wait
11354 * completion function and set local status
11355 */
dea3101e 11356
5a0916b4
JS
11357 iocb_completed = false;
11358 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11359 }
11360 spin_unlock_irqrestore(&phba->hbalock, iflags);
11361 if (iocb_completed) {
7054a606 11362 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11363 "0331 IOCB wake signaled\n");
53151bbb
JS
11364 /* Note: we are not indicating if the IOCB has a success
11365 * status or not - that's for the caller to check.
11366 * IOCB_SUCCESS means just that the command was sent and
11367 * completed. Not that it completed successfully.
11368 * */
7054a606 11369 } else if (timeleft == 0) {
68876920 11370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11371 "0338 IOCB wait timeout error - no "
11372 "wake response Data x%x\n", timeout);
68876920 11373 retval = IOCB_TIMEDOUT;
7054a606 11374 } else {
68876920 11375 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11376 "0330 IOCB wake NOT set, "
11377 "Data x%x x%lx\n",
68876920
JSEC
11378 timeout, (timeleft / jiffies));
11379 retval = IOCB_TIMEDOUT;
dea3101e 11380 }
2a9bf3d0 11381 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11382 if (phba->cfg_log_verbose & LOG_SLI) {
11383 list_for_each_entry(iocb, &pring->txq, list) {
11384 txq_cnt++;
11385 }
11386 list_for_each_entry(iocb, &pring->txcmplq, list) {
11387 txcmplq_cnt++;
11388 }
11389 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11390 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11391 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11392 }
2a9bf3d0 11393 return retval;
68876920
JSEC
11394 } else {
11395 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11396 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11397 retval);
68876920 11398 retval = IOCB_ERROR;
dea3101e
JB
11399 }
11400
875fbdfe 11401 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11402 if (lpfc_readl(phba->HCregaddr, &creg_val))
11403 return IOCB_ERROR;
875fbdfe
JSEC
11404 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11405 writel(creg_val, phba->HCregaddr);
11406 readl(phba->HCregaddr); /* flush */
11407 }
11408
68876920
JSEC
11409 if (prspiocbq)
11410 piocb->context2 = NULL;
11411
11412 piocb->context_un.wait_queue = NULL;
11413 piocb->iocb_cmpl = NULL;
dea3101e
JB
11414 return retval;
11415}
68876920 11416
e59058c4 11417/**
3621a710 11418 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11419 * @phba: Pointer to HBA context object.
11420 * @pmboxq: Pointer to driver mailbox object.
11421 * @timeout: Timeout in number of seconds.
11422 *
11423 * This function issues the mailbox to firmware and waits for the
11424 * mailbox command to complete. If the mailbox command is not
11425 * completed within timeout seconds, it returns MBX_TIMEOUT.
11426 * The function waits for the mailbox completion using an
11427 * interruptible wait. If the thread is woken up due to a
11428 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11429 * should not free the mailbox resources, if this function returns
11430 * MBX_TIMEOUT.
11431 * This function will sleep while waiting for mailbox completion.
11432 * So, this function should not be called from any context which
11433 * does not allow sleeping. Due to the same reason, this function
11434 * cannot be called with interrupt disabled.
11435 * This function assumes that the mailbox completion occurs while
11436 * this function sleep. So, this function cannot be called from
11437 * the worker thread which processes mailbox completion.
11438 * This function is called in the context of HBA management
11439 * applications.
11440 * This function returns MBX_SUCCESS when successful.
11441 * This function is called with no lock held.
11442 **/
dea3101e 11443int
2e0fef85 11444lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
11445 uint32_t timeout)
11446{
7259f0d0 11447 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 11448 MAILBOX_t *mb = NULL;
dea3101e 11449 int retval;
858c9f6c 11450 unsigned long flag;
dea3101e 11451
b230b8a2 11452 /* The caller might set context1 for extended buffer */
98c9ea5c 11453 if (pmboxq->context1)
b230b8a2 11454 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 11455
495a714c 11456 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11457 /* setup wake call as IOCB callback */
11458 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11459 /* setup context field to pass wait_queue pointer to wake function */
11460 pmboxq->context1 = &done_q;
11461
dea3101e
JB
11462 /* now issue the command */
11463 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11464 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
11465 wait_event_interruptible_timeout(done_q,
11466 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 11467 msecs_to_jiffies(timeout * 1000));
7054a606 11468
858c9f6c 11469 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
11470 /* restore the possible extended buffer for free resource */
11471 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
11472 /*
11473 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11474 * else do not free the resources.
11475 */
d7c47992 11476 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11477 retval = MBX_SUCCESS;
d7c47992 11478 } else {
7054a606 11479 retval = MBX_TIMEOUT;
858c9f6c
JS
11480 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11481 }
11482 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
11483 } else {
11484 /* restore the possible extended buffer for free resource */
11485 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
11486 }
11487
dea3101e
JB
11488 return retval;
11489}
11490
e59058c4 11491/**
3772a991 11492 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11493 * @phba: Pointer to HBA context.
11494 *
3772a991
JS
11495 * This function is called to shutdown the driver's mailbox sub-system.
11496 * It first marks the mailbox sub-system is in a block state to prevent
11497 * the asynchronous mailbox command from issued off the pending mailbox
11498 * command queue. If the mailbox command sub-system shutdown is due to
11499 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11500 * the mailbox sub-system flush routine to forcefully bring down the
11501 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11502 * as with offline or HBA function reset), this routine will wait for the
11503 * outstanding mailbox command to complete before invoking the mailbox
11504 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11505 **/
3772a991 11506void
618a5230 11507lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11508{
3772a991 11509 struct lpfc_sli *psli = &phba->sli;
3772a991 11510 unsigned long timeout;
b4c02652 11511
618a5230
JS
11512 if (mbx_action == LPFC_MBX_NO_WAIT) {
11513 /* delay 100ms for port state */
11514 msleep(100);
11515 lpfc_sli_mbox_sys_flush(phba);
11516 return;
11517 }
a183a15f 11518 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11519
3772a991
JS
11520 spin_lock_irq(&phba->hbalock);
11521 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11522
3772a991 11523 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11524 /* Determine how long we might wait for the active mailbox
11525 * command to be gracefully completed by firmware.
11526 */
a183a15f
JS
11527 if (phba->sli.mbox_active)
11528 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11529 phba->sli.mbox_active) *
11530 1000) + jiffies;
11531 spin_unlock_irq(&phba->hbalock);
11532
3772a991
JS
11533 while (phba->sli.mbox_active) {
11534 /* Check active mailbox complete status every 2ms */
11535 msleep(2);
11536 if (time_after(jiffies, timeout))
11537 /* Timeout, let the mailbox flush routine to
11538 * forcefully release active mailbox command
11539 */
11540 break;
11541 }
d7069f09
JS
11542 } else
11543 spin_unlock_irq(&phba->hbalock);
11544
3772a991
JS
11545 lpfc_sli_mbox_sys_flush(phba);
11546}
ed957684 11547
3772a991
JS
11548/**
11549 * lpfc_sli_eratt_read - read sli-3 error attention events
11550 * @phba: Pointer to HBA context.
11551 *
11552 * This function is called to read the SLI3 device error attention registers
11553 * for possible error attention events. The caller must hold the hostlock
11554 * with spin_lock_irq().
11555 *
25985edc 11556 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11557 * Register and returns 0 otherwise.
11558 **/
11559static int
11560lpfc_sli_eratt_read(struct lpfc_hba *phba)
11561{
11562 uint32_t ha_copy;
b4c02652 11563
3772a991 11564 /* Read chip Host Attention (HA) register */
9940b97b
JS
11565 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11566 goto unplug_err;
11567
3772a991
JS
11568 if (ha_copy & HA_ERATT) {
11569 /* Read host status register to retrieve error event */
9940b97b
JS
11570 if (lpfc_sli_read_hs(phba))
11571 goto unplug_err;
b4c02652 11572
3772a991
JS
11573 /* Check if there is a deferred error condition is active */
11574 if ((HS_FFER1 & phba->work_hs) &&
11575 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11576 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11577 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11578 /* Clear all interrupt enable conditions */
11579 writel(0, phba->HCregaddr);
11580 readl(phba->HCregaddr);
11581 }
11582
11583 /* Set the driver HA work bitmap */
3772a991
JS
11584 phba->work_ha |= HA_ERATT;
11585 /* Indicate polling handles this ERATT */
11586 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11587 return 1;
11588 }
11589 return 0;
9940b97b
JS
11590
11591unplug_err:
11592 /* Set the driver HS work bitmap */
11593 phba->work_hs |= UNPLUG_ERR;
11594 /* Set the driver HA work bitmap */
11595 phba->work_ha |= HA_ERATT;
11596 /* Indicate polling handles this ERATT */
11597 phba->hba_flag |= HBA_ERATT_HANDLED;
11598 return 1;
b4c02652
JS
11599}
11600
da0436e9
JS
11601/**
11602 * lpfc_sli4_eratt_read - read sli-4 error attention events
11603 * @phba: Pointer to HBA context.
11604 *
11605 * This function is called to read the SLI4 device error attention registers
11606 * for possible error attention events. The caller must hold the hostlock
11607 * with spin_lock_irq().
11608 *
25985edc 11609 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11610 * Register and returns 0 otherwise.
11611 **/
11612static int
11613lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11614{
11615 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11616 uint32_t if_type, portsmphr;
11617 struct lpfc_register portstat_reg;
da0436e9 11618
2fcee4bf
JS
11619 /*
11620 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11621 * registers for error attention. This can be changed later.
11622 */
2fcee4bf
JS
11623 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11624 switch (if_type) {
11625 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11626 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11627 &uerr_sta_lo) ||
11628 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11629 &uerr_sta_hi)) {
11630 phba->work_hs |= UNPLUG_ERR;
11631 phba->work_ha |= HA_ERATT;
11632 phba->hba_flag |= HBA_ERATT_HANDLED;
11633 return 1;
11634 }
2fcee4bf
JS
11635 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11636 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11638 "1423 HBA Unrecoverable error: "
11639 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11640 "ue_mask_lo_reg=0x%x, "
11641 "ue_mask_hi_reg=0x%x\n",
11642 uerr_sta_lo, uerr_sta_hi,
11643 phba->sli4_hba.ue_mask_lo,
11644 phba->sli4_hba.ue_mask_hi);
11645 phba->work_status[0] = uerr_sta_lo;
11646 phba->work_status[1] = uerr_sta_hi;
11647 phba->work_ha |= HA_ERATT;
11648 phba->hba_flag |= HBA_ERATT_HANDLED;
11649 return 1;
11650 }
11651 break;
11652 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
11653 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11654 &portstat_reg.word0) ||
11655 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11656 &portsmphr)){
11657 phba->work_hs |= UNPLUG_ERR;
11658 phba->work_ha |= HA_ERATT;
11659 phba->hba_flag |= HBA_ERATT_HANDLED;
11660 return 1;
11661 }
2fcee4bf
JS
11662 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11663 phba->work_status[0] =
11664 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11665 phba->work_status[1] =
11666 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11668 "2885 Port Status Event: "
2fcee4bf
JS
11669 "port status reg 0x%x, "
11670 "port smphr reg 0x%x, "
11671 "error 1=0x%x, error 2=0x%x\n",
11672 portstat_reg.word0,
11673 portsmphr,
11674 phba->work_status[0],
11675 phba->work_status[1]);
11676 phba->work_ha |= HA_ERATT;
11677 phba->hba_flag |= HBA_ERATT_HANDLED;
11678 return 1;
11679 }
11680 break;
11681 case LPFC_SLI_INTF_IF_TYPE_1:
11682 default:
a747c9ce 11683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11684 "2886 HBA Error Attention on unsupported "
11685 "if type %d.", if_type);
a747c9ce 11686 return 1;
da0436e9 11687 }
2fcee4bf 11688
da0436e9
JS
11689 return 0;
11690}
11691
e59058c4 11692/**
3621a710 11693 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11694 * @phba: Pointer to HBA context.
11695 *
3772a991 11696 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11697 * error attention register bit for error attention events.
11698 *
25985edc 11699 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11700 * Register and returns 0 otherwise.
11701 **/
11702int
11703lpfc_sli_check_eratt(struct lpfc_hba *phba)
11704{
11705 uint32_t ha_copy;
11706
11707 /* If somebody is waiting to handle an eratt, don't process it
11708 * here. The brdkill function will do this.
11709 */
11710 if (phba->link_flag & LS_IGNORE_ERATT)
11711 return 0;
11712
11713 /* Check if interrupt handler handles this ERATT */
11714 spin_lock_irq(&phba->hbalock);
11715 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11716 /* Interrupt handler has handled ERATT */
11717 spin_unlock_irq(&phba->hbalock);
11718 return 0;
11719 }
11720
a257bf90
JS
11721 /*
11722 * If there is deferred error attention, do not check for error
11723 * attention
11724 */
11725 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11726 spin_unlock_irq(&phba->hbalock);
11727 return 0;
11728 }
11729
3772a991
JS
11730 /* If PCI channel is offline, don't process it */
11731 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11732 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11733 return 0;
11734 }
11735
11736 switch (phba->sli_rev) {
11737 case LPFC_SLI_REV2:
11738 case LPFC_SLI_REV3:
11739 /* Read chip Host Attention (HA) register */
11740 ha_copy = lpfc_sli_eratt_read(phba);
11741 break;
da0436e9 11742 case LPFC_SLI_REV4:
2fcee4bf 11743 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11744 ha_copy = lpfc_sli4_eratt_read(phba);
11745 break;
3772a991
JS
11746 default:
11747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11748 "0299 Invalid SLI revision (%d)\n",
11749 phba->sli_rev);
11750 ha_copy = 0;
11751 break;
9399627f
JS
11752 }
11753 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11754
11755 return ha_copy;
11756}
11757
11758/**
11759 * lpfc_intr_state_check - Check device state for interrupt handling
11760 * @phba: Pointer to HBA context.
11761 *
11762 * This inline routine checks whether a device or its PCI slot is in a state
11763 * that the interrupt should be handled.
11764 *
11765 * This function returns 0 if the device or the PCI slot is in a state that
11766 * interrupt should be handled, otherwise -EIO.
11767 */
11768static inline int
11769lpfc_intr_state_check(struct lpfc_hba *phba)
11770{
11771 /* If the pci channel is offline, ignore all the interrupts */
11772 if (unlikely(pci_channel_offline(phba->pcidev)))
11773 return -EIO;
11774
11775 /* Update device level interrupt statistics */
11776 phba->sli.slistat.sli_intr++;
11777
11778 /* Ignore all interrupts during initialization. */
11779 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11780 return -EIO;
11781
9399627f
JS
11782 return 0;
11783}
11784
11785/**
3772a991 11786 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11787 * @irq: Interrupt number.
11788 * @dev_id: The device context pointer.
11789 *
9399627f 11790 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11791 * service routine when device with SLI-3 interface spec is enabled with
11792 * MSI-X multi-message interrupt mode and there are slow-path events in
11793 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11794 * interrupt mode, this function is called as part of the device-level
11795 * interrupt handler. When the PCI slot is in error recovery or the HBA
11796 * is undergoing initialization, the interrupt handler will not process
11797 * the interrupt. The link attention and ELS ring attention events are
11798 * handled by the worker thread. The interrupt handler signals the worker
11799 * thread and returns for these events. This function is called without
11800 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
11801 * structures.
11802 *
11803 * This function returns IRQ_HANDLED when interrupt is handled else it
11804 * returns IRQ_NONE.
e59058c4 11805 **/
dea3101e 11806irqreturn_t
3772a991 11807lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 11808{
2e0fef85 11809 struct lpfc_hba *phba;
a747c9ce 11810 uint32_t ha_copy, hc_copy;
dea3101e
JB
11811 uint32_t work_ha_copy;
11812 unsigned long status;
5b75da2f 11813 unsigned long iflag;
dea3101e
JB
11814 uint32_t control;
11815
92d7f7b0 11816 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
11817 struct lpfc_vport *vport;
11818 struct lpfc_nodelist *ndlp;
11819 struct lpfc_dmabuf *mp;
92d7f7b0
JS
11820 LPFC_MBOXQ_t *pmb;
11821 int rc;
11822
dea3101e
JB
11823 /*
11824 * Get the driver's phba structure from the dev_id and
11825 * assume the HBA is not interrupting.
11826 */
9399627f 11827 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
11828
11829 if (unlikely(!phba))
11830 return IRQ_NONE;
11831
dea3101e 11832 /*
9399627f
JS
11833 * Stuff needs to be attented to when this function is invoked as an
11834 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 11835 */
9399627f 11836 if (phba->intr_type == MSIX) {
3772a991
JS
11837 /* Check device state for handling interrupt */
11838 if (lpfc_intr_state_check(phba))
9399627f
JS
11839 return IRQ_NONE;
11840 /* Need to read HA REG for slow-path events */
5b75da2f 11841 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11842 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11843 goto unplug_error;
9399627f
JS
11844 /* If somebody is waiting to handle an eratt don't process it
11845 * here. The brdkill function will do this.
11846 */
11847 if (phba->link_flag & LS_IGNORE_ERATT)
11848 ha_copy &= ~HA_ERATT;
11849 /* Check the need for handling ERATT in interrupt handler */
11850 if (ha_copy & HA_ERATT) {
11851 if (phba->hba_flag & HBA_ERATT_HANDLED)
11852 /* ERATT polling has handled ERATT */
11853 ha_copy &= ~HA_ERATT;
11854 else
11855 /* Indicate interrupt handler handles ERATT */
11856 phba->hba_flag |= HBA_ERATT_HANDLED;
11857 }
a257bf90
JS
11858
11859 /*
11860 * If there is deferred error attention, do not check for any
11861 * interrupt.
11862 */
11863 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 11864 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
11865 return IRQ_NONE;
11866 }
11867
9399627f 11868 /* Clear up only attention source related to slow-path */
9940b97b
JS
11869 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11870 goto unplug_error;
11871
a747c9ce
JS
11872 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11873 HC_LAINT_ENA | HC_ERINT_ENA),
11874 phba->HCregaddr);
9399627f
JS
11875 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11876 phba->HAregaddr);
a747c9ce 11877 writel(hc_copy, phba->HCregaddr);
9399627f 11878 readl(phba->HAregaddr); /* flush */
5b75da2f 11879 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
11880 } else
11881 ha_copy = phba->ha_copy;
dea3101e 11882
dea3101e
JB
11883 work_ha_copy = ha_copy & phba->work_ha_mask;
11884
9399627f 11885 if (work_ha_copy) {
dea3101e
JB
11886 if (work_ha_copy & HA_LATT) {
11887 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11888 /*
11889 * Turn off Link Attention interrupts
11890 * until CLEAR_LA done
11891 */
5b75da2f 11892 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11893 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
11894 if (lpfc_readl(phba->HCregaddr, &control))
11895 goto unplug_error;
dea3101e
JB
11896 control &= ~HC_LAINT_ENA;
11897 writel(control, phba->HCregaddr);
11898 readl(phba->HCregaddr); /* flush */
5b75da2f 11899 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11900 }
11901 else
11902 work_ha_copy &= ~HA_LATT;
11903 }
11904
9399627f 11905 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
11906 /*
11907 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11908 * the only slow ring.
11909 */
11910 status = (work_ha_copy &
11911 (HA_RXMASK << (4*LPFC_ELS_RING)));
11912 status >>= (4*LPFC_ELS_RING);
11913 if (status & HA_RXMASK) {
5b75da2f 11914 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11915 if (lpfc_readl(phba->HCregaddr, &control))
11916 goto unplug_error;
a58cbd52
JS
11917
11918 lpfc_debugfs_slow_ring_trc(phba,
11919 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11920 control, status,
11921 (uint32_t)phba->sli.slistat.sli_intr);
11922
858c9f6c 11923 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
11924 lpfc_debugfs_slow_ring_trc(phba,
11925 "ISR Disable ring:"
11926 "pwork:x%x hawork:x%x wait:x%x",
11927 phba->work_ha, work_ha_copy,
11928 (uint32_t)((unsigned long)
5e9d9b82 11929 &phba->work_waitq));
a58cbd52 11930
858c9f6c
JS
11931 control &=
11932 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
11933 writel(control, phba->HCregaddr);
11934 readl(phba->HCregaddr); /* flush */
dea3101e 11935 }
a58cbd52
JS
11936 else {
11937 lpfc_debugfs_slow_ring_trc(phba,
11938 "ISR slow ring: pwork:"
11939 "x%x hawork:x%x wait:x%x",
11940 phba->work_ha, work_ha_copy,
11941 (uint32_t)((unsigned long)
5e9d9b82 11942 &phba->work_waitq));
a58cbd52 11943 }
5b75da2f 11944 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11945 }
11946 }
5b75da2f 11947 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 11948 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
11949 if (lpfc_sli_read_hs(phba))
11950 goto unplug_error;
a257bf90
JS
11951 /*
11952 * Check if there is a deferred error condition
11953 * is active
11954 */
11955 if ((HS_FFER1 & phba->work_hs) &&
11956 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
11957 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11958 phba->work_hs)) {
a257bf90
JS
11959 phba->hba_flag |= DEFER_ERATT;
11960 /* Clear all interrupt enable conditions */
11961 writel(0, phba->HCregaddr);
11962 readl(phba->HCregaddr);
11963 }
11964 }
11965
9399627f 11966 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 11967 pmb = phba->sli.mbox_active;
04c68496 11968 pmbox = &pmb->u.mb;
34b02dcd 11969 mbox = phba->mbox;
858c9f6c 11970 vport = pmb->vport;
92d7f7b0
JS
11971
11972 /* First check out the status word */
11973 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11974 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 11975 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
11976 /*
11977 * Stray Mailbox Interrupt, mbxCommand <cmd>
11978 * mbxStatus <status>
11979 */
09372820 11980 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 11981 LOG_SLI,
e8b62011 11982 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
11983 "Interrupt mbxCommand x%x "
11984 "mbxStatus x%x\n",
e8b62011 11985 (vport ? vport->vpi : 0),
92d7f7b0
JS
11986 pmbox->mbxCommand,
11987 pmbox->mbxStatus);
09372820
JS
11988 /* clear mailbox attention bit */
11989 work_ha_copy &= ~HA_MBATT;
11990 } else {
97eab634 11991 phba->sli.mbox_active = NULL;
5b75da2f 11992 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
11993 phba->last_completion_time = jiffies;
11994 del_timer(&phba->sli.mbox_tmo);
09372820
JS
11995 if (pmb->mbox_cmpl) {
11996 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11997 MAILBOX_CMD_SIZE);
7a470277
JS
11998 if (pmb->out_ext_byte_len &&
11999 pmb->context2)
12000 lpfc_sli_pcimem_bcopy(
12001 phba->mbox_ext,
12002 pmb->context2,
12003 pmb->out_ext_byte_len);
09372820
JS
12004 }
12005 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12006 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12007
12008 lpfc_debugfs_disc_trc(vport,
12009 LPFC_DISC_TRC_MBOX_VPORT,
12010 "MBOX dflt rpi: : "
12011 "status:x%x rpi:x%x",
12012 (uint32_t)pmbox->mbxStatus,
12013 pmbox->un.varWords[0], 0);
12014
12015 if (!pmbox->mbxStatus) {
12016 mp = (struct lpfc_dmabuf *)
12017 (pmb->context1);
12018 ndlp = (struct lpfc_nodelist *)
12019 pmb->context2;
12020
12021 /* Reg_LOGIN of dflt RPI was
12022 * successful. new lets get
12023 * rid of the RPI using the
12024 * same mbox buffer.
12025 */
12026 lpfc_unreg_login(phba,
12027 vport->vpi,
12028 pmbox->un.varWords[0],
12029 pmb);
12030 pmb->mbox_cmpl =
12031 lpfc_mbx_cmpl_dflt_rpi;
12032 pmb->context1 = mp;
12033 pmb->context2 = ndlp;
12034 pmb->vport = vport;
58da1ffb
JS
12035 rc = lpfc_sli_issue_mbox(phba,
12036 pmb,
12037 MBX_NOWAIT);
12038 if (rc != MBX_BUSY)
12039 lpfc_printf_log(phba,
12040 KERN_ERR,
12041 LOG_MBOX | LOG_SLI,
d7c255b2 12042 "0350 rc should have"
6a9c52cf 12043 "been MBX_BUSY\n");
3772a991
JS
12044 if (rc != MBX_NOT_FINISHED)
12045 goto send_current_mbox;
09372820 12046 }
858c9f6c 12047 }
5b75da2f
JS
12048 spin_lock_irqsave(
12049 &phba->pport->work_port_lock,
12050 iflag);
09372820
JS
12051 phba->pport->work_port_events &=
12052 ~WORKER_MBOX_TMO;
5b75da2f
JS
12053 spin_unlock_irqrestore(
12054 &phba->pport->work_port_lock,
12055 iflag);
09372820 12056 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12057 }
97eab634 12058 } else
5b75da2f 12059 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12060
92d7f7b0
JS
12061 if ((work_ha_copy & HA_MBATT) &&
12062 (phba->sli.mbox_active == NULL)) {
858c9f6c 12063send_current_mbox:
92d7f7b0 12064 /* Process next mailbox command if there is one */
58da1ffb
JS
12065 do {
12066 rc = lpfc_sli_issue_mbox(phba, NULL,
12067 MBX_NOWAIT);
12068 } while (rc == MBX_NOT_FINISHED);
12069 if (rc != MBX_SUCCESS)
12070 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12071 LOG_SLI, "0349 rc should be "
6a9c52cf 12072 "MBX_SUCCESS\n");
92d7f7b0
JS
12073 }
12074
5b75da2f 12075 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12076 phba->work_ha |= work_ha_copy;
5b75da2f 12077 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12078 lpfc_worker_wake_up(phba);
dea3101e 12079 }
9399627f 12080 return IRQ_HANDLED;
9940b97b
JS
12081unplug_error:
12082 spin_unlock_irqrestore(&phba->hbalock, iflag);
12083 return IRQ_HANDLED;
dea3101e 12084
3772a991 12085} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12086
12087/**
3772a991 12088 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12089 * @irq: Interrupt number.
12090 * @dev_id: The device context pointer.
12091 *
12092 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12093 * service routine when device with SLI-3 interface spec is enabled with
12094 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12095 * ring event in the HBA. However, when the device is enabled with either
12096 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12097 * device-level interrupt handler. When the PCI slot is in error recovery
12098 * or the HBA is undergoing initialization, the interrupt handler will not
12099 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12100 * the intrrupt context. This function is called without any lock held.
12101 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12102 *
12103 * This function returns IRQ_HANDLED when interrupt is handled else it
12104 * returns IRQ_NONE.
12105 **/
12106irqreturn_t
3772a991 12107lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12108{
12109 struct lpfc_hba *phba;
12110 uint32_t ha_copy;
12111 unsigned long status;
5b75da2f 12112 unsigned long iflag;
895427bd 12113 struct lpfc_sli_ring *pring;
9399627f
JS
12114
12115 /* Get the driver's phba structure from the dev_id and
12116 * assume the HBA is not interrupting.
12117 */
12118 phba = (struct lpfc_hba *) dev_id;
12119
12120 if (unlikely(!phba))
12121 return IRQ_NONE;
12122
12123 /*
12124 * Stuff needs to be attented to when this function is invoked as an
12125 * individual interrupt handler in MSI-X multi-message interrupt mode
12126 */
12127 if (phba->intr_type == MSIX) {
3772a991
JS
12128 /* Check device state for handling interrupt */
12129 if (lpfc_intr_state_check(phba))
9399627f
JS
12130 return IRQ_NONE;
12131 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12132 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12133 return IRQ_HANDLED;
9399627f 12134 /* Clear up only attention source related to fast-path */
5b75da2f 12135 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12136 /*
12137 * If there is deferred error attention, do not check for
12138 * any interrupt.
12139 */
12140 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12141 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12142 return IRQ_NONE;
12143 }
9399627f
JS
12144 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12145 phba->HAregaddr);
12146 readl(phba->HAregaddr); /* flush */
5b75da2f 12147 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12148 } else
12149 ha_copy = phba->ha_copy;
dea3101e
JB
12150
12151 /*
9399627f 12152 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12153 */
9399627f
JS
12154 ha_copy &= ~(phba->work_ha_mask);
12155
12156 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12157 status >>= (4*LPFC_FCP_RING);
895427bd 12158 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12159 if (status & HA_RXMASK)
895427bd 12160 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12161
12162 if (phba->cfg_multi_ring_support == 2) {
12163 /*
9399627f
JS
12164 * Process all events on extra ring. Take the optimized path
12165 * for extra ring IO.
a4bc3379 12166 */
9399627f 12167 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12168 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12169 if (status & HA_RXMASK) {
a4bc3379 12170 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12171 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12172 status);
12173 }
12174 }
dea3101e 12175 return IRQ_HANDLED;
3772a991 12176} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12177
12178/**
3772a991 12179 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12180 * @irq: Interrupt number.
12181 * @dev_id: The device context pointer.
12182 *
3772a991
JS
12183 * This function is the HBA device-level interrupt handler to device with
12184 * SLI-3 interface spec, called from the PCI layer when either MSI or
12185 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12186 * requires driver attention. This function invokes the slow-path interrupt
12187 * attention handling function and fast-path interrupt attention handling
12188 * function in turn to process the relevant HBA attention events. This
12189 * function is called without any lock held. It gets the hbalock to access
12190 * and update SLI data structures.
9399627f
JS
12191 *
12192 * This function returns IRQ_HANDLED when interrupt is handled, else it
12193 * returns IRQ_NONE.
12194 **/
12195irqreturn_t
3772a991 12196lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12197{
12198 struct lpfc_hba *phba;
12199 irqreturn_t sp_irq_rc, fp_irq_rc;
12200 unsigned long status1, status2;
a747c9ce 12201 uint32_t hc_copy;
9399627f
JS
12202
12203 /*
12204 * Get the driver's phba structure from the dev_id and
12205 * assume the HBA is not interrupting.
12206 */
12207 phba = (struct lpfc_hba *) dev_id;
12208
12209 if (unlikely(!phba))
12210 return IRQ_NONE;
12211
3772a991
JS
12212 /* Check device state for handling interrupt */
12213 if (lpfc_intr_state_check(phba))
9399627f
JS
12214 return IRQ_NONE;
12215
12216 spin_lock(&phba->hbalock);
9940b97b
JS
12217 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12218 spin_unlock(&phba->hbalock);
12219 return IRQ_HANDLED;
12220 }
12221
9399627f
JS
12222 if (unlikely(!phba->ha_copy)) {
12223 spin_unlock(&phba->hbalock);
12224 return IRQ_NONE;
12225 } else if (phba->ha_copy & HA_ERATT) {
12226 if (phba->hba_flag & HBA_ERATT_HANDLED)
12227 /* ERATT polling has handled ERATT */
12228 phba->ha_copy &= ~HA_ERATT;
12229 else
12230 /* Indicate interrupt handler handles ERATT */
12231 phba->hba_flag |= HBA_ERATT_HANDLED;
12232 }
12233
a257bf90
JS
12234 /*
12235 * If there is deferred error attention, do not check for any interrupt.
12236 */
12237 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12238 spin_unlock(&phba->hbalock);
a257bf90
JS
12239 return IRQ_NONE;
12240 }
12241
9399627f 12242 /* Clear attention sources except link and error attentions */
9940b97b
JS
12243 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12244 spin_unlock(&phba->hbalock);
12245 return IRQ_HANDLED;
12246 }
a747c9ce
JS
12247 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12248 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12249 phba->HCregaddr);
9399627f 12250 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12251 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12252 readl(phba->HAregaddr); /* flush */
12253 spin_unlock(&phba->hbalock);
12254
12255 /*
12256 * Invokes slow-path host attention interrupt handling as appropriate.
12257 */
12258
12259 /* status of events with mailbox and link attention */
12260 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12261
12262 /* status of events with ELS ring */
12263 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12264 status2 >>= (4*LPFC_ELS_RING);
12265
12266 if (status1 || (status2 & HA_RXMASK))
3772a991 12267 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12268 else
12269 sp_irq_rc = IRQ_NONE;
12270
12271 /*
12272 * Invoke fast-path host attention interrupt handling as appropriate.
12273 */
12274
12275 /* status of events with FCP ring */
12276 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12277 status1 >>= (4*LPFC_FCP_RING);
12278
12279 /* status of events with extra ring */
12280 if (phba->cfg_multi_ring_support == 2) {
12281 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12282 status2 >>= (4*LPFC_EXTRA_RING);
12283 } else
12284 status2 = 0;
12285
12286 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12287 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12288 else
12289 fp_irq_rc = IRQ_NONE;
dea3101e 12290
9399627f
JS
12291 /* Return device-level interrupt handling status */
12292 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12293} /* lpfc_sli_intr_handler */
4f774513
JS
12294
12295/**
12296 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12297 * @phba: pointer to lpfc hba data structure.
12298 *
12299 * This routine is invoked by the worker thread to process all the pending
12300 * SLI4 FCP abort XRI events.
12301 **/
12302void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12303{
12304 struct lpfc_cq_event *cq_event;
12305
12306 /* First, declare the fcp xri abort event has been handled */
12307 spin_lock_irq(&phba->hbalock);
12308 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12309 spin_unlock_irq(&phba->hbalock);
12310 /* Now, handle all the fcp xri abort events */
12311 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12312 /* Get the first event from the head of the event queue */
12313 spin_lock_irq(&phba->hbalock);
12314 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12315 cq_event, struct lpfc_cq_event, list);
12316 spin_unlock_irq(&phba->hbalock);
12317 /* Notify aborted XRI for FCP work queue */
12318 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12319 /* Free the event processed back to the free pool */
12320 lpfc_sli4_cq_event_release(phba, cq_event);
12321 }
12322}
12323
12324/**
12325 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12326 * @phba: pointer to lpfc hba data structure.
12327 *
12328 * This routine is invoked by the worker thread to process all the pending
12329 * SLI4 els abort xri events.
12330 **/
12331void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12332{
12333 struct lpfc_cq_event *cq_event;
12334
12335 /* First, declare the els xri abort event has been handled */
12336 spin_lock_irq(&phba->hbalock);
12337 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12338 spin_unlock_irq(&phba->hbalock);
12339 /* Now, handle all the els xri abort events */
12340 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12341 /* Get the first event from the head of the event queue */
12342 spin_lock_irq(&phba->hbalock);
12343 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12344 cq_event, struct lpfc_cq_event, list);
12345 spin_unlock_irq(&phba->hbalock);
12346 /* Notify aborted XRI for ELS work queue */
12347 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12348 /* Free the event processed back to the free pool */
12349 lpfc_sli4_cq_event_release(phba, cq_event);
12350 }
12351}
12352
341af102
JS
12353/**
12354 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12355 * @phba: pointer to lpfc hba data structure
12356 * @pIocbIn: pointer to the rspiocbq
12357 * @pIocbOut: pointer to the cmdiocbq
12358 * @wcqe: pointer to the complete wcqe
12359 *
12360 * This routine transfers the fields of a command iocbq to a response iocbq
12361 * by copying all the IOCB fields from command iocbq and transferring the
12362 * completion status information from the complete wcqe.
12363 **/
4f774513 12364static void
341af102
JS
12365lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12366 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12367 struct lpfc_iocbq *pIocbOut,
12368 struct lpfc_wcqe_complete *wcqe)
12369{
af22741c 12370 int numBdes, i;
341af102 12371 unsigned long iflags;
af22741c
JS
12372 uint32_t status, max_response;
12373 struct lpfc_dmabuf *dmabuf;
12374 struct ulp_bde64 *bpl, bde;
4f774513
JS
12375 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12376
12377 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12378 sizeof(struct lpfc_iocbq) - offset);
4f774513 12379 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12380 status = bf_get(lpfc_wcqe_c_status, wcqe);
12381 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12382 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12383 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12384 pIocbIn->iocb.un.fcpi.fcpi_parm =
12385 pIocbOut->iocb.un.fcpi.fcpi_parm -
12386 wcqe->total_data_placed;
12387 else
12388 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12389 else {
4f774513 12390 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12391 switch (pIocbOut->iocb.ulpCommand) {
12392 case CMD_ELS_REQUEST64_CR:
12393 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12394 bpl = (struct ulp_bde64 *)dmabuf->virt;
12395 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12396 max_response = bde.tus.f.bdeSize;
12397 break;
12398 case CMD_GEN_REQUEST64_CR:
12399 max_response = 0;
12400 if (!pIocbOut->context3)
12401 break;
12402 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12403 sizeof(struct ulp_bde64);
12404 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12405 bpl = (struct ulp_bde64 *)dmabuf->virt;
12406 for (i = 0; i < numBdes; i++) {
12407 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12408 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12409 max_response += bde.tus.f.bdeSize;
12410 }
12411 break;
12412 default:
12413 max_response = wcqe->total_data_placed;
12414 break;
12415 }
12416 if (max_response < wcqe->total_data_placed)
12417 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12418 else
12419 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12420 wcqe->total_data_placed;
695a814e 12421 }
341af102 12422
acd6859b
JS
12423 /* Convert BG errors for completion status */
12424 if (status == CQE_STATUS_DI_ERROR) {
12425 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12426
12427 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12428 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12429 else
12430 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12431
12432 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12433 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12434 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12435 BGS_GUARD_ERR_MASK;
12436 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12437 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12438 BGS_APPTAG_ERR_MASK;
12439 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12440 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12441 BGS_REFTAG_ERR_MASK;
12442
12443 /* Check to see if there was any good data before the error */
12444 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12445 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12446 BGS_HI_WATER_MARK_PRESENT_MASK;
12447 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12448 wcqe->total_data_placed;
12449 }
12450
12451 /*
12452 * Set ALL the error bits to indicate we don't know what
12453 * type of error it is.
12454 */
12455 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12456 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12457 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12458 BGS_GUARD_ERR_MASK);
12459 }
12460
341af102
JS
12461 /* Pick up HBA exchange busy condition */
12462 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12463 spin_lock_irqsave(&phba->hbalock, iflags);
12464 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12465 spin_unlock_irqrestore(&phba->hbalock, iflags);
12466 }
4f774513
JS
12467}
12468
45ed1190
JS
12469/**
12470 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12471 * @phba: Pointer to HBA context object.
12472 * @wcqe: Pointer to work-queue completion queue entry.
12473 *
12474 * This routine handles an ELS work-queue completion event and construct
12475 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12476 * discovery engine to handle.
12477 *
12478 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12479 **/
12480static struct lpfc_iocbq *
12481lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12482 struct lpfc_iocbq *irspiocbq)
12483{
895427bd 12484 struct lpfc_sli_ring *pring;
45ed1190
JS
12485 struct lpfc_iocbq *cmdiocbq;
12486 struct lpfc_wcqe_complete *wcqe;
12487 unsigned long iflags;
12488
895427bd 12489 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12490 if (unlikely(!pring))
12491 return NULL;
895427bd 12492
45ed1190 12493 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12494 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12495 pring->stats.iocb_event++;
12496 /* Look up the ELS command IOCB and create pseudo response IOCB */
12497 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12498 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12499 if (unlikely(!cmdiocbq)) {
401bb416 12500 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12501 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12502 "0386 ELS complete with no corresponding "
401bb416
DK
12503 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12504 wcqe->word0, wcqe->total_data_placed,
12505 wcqe->parameter, wcqe->word3);
45ed1190
JS
12506 lpfc_sli_release_iocbq(phba, irspiocbq);
12507 return NULL;
12508 }
12509
401bb416
DK
12510 /* Put the iocb back on the txcmplq */
12511 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12512 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12513
45ed1190 12514 /* Fake the irspiocbq and copy necessary response information */
341af102 12515 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12516
12517 return irspiocbq;
12518}
12519
8a5ca109
JS
12520inline struct lpfc_cq_event *
12521lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12522{
12523 struct lpfc_cq_event *cq_event;
12524
12525 /* Allocate a new internal CQ_EVENT entry */
12526 cq_event = lpfc_sli4_cq_event_alloc(phba);
12527 if (!cq_event) {
12528 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12529 "0602 Failed to alloc CQ_EVENT entry\n");
12530 return NULL;
12531 }
12532
12533 /* Move the CQE into the event */
12534 memcpy(&cq_event->cqe, entry, size);
12535 return cq_event;
12536}
12537
04c68496
JS
12538/**
12539 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12540 * @phba: Pointer to HBA context object.
12541 * @cqe: Pointer to mailbox completion queue entry.
12542 *
12543 * This routine process a mailbox completion queue entry with asynchrous
12544 * event.
12545 *
12546 * Return: true if work posted to worker thread, otherwise false.
12547 **/
12548static bool
12549lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12550{
12551 struct lpfc_cq_event *cq_event;
12552 unsigned long iflags;
12553
12554 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12555 "0392 Async Event: word0:x%x, word1:x%x, "
12556 "word2:x%x, word3:x%x\n", mcqe->word0,
12557 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12558
8a5ca109
JS
12559 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12560 if (!cq_event)
04c68496 12561 return false;
04c68496
JS
12562 spin_lock_irqsave(&phba->hbalock, iflags);
12563 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12564 /* Set the async event flag */
12565 phba->hba_flag |= ASYNC_EVENT;
12566 spin_unlock_irqrestore(&phba->hbalock, iflags);
12567
12568 return true;
12569}
12570
12571/**
12572 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12573 * @phba: Pointer to HBA context object.
12574 * @cqe: Pointer to mailbox completion queue entry.
12575 *
12576 * This routine process a mailbox completion queue entry with mailbox
12577 * completion event.
12578 *
12579 * Return: true if work posted to worker thread, otherwise false.
12580 **/
12581static bool
12582lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12583{
12584 uint32_t mcqe_status;
12585 MAILBOX_t *mbox, *pmbox;
12586 struct lpfc_mqe *mqe;
12587 struct lpfc_vport *vport;
12588 struct lpfc_nodelist *ndlp;
12589 struct lpfc_dmabuf *mp;
12590 unsigned long iflags;
12591 LPFC_MBOXQ_t *pmb;
12592 bool workposted = false;
12593 int rc;
12594
12595 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12596 if (!bf_get(lpfc_trailer_completed, mcqe))
12597 goto out_no_mqe_complete;
12598
12599 /* Get the reference to the active mbox command */
12600 spin_lock_irqsave(&phba->hbalock, iflags);
12601 pmb = phba->sli.mbox_active;
12602 if (unlikely(!pmb)) {
12603 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12604 "1832 No pending MBOX command to handle\n");
12605 spin_unlock_irqrestore(&phba->hbalock, iflags);
12606 goto out_no_mqe_complete;
12607 }
12608 spin_unlock_irqrestore(&phba->hbalock, iflags);
12609 mqe = &pmb->u.mqe;
12610 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12611 mbox = phba->mbox;
12612 vport = pmb->vport;
12613
12614 /* Reset heartbeat timer */
12615 phba->last_completion_time = jiffies;
12616 del_timer(&phba->sli.mbox_tmo);
12617
12618 /* Move mbox data to caller's mailbox region, do endian swapping */
12619 if (pmb->mbox_cmpl && mbox)
12620 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12621
73d91e50
JS
12622 /*
12623 * For mcqe errors, conditionally move a modified error code to
12624 * the mbox so that the error will not be missed.
12625 */
12626 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12627 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12628 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12629 bf_set(lpfc_mqe_status, mqe,
12630 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12631 }
04c68496
JS
12632 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12633 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12634 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12635 "MBOX dflt rpi: status:x%x rpi:x%x",
12636 mcqe_status,
12637 pmbox->un.varWords[0], 0);
12638 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12639 mp = (struct lpfc_dmabuf *)(pmb->context1);
12640 ndlp = (struct lpfc_nodelist *)pmb->context2;
12641 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12642 * RID of the PPI using the same mbox buffer.
12643 */
12644 lpfc_unreg_login(phba, vport->vpi,
12645 pmbox->un.varWords[0], pmb);
12646 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12647 pmb->context1 = mp;
12648 pmb->context2 = ndlp;
12649 pmb->vport = vport;
12650 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12651 if (rc != MBX_BUSY)
12652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12653 LOG_SLI, "0385 rc should "
12654 "have been MBX_BUSY\n");
12655 if (rc != MBX_NOT_FINISHED)
12656 goto send_current_mbox;
12657 }
12658 }
12659 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12660 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12661 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12662
12663 /* There is mailbox completion work to do */
12664 spin_lock_irqsave(&phba->hbalock, iflags);
12665 __lpfc_mbox_cmpl_put(phba, pmb);
12666 phba->work_ha |= HA_MBATT;
12667 spin_unlock_irqrestore(&phba->hbalock, iflags);
12668 workposted = true;
12669
12670send_current_mbox:
12671 spin_lock_irqsave(&phba->hbalock, iflags);
12672 /* Release the mailbox command posting token */
12673 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12674 /* Setting active mailbox pointer need to be in sync to flag clear */
12675 phba->sli.mbox_active = NULL;
12676 spin_unlock_irqrestore(&phba->hbalock, iflags);
12677 /* Wake up worker thread to post the next pending mailbox command */
12678 lpfc_worker_wake_up(phba);
12679out_no_mqe_complete:
12680 if (bf_get(lpfc_trailer_consumed, mcqe))
12681 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12682 return workposted;
12683}
12684
12685/**
12686 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12687 * @phba: Pointer to HBA context object.
12688 * @cqe: Pointer to mailbox completion queue entry.
12689 *
12690 * This routine process a mailbox completion queue entry, it invokes the
12691 * proper mailbox complete handling or asynchrous event handling routine
12692 * according to the MCQE's async bit.
12693 *
12694 * Return: true if work posted to worker thread, otherwise false.
12695 **/
12696static bool
12697lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12698{
12699 struct lpfc_mcqe mcqe;
12700 bool workposted;
12701
12702 /* Copy the mailbox MCQE and convert endian order as needed */
12703 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12704
12705 /* Invoke the proper event handling routine */
12706 if (!bf_get(lpfc_trailer_async, &mcqe))
12707 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12708 else
12709 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12710 return workposted;
12711}
12712
4f774513
JS
12713/**
12714 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12715 * @phba: Pointer to HBA context object.
2a76a283 12716 * @cq: Pointer to associated CQ
4f774513
JS
12717 * @wcqe: Pointer to work-queue completion queue entry.
12718 *
12719 * This routine handles an ELS work-queue completion event.
12720 *
12721 * Return: true if work posted to worker thread, otherwise false.
12722 **/
12723static bool
2a76a283 12724lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12725 struct lpfc_wcqe_complete *wcqe)
12726{
4f774513
JS
12727 struct lpfc_iocbq *irspiocbq;
12728 unsigned long iflags;
2a76a283 12729 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12730 int txq_cnt = 0;
12731 int txcmplq_cnt = 0;
12732 int fcp_txcmplq_cnt = 0;
4f774513 12733
45ed1190 12734 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12735 irspiocbq = lpfc_sli_get_iocbq(phba);
12736 if (!irspiocbq) {
0e9bb8d7
JS
12737 if (!list_empty(&pring->txq))
12738 txq_cnt++;
12739 if (!list_empty(&pring->txcmplq))
12740 txcmplq_cnt++;
4f774513 12741 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12742 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12743 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12744 txq_cnt, phba->iocb_cnt,
12745 fcp_txcmplq_cnt,
12746 txcmplq_cnt);
45ed1190 12747 return false;
4f774513 12748 }
4f774513 12749
45ed1190
JS
12750 /* Save off the slow-path queue event for work thread to process */
12751 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12752 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12753 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12754 &phba->sli4_hba.sp_queue_event);
12755 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12756 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12757
45ed1190 12758 return true;
4f774513
JS
12759}
12760
12761/**
12762 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12763 * @phba: Pointer to HBA context object.
12764 * @wcqe: Pointer to work-queue completion queue entry.
12765 *
3f8b6fb7 12766 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
12767 * proper WQ release routine to the slow-path WQ.
12768 **/
12769static void
12770lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12771 struct lpfc_wcqe_release *wcqe)
12772{
2e90f4b5
JS
12773 /* sanity check on queue memory */
12774 if (unlikely(!phba->sli4_hba.els_wq))
12775 return;
4f774513
JS
12776 /* Check for the slow-path ELS work queue */
12777 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12778 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12779 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12780 else
12781 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12782 "2579 Slow-path wqe consume event carries "
12783 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12784 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12785 phba->sli4_hba.els_wq->queue_id);
12786}
12787
12788/**
12789 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12790 * @phba: Pointer to HBA context object.
12791 * @cq: Pointer to a WQ completion queue.
12792 * @wcqe: Pointer to work-queue completion queue entry.
12793 *
12794 * This routine handles an XRI abort event.
12795 *
12796 * Return: true if work posted to worker thread, otherwise false.
12797 **/
12798static bool
12799lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12800 struct lpfc_queue *cq,
12801 struct sli4_wcqe_xri_aborted *wcqe)
12802{
12803 bool workposted = false;
12804 struct lpfc_cq_event *cq_event;
12805 unsigned long iflags;
12806
4f774513
JS
12807 switch (cq->subtype) {
12808 case LPFC_FCP:
8a5ca109
JS
12809 cq_event = lpfc_cq_event_setup(
12810 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12811 if (!cq_event)
12812 return false;
4f774513
JS
12813 spin_lock_irqsave(&phba->hbalock, iflags);
12814 list_add_tail(&cq_event->list,
12815 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12816 /* Set the fcp xri abort event flag */
12817 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12818 spin_unlock_irqrestore(&phba->hbalock, iflags);
12819 workposted = true;
12820 break;
422c4cb7 12821 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 12822 case LPFC_ELS:
8a5ca109
JS
12823 cq_event = lpfc_cq_event_setup(
12824 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12825 if (!cq_event)
12826 return false;
4f774513
JS
12827 spin_lock_irqsave(&phba->hbalock, iflags);
12828 list_add_tail(&cq_event->list,
12829 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12830 /* Set the els xri abort event flag */
12831 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12832 spin_unlock_irqrestore(&phba->hbalock, iflags);
12833 workposted = true;
12834 break;
318083ad 12835 case LPFC_NVME:
8a5ca109
JS
12836 /* Notify aborted XRI for NVME work queue */
12837 if (phba->nvmet_support)
12838 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12839 else
12840 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12841
12842 workposted = false;
318083ad 12843 break;
4f774513
JS
12844 default:
12845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
12846 "0603 Invalid CQ subtype %d: "
12847 "%08x %08x %08x %08x\n",
12848 cq->subtype, wcqe->word0, wcqe->parameter,
12849 wcqe->word2, wcqe->word3);
4f774513
JS
12850 workposted = false;
12851 break;
12852 }
12853 return workposted;
12854}
12855
4f774513
JS
12856/**
12857 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12858 * @phba: Pointer to HBA context object.
12859 * @rcqe: Pointer to receive-queue completion queue entry.
12860 *
12861 * This routine process a receive-queue completion queue entry.
12862 *
12863 * Return: true if work posted to worker thread, otherwise false.
12864 **/
12865static bool
4d9ab994 12866lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 12867{
4f774513 12868 bool workposted = false;
895427bd 12869 struct fc_frame_header *fc_hdr;
4f774513
JS
12870 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12871 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 12872 struct lpfc_nvmet_tgtport *tgtp;
4f774513 12873 struct hbq_dmabuf *dma_buf;
7851fe2c 12874 uint32_t status, rq_id;
4f774513
JS
12875 unsigned long iflags;
12876
2e90f4b5
JS
12877 /* sanity check on queue memory */
12878 if (unlikely(!hrq) || unlikely(!drq))
12879 return workposted;
12880
7851fe2c
JS
12881 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12882 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12883 else
12884 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12885 if (rq_id != hrq->queue_id)
4f774513
JS
12886 goto out;
12887
4d9ab994 12888 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
12889 switch (status) {
12890 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12892 "2537 Receive Frame Truncated!!\n");
12893 case FC_STATUS_RQ_SUCCESS:
12894 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 12895 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
12896 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12897 if (!dma_buf) {
b84daac9 12898 hrq->RQ_no_buf_found++;
4f774513
JS
12899 spin_unlock_irqrestore(&phba->hbalock, iflags);
12900 goto out;
12901 }
b84daac9 12902 hrq->RQ_rcv_buf++;
547077a4 12903 hrq->RQ_buf_posted--;
4d9ab994 12904 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
12905
12906 /* If a NVME LS event (type 0x28), treat it as Fast path */
12907 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12908
4f774513 12909 /* save off the frame for the word thread to process */
4d9ab994 12910 list_add_tail(&dma_buf->cq_event.list,
45ed1190 12911 &phba->sli4_hba.sp_queue_event);
4f774513 12912 /* Frame received */
45ed1190 12913 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
12914 spin_unlock_irqrestore(&phba->hbalock, iflags);
12915 workposted = true;
12916 break;
4f774513 12917 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
12918 if (phba->nvmet_support) {
12919 tgtp = phba->targetport->private;
12920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12921 "6402 RQE Error x%x, posted %d err_cnt "
12922 "%d: %x %x %x\n",
12923 status, hrq->RQ_buf_posted,
12924 hrq->RQ_no_posted_buf,
12925 atomic_read(&tgtp->rcv_fcp_cmd_in),
12926 atomic_read(&tgtp->rcv_fcp_cmd_out),
12927 atomic_read(&tgtp->xmt_fcp_release));
12928 }
12929 /* fallthrough */
12930
12931 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 12932 hrq->RQ_no_posted_buf++;
4f774513
JS
12933 /* Post more buffers if possible */
12934 spin_lock_irqsave(&phba->hbalock, iflags);
12935 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12936 spin_unlock_irqrestore(&phba->hbalock, iflags);
12937 workposted = true;
12938 break;
12939 }
12940out:
12941 return workposted;
4f774513
JS
12942}
12943
4d9ab994
JS
12944/**
12945 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12946 * @phba: Pointer to HBA context object.
12947 * @cq: Pointer to the completion queue.
12948 * @wcqe: Pointer to a completion queue entry.
12949 *
25985edc 12950 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
12951 * entry.
12952 *
12953 * Return: true if work posted to worker thread, otherwise false.
12954 **/
12955static bool
12956lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12957 struct lpfc_cqe *cqe)
12958{
45ed1190 12959 struct lpfc_cqe cqevt;
4d9ab994
JS
12960 bool workposted = false;
12961
12962 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 12963 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
12964
12965 /* Check and process for different type of WCQE and dispatch */
45ed1190 12966 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 12967 case CQE_CODE_COMPL_WQE:
45ed1190 12968 /* Process the WQ/RQ complete event */
bc73905a 12969 phba->last_completion_time = jiffies;
2a76a283 12970 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 12971 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
12972 break;
12973 case CQE_CODE_RELEASE_WQE:
12974 /* Process the WQ release event */
12975 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 12976 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
12977 break;
12978 case CQE_CODE_XRI_ABORTED:
12979 /* Process the WQ XRI abort event */
bc73905a 12980 phba->last_completion_time = jiffies;
4d9ab994 12981 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 12982 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
12983 break;
12984 case CQE_CODE_RECEIVE:
7851fe2c 12985 case CQE_CODE_RECEIVE_V1:
4d9ab994 12986 /* Process the RQ event */
bc73905a 12987 phba->last_completion_time = jiffies;
4d9ab994 12988 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 12989 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
12990 break;
12991 default:
12992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12993 "0388 Not a valid WCQE code: x%x\n",
45ed1190 12994 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
12995 break;
12996 }
12997 return workposted;
12998}
12999
4f774513
JS
13000/**
13001 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13002 * @phba: Pointer to HBA context object.
13003 * @eqe: Pointer to fast-path event queue entry.
13004 *
13005 * This routine process a event queue entry from the slow-path event queue.
13006 * It will check the MajorCode and MinorCode to determine this is for a
13007 * completion event on a completion queue, if not, an error shall be logged
13008 * and just return. Otherwise, it will get to the corresponding completion
13009 * queue and process all the entries on that completion queue, rearm the
13010 * completion queue, and then return.
13011 *
13012 **/
f485c18d 13013static void
67d12733
JS
13014lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13015 struct lpfc_queue *speq)
4f774513 13016{
67d12733 13017 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13018 uint16_t cqid;
13019
4f774513 13020 /* Get the reference to the corresponding CQ */
cb5172ea 13021 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13022
4f774513
JS
13023 list_for_each_entry(childq, &speq->child_list, list) {
13024 if (childq->queue_id == cqid) {
13025 cq = childq;
13026 break;
13027 }
13028 }
13029 if (unlikely(!cq)) {
75baf696
JS
13030 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13032 "0365 Slow-path CQ identifier "
13033 "(%d) does not exist\n", cqid);
f485c18d 13034 return;
4f774513
JS
13035 }
13036
895427bd
JS
13037 /* Save EQ associated with this CQ */
13038 cq->assoc_qp = speq;
13039
f485c18d
DK
13040 if (!queue_work(phba->wq, &cq->spwork))
13041 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13042 "0390 Cannot schedule soft IRQ "
13043 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13044 cqid, cq->queue_id, smp_processor_id());
13045}
13046
13047/**
13048 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13049 * @phba: Pointer to HBA context object.
13050 *
13051 * This routine process a event queue entry from the slow-path event queue.
13052 * It will check the MajorCode and MinorCode to determine this is for a
13053 * completion event on a completion queue, if not, an error shall be logged
13054 * and just return. Otherwise, it will get to the corresponding completion
13055 * queue and process all the entries on that completion queue, rearm the
13056 * completion queue, and then return.
13057 *
13058 **/
13059static void
13060lpfc_sli4_sp_process_cq(struct work_struct *work)
13061{
13062 struct lpfc_queue *cq =
13063 container_of(work, struct lpfc_queue, spwork);
13064 struct lpfc_hba *phba = cq->phba;
13065 struct lpfc_cqe *cqe;
13066 bool workposted = false;
13067 int ccount = 0;
13068
4f774513
JS
13069 /* Process all the entries to the CQ */
13070 switch (cq->type) {
13071 case LPFC_MCQ:
13072 while ((cqe = lpfc_sli4_cq_get(cq))) {
13073 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13074 if (!(++ccount % cq->entry_repost))
7869da18 13075 break;
b84daac9 13076 cq->CQ_mbox++;
4f774513
JS
13077 }
13078 break;
13079 case LPFC_WCQ:
13080 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13081 if (cq->subtype == LPFC_FCP ||
13082 cq->subtype == LPFC_NVME) {
13083#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13084 if (phba->ktime_on)
13085 cq->isr_timestamp = ktime_get_ns();
13086 else
13087 cq->isr_timestamp = 0;
13088#endif
895427bd 13089 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13090 cqe);
c8a4ce0b 13091 } else {
0558056c
JS
13092 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13093 cqe);
c8a4ce0b 13094 }
f485c18d 13095 if (!(++ccount % cq->entry_repost))
7869da18 13096 break;
4f774513 13097 }
b84daac9
JS
13098
13099 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13100 if (ccount > cq->CQ_max_cqe)
13101 cq->CQ_max_cqe = ccount;
4f774513
JS
13102 break;
13103 default:
13104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13105 "0370 Invalid completion queue type (%d)\n",
13106 cq->type);
f485c18d 13107 return;
4f774513
JS
13108 }
13109
13110 /* Catch the no cq entry condition, log an error */
f485c18d 13111 if (unlikely(ccount == 0))
4f774513
JS
13112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13113 "0371 No entry from the CQ: identifier "
13114 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13115
13116 /* In any case, flash and re-arm the RCQ */
13117 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13118
13119 /* wake up worker thread if there are works to be done */
13120 if (workposted)
13121 lpfc_worker_wake_up(phba);
13122}
13123
13124/**
13125 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13126 * @phba: Pointer to HBA context object.
13127 * @cq: Pointer to associated CQ
13128 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13129 *
13130 * This routine process a fast-path work queue completion entry from fast-path
13131 * event queue for FCP command response completion.
13132 **/
13133static void
2a76a283 13134lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13135 struct lpfc_wcqe_complete *wcqe)
13136{
2a76a283 13137 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13138 struct lpfc_iocbq *cmdiocbq;
13139 struct lpfc_iocbq irspiocbq;
13140 unsigned long iflags;
13141
4f774513
JS
13142 /* Check for response status */
13143 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13144 /* If resource errors reported from HBA, reduce queue
13145 * depth of the SCSI device.
13146 */
e3d2b802
JS
13147 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13148 IOSTAT_LOCAL_REJECT)) &&
13149 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13150 IOERR_NO_RESOURCES))
4f774513 13151 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13152
4f774513
JS
13153 /* Log the error status */
13154 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13155 "0373 FCP complete error: status=x%x, "
13156 "hw_status=x%x, total_data_specified=%d, "
13157 "parameter=x%x, word3=x%x\n",
13158 bf_get(lpfc_wcqe_c_status, wcqe),
13159 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13160 wcqe->total_data_placed, wcqe->parameter,
13161 wcqe->word3);
13162 }
13163
13164 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13165 spin_lock_irqsave(&pring->ring_lock, iflags);
13166 pring->stats.iocb_event++;
4f774513
JS
13167 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13168 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13169 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13170 if (unlikely(!cmdiocbq)) {
13171 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13172 "0374 FCP complete with no corresponding "
13173 "cmdiocb: iotag (%d)\n",
13174 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13175 return;
13176 }
c8a4ce0b
DK
13177#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13178 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13179#endif
895427bd
JS
13180 if (cmdiocbq->iocb_cmpl == NULL) {
13181 if (cmdiocbq->wqe_cmpl) {
13182 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13183 spin_lock_irqsave(&phba->hbalock, iflags);
13184 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13185 spin_unlock_irqrestore(&phba->hbalock, iflags);
13186 }
13187
13188 /* Pass the cmd_iocb and the wcqe to the upper layer */
13189 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13190 return;
13191 }
4f774513
JS
13192 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13193 "0375 FCP cmdiocb not callback function "
13194 "iotag: (%d)\n",
13195 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13196 return;
13197 }
13198
13199 /* Fake the irspiocb and copy necessary response information */
341af102 13200 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13201
0f65ff68
JS
13202 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13203 spin_lock_irqsave(&phba->hbalock, iflags);
13204 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13205 spin_unlock_irqrestore(&phba->hbalock, iflags);
13206 }
13207
4f774513
JS
13208 /* Pass the cmd_iocb and the rsp state to the upper layer */
13209 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13210}
13211
13212/**
13213 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13214 * @phba: Pointer to HBA context object.
13215 * @cq: Pointer to completion queue.
13216 * @wcqe: Pointer to work-queue completion queue entry.
13217 *
3f8b6fb7 13218 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13219 * proper WQ release routine to the slow-path WQ.
13220 **/
13221static void
13222lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13223 struct lpfc_wcqe_release *wcqe)
13224{
13225 struct lpfc_queue *childwq;
13226 bool wqid_matched = false;
895427bd 13227 uint16_t hba_wqid;
4f774513
JS
13228
13229 /* Check for fast-path FCP work queue release */
895427bd 13230 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13231 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13232 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13233 lpfc_sli4_wq_release(childwq,
13234 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13235 wqid_matched = true;
13236 break;
13237 }
13238 }
13239 /* Report warning log message if no match found */
13240 if (wqid_matched != true)
13241 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13242 "2580 Fast-path wqe consume event carries "
895427bd 13243 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13244}
13245
13246/**
2d7dbc4c
JS
13247 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13248 * @phba: Pointer to HBA context object.
13249 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13250 *
2d7dbc4c
JS
13251 * This routine process a receive-queue completion queue entry.
13252 *
13253 * Return: true if work posted to worker thread, otherwise false.
13254 **/
13255static bool
13256lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13257 struct lpfc_rcqe *rcqe)
13258{
13259 bool workposted = false;
13260 struct lpfc_queue *hrq;
13261 struct lpfc_queue *drq;
13262 struct rqb_dmabuf *dma_buf;
13263 struct fc_frame_header *fc_hdr;
547077a4 13264 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13265 uint32_t status, rq_id;
13266 unsigned long iflags;
13267 uint32_t fctl, idx;
13268
13269 if ((phba->nvmet_support == 0) ||
13270 (phba->sli4_hba.nvmet_cqset == NULL))
13271 return workposted;
13272
13273 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13274 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13275 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13276
13277 /* sanity check on queue memory */
13278 if (unlikely(!hrq) || unlikely(!drq))
13279 return workposted;
13280
13281 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13282 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13283 else
13284 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13285
13286 if ((phba->nvmet_support == 0) ||
13287 (rq_id != hrq->queue_id))
13288 return workposted;
13289
13290 status = bf_get(lpfc_rcqe_status, rcqe);
13291 switch (status) {
13292 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13294 "6126 Receive Frame Truncated!!\n");
78e1d200 13295 /* Drop thru */
2d7dbc4c 13296 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13297 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13298 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13299 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13300 if (!dma_buf) {
13301 hrq->RQ_no_buf_found++;
13302 spin_unlock_irqrestore(&phba->hbalock, iflags);
13303 goto out;
13304 }
13305 spin_unlock_irqrestore(&phba->hbalock, iflags);
13306 hrq->RQ_rcv_buf++;
547077a4 13307 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13308 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13309
13310 /* Just some basic sanity checks on FCP Command frame */
13311 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13312 fc_hdr->fh_f_ctl[1] << 8 |
13313 fc_hdr->fh_f_ctl[2]);
13314 if (((fctl &
13315 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13316 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13317 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13318 goto drop;
13319
13320 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13321 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13322 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13323 phba, idx, dma_buf,
c8a4ce0b 13324 cq->isr_timestamp);
2d7dbc4c
JS
13325 return false;
13326 }
13327drop:
13328 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13329 break;
2d7dbc4c 13330 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13331 if (phba->nvmet_support) {
13332 tgtp = phba->targetport->private;
13333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13334 "6401 RQE Error x%x, posted %d err_cnt "
13335 "%d: %x %x %x\n",
13336 status, hrq->RQ_buf_posted,
13337 hrq->RQ_no_posted_buf,
13338 atomic_read(&tgtp->rcv_fcp_cmd_in),
13339 atomic_read(&tgtp->rcv_fcp_cmd_out),
13340 atomic_read(&tgtp->xmt_fcp_release));
13341 }
13342 /* fallthrough */
13343
13344 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13345 hrq->RQ_no_posted_buf++;
13346 /* Post more buffers if possible */
2d7dbc4c
JS
13347 break;
13348 }
13349out:
13350 return workposted;
13351}
13352
4f774513 13353/**
895427bd 13354 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13355 * @cq: Pointer to the completion queue.
13356 * @eqe: Pointer to fast-path completion queue entry.
13357 *
13358 * This routine process a fast-path work queue completion entry from fast-path
13359 * event queue for FCP command response completion.
13360 **/
13361static int
895427bd 13362lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13363 struct lpfc_cqe *cqe)
13364{
13365 struct lpfc_wcqe_release wcqe;
13366 bool workposted = false;
13367
13368 /* Copy the work queue CQE and convert endian order if needed */
13369 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13370
13371 /* Check and process for different type of WCQE and dispatch */
13372 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13373 case CQE_CODE_COMPL_WQE:
895427bd 13374 case CQE_CODE_NVME_ERSP:
b84daac9 13375 cq->CQ_wq++;
4f774513 13376 /* Process the WQ complete event */
98fc5dd9 13377 phba->last_completion_time = jiffies;
895427bd
JS
13378 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13379 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13380 (struct lpfc_wcqe_complete *)&wcqe);
13381 if (cq->subtype == LPFC_NVME_LS)
13382 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13383 (struct lpfc_wcqe_complete *)&wcqe);
13384 break;
13385 case CQE_CODE_RELEASE_WQE:
b84daac9 13386 cq->CQ_release_wqe++;
4f774513
JS
13387 /* Process the WQ release event */
13388 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13389 (struct lpfc_wcqe_release *)&wcqe);
13390 break;
13391 case CQE_CODE_XRI_ABORTED:
b84daac9 13392 cq->CQ_xri_aborted++;
4f774513 13393 /* Process the WQ XRI abort event */
bc73905a 13394 phba->last_completion_time = jiffies;
4f774513
JS
13395 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13396 (struct sli4_wcqe_xri_aborted *)&wcqe);
13397 break;
895427bd
JS
13398 case CQE_CODE_RECEIVE_V1:
13399 case CQE_CODE_RECEIVE:
13400 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13401 if (cq->subtype == LPFC_NVMET) {
13402 workposted = lpfc_sli4_nvmet_handle_rcqe(
13403 phba, cq, (struct lpfc_rcqe *)&wcqe);
13404 }
895427bd 13405 break;
4f774513
JS
13406 default:
13407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13408 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13409 bf_get(lpfc_wcqe_c_code, &wcqe));
13410 break;
13411 }
13412 return workposted;
13413}
13414
13415/**
67d12733 13416 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13417 * @phba: Pointer to HBA context object.
13418 * @eqe: Pointer to fast-path event queue entry.
13419 *
13420 * This routine process a event queue entry from the fast-path event queue.
13421 * It will check the MajorCode and MinorCode to determine this is for a
13422 * completion event on a completion queue, if not, an error shall be logged
13423 * and just return. Otherwise, it will get to the corresponding completion
13424 * queue and process all the entries on the completion queue, rearm the
13425 * completion queue, and then return.
13426 **/
f485c18d 13427static void
67d12733
JS
13428lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13429 uint32_t qidx)
4f774513 13430{
895427bd 13431 struct lpfc_queue *cq = NULL;
2d7dbc4c 13432 uint16_t cqid, id;
4f774513 13433
cb5172ea 13434 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13436 "0366 Not a valid completion "
4f774513 13437 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13438 bf_get_le32(lpfc_eqe_major_code, eqe),
13439 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13440 return;
4f774513
JS
13441 }
13442
67d12733
JS
13443 /* Get the reference to the corresponding CQ */
13444 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13445
2d7dbc4c
JS
13446 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13447 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13448 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13449 /* Process NVMET unsol rcv */
13450 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13451 goto process_cq;
13452 }
67d12733
JS
13453 }
13454
895427bd
JS
13455 if (phba->sli4_hba.nvme_cq_map &&
13456 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13457 /* Process NVME / NVMET command completion */
895427bd
JS
13458 cq = phba->sli4_hba.nvme_cq[qidx];
13459 goto process_cq;
2e90f4b5 13460 }
67d12733 13461
895427bd
JS
13462 if (phba->sli4_hba.fcp_cq_map &&
13463 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13464 /* Process FCP command completion */
13465 cq = phba->sli4_hba.fcp_cq[qidx];
13466 goto process_cq;
2e90f4b5 13467 }
895427bd
JS
13468
13469 if (phba->sli4_hba.nvmels_cq &&
13470 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13471 /* Process NVME unsol rcv */
13472 cq = phba->sli4_hba.nvmels_cq;
13473 }
13474
13475 /* Otherwise this is a Slow path event */
13476 if (cq == NULL) {
f485c18d
DK
13477 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13478 return;
4f774513
JS
13479 }
13480
895427bd 13481process_cq:
4f774513
JS
13482 if (unlikely(cqid != cq->queue_id)) {
13483 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13484 "0368 Miss-matched fast-path completion "
13485 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13486 cqid, cq->queue_id);
f485c18d 13487 return;
4f774513
JS
13488 }
13489
895427bd
JS
13490 /* Save EQ associated with this CQ */
13491 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13492
f485c18d
DK
13493 if (!queue_work(phba->wq, &cq->irqwork))
13494 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13495 "0363 Cannot schedule soft IRQ "
13496 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13497 cqid, cq->queue_id, smp_processor_id());
13498}
13499
13500/**
13501 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13502 * @phba: Pointer to HBA context object.
13503 * @eqe: Pointer to fast-path event queue entry.
13504 *
13505 * This routine process a event queue entry from the fast-path event queue.
13506 * It will check the MajorCode and MinorCode to determine this is for a
13507 * completion event on a completion queue, if not, an error shall be logged
13508 * and just return. Otherwise, it will get to the corresponding completion
13509 * queue and process all the entries on the completion queue, rearm the
13510 * completion queue, and then return.
13511 **/
13512static void
13513lpfc_sli4_hba_process_cq(struct work_struct *work)
13514{
13515 struct lpfc_queue *cq =
13516 container_of(work, struct lpfc_queue, irqwork);
13517 struct lpfc_hba *phba = cq->phba;
13518 struct lpfc_cqe *cqe;
13519 bool workposted = false;
13520 int ccount = 0;
13521
4f774513
JS
13522 /* Process all the entries to the CQ */
13523 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13524#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13525 if (phba->ktime_on)
13526 cq->isr_timestamp = ktime_get_ns();
13527 else
13528 cq->isr_timestamp = 0;
13529#endif
895427bd 13530 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13531 if (!(++ccount % cq->entry_repost))
7869da18 13532 break;
4f774513
JS
13533 }
13534
b84daac9 13535 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13536 if (ccount > cq->CQ_max_cqe)
13537 cq->CQ_max_cqe = ccount;
13538 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13539
4f774513 13540 /* Catch the no cq entry condition */
f485c18d 13541 if (unlikely(ccount == 0))
4f774513
JS
13542 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13543 "0369 No entry from fast-path completion "
13544 "queue fcpcqid=%d\n", cq->queue_id);
13545
13546 /* In any case, flash and re-arm the CQ */
13547 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13548
13549 /* wake up worker thread if there are works to be done */
13550 if (workposted)
13551 lpfc_worker_wake_up(phba);
13552}
13553
13554static void
13555lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13556{
13557 struct lpfc_eqe *eqe;
13558
13559 /* walk all the EQ entries and drop on the floor */
13560 while ((eqe = lpfc_sli4_eq_get(eq)))
13561 ;
13562
13563 /* Clear and re-arm the EQ */
13564 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13565}
13566
1ba981fd
JS
13567
13568/**
13569 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13570 * entry
13571 * @phba: Pointer to HBA context object.
13572 * @eqe: Pointer to fast-path event queue entry.
13573 *
13574 * This routine process a event queue entry from the Flash Optimized Fabric
13575 * event queue. It will check the MajorCode and MinorCode to determine this
13576 * is for a completion event on a completion queue, if not, an error shall be
13577 * logged and just return. Otherwise, it will get to the corresponding
13578 * completion queue and process all the entries on the completion queue, rearm
13579 * the completion queue, and then return.
13580 **/
13581static void
13582lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13583{
13584 struct lpfc_queue *cq;
1ba981fd 13585 uint16_t cqid;
1ba981fd
JS
13586
13587 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13589 "9147 Not a valid completion "
13590 "event: majorcode=x%x, minorcode=x%x\n",
13591 bf_get_le32(lpfc_eqe_major_code, eqe),
13592 bf_get_le32(lpfc_eqe_minor_code, eqe));
13593 return;
13594 }
13595
13596 /* Get the reference to the corresponding CQ */
13597 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13598
13599 /* Next check for OAS */
13600 cq = phba->sli4_hba.oas_cq;
13601 if (unlikely(!cq)) {
13602 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13604 "9148 OAS completion queue "
13605 "does not exist\n");
13606 return;
13607 }
13608
13609 if (unlikely(cqid != cq->queue_id)) {
13610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13611 "9149 Miss-matched fast-path compl "
13612 "queue id: eqcqid=%d, fcpcqid=%d\n",
13613 cqid, cq->queue_id);
13614 return;
13615 }
13616
d41b65bc
JS
13617 /* Save EQ associated with this CQ */
13618 cq->assoc_qp = phba->sli4_hba.fof_eq;
13619
f485c18d
DK
13620 /* CQ work will be processed on CPU affinitized to this IRQ */
13621 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13623 "0367 Cannot schedule soft IRQ "
13624 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13625 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13626}
13627
13628/**
13629 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13630 * @irq: Interrupt number.
13631 * @dev_id: The device context pointer.
13632 *
13633 * This function is directly called from the PCI layer as an interrupt
13634 * service routine when device with SLI-4 interface spec is enabled with
13635 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13636 * IOCB ring event in the HBA. However, when the device is enabled with either
13637 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13638 * device-level interrupt handler. When the PCI slot is in error recovery
13639 * or the HBA is undergoing initialization, the interrupt handler will not
13640 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13641 * the intrrupt context. This function is called without any lock held.
13642 * It gets the hbalock to access and update SLI data structures. Note that,
13643 * the EQ to CQ are one-to-one map such that the EQ index is
13644 * equal to that of CQ index.
13645 *
13646 * This function returns IRQ_HANDLED when interrupt is handled else it
13647 * returns IRQ_NONE.
13648 **/
13649irqreturn_t
13650lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13651{
13652 struct lpfc_hba *phba;
895427bd 13653 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13654 struct lpfc_queue *eq;
13655 struct lpfc_eqe *eqe;
13656 unsigned long iflag;
13657 int ecount = 0;
1ba981fd
JS
13658
13659 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13660 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13661 phba = hba_eq_hdl->phba;
1ba981fd
JS
13662
13663 if (unlikely(!phba))
13664 return IRQ_NONE;
13665
13666 /* Get to the EQ struct associated with this vector */
13667 eq = phba->sli4_hba.fof_eq;
13668 if (unlikely(!eq))
13669 return IRQ_NONE;
13670
13671 /* Check device state for handling interrupt */
13672 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13673 /* Check again for link_state with lock held */
13674 spin_lock_irqsave(&phba->hbalock, iflag);
13675 if (phba->link_state < LPFC_LINK_DOWN)
13676 /* Flush, clear interrupt, and rearm the EQ */
13677 lpfc_sli4_eq_flush(phba, eq);
13678 spin_unlock_irqrestore(&phba->hbalock, iflag);
13679 return IRQ_NONE;
13680 }
13681
13682 /*
13683 * Process all the event on FCP fast-path EQ
13684 */
13685 while ((eqe = lpfc_sli4_eq_get(eq))) {
13686 lpfc_sli4_fof_handle_eqe(phba, eqe);
13687 if (!(++ecount % eq->entry_repost))
7869da18 13688 break;
1ba981fd
JS
13689 eq->EQ_processed++;
13690 }
13691
13692 /* Track the max number of EQEs processed in 1 intr */
13693 if (ecount > eq->EQ_max_eqe)
13694 eq->EQ_max_eqe = ecount;
13695
13696
13697 if (unlikely(ecount == 0)) {
13698 eq->EQ_no_entry++;
13699
13700 if (phba->intr_type == MSIX)
13701 /* MSI-X treated interrupt served as no EQ share INT */
13702 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13703 "9145 MSI-X interrupt with no EQE\n");
13704 else {
13705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13706 "9146 ISR interrupt with no EQE\n");
13707 /* Non MSI-X treated on interrupt as EQ share INT */
13708 return IRQ_NONE;
13709 }
13710 }
13711 /* Always clear and re-arm the fast-path EQ */
13712 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13713 return IRQ_HANDLED;
13714}
13715
4f774513 13716/**
67d12733 13717 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13718 * @irq: Interrupt number.
13719 * @dev_id: The device context pointer.
13720 *
13721 * This function is directly called from the PCI layer as an interrupt
13722 * service routine when device with SLI-4 interface spec is enabled with
13723 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13724 * ring event in the HBA. However, when the device is enabled with either
13725 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13726 * device-level interrupt handler. When the PCI slot is in error recovery
13727 * or the HBA is undergoing initialization, the interrupt handler will not
13728 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13729 * the intrrupt context. This function is called without any lock held.
13730 * It gets the hbalock to access and update SLI data structures. Note that,
13731 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13732 * equal to that of FCP CQ index.
13733 *
67d12733
JS
13734 * The link attention and ELS ring attention events are handled
13735 * by the worker thread. The interrupt handler signals the worker thread
13736 * and returns for these events. This function is called without any lock
13737 * held. It gets the hbalock to access and update SLI data structures.
13738 *
4f774513
JS
13739 * This function returns IRQ_HANDLED when interrupt is handled else it
13740 * returns IRQ_NONE.
13741 **/
13742irqreturn_t
67d12733 13743lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13744{
13745 struct lpfc_hba *phba;
895427bd 13746 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13747 struct lpfc_queue *fpeq;
13748 struct lpfc_eqe *eqe;
13749 unsigned long iflag;
13750 int ecount = 0;
895427bd 13751 int hba_eqidx;
4f774513
JS
13752
13753 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13754 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13755 phba = hba_eq_hdl->phba;
13756 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13757
13758 if (unlikely(!phba))
13759 return IRQ_NONE;
67d12733 13760 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13761 return IRQ_NONE;
4f774513
JS
13762
13763 /* Get to the EQ struct associated with this vector */
895427bd 13764 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13765 if (unlikely(!fpeq))
13766 return IRQ_NONE;
4f774513 13767
ba20c853 13768 if (lpfc_fcp_look_ahead) {
895427bd 13769 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
ba20c853
JS
13770 lpfc_sli4_eq_clr_intr(fpeq);
13771 else {
895427bd 13772 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13773 return IRQ_NONE;
13774 }
13775 }
13776
4f774513
JS
13777 /* Check device state for handling interrupt */
13778 if (unlikely(lpfc_intr_state_check(phba))) {
13779 /* Check again for link_state with lock held */
13780 spin_lock_irqsave(&phba->hbalock, iflag);
13781 if (phba->link_state < LPFC_LINK_DOWN)
13782 /* Flush, clear interrupt, and rearm the EQ */
13783 lpfc_sli4_eq_flush(phba, fpeq);
13784 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13785 if (lpfc_fcp_look_ahead)
895427bd 13786 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
13787 return IRQ_NONE;
13788 }
13789
13790 /*
13791 * Process all the event on FCP fast-path EQ
13792 */
13793 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
13794 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13795 if (!(++ecount % fpeq->entry_repost))
7869da18 13796 break;
b84daac9 13797 fpeq->EQ_processed++;
4f774513
JS
13798 }
13799
b84daac9
JS
13800 /* Track the max number of EQEs processed in 1 intr */
13801 if (ecount > fpeq->EQ_max_eqe)
13802 fpeq->EQ_max_eqe = ecount;
13803
4f774513
JS
13804 /* Always clear and re-arm the fast-path EQ */
13805 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13806
13807 if (unlikely(ecount == 0)) {
b84daac9 13808 fpeq->EQ_no_entry++;
ba20c853
JS
13809
13810 if (lpfc_fcp_look_ahead) {
895427bd 13811 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13812 return IRQ_NONE;
13813 }
13814
4f774513
JS
13815 if (phba->intr_type == MSIX)
13816 /* MSI-X treated interrupt served as no EQ share INT */
13817 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13818 "0358 MSI-X interrupt with no EQE\n");
13819 else
13820 /* Non MSI-X treated on interrupt as EQ share INT */
13821 return IRQ_NONE;
13822 }
13823
ba20c853 13824 if (lpfc_fcp_look_ahead)
895427bd
JS
13825 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13826
4f774513
JS
13827 return IRQ_HANDLED;
13828} /* lpfc_sli4_fp_intr_handler */
13829
13830/**
13831 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13832 * @irq: Interrupt number.
13833 * @dev_id: The device context pointer.
13834 *
13835 * This function is the device-level interrupt handler to device with SLI-4
13836 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13837 * interrupt mode is enabled and there is an event in the HBA which requires
13838 * driver attention. This function invokes the slow-path interrupt attention
13839 * handling function and fast-path interrupt attention handling function in
13840 * turn to process the relevant HBA attention events. This function is called
13841 * without any lock held. It gets the hbalock to access and update SLI data
13842 * structures.
13843 *
13844 * This function returns IRQ_HANDLED when interrupt is handled, else it
13845 * returns IRQ_NONE.
13846 **/
13847irqreturn_t
13848lpfc_sli4_intr_handler(int irq, void *dev_id)
13849{
13850 struct lpfc_hba *phba;
67d12733
JS
13851 irqreturn_t hba_irq_rc;
13852 bool hba_handled = false;
895427bd 13853 int qidx;
4f774513
JS
13854
13855 /* Get the driver's phba structure from the dev_id */
13856 phba = (struct lpfc_hba *)dev_id;
13857
13858 if (unlikely(!phba))
13859 return IRQ_NONE;
13860
4f774513
JS
13861 /*
13862 * Invoke fast-path host attention interrupt handling as appropriate.
13863 */
895427bd 13864 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 13865 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 13866 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
13867 if (hba_irq_rc == IRQ_HANDLED)
13868 hba_handled |= true;
4f774513
JS
13869 }
13870
1ba981fd
JS
13871 if (phba->cfg_fof) {
13872 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 13873 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
13874 if (hba_irq_rc == IRQ_HANDLED)
13875 hba_handled |= true;
13876 }
13877
67d12733 13878 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
13879} /* lpfc_sli4_intr_handler */
13880
13881/**
13882 * lpfc_sli4_queue_free - free a queue structure and associated memory
13883 * @queue: The queue structure to free.
13884 *
b595076a 13885 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
13886 * the host resident queue. This function must be called after destroying the
13887 * queue on the HBA.
13888 **/
13889void
13890lpfc_sli4_queue_free(struct lpfc_queue *queue)
13891{
13892 struct lpfc_dmabuf *dmabuf;
13893
13894 if (!queue)
13895 return;
13896
13897 while (!list_empty(&queue->page_list)) {
13898 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13899 list);
81b96eda 13900 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
13901 dmabuf->virt, dmabuf->phys);
13902 kfree(dmabuf);
13903 }
895427bd
JS
13904 if (queue->rqbp) {
13905 lpfc_free_rq_buffer(queue->phba, queue);
13906 kfree(queue->rqbp);
13907 }
d1f525aa
JS
13908
13909 if (!list_empty(&queue->wq_list))
13910 list_del(&queue->wq_list);
13911
4f774513
JS
13912 kfree(queue);
13913 return;
13914}
13915
13916/**
13917 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13918 * @phba: The HBA that this queue is being created on.
81b96eda 13919 * @page_size: The size of a queue page
4f774513
JS
13920 * @entry_size: The size of each queue entry for this queue.
13921 * @entry count: The number of entries that this queue will handle.
13922 *
13923 * This function allocates a queue structure and the DMAable memory used for
13924 * the host resident queue. This function must be called before creating the
13925 * queue on the HBA.
13926 **/
13927struct lpfc_queue *
81b96eda
JS
13928lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13929 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
13930{
13931 struct lpfc_queue *queue;
13932 struct lpfc_dmabuf *dmabuf;
13933 int x, total_qe_count;
13934 void *dma_pointer;
cb5172ea 13935 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 13936
cb5172ea 13937 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 13938 hw_page_size = page_size;
cb5172ea 13939
4f774513
JS
13940 queue = kzalloc(sizeof(struct lpfc_queue) +
13941 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13942 if (!queue)
13943 return NULL;
cb5172ea
JS
13944 queue->page_count = (ALIGN(entry_size * entry_count,
13945 hw_page_size))/hw_page_size;
895427bd
JS
13946
13947 /* If needed, Adjust page count to match the max the adapter supports */
13948 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13949 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13950
4f774513 13951 INIT_LIST_HEAD(&queue->list);
895427bd 13952 INIT_LIST_HEAD(&queue->wq_list);
4f774513
JS
13953 INIT_LIST_HEAD(&queue->page_list);
13954 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
13955
13956 /* Set queue parameters now. If the system cannot provide memory
13957 * resources, the free routine needs to know what was allocated.
13958 */
13959 queue->entry_size = entry_size;
13960 queue->entry_count = entry_count;
13961 queue->page_size = hw_page_size;
13962 queue->phba = phba;
13963
4f774513
JS
13964 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13965 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13966 if (!dmabuf)
13967 goto out_fail;
1aee383d
JP
13968 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13969 hw_page_size, &dmabuf->phys,
13970 GFP_KERNEL);
4f774513
JS
13971 if (!dmabuf->virt) {
13972 kfree(dmabuf);
13973 goto out_fail;
13974 }
13975 dmabuf->buffer_tag = x;
13976 list_add_tail(&dmabuf->list, &queue->page_list);
13977 /* initialize queue's entry array */
13978 dma_pointer = dmabuf->virt;
13979 for (; total_qe_count < entry_count &&
cb5172ea 13980 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
13981 total_qe_count++, dma_pointer += entry_size) {
13982 queue->qe[total_qe_count].address = dma_pointer;
13983 }
13984 }
f485c18d
DK
13985 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
13986 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 13987
64eb4dcb
JS
13988 /* entry_repost will be set during q creation */
13989
4f774513
JS
13990 return queue;
13991out_fail:
13992 lpfc_sli4_queue_free(queue);
13993 return NULL;
13994}
13995
962bc51b
JS
13996/**
13997 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13998 * @phba: HBA structure that indicates port to create a queue on.
13999 * @pci_barset: PCI BAR set flag.
14000 *
14001 * This function shall perform iomap of the specified PCI BAR address to host
14002 * memory address if not already done so and return it. The returned host
14003 * memory address can be NULL.
14004 */
14005static void __iomem *
14006lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14007{
962bc51b
JS
14008 if (!phba->pcidev)
14009 return NULL;
962bc51b
JS
14010
14011 switch (pci_barset) {
14012 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14013 return phba->pci_bar0_memmap_p;
14014 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14015 return phba->pci_bar2_memmap_p;
14016 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14017 return phba->pci_bar4_memmap_p;
14018 default:
14019 break;
14020 }
14021 return NULL;
14022}
14023
173edbb2 14024/**
895427bd 14025 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14026 * @phba: HBA structure that indicates port to create a queue on.
14027 * @startq: The starting FCP EQ to modify
14028 *
14029 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14030 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14031 * updated in one mailbox command.
173edbb2
JS
14032 *
14033 * The @phba struct is used to send mailbox command to HBA. The @startq
14034 * is used to get the starting FCP EQ to change.
14035 * This function is asynchronous and will wait for the mailbox
14036 * command to finish before continuing.
14037 *
14038 * On success this function will return a zero. If unable to allocate enough
14039 * memory this function will return -ENOMEM. If the queue create mailbox command
14040 * fails this function will return -ENXIO.
14041 **/
a2fc4aef 14042int
0cf07f84
JS
14043lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14044 uint32_t numq, uint32_t imax)
173edbb2
JS
14045{
14046 struct lpfc_mbx_modify_eq_delay *eq_delay;
14047 LPFC_MBOXQ_t *mbox;
14048 struct lpfc_queue *eq;
14049 int cnt, rc, length, status = 0;
14050 uint32_t shdr_status, shdr_add_status;
0cf07f84 14051 uint32_t result, val;
895427bd 14052 int qidx;
173edbb2
JS
14053 union lpfc_sli4_cfg_shdr *shdr;
14054 uint16_t dmult;
14055
895427bd 14056 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14057 return 0;
14058
14059 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14060 if (!mbox)
14061 return -ENOMEM;
14062 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14063 sizeof(struct lpfc_sli4_cfg_mhdr));
14064 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14065 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14066 length, LPFC_SLI4_MBX_EMBED);
14067 eq_delay = &mbox->u.mqe.un.eq_delay;
14068
14069 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14070 result = imax / phba->io_channel_irqs;
895427bd 14071 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14072 dmult = 0;
14073 else
14074 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14075 if (dmult > LPFC_DMULT_MAX)
14076 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14077
14078 cnt = 0;
895427bd
JS
14079 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14080 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14081 if (!eq)
14082 continue;
0cf07f84 14083 eq->q_mode = imax;
173edbb2
JS
14084 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14085 eq_delay->u.request.eq[cnt].phase = 0;
14086 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14087 cnt++;
0cf07f84
JS
14088
14089 /* q_mode is only used for auto_imax */
14090 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14091 /* Use EQ Delay Register method for q_mode */
14092
14093 /* Convert for EQ Delay register */
14094 val = phba->cfg_fcp_imax;
14095 if (val) {
14096 /* First, interrupts per sec per EQ */
14097 val = phba->cfg_fcp_imax /
14098 phba->io_channel_irqs;
14099
14100 /* us delay between each interrupt */
14101 val = LPFC_SEC_TO_USEC / val;
14102 }
14103 eq->q_mode = val;
14104 } else {
14105 eq->q_mode = imax;
14106 }
14107
14108 if (cnt >= numq)
173edbb2
JS
14109 break;
14110 }
14111 eq_delay->u.request.num_eq = cnt;
14112
14113 mbox->vport = phba->pport;
14114 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14115 mbox->context1 = NULL;
14116 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14117 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14118 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14119 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14120 if (shdr_status || shdr_add_status || rc) {
14121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14122 "2512 MODIFY_EQ_DELAY mailbox failed with "
14123 "status x%x add_status x%x, mbx status x%x\n",
14124 shdr_status, shdr_add_status, rc);
14125 status = -ENXIO;
14126 }
14127 mempool_free(mbox, phba->mbox_mem_pool);
14128 return status;
14129}
14130
4f774513
JS
14131/**
14132 * lpfc_eq_create - Create an Event Queue on the HBA
14133 * @phba: HBA structure that indicates port to create a queue on.
14134 * @eq: The queue structure to use to create the event queue.
14135 * @imax: The maximum interrupt per second limit.
14136 *
14137 * This function creates an event queue, as detailed in @eq, on a port,
14138 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14139 *
14140 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14141 * is used to get the entry count and entry size that are necessary to
14142 * determine the number of pages to allocate and use for this queue. This
14143 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14144 * event queue. This function is asynchronous and will wait for the mailbox
14145 * command to finish before continuing.
14146 *
14147 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14148 * memory this function will return -ENOMEM. If the queue create mailbox command
14149 * fails this function will return -ENXIO.
4f774513 14150 **/
a2fc4aef 14151int
ee02006b 14152lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14153{
14154 struct lpfc_mbx_eq_create *eq_create;
14155 LPFC_MBOXQ_t *mbox;
14156 int rc, length, status = 0;
14157 struct lpfc_dmabuf *dmabuf;
14158 uint32_t shdr_status, shdr_add_status;
14159 union lpfc_sli4_cfg_shdr *shdr;
14160 uint16_t dmult;
49198b37
JS
14161 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14162
2e90f4b5
JS
14163 /* sanity check on queue memory */
14164 if (!eq)
14165 return -ENODEV;
49198b37
JS
14166 if (!phba->sli4_hba.pc_sli4_params.supported)
14167 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14168
14169 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14170 if (!mbox)
14171 return -ENOMEM;
14172 length = (sizeof(struct lpfc_mbx_eq_create) -
14173 sizeof(struct lpfc_sli4_cfg_mhdr));
14174 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14175 LPFC_MBOX_OPCODE_EQ_CREATE,
14176 length, LPFC_SLI4_MBX_EMBED);
14177 eq_create = &mbox->u.mqe.un.eq_create;
14178 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14179 eq->page_count);
14180 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14181 LPFC_EQE_SIZE);
14182 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
14183 /* don't setup delay multiplier using EQ_CREATE */
14184 dmult = 0;
4f774513
JS
14185 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14186 dmult);
14187 switch (eq->entry_count) {
14188 default:
14189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14190 "0360 Unsupported EQ count. (%d)\n",
14191 eq->entry_count);
14192 if (eq->entry_count < 256)
14193 return -EINVAL;
14194 /* otherwise default to smallest count (drop through) */
14195 case 256:
14196 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14197 LPFC_EQ_CNT_256);
14198 break;
14199 case 512:
14200 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14201 LPFC_EQ_CNT_512);
14202 break;
14203 case 1024:
14204 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14205 LPFC_EQ_CNT_1024);
14206 break;
14207 case 2048:
14208 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14209 LPFC_EQ_CNT_2048);
14210 break;
14211 case 4096:
14212 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14213 LPFC_EQ_CNT_4096);
14214 break;
14215 }
14216 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14217 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14218 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14219 putPaddrLow(dmabuf->phys);
14220 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14221 putPaddrHigh(dmabuf->phys);
14222 }
14223 mbox->vport = phba->pport;
14224 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14225 mbox->context1 = NULL;
14226 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14227 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14228 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14229 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14230 if (shdr_status || shdr_add_status || rc) {
14231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14232 "2500 EQ_CREATE mailbox failed with "
14233 "status x%x add_status x%x, mbx status x%x\n",
14234 shdr_status, shdr_add_status, rc);
14235 status = -ENXIO;
14236 }
14237 eq->type = LPFC_EQ;
14238 eq->subtype = LPFC_NONE;
14239 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14240 if (eq->queue_id == 0xFFFF)
14241 status = -ENXIO;
14242 eq->host_index = 0;
14243 eq->hba_index = 0;
64eb4dcb 14244 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14245
8fa38513 14246 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14247 return status;
14248}
14249
14250/**
14251 * lpfc_cq_create - Create a Completion Queue on the HBA
14252 * @phba: HBA structure that indicates port to create a queue on.
14253 * @cq: The queue structure to use to create the completion queue.
14254 * @eq: The event queue to bind this completion queue to.
14255 *
14256 * This function creates a completion queue, as detailed in @wq, on a port,
14257 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14258 *
14259 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14260 * is used to get the entry count and entry size that are necessary to
14261 * determine the number of pages to allocate and use for this queue. The @eq
14262 * is used to indicate which event queue to bind this completion queue to. This
14263 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14264 * completion queue. This function is asynchronous and will wait for the mailbox
14265 * command to finish before continuing.
14266 *
14267 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14268 * memory this function will return -ENOMEM. If the queue create mailbox command
14269 * fails this function will return -ENXIO.
4f774513 14270 **/
a2fc4aef 14271int
4f774513
JS
14272lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14273 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14274{
14275 struct lpfc_mbx_cq_create *cq_create;
14276 struct lpfc_dmabuf *dmabuf;
14277 LPFC_MBOXQ_t *mbox;
14278 int rc, length, status = 0;
14279 uint32_t shdr_status, shdr_add_status;
14280 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14281 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14282
2e90f4b5
JS
14283 /* sanity check on queue memory */
14284 if (!cq || !eq)
14285 return -ENODEV;
49198b37 14286 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14287 hw_page_size = cq->page_size;
49198b37 14288
4f774513
JS
14289 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14290 if (!mbox)
14291 return -ENOMEM;
14292 length = (sizeof(struct lpfc_mbx_cq_create) -
14293 sizeof(struct lpfc_sli4_cfg_mhdr));
14294 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14295 LPFC_MBOX_OPCODE_CQ_CREATE,
14296 length, LPFC_SLI4_MBX_EMBED);
14297 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14298 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14299 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14300 cq->page_count);
14301 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14302 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14303 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14304 phba->sli4_hba.pc_sli4_params.cqv);
14305 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14306 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14307 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14308 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14309 eq->queue_id);
14310 } else {
14311 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14312 eq->queue_id);
14313 }
4f774513 14314 switch (cq->entry_count) {
81b96eda
JS
14315 case 2048:
14316 case 4096:
14317 if (phba->sli4_hba.pc_sli4_params.cqv ==
14318 LPFC_Q_CREATE_VERSION_2) {
14319 cq_create->u.request.context.lpfc_cq_context_count =
14320 cq->entry_count;
14321 bf_set(lpfc_cq_context_count,
14322 &cq_create->u.request.context,
14323 LPFC_CQ_CNT_WORD7);
14324 break;
14325 }
14326 /* Fall Thru */
4f774513
JS
14327 default:
14328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14329 "0361 Unsupported CQ count: "
64eb4dcb 14330 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14331 cq->entry_count, cq->entry_size,
64eb4dcb 14332 cq->page_count);
4f4c1863
JS
14333 if (cq->entry_count < 256) {
14334 status = -EINVAL;
14335 goto out;
14336 }
4f774513
JS
14337 /* otherwise default to smallest count (drop through) */
14338 case 256:
14339 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14340 LPFC_CQ_CNT_256);
14341 break;
14342 case 512:
14343 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14344 LPFC_CQ_CNT_512);
14345 break;
14346 case 1024:
14347 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14348 LPFC_CQ_CNT_1024);
14349 break;
14350 }
14351 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14352 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14353 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14354 putPaddrLow(dmabuf->phys);
14355 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14356 putPaddrHigh(dmabuf->phys);
14357 }
14358 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14359
14360 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14361 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14362 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14363 if (shdr_status || shdr_add_status || rc) {
14364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14365 "2501 CQ_CREATE mailbox failed with "
14366 "status x%x add_status x%x, mbx status x%x\n",
14367 shdr_status, shdr_add_status, rc);
14368 status = -ENXIO;
14369 goto out;
14370 }
14371 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14372 if (cq->queue_id == 0xFFFF) {
14373 status = -ENXIO;
14374 goto out;
14375 }
14376 /* link the cq onto the parent eq child list */
14377 list_add_tail(&cq->list, &eq->child_list);
14378 /* Set up completion queue's type and subtype */
14379 cq->type = type;
14380 cq->subtype = subtype;
14381 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14382 cq->assoc_qid = eq->queue_id;
4f774513
JS
14383 cq->host_index = 0;
14384 cq->hba_index = 0;
64eb4dcb 14385 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14386
8fa38513
JS
14387out:
14388 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14389 return status;
14390}
14391
2d7dbc4c
JS
14392/**
14393 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14394 * @phba: HBA structure that indicates port to create a queue on.
14395 * @cqp: The queue structure array to use to create the completion queues.
14396 * @eqp: The event queue array to bind these completion queues to.
14397 *
14398 * This function creates a set of completion queue, s to support MRQ
14399 * as detailed in @cqp, on a port,
14400 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14401 *
14402 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14403 * is used to get the entry count and entry size that are necessary to
14404 * determine the number of pages to allocate and use for this queue. The @eq
14405 * is used to indicate which event queue to bind this completion queue to. This
14406 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14407 * completion queue. This function is asynchronous and will wait for the mailbox
14408 * command to finish before continuing.
14409 *
14410 * On success this function will return a zero. If unable to allocate enough
14411 * memory this function will return -ENOMEM. If the queue create mailbox command
14412 * fails this function will return -ENXIO.
14413 **/
14414int
14415lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14416 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14417{
14418 struct lpfc_queue *cq;
14419 struct lpfc_queue *eq;
14420 struct lpfc_mbx_cq_create_set *cq_set;
14421 struct lpfc_dmabuf *dmabuf;
14422 LPFC_MBOXQ_t *mbox;
14423 int rc, length, alloclen, status = 0;
14424 int cnt, idx, numcq, page_idx = 0;
14425 uint32_t shdr_status, shdr_add_status;
14426 union lpfc_sli4_cfg_shdr *shdr;
14427 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14428
14429 /* sanity check on queue memory */
14430 numcq = phba->cfg_nvmet_mrq;
14431 if (!cqp || !eqp || !numcq)
14432 return -ENODEV;
2d7dbc4c
JS
14433
14434 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14435 if (!mbox)
14436 return -ENOMEM;
14437
14438 length = sizeof(struct lpfc_mbx_cq_create_set);
14439 length += ((numcq * cqp[0]->page_count) *
14440 sizeof(struct dma_address));
14441 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14442 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14443 LPFC_SLI4_MBX_NEMBED);
14444 if (alloclen < length) {
14445 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14446 "3098 Allocated DMA memory size (%d) is "
14447 "less than the requested DMA memory size "
14448 "(%d)\n", alloclen, length);
14449 status = -ENOMEM;
14450 goto out;
14451 }
14452 cq_set = mbox->sge_array->addr[0];
14453 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14454 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14455
14456 for (idx = 0; idx < numcq; idx++) {
14457 cq = cqp[idx];
14458 eq = eqp[idx];
14459 if (!cq || !eq) {
14460 status = -ENOMEM;
14461 goto out;
14462 }
81b96eda
JS
14463 if (!phba->sli4_hba.pc_sli4_params.supported)
14464 hw_page_size = cq->page_size;
2d7dbc4c
JS
14465
14466 switch (idx) {
14467 case 0:
14468 bf_set(lpfc_mbx_cq_create_set_page_size,
14469 &cq_set->u.request,
14470 (hw_page_size / SLI4_PAGE_SIZE));
14471 bf_set(lpfc_mbx_cq_create_set_num_pages,
14472 &cq_set->u.request, cq->page_count);
14473 bf_set(lpfc_mbx_cq_create_set_evt,
14474 &cq_set->u.request, 1);
14475 bf_set(lpfc_mbx_cq_create_set_valid,
14476 &cq_set->u.request, 1);
14477 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14478 &cq_set->u.request, 0);
14479 bf_set(lpfc_mbx_cq_create_set_num_cq,
14480 &cq_set->u.request, numcq);
14481 switch (cq->entry_count) {
81b96eda
JS
14482 case 2048:
14483 case 4096:
14484 if (phba->sli4_hba.pc_sli4_params.cqv ==
14485 LPFC_Q_CREATE_VERSION_2) {
14486 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14487 &cq_set->u.request,
14488 cq->entry_count);
14489 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14490 &cq_set->u.request,
14491 LPFC_CQ_CNT_WORD7);
14492 break;
14493 }
14494 /* Fall Thru */
2d7dbc4c
JS
14495 default:
14496 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14497 "3118 Bad CQ count. (%d)\n",
14498 cq->entry_count);
14499 if (cq->entry_count < 256) {
14500 status = -EINVAL;
14501 goto out;
14502 }
14503 /* otherwise default to smallest (drop thru) */
14504 case 256:
14505 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14506 &cq_set->u.request, LPFC_CQ_CNT_256);
14507 break;
14508 case 512:
14509 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14510 &cq_set->u.request, LPFC_CQ_CNT_512);
14511 break;
14512 case 1024:
14513 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14514 &cq_set->u.request, LPFC_CQ_CNT_1024);
14515 break;
14516 }
14517 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14518 &cq_set->u.request, eq->queue_id);
14519 break;
14520 case 1:
14521 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14522 &cq_set->u.request, eq->queue_id);
14523 break;
14524 case 2:
14525 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14526 &cq_set->u.request, eq->queue_id);
14527 break;
14528 case 3:
14529 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14530 &cq_set->u.request, eq->queue_id);
14531 break;
14532 case 4:
14533 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14534 &cq_set->u.request, eq->queue_id);
14535 break;
14536 case 5:
14537 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14538 &cq_set->u.request, eq->queue_id);
14539 break;
14540 case 6:
14541 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14542 &cq_set->u.request, eq->queue_id);
14543 break;
14544 case 7:
14545 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14546 &cq_set->u.request, eq->queue_id);
14547 break;
14548 case 8:
14549 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14550 &cq_set->u.request, eq->queue_id);
14551 break;
14552 case 9:
14553 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14554 &cq_set->u.request, eq->queue_id);
14555 break;
14556 case 10:
14557 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14558 &cq_set->u.request, eq->queue_id);
14559 break;
14560 case 11:
14561 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14562 &cq_set->u.request, eq->queue_id);
14563 break;
14564 case 12:
14565 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14566 &cq_set->u.request, eq->queue_id);
14567 break;
14568 case 13:
14569 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14570 &cq_set->u.request, eq->queue_id);
14571 break;
14572 case 14:
14573 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14574 &cq_set->u.request, eq->queue_id);
14575 break;
14576 case 15:
14577 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14578 &cq_set->u.request, eq->queue_id);
14579 break;
14580 }
14581
14582 /* link the cq onto the parent eq child list */
14583 list_add_tail(&cq->list, &eq->child_list);
14584 /* Set up completion queue's type and subtype */
14585 cq->type = type;
14586 cq->subtype = subtype;
14587 cq->assoc_qid = eq->queue_id;
14588 cq->host_index = 0;
14589 cq->hba_index = 0;
64eb4dcb 14590 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14591 cq->chann = idx;
2d7dbc4c
JS
14592
14593 rc = 0;
14594 list_for_each_entry(dmabuf, &cq->page_list, list) {
14595 memset(dmabuf->virt, 0, hw_page_size);
14596 cnt = page_idx + dmabuf->buffer_tag;
14597 cq_set->u.request.page[cnt].addr_lo =
14598 putPaddrLow(dmabuf->phys);
14599 cq_set->u.request.page[cnt].addr_hi =
14600 putPaddrHigh(dmabuf->phys);
14601 rc++;
14602 }
14603 page_idx += rc;
14604 }
14605
14606 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14607
14608 /* The IOCTL status is embedded in the mailbox subheader. */
14609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14611 if (shdr_status || shdr_add_status || rc) {
14612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14613 "3119 CQ_CREATE_SET mailbox failed with "
14614 "status x%x add_status x%x, mbx status x%x\n",
14615 shdr_status, shdr_add_status, rc);
14616 status = -ENXIO;
14617 goto out;
14618 }
14619 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14620 if (rc == 0xFFFF) {
14621 status = -ENXIO;
14622 goto out;
14623 }
14624
14625 for (idx = 0; idx < numcq; idx++) {
14626 cq = cqp[idx];
14627 cq->queue_id = rc + idx;
14628 }
14629
14630out:
14631 lpfc_sli4_mbox_cmd_free(phba, mbox);
14632 return status;
14633}
14634
b19a061a
JS
14635/**
14636 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14637 * @phba: HBA structure that indicates port to create a queue on.
14638 * @mq: The queue structure to use to create the mailbox queue.
14639 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14640 * @cq: The completion queue to associate with this cq.
14641 *
14642 * This function provides failback (fb) functionality when the
14643 * mq_create_ext fails on older FW generations. It's purpose is identical
14644 * to mq_create_ext otherwise.
14645 *
14646 * This routine cannot fail as all attributes were previously accessed and
14647 * initialized in mq_create_ext.
14648 **/
14649static void
14650lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14651 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14652{
14653 struct lpfc_mbx_mq_create *mq_create;
14654 struct lpfc_dmabuf *dmabuf;
14655 int length;
14656
14657 length = (sizeof(struct lpfc_mbx_mq_create) -
14658 sizeof(struct lpfc_sli4_cfg_mhdr));
14659 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14660 LPFC_MBOX_OPCODE_MQ_CREATE,
14661 length, LPFC_SLI4_MBX_EMBED);
14662 mq_create = &mbox->u.mqe.un.mq_create;
14663 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14664 mq->page_count);
14665 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14666 cq->queue_id);
14667 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14668 switch (mq->entry_count) {
14669 case 16:
5a6f133e
JS
14670 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14671 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14672 break;
14673 case 32:
5a6f133e
JS
14674 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14675 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14676 break;
14677 case 64:
5a6f133e
JS
14678 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14679 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14680 break;
14681 case 128:
5a6f133e
JS
14682 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14683 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14684 break;
14685 }
14686 list_for_each_entry(dmabuf, &mq->page_list, list) {
14687 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14688 putPaddrLow(dmabuf->phys);
14689 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14690 putPaddrHigh(dmabuf->phys);
14691 }
14692}
14693
04c68496
JS
14694/**
14695 * lpfc_mq_create - Create a mailbox Queue on the HBA
14696 * @phba: HBA structure that indicates port to create a queue on.
14697 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14698 * @cq: The completion queue to associate with this cq.
14699 * @subtype: The queue's subtype.
04c68496
JS
14700 *
14701 * This function creates a mailbox queue, as detailed in @mq, on a port,
14702 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14703 *
14704 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14705 * is used to get the entry count and entry size that are necessary to
14706 * determine the number of pages to allocate and use for this queue. This
14707 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14708 * mailbox queue. This function is asynchronous and will wait for the mailbox
14709 * command to finish before continuing.
14710 *
14711 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14712 * memory this function will return -ENOMEM. If the queue create mailbox command
14713 * fails this function will return -ENXIO.
04c68496 14714 **/
b19a061a 14715int32_t
04c68496
JS
14716lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14717 struct lpfc_queue *cq, uint32_t subtype)
14718{
14719 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14720 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14721 struct lpfc_dmabuf *dmabuf;
14722 LPFC_MBOXQ_t *mbox;
14723 int rc, length, status = 0;
14724 uint32_t shdr_status, shdr_add_status;
14725 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14726 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14727
2e90f4b5
JS
14728 /* sanity check on queue memory */
14729 if (!mq || !cq)
14730 return -ENODEV;
49198b37
JS
14731 if (!phba->sli4_hba.pc_sli4_params.supported)
14732 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14733
04c68496
JS
14734 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14735 if (!mbox)
14736 return -ENOMEM;
b19a061a 14737 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14738 sizeof(struct lpfc_sli4_cfg_mhdr));
14739 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14740 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14741 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14742
14743 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14744 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14745 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14746 &mq_create_ext->u.request, mq->page_count);
14747 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14748 &mq_create_ext->u.request, 1);
14749 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14750 &mq_create_ext->u.request, 1);
14751 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14752 &mq_create_ext->u.request, 1);
70f3c073
JS
14753 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14754 &mq_create_ext->u.request, 1);
14755 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14756 &mq_create_ext->u.request, 1);
b19a061a 14757 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14758 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14759 phba->sli4_hba.pc_sli4_params.mqv);
14760 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14761 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14762 cq->queue_id);
14763 else
14764 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14765 cq->queue_id);
04c68496
JS
14766 switch (mq->entry_count) {
14767 default:
14768 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14769 "0362 Unsupported MQ count. (%d)\n",
14770 mq->entry_count);
4f4c1863
JS
14771 if (mq->entry_count < 16) {
14772 status = -EINVAL;
14773 goto out;
14774 }
04c68496
JS
14775 /* otherwise default to smallest count (drop through) */
14776 case 16:
5a6f133e
JS
14777 bf_set(lpfc_mq_context_ring_size,
14778 &mq_create_ext->u.request.context,
14779 LPFC_MQ_RING_SIZE_16);
04c68496
JS
14780 break;
14781 case 32:
5a6f133e
JS
14782 bf_set(lpfc_mq_context_ring_size,
14783 &mq_create_ext->u.request.context,
14784 LPFC_MQ_RING_SIZE_32);
04c68496
JS
14785 break;
14786 case 64:
5a6f133e
JS
14787 bf_set(lpfc_mq_context_ring_size,
14788 &mq_create_ext->u.request.context,
14789 LPFC_MQ_RING_SIZE_64);
04c68496
JS
14790 break;
14791 case 128:
5a6f133e
JS
14792 bf_set(lpfc_mq_context_ring_size,
14793 &mq_create_ext->u.request.context,
14794 LPFC_MQ_RING_SIZE_128);
04c68496
JS
14795 break;
14796 }
14797 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 14798 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 14799 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 14800 putPaddrLow(dmabuf->phys);
b19a061a 14801 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
14802 putPaddrHigh(dmabuf->phys);
14803 }
14804 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
14805 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14806 &mq_create_ext->u.response);
14807 if (rc != MBX_SUCCESS) {
14808 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14809 "2795 MQ_CREATE_EXT failed with "
14810 "status x%x. Failback to MQ_CREATE.\n",
14811 rc);
14812 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14813 mq_create = &mbox->u.mqe.un.mq_create;
14814 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14815 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14816 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14817 &mq_create->u.response);
14818 }
14819
04c68496 14820 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
14821 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14822 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14823 if (shdr_status || shdr_add_status || rc) {
14824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14825 "2502 MQ_CREATE mailbox failed with "
14826 "status x%x add_status x%x, mbx status x%x\n",
14827 shdr_status, shdr_add_status, rc);
14828 status = -ENXIO;
14829 goto out;
14830 }
04c68496
JS
14831 if (mq->queue_id == 0xFFFF) {
14832 status = -ENXIO;
14833 goto out;
14834 }
14835 mq->type = LPFC_MQ;
2a622bfb 14836 mq->assoc_qid = cq->queue_id;
04c68496
JS
14837 mq->subtype = subtype;
14838 mq->host_index = 0;
14839 mq->hba_index = 0;
64eb4dcb 14840 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
14841
14842 /* link the mq onto the parent cq child list */
14843 list_add_tail(&mq->list, &cq->child_list);
14844out:
8fa38513 14845 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
14846 return status;
14847}
14848
4f774513
JS
14849/**
14850 * lpfc_wq_create - Create a Work Queue on the HBA
14851 * @phba: HBA structure that indicates port to create a queue on.
14852 * @wq: The queue structure to use to create the work queue.
14853 * @cq: The completion queue to bind this work queue to.
14854 * @subtype: The subtype of the work queue indicating its functionality.
14855 *
14856 * This function creates a work queue, as detailed in @wq, on a port, described
14857 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14858 *
14859 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14860 * is used to get the entry count and entry size that are necessary to
14861 * determine the number of pages to allocate and use for this queue. The @cq
14862 * is used to indicate which completion queue to bind this work queue to. This
14863 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14864 * work queue. This function is asynchronous and will wait for the mailbox
14865 * command to finish before continuing.
14866 *
14867 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14868 * memory this function will return -ENOMEM. If the queue create mailbox command
14869 * fails this function will return -ENXIO.
4f774513 14870 **/
a2fc4aef 14871int
4f774513
JS
14872lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14873 struct lpfc_queue *cq, uint32_t subtype)
14874{
14875 struct lpfc_mbx_wq_create *wq_create;
14876 struct lpfc_dmabuf *dmabuf;
14877 LPFC_MBOXQ_t *mbox;
14878 int rc, length, status = 0;
14879 uint32_t shdr_status, shdr_add_status;
14880 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14881 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 14882 struct dma_address *page;
962bc51b
JS
14883 void __iomem *bar_memmap_p;
14884 uint32_t db_offset;
14885 uint16_t pci_barset;
81b96eda 14886 uint8_t wq_create_version;
49198b37 14887
2e90f4b5
JS
14888 /* sanity check on queue memory */
14889 if (!wq || !cq)
14890 return -ENODEV;
49198b37 14891 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14892 hw_page_size = wq->page_size;
4f774513
JS
14893
14894 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14895 if (!mbox)
14896 return -ENOMEM;
14897 length = (sizeof(struct lpfc_mbx_wq_create) -
14898 sizeof(struct lpfc_sli4_cfg_mhdr));
14899 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14900 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14901 length, LPFC_SLI4_MBX_EMBED);
14902 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 14903 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
14904 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14905 wq->page_count);
14906 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14907 cq->queue_id);
0c651878
JS
14908
14909 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
14910 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14911 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 14912
81b96eda
JS
14913 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
14914 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14915 else
14916 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14917
14918 switch (wq_create_version) {
0c651878
JS
14919 case LPFC_Q_CREATE_VERSION_0:
14920 switch (wq->entry_size) {
14921 default:
14922 case 64:
14923 /* Nothing to do, version 0 ONLY supports 64 byte */
14924 page = wq_create->u.request.page;
14925 break;
14926 case 128:
14927 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14928 LPFC_WQ_SZ128_SUPPORT)) {
14929 status = -ERANGE;
14930 goto out;
14931 }
14932 /* If we get here the HBA MUST also support V1 and
14933 * we MUST use it
14934 */
14935 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14936 LPFC_Q_CREATE_VERSION_1);
14937
14938 bf_set(lpfc_mbx_wq_create_wqe_count,
14939 &wq_create->u.request_1, wq->entry_count);
14940 bf_set(lpfc_mbx_wq_create_wqe_size,
14941 &wq_create->u.request_1,
14942 LPFC_WQ_WQE_SIZE_128);
14943 bf_set(lpfc_mbx_wq_create_page_size,
14944 &wq_create->u.request_1,
8ea73db4 14945 LPFC_WQ_PAGE_SIZE_4096);
0c651878
JS
14946 page = wq_create->u.request_1.page;
14947 break;
14948 }
14949 break;
14950 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
14951 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14952 wq->entry_count);
3f247de7
JS
14953 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14954 LPFC_Q_CREATE_VERSION_1);
14955
5a6f133e
JS
14956 switch (wq->entry_size) {
14957 default:
14958 case 64:
14959 bf_set(lpfc_mbx_wq_create_wqe_size,
14960 &wq_create->u.request_1,
14961 LPFC_WQ_WQE_SIZE_64);
14962 break;
14963 case 128:
0c651878
JS
14964 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14965 LPFC_WQ_SZ128_SUPPORT)) {
14966 status = -ERANGE;
14967 goto out;
14968 }
5a6f133e
JS
14969 bf_set(lpfc_mbx_wq_create_wqe_size,
14970 &wq_create->u.request_1,
14971 LPFC_WQ_WQE_SIZE_128);
14972 break;
14973 }
8ea73db4
JS
14974 bf_set(lpfc_mbx_wq_create_page_size,
14975 &wq_create->u.request_1,
81b96eda 14976 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 14977 page = wq_create->u.request_1.page;
0c651878
JS
14978 break;
14979 default:
14980 status = -ERANGE;
14981 goto out;
5a6f133e 14982 }
0c651878 14983
4f774513 14984 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 14985 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
14986 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14987 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 14988 }
962bc51b
JS
14989
14990 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14991 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14992
4f774513
JS
14993 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14994 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14995 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14996 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14997 if (shdr_status || shdr_add_status || rc) {
14998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14999 "2503 WQ_CREATE mailbox failed with "
15000 "status x%x add_status x%x, mbx status x%x\n",
15001 shdr_status, shdr_add_status, rc);
15002 status = -ENXIO;
15003 goto out;
15004 }
15005 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15006 if (wq->queue_id == 0xFFFF) {
15007 status = -ENXIO;
15008 goto out;
15009 }
962bc51b
JS
15010 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15011 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15012 &wq_create->u.response);
15013 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15014 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15016 "3265 WQ[%d] doorbell format not "
15017 "supported: x%x\n", wq->queue_id,
15018 wq->db_format);
15019 status = -EINVAL;
15020 goto out;
15021 }
15022 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15023 &wq_create->u.response);
15024 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15025 if (!bar_memmap_p) {
15026 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15027 "3263 WQ[%d] failed to memmap pci "
15028 "barset:x%x\n", wq->queue_id,
15029 pci_barset);
15030 status = -ENOMEM;
15031 goto out;
15032 }
15033 db_offset = wq_create->u.response.doorbell_offset;
15034 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15035 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15037 "3252 WQ[%d] doorbell offset not "
15038 "supported: x%x\n", wq->queue_id,
15039 db_offset);
15040 status = -EINVAL;
15041 goto out;
15042 }
15043 wq->db_regaddr = bar_memmap_p + db_offset;
15044 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15045 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15046 "format:x%x\n", wq->queue_id, pci_barset,
15047 db_offset, wq->db_format);
962bc51b
JS
15048 } else {
15049 wq->db_format = LPFC_DB_LIST_FORMAT;
15050 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15051 }
895427bd
JS
15052 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15053 if (wq->pring == NULL) {
15054 status = -ENOMEM;
15055 goto out;
15056 }
4f774513 15057 wq->type = LPFC_WQ;
2a622bfb 15058 wq->assoc_qid = cq->queue_id;
4f774513
JS
15059 wq->subtype = subtype;
15060 wq->host_index = 0;
15061 wq->hba_index = 0;
ff78d8f9 15062 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15063
15064 /* link the wq onto the parent cq child list */
15065 list_add_tail(&wq->list, &cq->child_list);
15066out:
8fa38513 15067 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15068 return status;
15069}
15070
15071/**
15072 * lpfc_rq_create - Create a Receive Queue on the HBA
15073 * @phba: HBA structure that indicates port to create a queue on.
15074 * @hrq: The queue structure to use to create the header receive queue.
15075 * @drq: The queue structure to use to create the data receive queue.
15076 * @cq: The completion queue to bind this work queue to.
15077 *
15078 * This function creates a receive buffer queue pair , as detailed in @hrq and
15079 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15080 * to the HBA.
15081 *
15082 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15083 * struct is used to get the entry count that is necessary to determine the
15084 * number of pages to use for this queue. The @cq is used to indicate which
15085 * completion queue to bind received buffers that are posted to these queues to.
15086 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15087 * receive queue pair. This function is asynchronous and will wait for the
15088 * mailbox command to finish before continuing.
15089 *
15090 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15091 * memory this function will return -ENOMEM. If the queue create mailbox command
15092 * fails this function will return -ENXIO.
4f774513 15093 **/
a2fc4aef 15094int
4f774513
JS
15095lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15096 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15097{
15098 struct lpfc_mbx_rq_create *rq_create;
15099 struct lpfc_dmabuf *dmabuf;
15100 LPFC_MBOXQ_t *mbox;
15101 int rc, length, status = 0;
15102 uint32_t shdr_status, shdr_add_status;
15103 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15104 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15105 void __iomem *bar_memmap_p;
15106 uint32_t db_offset;
15107 uint16_t pci_barset;
49198b37 15108
2e90f4b5
JS
15109 /* sanity check on queue memory */
15110 if (!hrq || !drq || !cq)
15111 return -ENODEV;
49198b37
JS
15112 if (!phba->sli4_hba.pc_sli4_params.supported)
15113 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15114
15115 if (hrq->entry_count != drq->entry_count)
15116 return -EINVAL;
15117 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15118 if (!mbox)
15119 return -ENOMEM;
15120 length = (sizeof(struct lpfc_mbx_rq_create) -
15121 sizeof(struct lpfc_sli4_cfg_mhdr));
15122 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15123 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15124 length, LPFC_SLI4_MBX_EMBED);
15125 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15126 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15127 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15128 phba->sli4_hba.pc_sli4_params.rqv);
15129 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15130 bf_set(lpfc_rq_context_rqe_count_1,
15131 &rq_create->u.request.context,
15132 hrq->entry_count);
15133 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15134 bf_set(lpfc_rq_context_rqe_size,
15135 &rq_create->u.request.context,
15136 LPFC_RQE_SIZE_8);
15137 bf_set(lpfc_rq_context_page_size,
15138 &rq_create->u.request.context,
8ea73db4 15139 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15140 } else {
15141 switch (hrq->entry_count) {
15142 default:
15143 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15144 "2535 Unsupported RQ count. (%d)\n",
15145 hrq->entry_count);
4f4c1863
JS
15146 if (hrq->entry_count < 512) {
15147 status = -EINVAL;
15148 goto out;
15149 }
5a6f133e
JS
15150 /* otherwise default to smallest count (drop through) */
15151 case 512:
15152 bf_set(lpfc_rq_context_rqe_count,
15153 &rq_create->u.request.context,
15154 LPFC_RQ_RING_SIZE_512);
15155 break;
15156 case 1024:
15157 bf_set(lpfc_rq_context_rqe_count,
15158 &rq_create->u.request.context,
15159 LPFC_RQ_RING_SIZE_1024);
15160 break;
15161 case 2048:
15162 bf_set(lpfc_rq_context_rqe_count,
15163 &rq_create->u.request.context,
15164 LPFC_RQ_RING_SIZE_2048);
15165 break;
15166 case 4096:
15167 bf_set(lpfc_rq_context_rqe_count,
15168 &rq_create->u.request.context,
15169 LPFC_RQ_RING_SIZE_4096);
15170 break;
15171 }
15172 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15173 LPFC_HDR_BUF_SIZE);
4f774513
JS
15174 }
15175 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15176 cq->queue_id);
15177 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15178 hrq->page_count);
4f774513 15179 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15180 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15181 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15182 putPaddrLow(dmabuf->phys);
15183 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15184 putPaddrHigh(dmabuf->phys);
15185 }
962bc51b
JS
15186 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15187 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15188
4f774513
JS
15189 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15190 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15191 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15192 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15193 if (shdr_status || shdr_add_status || rc) {
15194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15195 "2504 RQ_CREATE mailbox failed with "
15196 "status x%x add_status x%x, mbx status x%x\n",
15197 shdr_status, shdr_add_status, rc);
15198 status = -ENXIO;
15199 goto out;
15200 }
15201 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15202 if (hrq->queue_id == 0xFFFF) {
15203 status = -ENXIO;
15204 goto out;
15205 }
962bc51b
JS
15206
15207 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15208 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15209 &rq_create->u.response);
15210 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15211 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15213 "3262 RQ [%d] doorbell format not "
15214 "supported: x%x\n", hrq->queue_id,
15215 hrq->db_format);
15216 status = -EINVAL;
15217 goto out;
15218 }
15219
15220 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15221 &rq_create->u.response);
15222 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15223 if (!bar_memmap_p) {
15224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15225 "3269 RQ[%d] failed to memmap pci "
15226 "barset:x%x\n", hrq->queue_id,
15227 pci_barset);
15228 status = -ENOMEM;
15229 goto out;
15230 }
15231
15232 db_offset = rq_create->u.response.doorbell_offset;
15233 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15234 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15236 "3270 RQ[%d] doorbell offset not "
15237 "supported: x%x\n", hrq->queue_id,
15238 db_offset);
15239 status = -EINVAL;
15240 goto out;
15241 }
15242 hrq->db_regaddr = bar_memmap_p + db_offset;
15243 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15244 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15245 "format:x%x\n", hrq->queue_id, pci_barset,
15246 db_offset, hrq->db_format);
962bc51b
JS
15247 } else {
15248 hrq->db_format = LPFC_DB_RING_FORMAT;
15249 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15250 }
4f774513 15251 hrq->type = LPFC_HRQ;
2a622bfb 15252 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15253 hrq->subtype = subtype;
15254 hrq->host_index = 0;
15255 hrq->hba_index = 0;
61f3d4bf 15256 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15257
15258 /* now create the data queue */
15259 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15260 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15261 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15262 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15263 phba->sli4_hba.pc_sli4_params.rqv);
15264 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15265 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15266 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15267 if (subtype == LPFC_NVMET)
15268 rq_create->u.request.context.buffer_size =
15269 LPFC_NVMET_DATA_BUF_SIZE;
15270 else
15271 rq_create->u.request.context.buffer_size =
15272 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15273 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15274 LPFC_RQE_SIZE_8);
15275 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15276 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15277 } else {
15278 switch (drq->entry_count) {
15279 default:
15280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15281 "2536 Unsupported RQ count. (%d)\n",
15282 drq->entry_count);
4f4c1863
JS
15283 if (drq->entry_count < 512) {
15284 status = -EINVAL;
15285 goto out;
15286 }
5a6f133e
JS
15287 /* otherwise default to smallest count (drop through) */
15288 case 512:
15289 bf_set(lpfc_rq_context_rqe_count,
15290 &rq_create->u.request.context,
15291 LPFC_RQ_RING_SIZE_512);
15292 break;
15293 case 1024:
15294 bf_set(lpfc_rq_context_rqe_count,
15295 &rq_create->u.request.context,
15296 LPFC_RQ_RING_SIZE_1024);
15297 break;
15298 case 2048:
15299 bf_set(lpfc_rq_context_rqe_count,
15300 &rq_create->u.request.context,
15301 LPFC_RQ_RING_SIZE_2048);
15302 break;
15303 case 4096:
15304 bf_set(lpfc_rq_context_rqe_count,
15305 &rq_create->u.request.context,
15306 LPFC_RQ_RING_SIZE_4096);
15307 break;
15308 }
3c603be9
JS
15309 if (subtype == LPFC_NVMET)
15310 bf_set(lpfc_rq_context_buf_size,
15311 &rq_create->u.request.context,
15312 LPFC_NVMET_DATA_BUF_SIZE);
15313 else
15314 bf_set(lpfc_rq_context_buf_size,
15315 &rq_create->u.request.context,
15316 LPFC_DATA_BUF_SIZE);
4f774513
JS
15317 }
15318 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15319 cq->queue_id);
15320 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15321 drq->page_count);
4f774513
JS
15322 list_for_each_entry(dmabuf, &drq->page_list, list) {
15323 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15324 putPaddrLow(dmabuf->phys);
15325 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15326 putPaddrHigh(dmabuf->phys);
15327 }
962bc51b
JS
15328 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15329 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15330 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15331 /* The IOCTL status is embedded in the mailbox subheader. */
15332 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15333 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15334 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15335 if (shdr_status || shdr_add_status || rc) {
15336 status = -ENXIO;
15337 goto out;
15338 }
15339 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15340 if (drq->queue_id == 0xFFFF) {
15341 status = -ENXIO;
15342 goto out;
15343 }
15344 drq->type = LPFC_DRQ;
2a622bfb 15345 drq->assoc_qid = cq->queue_id;
4f774513
JS
15346 drq->subtype = subtype;
15347 drq->host_index = 0;
15348 drq->hba_index = 0;
61f3d4bf 15349 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15350
15351 /* link the header and data RQs onto the parent cq child list */
15352 list_add_tail(&hrq->list, &cq->child_list);
15353 list_add_tail(&drq->list, &cq->child_list);
15354
15355out:
8fa38513 15356 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15357 return status;
15358}
15359
2d7dbc4c
JS
15360/**
15361 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15362 * @phba: HBA structure that indicates port to create a queue on.
15363 * @hrqp: The queue structure array to use to create the header receive queues.
15364 * @drqp: The queue structure array to use to create the data receive queues.
15365 * @cqp: The completion queue array to bind these receive queues to.
15366 *
15367 * This function creates a receive buffer queue pair , as detailed in @hrq and
15368 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15369 * to the HBA.
15370 *
15371 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15372 * struct is used to get the entry count that is necessary to determine the
15373 * number of pages to use for this queue. The @cq is used to indicate which
15374 * completion queue to bind received buffers that are posted to these queues to.
15375 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15376 * receive queue pair. This function is asynchronous and will wait for the
15377 * mailbox command to finish before continuing.
15378 *
15379 * On success this function will return a zero. If unable to allocate enough
15380 * memory this function will return -ENOMEM. If the queue create mailbox command
15381 * fails this function will return -ENXIO.
15382 **/
15383int
15384lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15385 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15386 uint32_t subtype)
15387{
15388 struct lpfc_queue *hrq, *drq, *cq;
15389 struct lpfc_mbx_rq_create_v2 *rq_create;
15390 struct lpfc_dmabuf *dmabuf;
15391 LPFC_MBOXQ_t *mbox;
15392 int rc, length, alloclen, status = 0;
15393 int cnt, idx, numrq, page_idx = 0;
15394 uint32_t shdr_status, shdr_add_status;
15395 union lpfc_sli4_cfg_shdr *shdr;
15396 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15397
15398 numrq = phba->cfg_nvmet_mrq;
15399 /* sanity check on array memory */
15400 if (!hrqp || !drqp || !cqp || !numrq)
15401 return -ENODEV;
15402 if (!phba->sli4_hba.pc_sli4_params.supported)
15403 hw_page_size = SLI4_PAGE_SIZE;
15404
15405 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15406 if (!mbox)
15407 return -ENOMEM;
15408
15409 length = sizeof(struct lpfc_mbx_rq_create_v2);
15410 length += ((2 * numrq * hrqp[0]->page_count) *
15411 sizeof(struct dma_address));
15412
15413 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15414 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15415 LPFC_SLI4_MBX_NEMBED);
15416 if (alloclen < length) {
15417 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15418 "3099 Allocated DMA memory size (%d) is "
15419 "less than the requested DMA memory size "
15420 "(%d)\n", alloclen, length);
15421 status = -ENOMEM;
15422 goto out;
15423 }
15424
15425
15426
15427 rq_create = mbox->sge_array->addr[0];
15428 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15429
15430 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15431 cnt = 0;
15432
15433 for (idx = 0; idx < numrq; idx++) {
15434 hrq = hrqp[idx];
15435 drq = drqp[idx];
15436 cq = cqp[idx];
15437
2d7dbc4c
JS
15438 /* sanity check on queue memory */
15439 if (!hrq || !drq || !cq) {
15440 status = -ENODEV;
15441 goto out;
15442 }
15443
7aabe84b
JS
15444 if (hrq->entry_count != drq->entry_count) {
15445 status = -EINVAL;
15446 goto out;
15447 }
15448
2d7dbc4c
JS
15449 if (idx == 0) {
15450 bf_set(lpfc_mbx_rq_create_num_pages,
15451 &rq_create->u.request,
15452 hrq->page_count);
15453 bf_set(lpfc_mbx_rq_create_rq_cnt,
15454 &rq_create->u.request, (numrq * 2));
15455 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15456 1);
15457 bf_set(lpfc_rq_context_base_cq,
15458 &rq_create->u.request.context,
15459 cq->queue_id);
15460 bf_set(lpfc_rq_context_data_size,
15461 &rq_create->u.request.context,
3c603be9 15462 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15463 bf_set(lpfc_rq_context_hdr_size,
15464 &rq_create->u.request.context,
15465 LPFC_HDR_BUF_SIZE);
15466 bf_set(lpfc_rq_context_rqe_count_1,
15467 &rq_create->u.request.context,
15468 hrq->entry_count);
15469 bf_set(lpfc_rq_context_rqe_size,
15470 &rq_create->u.request.context,
15471 LPFC_RQE_SIZE_8);
15472 bf_set(lpfc_rq_context_page_size,
15473 &rq_create->u.request.context,
15474 (PAGE_SIZE/SLI4_PAGE_SIZE));
15475 }
15476 rc = 0;
15477 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15478 memset(dmabuf->virt, 0, hw_page_size);
15479 cnt = page_idx + dmabuf->buffer_tag;
15480 rq_create->u.request.page[cnt].addr_lo =
15481 putPaddrLow(dmabuf->phys);
15482 rq_create->u.request.page[cnt].addr_hi =
15483 putPaddrHigh(dmabuf->phys);
15484 rc++;
15485 }
15486 page_idx += rc;
15487
15488 rc = 0;
15489 list_for_each_entry(dmabuf, &drq->page_list, list) {
15490 memset(dmabuf->virt, 0, hw_page_size);
15491 cnt = page_idx + dmabuf->buffer_tag;
15492 rq_create->u.request.page[cnt].addr_lo =
15493 putPaddrLow(dmabuf->phys);
15494 rq_create->u.request.page[cnt].addr_hi =
15495 putPaddrHigh(dmabuf->phys);
15496 rc++;
15497 }
15498 page_idx += rc;
15499
15500 hrq->db_format = LPFC_DB_RING_FORMAT;
15501 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15502 hrq->type = LPFC_HRQ;
15503 hrq->assoc_qid = cq->queue_id;
15504 hrq->subtype = subtype;
15505 hrq->host_index = 0;
15506 hrq->hba_index = 0;
61f3d4bf 15507 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15508
15509 drq->db_format = LPFC_DB_RING_FORMAT;
15510 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15511 drq->type = LPFC_DRQ;
15512 drq->assoc_qid = cq->queue_id;
15513 drq->subtype = subtype;
15514 drq->host_index = 0;
15515 drq->hba_index = 0;
61f3d4bf 15516 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15517
15518 list_add_tail(&hrq->list, &cq->child_list);
15519 list_add_tail(&drq->list, &cq->child_list);
15520 }
15521
15522 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15523 /* The IOCTL status is embedded in the mailbox subheader. */
15524 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15525 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15526 if (shdr_status || shdr_add_status || rc) {
15527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15528 "3120 RQ_CREATE mailbox failed with "
15529 "status x%x add_status x%x, mbx status x%x\n",
15530 shdr_status, shdr_add_status, rc);
15531 status = -ENXIO;
15532 goto out;
15533 }
15534 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15535 if (rc == 0xFFFF) {
15536 status = -ENXIO;
15537 goto out;
15538 }
15539
15540 /* Initialize all RQs with associated queue id */
15541 for (idx = 0; idx < numrq; idx++) {
15542 hrq = hrqp[idx];
15543 hrq->queue_id = rc + (2 * idx);
15544 drq = drqp[idx];
15545 drq->queue_id = rc + (2 * idx) + 1;
15546 }
15547
15548out:
15549 lpfc_sli4_mbox_cmd_free(phba, mbox);
15550 return status;
15551}
15552
4f774513
JS
15553/**
15554 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15555 * @eq: The queue structure associated with the queue to destroy.
15556 *
15557 * This function destroys a queue, as detailed in @eq by sending an mailbox
15558 * command, specific to the type of queue, to the HBA.
15559 *
15560 * The @eq struct is used to get the queue ID of the queue to destroy.
15561 *
15562 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15563 * command fails this function will return -ENXIO.
4f774513 15564 **/
a2fc4aef 15565int
4f774513
JS
15566lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15567{
15568 LPFC_MBOXQ_t *mbox;
15569 int rc, length, status = 0;
15570 uint32_t shdr_status, shdr_add_status;
15571 union lpfc_sli4_cfg_shdr *shdr;
15572
2e90f4b5 15573 /* sanity check on queue memory */
4f774513
JS
15574 if (!eq)
15575 return -ENODEV;
15576 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15577 if (!mbox)
15578 return -ENOMEM;
15579 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15580 sizeof(struct lpfc_sli4_cfg_mhdr));
15581 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15582 LPFC_MBOX_OPCODE_EQ_DESTROY,
15583 length, LPFC_SLI4_MBX_EMBED);
15584 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15585 eq->queue_id);
15586 mbox->vport = eq->phba->pport;
15587 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15588
15589 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15590 /* The IOCTL status is embedded in the mailbox subheader. */
15591 shdr = (union lpfc_sli4_cfg_shdr *)
15592 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15593 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15594 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15595 if (shdr_status || shdr_add_status || rc) {
15596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15597 "2505 EQ_DESTROY mailbox failed with "
15598 "status x%x add_status x%x, mbx status x%x\n",
15599 shdr_status, shdr_add_status, rc);
15600 status = -ENXIO;
15601 }
15602
15603 /* Remove eq from any list */
15604 list_del_init(&eq->list);
8fa38513 15605 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15606 return status;
15607}
15608
15609/**
15610 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15611 * @cq: The queue structure associated with the queue to destroy.
15612 *
15613 * This function destroys a queue, as detailed in @cq by sending an mailbox
15614 * command, specific to the type of queue, to the HBA.
15615 *
15616 * The @cq struct is used to get the queue ID of the queue to destroy.
15617 *
15618 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15619 * command fails this function will return -ENXIO.
4f774513 15620 **/
a2fc4aef 15621int
4f774513
JS
15622lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15623{
15624 LPFC_MBOXQ_t *mbox;
15625 int rc, length, status = 0;
15626 uint32_t shdr_status, shdr_add_status;
15627 union lpfc_sli4_cfg_shdr *shdr;
15628
2e90f4b5 15629 /* sanity check on queue memory */
4f774513
JS
15630 if (!cq)
15631 return -ENODEV;
15632 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15633 if (!mbox)
15634 return -ENOMEM;
15635 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15636 sizeof(struct lpfc_sli4_cfg_mhdr));
15637 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15638 LPFC_MBOX_OPCODE_CQ_DESTROY,
15639 length, LPFC_SLI4_MBX_EMBED);
15640 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15641 cq->queue_id);
15642 mbox->vport = cq->phba->pport;
15643 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15644 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15645 /* The IOCTL status is embedded in the mailbox subheader. */
15646 shdr = (union lpfc_sli4_cfg_shdr *)
15647 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15648 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15649 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15650 if (shdr_status || shdr_add_status || rc) {
15651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15652 "2506 CQ_DESTROY mailbox failed with "
15653 "status x%x add_status x%x, mbx status x%x\n",
15654 shdr_status, shdr_add_status, rc);
15655 status = -ENXIO;
15656 }
15657 /* Remove cq from any list */
15658 list_del_init(&cq->list);
8fa38513 15659 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15660 return status;
15661}
15662
04c68496
JS
15663/**
15664 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15665 * @qm: The queue structure associated with the queue to destroy.
15666 *
15667 * This function destroys a queue, as detailed in @mq by sending an mailbox
15668 * command, specific to the type of queue, to the HBA.
15669 *
15670 * The @mq struct is used to get the queue ID of the queue to destroy.
15671 *
15672 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15673 * command fails this function will return -ENXIO.
04c68496 15674 **/
a2fc4aef 15675int
04c68496
JS
15676lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15677{
15678 LPFC_MBOXQ_t *mbox;
15679 int rc, length, status = 0;
15680 uint32_t shdr_status, shdr_add_status;
15681 union lpfc_sli4_cfg_shdr *shdr;
15682
2e90f4b5 15683 /* sanity check on queue memory */
04c68496
JS
15684 if (!mq)
15685 return -ENODEV;
15686 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15687 if (!mbox)
15688 return -ENOMEM;
15689 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15690 sizeof(struct lpfc_sli4_cfg_mhdr));
15691 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15692 LPFC_MBOX_OPCODE_MQ_DESTROY,
15693 length, LPFC_SLI4_MBX_EMBED);
15694 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15695 mq->queue_id);
15696 mbox->vport = mq->phba->pport;
15697 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15698 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15699 /* The IOCTL status is embedded in the mailbox subheader. */
15700 shdr = (union lpfc_sli4_cfg_shdr *)
15701 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15702 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15703 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15704 if (shdr_status || shdr_add_status || rc) {
15705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15706 "2507 MQ_DESTROY mailbox failed with "
15707 "status x%x add_status x%x, mbx status x%x\n",
15708 shdr_status, shdr_add_status, rc);
15709 status = -ENXIO;
15710 }
15711 /* Remove mq from any list */
15712 list_del_init(&mq->list);
8fa38513 15713 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15714 return status;
15715}
15716
4f774513
JS
15717/**
15718 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15719 * @wq: The queue structure associated with the queue to destroy.
15720 *
15721 * This function destroys a queue, as detailed in @wq by sending an mailbox
15722 * command, specific to the type of queue, to the HBA.
15723 *
15724 * The @wq struct is used to get the queue ID of the queue to destroy.
15725 *
15726 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15727 * command fails this function will return -ENXIO.
4f774513 15728 **/
a2fc4aef 15729int
4f774513
JS
15730lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15731{
15732 LPFC_MBOXQ_t *mbox;
15733 int rc, length, status = 0;
15734 uint32_t shdr_status, shdr_add_status;
15735 union lpfc_sli4_cfg_shdr *shdr;
15736
2e90f4b5 15737 /* sanity check on queue memory */
4f774513
JS
15738 if (!wq)
15739 return -ENODEV;
15740 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15741 if (!mbox)
15742 return -ENOMEM;
15743 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15744 sizeof(struct lpfc_sli4_cfg_mhdr));
15745 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15746 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15747 length, LPFC_SLI4_MBX_EMBED);
15748 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15749 wq->queue_id);
15750 mbox->vport = wq->phba->pport;
15751 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15752 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15753 shdr = (union lpfc_sli4_cfg_shdr *)
15754 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15755 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15756 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15757 if (shdr_status || shdr_add_status || rc) {
15758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15759 "2508 WQ_DESTROY mailbox failed with "
15760 "status x%x add_status x%x, mbx status x%x\n",
15761 shdr_status, shdr_add_status, rc);
15762 status = -ENXIO;
15763 }
15764 /* Remove wq from any list */
15765 list_del_init(&wq->list);
d1f525aa
JS
15766 kfree(wq->pring);
15767 wq->pring = NULL;
8fa38513 15768 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
15769 return status;
15770}
15771
15772/**
15773 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15774 * @rq: The queue structure associated with the queue to destroy.
15775 *
15776 * This function destroys a queue, as detailed in @rq by sending an mailbox
15777 * command, specific to the type of queue, to the HBA.
15778 *
15779 * The @rq struct is used to get the queue ID of the queue to destroy.
15780 *
15781 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15782 * command fails this function will return -ENXIO.
4f774513 15783 **/
a2fc4aef 15784int
4f774513
JS
15785lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15786 struct lpfc_queue *drq)
15787{
15788 LPFC_MBOXQ_t *mbox;
15789 int rc, length, status = 0;
15790 uint32_t shdr_status, shdr_add_status;
15791 union lpfc_sli4_cfg_shdr *shdr;
15792
2e90f4b5 15793 /* sanity check on queue memory */
4f774513
JS
15794 if (!hrq || !drq)
15795 return -ENODEV;
15796 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15797 if (!mbox)
15798 return -ENOMEM;
15799 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 15800 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
15801 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15802 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15803 length, LPFC_SLI4_MBX_EMBED);
15804 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15805 hrq->queue_id);
15806 mbox->vport = hrq->phba->pport;
15807 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15808 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15809 /* The IOCTL status is embedded in the mailbox subheader. */
15810 shdr = (union lpfc_sli4_cfg_shdr *)
15811 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15812 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15813 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15814 if (shdr_status || shdr_add_status || rc) {
15815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15816 "2509 RQ_DESTROY mailbox failed with "
15817 "status x%x add_status x%x, mbx status x%x\n",
15818 shdr_status, shdr_add_status, rc);
15819 if (rc != MBX_TIMEOUT)
15820 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15821 return -ENXIO;
15822 }
15823 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15824 drq->queue_id);
15825 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15826 shdr = (union lpfc_sli4_cfg_shdr *)
15827 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15828 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15829 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15830 if (shdr_status || shdr_add_status || rc) {
15831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15832 "2510 RQ_DESTROY mailbox failed with "
15833 "status x%x add_status x%x, mbx status x%x\n",
15834 shdr_status, shdr_add_status, rc);
15835 status = -ENXIO;
15836 }
15837 list_del_init(&hrq->list);
15838 list_del_init(&drq->list);
8fa38513 15839 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
15840 return status;
15841}
15842
15843/**
15844 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15845 * @phba: The virtual port for which this call being executed.
15846 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15847 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15848 * @xritag: the xritag that ties this io to the SGL pages.
15849 *
15850 * This routine will post the sgl pages for the IO that has the xritag
15851 * that is in the iocbq structure. The xritag is assigned during iocbq
15852 * creation and persists for as long as the driver is loaded.
15853 * if the caller has fewer than 256 scatter gather segments to map then
15854 * pdma_phys_addr1 should be 0.
15855 * If the caller needs to map more than 256 scatter gather segment then
15856 * pdma_phys_addr1 should be a valid physical address.
15857 * physical address for SGLs must be 64 byte aligned.
15858 * If you are going to map 2 SGL's then the first one must have 256 entries
15859 * the second sgl can have between 1 and 256 entries.
15860 *
15861 * Return codes:
15862 * 0 - Success
15863 * -ENXIO, -ENOMEM - Failure
15864 **/
15865int
15866lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15867 dma_addr_t pdma_phys_addr0,
15868 dma_addr_t pdma_phys_addr1,
15869 uint16_t xritag)
15870{
15871 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15872 LPFC_MBOXQ_t *mbox;
15873 int rc;
15874 uint32_t shdr_status, shdr_add_status;
6d368e53 15875 uint32_t mbox_tmo;
4f774513
JS
15876 union lpfc_sli4_cfg_shdr *shdr;
15877
15878 if (xritag == NO_XRI) {
15879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15880 "0364 Invalid param:\n");
15881 return -EINVAL;
15882 }
15883
15884 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15885 if (!mbox)
15886 return -ENOMEM;
15887
15888 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15889 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15890 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 15891 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
15892
15893 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15894 &mbox->u.mqe.un.post_sgl_pages;
15895 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15896 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15897
15898 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15899 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15900 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15901 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15902
15903 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15904 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15905 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15906 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15907 if (!phba->sli4_hba.intr_enable)
15908 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 15909 else {
a183a15f 15910 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
15911 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15912 }
4f774513
JS
15913 /* The IOCTL status is embedded in the mailbox subheader. */
15914 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15915 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15916 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15917 if (rc != MBX_TIMEOUT)
15918 mempool_free(mbox, phba->mbox_mem_pool);
15919 if (shdr_status || shdr_add_status || rc) {
15920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15921 "2511 POST_SGL mailbox failed with "
15922 "status x%x add_status x%x, mbx status x%x\n",
15923 shdr_status, shdr_add_status, rc);
4f774513
JS
15924 }
15925 return 0;
15926}
4f774513 15927
6d368e53 15928/**
88a2cfbb 15929 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
15930 * @phba: pointer to lpfc hba data structure.
15931 *
15932 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
15933 * HBA consistent with the SLI-4 interface spec. This routine
15934 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15935 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 15936 *
88a2cfbb
JS
15937 * Returns
15938 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15939 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15940 **/
5d8b8167 15941static uint16_t
6d368e53
JS
15942lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15943{
15944 unsigned long xri;
15945
15946 /*
15947 * Fetch the next logical xri. Because this index is logical,
15948 * the driver starts at 0 each time.
15949 */
15950 spin_lock_irq(&phba->hbalock);
15951 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15952 phba->sli4_hba.max_cfg_param.max_xri, 0);
15953 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15954 spin_unlock_irq(&phba->hbalock);
15955 return NO_XRI;
15956 } else {
15957 set_bit(xri, phba->sli4_hba.xri_bmask);
15958 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 15959 }
6d368e53
JS
15960 spin_unlock_irq(&phba->hbalock);
15961 return xri;
15962}
15963
15964/**
15965 * lpfc_sli4_free_xri - Release an xri for reuse.
15966 * @phba: pointer to lpfc hba data structure.
15967 *
15968 * This routine is invoked to release an xri to the pool of
15969 * available rpis maintained by the driver.
15970 **/
5d8b8167 15971static void
6d368e53
JS
15972__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15973{
15974 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
15975 phba->sli4_hba.max_cfg_param.xri_used--;
15976 }
15977}
15978
15979/**
15980 * lpfc_sli4_free_xri - Release an xri for reuse.
15981 * @phba: pointer to lpfc hba data structure.
15982 *
15983 * This routine is invoked to release an xri to the pool of
15984 * available rpis maintained by the driver.
15985 **/
15986void
15987lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15988{
15989 spin_lock_irq(&phba->hbalock);
15990 __lpfc_sli4_free_xri(phba, xri);
15991 spin_unlock_irq(&phba->hbalock);
15992}
15993
4f774513
JS
15994/**
15995 * lpfc_sli4_next_xritag - Get an xritag for the io
15996 * @phba: Pointer to HBA context object.
15997 *
15998 * This function gets an xritag for the iocb. If there is no unused xritag
15999 * it will return 0xffff.
16000 * The function returns the allocated xritag if successful, else returns zero.
16001 * Zero is not a valid xritag.
16002 * The caller is not required to hold any lock.
16003 **/
16004uint16_t
16005lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16006{
6d368e53 16007 uint16_t xri_index;
4f774513 16008
6d368e53 16009 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16010 if (xri_index == NO_XRI)
16011 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16012 "2004 Failed to allocate XRI.last XRITAG is %d"
16013 " Max XRI is %d, Used XRI is %d\n",
16014 xri_index,
16015 phba->sli4_hba.max_cfg_param.max_xri,
16016 phba->sli4_hba.max_cfg_param.xri_used);
16017 return xri_index;
4f774513
JS
16018}
16019
16020/**
895427bd 16021 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16022 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16023 * @post_sgl_list: pointer to els sgl entry list.
16024 * @count: number of els sgl entries on the list.
4f774513
JS
16025 *
16026 * This routine is invoked to post a block of driver's sgl pages to the
16027 * HBA using non-embedded mailbox command. No Lock is held. This routine
16028 * is only called when the driver is loading and after all IO has been
16029 * stopped.
16030 **/
8a9d2e80 16031static int
895427bd 16032lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16033 struct list_head *post_sgl_list,
16034 int post_cnt)
4f774513 16035{
8a9d2e80 16036 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16037 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16038 struct sgl_page_pairs *sgl_pg_pairs;
16039 void *viraddr;
16040 LPFC_MBOXQ_t *mbox;
16041 uint32_t reqlen, alloclen, pg_pairs;
16042 uint32_t mbox_tmo;
8a9d2e80
JS
16043 uint16_t xritag_start = 0;
16044 int rc = 0;
4f774513
JS
16045 uint32_t shdr_status, shdr_add_status;
16046 union lpfc_sli4_cfg_shdr *shdr;
16047
895427bd 16048 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16049 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16050 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16052 "2559 Block sgl registration required DMA "
16053 "size (%d) great than a page\n", reqlen);
16054 return -ENOMEM;
16055 }
895427bd 16056
4f774513 16057 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16058 if (!mbox)
4f774513 16059 return -ENOMEM;
4f774513
JS
16060
16061 /* Allocate DMA memory and set up the non-embedded mailbox command */
16062 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16063 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16064 LPFC_SLI4_MBX_NEMBED);
16065
16066 if (alloclen < reqlen) {
16067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16068 "0285 Allocated DMA memory size (%d) is "
16069 "less than the requested DMA memory "
16070 "size (%d)\n", alloclen, reqlen);
16071 lpfc_sli4_mbox_cmd_free(phba, mbox);
16072 return -ENOMEM;
16073 }
4f774513 16074 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16075 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16076 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16077 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16078
8a9d2e80
JS
16079 pg_pairs = 0;
16080 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16081 /* Set up the sge entry */
16082 sgl_pg_pairs->sgl_pg0_addr_lo =
16083 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16084 sgl_pg_pairs->sgl_pg0_addr_hi =
16085 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16086 sgl_pg_pairs->sgl_pg1_addr_lo =
16087 cpu_to_le32(putPaddrLow(0));
16088 sgl_pg_pairs->sgl_pg1_addr_hi =
16089 cpu_to_le32(putPaddrHigh(0));
6d368e53 16090
4f774513
JS
16091 /* Keep the first xritag on the list */
16092 if (pg_pairs == 0)
16093 xritag_start = sglq_entry->sli4_xritag;
16094 sgl_pg_pairs++;
8a9d2e80 16095 pg_pairs++;
4f774513 16096 }
6d368e53
JS
16097
16098 /* Complete initialization and perform endian conversion. */
4f774513 16099 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16100 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16101 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16102
4f774513
JS
16103 if (!phba->sli4_hba.intr_enable)
16104 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16105 else {
a183a15f 16106 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16107 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16108 }
16109 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16112 if (rc != MBX_TIMEOUT)
16113 lpfc_sli4_mbox_cmd_free(phba, mbox);
16114 if (shdr_status || shdr_add_status || rc) {
16115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16116 "2513 POST_SGL_BLOCK mailbox command failed "
16117 "status x%x add_status x%x mbx status x%x\n",
16118 shdr_status, shdr_add_status, rc);
16119 rc = -ENXIO;
16120 }
16121 return rc;
16122}
16123
16124/**
16125 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16126 * @phba: pointer to lpfc hba data structure.
16127 * @sblist: pointer to scsi buffer list.
16128 * @count: number of scsi buffers on the list.
16129 *
16130 * This routine is invoked to post a block of @count scsi sgl pages from a
16131 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16132 * No Lock is held.
16133 *
16134 **/
16135int
8a9d2e80
JS
16136lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16137 struct list_head *sblist,
16138 int count)
4f774513
JS
16139{
16140 struct lpfc_scsi_buf *psb;
16141 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16142 struct sgl_page_pairs *sgl_pg_pairs;
16143 void *viraddr;
16144 LPFC_MBOXQ_t *mbox;
16145 uint32_t reqlen, alloclen, pg_pairs;
16146 uint32_t mbox_tmo;
16147 uint16_t xritag_start = 0;
16148 int rc = 0;
16149 uint32_t shdr_status, shdr_add_status;
16150 dma_addr_t pdma_phys_bpl1;
16151 union lpfc_sli4_cfg_shdr *shdr;
16152
16153 /* Calculate the requested length of the dma memory */
8a9d2e80 16154 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16155 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16156 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16157 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16158 "0217 Block sgl registration required DMA "
16159 "size (%d) great than a page\n", reqlen);
16160 return -ENOMEM;
16161 }
16162 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16163 if (!mbox) {
16164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16165 "0283 Failed to allocate mbox cmd memory\n");
16166 return -ENOMEM;
16167 }
16168
16169 /* Allocate DMA memory and set up the non-embedded mailbox command */
16170 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16171 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16172 LPFC_SLI4_MBX_NEMBED);
16173
16174 if (alloclen < reqlen) {
16175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16176 "2561 Allocated DMA memory size (%d) is "
16177 "less than the requested DMA memory "
16178 "size (%d)\n", alloclen, reqlen);
16179 lpfc_sli4_mbox_cmd_free(phba, mbox);
16180 return -ENOMEM;
16181 }
6d368e53 16182
4f774513 16183 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16184 viraddr = mbox->sge_array->addr[0];
16185
16186 /* Set up the SGL pages in the non-embedded DMA pages */
16187 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16188 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16189
16190 pg_pairs = 0;
16191 list_for_each_entry(psb, sblist, list) {
16192 /* Set up the sge entry */
16193 sgl_pg_pairs->sgl_pg0_addr_lo =
16194 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16195 sgl_pg_pairs->sgl_pg0_addr_hi =
16196 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16197 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16198 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16199 else
16200 pdma_phys_bpl1 = 0;
16201 sgl_pg_pairs->sgl_pg1_addr_lo =
16202 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16203 sgl_pg_pairs->sgl_pg1_addr_hi =
16204 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16205 /* Keep the first xritag on the list */
16206 if (pg_pairs == 0)
16207 xritag_start = psb->cur_iocbq.sli4_xritag;
16208 sgl_pg_pairs++;
16209 pg_pairs++;
16210 }
16211 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16212 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16213 /* Perform endian conversion if necessary */
16214 sgl->word0 = cpu_to_le32(sgl->word0);
16215
16216 if (!phba->sli4_hba.intr_enable)
16217 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16218 else {
a183a15f 16219 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16220 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16221 }
16222 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16223 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16224 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16225 if (rc != MBX_TIMEOUT)
16226 lpfc_sli4_mbox_cmd_free(phba, mbox);
16227 if (shdr_status || shdr_add_status || rc) {
16228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16229 "2564 POST_SGL_BLOCK mailbox command failed "
16230 "status x%x add_status x%x mbx status x%x\n",
16231 shdr_status, shdr_add_status, rc);
16232 rc = -ENXIO;
16233 }
16234 return rc;
16235}
16236
16237/**
16238 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16239 * @phba: pointer to lpfc_hba struct that the frame was received on
16240 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16241 *
16242 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16243 * valid type of frame that the LPFC driver will handle. This function will
16244 * return a zero if the frame is a valid frame or a non zero value when the
16245 * frame does not pass the check.
16246 **/
16247static int
16248lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16249{
474ffb74 16250 /* make rctl_names static to save stack space */
4f774513 16251 struct fc_vft_header *fc_vft_hdr;
546fc854 16252 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16253
ae9e28f3
JS
16254#define FC_RCTL_MDS_DIAGS 0xF4
16255
4f774513
JS
16256 switch (fc_hdr->fh_r_ctl) {
16257 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16258 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16259 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16260 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16261 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16262 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16263 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16264 case FC_RCTL_DD_CMD_STATUS: /* command status */
16265 case FC_RCTL_ELS_REQ: /* extended link services request */
16266 case FC_RCTL_ELS_REP: /* extended link services reply */
16267 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16268 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16269 case FC_RCTL_BA_NOP: /* basic link service NOP */
16270 case FC_RCTL_BA_ABTS: /* basic link service abort */
16271 case FC_RCTL_BA_RMC: /* remove connection */
16272 case FC_RCTL_BA_ACC: /* basic accept */
16273 case FC_RCTL_BA_RJT: /* basic reject */
16274 case FC_RCTL_BA_PRMT:
16275 case FC_RCTL_ACK_1: /* acknowledge_1 */
16276 case FC_RCTL_ACK_0: /* acknowledge_0 */
16277 case FC_RCTL_P_RJT: /* port reject */
16278 case FC_RCTL_F_RJT: /* fabric reject */
16279 case FC_RCTL_P_BSY: /* port busy */
16280 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16281 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16282 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16283 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16284 case FC_RCTL_END: /* end */
16285 break;
16286 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16287 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16288 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16289 return lpfc_fc_frame_check(phba, fc_hdr);
16290 default:
16291 goto drop;
16292 }
ae9e28f3
JS
16293
16294#define FC_TYPE_VENDOR_UNIQUE 0xFF
16295
4f774513
JS
16296 switch (fc_hdr->fh_type) {
16297 case FC_TYPE_BLS:
16298 case FC_TYPE_ELS:
16299 case FC_TYPE_FCP:
16300 case FC_TYPE_CT:
895427bd 16301 case FC_TYPE_NVME:
ae9e28f3 16302 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16303 break;
16304 case FC_TYPE_IP:
16305 case FC_TYPE_ILS:
16306 default:
16307 goto drop;
16308 }
546fc854 16309
4f774513 16310 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16311 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16312 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16313 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16314 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16315 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16316 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16317 be32_to_cpu(header[6]));
4f774513
JS
16318 return 0;
16319drop:
16320 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16321 "2539 Dropped frame rctl:x%x type:x%x\n",
16322 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16323 return 1;
16324}
16325
16326/**
16327 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16328 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16329 *
16330 * This function processes the FC header to retrieve the VFI from the VF
16331 * header, if one exists. This function will return the VFI if one exists
16332 * or 0 if no VSAN Header exists.
16333 **/
16334static uint32_t
16335lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16336{
16337 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16338
16339 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16340 return 0;
16341 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16342}
16343
16344/**
16345 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16346 * @phba: Pointer to the HBA structure to search for the vport on
16347 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16348 * @fcfi: The FC Fabric ID that the frame came from
16349 *
16350 * This function searches the @phba for a vport that matches the content of the
16351 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16352 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16353 * returns the matching vport pointer or NULL if unable to match frame to a
16354 * vport.
16355 **/
16356static struct lpfc_vport *
16357lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16358 uint16_t fcfi, uint32_t did)
4f774513
JS
16359{
16360 struct lpfc_vport **vports;
16361 struct lpfc_vport *vport = NULL;
16362 int i;
939723a4 16363
bf08611b
JS
16364 if (did == Fabric_DID)
16365 return phba->pport;
939723a4
JS
16366 if ((phba->pport->fc_flag & FC_PT2PT) &&
16367 !(phba->link_state == LPFC_HBA_READY))
16368 return phba->pport;
16369
4f774513 16370 vports = lpfc_create_vport_work_array(phba);
895427bd 16371 if (vports != NULL) {
4f774513
JS
16372 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16373 if (phba->fcf.fcfi == fcfi &&
16374 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16375 vports[i]->fc_myDID == did) {
16376 vport = vports[i];
16377 break;
16378 }
16379 }
895427bd 16380 }
4f774513
JS
16381 lpfc_destroy_vport_work_array(phba, vports);
16382 return vport;
16383}
16384
45ed1190
JS
16385/**
16386 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16387 * @vport: The vport to work on.
16388 *
16389 * This function updates the receive sequence time stamp for this vport. The
16390 * receive sequence time stamp indicates the time that the last frame of the
16391 * the sequence that has been idle for the longest amount of time was received.
16392 * the driver uses this time stamp to indicate if any received sequences have
16393 * timed out.
16394 **/
5d8b8167 16395static void
45ed1190
JS
16396lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16397{
16398 struct lpfc_dmabuf *h_buf;
16399 struct hbq_dmabuf *dmabuf = NULL;
16400
16401 /* get the oldest sequence on the rcv list */
16402 h_buf = list_get_first(&vport->rcv_buffer_list,
16403 struct lpfc_dmabuf, list);
16404 if (!h_buf)
16405 return;
16406 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16407 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16408}
16409
16410/**
16411 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16412 * @vport: The vport that the received sequences were sent to.
16413 *
16414 * This function cleans up all outstanding received sequences. This is called
16415 * by the driver when a link event or user action invalidates all the received
16416 * sequences.
16417 **/
16418void
16419lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16420{
16421 struct lpfc_dmabuf *h_buf, *hnext;
16422 struct lpfc_dmabuf *d_buf, *dnext;
16423 struct hbq_dmabuf *dmabuf = NULL;
16424
16425 /* start with the oldest sequence on the rcv list */
16426 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16427 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16428 list_del_init(&dmabuf->hbuf.list);
16429 list_for_each_entry_safe(d_buf, dnext,
16430 &dmabuf->dbuf.list, list) {
16431 list_del_init(&d_buf->list);
16432 lpfc_in_buf_free(vport->phba, d_buf);
16433 }
16434 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16435 }
16436}
16437
16438/**
16439 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16440 * @vport: The vport that the received sequences were sent to.
16441 *
16442 * This function determines whether any received sequences have timed out by
16443 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16444 * indicates that there is at least one timed out sequence this routine will
16445 * go through the received sequences one at a time from most inactive to most
16446 * active to determine which ones need to be cleaned up. Once it has determined
16447 * that a sequence needs to be cleaned up it will simply free up the resources
16448 * without sending an abort.
16449 **/
16450void
16451lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16452{
16453 struct lpfc_dmabuf *h_buf, *hnext;
16454 struct lpfc_dmabuf *d_buf, *dnext;
16455 struct hbq_dmabuf *dmabuf = NULL;
16456 unsigned long timeout;
16457 int abort_count = 0;
16458
16459 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16460 vport->rcv_buffer_time_stamp);
16461 if (list_empty(&vport->rcv_buffer_list) ||
16462 time_before(jiffies, timeout))
16463 return;
16464 /* start with the oldest sequence on the rcv list */
16465 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16466 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16467 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16468 dmabuf->time_stamp);
16469 if (time_before(jiffies, timeout))
16470 break;
16471 abort_count++;
16472 list_del_init(&dmabuf->hbuf.list);
16473 list_for_each_entry_safe(d_buf, dnext,
16474 &dmabuf->dbuf.list, list) {
16475 list_del_init(&d_buf->list);
16476 lpfc_in_buf_free(vport->phba, d_buf);
16477 }
16478 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16479 }
16480 if (abort_count)
16481 lpfc_update_rcv_time_stamp(vport);
16482}
16483
4f774513
JS
16484/**
16485 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16486 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16487 *
16488 * This function searches through the existing incomplete sequences that have
16489 * been sent to this @vport. If the frame matches one of the incomplete
16490 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16491 * make up that sequence. If no sequence is found that matches this frame then
16492 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16493 * This function returns a pointer to the first dmabuf in the sequence list that
16494 * the frame was linked to.
16495 **/
16496static struct hbq_dmabuf *
16497lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16498{
16499 struct fc_frame_header *new_hdr;
16500 struct fc_frame_header *temp_hdr;
16501 struct lpfc_dmabuf *d_buf;
16502 struct lpfc_dmabuf *h_buf;
16503 struct hbq_dmabuf *seq_dmabuf = NULL;
16504 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16505 uint8_t found = 0;
4f774513 16506
4d9ab994 16507 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16508 dmabuf->time_stamp = jiffies;
4f774513 16509 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16510
4f774513
JS
16511 /* Use the hdr_buf to find the sequence that this frame belongs to */
16512 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16513 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16514 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16515 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16516 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16517 continue;
16518 /* found a pending sequence that matches this frame */
16519 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16520 break;
16521 }
16522 if (!seq_dmabuf) {
16523 /*
16524 * This indicates first frame received for this sequence.
16525 * Queue the buffer on the vport's rcv_buffer_list.
16526 */
16527 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16528 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16529 return dmabuf;
16530 }
16531 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16532 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16533 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16534 list_del_init(&seq_dmabuf->hbuf.list);
16535 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16536 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16537 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16538 return dmabuf;
16539 }
45ed1190
JS
16540 /* move this sequence to the tail to indicate a young sequence */
16541 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16542 seq_dmabuf->time_stamp = jiffies;
16543 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16544 if (list_empty(&seq_dmabuf->dbuf.list)) {
16545 temp_hdr = dmabuf->hbuf.virt;
16546 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16547 return seq_dmabuf;
16548 }
4f774513 16549 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16550 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16551 while (!found) {
4f774513
JS
16552 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16553 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16554 /*
16555 * If the frame's sequence count is greater than the frame on
16556 * the list then insert the frame right after this frame
16557 */
eeead811
JS
16558 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16559 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16560 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16561 found = 1;
16562 break;
4f774513 16563 }
4360ca9c
JS
16564
16565 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16566 break;
16567 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16568 }
4360ca9c
JS
16569
16570 if (found)
16571 return seq_dmabuf;
4f774513
JS
16572 return NULL;
16573}
16574
6669f9bb
JS
16575/**
16576 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16577 * @vport: pointer to a vitural port
16578 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16579 *
16580 * This function tries to abort from the partially assembed sequence, described
16581 * by the information from basic abbort @dmabuf. It checks to see whether such
16582 * partially assembled sequence held by the driver. If so, it shall free up all
16583 * the frames from the partially assembled sequence.
16584 *
16585 * Return
16586 * true -- if there is matching partially assembled sequence present and all
16587 * the frames freed with the sequence;
16588 * false -- if there is no matching partially assembled sequence present so
16589 * nothing got aborted in the lower layer driver
16590 **/
16591static bool
16592lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16593 struct hbq_dmabuf *dmabuf)
16594{
16595 struct fc_frame_header *new_hdr;
16596 struct fc_frame_header *temp_hdr;
16597 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16598 struct hbq_dmabuf *seq_dmabuf = NULL;
16599
16600 /* Use the hdr_buf to find the sequence that matches this frame */
16601 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16602 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16603 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16604 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16605 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16606 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16607 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16608 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16609 continue;
16610 /* found a pending sequence that matches this frame */
16611 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16612 break;
16613 }
16614
16615 /* Free up all the frames from the partially assembled sequence */
16616 if (seq_dmabuf) {
16617 list_for_each_entry_safe(d_buf, n_buf,
16618 &seq_dmabuf->dbuf.list, list) {
16619 list_del_init(&d_buf->list);
16620 lpfc_in_buf_free(vport->phba, d_buf);
16621 }
16622 return true;
16623 }
16624 return false;
16625}
16626
6dd9e31c
JS
16627/**
16628 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16629 * @vport: pointer to a vitural port
16630 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16631 *
16632 * This function tries to abort from the assembed sequence from upper level
16633 * protocol, described by the information from basic abbort @dmabuf. It
16634 * checks to see whether such pending context exists at upper level protocol.
16635 * If so, it shall clean up the pending context.
16636 *
16637 * Return
16638 * true -- if there is matching pending context of the sequence cleaned
16639 * at ulp;
16640 * false -- if there is no matching pending context of the sequence present
16641 * at ulp.
16642 **/
16643static bool
16644lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16645{
16646 struct lpfc_hba *phba = vport->phba;
16647 int handled;
16648
16649 /* Accepting abort at ulp with SLI4 only */
16650 if (phba->sli_rev < LPFC_SLI_REV4)
16651 return false;
16652
16653 /* Register all caring upper level protocols to attend abort */
16654 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16655 if (handled)
16656 return true;
16657
16658 return false;
16659}
16660
6669f9bb 16661/**
546fc854 16662 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16663 * @phba: Pointer to HBA context object.
16664 * @cmd_iocbq: pointer to the command iocbq structure.
16665 * @rsp_iocbq: pointer to the response iocbq structure.
16666 *
546fc854 16667 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16668 * event. It properly releases the memory allocated to the sequence abort
16669 * accept iocb.
16670 **/
16671static void
546fc854 16672lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16673 struct lpfc_iocbq *cmd_iocbq,
16674 struct lpfc_iocbq *rsp_iocbq)
16675{
6dd9e31c
JS
16676 struct lpfc_nodelist *ndlp;
16677
16678 if (cmd_iocbq) {
16679 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16680 lpfc_nlp_put(ndlp);
16681 lpfc_nlp_not_used(ndlp);
6669f9bb 16682 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16683 }
6b5151fd
JS
16684
16685 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16686 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16688 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16689 rsp_iocbq->iocb.ulpStatus,
16690 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16691}
16692
6d368e53
JS
16693/**
16694 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16695 * @phba: Pointer to HBA context object.
16696 * @xri: xri id in transaction.
16697 *
16698 * This function validates the xri maps to the known range of XRIs allocated an
16699 * used by the driver.
16700 **/
7851fe2c 16701uint16_t
6d368e53
JS
16702lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16703 uint16_t xri)
16704{
a2fc4aef 16705 uint16_t i;
6d368e53
JS
16706
16707 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16708 if (xri == phba->sli4_hba.xri_ids[i])
16709 return i;
16710 }
16711 return NO_XRI;
16712}
16713
6669f9bb 16714/**
546fc854 16715 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16716 * @phba: Pointer to HBA context object.
16717 * @fc_hdr: pointer to a FC frame header.
16718 *
546fc854 16719 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16720 * event after aborting the sequence handling.
16721 **/
86c67379 16722void
6dd9e31c
JS
16723lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16724 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16725{
6dd9e31c 16726 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
16727 struct lpfc_iocbq *ctiocb = NULL;
16728 struct lpfc_nodelist *ndlp;
ee0f4fe1 16729 uint16_t oxid, rxid, xri, lxri;
5ffc266e 16730 uint32_t sid, fctl;
6669f9bb 16731 IOCB_t *icmd;
546fc854 16732 int rc;
6669f9bb
JS
16733
16734 if (!lpfc_is_link_up(phba))
16735 return;
16736
16737 sid = sli4_sid_from_fc_hdr(fc_hdr);
16738 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 16739 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 16740
6dd9e31c 16741 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 16742 if (!ndlp) {
9d3d340d 16743 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
16744 if (!ndlp) {
16745 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16746 "1268 Failed to allocate ndlp for "
16747 "oxid:x%x SID:x%x\n", oxid, sid);
16748 return;
16749 }
6dd9e31c
JS
16750 /* Put ndlp onto pport node list */
16751 lpfc_enqueue_node(vport, ndlp);
16752 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16753 /* re-setup ndlp without removing from node list */
16754 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16755 if (!ndlp) {
16756 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16757 "3275 Failed to active ndlp found "
16758 "for oxid:x%x SID:x%x\n", oxid, sid);
16759 return;
16760 }
6669f9bb
JS
16761 }
16762
546fc854 16763 /* Allocate buffer for rsp iocb */
6669f9bb
JS
16764 ctiocb = lpfc_sli_get_iocbq(phba);
16765 if (!ctiocb)
16766 return;
16767
5ffc266e
JS
16768 /* Extract the F_CTL field from FC_HDR */
16769 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16770
6669f9bb 16771 icmd = &ctiocb->iocb;
6669f9bb 16772 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 16773 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
16774 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16775 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16776 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16777
16778 /* Fill in the rest of iocb fields */
16779 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16780 icmd->ulpBdeCount = 0;
16781 icmd->ulpLe = 1;
16782 icmd->ulpClass = CLASS3;
6d368e53 16783 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 16784 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 16785
6669f9bb
JS
16786 ctiocb->iocb_cmpl = NULL;
16787 ctiocb->vport = phba->pport;
546fc854 16788 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 16789 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
16790 ctiocb->sli4_xritag = NO_XRI;
16791
ee0f4fe1
JS
16792 if (fctl & FC_FC_EX_CTX)
16793 /* Exchange responder sent the abort so we
16794 * own the oxid.
16795 */
16796 xri = oxid;
16797 else
16798 xri = rxid;
16799 lxri = lpfc_sli4_xri_inrange(phba, xri);
16800 if (lxri != NO_XRI)
16801 lpfc_set_rrq_active(phba, ndlp, lxri,
16802 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
16803 /* For BA_ABTS from exchange responder, if the logical xri with
16804 * the oxid maps to the FCP XRI range, the port no longer has
16805 * that exchange context, send a BLS_RJT. Override the IOCB for
16806 * a BA_RJT.
16807 */
16808 if ((fctl & FC_FC_EX_CTX) &&
895427bd 16809 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
16810 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16811 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16812 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16813 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16814 }
16815
16816 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16817 * the driver no longer has that exchange, send a BLS_RJT. Override
16818 * the IOCB for a BA_RJT.
546fc854 16819 */
6dd9e31c 16820 if (aborted == false) {
546fc854
JS
16821 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16822 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16823 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16824 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16825 }
6669f9bb 16826
5ffc266e
JS
16827 if (fctl & FC_FC_EX_CTX) {
16828 /* ABTS sent by responder to CT exchange, construction
16829 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16830 * field and RX_ID from ABTS for RX_ID field.
16831 */
546fc854 16832 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
16833 } else {
16834 /* ABTS sent by initiator to CT exchange, construction
16835 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 16836 * XRI_TAG field.
5ffc266e 16837 */
546fc854 16838 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 16839 }
f09c3acc 16840 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 16841 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 16842
546fc854 16843 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
16844 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16845 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16846 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
16847
16848 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16849 if (rc == IOCB_ERROR) {
6dd9e31c
JS
16850 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16851 "2925 Failed to issue CT ABTS RSP x%x on "
16852 "xri x%x, Data x%x\n",
16853 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16854 phba->link_state);
16855 lpfc_nlp_put(ndlp);
16856 ctiocb->context1 = NULL;
546fc854
JS
16857 lpfc_sli_release_iocbq(phba, ctiocb);
16858 }
6669f9bb
JS
16859}
16860
16861/**
16862 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16863 * @vport: Pointer to the vport on which this sequence was received
16864 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16865 *
16866 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16867 * receive sequence is only partially assembed by the driver, it shall abort
16868 * the partially assembled frames for the sequence. Otherwise, if the
16869 * unsolicited receive sequence has been completely assembled and passed to
16870 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16871 * unsolicited sequence has been aborted. After that, it will issue a basic
16872 * accept to accept the abort.
16873 **/
5d8b8167 16874static void
6669f9bb
JS
16875lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16876 struct hbq_dmabuf *dmabuf)
16877{
16878 struct lpfc_hba *phba = vport->phba;
16879 struct fc_frame_header fc_hdr;
5ffc266e 16880 uint32_t fctl;
6dd9e31c 16881 bool aborted;
6669f9bb 16882
6669f9bb
JS
16883 /* Make a copy of fc_hdr before the dmabuf being released */
16884 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 16885 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 16886
5ffc266e 16887 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
16888 /* ABTS by responder to exchange, no cleanup needed */
16889 aborted = true;
5ffc266e 16890 } else {
6dd9e31c
JS
16891 /* ABTS by initiator to exchange, need to do cleanup */
16892 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16893 if (aborted == false)
16894 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 16895 }
6dd9e31c
JS
16896 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16897
86c67379
JS
16898 if (phba->nvmet_support) {
16899 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16900 return;
16901 }
16902
6dd9e31c
JS
16903 /* Respond with BA_ACC or BA_RJT accordingly */
16904 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
16905}
16906
4f774513
JS
16907/**
16908 * lpfc_seq_complete - Indicates if a sequence is complete
16909 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16910 *
16911 * This function checks the sequence, starting with the frame described by
16912 * @dmabuf, to see if all the frames associated with this sequence are present.
16913 * the frames associated with this sequence are linked to the @dmabuf using the
16914 * dbuf list. This function looks for two major things. 1) That the first frame
16915 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16916 * set. 3) That there are no holes in the sequence count. The function will
16917 * return 1 when the sequence is complete, otherwise it will return 0.
16918 **/
16919static int
16920lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16921{
16922 struct fc_frame_header *hdr;
16923 struct lpfc_dmabuf *d_buf;
16924 struct hbq_dmabuf *seq_dmabuf;
16925 uint32_t fctl;
16926 int seq_count = 0;
16927
16928 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16929 /* make sure first fame of sequence has a sequence count of zero */
16930 if (hdr->fh_seq_cnt != seq_count)
16931 return 0;
16932 fctl = (hdr->fh_f_ctl[0] << 16 |
16933 hdr->fh_f_ctl[1] << 8 |
16934 hdr->fh_f_ctl[2]);
16935 /* If last frame of sequence we can return success. */
16936 if (fctl & FC_FC_END_SEQ)
16937 return 1;
16938 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16939 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16940 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16941 /* If there is a hole in the sequence count then fail. */
eeead811 16942 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
16943 return 0;
16944 fctl = (hdr->fh_f_ctl[0] << 16 |
16945 hdr->fh_f_ctl[1] << 8 |
16946 hdr->fh_f_ctl[2]);
16947 /* If last frame of sequence we can return success. */
16948 if (fctl & FC_FC_END_SEQ)
16949 return 1;
16950 }
16951 return 0;
16952}
16953
16954/**
16955 * lpfc_prep_seq - Prep sequence for ULP processing
16956 * @vport: Pointer to the vport on which this sequence was received
16957 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16958 *
16959 * This function takes a sequence, described by a list of frames, and creates
16960 * a list of iocbq structures to describe the sequence. This iocbq list will be
16961 * used to issue to the generic unsolicited sequence handler. This routine
16962 * returns a pointer to the first iocbq in the list. If the function is unable
16963 * to allocate an iocbq then it throw out the received frames that were not
16964 * able to be described and return a pointer to the first iocbq. If unable to
16965 * allocate any iocbqs (including the first) this function will return NULL.
16966 **/
16967static struct lpfc_iocbq *
16968lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16969{
7851fe2c 16970 struct hbq_dmabuf *hbq_buf;
4f774513
JS
16971 struct lpfc_dmabuf *d_buf, *n_buf;
16972 struct lpfc_iocbq *first_iocbq, *iocbq;
16973 struct fc_frame_header *fc_hdr;
16974 uint32_t sid;
7851fe2c 16975 uint32_t len, tot_len;
eeead811 16976 struct ulp_bde64 *pbde;
4f774513
JS
16977
16978 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16979 /* remove from receive buffer list */
16980 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 16981 lpfc_update_rcv_time_stamp(vport);
4f774513 16982 /* get the Remote Port's SID */
6669f9bb 16983 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 16984 tot_len = 0;
4f774513
JS
16985 /* Get an iocbq struct to fill in. */
16986 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16987 if (first_iocbq) {
16988 /* Initialize the first IOCB. */
8fa38513 16989 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 16990 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 16991 first_iocbq->vport = vport;
939723a4
JS
16992
16993 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16994 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16995 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16996 first_iocbq->iocb.un.rcvels.parmRo =
16997 sli4_did_from_fc_hdr(fc_hdr);
16998 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16999 } else
17000 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17001 first_iocbq->iocb.ulpContext = NO_XRI;
17002 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17003 be16_to_cpu(fc_hdr->fh_ox_id);
17004 /* iocbq is prepped for internal consumption. Physical vpi. */
17005 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17006 vport->phba->vpi_ids[vport->vpi];
4f774513 17007 /* put the first buffer into the first IOCBq */
48a5a664
JS
17008 tot_len = bf_get(lpfc_rcqe_length,
17009 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17010
4f774513
JS
17011 first_iocbq->context2 = &seq_dmabuf->dbuf;
17012 first_iocbq->context3 = NULL;
17013 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17014 if (tot_len > LPFC_DATA_BUF_SIZE)
17015 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17016 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17017 else
17018 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17019
4f774513 17020 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17021
7851fe2c 17022 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17023 }
17024 iocbq = first_iocbq;
17025 /*
17026 * Each IOCBq can have two Buffers assigned, so go through the list
17027 * of buffers for this sequence and save two buffers in each IOCBq
17028 */
17029 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17030 if (!iocbq) {
17031 lpfc_in_buf_free(vport->phba, d_buf);
17032 continue;
17033 }
17034 if (!iocbq->context3) {
17035 iocbq->context3 = d_buf;
17036 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17037 /* We need to get the size out of the right CQE */
17038 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17039 len = bf_get(lpfc_rcqe_length,
17040 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17041 pbde = (struct ulp_bde64 *)
17042 &iocbq->iocb.unsli3.sli3Words[4];
17043 if (len > LPFC_DATA_BUF_SIZE)
17044 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17045 else
17046 pbde->tus.f.bdeSize = len;
17047
7851fe2c
JS
17048 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17049 tot_len += len;
4f774513
JS
17050 } else {
17051 iocbq = lpfc_sli_get_iocbq(vport->phba);
17052 if (!iocbq) {
17053 if (first_iocbq) {
17054 first_iocbq->iocb.ulpStatus =
17055 IOSTAT_FCP_RSP_ERROR;
17056 first_iocbq->iocb.un.ulpWord[4] =
17057 IOERR_NO_RESOURCES;
17058 }
17059 lpfc_in_buf_free(vport->phba, d_buf);
17060 continue;
17061 }
48a5a664
JS
17062 /* We need to get the size out of the right CQE */
17063 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17064 len = bf_get(lpfc_rcqe_length,
17065 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17066 iocbq->context2 = d_buf;
17067 iocbq->context3 = NULL;
17068 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17069 if (len > LPFC_DATA_BUF_SIZE)
17070 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17071 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17072 else
17073 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17074
7851fe2c
JS
17075 tot_len += len;
17076 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17077
4f774513
JS
17078 iocbq->iocb.un.rcvels.remoteID = sid;
17079 list_add_tail(&iocbq->list, &first_iocbq->list);
17080 }
17081 }
17082 return first_iocbq;
17083}
17084
6669f9bb
JS
17085static void
17086lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17087 struct hbq_dmabuf *seq_dmabuf)
17088{
17089 struct fc_frame_header *fc_hdr;
17090 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17091 struct lpfc_hba *phba = vport->phba;
17092
17093 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17094 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17095 if (!iocbq) {
17096 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17097 "2707 Ring %d handler: Failed to allocate "
17098 "iocb Rctl x%x Type x%x received\n",
17099 LPFC_ELS_RING,
17100 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17101 return;
17102 }
17103 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17104 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17105 iocbq, fc_hdr->fh_r_ctl,
17106 fc_hdr->fh_type))
6d368e53 17107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17108 "2540 Ring %d handler: unexpected Rctl "
17109 "x%x Type x%x received\n",
17110 LPFC_ELS_RING,
17111 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17112
17113 /* Free iocb created in lpfc_prep_seq */
17114 list_for_each_entry_safe(curr_iocb, next_iocb,
17115 &iocbq->list, list) {
17116 list_del_init(&curr_iocb->list);
17117 lpfc_sli_release_iocbq(phba, curr_iocb);
17118 }
17119 lpfc_sli_release_iocbq(phba, iocbq);
17120}
17121
ae9e28f3
JS
17122static void
17123lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17124 struct lpfc_iocbq *rspiocb)
17125{
17126 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17127
17128 if (pcmd && pcmd->virt)
771db5c0 17129 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17130 kfree(pcmd);
17131 lpfc_sli_release_iocbq(phba, cmdiocb);
17132}
17133
17134static void
17135lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17136 struct hbq_dmabuf *dmabuf)
17137{
17138 struct fc_frame_header *fc_hdr;
17139 struct lpfc_hba *phba = vport->phba;
17140 struct lpfc_iocbq *iocbq = NULL;
17141 union lpfc_wqe *wqe;
17142 struct lpfc_dmabuf *pcmd = NULL;
17143 uint32_t frame_len;
17144 int rc;
17145
17146 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17147 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17148
17149 /* Send the received frame back */
17150 iocbq = lpfc_sli_get_iocbq(phba);
17151 if (!iocbq)
17152 goto exit;
17153
17154 /* Allocate buffer for command payload */
17155 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17156 if (pcmd)
771db5c0 17157 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17158 &pcmd->phys);
17159 if (!pcmd || !pcmd->virt)
17160 goto exit;
17161
17162 INIT_LIST_HEAD(&pcmd->list);
17163
17164 /* copyin the payload */
17165 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17166
17167 /* fill in BDE's for command */
17168 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17169 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17170 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17171 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17172
17173 iocbq->context2 = pcmd;
17174 iocbq->vport = vport;
17175 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17176 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17177
17178 /*
17179 * Setup rest of the iocb as though it were a WQE
17180 * Build the SEND_FRAME WQE
17181 */
17182 wqe = (union lpfc_wqe *)&iocbq->iocb;
17183
17184 wqe->send_frame.frame_len = frame_len;
17185 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17186 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17187 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17188 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17189 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17190 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17191
17192 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17193 iocbq->iocb.ulpLe = 1;
17194 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17195 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17196 if (rc == IOCB_ERROR)
17197 goto exit;
17198
17199 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17200 return;
17201
17202exit:
17203 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17204 "2023 Unable to process MDS loopback frame\n");
17205 if (pcmd && pcmd->virt)
771db5c0 17206 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17207 kfree(pcmd);
401bb416
DK
17208 if (iocbq)
17209 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17210 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17211}
17212
4f774513
JS
17213/**
17214 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17215 * @phba: Pointer to HBA context object.
17216 *
17217 * This function is called with no lock held. This function processes all
17218 * the received buffers and gives it to upper layers when a received buffer
17219 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17220 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17221 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17222 * appropriate receive function when the final frame in a sequence is received.
17223 **/
4d9ab994
JS
17224void
17225lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17226 struct hbq_dmabuf *dmabuf)
4f774513 17227{
4d9ab994 17228 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17229 struct fc_frame_header *fc_hdr;
17230 struct lpfc_vport *vport;
17231 uint32_t fcfi;
939723a4 17232 uint32_t did;
4f774513 17233
4f774513 17234 /* Process each received buffer */
4d9ab994 17235 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17236
4d9ab994
JS
17237 /* check to see if this a valid type of frame */
17238 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17239 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17240 return;
17241 }
2ea259ee 17242
7851fe2c
JS
17243 if ((bf_get(lpfc_cqe_code,
17244 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17245 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17246 &dmabuf->cq_event.cqe.rcqe_cmpl);
17247 else
17248 fcfi = bf_get(lpfc_rcqe_fcf_id,
17249 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17250
ae9e28f3
JS
17251 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17252 vport = phba->pport;
17253 /* Handle MDS Loopback frames */
17254 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17255 return;
17256 }
17257
895427bd
JS
17258 /* d_id this frame is directed to */
17259 did = sli4_did_from_fc_hdr(fc_hdr);
17260
17261 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17262 if (!vport) {
4d9ab994
JS
17263 /* throw out the frame */
17264 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17265 return;
17266 }
939723a4 17267
939723a4
JS
17268 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17269 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17270 (did != Fabric_DID)) {
17271 /*
17272 * Throw out the frame if we are not pt2pt.
17273 * The pt2pt protocol allows for discovery frames
17274 * to be received without a registered VPI.
17275 */
17276 if (!(vport->fc_flag & FC_PT2PT) ||
17277 (phba->link_state == LPFC_HBA_READY)) {
17278 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17279 return;
17280 }
17281 }
17282
6669f9bb
JS
17283 /* Handle the basic abort sequence (BA_ABTS) event */
17284 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17285 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17286 return;
17287 }
17288
4d9ab994
JS
17289 /* Link this frame */
17290 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17291 if (!seq_dmabuf) {
17292 /* unable to add frame to vport - throw it out */
17293 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17294 return;
17295 }
17296 /* If not last frame in sequence continue processing frames. */
def9c7a9 17297 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17298 return;
def9c7a9 17299
6669f9bb
JS
17300 /* Send the complete sequence to the upper layer protocol */
17301 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17302}
6fb120a7
JS
17303
17304/**
17305 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17306 * @phba: pointer to lpfc hba data structure.
17307 *
17308 * This routine is invoked to post rpi header templates to the
17309 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17310 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17311 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17312 *
17313 * This routine does not require any locks. It's usage is expected
17314 * to be driver load or reset recovery when the driver is
17315 * sequential.
17316 *
17317 * Return codes
af901ca1 17318 * 0 - successful
d439d286 17319 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17320 * When this error occurs, the driver is not guaranteed
17321 * to have any rpi regions posted to the device and
17322 * must either attempt to repost the regions or take a
17323 * fatal error.
17324 **/
17325int
17326lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17327{
17328 struct lpfc_rpi_hdr *rpi_page;
17329 uint32_t rc = 0;
6d368e53
JS
17330 uint16_t lrpi = 0;
17331
17332 /* SLI4 ports that support extents do not require RPI headers. */
17333 if (!phba->sli4_hba.rpi_hdrs_in_use)
17334 goto exit;
17335 if (phba->sli4_hba.extents_in_use)
17336 return -EIO;
6fb120a7 17337
6fb120a7 17338 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17339 /*
17340 * Assign the rpi headers a physical rpi only if the driver
17341 * has not initialized those resources. A port reset only
17342 * needs the headers posted.
17343 */
17344 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17345 LPFC_RPI_RSRC_RDY)
17346 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17347
6fb120a7
JS
17348 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17349 if (rc != MBX_SUCCESS) {
17350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17351 "2008 Error %d posting all rpi "
17352 "headers\n", rc);
17353 rc = -EIO;
17354 break;
17355 }
17356 }
17357
6d368e53
JS
17358 exit:
17359 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17360 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17361 return rc;
17362}
17363
17364/**
17365 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17366 * @phba: pointer to lpfc hba data structure.
17367 * @rpi_page: pointer to the rpi memory region.
17368 *
17369 * This routine is invoked to post a single rpi header to the
17370 * HBA consistent with the SLI-4 interface spec. This memory region
17371 * maps up to 64 rpi context regions.
17372 *
17373 * Return codes
af901ca1 17374 * 0 - successful
d439d286
JS
17375 * -ENOMEM - No available memory
17376 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17377 **/
17378int
17379lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17380{
17381 LPFC_MBOXQ_t *mboxq;
17382 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17383 uint32_t rc = 0;
6fb120a7
JS
17384 uint32_t shdr_status, shdr_add_status;
17385 union lpfc_sli4_cfg_shdr *shdr;
17386
6d368e53
JS
17387 /* SLI4 ports that support extents do not require RPI headers. */
17388 if (!phba->sli4_hba.rpi_hdrs_in_use)
17389 return rc;
17390 if (phba->sli4_hba.extents_in_use)
17391 return -EIO;
17392
6fb120a7
JS
17393 /* The port is notified of the header region via a mailbox command. */
17394 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17395 if (!mboxq) {
17396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17397 "2001 Unable to allocate memory for issuing "
17398 "SLI_CONFIG_SPECIAL mailbox command\n");
17399 return -ENOMEM;
17400 }
17401
17402 /* Post all rpi memory regions to the port. */
17403 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17404 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17405 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17406 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17407 sizeof(struct lpfc_sli4_cfg_mhdr),
17408 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17409
17410
17411 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17412 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17413 rpi_page->start_rpi);
6d368e53
JS
17414 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17415 hdr_tmpl, rpi_page->page_count);
17416
6fb120a7
JS
17417 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17418 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17419 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17420 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17421 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17422 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17423 if (rc != MBX_TIMEOUT)
17424 mempool_free(mboxq, phba->mbox_mem_pool);
17425 if (shdr_status || shdr_add_status || rc) {
17426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17427 "2514 POST_RPI_HDR mailbox failed with "
17428 "status x%x add_status x%x, mbx status x%x\n",
17429 shdr_status, shdr_add_status, rc);
17430 rc = -ENXIO;
845d9e8d
JS
17431 } else {
17432 /*
17433 * The next_rpi stores the next logical module-64 rpi value used
17434 * to post physical rpis in subsequent rpi postings.
17435 */
17436 spin_lock_irq(&phba->hbalock);
17437 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17438 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17439 }
17440 return rc;
17441}
17442
17443/**
17444 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17445 * @phba: pointer to lpfc hba data structure.
17446 *
17447 * This routine is invoked to post rpi header templates to the
17448 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17449 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17450 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17451 *
17452 * Returns
af901ca1 17453 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17454 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17455 **/
17456int
17457lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17458{
6d368e53
JS
17459 unsigned long rpi;
17460 uint16_t max_rpi, rpi_limit;
17461 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17462 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17463 unsigned long iflag;
6fb120a7 17464
6fb120a7 17465 /*
6d368e53
JS
17466 * Fetch the next logical rpi. Because this index is logical,
17467 * the driver starts at 0 each time.
6fb120a7 17468 */
4902b381 17469 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17470 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17471 rpi_limit = phba->sli4_hba.next_rpi;
17472
6d368e53
JS
17473 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17474 if (rpi >= rpi_limit)
6fb120a7
JS
17475 rpi = LPFC_RPI_ALLOC_ERROR;
17476 else {
17477 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17478 phba->sli4_hba.max_cfg_param.rpi_used++;
17479 phba->sli4_hba.rpi_count++;
17480 }
be6bb941
JS
17481 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17482 "0001 rpi:%x max:%x lim:%x\n",
17483 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17484
17485 /*
17486 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17487 * has been exhausted.
6fb120a7
JS
17488 */
17489 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17490 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17491 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17492 return rpi;
17493 }
17494
6d368e53
JS
17495 /*
17496 * RPI header postings are not required for SLI4 ports capable of
17497 * extents.
17498 */
17499 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17500 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17501 return rpi;
17502 }
17503
6fb120a7
JS
17504 /*
17505 * If the driver is running low on rpi resources, allocate another
17506 * page now. Note that the next_rpi value is used because
17507 * it represents how many are actually in use whereas max_rpi notes
17508 * how many are supported max by the device.
17509 */
6d368e53 17510 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17511 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17512 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17513 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17514 if (!rpi_hdr) {
17515 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17516 "2002 Error Could not grow rpi "
17517 "count\n");
17518 } else {
6d368e53
JS
17519 lrpi = rpi_hdr->start_rpi;
17520 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17521 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17522 }
17523 }
17524
17525 return rpi;
17526}
17527
d7c47992
JS
17528/**
17529 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17530 * @phba: pointer to lpfc hba data structure.
17531 *
17532 * This routine is invoked to release an rpi to the pool of
17533 * available rpis maintained by the driver.
17534 **/
5d8b8167 17535static void
d7c47992
JS
17536__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17537{
17538 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17539 phba->sli4_hba.rpi_count--;
17540 phba->sli4_hba.max_cfg_param.rpi_used--;
17541 }
17542}
17543
6fb120a7
JS
17544/**
17545 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17546 * @phba: pointer to lpfc hba data structure.
17547 *
17548 * This routine is invoked to release an rpi to the pool of
17549 * available rpis maintained by the driver.
17550 **/
17551void
17552lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17553{
17554 spin_lock_irq(&phba->hbalock);
d7c47992 17555 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17556 spin_unlock_irq(&phba->hbalock);
17557}
17558
17559/**
17560 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17561 * @phba: pointer to lpfc hba data structure.
17562 *
17563 * This routine is invoked to remove the memory region that
17564 * provided rpi via a bitmask.
17565 **/
17566void
17567lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17568{
17569 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17570 kfree(phba->sli4_hba.rpi_ids);
17571 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17572}
17573
17574/**
17575 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17576 * @phba: pointer to lpfc hba data structure.
17577 *
17578 * This routine is invoked to remove the memory region that
17579 * provided rpi via a bitmask.
17580 **/
17581int
6b5151fd
JS
17582lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17583 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17584{
17585 LPFC_MBOXQ_t *mboxq;
17586 struct lpfc_hba *phba = ndlp->phba;
17587 int rc;
17588
17589 /* The port is notified of the header region via a mailbox command. */
17590 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17591 if (!mboxq)
17592 return -ENOMEM;
17593
17594 /* Post all rpi memory regions to the port. */
17595 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17596 if (cmpl) {
17597 mboxq->mbox_cmpl = cmpl;
17598 mboxq->context1 = arg;
17599 mboxq->context2 = ndlp;
72859909
JS
17600 } else
17601 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17602 mboxq->vport = ndlp->vport;
6fb120a7
JS
17603 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17604 if (rc == MBX_NOT_FINISHED) {
17605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17606 "2010 Resume RPI Mailbox failed "
17607 "status %d, mbxStatus x%x\n", rc,
17608 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17609 mempool_free(mboxq, phba->mbox_mem_pool);
17610 return -EIO;
17611 }
17612 return 0;
17613}
17614
17615/**
17616 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17617 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17618 *
76a95d75 17619 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17620 *
17621 * Returns:
17622 * 0 success
17623 * -Evalue otherwise
17624 **/
17625int
76a95d75 17626lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17627{
17628 LPFC_MBOXQ_t *mboxq;
17629 int rc = 0;
6a9c52cf 17630 int retval = MBX_SUCCESS;
6fb120a7 17631 uint32_t mbox_tmo;
76a95d75 17632 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17633 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17634 if (!mboxq)
17635 return -ENOMEM;
76a95d75 17636 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17637 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17638 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17639 if (rc != MBX_SUCCESS) {
76a95d75 17640 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17641 "2022 INIT VPI Mailbox failed "
17642 "status %d, mbxStatus x%x\n", rc,
17643 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17644 retval = -EIO;
6fb120a7 17645 }
6a9c52cf 17646 if (rc != MBX_TIMEOUT)
76a95d75 17647 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17648
17649 return retval;
6fb120a7
JS
17650}
17651
17652/**
17653 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17654 * @phba: pointer to lpfc hba data structure.
17655 * @mboxq: Pointer to mailbox object.
17656 *
17657 * This routine is invoked to manually add a single FCF record. The caller
17658 * must pass a completely initialized FCF_Record. This routine takes
17659 * care of the nonembedded mailbox operations.
17660 **/
17661static void
17662lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17663{
17664 void *virt_addr;
17665 union lpfc_sli4_cfg_shdr *shdr;
17666 uint32_t shdr_status, shdr_add_status;
17667
17668 virt_addr = mboxq->sge_array->addr[0];
17669 /* The IOCTL status is embedded in the mailbox subheader. */
17670 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17671 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17672 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17673
17674 if ((shdr_status || shdr_add_status) &&
17675 (shdr_status != STATUS_FCF_IN_USE))
17676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17677 "2558 ADD_FCF_RECORD mailbox failed with "
17678 "status x%x add_status x%x\n",
17679 shdr_status, shdr_add_status);
17680
17681 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17682}
17683
17684/**
17685 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17686 * @phba: pointer to lpfc hba data structure.
17687 * @fcf_record: pointer to the initialized fcf record to add.
17688 *
17689 * This routine is invoked to manually add a single FCF record. The caller
17690 * must pass a completely initialized FCF_Record. This routine takes
17691 * care of the nonembedded mailbox operations.
17692 **/
17693int
17694lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17695{
17696 int rc = 0;
17697 LPFC_MBOXQ_t *mboxq;
17698 uint8_t *bytep;
17699 void *virt_addr;
6fb120a7
JS
17700 struct lpfc_mbx_sge sge;
17701 uint32_t alloc_len, req_len;
17702 uint32_t fcfindex;
17703
17704 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17705 if (!mboxq) {
17706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17707 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17708 return -ENOMEM;
17709 }
17710
17711 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17712 sizeof(uint32_t);
17713
17714 /* Allocate DMA memory and set up the non-embedded mailbox command */
17715 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17716 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17717 req_len, LPFC_SLI4_MBX_NEMBED);
17718 if (alloc_len < req_len) {
17719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17720 "2523 Allocated DMA memory size (x%x) is "
17721 "less than the requested DMA memory "
17722 "size (x%x)\n", alloc_len, req_len);
17723 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17724 return -ENOMEM;
17725 }
17726
17727 /*
17728 * Get the first SGE entry from the non-embedded DMA memory. This
17729 * routine only uses a single SGE.
17730 */
17731 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
17732 virt_addr = mboxq->sge_array->addr[0];
17733 /*
17734 * Configure the FCF record for FCFI 0. This is the driver's
17735 * hardcoded default and gets used in nonFIP mode.
17736 */
17737 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17738 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17739 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17740
17741 /*
17742 * Copy the fcf_index and the FCF Record Data. The data starts after
17743 * the FCoE header plus word10. The data copy needs to be endian
17744 * correct.
17745 */
17746 bytep += sizeof(uint32_t);
17747 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17748 mboxq->vport = phba->pport;
17749 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17750 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17751 if (rc == MBX_NOT_FINISHED) {
17752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17753 "2515 ADD_FCF_RECORD mailbox failed with "
17754 "status 0x%x\n", rc);
17755 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17756 rc = -EIO;
17757 } else
17758 rc = 0;
17759
17760 return rc;
17761}
17762
17763/**
17764 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17765 * @phba: pointer to lpfc hba data structure.
17766 * @fcf_record: pointer to the fcf record to write the default data.
17767 * @fcf_index: FCF table entry index.
17768 *
17769 * This routine is invoked to build the driver's default FCF record. The
17770 * values used are hardcoded. This routine handles memory initialization.
17771 *
17772 **/
17773void
17774lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17775 struct fcf_record *fcf_record,
17776 uint16_t fcf_index)
17777{
17778 memset(fcf_record, 0, sizeof(struct fcf_record));
17779 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17780 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17781 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17782 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17783 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17784 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17785 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17786 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17787 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17788 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17789 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17790 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17791 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 17792 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
17793 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17794 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17795 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17796 /* Set the VLAN bit map */
17797 if (phba->valid_vlan) {
17798 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17799 = 1 << (phba->vlan_id % 8);
17800 }
17801}
17802
17803/**
0c9ab6f5 17804 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
17805 * @phba: pointer to lpfc hba data structure.
17806 * @fcf_index: FCF table entry offset.
17807 *
0c9ab6f5
JS
17808 * This routine is invoked to scan the entire FCF table by reading FCF
17809 * record and processing it one at a time starting from the @fcf_index
17810 * for initial FCF discovery or fast FCF failover rediscovery.
17811 *
25985edc 17812 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 17813 * otherwise.
6fb120a7
JS
17814 **/
17815int
0c9ab6f5 17816lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
17817{
17818 int rc = 0, error;
17819 LPFC_MBOXQ_t *mboxq;
6fb120a7 17820
32b9793f 17821 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 17822 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
17823 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17824 if (!mboxq) {
17825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17826 "2000 Failed to allocate mbox for "
17827 "READ_FCF cmd\n");
4d9ab994 17828 error = -ENOMEM;
0c9ab6f5 17829 goto fail_fcf_scan;
6fb120a7 17830 }
ecfd03c6 17831 /* Construct the read FCF record mailbox command */
0c9ab6f5 17832 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
17833 if (rc) {
17834 error = -EINVAL;
0c9ab6f5 17835 goto fail_fcf_scan;
6fb120a7 17836 }
ecfd03c6 17837 /* Issue the mailbox command asynchronously */
6fb120a7 17838 mboxq->vport = phba->pport;
0c9ab6f5 17839 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
17840
17841 spin_lock_irq(&phba->hbalock);
17842 phba->hba_flag |= FCF_TS_INPROG;
17843 spin_unlock_irq(&phba->hbalock);
17844
6fb120a7 17845 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 17846 if (rc == MBX_NOT_FINISHED)
6fb120a7 17847 error = -EIO;
ecfd03c6 17848 else {
38b92ef8
JS
17849 /* Reset eligible FCF count for new scan */
17850 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 17851 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 17852 error = 0;
32b9793f 17853 }
0c9ab6f5 17854fail_fcf_scan:
4d9ab994
JS
17855 if (error) {
17856 if (mboxq)
17857 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 17858 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 17859 spin_lock_irq(&phba->hbalock);
a93ff37a 17860 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
17861 spin_unlock_irq(&phba->hbalock);
17862 }
6fb120a7
JS
17863 return error;
17864}
a0c87cbd 17865
0c9ab6f5 17866/**
a93ff37a 17867 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
17868 * @phba: pointer to lpfc hba data structure.
17869 * @fcf_index: FCF table entry offset.
17870 *
17871 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 17872 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 17873 *
25985edc 17874 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17875 * otherwise.
17876 **/
17877int
17878lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17879{
17880 int rc = 0, error;
17881 LPFC_MBOXQ_t *mboxq;
17882
17883 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17884 if (!mboxq) {
17885 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17886 "2763 Failed to allocate mbox for "
17887 "READ_FCF cmd\n");
17888 error = -ENOMEM;
17889 goto fail_fcf_read;
17890 }
17891 /* Construct the read FCF record mailbox command */
17892 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17893 if (rc) {
17894 error = -EINVAL;
17895 goto fail_fcf_read;
17896 }
17897 /* Issue the mailbox command asynchronously */
17898 mboxq->vport = phba->pport;
17899 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17900 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17901 if (rc == MBX_NOT_FINISHED)
17902 error = -EIO;
17903 else
17904 error = 0;
17905
17906fail_fcf_read:
17907 if (error && mboxq)
17908 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17909 return error;
17910}
17911
17912/**
17913 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17914 * @phba: pointer to lpfc hba data structure.
17915 * @fcf_index: FCF table entry offset.
17916 *
17917 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 17918 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 17919 *
25985edc 17920 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17921 * otherwise.
17922 **/
17923int
17924lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17925{
17926 int rc = 0, error;
17927 LPFC_MBOXQ_t *mboxq;
17928
17929 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17930 if (!mboxq) {
17931 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17932 "2758 Failed to allocate mbox for "
17933 "READ_FCF cmd\n");
17934 error = -ENOMEM;
17935 goto fail_fcf_read;
17936 }
17937 /* Construct the read FCF record mailbox command */
17938 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17939 if (rc) {
17940 error = -EINVAL;
17941 goto fail_fcf_read;
17942 }
17943 /* Issue the mailbox command asynchronously */
17944 mboxq->vport = phba->pport;
17945 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17946 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17947 if (rc == MBX_NOT_FINISHED)
17948 error = -EIO;
17949 else
17950 error = 0;
17951
17952fail_fcf_read:
17953 if (error && mboxq)
17954 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17955 return error;
17956}
17957
7d791df7 17958/**
f5cb5304 17959 * lpfc_check_next_fcf_pri_level
7d791df7
JS
17960 * phba pointer to the lpfc_hba struct for this port.
17961 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17962 * routine when the rr_bmask is empty. The FCF indecies are put into the
17963 * rr_bmask based on their priority level. Starting from the highest priority
17964 * to the lowest. The most likely FCF candidate will be in the highest
17965 * priority group. When this routine is called it searches the fcf_pri list for
17966 * next lowest priority group and repopulates the rr_bmask with only those
17967 * fcf_indexes.
17968 * returns:
17969 * 1=success 0=failure
17970 **/
5d8b8167 17971static int
7d791df7
JS
17972lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17973{
17974 uint16_t next_fcf_pri;
17975 uint16_t last_index;
17976 struct lpfc_fcf_pri *fcf_pri;
17977 int rc;
17978 int ret = 0;
17979
17980 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17981 LPFC_SLI4_FCF_TBL_INDX_MAX);
17982 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17983 "3060 Last IDX %d\n", last_index);
2562669c
JS
17984
17985 /* Verify the priority list has 2 or more entries */
17986 spin_lock_irq(&phba->hbalock);
17987 if (list_empty(&phba->fcf.fcf_pri_list) ||
17988 list_is_singular(&phba->fcf.fcf_pri_list)) {
17989 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
17990 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17991 "3061 Last IDX %d\n", last_index);
17992 return 0; /* Empty rr list */
17993 }
2562669c
JS
17994 spin_unlock_irq(&phba->hbalock);
17995
7d791df7
JS
17996 next_fcf_pri = 0;
17997 /*
17998 * Clear the rr_bmask and set all of the bits that are at this
17999 * priority.
18000 */
18001 memset(phba->fcf.fcf_rr_bmask, 0,
18002 sizeof(*phba->fcf.fcf_rr_bmask));
18003 spin_lock_irq(&phba->hbalock);
18004 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18005 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18006 continue;
18007 /*
18008 * the 1st priority that has not FLOGI failed
18009 * will be the highest.
18010 */
18011 if (!next_fcf_pri)
18012 next_fcf_pri = fcf_pri->fcf_rec.priority;
18013 spin_unlock_irq(&phba->hbalock);
18014 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18015 rc = lpfc_sli4_fcf_rr_index_set(phba,
18016 fcf_pri->fcf_rec.fcf_index);
18017 if (rc)
18018 return 0;
18019 }
18020 spin_lock_irq(&phba->hbalock);
18021 }
18022 /*
18023 * if next_fcf_pri was not set above and the list is not empty then
18024 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18025 * and start at the beginning.
7d791df7
JS
18026 */
18027 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18028 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18029 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18030 /*
18031 * the 1st priority that has not FLOGI failed
18032 * will be the highest.
18033 */
18034 if (!next_fcf_pri)
18035 next_fcf_pri = fcf_pri->fcf_rec.priority;
18036 spin_unlock_irq(&phba->hbalock);
18037 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18038 rc = lpfc_sli4_fcf_rr_index_set(phba,
18039 fcf_pri->fcf_rec.fcf_index);
18040 if (rc)
18041 return 0;
18042 }
18043 spin_lock_irq(&phba->hbalock);
18044 }
18045 } else
18046 ret = 1;
18047 spin_unlock_irq(&phba->hbalock);
18048
18049 return ret;
18050}
0c9ab6f5
JS
18051/**
18052 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18053 * @phba: pointer to lpfc hba data structure.
18054 *
18055 * This routine is to get the next eligible FCF record index in a round
18056 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18057 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18058 * shall be returned, otherwise, the next eligible FCF record's index
18059 * shall be returned.
18060 **/
18061uint16_t
18062lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18063{
18064 uint16_t next_fcf_index;
18065
421c6622 18066initial_priority:
3804dc84 18067 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18068 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18069
7d791df7 18070next_priority:
421c6622
JS
18071 /* Determine the next fcf index to check */
18072 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18073 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18074 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18075 next_fcf_index);
18076
0c9ab6f5 18077 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18078 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18079 /*
18080 * If we have wrapped then we need to clear the bits that
18081 * have been tested so that we can detect when we should
18082 * change the priority level.
18083 */
0c9ab6f5
JS
18084 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18085 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18086 }
18087
3804dc84
JS
18088
18089 /* Check roundrobin failover list empty condition */
7d791df7
JS
18090 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18091 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18092 /*
18093 * If next fcf index is not found check if there are lower
18094 * Priority level fcf's in the fcf_priority list.
18095 * Set up the rr_bmask with all of the avaiable fcf bits
18096 * at that level and continue the selection process.
18097 */
18098 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18099 goto initial_priority;
3804dc84
JS
18100 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18101 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18102 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18103 return LPFC_FCOE_FCF_NEXT_NONE;
18104 else {
18105 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18106 "3063 Only FCF available idx %d, flag %x\n",
18107 next_fcf_index,
18108 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18109 return next_fcf_index;
18110 }
3804dc84
JS
18111 }
18112
7d791df7
JS
18113 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18114 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18115 LPFC_FCF_FLOGI_FAILED) {
18116 if (list_is_singular(&phba->fcf.fcf_pri_list))
18117 return LPFC_FCOE_FCF_NEXT_NONE;
18118
7d791df7 18119 goto next_priority;
f5cb5304 18120 }
7d791df7 18121
3804dc84 18122 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18123 "2845 Get next roundrobin failover FCF (x%x)\n",
18124 next_fcf_index);
18125
0c9ab6f5
JS
18126 return next_fcf_index;
18127}
18128
18129/**
18130 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18131 * @phba: pointer to lpfc hba data structure.
18132 *
18133 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18134 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18135 * does not go beyond the range of the driver allocated bmask dimension
18136 * before setting the bit.
18137 *
18138 * Returns 0 if the index bit successfully set, otherwise, it returns
18139 * -EINVAL.
18140 **/
18141int
18142lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18143{
18144 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18145 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18146 "2610 FCF (x%x) reached driver's book "
18147 "keeping dimension:x%x\n",
0c9ab6f5
JS
18148 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18149 return -EINVAL;
18150 }
18151 /* Set the eligible FCF record index bmask */
18152 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18153
3804dc84 18154 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18155 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18156 "bmask\n", fcf_index);
18157
0c9ab6f5
JS
18158 return 0;
18159}
18160
18161/**
3804dc84 18162 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18163 * @phba: pointer to lpfc hba data structure.
18164 *
18165 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18166 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18167 * does not go beyond the range of the driver allocated bmask dimension
18168 * before clearing the bit.
18169 **/
18170void
18171lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18172{
9a803a74 18173 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18174 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18175 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18176 "2762 FCF (x%x) reached driver's book "
18177 "keeping dimension:x%x\n",
0c9ab6f5
JS
18178 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18179 return;
18180 }
18181 /* Clear the eligible FCF record index bmask */
7d791df7 18182 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18183 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18184 list) {
7d791df7
JS
18185 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18186 list_del_init(&fcf_pri->list);
18187 break;
18188 }
18189 }
18190 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18191 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18192
18193 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18194 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18195 "bmask\n", fcf_index);
0c9ab6f5
JS
18196}
18197
ecfd03c6
JS
18198/**
18199 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18200 * @phba: pointer to lpfc hba data structure.
18201 *
18202 * This routine is the completion routine for the rediscover FCF table mailbox
18203 * command. If the mailbox command returned failure, it will try to stop the
18204 * FCF rediscover wait timer.
18205 **/
5d8b8167 18206static void
ecfd03c6
JS
18207lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18208{
18209 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18210 uint32_t shdr_status, shdr_add_status;
18211
18212 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18213
18214 shdr_status = bf_get(lpfc_mbox_hdr_status,
18215 &redisc_fcf->header.cfg_shdr.response);
18216 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18217 &redisc_fcf->header.cfg_shdr.response);
18218 if (shdr_status || shdr_add_status) {
0c9ab6f5 18219 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18220 "2746 Requesting for FCF rediscovery failed "
18221 "status x%x add_status x%x\n",
18222 shdr_status, shdr_add_status);
0c9ab6f5 18223 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18224 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18225 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18226 spin_unlock_irq(&phba->hbalock);
18227 /*
18228 * CVL event triggered FCF rediscover request failed,
18229 * last resort to re-try current registered FCF entry.
18230 */
18231 lpfc_retry_pport_discovery(phba);
18232 } else {
18233 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18234 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18235 spin_unlock_irq(&phba->hbalock);
18236 /*
18237 * DEAD FCF event triggered FCF rediscover request
18238 * failed, last resort to fail over as a link down
18239 * to FCF registration.
18240 */
18241 lpfc_sli4_fcf_dead_failthrough(phba);
18242 }
0c9ab6f5
JS
18243 } else {
18244 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18245 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18246 /*
18247 * Start FCF rediscovery wait timer for pending FCF
18248 * before rescan FCF record table.
18249 */
18250 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18251 }
ecfd03c6
JS
18252
18253 mempool_free(mbox, phba->mbox_mem_pool);
18254}
18255
18256/**
3804dc84 18257 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18258 * @phba: pointer to lpfc hba data structure.
18259 *
18260 * This routine is invoked to request for rediscovery of the entire FCF table
18261 * by the port.
18262 **/
18263int
18264lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18265{
18266 LPFC_MBOXQ_t *mbox;
18267 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18268 int rc, length;
18269
0c9ab6f5
JS
18270 /* Cancel retry delay timers to all vports before FCF rediscover */
18271 lpfc_cancel_all_vport_retry_delay_timer(phba);
18272
ecfd03c6
JS
18273 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18274 if (!mbox) {
18275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18276 "2745 Failed to allocate mbox for "
18277 "requesting FCF rediscover.\n");
18278 return -ENOMEM;
18279 }
18280
18281 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18282 sizeof(struct lpfc_sli4_cfg_mhdr));
18283 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18284 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18285 length, LPFC_SLI4_MBX_EMBED);
18286
18287 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18288 /* Set count to 0 for invalidating the entire FCF database */
18289 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18290
18291 /* Issue the mailbox command asynchronously */
18292 mbox->vport = phba->pport;
18293 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18295
18296 if (rc == MBX_NOT_FINISHED) {
18297 mempool_free(mbox, phba->mbox_mem_pool);
18298 return -EIO;
18299 }
18300 return 0;
18301}
18302
fc2b989b
JS
18303/**
18304 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18305 * @phba: pointer to lpfc hba data structure.
18306 *
18307 * This function is the failover routine as a last resort to the FCF DEAD
18308 * event when driver failed to perform fast FCF failover.
18309 **/
18310void
18311lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18312{
18313 uint32_t link_state;
18314
18315 /*
18316 * Last resort as FCF DEAD event failover will treat this as
18317 * a link down, but save the link state because we don't want
18318 * it to be changed to Link Down unless it is already down.
18319 */
18320 link_state = phba->link_state;
18321 lpfc_linkdown(phba);
18322 phba->link_state = link_state;
18323
18324 /* Unregister FCF if no devices connected to it */
18325 lpfc_unregister_unused_fcf(phba);
18326}
18327
a0c87cbd 18328/**
026abb87 18329 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18330 * @phba: pointer to lpfc hba data structure.
026abb87 18331 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18332 *
026abb87
JS
18333 * This function gets SLI3 port configure region 23 data through memory dump
18334 * mailbox command. When it successfully retrieves data, the size of the data
18335 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18336 **/
026abb87
JS
18337static uint32_t
18338lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18339{
18340 LPFC_MBOXQ_t *pmb = NULL;
18341 MAILBOX_t *mb;
026abb87 18342 uint32_t offset = 0;
a0c87cbd
JS
18343 int rc;
18344
026abb87
JS
18345 if (!rgn23_data)
18346 return 0;
18347
a0c87cbd
JS
18348 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18349 if (!pmb) {
18350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18351 "2600 failed to allocate mailbox memory\n");
18352 return 0;
a0c87cbd
JS
18353 }
18354 mb = &pmb->u.mb;
18355
a0c87cbd
JS
18356 do {
18357 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18358 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18359
18360 if (rc != MBX_SUCCESS) {
18361 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18362 "2601 failed to read config "
18363 "region 23, rc 0x%x Status 0x%x\n",
18364 rc, mb->mbxStatus);
a0c87cbd
JS
18365 mb->un.varDmp.word_cnt = 0;
18366 }
18367 /*
18368 * dump mem may return a zero when finished or we got a
18369 * mailbox error, either way we are done.
18370 */
18371 if (mb->un.varDmp.word_cnt == 0)
18372 break;
18373 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18374 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18375
18376 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18377 rgn23_data + offset,
18378 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18379 offset += mb->un.varDmp.word_cnt;
18380 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18381
026abb87
JS
18382 mempool_free(pmb, phba->mbox_mem_pool);
18383 return offset;
18384}
18385
18386/**
18387 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18388 * @phba: pointer to lpfc hba data structure.
18389 * @rgn23_data: pointer to configure region 23 data.
18390 *
18391 * This function gets SLI4 port configure region 23 data through memory dump
18392 * mailbox command. When it successfully retrieves data, the size of the data
18393 * will be returned, otherwise, 0 will be returned.
18394 **/
18395static uint32_t
18396lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18397{
18398 LPFC_MBOXQ_t *mboxq = NULL;
18399 struct lpfc_dmabuf *mp = NULL;
18400 struct lpfc_mqe *mqe;
18401 uint32_t data_length = 0;
18402 int rc;
18403
18404 if (!rgn23_data)
18405 return 0;
18406
18407 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18408 if (!mboxq) {
18409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18410 "3105 failed to allocate mailbox memory\n");
18411 return 0;
18412 }
18413
18414 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18415 goto out;
18416 mqe = &mboxq->u.mqe;
18417 mp = (struct lpfc_dmabuf *) mboxq->context1;
18418 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18419 if (rc)
18420 goto out;
18421 data_length = mqe->un.mb_words[5];
18422 if (data_length == 0)
18423 goto out;
18424 if (data_length > DMP_RGN23_SIZE) {
18425 data_length = 0;
18426 goto out;
18427 }
18428 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18429out:
18430 mempool_free(mboxq, phba->mbox_mem_pool);
18431 if (mp) {
18432 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18433 kfree(mp);
18434 }
18435 return data_length;
18436}
18437
18438/**
18439 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18440 * @phba: pointer to lpfc hba data structure.
18441 *
18442 * This function read region 23 and parse TLV for port status to
18443 * decide if the user disaled the port. If the TLV indicates the
18444 * port is disabled, the hba_flag is set accordingly.
18445 **/
18446void
18447lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18448{
18449 uint8_t *rgn23_data = NULL;
18450 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18451 uint32_t offset = 0;
18452
18453 /* Get adapter Region 23 data */
18454 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18455 if (!rgn23_data)
18456 goto out;
18457
18458 if (phba->sli_rev < LPFC_SLI_REV4)
18459 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18460 else {
18461 if_type = bf_get(lpfc_sli_intf_if_type,
18462 &phba->sli4_hba.sli_intf);
18463 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18464 goto out;
18465 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18466 }
a0c87cbd
JS
18467
18468 if (!data_size)
18469 goto out;
18470
18471 /* Check the region signature first */
18472 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18474 "2619 Config region 23 has bad signature\n");
18475 goto out;
18476 }
18477 offset += 4;
18478
18479 /* Check the data structure version */
18480 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18482 "2620 Config region 23 has bad version\n");
18483 goto out;
18484 }
18485 offset += 4;
18486
18487 /* Parse TLV entries in the region */
18488 while (offset < data_size) {
18489 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18490 break;
18491 /*
18492 * If the TLV is not driver specific TLV or driver id is
18493 * not linux driver id, skip the record.
18494 */
18495 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18496 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18497 (rgn23_data[offset + 3] != 0)) {
18498 offset += rgn23_data[offset + 1] * 4 + 4;
18499 continue;
18500 }
18501
18502 /* Driver found a driver specific TLV in the config region */
18503 sub_tlv_len = rgn23_data[offset + 1] * 4;
18504 offset += 4;
18505 tlv_offset = 0;
18506
18507 /*
18508 * Search for configured port state sub-TLV.
18509 */
18510 while ((offset < data_size) &&
18511 (tlv_offset < sub_tlv_len)) {
18512 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18513 offset += 4;
18514 tlv_offset += 4;
18515 break;
18516 }
18517 if (rgn23_data[offset] != PORT_STE_TYPE) {
18518 offset += rgn23_data[offset + 1] * 4 + 4;
18519 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18520 continue;
18521 }
18522
18523 /* This HBA contains PORT_STE configured */
18524 if (!rgn23_data[offset + 2])
18525 phba->hba_flag |= LINK_DISABLED;
18526
18527 goto out;
18528 }
18529 }
026abb87 18530
a0c87cbd 18531out:
a0c87cbd
JS
18532 kfree(rgn23_data);
18533 return;
18534}
695a814e 18535
52d52440
JS
18536/**
18537 * lpfc_wr_object - write an object to the firmware
18538 * @phba: HBA structure that indicates port to create a queue on.
18539 * @dmabuf_list: list of dmabufs to write to the port.
18540 * @size: the total byte value of the objects to write to the port.
18541 * @offset: the current offset to be used to start the transfer.
18542 *
18543 * This routine will create a wr_object mailbox command to send to the port.
18544 * the mailbox command will be constructed using the dma buffers described in
18545 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18546 * BDEs that the imbedded mailbox can support. The @offset variable will be
18547 * used to indicate the starting offset of the transfer and will also return
18548 * the offset after the write object mailbox has completed. @size is used to
18549 * determine the end of the object and whether the eof bit should be set.
18550 *
18551 * Return 0 is successful and offset will contain the the new offset to use
18552 * for the next write.
18553 * Return negative value for error cases.
18554 **/
18555int
18556lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18557 uint32_t size, uint32_t *offset)
18558{
18559 struct lpfc_mbx_wr_object *wr_object;
18560 LPFC_MBOXQ_t *mbox;
18561 int rc = 0, i = 0;
18562 uint32_t shdr_status, shdr_add_status;
18563 uint32_t mbox_tmo;
18564 union lpfc_sli4_cfg_shdr *shdr;
18565 struct lpfc_dmabuf *dmabuf;
18566 uint32_t written = 0;
18567
18568 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18569 if (!mbox)
18570 return -ENOMEM;
18571
18572 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18573 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18574 sizeof(struct lpfc_mbx_wr_object) -
18575 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18576
18577 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18578 wr_object->u.request.write_offset = *offset;
18579 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18580 wr_object->u.request.object_name[0] =
18581 cpu_to_le32(wr_object->u.request.object_name[0]);
18582 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18583 list_for_each_entry(dmabuf, dmabuf_list, list) {
18584 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18585 break;
18586 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18587 wr_object->u.request.bde[i].addrHigh =
18588 putPaddrHigh(dmabuf->phys);
18589 if (written + SLI4_PAGE_SIZE >= size) {
18590 wr_object->u.request.bde[i].tus.f.bdeSize =
18591 (size - written);
18592 written += (size - written);
18593 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18594 } else {
18595 wr_object->u.request.bde[i].tus.f.bdeSize =
18596 SLI4_PAGE_SIZE;
18597 written += SLI4_PAGE_SIZE;
18598 }
18599 i++;
18600 }
18601 wr_object->u.request.bde_count = i;
18602 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18603 if (!phba->sli4_hba.intr_enable)
18604 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18605 else {
a183a15f 18606 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18607 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18608 }
18609 /* The IOCTL status is embedded in the mailbox subheader. */
18610 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18611 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18612 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18613 if (rc != MBX_TIMEOUT)
18614 mempool_free(mbox, phba->mbox_mem_pool);
18615 if (shdr_status || shdr_add_status || rc) {
18616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18617 "3025 Write Object mailbox failed with "
18618 "status x%x add_status x%x, mbx status x%x\n",
18619 shdr_status, shdr_add_status, rc);
18620 rc = -ENXIO;
18621 } else
18622 *offset += wr_object->u.response.actual_write_length;
18623 return rc;
18624}
18625
695a814e
JS
18626/**
18627 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18628 * @vport: pointer to vport data structure.
18629 *
18630 * This function iterate through the mailboxq and clean up all REG_LOGIN
18631 * and REG_VPI mailbox commands associated with the vport. This function
18632 * is called when driver want to restart discovery of the vport due to
18633 * a Clear Virtual Link event.
18634 **/
18635void
18636lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18637{
18638 struct lpfc_hba *phba = vport->phba;
18639 LPFC_MBOXQ_t *mb, *nextmb;
18640 struct lpfc_dmabuf *mp;
78730cfe 18641 struct lpfc_nodelist *ndlp;
d439d286 18642 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18643 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18644 LIST_HEAD(mbox_cmd_list);
63e801ce 18645 uint8_t restart_loop;
695a814e 18646
d439d286 18647 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18648 spin_lock_irq(&phba->hbalock);
18649 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18650 if (mb->vport != vport)
18651 continue;
18652
18653 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18654 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18655 continue;
18656
d439d286
JS
18657 list_del(&mb->list);
18658 list_add_tail(&mb->list, &mbox_cmd_list);
18659 }
18660 /* Clean up active mailbox command with the vport */
18661 mb = phba->sli.mbox_active;
18662 if (mb && (mb->vport == vport)) {
18663 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18664 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18665 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18666 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18667 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18668 /* Put reference count for delayed processing */
18669 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18670 /* Unregister the RPI when mailbox complete */
18671 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18672 }
18673 }
63e801ce
JS
18674 /* Cleanup any mailbox completions which are not yet processed */
18675 do {
18676 restart_loop = 0;
18677 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18678 /*
18679 * If this mailox is already processed or it is
18680 * for another vport ignore it.
18681 */
18682 if ((mb->vport != vport) ||
18683 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18684 continue;
18685
18686 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18687 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18688 continue;
18689
18690 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18691 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18692 ndlp = (struct lpfc_nodelist *)mb->context2;
18693 /* Unregister the RPI when mailbox complete */
18694 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18695 restart_loop = 1;
18696 spin_unlock_irq(&phba->hbalock);
18697 spin_lock(shost->host_lock);
18698 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18699 spin_unlock(shost->host_lock);
18700 spin_lock_irq(&phba->hbalock);
18701 break;
18702 }
18703 }
18704 } while (restart_loop);
18705
d439d286
JS
18706 spin_unlock_irq(&phba->hbalock);
18707
18708 /* Release the cleaned-up mailbox commands */
18709 while (!list_empty(&mbox_cmd_list)) {
18710 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18711 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18712 mp = (struct lpfc_dmabuf *) (mb->context1);
18713 if (mp) {
18714 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18715 kfree(mp);
18716 }
78730cfe 18717 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18718 mb->context2 = NULL;
78730cfe 18719 if (ndlp) {
ec21b3b0 18720 spin_lock(shost->host_lock);
589a52d6 18721 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18722 spin_unlock(shost->host_lock);
78730cfe 18723 lpfc_nlp_put(ndlp);
78730cfe 18724 }
695a814e 18725 }
695a814e
JS
18726 mempool_free(mb, phba->mbox_mem_pool);
18727 }
d439d286
JS
18728
18729 /* Release the ndlp with the cleaned-up active mailbox command */
18730 if (act_mbx_ndlp) {
18731 spin_lock(shost->host_lock);
18732 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18733 spin_unlock(shost->host_lock);
18734 lpfc_nlp_put(act_mbx_ndlp);
695a814e 18735 }
695a814e
JS
18736}
18737
2a9bf3d0
JS
18738/**
18739 * lpfc_drain_txq - Drain the txq
18740 * @phba: Pointer to HBA context object.
18741 *
18742 * This function attempt to submit IOCBs on the txq
18743 * to the adapter. For SLI4 adapters, the txq contains
18744 * ELS IOCBs that have been deferred because the there
18745 * are no SGLs. This congestion can occur with large
18746 * vport counts during node discovery.
18747 **/
18748
18749uint32_t
18750lpfc_drain_txq(struct lpfc_hba *phba)
18751{
18752 LIST_HEAD(completions);
895427bd 18753 struct lpfc_sli_ring *pring;
2e706377 18754 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
18755 unsigned long iflags = 0;
18756 char *fail_msg = NULL;
18757 struct lpfc_sglq *sglq;
2f07784f
JS
18758 union lpfc_wqe128 wqe128;
18759 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 18760 uint32_t txq_cnt = 0;
2a9bf3d0 18761
895427bd 18762 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
18763 if (unlikely(!pring))
18764 return 0;
895427bd 18765
398d81c9 18766 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
18767 list_for_each_entry(piocbq, &pring->txq, list) {
18768 txq_cnt++;
18769 }
18770
18771 if (txq_cnt > pring->txq_max)
18772 pring->txq_max = txq_cnt;
2a9bf3d0 18773
398d81c9 18774 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18775
0e9bb8d7 18776 while (!list_empty(&pring->txq)) {
398d81c9 18777 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 18778
19ca7609 18779 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 18780 if (!piocbq) {
398d81c9 18781 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
18782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18783 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 18784 txq_cnt);
a629852a
JS
18785 break;
18786 }
895427bd 18787 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 18788 if (!sglq) {
19ca7609 18789 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 18790 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18791 break;
2a9bf3d0 18792 }
0e9bb8d7 18793 txq_cnt--;
2a9bf3d0
JS
18794
18795 /* The xri and iocb resources secured,
18796 * attempt to issue request
18797 */
6d368e53 18798 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
18799 piocbq->sli4_xritag = sglq->sli4_xritag;
18800 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18801 fail_msg = "to convert bpl to sgl";
2f07784f 18802 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 18803 fail_msg = "to convert iocb to wqe";
2f07784f 18804 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
18805 fail_msg = " - Wq is full";
18806 else
18807 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18808
18809 if (fail_msg) {
18810 /* Failed means we can't issue and need to cancel */
18811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18812 "2822 IOCB failed %s iotag 0x%x "
18813 "xri 0x%x\n",
18814 fail_msg,
18815 piocbq->iotag, piocbq->sli4_xritag);
18816 list_add_tail(&piocbq->list, &completions);
18817 }
398d81c9 18818 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
18819 }
18820
2a9bf3d0
JS
18821 /* Cancel all the IOCBs that cannot be issued */
18822 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18823 IOERR_SLI_ABORTED);
18824
0e9bb8d7 18825 return txq_cnt;
2a9bf3d0 18826}
895427bd
JS
18827
18828/**
18829 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18830 * @phba: Pointer to HBA context object.
18831 * @pwqe: Pointer to command WQE.
18832 * @sglq: Pointer to the scatter gather queue object.
18833 *
18834 * This routine converts the bpl or bde that is in the WQE
18835 * to a sgl list for the sli4 hardware. The physical address
18836 * of the bpl/bde is converted back to a virtual address.
18837 * If the WQE contains a BPL then the list of BDE's is
18838 * converted to sli4_sge's. If the WQE contains a single
18839 * BDE then it is converted to a single sli_sge.
18840 * The WQE is still in cpu endianness so the contents of
18841 * the bpl can be used without byte swapping.
18842 *
18843 * Returns valid XRI = Success, NO_XRI = Failure.
18844 */
18845static uint16_t
18846lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18847 struct lpfc_sglq *sglq)
18848{
18849 uint16_t xritag = NO_XRI;
18850 struct ulp_bde64 *bpl = NULL;
18851 struct ulp_bde64 bde;
18852 struct sli4_sge *sgl = NULL;
18853 struct lpfc_dmabuf *dmabuf;
18854 union lpfc_wqe *wqe;
18855 int numBdes = 0;
18856 int i = 0;
18857 uint32_t offset = 0; /* accumulated offset in the sg request list */
18858 int inbound = 0; /* number of sg reply entries inbound from firmware */
18859 uint32_t cmd;
18860
18861 if (!pwqeq || !sglq)
18862 return xritag;
18863
18864 sgl = (struct sli4_sge *)sglq->sgl;
18865 wqe = &pwqeq->wqe;
18866 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18867
18868 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18869 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18870 return sglq->sli4_xritag;
18871 numBdes = pwqeq->rsvd2;
18872 if (numBdes) {
18873 /* The addrHigh and addrLow fields within the WQE
18874 * have not been byteswapped yet so there is no
18875 * need to swap them back.
18876 */
18877 if (pwqeq->context3)
18878 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18879 else
18880 return xritag;
18881
18882 bpl = (struct ulp_bde64 *)dmabuf->virt;
18883 if (!bpl)
18884 return xritag;
18885
18886 for (i = 0; i < numBdes; i++) {
18887 /* Should already be byte swapped. */
18888 sgl->addr_hi = bpl->addrHigh;
18889 sgl->addr_lo = bpl->addrLow;
18890
18891 sgl->word2 = le32_to_cpu(sgl->word2);
18892 if ((i+1) == numBdes)
18893 bf_set(lpfc_sli4_sge_last, sgl, 1);
18894 else
18895 bf_set(lpfc_sli4_sge_last, sgl, 0);
18896 /* swap the size field back to the cpu so we
18897 * can assign it to the sgl.
18898 */
18899 bde.tus.w = le32_to_cpu(bpl->tus.w);
18900 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18901 /* The offsets in the sgl need to be accumulated
18902 * separately for the request and reply lists.
18903 * The request is always first, the reply follows.
18904 */
18905 switch (cmd) {
18906 case CMD_GEN_REQUEST64_WQE:
18907 /* add up the reply sg entries */
18908 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18909 inbound++;
18910 /* first inbound? reset the offset */
18911 if (inbound == 1)
18912 offset = 0;
18913 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18914 bf_set(lpfc_sli4_sge_type, sgl,
18915 LPFC_SGE_TYPE_DATA);
18916 offset += bde.tus.f.bdeSize;
18917 break;
18918 case CMD_FCP_TRSP64_WQE:
18919 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18920 bf_set(lpfc_sli4_sge_type, sgl,
18921 LPFC_SGE_TYPE_DATA);
18922 break;
18923 case CMD_FCP_TSEND64_WQE:
18924 case CMD_FCP_TRECEIVE64_WQE:
18925 bf_set(lpfc_sli4_sge_type, sgl,
18926 bpl->tus.f.bdeFlags);
18927 if (i < 3)
18928 offset = 0;
18929 else
18930 offset += bde.tus.f.bdeSize;
18931 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18932 break;
18933 }
18934 sgl->word2 = cpu_to_le32(sgl->word2);
18935 bpl++;
18936 sgl++;
18937 }
18938 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18939 /* The addrHigh and addrLow fields of the BDE have not
18940 * been byteswapped yet so they need to be swapped
18941 * before putting them in the sgl.
18942 */
18943 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18944 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18945 sgl->word2 = le32_to_cpu(sgl->word2);
18946 bf_set(lpfc_sli4_sge_last, sgl, 1);
18947 sgl->word2 = cpu_to_le32(sgl->word2);
18948 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18949 }
18950 return sglq->sli4_xritag;
18951}
18952
18953/**
18954 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18955 * @phba: Pointer to HBA context object.
18956 * @ring_number: Base sli ring number
18957 * @pwqe: Pointer to command WQE.
18958 **/
18959int
18960lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18961 struct lpfc_iocbq *pwqe)
18962{
18963 union lpfc_wqe *wqe = &pwqe->wqe;
f358dd0c 18964 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
18965 struct lpfc_queue *wq;
18966 struct lpfc_sglq *sglq;
18967 struct lpfc_sli_ring *pring;
18968 unsigned long iflags;
cd22d605 18969 uint32_t ret = 0;
895427bd
JS
18970
18971 /* NVME_LS and NVME_LS ABTS requests. */
18972 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18973 pring = phba->sli4_hba.nvmels_wq->pring;
18974 spin_lock_irqsave(&pring->ring_lock, iflags);
18975 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18976 if (!sglq) {
18977 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18978 return WQE_BUSY;
18979 }
18980 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18981 pwqe->sli4_xritag = sglq->sli4_xritag;
18982 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18983 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18984 return WQE_ERROR;
18985 }
18986 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18987 pwqe->sli4_xritag);
cd22d605
DK
18988 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
18989 if (ret) {
895427bd 18990 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 18991 return ret;
895427bd 18992 }
cd22d605 18993
895427bd
JS
18994 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18995 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18996 return 0;
18997 }
18998
18999 /* NVME_FCREQ and NVME_ABTS requests */
19000 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19001 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19002 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19003
19004 spin_lock_irqsave(&pring->ring_lock, iflags);
19005 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19006 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19007 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19008 ret = lpfc_sli4_wq_put(wq, wqe);
19009 if (ret) {
895427bd 19010 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19011 return ret;
895427bd
JS
19012 }
19013 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19014 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19015 return 0;
19016 }
19017
f358dd0c
JS
19018 /* NVMET requests */
19019 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19020 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19021 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19022
19023 spin_lock_irqsave(&pring->ring_lock, iflags);
19024 ctxp = pwqe->context2;
6c621a22 19025 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19026 if (pwqe->sli4_xritag == NO_XRI) {
19027 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19028 pwqe->sli4_xritag = sglq->sli4_xritag;
19029 }
19030 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19031 pwqe->sli4_xritag);
19032 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19033 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19034 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19035 ret = lpfc_sli4_wq_put(wq, wqe);
19036 if (ret) {
f358dd0c 19037 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19038 return ret;
f358dd0c
JS
19039 }
19040 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19041 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19042 return 0;
19043 }
895427bd
JS
19044 return WQE_ERROR;
19045}