]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix IO failure during hba reset testing with nvme io.
[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));
6e8e1c14
JS
13235 if (childwq->q_flag & HBA_NVMET_WQFULL)
13236 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13237 wqid_matched = true;
13238 break;
13239 }
13240 }
13241 /* Report warning log message if no match found */
13242 if (wqid_matched != true)
13243 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13244 "2580 Fast-path wqe consume event carries "
895427bd 13245 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13246}
13247
13248/**
2d7dbc4c
JS
13249 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13250 * @phba: Pointer to HBA context object.
13251 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13252 *
2d7dbc4c
JS
13253 * This routine process a receive-queue completion queue entry.
13254 *
13255 * Return: true if work posted to worker thread, otherwise false.
13256 **/
13257static bool
13258lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13259 struct lpfc_rcqe *rcqe)
13260{
13261 bool workposted = false;
13262 struct lpfc_queue *hrq;
13263 struct lpfc_queue *drq;
13264 struct rqb_dmabuf *dma_buf;
13265 struct fc_frame_header *fc_hdr;
547077a4 13266 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13267 uint32_t status, rq_id;
13268 unsigned long iflags;
13269 uint32_t fctl, idx;
13270
13271 if ((phba->nvmet_support == 0) ||
13272 (phba->sli4_hba.nvmet_cqset == NULL))
13273 return workposted;
13274
13275 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13276 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13277 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13278
13279 /* sanity check on queue memory */
13280 if (unlikely(!hrq) || unlikely(!drq))
13281 return workposted;
13282
13283 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13284 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13285 else
13286 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13287
13288 if ((phba->nvmet_support == 0) ||
13289 (rq_id != hrq->queue_id))
13290 return workposted;
13291
13292 status = bf_get(lpfc_rcqe_status, rcqe);
13293 switch (status) {
13294 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13296 "6126 Receive Frame Truncated!!\n");
78e1d200 13297 /* Drop thru */
2d7dbc4c 13298 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13299 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13300 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13301 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13302 if (!dma_buf) {
13303 hrq->RQ_no_buf_found++;
13304 spin_unlock_irqrestore(&phba->hbalock, iflags);
13305 goto out;
13306 }
13307 spin_unlock_irqrestore(&phba->hbalock, iflags);
13308 hrq->RQ_rcv_buf++;
547077a4 13309 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13310 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13311
13312 /* Just some basic sanity checks on FCP Command frame */
13313 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13314 fc_hdr->fh_f_ctl[1] << 8 |
13315 fc_hdr->fh_f_ctl[2]);
13316 if (((fctl &
13317 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13318 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13319 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13320 goto drop;
13321
13322 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13323 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13324 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13325 phba, idx, dma_buf,
c8a4ce0b 13326 cq->isr_timestamp);
2d7dbc4c
JS
13327 return false;
13328 }
13329drop:
13330 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13331 break;
2d7dbc4c 13332 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13333 if (phba->nvmet_support) {
13334 tgtp = phba->targetport->private;
13335 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13336 "6401 RQE Error x%x, posted %d err_cnt "
13337 "%d: %x %x %x\n",
13338 status, hrq->RQ_buf_posted,
13339 hrq->RQ_no_posted_buf,
13340 atomic_read(&tgtp->rcv_fcp_cmd_in),
13341 atomic_read(&tgtp->rcv_fcp_cmd_out),
13342 atomic_read(&tgtp->xmt_fcp_release));
13343 }
13344 /* fallthrough */
13345
13346 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13347 hrq->RQ_no_posted_buf++;
13348 /* Post more buffers if possible */
2d7dbc4c
JS
13349 break;
13350 }
13351out:
13352 return workposted;
13353}
13354
4f774513 13355/**
895427bd 13356 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13357 * @cq: Pointer to the completion queue.
13358 * @eqe: Pointer to fast-path completion queue entry.
13359 *
13360 * This routine process a fast-path work queue completion entry from fast-path
13361 * event queue for FCP command response completion.
13362 **/
13363static int
895427bd 13364lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13365 struct lpfc_cqe *cqe)
13366{
13367 struct lpfc_wcqe_release wcqe;
13368 bool workposted = false;
13369
13370 /* Copy the work queue CQE and convert endian order if needed */
13371 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13372
13373 /* Check and process for different type of WCQE and dispatch */
13374 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13375 case CQE_CODE_COMPL_WQE:
895427bd 13376 case CQE_CODE_NVME_ERSP:
b84daac9 13377 cq->CQ_wq++;
4f774513 13378 /* Process the WQ complete event */
98fc5dd9 13379 phba->last_completion_time = jiffies;
895427bd
JS
13380 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13381 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13382 (struct lpfc_wcqe_complete *)&wcqe);
13383 if (cq->subtype == LPFC_NVME_LS)
13384 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13385 (struct lpfc_wcqe_complete *)&wcqe);
13386 break;
13387 case CQE_CODE_RELEASE_WQE:
b84daac9 13388 cq->CQ_release_wqe++;
4f774513
JS
13389 /* Process the WQ release event */
13390 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13391 (struct lpfc_wcqe_release *)&wcqe);
13392 break;
13393 case CQE_CODE_XRI_ABORTED:
b84daac9 13394 cq->CQ_xri_aborted++;
4f774513 13395 /* Process the WQ XRI abort event */
bc73905a 13396 phba->last_completion_time = jiffies;
4f774513
JS
13397 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13398 (struct sli4_wcqe_xri_aborted *)&wcqe);
13399 break;
895427bd
JS
13400 case CQE_CODE_RECEIVE_V1:
13401 case CQE_CODE_RECEIVE:
13402 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13403 if (cq->subtype == LPFC_NVMET) {
13404 workposted = lpfc_sli4_nvmet_handle_rcqe(
13405 phba, cq, (struct lpfc_rcqe *)&wcqe);
13406 }
895427bd 13407 break;
4f774513
JS
13408 default:
13409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13410 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13411 bf_get(lpfc_wcqe_c_code, &wcqe));
13412 break;
13413 }
13414 return workposted;
13415}
13416
13417/**
67d12733 13418 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13419 * @phba: Pointer to HBA context object.
13420 * @eqe: Pointer to fast-path event queue entry.
13421 *
13422 * This routine process a event queue entry from the fast-path event queue.
13423 * It will check the MajorCode and MinorCode to determine this is for a
13424 * completion event on a completion queue, if not, an error shall be logged
13425 * and just return. Otherwise, it will get to the corresponding completion
13426 * queue and process all the entries on the completion queue, rearm the
13427 * completion queue, and then return.
13428 **/
f485c18d 13429static void
67d12733
JS
13430lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13431 uint32_t qidx)
4f774513 13432{
895427bd 13433 struct lpfc_queue *cq = NULL;
2d7dbc4c 13434 uint16_t cqid, id;
4f774513 13435
cb5172ea 13436 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13437 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13438 "0366 Not a valid completion "
4f774513 13439 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13440 bf_get_le32(lpfc_eqe_major_code, eqe),
13441 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13442 return;
4f774513
JS
13443 }
13444
67d12733
JS
13445 /* Get the reference to the corresponding CQ */
13446 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13447
2d7dbc4c
JS
13448 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13449 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13450 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13451 /* Process NVMET unsol rcv */
13452 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13453 goto process_cq;
13454 }
67d12733
JS
13455 }
13456
895427bd
JS
13457 if (phba->sli4_hba.nvme_cq_map &&
13458 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13459 /* Process NVME / NVMET command completion */
895427bd
JS
13460 cq = phba->sli4_hba.nvme_cq[qidx];
13461 goto process_cq;
2e90f4b5 13462 }
67d12733 13463
895427bd
JS
13464 if (phba->sli4_hba.fcp_cq_map &&
13465 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13466 /* Process FCP command completion */
13467 cq = phba->sli4_hba.fcp_cq[qidx];
13468 goto process_cq;
2e90f4b5 13469 }
895427bd
JS
13470
13471 if (phba->sli4_hba.nvmels_cq &&
13472 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13473 /* Process NVME unsol rcv */
13474 cq = phba->sli4_hba.nvmels_cq;
13475 }
13476
13477 /* Otherwise this is a Slow path event */
13478 if (cq == NULL) {
f485c18d
DK
13479 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13480 return;
4f774513
JS
13481 }
13482
895427bd 13483process_cq:
4f774513
JS
13484 if (unlikely(cqid != cq->queue_id)) {
13485 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13486 "0368 Miss-matched fast-path completion "
13487 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13488 cqid, cq->queue_id);
f485c18d 13489 return;
4f774513
JS
13490 }
13491
895427bd
JS
13492 /* Save EQ associated with this CQ */
13493 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13494
f485c18d
DK
13495 if (!queue_work(phba->wq, &cq->irqwork))
13496 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13497 "0363 Cannot schedule soft IRQ "
13498 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13499 cqid, cq->queue_id, smp_processor_id());
13500}
13501
13502/**
13503 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13504 * @phba: Pointer to HBA context object.
13505 * @eqe: Pointer to fast-path event queue entry.
13506 *
13507 * This routine process a event queue entry from the fast-path event queue.
13508 * It will check the MajorCode and MinorCode to determine this is for a
13509 * completion event on a completion queue, if not, an error shall be logged
13510 * and just return. Otherwise, it will get to the corresponding completion
13511 * queue and process all the entries on the completion queue, rearm the
13512 * completion queue, and then return.
13513 **/
13514static void
13515lpfc_sli4_hba_process_cq(struct work_struct *work)
13516{
13517 struct lpfc_queue *cq =
13518 container_of(work, struct lpfc_queue, irqwork);
13519 struct lpfc_hba *phba = cq->phba;
13520 struct lpfc_cqe *cqe;
13521 bool workposted = false;
13522 int ccount = 0;
13523
4f774513
JS
13524 /* Process all the entries to the CQ */
13525 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13526#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13527 if (phba->ktime_on)
13528 cq->isr_timestamp = ktime_get_ns();
13529 else
13530 cq->isr_timestamp = 0;
13531#endif
895427bd 13532 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13533 if (!(++ccount % cq->entry_repost))
7869da18 13534 break;
4f774513
JS
13535 }
13536
b84daac9 13537 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13538 if (ccount > cq->CQ_max_cqe)
13539 cq->CQ_max_cqe = ccount;
13540 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13541
4f774513 13542 /* Catch the no cq entry condition */
f485c18d 13543 if (unlikely(ccount == 0))
4f774513
JS
13544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13545 "0369 No entry from fast-path completion "
13546 "queue fcpcqid=%d\n", cq->queue_id);
13547
13548 /* In any case, flash and re-arm the CQ */
13549 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13550
13551 /* wake up worker thread if there are works to be done */
13552 if (workposted)
13553 lpfc_worker_wake_up(phba);
13554}
13555
13556static void
13557lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13558{
13559 struct lpfc_eqe *eqe;
13560
13561 /* walk all the EQ entries and drop on the floor */
13562 while ((eqe = lpfc_sli4_eq_get(eq)))
13563 ;
13564
13565 /* Clear and re-arm the EQ */
13566 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13567}
13568
1ba981fd
JS
13569
13570/**
13571 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13572 * entry
13573 * @phba: Pointer to HBA context object.
13574 * @eqe: Pointer to fast-path event queue entry.
13575 *
13576 * This routine process a event queue entry from the Flash Optimized Fabric
13577 * event queue. It will check the MajorCode and MinorCode to determine this
13578 * is for a completion event on a completion queue, if not, an error shall be
13579 * logged and just return. Otherwise, it will get to the corresponding
13580 * completion queue and process all the entries on the completion queue, rearm
13581 * the completion queue, and then return.
13582 **/
13583static void
13584lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13585{
13586 struct lpfc_queue *cq;
1ba981fd 13587 uint16_t cqid;
1ba981fd
JS
13588
13589 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13590 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13591 "9147 Not a valid completion "
13592 "event: majorcode=x%x, minorcode=x%x\n",
13593 bf_get_le32(lpfc_eqe_major_code, eqe),
13594 bf_get_le32(lpfc_eqe_minor_code, eqe));
13595 return;
13596 }
13597
13598 /* Get the reference to the corresponding CQ */
13599 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13600
13601 /* Next check for OAS */
13602 cq = phba->sli4_hba.oas_cq;
13603 if (unlikely(!cq)) {
13604 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13605 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13606 "9148 OAS completion queue "
13607 "does not exist\n");
13608 return;
13609 }
13610
13611 if (unlikely(cqid != cq->queue_id)) {
13612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13613 "9149 Miss-matched fast-path compl "
13614 "queue id: eqcqid=%d, fcpcqid=%d\n",
13615 cqid, cq->queue_id);
13616 return;
13617 }
13618
d41b65bc
JS
13619 /* Save EQ associated with this CQ */
13620 cq->assoc_qp = phba->sli4_hba.fof_eq;
13621
f485c18d
DK
13622 /* CQ work will be processed on CPU affinitized to this IRQ */
13623 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13624 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13625 "0367 Cannot schedule soft IRQ "
13626 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13627 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13628}
13629
13630/**
13631 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13632 * @irq: Interrupt number.
13633 * @dev_id: The device context pointer.
13634 *
13635 * This function is directly called from the PCI layer as an interrupt
13636 * service routine when device with SLI-4 interface spec is enabled with
13637 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13638 * IOCB ring event in the HBA. However, when the device is enabled with either
13639 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13640 * device-level interrupt handler. When the PCI slot is in error recovery
13641 * or the HBA is undergoing initialization, the interrupt handler will not
13642 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13643 * the intrrupt context. This function is called without any lock held.
13644 * It gets the hbalock to access and update SLI data structures. Note that,
13645 * the EQ to CQ are one-to-one map such that the EQ index is
13646 * equal to that of CQ index.
13647 *
13648 * This function returns IRQ_HANDLED when interrupt is handled else it
13649 * returns IRQ_NONE.
13650 **/
13651irqreturn_t
13652lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13653{
13654 struct lpfc_hba *phba;
895427bd 13655 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13656 struct lpfc_queue *eq;
13657 struct lpfc_eqe *eqe;
13658 unsigned long iflag;
13659 int ecount = 0;
1ba981fd
JS
13660
13661 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13662 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13663 phba = hba_eq_hdl->phba;
1ba981fd
JS
13664
13665 if (unlikely(!phba))
13666 return IRQ_NONE;
13667
13668 /* Get to the EQ struct associated with this vector */
13669 eq = phba->sli4_hba.fof_eq;
13670 if (unlikely(!eq))
13671 return IRQ_NONE;
13672
13673 /* Check device state for handling interrupt */
13674 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13675 /* Check again for link_state with lock held */
13676 spin_lock_irqsave(&phba->hbalock, iflag);
13677 if (phba->link_state < LPFC_LINK_DOWN)
13678 /* Flush, clear interrupt, and rearm the EQ */
13679 lpfc_sli4_eq_flush(phba, eq);
13680 spin_unlock_irqrestore(&phba->hbalock, iflag);
13681 return IRQ_NONE;
13682 }
13683
13684 /*
13685 * Process all the event on FCP fast-path EQ
13686 */
13687 while ((eqe = lpfc_sli4_eq_get(eq))) {
13688 lpfc_sli4_fof_handle_eqe(phba, eqe);
13689 if (!(++ecount % eq->entry_repost))
7869da18 13690 break;
1ba981fd
JS
13691 eq->EQ_processed++;
13692 }
13693
13694 /* Track the max number of EQEs processed in 1 intr */
13695 if (ecount > eq->EQ_max_eqe)
13696 eq->EQ_max_eqe = ecount;
13697
13698
13699 if (unlikely(ecount == 0)) {
13700 eq->EQ_no_entry++;
13701
13702 if (phba->intr_type == MSIX)
13703 /* MSI-X treated interrupt served as no EQ share INT */
13704 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13705 "9145 MSI-X interrupt with no EQE\n");
13706 else {
13707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13708 "9146 ISR interrupt with no EQE\n");
13709 /* Non MSI-X treated on interrupt as EQ share INT */
13710 return IRQ_NONE;
13711 }
13712 }
13713 /* Always clear and re-arm the fast-path EQ */
13714 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13715 return IRQ_HANDLED;
13716}
13717
4f774513 13718/**
67d12733 13719 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13720 * @irq: Interrupt number.
13721 * @dev_id: The device context pointer.
13722 *
13723 * This function is directly called from the PCI layer as an interrupt
13724 * service routine when device with SLI-4 interface spec is enabled with
13725 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13726 * ring event in the HBA. However, when the device is enabled with either
13727 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13728 * device-level interrupt handler. When the PCI slot is in error recovery
13729 * or the HBA is undergoing initialization, the interrupt handler will not
13730 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13731 * the intrrupt context. This function is called without any lock held.
13732 * It gets the hbalock to access and update SLI data structures. Note that,
13733 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13734 * equal to that of FCP CQ index.
13735 *
67d12733
JS
13736 * The link attention and ELS ring attention events are handled
13737 * by the worker thread. The interrupt handler signals the worker thread
13738 * and returns for these events. This function is called without any lock
13739 * held. It gets the hbalock to access and update SLI data structures.
13740 *
4f774513
JS
13741 * This function returns IRQ_HANDLED when interrupt is handled else it
13742 * returns IRQ_NONE.
13743 **/
13744irqreturn_t
67d12733 13745lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13746{
13747 struct lpfc_hba *phba;
895427bd 13748 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13749 struct lpfc_queue *fpeq;
13750 struct lpfc_eqe *eqe;
13751 unsigned long iflag;
13752 int ecount = 0;
895427bd 13753 int hba_eqidx;
4f774513
JS
13754
13755 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13756 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13757 phba = hba_eq_hdl->phba;
13758 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13759
13760 if (unlikely(!phba))
13761 return IRQ_NONE;
67d12733 13762 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13763 return IRQ_NONE;
4f774513
JS
13764
13765 /* Get to the EQ struct associated with this vector */
895427bd 13766 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13767 if (unlikely(!fpeq))
13768 return IRQ_NONE;
4f774513 13769
ba20c853 13770 if (lpfc_fcp_look_ahead) {
895427bd 13771 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
ba20c853
JS
13772 lpfc_sli4_eq_clr_intr(fpeq);
13773 else {
895427bd 13774 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13775 return IRQ_NONE;
13776 }
13777 }
13778
4f774513
JS
13779 /* Check device state for handling interrupt */
13780 if (unlikely(lpfc_intr_state_check(phba))) {
13781 /* Check again for link_state with lock held */
13782 spin_lock_irqsave(&phba->hbalock, iflag);
13783 if (phba->link_state < LPFC_LINK_DOWN)
13784 /* Flush, clear interrupt, and rearm the EQ */
13785 lpfc_sli4_eq_flush(phba, fpeq);
13786 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13787 if (lpfc_fcp_look_ahead)
895427bd 13788 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
13789 return IRQ_NONE;
13790 }
13791
13792 /*
13793 * Process all the event on FCP fast-path EQ
13794 */
13795 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
13796 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13797 if (!(++ecount % fpeq->entry_repost))
7869da18 13798 break;
b84daac9 13799 fpeq->EQ_processed++;
4f774513
JS
13800 }
13801
b84daac9
JS
13802 /* Track the max number of EQEs processed in 1 intr */
13803 if (ecount > fpeq->EQ_max_eqe)
13804 fpeq->EQ_max_eqe = ecount;
13805
4f774513
JS
13806 /* Always clear and re-arm the fast-path EQ */
13807 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13808
13809 if (unlikely(ecount == 0)) {
b84daac9 13810 fpeq->EQ_no_entry++;
ba20c853
JS
13811
13812 if (lpfc_fcp_look_ahead) {
895427bd 13813 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13814 return IRQ_NONE;
13815 }
13816
4f774513
JS
13817 if (phba->intr_type == MSIX)
13818 /* MSI-X treated interrupt served as no EQ share INT */
13819 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13820 "0358 MSI-X interrupt with no EQE\n");
13821 else
13822 /* Non MSI-X treated on interrupt as EQ share INT */
13823 return IRQ_NONE;
13824 }
13825
ba20c853 13826 if (lpfc_fcp_look_ahead)
895427bd
JS
13827 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13828
4f774513
JS
13829 return IRQ_HANDLED;
13830} /* lpfc_sli4_fp_intr_handler */
13831
13832/**
13833 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13834 * @irq: Interrupt number.
13835 * @dev_id: The device context pointer.
13836 *
13837 * This function is the device-level interrupt handler to device with SLI-4
13838 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13839 * interrupt mode is enabled and there is an event in the HBA which requires
13840 * driver attention. This function invokes the slow-path interrupt attention
13841 * handling function and fast-path interrupt attention handling function in
13842 * turn to process the relevant HBA attention events. This function is called
13843 * without any lock held. It gets the hbalock to access and update SLI data
13844 * structures.
13845 *
13846 * This function returns IRQ_HANDLED when interrupt is handled, else it
13847 * returns IRQ_NONE.
13848 **/
13849irqreturn_t
13850lpfc_sli4_intr_handler(int irq, void *dev_id)
13851{
13852 struct lpfc_hba *phba;
67d12733
JS
13853 irqreturn_t hba_irq_rc;
13854 bool hba_handled = false;
895427bd 13855 int qidx;
4f774513
JS
13856
13857 /* Get the driver's phba structure from the dev_id */
13858 phba = (struct lpfc_hba *)dev_id;
13859
13860 if (unlikely(!phba))
13861 return IRQ_NONE;
13862
4f774513
JS
13863 /*
13864 * Invoke fast-path host attention interrupt handling as appropriate.
13865 */
895427bd 13866 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 13867 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 13868 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
13869 if (hba_irq_rc == IRQ_HANDLED)
13870 hba_handled |= true;
4f774513
JS
13871 }
13872
1ba981fd
JS
13873 if (phba->cfg_fof) {
13874 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 13875 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
13876 if (hba_irq_rc == IRQ_HANDLED)
13877 hba_handled |= true;
13878 }
13879
67d12733 13880 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
13881} /* lpfc_sli4_intr_handler */
13882
13883/**
13884 * lpfc_sli4_queue_free - free a queue structure and associated memory
13885 * @queue: The queue structure to free.
13886 *
b595076a 13887 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
13888 * the host resident queue. This function must be called after destroying the
13889 * queue on the HBA.
13890 **/
13891void
13892lpfc_sli4_queue_free(struct lpfc_queue *queue)
13893{
13894 struct lpfc_dmabuf *dmabuf;
13895
13896 if (!queue)
13897 return;
13898
13899 while (!list_empty(&queue->page_list)) {
13900 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13901 list);
81b96eda 13902 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
13903 dmabuf->virt, dmabuf->phys);
13904 kfree(dmabuf);
13905 }
895427bd
JS
13906 if (queue->rqbp) {
13907 lpfc_free_rq_buffer(queue->phba, queue);
13908 kfree(queue->rqbp);
13909 }
d1f525aa
JS
13910
13911 if (!list_empty(&queue->wq_list))
13912 list_del(&queue->wq_list);
13913
4f774513
JS
13914 kfree(queue);
13915 return;
13916}
13917
13918/**
13919 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13920 * @phba: The HBA that this queue is being created on.
81b96eda 13921 * @page_size: The size of a queue page
4f774513
JS
13922 * @entry_size: The size of each queue entry for this queue.
13923 * @entry count: The number of entries that this queue will handle.
13924 *
13925 * This function allocates a queue structure and the DMAable memory used for
13926 * the host resident queue. This function must be called before creating the
13927 * queue on the HBA.
13928 **/
13929struct lpfc_queue *
81b96eda
JS
13930lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13931 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
13932{
13933 struct lpfc_queue *queue;
13934 struct lpfc_dmabuf *dmabuf;
13935 int x, total_qe_count;
13936 void *dma_pointer;
cb5172ea 13937 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 13938
cb5172ea 13939 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 13940 hw_page_size = page_size;
cb5172ea 13941
4f774513
JS
13942 queue = kzalloc(sizeof(struct lpfc_queue) +
13943 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13944 if (!queue)
13945 return NULL;
cb5172ea
JS
13946 queue->page_count = (ALIGN(entry_size * entry_count,
13947 hw_page_size))/hw_page_size;
895427bd
JS
13948
13949 /* If needed, Adjust page count to match the max the adapter supports */
13950 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13951 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13952
4f774513 13953 INIT_LIST_HEAD(&queue->list);
895427bd 13954 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 13955 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
13956 INIT_LIST_HEAD(&queue->page_list);
13957 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
13958
13959 /* Set queue parameters now. If the system cannot provide memory
13960 * resources, the free routine needs to know what was allocated.
13961 */
13962 queue->entry_size = entry_size;
13963 queue->entry_count = entry_count;
13964 queue->page_size = hw_page_size;
13965 queue->phba = phba;
13966
4f774513
JS
13967 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13968 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13969 if (!dmabuf)
13970 goto out_fail;
1aee383d
JP
13971 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13972 hw_page_size, &dmabuf->phys,
13973 GFP_KERNEL);
4f774513
JS
13974 if (!dmabuf->virt) {
13975 kfree(dmabuf);
13976 goto out_fail;
13977 }
13978 dmabuf->buffer_tag = x;
13979 list_add_tail(&dmabuf->list, &queue->page_list);
13980 /* initialize queue's entry array */
13981 dma_pointer = dmabuf->virt;
13982 for (; total_qe_count < entry_count &&
cb5172ea 13983 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
13984 total_qe_count++, dma_pointer += entry_size) {
13985 queue->qe[total_qe_count].address = dma_pointer;
13986 }
13987 }
f485c18d
DK
13988 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
13989 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 13990
64eb4dcb
JS
13991 /* entry_repost will be set during q creation */
13992
4f774513
JS
13993 return queue;
13994out_fail:
13995 lpfc_sli4_queue_free(queue);
13996 return NULL;
13997}
13998
962bc51b
JS
13999/**
14000 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14001 * @phba: HBA structure that indicates port to create a queue on.
14002 * @pci_barset: PCI BAR set flag.
14003 *
14004 * This function shall perform iomap of the specified PCI BAR address to host
14005 * memory address if not already done so and return it. The returned host
14006 * memory address can be NULL.
14007 */
14008static void __iomem *
14009lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14010{
962bc51b
JS
14011 if (!phba->pcidev)
14012 return NULL;
962bc51b
JS
14013
14014 switch (pci_barset) {
14015 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14016 return phba->pci_bar0_memmap_p;
14017 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14018 return phba->pci_bar2_memmap_p;
14019 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14020 return phba->pci_bar4_memmap_p;
14021 default:
14022 break;
14023 }
14024 return NULL;
14025}
14026
173edbb2 14027/**
895427bd 14028 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14029 * @phba: HBA structure that indicates port to create a queue on.
14030 * @startq: The starting FCP EQ to modify
14031 *
14032 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14033 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14034 * updated in one mailbox command.
173edbb2
JS
14035 *
14036 * The @phba struct is used to send mailbox command to HBA. The @startq
14037 * is used to get the starting FCP EQ to change.
14038 * This function is asynchronous and will wait for the mailbox
14039 * command to finish before continuing.
14040 *
14041 * On success this function will return a zero. If unable to allocate enough
14042 * memory this function will return -ENOMEM. If the queue create mailbox command
14043 * fails this function will return -ENXIO.
14044 **/
a2fc4aef 14045int
0cf07f84
JS
14046lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14047 uint32_t numq, uint32_t imax)
173edbb2
JS
14048{
14049 struct lpfc_mbx_modify_eq_delay *eq_delay;
14050 LPFC_MBOXQ_t *mbox;
14051 struct lpfc_queue *eq;
14052 int cnt, rc, length, status = 0;
14053 uint32_t shdr_status, shdr_add_status;
0cf07f84 14054 uint32_t result, val;
895427bd 14055 int qidx;
173edbb2
JS
14056 union lpfc_sli4_cfg_shdr *shdr;
14057 uint16_t dmult;
14058
895427bd 14059 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14060 return 0;
14061
14062 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14063 if (!mbox)
14064 return -ENOMEM;
14065 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14066 sizeof(struct lpfc_sli4_cfg_mhdr));
14067 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14068 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14069 length, LPFC_SLI4_MBX_EMBED);
14070 eq_delay = &mbox->u.mqe.un.eq_delay;
14071
14072 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14073 result = imax / phba->io_channel_irqs;
895427bd 14074 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14075 dmult = 0;
14076 else
14077 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14078 if (dmult > LPFC_DMULT_MAX)
14079 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14080
14081 cnt = 0;
895427bd
JS
14082 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14083 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14084 if (!eq)
14085 continue;
0cf07f84 14086 eq->q_mode = imax;
173edbb2
JS
14087 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14088 eq_delay->u.request.eq[cnt].phase = 0;
14089 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14090 cnt++;
0cf07f84
JS
14091
14092 /* q_mode is only used for auto_imax */
14093 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14094 /* Use EQ Delay Register method for q_mode */
14095
14096 /* Convert for EQ Delay register */
14097 val = phba->cfg_fcp_imax;
14098 if (val) {
14099 /* First, interrupts per sec per EQ */
14100 val = phba->cfg_fcp_imax /
14101 phba->io_channel_irqs;
14102
14103 /* us delay between each interrupt */
14104 val = LPFC_SEC_TO_USEC / val;
14105 }
14106 eq->q_mode = val;
14107 } else {
14108 eq->q_mode = imax;
14109 }
14110
14111 if (cnt >= numq)
173edbb2
JS
14112 break;
14113 }
14114 eq_delay->u.request.num_eq = cnt;
14115
14116 mbox->vport = phba->pport;
14117 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14118 mbox->context1 = NULL;
14119 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14120 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14121 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14122 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14123 if (shdr_status || shdr_add_status || rc) {
14124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14125 "2512 MODIFY_EQ_DELAY mailbox failed with "
14126 "status x%x add_status x%x, mbx status x%x\n",
14127 shdr_status, shdr_add_status, rc);
14128 status = -ENXIO;
14129 }
14130 mempool_free(mbox, phba->mbox_mem_pool);
14131 return status;
14132}
14133
4f774513
JS
14134/**
14135 * lpfc_eq_create - Create an Event Queue on the HBA
14136 * @phba: HBA structure that indicates port to create a queue on.
14137 * @eq: The queue structure to use to create the event queue.
14138 * @imax: The maximum interrupt per second limit.
14139 *
14140 * This function creates an event queue, as detailed in @eq, on a port,
14141 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14142 *
14143 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14144 * is used to get the entry count and entry size that are necessary to
14145 * determine the number of pages to allocate and use for this queue. This
14146 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14147 * event queue. This function is asynchronous and will wait for the mailbox
14148 * command to finish before continuing.
14149 *
14150 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14151 * memory this function will return -ENOMEM. If the queue create mailbox command
14152 * fails this function will return -ENXIO.
4f774513 14153 **/
a2fc4aef 14154int
ee02006b 14155lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14156{
14157 struct lpfc_mbx_eq_create *eq_create;
14158 LPFC_MBOXQ_t *mbox;
14159 int rc, length, status = 0;
14160 struct lpfc_dmabuf *dmabuf;
14161 uint32_t shdr_status, shdr_add_status;
14162 union lpfc_sli4_cfg_shdr *shdr;
14163 uint16_t dmult;
49198b37
JS
14164 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14165
2e90f4b5
JS
14166 /* sanity check on queue memory */
14167 if (!eq)
14168 return -ENODEV;
49198b37
JS
14169 if (!phba->sli4_hba.pc_sli4_params.supported)
14170 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14171
14172 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14173 if (!mbox)
14174 return -ENOMEM;
14175 length = (sizeof(struct lpfc_mbx_eq_create) -
14176 sizeof(struct lpfc_sli4_cfg_mhdr));
14177 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14178 LPFC_MBOX_OPCODE_EQ_CREATE,
14179 length, LPFC_SLI4_MBX_EMBED);
14180 eq_create = &mbox->u.mqe.un.eq_create;
14181 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14182 eq->page_count);
14183 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14184 LPFC_EQE_SIZE);
14185 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
14186 /* don't setup delay multiplier using EQ_CREATE */
14187 dmult = 0;
4f774513
JS
14188 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14189 dmult);
14190 switch (eq->entry_count) {
14191 default:
14192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14193 "0360 Unsupported EQ count. (%d)\n",
14194 eq->entry_count);
14195 if (eq->entry_count < 256)
14196 return -EINVAL;
14197 /* otherwise default to smallest count (drop through) */
14198 case 256:
14199 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14200 LPFC_EQ_CNT_256);
14201 break;
14202 case 512:
14203 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14204 LPFC_EQ_CNT_512);
14205 break;
14206 case 1024:
14207 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14208 LPFC_EQ_CNT_1024);
14209 break;
14210 case 2048:
14211 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14212 LPFC_EQ_CNT_2048);
14213 break;
14214 case 4096:
14215 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14216 LPFC_EQ_CNT_4096);
14217 break;
14218 }
14219 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14220 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14221 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14222 putPaddrLow(dmabuf->phys);
14223 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14224 putPaddrHigh(dmabuf->phys);
14225 }
14226 mbox->vport = phba->pport;
14227 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14228 mbox->context1 = NULL;
14229 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14230 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14231 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14232 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14233 if (shdr_status || shdr_add_status || rc) {
14234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14235 "2500 EQ_CREATE mailbox failed with "
14236 "status x%x add_status x%x, mbx status x%x\n",
14237 shdr_status, shdr_add_status, rc);
14238 status = -ENXIO;
14239 }
14240 eq->type = LPFC_EQ;
14241 eq->subtype = LPFC_NONE;
14242 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14243 if (eq->queue_id == 0xFFFF)
14244 status = -ENXIO;
14245 eq->host_index = 0;
14246 eq->hba_index = 0;
64eb4dcb 14247 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14248
8fa38513 14249 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14250 return status;
14251}
14252
14253/**
14254 * lpfc_cq_create - Create a Completion Queue on the HBA
14255 * @phba: HBA structure that indicates port to create a queue on.
14256 * @cq: The queue structure to use to create the completion queue.
14257 * @eq: The event queue to bind this completion queue to.
14258 *
14259 * This function creates a completion queue, as detailed in @wq, on a port,
14260 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14261 *
14262 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14263 * is used to get the entry count and entry size that are necessary to
14264 * determine the number of pages to allocate and use for this queue. The @eq
14265 * is used to indicate which event queue to bind this completion queue to. This
14266 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14267 * completion queue. This function is asynchronous and will wait for the mailbox
14268 * command to finish before continuing.
14269 *
14270 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14271 * memory this function will return -ENOMEM. If the queue create mailbox command
14272 * fails this function will return -ENXIO.
4f774513 14273 **/
a2fc4aef 14274int
4f774513
JS
14275lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14276 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14277{
14278 struct lpfc_mbx_cq_create *cq_create;
14279 struct lpfc_dmabuf *dmabuf;
14280 LPFC_MBOXQ_t *mbox;
14281 int rc, length, status = 0;
14282 uint32_t shdr_status, shdr_add_status;
14283 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14284 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14285
2e90f4b5
JS
14286 /* sanity check on queue memory */
14287 if (!cq || !eq)
14288 return -ENODEV;
49198b37 14289 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14290 hw_page_size = cq->page_size;
49198b37 14291
4f774513
JS
14292 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14293 if (!mbox)
14294 return -ENOMEM;
14295 length = (sizeof(struct lpfc_mbx_cq_create) -
14296 sizeof(struct lpfc_sli4_cfg_mhdr));
14297 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14298 LPFC_MBOX_OPCODE_CQ_CREATE,
14299 length, LPFC_SLI4_MBX_EMBED);
14300 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14301 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14302 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14303 cq->page_count);
14304 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14305 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14306 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14307 phba->sli4_hba.pc_sli4_params.cqv);
14308 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14309 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14310 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14311 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14312 eq->queue_id);
14313 } else {
14314 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14315 eq->queue_id);
14316 }
4f774513 14317 switch (cq->entry_count) {
81b96eda
JS
14318 case 2048:
14319 case 4096:
14320 if (phba->sli4_hba.pc_sli4_params.cqv ==
14321 LPFC_Q_CREATE_VERSION_2) {
14322 cq_create->u.request.context.lpfc_cq_context_count =
14323 cq->entry_count;
14324 bf_set(lpfc_cq_context_count,
14325 &cq_create->u.request.context,
14326 LPFC_CQ_CNT_WORD7);
14327 break;
14328 }
14329 /* Fall Thru */
4f774513
JS
14330 default:
14331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14332 "0361 Unsupported CQ count: "
64eb4dcb 14333 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14334 cq->entry_count, cq->entry_size,
64eb4dcb 14335 cq->page_count);
4f4c1863
JS
14336 if (cq->entry_count < 256) {
14337 status = -EINVAL;
14338 goto out;
14339 }
4f774513
JS
14340 /* otherwise default to smallest count (drop through) */
14341 case 256:
14342 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14343 LPFC_CQ_CNT_256);
14344 break;
14345 case 512:
14346 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14347 LPFC_CQ_CNT_512);
14348 break;
14349 case 1024:
14350 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14351 LPFC_CQ_CNT_1024);
14352 break;
14353 }
14354 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14355 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14356 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14357 putPaddrLow(dmabuf->phys);
14358 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14359 putPaddrHigh(dmabuf->phys);
14360 }
14361 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14362
14363 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14366 if (shdr_status || shdr_add_status || rc) {
14367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14368 "2501 CQ_CREATE mailbox failed with "
14369 "status x%x add_status x%x, mbx status x%x\n",
14370 shdr_status, shdr_add_status, rc);
14371 status = -ENXIO;
14372 goto out;
14373 }
14374 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14375 if (cq->queue_id == 0xFFFF) {
14376 status = -ENXIO;
14377 goto out;
14378 }
14379 /* link the cq onto the parent eq child list */
14380 list_add_tail(&cq->list, &eq->child_list);
14381 /* Set up completion queue's type and subtype */
14382 cq->type = type;
14383 cq->subtype = subtype;
14384 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14385 cq->assoc_qid = eq->queue_id;
4f774513
JS
14386 cq->host_index = 0;
14387 cq->hba_index = 0;
64eb4dcb 14388 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14389
8fa38513
JS
14390out:
14391 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14392 return status;
14393}
14394
2d7dbc4c
JS
14395/**
14396 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14397 * @phba: HBA structure that indicates port to create a queue on.
14398 * @cqp: The queue structure array to use to create the completion queues.
14399 * @eqp: The event queue array to bind these completion queues to.
14400 *
14401 * This function creates a set of completion queue, s to support MRQ
14402 * as detailed in @cqp, on a port,
14403 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14404 *
14405 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14406 * is used to get the entry count and entry size that are necessary to
14407 * determine the number of pages to allocate and use for this queue. The @eq
14408 * is used to indicate which event queue to bind this completion queue to. This
14409 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14410 * completion queue. This function is asynchronous and will wait for the mailbox
14411 * command to finish before continuing.
14412 *
14413 * On success this function will return a zero. If unable to allocate enough
14414 * memory this function will return -ENOMEM. If the queue create mailbox command
14415 * fails this function will return -ENXIO.
14416 **/
14417int
14418lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14419 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14420{
14421 struct lpfc_queue *cq;
14422 struct lpfc_queue *eq;
14423 struct lpfc_mbx_cq_create_set *cq_set;
14424 struct lpfc_dmabuf *dmabuf;
14425 LPFC_MBOXQ_t *mbox;
14426 int rc, length, alloclen, status = 0;
14427 int cnt, idx, numcq, page_idx = 0;
14428 uint32_t shdr_status, shdr_add_status;
14429 union lpfc_sli4_cfg_shdr *shdr;
14430 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14431
14432 /* sanity check on queue memory */
14433 numcq = phba->cfg_nvmet_mrq;
14434 if (!cqp || !eqp || !numcq)
14435 return -ENODEV;
2d7dbc4c
JS
14436
14437 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14438 if (!mbox)
14439 return -ENOMEM;
14440
14441 length = sizeof(struct lpfc_mbx_cq_create_set);
14442 length += ((numcq * cqp[0]->page_count) *
14443 sizeof(struct dma_address));
14444 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14445 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14446 LPFC_SLI4_MBX_NEMBED);
14447 if (alloclen < length) {
14448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14449 "3098 Allocated DMA memory size (%d) is "
14450 "less than the requested DMA memory size "
14451 "(%d)\n", alloclen, length);
14452 status = -ENOMEM;
14453 goto out;
14454 }
14455 cq_set = mbox->sge_array->addr[0];
14456 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14457 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14458
14459 for (idx = 0; idx < numcq; idx++) {
14460 cq = cqp[idx];
14461 eq = eqp[idx];
14462 if (!cq || !eq) {
14463 status = -ENOMEM;
14464 goto out;
14465 }
81b96eda
JS
14466 if (!phba->sli4_hba.pc_sli4_params.supported)
14467 hw_page_size = cq->page_size;
2d7dbc4c
JS
14468
14469 switch (idx) {
14470 case 0:
14471 bf_set(lpfc_mbx_cq_create_set_page_size,
14472 &cq_set->u.request,
14473 (hw_page_size / SLI4_PAGE_SIZE));
14474 bf_set(lpfc_mbx_cq_create_set_num_pages,
14475 &cq_set->u.request, cq->page_count);
14476 bf_set(lpfc_mbx_cq_create_set_evt,
14477 &cq_set->u.request, 1);
14478 bf_set(lpfc_mbx_cq_create_set_valid,
14479 &cq_set->u.request, 1);
14480 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14481 &cq_set->u.request, 0);
14482 bf_set(lpfc_mbx_cq_create_set_num_cq,
14483 &cq_set->u.request, numcq);
14484 switch (cq->entry_count) {
81b96eda
JS
14485 case 2048:
14486 case 4096:
14487 if (phba->sli4_hba.pc_sli4_params.cqv ==
14488 LPFC_Q_CREATE_VERSION_2) {
14489 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14490 &cq_set->u.request,
14491 cq->entry_count);
14492 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14493 &cq_set->u.request,
14494 LPFC_CQ_CNT_WORD7);
14495 break;
14496 }
14497 /* Fall Thru */
2d7dbc4c
JS
14498 default:
14499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14500 "3118 Bad CQ count. (%d)\n",
14501 cq->entry_count);
14502 if (cq->entry_count < 256) {
14503 status = -EINVAL;
14504 goto out;
14505 }
14506 /* otherwise default to smallest (drop thru) */
14507 case 256:
14508 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14509 &cq_set->u.request, LPFC_CQ_CNT_256);
14510 break;
14511 case 512:
14512 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14513 &cq_set->u.request, LPFC_CQ_CNT_512);
14514 break;
14515 case 1024:
14516 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14517 &cq_set->u.request, LPFC_CQ_CNT_1024);
14518 break;
14519 }
14520 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14521 &cq_set->u.request, eq->queue_id);
14522 break;
14523 case 1:
14524 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14525 &cq_set->u.request, eq->queue_id);
14526 break;
14527 case 2:
14528 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14529 &cq_set->u.request, eq->queue_id);
14530 break;
14531 case 3:
14532 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14533 &cq_set->u.request, eq->queue_id);
14534 break;
14535 case 4:
14536 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14537 &cq_set->u.request, eq->queue_id);
14538 break;
14539 case 5:
14540 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14541 &cq_set->u.request, eq->queue_id);
14542 break;
14543 case 6:
14544 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14545 &cq_set->u.request, eq->queue_id);
14546 break;
14547 case 7:
14548 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14549 &cq_set->u.request, eq->queue_id);
14550 break;
14551 case 8:
14552 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14553 &cq_set->u.request, eq->queue_id);
14554 break;
14555 case 9:
14556 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14557 &cq_set->u.request, eq->queue_id);
14558 break;
14559 case 10:
14560 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14561 &cq_set->u.request, eq->queue_id);
14562 break;
14563 case 11:
14564 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14565 &cq_set->u.request, eq->queue_id);
14566 break;
14567 case 12:
14568 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14569 &cq_set->u.request, eq->queue_id);
14570 break;
14571 case 13:
14572 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14573 &cq_set->u.request, eq->queue_id);
14574 break;
14575 case 14:
14576 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14577 &cq_set->u.request, eq->queue_id);
14578 break;
14579 case 15:
14580 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14581 &cq_set->u.request, eq->queue_id);
14582 break;
14583 }
14584
14585 /* link the cq onto the parent eq child list */
14586 list_add_tail(&cq->list, &eq->child_list);
14587 /* Set up completion queue's type and subtype */
14588 cq->type = type;
14589 cq->subtype = subtype;
14590 cq->assoc_qid = eq->queue_id;
14591 cq->host_index = 0;
14592 cq->hba_index = 0;
64eb4dcb 14593 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14594 cq->chann = idx;
2d7dbc4c
JS
14595
14596 rc = 0;
14597 list_for_each_entry(dmabuf, &cq->page_list, list) {
14598 memset(dmabuf->virt, 0, hw_page_size);
14599 cnt = page_idx + dmabuf->buffer_tag;
14600 cq_set->u.request.page[cnt].addr_lo =
14601 putPaddrLow(dmabuf->phys);
14602 cq_set->u.request.page[cnt].addr_hi =
14603 putPaddrHigh(dmabuf->phys);
14604 rc++;
14605 }
14606 page_idx += rc;
14607 }
14608
14609 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14610
14611 /* The IOCTL status is embedded in the mailbox subheader. */
14612 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14613 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14614 if (shdr_status || shdr_add_status || rc) {
14615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14616 "3119 CQ_CREATE_SET mailbox failed with "
14617 "status x%x add_status x%x, mbx status x%x\n",
14618 shdr_status, shdr_add_status, rc);
14619 status = -ENXIO;
14620 goto out;
14621 }
14622 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14623 if (rc == 0xFFFF) {
14624 status = -ENXIO;
14625 goto out;
14626 }
14627
14628 for (idx = 0; idx < numcq; idx++) {
14629 cq = cqp[idx];
14630 cq->queue_id = rc + idx;
14631 }
14632
14633out:
14634 lpfc_sli4_mbox_cmd_free(phba, mbox);
14635 return status;
14636}
14637
b19a061a
JS
14638/**
14639 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14640 * @phba: HBA structure that indicates port to create a queue on.
14641 * @mq: The queue structure to use to create the mailbox queue.
14642 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14643 * @cq: The completion queue to associate with this cq.
14644 *
14645 * This function provides failback (fb) functionality when the
14646 * mq_create_ext fails on older FW generations. It's purpose is identical
14647 * to mq_create_ext otherwise.
14648 *
14649 * This routine cannot fail as all attributes were previously accessed and
14650 * initialized in mq_create_ext.
14651 **/
14652static void
14653lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14654 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14655{
14656 struct lpfc_mbx_mq_create *mq_create;
14657 struct lpfc_dmabuf *dmabuf;
14658 int length;
14659
14660 length = (sizeof(struct lpfc_mbx_mq_create) -
14661 sizeof(struct lpfc_sli4_cfg_mhdr));
14662 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14663 LPFC_MBOX_OPCODE_MQ_CREATE,
14664 length, LPFC_SLI4_MBX_EMBED);
14665 mq_create = &mbox->u.mqe.un.mq_create;
14666 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14667 mq->page_count);
14668 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14669 cq->queue_id);
14670 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14671 switch (mq->entry_count) {
14672 case 16:
5a6f133e
JS
14673 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14674 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14675 break;
14676 case 32:
5a6f133e
JS
14677 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14678 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14679 break;
14680 case 64:
5a6f133e
JS
14681 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14682 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14683 break;
14684 case 128:
5a6f133e
JS
14685 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14686 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14687 break;
14688 }
14689 list_for_each_entry(dmabuf, &mq->page_list, list) {
14690 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14691 putPaddrLow(dmabuf->phys);
14692 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14693 putPaddrHigh(dmabuf->phys);
14694 }
14695}
14696
04c68496
JS
14697/**
14698 * lpfc_mq_create - Create a mailbox Queue on the HBA
14699 * @phba: HBA structure that indicates port to create a queue on.
14700 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14701 * @cq: The completion queue to associate with this cq.
14702 * @subtype: The queue's subtype.
04c68496
JS
14703 *
14704 * This function creates a mailbox queue, as detailed in @mq, on a port,
14705 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14706 *
14707 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14708 * is used to get the entry count and entry size that are necessary to
14709 * determine the number of pages to allocate and use for this queue. This
14710 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14711 * mailbox queue. This function is asynchronous and will wait for the mailbox
14712 * command to finish before continuing.
14713 *
14714 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14715 * memory this function will return -ENOMEM. If the queue create mailbox command
14716 * fails this function will return -ENXIO.
04c68496 14717 **/
b19a061a 14718int32_t
04c68496
JS
14719lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14720 struct lpfc_queue *cq, uint32_t subtype)
14721{
14722 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14723 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14724 struct lpfc_dmabuf *dmabuf;
14725 LPFC_MBOXQ_t *mbox;
14726 int rc, length, status = 0;
14727 uint32_t shdr_status, shdr_add_status;
14728 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14729 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14730
2e90f4b5
JS
14731 /* sanity check on queue memory */
14732 if (!mq || !cq)
14733 return -ENODEV;
49198b37
JS
14734 if (!phba->sli4_hba.pc_sli4_params.supported)
14735 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14736
04c68496
JS
14737 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14738 if (!mbox)
14739 return -ENOMEM;
b19a061a 14740 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14741 sizeof(struct lpfc_sli4_cfg_mhdr));
14742 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14743 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14744 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14745
14746 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14747 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14748 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14749 &mq_create_ext->u.request, mq->page_count);
14750 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14751 &mq_create_ext->u.request, 1);
14752 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14753 &mq_create_ext->u.request, 1);
14754 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14755 &mq_create_ext->u.request, 1);
70f3c073
JS
14756 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14757 &mq_create_ext->u.request, 1);
14758 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14759 &mq_create_ext->u.request, 1);
b19a061a 14760 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14761 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14762 phba->sli4_hba.pc_sli4_params.mqv);
14763 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14764 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14765 cq->queue_id);
14766 else
14767 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14768 cq->queue_id);
04c68496
JS
14769 switch (mq->entry_count) {
14770 default:
14771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14772 "0362 Unsupported MQ count. (%d)\n",
14773 mq->entry_count);
4f4c1863
JS
14774 if (mq->entry_count < 16) {
14775 status = -EINVAL;
14776 goto out;
14777 }
04c68496
JS
14778 /* otherwise default to smallest count (drop through) */
14779 case 16:
5a6f133e
JS
14780 bf_set(lpfc_mq_context_ring_size,
14781 &mq_create_ext->u.request.context,
14782 LPFC_MQ_RING_SIZE_16);
04c68496
JS
14783 break;
14784 case 32:
5a6f133e
JS
14785 bf_set(lpfc_mq_context_ring_size,
14786 &mq_create_ext->u.request.context,
14787 LPFC_MQ_RING_SIZE_32);
04c68496
JS
14788 break;
14789 case 64:
5a6f133e
JS
14790 bf_set(lpfc_mq_context_ring_size,
14791 &mq_create_ext->u.request.context,
14792 LPFC_MQ_RING_SIZE_64);
04c68496
JS
14793 break;
14794 case 128:
5a6f133e
JS
14795 bf_set(lpfc_mq_context_ring_size,
14796 &mq_create_ext->u.request.context,
14797 LPFC_MQ_RING_SIZE_128);
04c68496
JS
14798 break;
14799 }
14800 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 14801 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 14802 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 14803 putPaddrLow(dmabuf->phys);
b19a061a 14804 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
14805 putPaddrHigh(dmabuf->phys);
14806 }
14807 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
14808 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14809 &mq_create_ext->u.response);
14810 if (rc != MBX_SUCCESS) {
14811 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14812 "2795 MQ_CREATE_EXT failed with "
14813 "status x%x. Failback to MQ_CREATE.\n",
14814 rc);
14815 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14816 mq_create = &mbox->u.mqe.un.mq_create;
14817 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14818 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14819 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14820 &mq_create->u.response);
14821 }
14822
04c68496 14823 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
14824 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14825 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14826 if (shdr_status || shdr_add_status || rc) {
14827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14828 "2502 MQ_CREATE mailbox failed with "
14829 "status x%x add_status x%x, mbx status x%x\n",
14830 shdr_status, shdr_add_status, rc);
14831 status = -ENXIO;
14832 goto out;
14833 }
04c68496
JS
14834 if (mq->queue_id == 0xFFFF) {
14835 status = -ENXIO;
14836 goto out;
14837 }
14838 mq->type = LPFC_MQ;
2a622bfb 14839 mq->assoc_qid = cq->queue_id;
04c68496
JS
14840 mq->subtype = subtype;
14841 mq->host_index = 0;
14842 mq->hba_index = 0;
64eb4dcb 14843 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
14844
14845 /* link the mq onto the parent cq child list */
14846 list_add_tail(&mq->list, &cq->child_list);
14847out:
8fa38513 14848 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
14849 return status;
14850}
14851
4f774513
JS
14852/**
14853 * lpfc_wq_create - Create a Work Queue on the HBA
14854 * @phba: HBA structure that indicates port to create a queue on.
14855 * @wq: The queue structure to use to create the work queue.
14856 * @cq: The completion queue to bind this work queue to.
14857 * @subtype: The subtype of the work queue indicating its functionality.
14858 *
14859 * This function creates a work queue, as detailed in @wq, on a port, described
14860 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14861 *
14862 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14863 * is used to get the entry count and entry size that are necessary to
14864 * determine the number of pages to allocate and use for this queue. The @cq
14865 * is used to indicate which completion queue to bind this work queue to. This
14866 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14867 * work queue. This function is asynchronous and will wait for the mailbox
14868 * command to finish before continuing.
14869 *
14870 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14871 * memory this function will return -ENOMEM. If the queue create mailbox command
14872 * fails this function will return -ENXIO.
4f774513 14873 **/
a2fc4aef 14874int
4f774513
JS
14875lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14876 struct lpfc_queue *cq, uint32_t subtype)
14877{
14878 struct lpfc_mbx_wq_create *wq_create;
14879 struct lpfc_dmabuf *dmabuf;
14880 LPFC_MBOXQ_t *mbox;
14881 int rc, length, status = 0;
14882 uint32_t shdr_status, shdr_add_status;
14883 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14884 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 14885 struct dma_address *page;
962bc51b
JS
14886 void __iomem *bar_memmap_p;
14887 uint32_t db_offset;
14888 uint16_t pci_barset;
81b96eda 14889 uint8_t wq_create_version;
49198b37 14890
2e90f4b5
JS
14891 /* sanity check on queue memory */
14892 if (!wq || !cq)
14893 return -ENODEV;
49198b37 14894 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14895 hw_page_size = wq->page_size;
4f774513
JS
14896
14897 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14898 if (!mbox)
14899 return -ENOMEM;
14900 length = (sizeof(struct lpfc_mbx_wq_create) -
14901 sizeof(struct lpfc_sli4_cfg_mhdr));
14902 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14903 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14904 length, LPFC_SLI4_MBX_EMBED);
14905 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 14906 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
14907 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14908 wq->page_count);
14909 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14910 cq->queue_id);
0c651878
JS
14911
14912 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
14913 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14914 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 14915
c176ffa0
JS
14916 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
14917 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
14918 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14919 else
14920 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14921
14922 switch (wq_create_version) {
0c651878
JS
14923 case LPFC_Q_CREATE_VERSION_0:
14924 switch (wq->entry_size) {
14925 default:
14926 case 64:
14927 /* Nothing to do, version 0 ONLY supports 64 byte */
14928 page = wq_create->u.request.page;
14929 break;
14930 case 128:
14931 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14932 LPFC_WQ_SZ128_SUPPORT)) {
14933 status = -ERANGE;
14934 goto out;
14935 }
14936 /* If we get here the HBA MUST also support V1 and
14937 * we MUST use it
14938 */
14939 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14940 LPFC_Q_CREATE_VERSION_1);
14941
14942 bf_set(lpfc_mbx_wq_create_wqe_count,
14943 &wq_create->u.request_1, wq->entry_count);
14944 bf_set(lpfc_mbx_wq_create_wqe_size,
14945 &wq_create->u.request_1,
14946 LPFC_WQ_WQE_SIZE_128);
14947 bf_set(lpfc_mbx_wq_create_page_size,
14948 &wq_create->u.request_1,
8ea73db4 14949 LPFC_WQ_PAGE_SIZE_4096);
0c651878
JS
14950 page = wq_create->u.request_1.page;
14951 break;
14952 }
14953 break;
14954 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
14955 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14956 wq->entry_count);
3f247de7
JS
14957 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14958 LPFC_Q_CREATE_VERSION_1);
14959
5a6f133e
JS
14960 switch (wq->entry_size) {
14961 default:
14962 case 64:
14963 bf_set(lpfc_mbx_wq_create_wqe_size,
14964 &wq_create->u.request_1,
14965 LPFC_WQ_WQE_SIZE_64);
14966 break;
14967 case 128:
0c651878
JS
14968 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14969 LPFC_WQ_SZ128_SUPPORT)) {
14970 status = -ERANGE;
14971 goto out;
14972 }
5a6f133e
JS
14973 bf_set(lpfc_mbx_wq_create_wqe_size,
14974 &wq_create->u.request_1,
14975 LPFC_WQ_WQE_SIZE_128);
14976 break;
14977 }
8ea73db4
JS
14978 bf_set(lpfc_mbx_wq_create_page_size,
14979 &wq_create->u.request_1,
81b96eda 14980 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 14981 page = wq_create->u.request_1.page;
0c651878
JS
14982 break;
14983 default:
14984 status = -ERANGE;
14985 goto out;
5a6f133e 14986 }
0c651878 14987
4f774513 14988 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 14989 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
14990 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14991 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 14992 }
962bc51b
JS
14993
14994 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14995 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14996
4f774513
JS
14997 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14998 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14999 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15000 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15001 if (shdr_status || shdr_add_status || rc) {
15002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15003 "2503 WQ_CREATE mailbox failed with "
15004 "status x%x add_status x%x, mbx status x%x\n",
15005 shdr_status, shdr_add_status, rc);
15006 status = -ENXIO;
15007 goto out;
15008 }
15009 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15010 if (wq->queue_id == 0xFFFF) {
15011 status = -ENXIO;
15012 goto out;
15013 }
962bc51b
JS
15014 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15015 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15016 &wq_create->u.response);
15017 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15018 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15020 "3265 WQ[%d] doorbell format not "
15021 "supported: x%x\n", wq->queue_id,
15022 wq->db_format);
15023 status = -EINVAL;
15024 goto out;
15025 }
15026 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15027 &wq_create->u.response);
15028 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15029 if (!bar_memmap_p) {
15030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15031 "3263 WQ[%d] failed to memmap pci "
15032 "barset:x%x\n", wq->queue_id,
15033 pci_barset);
15034 status = -ENOMEM;
15035 goto out;
15036 }
15037 db_offset = wq_create->u.response.doorbell_offset;
15038 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15039 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15041 "3252 WQ[%d] doorbell offset not "
15042 "supported: x%x\n", wq->queue_id,
15043 db_offset);
15044 status = -EINVAL;
15045 goto out;
15046 }
15047 wq->db_regaddr = bar_memmap_p + db_offset;
15048 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15049 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15050 "format:x%x\n", wq->queue_id, pci_barset,
15051 db_offset, wq->db_format);
962bc51b
JS
15052 } else {
15053 wq->db_format = LPFC_DB_LIST_FORMAT;
15054 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15055 }
895427bd
JS
15056 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15057 if (wq->pring == NULL) {
15058 status = -ENOMEM;
15059 goto out;
15060 }
4f774513 15061 wq->type = LPFC_WQ;
2a622bfb 15062 wq->assoc_qid = cq->queue_id;
4f774513
JS
15063 wq->subtype = subtype;
15064 wq->host_index = 0;
15065 wq->hba_index = 0;
ff78d8f9 15066 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15067
15068 /* link the wq onto the parent cq child list */
15069 list_add_tail(&wq->list, &cq->child_list);
15070out:
8fa38513 15071 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15072 return status;
15073}
15074
15075/**
15076 * lpfc_rq_create - Create a Receive Queue on the HBA
15077 * @phba: HBA structure that indicates port to create a queue on.
15078 * @hrq: The queue structure to use to create the header receive queue.
15079 * @drq: The queue structure to use to create the data receive queue.
15080 * @cq: The completion queue to bind this work queue to.
15081 *
15082 * This function creates a receive buffer queue pair , as detailed in @hrq and
15083 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15084 * to the HBA.
15085 *
15086 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15087 * struct is used to get the entry count that is necessary to determine the
15088 * number of pages to use for this queue. The @cq is used to indicate which
15089 * completion queue to bind received buffers that are posted to these queues to.
15090 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15091 * receive queue pair. This function is asynchronous and will wait for the
15092 * mailbox command to finish before continuing.
15093 *
15094 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15095 * memory this function will return -ENOMEM. If the queue create mailbox command
15096 * fails this function will return -ENXIO.
4f774513 15097 **/
a2fc4aef 15098int
4f774513
JS
15099lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15100 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15101{
15102 struct lpfc_mbx_rq_create *rq_create;
15103 struct lpfc_dmabuf *dmabuf;
15104 LPFC_MBOXQ_t *mbox;
15105 int rc, length, status = 0;
15106 uint32_t shdr_status, shdr_add_status;
15107 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15108 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15109 void __iomem *bar_memmap_p;
15110 uint32_t db_offset;
15111 uint16_t pci_barset;
49198b37 15112
2e90f4b5
JS
15113 /* sanity check on queue memory */
15114 if (!hrq || !drq || !cq)
15115 return -ENODEV;
49198b37
JS
15116 if (!phba->sli4_hba.pc_sli4_params.supported)
15117 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15118
15119 if (hrq->entry_count != drq->entry_count)
15120 return -EINVAL;
15121 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15122 if (!mbox)
15123 return -ENOMEM;
15124 length = (sizeof(struct lpfc_mbx_rq_create) -
15125 sizeof(struct lpfc_sli4_cfg_mhdr));
15126 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15127 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15128 length, LPFC_SLI4_MBX_EMBED);
15129 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15130 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15131 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15132 phba->sli4_hba.pc_sli4_params.rqv);
15133 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15134 bf_set(lpfc_rq_context_rqe_count_1,
15135 &rq_create->u.request.context,
15136 hrq->entry_count);
15137 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15138 bf_set(lpfc_rq_context_rqe_size,
15139 &rq_create->u.request.context,
15140 LPFC_RQE_SIZE_8);
15141 bf_set(lpfc_rq_context_page_size,
15142 &rq_create->u.request.context,
8ea73db4 15143 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15144 } else {
15145 switch (hrq->entry_count) {
15146 default:
15147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15148 "2535 Unsupported RQ count. (%d)\n",
15149 hrq->entry_count);
4f4c1863
JS
15150 if (hrq->entry_count < 512) {
15151 status = -EINVAL;
15152 goto out;
15153 }
5a6f133e
JS
15154 /* otherwise default to smallest count (drop through) */
15155 case 512:
15156 bf_set(lpfc_rq_context_rqe_count,
15157 &rq_create->u.request.context,
15158 LPFC_RQ_RING_SIZE_512);
15159 break;
15160 case 1024:
15161 bf_set(lpfc_rq_context_rqe_count,
15162 &rq_create->u.request.context,
15163 LPFC_RQ_RING_SIZE_1024);
15164 break;
15165 case 2048:
15166 bf_set(lpfc_rq_context_rqe_count,
15167 &rq_create->u.request.context,
15168 LPFC_RQ_RING_SIZE_2048);
15169 break;
15170 case 4096:
15171 bf_set(lpfc_rq_context_rqe_count,
15172 &rq_create->u.request.context,
15173 LPFC_RQ_RING_SIZE_4096);
15174 break;
15175 }
15176 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15177 LPFC_HDR_BUF_SIZE);
4f774513
JS
15178 }
15179 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15180 cq->queue_id);
15181 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15182 hrq->page_count);
4f774513 15183 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15184 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15185 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15186 putPaddrLow(dmabuf->phys);
15187 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15188 putPaddrHigh(dmabuf->phys);
15189 }
962bc51b
JS
15190 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15191 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15192
4f774513
JS
15193 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15194 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15195 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15196 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15197 if (shdr_status || shdr_add_status || rc) {
15198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15199 "2504 RQ_CREATE mailbox failed with "
15200 "status x%x add_status x%x, mbx status x%x\n",
15201 shdr_status, shdr_add_status, rc);
15202 status = -ENXIO;
15203 goto out;
15204 }
15205 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15206 if (hrq->queue_id == 0xFFFF) {
15207 status = -ENXIO;
15208 goto out;
15209 }
962bc51b
JS
15210
15211 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15212 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15213 &rq_create->u.response);
15214 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15215 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15217 "3262 RQ [%d] doorbell format not "
15218 "supported: x%x\n", hrq->queue_id,
15219 hrq->db_format);
15220 status = -EINVAL;
15221 goto out;
15222 }
15223
15224 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15225 &rq_create->u.response);
15226 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15227 if (!bar_memmap_p) {
15228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15229 "3269 RQ[%d] failed to memmap pci "
15230 "barset:x%x\n", hrq->queue_id,
15231 pci_barset);
15232 status = -ENOMEM;
15233 goto out;
15234 }
15235
15236 db_offset = rq_create->u.response.doorbell_offset;
15237 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15238 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15240 "3270 RQ[%d] doorbell offset not "
15241 "supported: x%x\n", hrq->queue_id,
15242 db_offset);
15243 status = -EINVAL;
15244 goto out;
15245 }
15246 hrq->db_regaddr = bar_memmap_p + db_offset;
15247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15248 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15249 "format:x%x\n", hrq->queue_id, pci_barset,
15250 db_offset, hrq->db_format);
962bc51b
JS
15251 } else {
15252 hrq->db_format = LPFC_DB_RING_FORMAT;
15253 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15254 }
4f774513 15255 hrq->type = LPFC_HRQ;
2a622bfb 15256 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15257 hrq->subtype = subtype;
15258 hrq->host_index = 0;
15259 hrq->hba_index = 0;
61f3d4bf 15260 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15261
15262 /* now create the data queue */
15263 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15264 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15265 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15266 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15267 phba->sli4_hba.pc_sli4_params.rqv);
15268 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15269 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15270 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15271 if (subtype == LPFC_NVMET)
15272 rq_create->u.request.context.buffer_size =
15273 LPFC_NVMET_DATA_BUF_SIZE;
15274 else
15275 rq_create->u.request.context.buffer_size =
15276 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15277 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15278 LPFC_RQE_SIZE_8);
15279 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15280 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15281 } else {
15282 switch (drq->entry_count) {
15283 default:
15284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15285 "2536 Unsupported RQ count. (%d)\n",
15286 drq->entry_count);
4f4c1863
JS
15287 if (drq->entry_count < 512) {
15288 status = -EINVAL;
15289 goto out;
15290 }
5a6f133e
JS
15291 /* otherwise default to smallest count (drop through) */
15292 case 512:
15293 bf_set(lpfc_rq_context_rqe_count,
15294 &rq_create->u.request.context,
15295 LPFC_RQ_RING_SIZE_512);
15296 break;
15297 case 1024:
15298 bf_set(lpfc_rq_context_rqe_count,
15299 &rq_create->u.request.context,
15300 LPFC_RQ_RING_SIZE_1024);
15301 break;
15302 case 2048:
15303 bf_set(lpfc_rq_context_rqe_count,
15304 &rq_create->u.request.context,
15305 LPFC_RQ_RING_SIZE_2048);
15306 break;
15307 case 4096:
15308 bf_set(lpfc_rq_context_rqe_count,
15309 &rq_create->u.request.context,
15310 LPFC_RQ_RING_SIZE_4096);
15311 break;
15312 }
3c603be9
JS
15313 if (subtype == LPFC_NVMET)
15314 bf_set(lpfc_rq_context_buf_size,
15315 &rq_create->u.request.context,
15316 LPFC_NVMET_DATA_BUF_SIZE);
15317 else
15318 bf_set(lpfc_rq_context_buf_size,
15319 &rq_create->u.request.context,
15320 LPFC_DATA_BUF_SIZE);
4f774513
JS
15321 }
15322 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15323 cq->queue_id);
15324 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15325 drq->page_count);
4f774513
JS
15326 list_for_each_entry(dmabuf, &drq->page_list, list) {
15327 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15328 putPaddrLow(dmabuf->phys);
15329 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15330 putPaddrHigh(dmabuf->phys);
15331 }
962bc51b
JS
15332 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15333 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15334 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15335 /* The IOCTL status is embedded in the mailbox subheader. */
15336 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15339 if (shdr_status || shdr_add_status || rc) {
15340 status = -ENXIO;
15341 goto out;
15342 }
15343 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15344 if (drq->queue_id == 0xFFFF) {
15345 status = -ENXIO;
15346 goto out;
15347 }
15348 drq->type = LPFC_DRQ;
2a622bfb 15349 drq->assoc_qid = cq->queue_id;
4f774513
JS
15350 drq->subtype = subtype;
15351 drq->host_index = 0;
15352 drq->hba_index = 0;
61f3d4bf 15353 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15354
15355 /* link the header and data RQs onto the parent cq child list */
15356 list_add_tail(&hrq->list, &cq->child_list);
15357 list_add_tail(&drq->list, &cq->child_list);
15358
15359out:
8fa38513 15360 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15361 return status;
15362}
15363
2d7dbc4c
JS
15364/**
15365 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15366 * @phba: HBA structure that indicates port to create a queue on.
15367 * @hrqp: The queue structure array to use to create the header receive queues.
15368 * @drqp: The queue structure array to use to create the data receive queues.
15369 * @cqp: The completion queue array to bind these receive queues to.
15370 *
15371 * This function creates a receive buffer queue pair , as detailed in @hrq and
15372 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15373 * to the HBA.
15374 *
15375 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15376 * struct is used to get the entry count that is necessary to determine the
15377 * number of pages to use for this queue. The @cq is used to indicate which
15378 * completion queue to bind received buffers that are posted to these queues to.
15379 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15380 * receive queue pair. This function is asynchronous and will wait for the
15381 * mailbox command to finish before continuing.
15382 *
15383 * On success this function will return a zero. If unable to allocate enough
15384 * memory this function will return -ENOMEM. If the queue create mailbox command
15385 * fails this function will return -ENXIO.
15386 **/
15387int
15388lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15389 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15390 uint32_t subtype)
15391{
15392 struct lpfc_queue *hrq, *drq, *cq;
15393 struct lpfc_mbx_rq_create_v2 *rq_create;
15394 struct lpfc_dmabuf *dmabuf;
15395 LPFC_MBOXQ_t *mbox;
15396 int rc, length, alloclen, status = 0;
15397 int cnt, idx, numrq, page_idx = 0;
15398 uint32_t shdr_status, shdr_add_status;
15399 union lpfc_sli4_cfg_shdr *shdr;
15400 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15401
15402 numrq = phba->cfg_nvmet_mrq;
15403 /* sanity check on array memory */
15404 if (!hrqp || !drqp || !cqp || !numrq)
15405 return -ENODEV;
15406 if (!phba->sli4_hba.pc_sli4_params.supported)
15407 hw_page_size = SLI4_PAGE_SIZE;
15408
15409 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15410 if (!mbox)
15411 return -ENOMEM;
15412
15413 length = sizeof(struct lpfc_mbx_rq_create_v2);
15414 length += ((2 * numrq * hrqp[0]->page_count) *
15415 sizeof(struct dma_address));
15416
15417 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15418 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15419 LPFC_SLI4_MBX_NEMBED);
15420 if (alloclen < length) {
15421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15422 "3099 Allocated DMA memory size (%d) is "
15423 "less than the requested DMA memory size "
15424 "(%d)\n", alloclen, length);
15425 status = -ENOMEM;
15426 goto out;
15427 }
15428
15429
15430
15431 rq_create = mbox->sge_array->addr[0];
15432 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15433
15434 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15435 cnt = 0;
15436
15437 for (idx = 0; idx < numrq; idx++) {
15438 hrq = hrqp[idx];
15439 drq = drqp[idx];
15440 cq = cqp[idx];
15441
2d7dbc4c
JS
15442 /* sanity check on queue memory */
15443 if (!hrq || !drq || !cq) {
15444 status = -ENODEV;
15445 goto out;
15446 }
15447
7aabe84b
JS
15448 if (hrq->entry_count != drq->entry_count) {
15449 status = -EINVAL;
15450 goto out;
15451 }
15452
2d7dbc4c
JS
15453 if (idx == 0) {
15454 bf_set(lpfc_mbx_rq_create_num_pages,
15455 &rq_create->u.request,
15456 hrq->page_count);
15457 bf_set(lpfc_mbx_rq_create_rq_cnt,
15458 &rq_create->u.request, (numrq * 2));
15459 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15460 1);
15461 bf_set(lpfc_rq_context_base_cq,
15462 &rq_create->u.request.context,
15463 cq->queue_id);
15464 bf_set(lpfc_rq_context_data_size,
15465 &rq_create->u.request.context,
3c603be9 15466 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15467 bf_set(lpfc_rq_context_hdr_size,
15468 &rq_create->u.request.context,
15469 LPFC_HDR_BUF_SIZE);
15470 bf_set(lpfc_rq_context_rqe_count_1,
15471 &rq_create->u.request.context,
15472 hrq->entry_count);
15473 bf_set(lpfc_rq_context_rqe_size,
15474 &rq_create->u.request.context,
15475 LPFC_RQE_SIZE_8);
15476 bf_set(lpfc_rq_context_page_size,
15477 &rq_create->u.request.context,
15478 (PAGE_SIZE/SLI4_PAGE_SIZE));
15479 }
15480 rc = 0;
15481 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15482 memset(dmabuf->virt, 0, hw_page_size);
15483 cnt = page_idx + dmabuf->buffer_tag;
15484 rq_create->u.request.page[cnt].addr_lo =
15485 putPaddrLow(dmabuf->phys);
15486 rq_create->u.request.page[cnt].addr_hi =
15487 putPaddrHigh(dmabuf->phys);
15488 rc++;
15489 }
15490 page_idx += rc;
15491
15492 rc = 0;
15493 list_for_each_entry(dmabuf, &drq->page_list, list) {
15494 memset(dmabuf->virt, 0, hw_page_size);
15495 cnt = page_idx + dmabuf->buffer_tag;
15496 rq_create->u.request.page[cnt].addr_lo =
15497 putPaddrLow(dmabuf->phys);
15498 rq_create->u.request.page[cnt].addr_hi =
15499 putPaddrHigh(dmabuf->phys);
15500 rc++;
15501 }
15502 page_idx += rc;
15503
15504 hrq->db_format = LPFC_DB_RING_FORMAT;
15505 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15506 hrq->type = LPFC_HRQ;
15507 hrq->assoc_qid = cq->queue_id;
15508 hrq->subtype = subtype;
15509 hrq->host_index = 0;
15510 hrq->hba_index = 0;
61f3d4bf 15511 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15512
15513 drq->db_format = LPFC_DB_RING_FORMAT;
15514 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15515 drq->type = LPFC_DRQ;
15516 drq->assoc_qid = cq->queue_id;
15517 drq->subtype = subtype;
15518 drq->host_index = 0;
15519 drq->hba_index = 0;
61f3d4bf 15520 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15521
15522 list_add_tail(&hrq->list, &cq->child_list);
15523 list_add_tail(&drq->list, &cq->child_list);
15524 }
15525
15526 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15527 /* The IOCTL status is embedded in the mailbox subheader. */
15528 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15529 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15530 if (shdr_status || shdr_add_status || rc) {
15531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15532 "3120 RQ_CREATE mailbox failed with "
15533 "status x%x add_status x%x, mbx status x%x\n",
15534 shdr_status, shdr_add_status, rc);
15535 status = -ENXIO;
15536 goto out;
15537 }
15538 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15539 if (rc == 0xFFFF) {
15540 status = -ENXIO;
15541 goto out;
15542 }
15543
15544 /* Initialize all RQs with associated queue id */
15545 for (idx = 0; idx < numrq; idx++) {
15546 hrq = hrqp[idx];
15547 hrq->queue_id = rc + (2 * idx);
15548 drq = drqp[idx];
15549 drq->queue_id = rc + (2 * idx) + 1;
15550 }
15551
15552out:
15553 lpfc_sli4_mbox_cmd_free(phba, mbox);
15554 return status;
15555}
15556
4f774513
JS
15557/**
15558 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15559 * @eq: The queue structure associated with the queue to destroy.
15560 *
15561 * This function destroys a queue, as detailed in @eq by sending an mailbox
15562 * command, specific to the type of queue, to the HBA.
15563 *
15564 * The @eq struct is used to get the queue ID of the queue to destroy.
15565 *
15566 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15567 * command fails this function will return -ENXIO.
4f774513 15568 **/
a2fc4aef 15569int
4f774513
JS
15570lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15571{
15572 LPFC_MBOXQ_t *mbox;
15573 int rc, length, status = 0;
15574 uint32_t shdr_status, shdr_add_status;
15575 union lpfc_sli4_cfg_shdr *shdr;
15576
2e90f4b5 15577 /* sanity check on queue memory */
4f774513
JS
15578 if (!eq)
15579 return -ENODEV;
15580 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15581 if (!mbox)
15582 return -ENOMEM;
15583 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15584 sizeof(struct lpfc_sli4_cfg_mhdr));
15585 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15586 LPFC_MBOX_OPCODE_EQ_DESTROY,
15587 length, LPFC_SLI4_MBX_EMBED);
15588 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15589 eq->queue_id);
15590 mbox->vport = eq->phba->pport;
15591 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15592
15593 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15594 /* The IOCTL status is embedded in the mailbox subheader. */
15595 shdr = (union lpfc_sli4_cfg_shdr *)
15596 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15597 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15598 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15599 if (shdr_status || shdr_add_status || rc) {
15600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15601 "2505 EQ_DESTROY mailbox failed with "
15602 "status x%x add_status x%x, mbx status x%x\n",
15603 shdr_status, shdr_add_status, rc);
15604 status = -ENXIO;
15605 }
15606
15607 /* Remove eq from any list */
15608 list_del_init(&eq->list);
8fa38513 15609 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15610 return status;
15611}
15612
15613/**
15614 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15615 * @cq: The queue structure associated with the queue to destroy.
15616 *
15617 * This function destroys a queue, as detailed in @cq by sending an mailbox
15618 * command, specific to the type of queue, to the HBA.
15619 *
15620 * The @cq struct is used to get the queue ID of the queue to destroy.
15621 *
15622 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15623 * command fails this function will return -ENXIO.
4f774513 15624 **/
a2fc4aef 15625int
4f774513
JS
15626lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15627{
15628 LPFC_MBOXQ_t *mbox;
15629 int rc, length, status = 0;
15630 uint32_t shdr_status, shdr_add_status;
15631 union lpfc_sli4_cfg_shdr *shdr;
15632
2e90f4b5 15633 /* sanity check on queue memory */
4f774513
JS
15634 if (!cq)
15635 return -ENODEV;
15636 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15637 if (!mbox)
15638 return -ENOMEM;
15639 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15640 sizeof(struct lpfc_sli4_cfg_mhdr));
15641 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15642 LPFC_MBOX_OPCODE_CQ_DESTROY,
15643 length, LPFC_SLI4_MBX_EMBED);
15644 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15645 cq->queue_id);
15646 mbox->vport = cq->phba->pport;
15647 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15648 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15649 /* The IOCTL status is embedded in the mailbox subheader. */
15650 shdr = (union lpfc_sli4_cfg_shdr *)
15651 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15652 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15653 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15654 if (shdr_status || shdr_add_status || rc) {
15655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15656 "2506 CQ_DESTROY mailbox failed with "
15657 "status x%x add_status x%x, mbx status x%x\n",
15658 shdr_status, shdr_add_status, rc);
15659 status = -ENXIO;
15660 }
15661 /* Remove cq from any list */
15662 list_del_init(&cq->list);
8fa38513 15663 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15664 return status;
15665}
15666
04c68496
JS
15667/**
15668 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15669 * @qm: The queue structure associated with the queue to destroy.
15670 *
15671 * This function destroys a queue, as detailed in @mq by sending an mailbox
15672 * command, specific to the type of queue, to the HBA.
15673 *
15674 * The @mq struct is used to get the queue ID of the queue to destroy.
15675 *
15676 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15677 * command fails this function will return -ENXIO.
04c68496 15678 **/
a2fc4aef 15679int
04c68496
JS
15680lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15681{
15682 LPFC_MBOXQ_t *mbox;
15683 int rc, length, status = 0;
15684 uint32_t shdr_status, shdr_add_status;
15685 union lpfc_sli4_cfg_shdr *shdr;
15686
2e90f4b5 15687 /* sanity check on queue memory */
04c68496
JS
15688 if (!mq)
15689 return -ENODEV;
15690 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15691 if (!mbox)
15692 return -ENOMEM;
15693 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15694 sizeof(struct lpfc_sli4_cfg_mhdr));
15695 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15696 LPFC_MBOX_OPCODE_MQ_DESTROY,
15697 length, LPFC_SLI4_MBX_EMBED);
15698 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15699 mq->queue_id);
15700 mbox->vport = mq->phba->pport;
15701 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15702 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15703 /* The IOCTL status is embedded in the mailbox subheader. */
15704 shdr = (union lpfc_sli4_cfg_shdr *)
15705 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15706 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15707 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15708 if (shdr_status || shdr_add_status || rc) {
15709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15710 "2507 MQ_DESTROY mailbox failed with "
15711 "status x%x add_status x%x, mbx status x%x\n",
15712 shdr_status, shdr_add_status, rc);
15713 status = -ENXIO;
15714 }
15715 /* Remove mq from any list */
15716 list_del_init(&mq->list);
8fa38513 15717 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15718 return status;
15719}
15720
4f774513
JS
15721/**
15722 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15723 * @wq: The queue structure associated with the queue to destroy.
15724 *
15725 * This function destroys a queue, as detailed in @wq by sending an mailbox
15726 * command, specific to the type of queue, to the HBA.
15727 *
15728 * The @wq struct is used to get the queue ID of the queue to destroy.
15729 *
15730 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15731 * command fails this function will return -ENXIO.
4f774513 15732 **/
a2fc4aef 15733int
4f774513
JS
15734lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15735{
15736 LPFC_MBOXQ_t *mbox;
15737 int rc, length, status = 0;
15738 uint32_t shdr_status, shdr_add_status;
15739 union lpfc_sli4_cfg_shdr *shdr;
15740
2e90f4b5 15741 /* sanity check on queue memory */
4f774513
JS
15742 if (!wq)
15743 return -ENODEV;
15744 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15745 if (!mbox)
15746 return -ENOMEM;
15747 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15748 sizeof(struct lpfc_sli4_cfg_mhdr));
15749 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15750 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15751 length, LPFC_SLI4_MBX_EMBED);
15752 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15753 wq->queue_id);
15754 mbox->vport = wq->phba->pport;
15755 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15756 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15757 shdr = (union lpfc_sli4_cfg_shdr *)
15758 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15759 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15760 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15761 if (shdr_status || shdr_add_status || rc) {
15762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15763 "2508 WQ_DESTROY mailbox failed with "
15764 "status x%x add_status x%x, mbx status x%x\n",
15765 shdr_status, shdr_add_status, rc);
15766 status = -ENXIO;
15767 }
15768 /* Remove wq from any list */
15769 list_del_init(&wq->list);
d1f525aa
JS
15770 kfree(wq->pring);
15771 wq->pring = NULL;
8fa38513 15772 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
15773 return status;
15774}
15775
15776/**
15777 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15778 * @rq: The queue structure associated with the queue to destroy.
15779 *
15780 * This function destroys a queue, as detailed in @rq by sending an mailbox
15781 * command, specific to the type of queue, to the HBA.
15782 *
15783 * The @rq struct is used to get the queue ID of the queue to destroy.
15784 *
15785 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15786 * command fails this function will return -ENXIO.
4f774513 15787 **/
a2fc4aef 15788int
4f774513
JS
15789lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15790 struct lpfc_queue *drq)
15791{
15792 LPFC_MBOXQ_t *mbox;
15793 int rc, length, status = 0;
15794 uint32_t shdr_status, shdr_add_status;
15795 union lpfc_sli4_cfg_shdr *shdr;
15796
2e90f4b5 15797 /* sanity check on queue memory */
4f774513
JS
15798 if (!hrq || !drq)
15799 return -ENODEV;
15800 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15801 if (!mbox)
15802 return -ENOMEM;
15803 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 15804 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
15805 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15806 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15807 length, LPFC_SLI4_MBX_EMBED);
15808 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15809 hrq->queue_id);
15810 mbox->vport = hrq->phba->pport;
15811 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15812 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15813 /* The IOCTL status is embedded in the mailbox subheader. */
15814 shdr = (union lpfc_sli4_cfg_shdr *)
15815 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15816 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15817 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15818 if (shdr_status || shdr_add_status || rc) {
15819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15820 "2509 RQ_DESTROY mailbox failed with "
15821 "status x%x add_status x%x, mbx status x%x\n",
15822 shdr_status, shdr_add_status, rc);
15823 if (rc != MBX_TIMEOUT)
15824 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15825 return -ENXIO;
15826 }
15827 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15828 drq->queue_id);
15829 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15830 shdr = (union lpfc_sli4_cfg_shdr *)
15831 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15832 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15833 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15834 if (shdr_status || shdr_add_status || rc) {
15835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15836 "2510 RQ_DESTROY mailbox failed with "
15837 "status x%x add_status x%x, mbx status x%x\n",
15838 shdr_status, shdr_add_status, rc);
15839 status = -ENXIO;
15840 }
15841 list_del_init(&hrq->list);
15842 list_del_init(&drq->list);
8fa38513 15843 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
15844 return status;
15845}
15846
15847/**
15848 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15849 * @phba: The virtual port for which this call being executed.
15850 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15851 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15852 * @xritag: the xritag that ties this io to the SGL pages.
15853 *
15854 * This routine will post the sgl pages for the IO that has the xritag
15855 * that is in the iocbq structure. The xritag is assigned during iocbq
15856 * creation and persists for as long as the driver is loaded.
15857 * if the caller has fewer than 256 scatter gather segments to map then
15858 * pdma_phys_addr1 should be 0.
15859 * If the caller needs to map more than 256 scatter gather segment then
15860 * pdma_phys_addr1 should be a valid physical address.
15861 * physical address for SGLs must be 64 byte aligned.
15862 * If you are going to map 2 SGL's then the first one must have 256 entries
15863 * the second sgl can have between 1 and 256 entries.
15864 *
15865 * Return codes:
15866 * 0 - Success
15867 * -ENXIO, -ENOMEM - Failure
15868 **/
15869int
15870lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15871 dma_addr_t pdma_phys_addr0,
15872 dma_addr_t pdma_phys_addr1,
15873 uint16_t xritag)
15874{
15875 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15876 LPFC_MBOXQ_t *mbox;
15877 int rc;
15878 uint32_t shdr_status, shdr_add_status;
6d368e53 15879 uint32_t mbox_tmo;
4f774513
JS
15880 union lpfc_sli4_cfg_shdr *shdr;
15881
15882 if (xritag == NO_XRI) {
15883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15884 "0364 Invalid param:\n");
15885 return -EINVAL;
15886 }
15887
15888 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15889 if (!mbox)
15890 return -ENOMEM;
15891
15892 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15893 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15894 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 15895 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
15896
15897 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15898 &mbox->u.mqe.un.post_sgl_pages;
15899 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15900 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15901
15902 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15903 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15904 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15905 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15906
15907 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15908 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15909 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15910 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15911 if (!phba->sli4_hba.intr_enable)
15912 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 15913 else {
a183a15f 15914 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
15915 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15916 }
4f774513
JS
15917 /* The IOCTL status is embedded in the mailbox subheader. */
15918 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15919 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15920 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15921 if (rc != MBX_TIMEOUT)
15922 mempool_free(mbox, phba->mbox_mem_pool);
15923 if (shdr_status || shdr_add_status || rc) {
15924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15925 "2511 POST_SGL mailbox failed with "
15926 "status x%x add_status x%x, mbx status x%x\n",
15927 shdr_status, shdr_add_status, rc);
4f774513
JS
15928 }
15929 return 0;
15930}
4f774513 15931
6d368e53 15932/**
88a2cfbb 15933 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
15934 * @phba: pointer to lpfc hba data structure.
15935 *
15936 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
15937 * HBA consistent with the SLI-4 interface spec. This routine
15938 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15939 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 15940 *
88a2cfbb
JS
15941 * Returns
15942 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15943 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15944 **/
5d8b8167 15945static uint16_t
6d368e53
JS
15946lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15947{
15948 unsigned long xri;
15949
15950 /*
15951 * Fetch the next logical xri. Because this index is logical,
15952 * the driver starts at 0 each time.
15953 */
15954 spin_lock_irq(&phba->hbalock);
15955 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15956 phba->sli4_hba.max_cfg_param.max_xri, 0);
15957 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15958 spin_unlock_irq(&phba->hbalock);
15959 return NO_XRI;
15960 } else {
15961 set_bit(xri, phba->sli4_hba.xri_bmask);
15962 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 15963 }
6d368e53
JS
15964 spin_unlock_irq(&phba->hbalock);
15965 return xri;
15966}
15967
15968/**
15969 * lpfc_sli4_free_xri - Release an xri for reuse.
15970 * @phba: pointer to lpfc hba data structure.
15971 *
15972 * This routine is invoked to release an xri to the pool of
15973 * available rpis maintained by the driver.
15974 **/
5d8b8167 15975static void
6d368e53
JS
15976__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15977{
15978 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
15979 phba->sli4_hba.max_cfg_param.xri_used--;
15980 }
15981}
15982
15983/**
15984 * lpfc_sli4_free_xri - Release an xri for reuse.
15985 * @phba: pointer to lpfc hba data structure.
15986 *
15987 * This routine is invoked to release an xri to the pool of
15988 * available rpis maintained by the driver.
15989 **/
15990void
15991lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15992{
15993 spin_lock_irq(&phba->hbalock);
15994 __lpfc_sli4_free_xri(phba, xri);
15995 spin_unlock_irq(&phba->hbalock);
15996}
15997
4f774513
JS
15998/**
15999 * lpfc_sli4_next_xritag - Get an xritag for the io
16000 * @phba: Pointer to HBA context object.
16001 *
16002 * This function gets an xritag for the iocb. If there is no unused xritag
16003 * it will return 0xffff.
16004 * The function returns the allocated xritag if successful, else returns zero.
16005 * Zero is not a valid xritag.
16006 * The caller is not required to hold any lock.
16007 **/
16008uint16_t
16009lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16010{
6d368e53 16011 uint16_t xri_index;
4f774513 16012
6d368e53 16013 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16014 if (xri_index == NO_XRI)
16015 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16016 "2004 Failed to allocate XRI.last XRITAG is %d"
16017 " Max XRI is %d, Used XRI is %d\n",
16018 xri_index,
16019 phba->sli4_hba.max_cfg_param.max_xri,
16020 phba->sli4_hba.max_cfg_param.xri_used);
16021 return xri_index;
4f774513
JS
16022}
16023
16024/**
895427bd 16025 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16026 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16027 * @post_sgl_list: pointer to els sgl entry list.
16028 * @count: number of els sgl entries on the list.
4f774513
JS
16029 *
16030 * This routine is invoked to post a block of driver's sgl pages to the
16031 * HBA using non-embedded mailbox command. No Lock is held. This routine
16032 * is only called when the driver is loading and after all IO has been
16033 * stopped.
16034 **/
8a9d2e80 16035static int
895427bd 16036lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16037 struct list_head *post_sgl_list,
16038 int post_cnt)
4f774513 16039{
8a9d2e80 16040 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16041 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16042 struct sgl_page_pairs *sgl_pg_pairs;
16043 void *viraddr;
16044 LPFC_MBOXQ_t *mbox;
16045 uint32_t reqlen, alloclen, pg_pairs;
16046 uint32_t mbox_tmo;
8a9d2e80
JS
16047 uint16_t xritag_start = 0;
16048 int rc = 0;
4f774513
JS
16049 uint32_t shdr_status, shdr_add_status;
16050 union lpfc_sli4_cfg_shdr *shdr;
16051
895427bd 16052 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16053 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16054 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16056 "2559 Block sgl registration required DMA "
16057 "size (%d) great than a page\n", reqlen);
16058 return -ENOMEM;
16059 }
895427bd 16060
4f774513 16061 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16062 if (!mbox)
4f774513 16063 return -ENOMEM;
4f774513
JS
16064
16065 /* Allocate DMA memory and set up the non-embedded mailbox command */
16066 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16067 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16068 LPFC_SLI4_MBX_NEMBED);
16069
16070 if (alloclen < reqlen) {
16071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16072 "0285 Allocated DMA memory size (%d) is "
16073 "less than the requested DMA memory "
16074 "size (%d)\n", alloclen, reqlen);
16075 lpfc_sli4_mbox_cmd_free(phba, mbox);
16076 return -ENOMEM;
16077 }
4f774513 16078 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16079 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16080 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16081 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16082
8a9d2e80
JS
16083 pg_pairs = 0;
16084 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16085 /* Set up the sge entry */
16086 sgl_pg_pairs->sgl_pg0_addr_lo =
16087 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16088 sgl_pg_pairs->sgl_pg0_addr_hi =
16089 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16090 sgl_pg_pairs->sgl_pg1_addr_lo =
16091 cpu_to_le32(putPaddrLow(0));
16092 sgl_pg_pairs->sgl_pg1_addr_hi =
16093 cpu_to_le32(putPaddrHigh(0));
6d368e53 16094
4f774513
JS
16095 /* Keep the first xritag on the list */
16096 if (pg_pairs == 0)
16097 xritag_start = sglq_entry->sli4_xritag;
16098 sgl_pg_pairs++;
8a9d2e80 16099 pg_pairs++;
4f774513 16100 }
6d368e53
JS
16101
16102 /* Complete initialization and perform endian conversion. */
4f774513 16103 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16104 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16105 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16106
4f774513
JS
16107 if (!phba->sli4_hba.intr_enable)
16108 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16109 else {
a183a15f 16110 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16111 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16112 }
16113 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16114 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16115 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16116 if (rc != MBX_TIMEOUT)
16117 lpfc_sli4_mbox_cmd_free(phba, mbox);
16118 if (shdr_status || shdr_add_status || rc) {
16119 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16120 "2513 POST_SGL_BLOCK mailbox command failed "
16121 "status x%x add_status x%x mbx status x%x\n",
16122 shdr_status, shdr_add_status, rc);
16123 rc = -ENXIO;
16124 }
16125 return rc;
16126}
16127
16128/**
16129 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16130 * @phba: pointer to lpfc hba data structure.
16131 * @sblist: pointer to scsi buffer list.
16132 * @count: number of scsi buffers on the list.
16133 *
16134 * This routine is invoked to post a block of @count scsi sgl pages from a
16135 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16136 * No Lock is held.
16137 *
16138 **/
16139int
8a9d2e80
JS
16140lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16141 struct list_head *sblist,
16142 int count)
4f774513
JS
16143{
16144 struct lpfc_scsi_buf *psb;
16145 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16146 struct sgl_page_pairs *sgl_pg_pairs;
16147 void *viraddr;
16148 LPFC_MBOXQ_t *mbox;
16149 uint32_t reqlen, alloclen, pg_pairs;
16150 uint32_t mbox_tmo;
16151 uint16_t xritag_start = 0;
16152 int rc = 0;
16153 uint32_t shdr_status, shdr_add_status;
16154 dma_addr_t pdma_phys_bpl1;
16155 union lpfc_sli4_cfg_shdr *shdr;
16156
16157 /* Calculate the requested length of the dma memory */
8a9d2e80 16158 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16159 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16160 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16161 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16162 "0217 Block sgl registration required DMA "
16163 "size (%d) great than a page\n", reqlen);
16164 return -ENOMEM;
16165 }
16166 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16167 if (!mbox) {
16168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16169 "0283 Failed to allocate mbox cmd memory\n");
16170 return -ENOMEM;
16171 }
16172
16173 /* Allocate DMA memory and set up the non-embedded mailbox command */
16174 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16175 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16176 LPFC_SLI4_MBX_NEMBED);
16177
16178 if (alloclen < reqlen) {
16179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16180 "2561 Allocated DMA memory size (%d) is "
16181 "less than the requested DMA memory "
16182 "size (%d)\n", alloclen, reqlen);
16183 lpfc_sli4_mbox_cmd_free(phba, mbox);
16184 return -ENOMEM;
16185 }
6d368e53 16186
4f774513 16187 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16188 viraddr = mbox->sge_array->addr[0];
16189
16190 /* Set up the SGL pages in the non-embedded DMA pages */
16191 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16192 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16193
16194 pg_pairs = 0;
16195 list_for_each_entry(psb, sblist, list) {
16196 /* Set up the sge entry */
16197 sgl_pg_pairs->sgl_pg0_addr_lo =
16198 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16199 sgl_pg_pairs->sgl_pg0_addr_hi =
16200 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16201 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16202 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16203 else
16204 pdma_phys_bpl1 = 0;
16205 sgl_pg_pairs->sgl_pg1_addr_lo =
16206 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16207 sgl_pg_pairs->sgl_pg1_addr_hi =
16208 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16209 /* Keep the first xritag on the list */
16210 if (pg_pairs == 0)
16211 xritag_start = psb->cur_iocbq.sli4_xritag;
16212 sgl_pg_pairs++;
16213 pg_pairs++;
16214 }
16215 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16216 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16217 /* Perform endian conversion if necessary */
16218 sgl->word0 = cpu_to_le32(sgl->word0);
16219
16220 if (!phba->sli4_hba.intr_enable)
16221 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16222 else {
a183a15f 16223 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16224 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16225 }
16226 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16227 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16228 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16229 if (rc != MBX_TIMEOUT)
16230 lpfc_sli4_mbox_cmd_free(phba, mbox);
16231 if (shdr_status || shdr_add_status || rc) {
16232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16233 "2564 POST_SGL_BLOCK mailbox command failed "
16234 "status x%x add_status x%x mbx status x%x\n",
16235 shdr_status, shdr_add_status, rc);
16236 rc = -ENXIO;
16237 }
16238 return rc;
16239}
16240
16241/**
16242 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16243 * @phba: pointer to lpfc_hba struct that the frame was received on
16244 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16245 *
16246 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16247 * valid type of frame that the LPFC driver will handle. This function will
16248 * return a zero if the frame is a valid frame or a non zero value when the
16249 * frame does not pass the check.
16250 **/
16251static int
16252lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16253{
474ffb74 16254 /* make rctl_names static to save stack space */
4f774513 16255 struct fc_vft_header *fc_vft_hdr;
546fc854 16256 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16257
ae9e28f3
JS
16258#define FC_RCTL_MDS_DIAGS 0xF4
16259
4f774513
JS
16260 switch (fc_hdr->fh_r_ctl) {
16261 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16262 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16263 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16264 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16265 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16266 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16267 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16268 case FC_RCTL_DD_CMD_STATUS: /* command status */
16269 case FC_RCTL_ELS_REQ: /* extended link services request */
16270 case FC_RCTL_ELS_REP: /* extended link services reply */
16271 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16272 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16273 case FC_RCTL_BA_NOP: /* basic link service NOP */
16274 case FC_RCTL_BA_ABTS: /* basic link service abort */
16275 case FC_RCTL_BA_RMC: /* remove connection */
16276 case FC_RCTL_BA_ACC: /* basic accept */
16277 case FC_RCTL_BA_RJT: /* basic reject */
16278 case FC_RCTL_BA_PRMT:
16279 case FC_RCTL_ACK_1: /* acknowledge_1 */
16280 case FC_RCTL_ACK_0: /* acknowledge_0 */
16281 case FC_RCTL_P_RJT: /* port reject */
16282 case FC_RCTL_F_RJT: /* fabric reject */
16283 case FC_RCTL_P_BSY: /* port busy */
16284 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16285 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16286 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16287 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16288 case FC_RCTL_END: /* end */
16289 break;
16290 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16291 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16292 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16293 return lpfc_fc_frame_check(phba, fc_hdr);
16294 default:
16295 goto drop;
16296 }
ae9e28f3
JS
16297
16298#define FC_TYPE_VENDOR_UNIQUE 0xFF
16299
4f774513
JS
16300 switch (fc_hdr->fh_type) {
16301 case FC_TYPE_BLS:
16302 case FC_TYPE_ELS:
16303 case FC_TYPE_FCP:
16304 case FC_TYPE_CT:
895427bd 16305 case FC_TYPE_NVME:
ae9e28f3 16306 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16307 break;
16308 case FC_TYPE_IP:
16309 case FC_TYPE_ILS:
16310 default:
16311 goto drop;
16312 }
546fc854 16313
4f774513 16314 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16315 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16316 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16317 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16318 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16319 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16320 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16321 be32_to_cpu(header[6]));
4f774513
JS
16322 return 0;
16323drop:
16324 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16325 "2539 Dropped frame rctl:x%x type:x%x\n",
16326 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16327 return 1;
16328}
16329
16330/**
16331 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16332 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16333 *
16334 * This function processes the FC header to retrieve the VFI from the VF
16335 * header, if one exists. This function will return the VFI if one exists
16336 * or 0 if no VSAN Header exists.
16337 **/
16338static uint32_t
16339lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16340{
16341 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16342
16343 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16344 return 0;
16345 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16346}
16347
16348/**
16349 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16350 * @phba: Pointer to the HBA structure to search for the vport on
16351 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16352 * @fcfi: The FC Fabric ID that the frame came from
16353 *
16354 * This function searches the @phba for a vport that matches the content of the
16355 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16356 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16357 * returns the matching vport pointer or NULL if unable to match frame to a
16358 * vport.
16359 **/
16360static struct lpfc_vport *
16361lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16362 uint16_t fcfi, uint32_t did)
4f774513
JS
16363{
16364 struct lpfc_vport **vports;
16365 struct lpfc_vport *vport = NULL;
16366 int i;
939723a4 16367
bf08611b
JS
16368 if (did == Fabric_DID)
16369 return phba->pport;
939723a4
JS
16370 if ((phba->pport->fc_flag & FC_PT2PT) &&
16371 !(phba->link_state == LPFC_HBA_READY))
16372 return phba->pport;
16373
4f774513 16374 vports = lpfc_create_vport_work_array(phba);
895427bd 16375 if (vports != NULL) {
4f774513
JS
16376 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16377 if (phba->fcf.fcfi == fcfi &&
16378 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16379 vports[i]->fc_myDID == did) {
16380 vport = vports[i];
16381 break;
16382 }
16383 }
895427bd 16384 }
4f774513
JS
16385 lpfc_destroy_vport_work_array(phba, vports);
16386 return vport;
16387}
16388
45ed1190
JS
16389/**
16390 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16391 * @vport: The vport to work on.
16392 *
16393 * This function updates the receive sequence time stamp for this vport. The
16394 * receive sequence time stamp indicates the time that the last frame of the
16395 * the sequence that has been idle for the longest amount of time was received.
16396 * the driver uses this time stamp to indicate if any received sequences have
16397 * timed out.
16398 **/
5d8b8167 16399static void
45ed1190
JS
16400lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16401{
16402 struct lpfc_dmabuf *h_buf;
16403 struct hbq_dmabuf *dmabuf = NULL;
16404
16405 /* get the oldest sequence on the rcv list */
16406 h_buf = list_get_first(&vport->rcv_buffer_list,
16407 struct lpfc_dmabuf, list);
16408 if (!h_buf)
16409 return;
16410 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16411 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16412}
16413
16414/**
16415 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16416 * @vport: The vport that the received sequences were sent to.
16417 *
16418 * This function cleans up all outstanding received sequences. This is called
16419 * by the driver when a link event or user action invalidates all the received
16420 * sequences.
16421 **/
16422void
16423lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16424{
16425 struct lpfc_dmabuf *h_buf, *hnext;
16426 struct lpfc_dmabuf *d_buf, *dnext;
16427 struct hbq_dmabuf *dmabuf = NULL;
16428
16429 /* start with the oldest sequence on the rcv list */
16430 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16431 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16432 list_del_init(&dmabuf->hbuf.list);
16433 list_for_each_entry_safe(d_buf, dnext,
16434 &dmabuf->dbuf.list, list) {
16435 list_del_init(&d_buf->list);
16436 lpfc_in_buf_free(vport->phba, d_buf);
16437 }
16438 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16439 }
16440}
16441
16442/**
16443 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16444 * @vport: The vport that the received sequences were sent to.
16445 *
16446 * This function determines whether any received sequences have timed out by
16447 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16448 * indicates that there is at least one timed out sequence this routine will
16449 * go through the received sequences one at a time from most inactive to most
16450 * active to determine which ones need to be cleaned up. Once it has determined
16451 * that a sequence needs to be cleaned up it will simply free up the resources
16452 * without sending an abort.
16453 **/
16454void
16455lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16456{
16457 struct lpfc_dmabuf *h_buf, *hnext;
16458 struct lpfc_dmabuf *d_buf, *dnext;
16459 struct hbq_dmabuf *dmabuf = NULL;
16460 unsigned long timeout;
16461 int abort_count = 0;
16462
16463 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16464 vport->rcv_buffer_time_stamp);
16465 if (list_empty(&vport->rcv_buffer_list) ||
16466 time_before(jiffies, timeout))
16467 return;
16468 /* start with the oldest sequence on the rcv list */
16469 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16470 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16471 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16472 dmabuf->time_stamp);
16473 if (time_before(jiffies, timeout))
16474 break;
16475 abort_count++;
16476 list_del_init(&dmabuf->hbuf.list);
16477 list_for_each_entry_safe(d_buf, dnext,
16478 &dmabuf->dbuf.list, list) {
16479 list_del_init(&d_buf->list);
16480 lpfc_in_buf_free(vport->phba, d_buf);
16481 }
16482 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16483 }
16484 if (abort_count)
16485 lpfc_update_rcv_time_stamp(vport);
16486}
16487
4f774513
JS
16488/**
16489 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16490 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16491 *
16492 * This function searches through the existing incomplete sequences that have
16493 * been sent to this @vport. If the frame matches one of the incomplete
16494 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16495 * make up that sequence. If no sequence is found that matches this frame then
16496 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16497 * This function returns a pointer to the first dmabuf in the sequence list that
16498 * the frame was linked to.
16499 **/
16500static struct hbq_dmabuf *
16501lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16502{
16503 struct fc_frame_header *new_hdr;
16504 struct fc_frame_header *temp_hdr;
16505 struct lpfc_dmabuf *d_buf;
16506 struct lpfc_dmabuf *h_buf;
16507 struct hbq_dmabuf *seq_dmabuf = NULL;
16508 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16509 uint8_t found = 0;
4f774513 16510
4d9ab994 16511 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16512 dmabuf->time_stamp = jiffies;
4f774513 16513 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16514
4f774513
JS
16515 /* Use the hdr_buf to find the sequence that this frame belongs to */
16516 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16517 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16518 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16519 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16520 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16521 continue;
16522 /* found a pending sequence that matches this frame */
16523 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16524 break;
16525 }
16526 if (!seq_dmabuf) {
16527 /*
16528 * This indicates first frame received for this sequence.
16529 * Queue the buffer on the vport's rcv_buffer_list.
16530 */
16531 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16532 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16533 return dmabuf;
16534 }
16535 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16536 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16537 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16538 list_del_init(&seq_dmabuf->hbuf.list);
16539 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16540 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16541 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16542 return dmabuf;
16543 }
45ed1190
JS
16544 /* move this sequence to the tail to indicate a young sequence */
16545 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16546 seq_dmabuf->time_stamp = jiffies;
16547 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16548 if (list_empty(&seq_dmabuf->dbuf.list)) {
16549 temp_hdr = dmabuf->hbuf.virt;
16550 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16551 return seq_dmabuf;
16552 }
4f774513 16553 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16554 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16555 while (!found) {
4f774513
JS
16556 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16557 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16558 /*
16559 * If the frame's sequence count is greater than the frame on
16560 * the list then insert the frame right after this frame
16561 */
eeead811
JS
16562 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16563 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16564 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16565 found = 1;
16566 break;
4f774513 16567 }
4360ca9c
JS
16568
16569 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16570 break;
16571 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16572 }
4360ca9c
JS
16573
16574 if (found)
16575 return seq_dmabuf;
4f774513
JS
16576 return NULL;
16577}
16578
6669f9bb
JS
16579/**
16580 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16581 * @vport: pointer to a vitural port
16582 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16583 *
16584 * This function tries to abort from the partially assembed sequence, described
16585 * by the information from basic abbort @dmabuf. It checks to see whether such
16586 * partially assembled sequence held by the driver. If so, it shall free up all
16587 * the frames from the partially assembled sequence.
16588 *
16589 * Return
16590 * true -- if there is matching partially assembled sequence present and all
16591 * the frames freed with the sequence;
16592 * false -- if there is no matching partially assembled sequence present so
16593 * nothing got aborted in the lower layer driver
16594 **/
16595static bool
16596lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16597 struct hbq_dmabuf *dmabuf)
16598{
16599 struct fc_frame_header *new_hdr;
16600 struct fc_frame_header *temp_hdr;
16601 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16602 struct hbq_dmabuf *seq_dmabuf = NULL;
16603
16604 /* Use the hdr_buf to find the sequence that matches this frame */
16605 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16606 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16607 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16608 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16609 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16610 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16611 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16612 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16613 continue;
16614 /* found a pending sequence that matches this frame */
16615 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16616 break;
16617 }
16618
16619 /* Free up all the frames from the partially assembled sequence */
16620 if (seq_dmabuf) {
16621 list_for_each_entry_safe(d_buf, n_buf,
16622 &seq_dmabuf->dbuf.list, list) {
16623 list_del_init(&d_buf->list);
16624 lpfc_in_buf_free(vport->phba, d_buf);
16625 }
16626 return true;
16627 }
16628 return false;
16629}
16630
6dd9e31c
JS
16631/**
16632 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16633 * @vport: pointer to a vitural port
16634 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16635 *
16636 * This function tries to abort from the assembed sequence from upper level
16637 * protocol, described by the information from basic abbort @dmabuf. It
16638 * checks to see whether such pending context exists at upper level protocol.
16639 * If so, it shall clean up the pending context.
16640 *
16641 * Return
16642 * true -- if there is matching pending context of the sequence cleaned
16643 * at ulp;
16644 * false -- if there is no matching pending context of the sequence present
16645 * at ulp.
16646 **/
16647static bool
16648lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16649{
16650 struct lpfc_hba *phba = vport->phba;
16651 int handled;
16652
16653 /* Accepting abort at ulp with SLI4 only */
16654 if (phba->sli_rev < LPFC_SLI_REV4)
16655 return false;
16656
16657 /* Register all caring upper level protocols to attend abort */
16658 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16659 if (handled)
16660 return true;
16661
16662 return false;
16663}
16664
6669f9bb 16665/**
546fc854 16666 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16667 * @phba: Pointer to HBA context object.
16668 * @cmd_iocbq: pointer to the command iocbq structure.
16669 * @rsp_iocbq: pointer to the response iocbq structure.
16670 *
546fc854 16671 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16672 * event. It properly releases the memory allocated to the sequence abort
16673 * accept iocb.
16674 **/
16675static void
546fc854 16676lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16677 struct lpfc_iocbq *cmd_iocbq,
16678 struct lpfc_iocbq *rsp_iocbq)
16679{
6dd9e31c
JS
16680 struct lpfc_nodelist *ndlp;
16681
16682 if (cmd_iocbq) {
16683 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16684 lpfc_nlp_put(ndlp);
16685 lpfc_nlp_not_used(ndlp);
6669f9bb 16686 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16687 }
6b5151fd
JS
16688
16689 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16690 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16691 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16692 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16693 rsp_iocbq->iocb.ulpStatus,
16694 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16695}
16696
6d368e53
JS
16697/**
16698 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16699 * @phba: Pointer to HBA context object.
16700 * @xri: xri id in transaction.
16701 *
16702 * This function validates the xri maps to the known range of XRIs allocated an
16703 * used by the driver.
16704 **/
7851fe2c 16705uint16_t
6d368e53
JS
16706lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16707 uint16_t xri)
16708{
a2fc4aef 16709 uint16_t i;
6d368e53
JS
16710
16711 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16712 if (xri == phba->sli4_hba.xri_ids[i])
16713 return i;
16714 }
16715 return NO_XRI;
16716}
16717
6669f9bb 16718/**
546fc854 16719 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16720 * @phba: Pointer to HBA context object.
16721 * @fc_hdr: pointer to a FC frame header.
16722 *
546fc854 16723 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16724 * event after aborting the sequence handling.
16725 **/
86c67379 16726void
6dd9e31c
JS
16727lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16728 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16729{
6dd9e31c 16730 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
16731 struct lpfc_iocbq *ctiocb = NULL;
16732 struct lpfc_nodelist *ndlp;
ee0f4fe1 16733 uint16_t oxid, rxid, xri, lxri;
5ffc266e 16734 uint32_t sid, fctl;
6669f9bb 16735 IOCB_t *icmd;
546fc854 16736 int rc;
6669f9bb
JS
16737
16738 if (!lpfc_is_link_up(phba))
16739 return;
16740
16741 sid = sli4_sid_from_fc_hdr(fc_hdr);
16742 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 16743 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 16744
6dd9e31c 16745 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 16746 if (!ndlp) {
9d3d340d 16747 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
16748 if (!ndlp) {
16749 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16750 "1268 Failed to allocate ndlp for "
16751 "oxid:x%x SID:x%x\n", oxid, sid);
16752 return;
16753 }
6dd9e31c
JS
16754 /* Put ndlp onto pport node list */
16755 lpfc_enqueue_node(vport, ndlp);
16756 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16757 /* re-setup ndlp without removing from node list */
16758 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16759 if (!ndlp) {
16760 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16761 "3275 Failed to active ndlp found "
16762 "for oxid:x%x SID:x%x\n", oxid, sid);
16763 return;
16764 }
6669f9bb
JS
16765 }
16766
546fc854 16767 /* Allocate buffer for rsp iocb */
6669f9bb
JS
16768 ctiocb = lpfc_sli_get_iocbq(phba);
16769 if (!ctiocb)
16770 return;
16771
5ffc266e
JS
16772 /* Extract the F_CTL field from FC_HDR */
16773 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16774
6669f9bb 16775 icmd = &ctiocb->iocb;
6669f9bb 16776 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 16777 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
16778 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16779 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16780 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16781
16782 /* Fill in the rest of iocb fields */
16783 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16784 icmd->ulpBdeCount = 0;
16785 icmd->ulpLe = 1;
16786 icmd->ulpClass = CLASS3;
6d368e53 16787 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 16788 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 16789
6669f9bb
JS
16790 ctiocb->iocb_cmpl = NULL;
16791 ctiocb->vport = phba->pport;
546fc854 16792 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 16793 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
16794 ctiocb->sli4_xritag = NO_XRI;
16795
ee0f4fe1
JS
16796 if (fctl & FC_FC_EX_CTX)
16797 /* Exchange responder sent the abort so we
16798 * own the oxid.
16799 */
16800 xri = oxid;
16801 else
16802 xri = rxid;
16803 lxri = lpfc_sli4_xri_inrange(phba, xri);
16804 if (lxri != NO_XRI)
16805 lpfc_set_rrq_active(phba, ndlp, lxri,
16806 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
16807 /* For BA_ABTS from exchange responder, if the logical xri with
16808 * the oxid maps to the FCP XRI range, the port no longer has
16809 * that exchange context, send a BLS_RJT. Override the IOCB for
16810 * a BA_RJT.
16811 */
16812 if ((fctl & FC_FC_EX_CTX) &&
895427bd 16813 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
16814 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16815 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16816 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16817 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16818 }
16819
16820 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16821 * the driver no longer has that exchange, send a BLS_RJT. Override
16822 * the IOCB for a BA_RJT.
546fc854 16823 */
6dd9e31c 16824 if (aborted == false) {
546fc854
JS
16825 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16826 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16827 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16828 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16829 }
6669f9bb 16830
5ffc266e
JS
16831 if (fctl & FC_FC_EX_CTX) {
16832 /* ABTS sent by responder to CT exchange, construction
16833 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16834 * field and RX_ID from ABTS for RX_ID field.
16835 */
546fc854 16836 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
16837 } else {
16838 /* ABTS sent by initiator to CT exchange, construction
16839 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 16840 * XRI_TAG field.
5ffc266e 16841 */
546fc854 16842 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 16843 }
f09c3acc 16844 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 16845 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 16846
546fc854 16847 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
16848 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16849 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16850 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
16851
16852 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16853 if (rc == IOCB_ERROR) {
6dd9e31c
JS
16854 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16855 "2925 Failed to issue CT ABTS RSP x%x on "
16856 "xri x%x, Data x%x\n",
16857 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16858 phba->link_state);
16859 lpfc_nlp_put(ndlp);
16860 ctiocb->context1 = NULL;
546fc854
JS
16861 lpfc_sli_release_iocbq(phba, ctiocb);
16862 }
6669f9bb
JS
16863}
16864
16865/**
16866 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16867 * @vport: Pointer to the vport on which this sequence was received
16868 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16869 *
16870 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16871 * receive sequence is only partially assembed by the driver, it shall abort
16872 * the partially assembled frames for the sequence. Otherwise, if the
16873 * unsolicited receive sequence has been completely assembled and passed to
16874 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16875 * unsolicited sequence has been aborted. After that, it will issue a basic
16876 * accept to accept the abort.
16877 **/
5d8b8167 16878static void
6669f9bb
JS
16879lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16880 struct hbq_dmabuf *dmabuf)
16881{
16882 struct lpfc_hba *phba = vport->phba;
16883 struct fc_frame_header fc_hdr;
5ffc266e 16884 uint32_t fctl;
6dd9e31c 16885 bool aborted;
6669f9bb 16886
6669f9bb
JS
16887 /* Make a copy of fc_hdr before the dmabuf being released */
16888 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 16889 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 16890
5ffc266e 16891 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
16892 /* ABTS by responder to exchange, no cleanup needed */
16893 aborted = true;
5ffc266e 16894 } else {
6dd9e31c
JS
16895 /* ABTS by initiator to exchange, need to do cleanup */
16896 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16897 if (aborted == false)
16898 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 16899 }
6dd9e31c
JS
16900 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16901
86c67379
JS
16902 if (phba->nvmet_support) {
16903 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16904 return;
16905 }
16906
6dd9e31c
JS
16907 /* Respond with BA_ACC or BA_RJT accordingly */
16908 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
16909}
16910
4f774513
JS
16911/**
16912 * lpfc_seq_complete - Indicates if a sequence is complete
16913 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16914 *
16915 * This function checks the sequence, starting with the frame described by
16916 * @dmabuf, to see if all the frames associated with this sequence are present.
16917 * the frames associated with this sequence are linked to the @dmabuf using the
16918 * dbuf list. This function looks for two major things. 1) That the first frame
16919 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16920 * set. 3) That there are no holes in the sequence count. The function will
16921 * return 1 when the sequence is complete, otherwise it will return 0.
16922 **/
16923static int
16924lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16925{
16926 struct fc_frame_header *hdr;
16927 struct lpfc_dmabuf *d_buf;
16928 struct hbq_dmabuf *seq_dmabuf;
16929 uint32_t fctl;
16930 int seq_count = 0;
16931
16932 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16933 /* make sure first fame of sequence has a sequence count of zero */
16934 if (hdr->fh_seq_cnt != seq_count)
16935 return 0;
16936 fctl = (hdr->fh_f_ctl[0] << 16 |
16937 hdr->fh_f_ctl[1] << 8 |
16938 hdr->fh_f_ctl[2]);
16939 /* If last frame of sequence we can return success. */
16940 if (fctl & FC_FC_END_SEQ)
16941 return 1;
16942 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16943 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16944 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16945 /* If there is a hole in the sequence count then fail. */
eeead811 16946 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
16947 return 0;
16948 fctl = (hdr->fh_f_ctl[0] << 16 |
16949 hdr->fh_f_ctl[1] << 8 |
16950 hdr->fh_f_ctl[2]);
16951 /* If last frame of sequence we can return success. */
16952 if (fctl & FC_FC_END_SEQ)
16953 return 1;
16954 }
16955 return 0;
16956}
16957
16958/**
16959 * lpfc_prep_seq - Prep sequence for ULP processing
16960 * @vport: Pointer to the vport on which this sequence was received
16961 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16962 *
16963 * This function takes a sequence, described by a list of frames, and creates
16964 * a list of iocbq structures to describe the sequence. This iocbq list will be
16965 * used to issue to the generic unsolicited sequence handler. This routine
16966 * returns a pointer to the first iocbq in the list. If the function is unable
16967 * to allocate an iocbq then it throw out the received frames that were not
16968 * able to be described and return a pointer to the first iocbq. If unable to
16969 * allocate any iocbqs (including the first) this function will return NULL.
16970 **/
16971static struct lpfc_iocbq *
16972lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16973{
7851fe2c 16974 struct hbq_dmabuf *hbq_buf;
4f774513
JS
16975 struct lpfc_dmabuf *d_buf, *n_buf;
16976 struct lpfc_iocbq *first_iocbq, *iocbq;
16977 struct fc_frame_header *fc_hdr;
16978 uint32_t sid;
7851fe2c 16979 uint32_t len, tot_len;
eeead811 16980 struct ulp_bde64 *pbde;
4f774513
JS
16981
16982 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16983 /* remove from receive buffer list */
16984 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 16985 lpfc_update_rcv_time_stamp(vport);
4f774513 16986 /* get the Remote Port's SID */
6669f9bb 16987 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 16988 tot_len = 0;
4f774513
JS
16989 /* Get an iocbq struct to fill in. */
16990 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16991 if (first_iocbq) {
16992 /* Initialize the first IOCB. */
8fa38513 16993 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 16994 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 16995 first_iocbq->vport = vport;
939723a4
JS
16996
16997 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16998 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16999 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17000 first_iocbq->iocb.un.rcvels.parmRo =
17001 sli4_did_from_fc_hdr(fc_hdr);
17002 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17003 } else
17004 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17005 first_iocbq->iocb.ulpContext = NO_XRI;
17006 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17007 be16_to_cpu(fc_hdr->fh_ox_id);
17008 /* iocbq is prepped for internal consumption. Physical vpi. */
17009 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17010 vport->phba->vpi_ids[vport->vpi];
4f774513 17011 /* put the first buffer into the first IOCBq */
48a5a664
JS
17012 tot_len = bf_get(lpfc_rcqe_length,
17013 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17014
4f774513
JS
17015 first_iocbq->context2 = &seq_dmabuf->dbuf;
17016 first_iocbq->context3 = NULL;
17017 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17018 if (tot_len > LPFC_DATA_BUF_SIZE)
17019 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17020 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17021 else
17022 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17023
4f774513 17024 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17025
7851fe2c 17026 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17027 }
17028 iocbq = first_iocbq;
17029 /*
17030 * Each IOCBq can have two Buffers assigned, so go through the list
17031 * of buffers for this sequence and save two buffers in each IOCBq
17032 */
17033 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17034 if (!iocbq) {
17035 lpfc_in_buf_free(vport->phba, d_buf);
17036 continue;
17037 }
17038 if (!iocbq->context3) {
17039 iocbq->context3 = d_buf;
17040 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17041 /* We need to get the size out of the right CQE */
17042 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17043 len = bf_get(lpfc_rcqe_length,
17044 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17045 pbde = (struct ulp_bde64 *)
17046 &iocbq->iocb.unsli3.sli3Words[4];
17047 if (len > LPFC_DATA_BUF_SIZE)
17048 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17049 else
17050 pbde->tus.f.bdeSize = len;
17051
7851fe2c
JS
17052 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17053 tot_len += len;
4f774513
JS
17054 } else {
17055 iocbq = lpfc_sli_get_iocbq(vport->phba);
17056 if (!iocbq) {
17057 if (first_iocbq) {
17058 first_iocbq->iocb.ulpStatus =
17059 IOSTAT_FCP_RSP_ERROR;
17060 first_iocbq->iocb.un.ulpWord[4] =
17061 IOERR_NO_RESOURCES;
17062 }
17063 lpfc_in_buf_free(vport->phba, d_buf);
17064 continue;
17065 }
48a5a664
JS
17066 /* We need to get the size out of the right CQE */
17067 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17068 len = bf_get(lpfc_rcqe_length,
17069 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17070 iocbq->context2 = d_buf;
17071 iocbq->context3 = NULL;
17072 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17073 if (len > LPFC_DATA_BUF_SIZE)
17074 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17075 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17076 else
17077 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17078
7851fe2c
JS
17079 tot_len += len;
17080 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17081
4f774513
JS
17082 iocbq->iocb.un.rcvels.remoteID = sid;
17083 list_add_tail(&iocbq->list, &first_iocbq->list);
17084 }
17085 }
17086 return first_iocbq;
17087}
17088
6669f9bb
JS
17089static void
17090lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17091 struct hbq_dmabuf *seq_dmabuf)
17092{
17093 struct fc_frame_header *fc_hdr;
17094 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17095 struct lpfc_hba *phba = vport->phba;
17096
17097 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17098 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17099 if (!iocbq) {
17100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17101 "2707 Ring %d handler: Failed to allocate "
17102 "iocb Rctl x%x Type x%x received\n",
17103 LPFC_ELS_RING,
17104 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17105 return;
17106 }
17107 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17108 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17109 iocbq, fc_hdr->fh_r_ctl,
17110 fc_hdr->fh_type))
6d368e53 17111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17112 "2540 Ring %d handler: unexpected Rctl "
17113 "x%x Type x%x received\n",
17114 LPFC_ELS_RING,
17115 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17116
17117 /* Free iocb created in lpfc_prep_seq */
17118 list_for_each_entry_safe(curr_iocb, next_iocb,
17119 &iocbq->list, list) {
17120 list_del_init(&curr_iocb->list);
17121 lpfc_sli_release_iocbq(phba, curr_iocb);
17122 }
17123 lpfc_sli_release_iocbq(phba, iocbq);
17124}
17125
ae9e28f3
JS
17126static void
17127lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17128 struct lpfc_iocbq *rspiocb)
17129{
17130 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17131
17132 if (pcmd && pcmd->virt)
771db5c0 17133 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17134 kfree(pcmd);
17135 lpfc_sli_release_iocbq(phba, cmdiocb);
17136}
17137
17138static void
17139lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17140 struct hbq_dmabuf *dmabuf)
17141{
17142 struct fc_frame_header *fc_hdr;
17143 struct lpfc_hba *phba = vport->phba;
17144 struct lpfc_iocbq *iocbq = NULL;
17145 union lpfc_wqe *wqe;
17146 struct lpfc_dmabuf *pcmd = NULL;
17147 uint32_t frame_len;
17148 int rc;
17149
17150 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17151 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17152
17153 /* Send the received frame back */
17154 iocbq = lpfc_sli_get_iocbq(phba);
17155 if (!iocbq)
17156 goto exit;
17157
17158 /* Allocate buffer for command payload */
17159 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17160 if (pcmd)
771db5c0 17161 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17162 &pcmd->phys);
17163 if (!pcmd || !pcmd->virt)
17164 goto exit;
17165
17166 INIT_LIST_HEAD(&pcmd->list);
17167
17168 /* copyin the payload */
17169 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17170
17171 /* fill in BDE's for command */
17172 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17173 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17174 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17175 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17176
17177 iocbq->context2 = pcmd;
17178 iocbq->vport = vport;
17179 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17180 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17181
17182 /*
17183 * Setup rest of the iocb as though it were a WQE
17184 * Build the SEND_FRAME WQE
17185 */
17186 wqe = (union lpfc_wqe *)&iocbq->iocb;
17187
17188 wqe->send_frame.frame_len = frame_len;
17189 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17190 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17191 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17192 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17193 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17194 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17195
17196 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17197 iocbq->iocb.ulpLe = 1;
17198 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17199 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17200 if (rc == IOCB_ERROR)
17201 goto exit;
17202
17203 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17204 return;
17205
17206exit:
17207 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17208 "2023 Unable to process MDS loopback frame\n");
17209 if (pcmd && pcmd->virt)
771db5c0 17210 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17211 kfree(pcmd);
401bb416
DK
17212 if (iocbq)
17213 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17214 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17215}
17216
4f774513
JS
17217/**
17218 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17219 * @phba: Pointer to HBA context object.
17220 *
17221 * This function is called with no lock held. This function processes all
17222 * the received buffers and gives it to upper layers when a received buffer
17223 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17224 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17225 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17226 * appropriate receive function when the final frame in a sequence is received.
17227 **/
4d9ab994
JS
17228void
17229lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17230 struct hbq_dmabuf *dmabuf)
4f774513 17231{
4d9ab994 17232 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17233 struct fc_frame_header *fc_hdr;
17234 struct lpfc_vport *vport;
17235 uint32_t fcfi;
939723a4 17236 uint32_t did;
4f774513 17237
4f774513 17238 /* Process each received buffer */
4d9ab994 17239 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17240
4d9ab994
JS
17241 /* check to see if this a valid type of frame */
17242 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17243 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17244 return;
17245 }
2ea259ee 17246
7851fe2c
JS
17247 if ((bf_get(lpfc_cqe_code,
17248 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17249 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17250 &dmabuf->cq_event.cqe.rcqe_cmpl);
17251 else
17252 fcfi = bf_get(lpfc_rcqe_fcf_id,
17253 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17254
ae9e28f3
JS
17255 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17256 vport = phba->pport;
17257 /* Handle MDS Loopback frames */
17258 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17259 return;
17260 }
17261
895427bd
JS
17262 /* d_id this frame is directed to */
17263 did = sli4_did_from_fc_hdr(fc_hdr);
17264
17265 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17266 if (!vport) {
4d9ab994
JS
17267 /* throw out the frame */
17268 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17269 return;
17270 }
939723a4 17271
939723a4
JS
17272 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17273 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17274 (did != Fabric_DID)) {
17275 /*
17276 * Throw out the frame if we are not pt2pt.
17277 * The pt2pt protocol allows for discovery frames
17278 * to be received without a registered VPI.
17279 */
17280 if (!(vport->fc_flag & FC_PT2PT) ||
17281 (phba->link_state == LPFC_HBA_READY)) {
17282 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17283 return;
17284 }
17285 }
17286
6669f9bb
JS
17287 /* Handle the basic abort sequence (BA_ABTS) event */
17288 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17289 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17290 return;
17291 }
17292
4d9ab994
JS
17293 /* Link this frame */
17294 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17295 if (!seq_dmabuf) {
17296 /* unable to add frame to vport - throw it out */
17297 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17298 return;
17299 }
17300 /* If not last frame in sequence continue processing frames. */
def9c7a9 17301 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17302 return;
def9c7a9 17303
6669f9bb
JS
17304 /* Send the complete sequence to the upper layer protocol */
17305 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17306}
6fb120a7
JS
17307
17308/**
17309 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17310 * @phba: pointer to lpfc hba data structure.
17311 *
17312 * This routine is invoked to post rpi header templates to the
17313 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17314 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17315 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17316 *
17317 * This routine does not require any locks. It's usage is expected
17318 * to be driver load or reset recovery when the driver is
17319 * sequential.
17320 *
17321 * Return codes
af901ca1 17322 * 0 - successful
d439d286 17323 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17324 * When this error occurs, the driver is not guaranteed
17325 * to have any rpi regions posted to the device and
17326 * must either attempt to repost the regions or take a
17327 * fatal error.
17328 **/
17329int
17330lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17331{
17332 struct lpfc_rpi_hdr *rpi_page;
17333 uint32_t rc = 0;
6d368e53
JS
17334 uint16_t lrpi = 0;
17335
17336 /* SLI4 ports that support extents do not require RPI headers. */
17337 if (!phba->sli4_hba.rpi_hdrs_in_use)
17338 goto exit;
17339 if (phba->sli4_hba.extents_in_use)
17340 return -EIO;
6fb120a7 17341
6fb120a7 17342 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17343 /*
17344 * Assign the rpi headers a physical rpi only if the driver
17345 * has not initialized those resources. A port reset only
17346 * needs the headers posted.
17347 */
17348 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17349 LPFC_RPI_RSRC_RDY)
17350 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17351
6fb120a7
JS
17352 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17353 if (rc != MBX_SUCCESS) {
17354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17355 "2008 Error %d posting all rpi "
17356 "headers\n", rc);
17357 rc = -EIO;
17358 break;
17359 }
17360 }
17361
6d368e53
JS
17362 exit:
17363 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17364 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17365 return rc;
17366}
17367
17368/**
17369 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17370 * @phba: pointer to lpfc hba data structure.
17371 * @rpi_page: pointer to the rpi memory region.
17372 *
17373 * This routine is invoked to post a single rpi header to the
17374 * HBA consistent with the SLI-4 interface spec. This memory region
17375 * maps up to 64 rpi context regions.
17376 *
17377 * Return codes
af901ca1 17378 * 0 - successful
d439d286
JS
17379 * -ENOMEM - No available memory
17380 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17381 **/
17382int
17383lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17384{
17385 LPFC_MBOXQ_t *mboxq;
17386 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17387 uint32_t rc = 0;
6fb120a7
JS
17388 uint32_t shdr_status, shdr_add_status;
17389 union lpfc_sli4_cfg_shdr *shdr;
17390
6d368e53
JS
17391 /* SLI4 ports that support extents do not require RPI headers. */
17392 if (!phba->sli4_hba.rpi_hdrs_in_use)
17393 return rc;
17394 if (phba->sli4_hba.extents_in_use)
17395 return -EIO;
17396
6fb120a7
JS
17397 /* The port is notified of the header region via a mailbox command. */
17398 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17399 if (!mboxq) {
17400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17401 "2001 Unable to allocate memory for issuing "
17402 "SLI_CONFIG_SPECIAL mailbox command\n");
17403 return -ENOMEM;
17404 }
17405
17406 /* Post all rpi memory regions to the port. */
17407 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17408 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17409 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17410 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17411 sizeof(struct lpfc_sli4_cfg_mhdr),
17412 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17413
17414
17415 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17416 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17417 rpi_page->start_rpi);
6d368e53
JS
17418 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17419 hdr_tmpl, rpi_page->page_count);
17420
6fb120a7
JS
17421 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17422 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17423 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17424 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17425 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17426 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17427 if (rc != MBX_TIMEOUT)
17428 mempool_free(mboxq, phba->mbox_mem_pool);
17429 if (shdr_status || shdr_add_status || rc) {
17430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17431 "2514 POST_RPI_HDR mailbox failed with "
17432 "status x%x add_status x%x, mbx status x%x\n",
17433 shdr_status, shdr_add_status, rc);
17434 rc = -ENXIO;
845d9e8d
JS
17435 } else {
17436 /*
17437 * The next_rpi stores the next logical module-64 rpi value used
17438 * to post physical rpis in subsequent rpi postings.
17439 */
17440 spin_lock_irq(&phba->hbalock);
17441 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17442 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17443 }
17444 return rc;
17445}
17446
17447/**
17448 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17449 * @phba: pointer to lpfc hba data structure.
17450 *
17451 * This routine is invoked to post rpi header templates to the
17452 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17453 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17454 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17455 *
17456 * Returns
af901ca1 17457 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17458 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17459 **/
17460int
17461lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17462{
6d368e53
JS
17463 unsigned long rpi;
17464 uint16_t max_rpi, rpi_limit;
17465 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17466 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17467 unsigned long iflag;
6fb120a7 17468
6fb120a7 17469 /*
6d368e53
JS
17470 * Fetch the next logical rpi. Because this index is logical,
17471 * the driver starts at 0 each time.
6fb120a7 17472 */
4902b381 17473 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17474 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17475 rpi_limit = phba->sli4_hba.next_rpi;
17476
6d368e53
JS
17477 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17478 if (rpi >= rpi_limit)
6fb120a7
JS
17479 rpi = LPFC_RPI_ALLOC_ERROR;
17480 else {
17481 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17482 phba->sli4_hba.max_cfg_param.rpi_used++;
17483 phba->sli4_hba.rpi_count++;
17484 }
be6bb941
JS
17485 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17486 "0001 rpi:%x max:%x lim:%x\n",
17487 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17488
17489 /*
17490 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17491 * has been exhausted.
6fb120a7
JS
17492 */
17493 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17494 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17495 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17496 return rpi;
17497 }
17498
6d368e53
JS
17499 /*
17500 * RPI header postings are not required for SLI4 ports capable of
17501 * extents.
17502 */
17503 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17504 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17505 return rpi;
17506 }
17507
6fb120a7
JS
17508 /*
17509 * If the driver is running low on rpi resources, allocate another
17510 * page now. Note that the next_rpi value is used because
17511 * it represents how many are actually in use whereas max_rpi notes
17512 * how many are supported max by the device.
17513 */
6d368e53 17514 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17515 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17516 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17517 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17518 if (!rpi_hdr) {
17519 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17520 "2002 Error Could not grow rpi "
17521 "count\n");
17522 } else {
6d368e53
JS
17523 lrpi = rpi_hdr->start_rpi;
17524 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17525 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17526 }
17527 }
17528
17529 return rpi;
17530}
17531
d7c47992
JS
17532/**
17533 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17534 * @phba: pointer to lpfc hba data structure.
17535 *
17536 * This routine is invoked to release an rpi to the pool of
17537 * available rpis maintained by the driver.
17538 **/
5d8b8167 17539static void
d7c47992
JS
17540__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17541{
17542 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17543 phba->sli4_hba.rpi_count--;
17544 phba->sli4_hba.max_cfg_param.rpi_used--;
17545 }
17546}
17547
6fb120a7
JS
17548/**
17549 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17550 * @phba: pointer to lpfc hba data structure.
17551 *
17552 * This routine is invoked to release an rpi to the pool of
17553 * available rpis maintained by the driver.
17554 **/
17555void
17556lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17557{
17558 spin_lock_irq(&phba->hbalock);
d7c47992 17559 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17560 spin_unlock_irq(&phba->hbalock);
17561}
17562
17563/**
17564 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17565 * @phba: pointer to lpfc hba data structure.
17566 *
17567 * This routine is invoked to remove the memory region that
17568 * provided rpi via a bitmask.
17569 **/
17570void
17571lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17572{
17573 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17574 kfree(phba->sli4_hba.rpi_ids);
17575 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17576}
17577
17578/**
17579 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17580 * @phba: pointer to lpfc hba data structure.
17581 *
17582 * This routine is invoked to remove the memory region that
17583 * provided rpi via a bitmask.
17584 **/
17585int
6b5151fd
JS
17586lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17587 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17588{
17589 LPFC_MBOXQ_t *mboxq;
17590 struct lpfc_hba *phba = ndlp->phba;
17591 int rc;
17592
17593 /* The port is notified of the header region via a mailbox command. */
17594 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17595 if (!mboxq)
17596 return -ENOMEM;
17597
17598 /* Post all rpi memory regions to the port. */
17599 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17600 if (cmpl) {
17601 mboxq->mbox_cmpl = cmpl;
17602 mboxq->context1 = arg;
17603 mboxq->context2 = ndlp;
72859909
JS
17604 } else
17605 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17606 mboxq->vport = ndlp->vport;
6fb120a7
JS
17607 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17608 if (rc == MBX_NOT_FINISHED) {
17609 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17610 "2010 Resume RPI Mailbox failed "
17611 "status %d, mbxStatus x%x\n", rc,
17612 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17613 mempool_free(mboxq, phba->mbox_mem_pool);
17614 return -EIO;
17615 }
17616 return 0;
17617}
17618
17619/**
17620 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17621 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17622 *
76a95d75 17623 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17624 *
17625 * Returns:
17626 * 0 success
17627 * -Evalue otherwise
17628 **/
17629int
76a95d75 17630lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17631{
17632 LPFC_MBOXQ_t *mboxq;
17633 int rc = 0;
6a9c52cf 17634 int retval = MBX_SUCCESS;
6fb120a7 17635 uint32_t mbox_tmo;
76a95d75 17636 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17637 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17638 if (!mboxq)
17639 return -ENOMEM;
76a95d75 17640 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17641 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17642 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17643 if (rc != MBX_SUCCESS) {
76a95d75 17644 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17645 "2022 INIT VPI Mailbox failed "
17646 "status %d, mbxStatus x%x\n", rc,
17647 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17648 retval = -EIO;
6fb120a7 17649 }
6a9c52cf 17650 if (rc != MBX_TIMEOUT)
76a95d75 17651 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17652
17653 return retval;
6fb120a7
JS
17654}
17655
17656/**
17657 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17658 * @phba: pointer to lpfc hba data structure.
17659 * @mboxq: Pointer to mailbox object.
17660 *
17661 * This routine is invoked to manually add a single FCF record. The caller
17662 * must pass a completely initialized FCF_Record. This routine takes
17663 * care of the nonembedded mailbox operations.
17664 **/
17665static void
17666lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17667{
17668 void *virt_addr;
17669 union lpfc_sli4_cfg_shdr *shdr;
17670 uint32_t shdr_status, shdr_add_status;
17671
17672 virt_addr = mboxq->sge_array->addr[0];
17673 /* The IOCTL status is embedded in the mailbox subheader. */
17674 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17675 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17676 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17677
17678 if ((shdr_status || shdr_add_status) &&
17679 (shdr_status != STATUS_FCF_IN_USE))
17680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17681 "2558 ADD_FCF_RECORD mailbox failed with "
17682 "status x%x add_status x%x\n",
17683 shdr_status, shdr_add_status);
17684
17685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17686}
17687
17688/**
17689 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17690 * @phba: pointer to lpfc hba data structure.
17691 * @fcf_record: pointer to the initialized fcf record to add.
17692 *
17693 * This routine is invoked to manually add a single FCF record. The caller
17694 * must pass a completely initialized FCF_Record. This routine takes
17695 * care of the nonembedded mailbox operations.
17696 **/
17697int
17698lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17699{
17700 int rc = 0;
17701 LPFC_MBOXQ_t *mboxq;
17702 uint8_t *bytep;
17703 void *virt_addr;
6fb120a7
JS
17704 struct lpfc_mbx_sge sge;
17705 uint32_t alloc_len, req_len;
17706 uint32_t fcfindex;
17707
17708 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17709 if (!mboxq) {
17710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17711 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17712 return -ENOMEM;
17713 }
17714
17715 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17716 sizeof(uint32_t);
17717
17718 /* Allocate DMA memory and set up the non-embedded mailbox command */
17719 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17720 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17721 req_len, LPFC_SLI4_MBX_NEMBED);
17722 if (alloc_len < req_len) {
17723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17724 "2523 Allocated DMA memory size (x%x) is "
17725 "less than the requested DMA memory "
17726 "size (x%x)\n", alloc_len, req_len);
17727 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17728 return -ENOMEM;
17729 }
17730
17731 /*
17732 * Get the first SGE entry from the non-embedded DMA memory. This
17733 * routine only uses a single SGE.
17734 */
17735 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
17736 virt_addr = mboxq->sge_array->addr[0];
17737 /*
17738 * Configure the FCF record for FCFI 0. This is the driver's
17739 * hardcoded default and gets used in nonFIP mode.
17740 */
17741 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17742 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17743 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17744
17745 /*
17746 * Copy the fcf_index and the FCF Record Data. The data starts after
17747 * the FCoE header plus word10. The data copy needs to be endian
17748 * correct.
17749 */
17750 bytep += sizeof(uint32_t);
17751 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17752 mboxq->vport = phba->pport;
17753 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17754 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17755 if (rc == MBX_NOT_FINISHED) {
17756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17757 "2515 ADD_FCF_RECORD mailbox failed with "
17758 "status 0x%x\n", rc);
17759 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17760 rc = -EIO;
17761 } else
17762 rc = 0;
17763
17764 return rc;
17765}
17766
17767/**
17768 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17769 * @phba: pointer to lpfc hba data structure.
17770 * @fcf_record: pointer to the fcf record to write the default data.
17771 * @fcf_index: FCF table entry index.
17772 *
17773 * This routine is invoked to build the driver's default FCF record. The
17774 * values used are hardcoded. This routine handles memory initialization.
17775 *
17776 **/
17777void
17778lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17779 struct fcf_record *fcf_record,
17780 uint16_t fcf_index)
17781{
17782 memset(fcf_record, 0, sizeof(struct fcf_record));
17783 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17784 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17785 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17786 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17787 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17788 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17789 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17790 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17791 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17792 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17793 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17794 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17795 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 17796 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
17797 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17798 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17799 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17800 /* Set the VLAN bit map */
17801 if (phba->valid_vlan) {
17802 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17803 = 1 << (phba->vlan_id % 8);
17804 }
17805}
17806
17807/**
0c9ab6f5 17808 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
17809 * @phba: pointer to lpfc hba data structure.
17810 * @fcf_index: FCF table entry offset.
17811 *
0c9ab6f5
JS
17812 * This routine is invoked to scan the entire FCF table by reading FCF
17813 * record and processing it one at a time starting from the @fcf_index
17814 * for initial FCF discovery or fast FCF failover rediscovery.
17815 *
25985edc 17816 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 17817 * otherwise.
6fb120a7
JS
17818 **/
17819int
0c9ab6f5 17820lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
17821{
17822 int rc = 0, error;
17823 LPFC_MBOXQ_t *mboxq;
6fb120a7 17824
32b9793f 17825 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 17826 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
17827 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17828 if (!mboxq) {
17829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17830 "2000 Failed to allocate mbox for "
17831 "READ_FCF cmd\n");
4d9ab994 17832 error = -ENOMEM;
0c9ab6f5 17833 goto fail_fcf_scan;
6fb120a7 17834 }
ecfd03c6 17835 /* Construct the read FCF record mailbox command */
0c9ab6f5 17836 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
17837 if (rc) {
17838 error = -EINVAL;
0c9ab6f5 17839 goto fail_fcf_scan;
6fb120a7 17840 }
ecfd03c6 17841 /* Issue the mailbox command asynchronously */
6fb120a7 17842 mboxq->vport = phba->pport;
0c9ab6f5 17843 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
17844
17845 spin_lock_irq(&phba->hbalock);
17846 phba->hba_flag |= FCF_TS_INPROG;
17847 spin_unlock_irq(&phba->hbalock);
17848
6fb120a7 17849 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 17850 if (rc == MBX_NOT_FINISHED)
6fb120a7 17851 error = -EIO;
ecfd03c6 17852 else {
38b92ef8
JS
17853 /* Reset eligible FCF count for new scan */
17854 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 17855 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 17856 error = 0;
32b9793f 17857 }
0c9ab6f5 17858fail_fcf_scan:
4d9ab994
JS
17859 if (error) {
17860 if (mboxq)
17861 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 17862 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 17863 spin_lock_irq(&phba->hbalock);
a93ff37a 17864 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
17865 spin_unlock_irq(&phba->hbalock);
17866 }
6fb120a7
JS
17867 return error;
17868}
a0c87cbd 17869
0c9ab6f5 17870/**
a93ff37a 17871 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
17872 * @phba: pointer to lpfc hba data structure.
17873 * @fcf_index: FCF table entry offset.
17874 *
17875 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 17876 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 17877 *
25985edc 17878 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17879 * otherwise.
17880 **/
17881int
17882lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17883{
17884 int rc = 0, error;
17885 LPFC_MBOXQ_t *mboxq;
17886
17887 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17888 if (!mboxq) {
17889 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17890 "2763 Failed to allocate mbox for "
17891 "READ_FCF cmd\n");
17892 error = -ENOMEM;
17893 goto fail_fcf_read;
17894 }
17895 /* Construct the read FCF record mailbox command */
17896 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17897 if (rc) {
17898 error = -EINVAL;
17899 goto fail_fcf_read;
17900 }
17901 /* Issue the mailbox command asynchronously */
17902 mboxq->vport = phba->pport;
17903 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17904 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17905 if (rc == MBX_NOT_FINISHED)
17906 error = -EIO;
17907 else
17908 error = 0;
17909
17910fail_fcf_read:
17911 if (error && mboxq)
17912 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17913 return error;
17914}
17915
17916/**
17917 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17918 * @phba: pointer to lpfc hba data structure.
17919 * @fcf_index: FCF table entry offset.
17920 *
17921 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 17922 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 17923 *
25985edc 17924 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17925 * otherwise.
17926 **/
17927int
17928lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17929{
17930 int rc = 0, error;
17931 LPFC_MBOXQ_t *mboxq;
17932
17933 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17934 if (!mboxq) {
17935 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17936 "2758 Failed to allocate mbox for "
17937 "READ_FCF cmd\n");
17938 error = -ENOMEM;
17939 goto fail_fcf_read;
17940 }
17941 /* Construct the read FCF record mailbox command */
17942 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17943 if (rc) {
17944 error = -EINVAL;
17945 goto fail_fcf_read;
17946 }
17947 /* Issue the mailbox command asynchronously */
17948 mboxq->vport = phba->pport;
17949 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17950 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17951 if (rc == MBX_NOT_FINISHED)
17952 error = -EIO;
17953 else
17954 error = 0;
17955
17956fail_fcf_read:
17957 if (error && mboxq)
17958 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17959 return error;
17960}
17961
7d791df7 17962/**
f5cb5304 17963 * lpfc_check_next_fcf_pri_level
7d791df7
JS
17964 * phba pointer to the lpfc_hba struct for this port.
17965 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17966 * routine when the rr_bmask is empty. The FCF indecies are put into the
17967 * rr_bmask based on their priority level. Starting from the highest priority
17968 * to the lowest. The most likely FCF candidate will be in the highest
17969 * priority group. When this routine is called it searches the fcf_pri list for
17970 * next lowest priority group and repopulates the rr_bmask with only those
17971 * fcf_indexes.
17972 * returns:
17973 * 1=success 0=failure
17974 **/
5d8b8167 17975static int
7d791df7
JS
17976lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17977{
17978 uint16_t next_fcf_pri;
17979 uint16_t last_index;
17980 struct lpfc_fcf_pri *fcf_pri;
17981 int rc;
17982 int ret = 0;
17983
17984 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17985 LPFC_SLI4_FCF_TBL_INDX_MAX);
17986 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17987 "3060 Last IDX %d\n", last_index);
2562669c
JS
17988
17989 /* Verify the priority list has 2 or more entries */
17990 spin_lock_irq(&phba->hbalock);
17991 if (list_empty(&phba->fcf.fcf_pri_list) ||
17992 list_is_singular(&phba->fcf.fcf_pri_list)) {
17993 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
17994 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17995 "3061 Last IDX %d\n", last_index);
17996 return 0; /* Empty rr list */
17997 }
2562669c
JS
17998 spin_unlock_irq(&phba->hbalock);
17999
7d791df7
JS
18000 next_fcf_pri = 0;
18001 /*
18002 * Clear the rr_bmask and set all of the bits that are at this
18003 * priority.
18004 */
18005 memset(phba->fcf.fcf_rr_bmask, 0,
18006 sizeof(*phba->fcf.fcf_rr_bmask));
18007 spin_lock_irq(&phba->hbalock);
18008 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18009 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18010 continue;
18011 /*
18012 * the 1st priority that has not FLOGI failed
18013 * will be the highest.
18014 */
18015 if (!next_fcf_pri)
18016 next_fcf_pri = fcf_pri->fcf_rec.priority;
18017 spin_unlock_irq(&phba->hbalock);
18018 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18019 rc = lpfc_sli4_fcf_rr_index_set(phba,
18020 fcf_pri->fcf_rec.fcf_index);
18021 if (rc)
18022 return 0;
18023 }
18024 spin_lock_irq(&phba->hbalock);
18025 }
18026 /*
18027 * if next_fcf_pri was not set above and the list is not empty then
18028 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18029 * and start at the beginning.
7d791df7
JS
18030 */
18031 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18032 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18033 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18034 /*
18035 * the 1st priority that has not FLOGI failed
18036 * will be the highest.
18037 */
18038 if (!next_fcf_pri)
18039 next_fcf_pri = fcf_pri->fcf_rec.priority;
18040 spin_unlock_irq(&phba->hbalock);
18041 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18042 rc = lpfc_sli4_fcf_rr_index_set(phba,
18043 fcf_pri->fcf_rec.fcf_index);
18044 if (rc)
18045 return 0;
18046 }
18047 spin_lock_irq(&phba->hbalock);
18048 }
18049 } else
18050 ret = 1;
18051 spin_unlock_irq(&phba->hbalock);
18052
18053 return ret;
18054}
0c9ab6f5
JS
18055/**
18056 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18057 * @phba: pointer to lpfc hba data structure.
18058 *
18059 * This routine is to get the next eligible FCF record index in a round
18060 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18061 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18062 * shall be returned, otherwise, the next eligible FCF record's index
18063 * shall be returned.
18064 **/
18065uint16_t
18066lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18067{
18068 uint16_t next_fcf_index;
18069
421c6622 18070initial_priority:
3804dc84 18071 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18072 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18073
7d791df7 18074next_priority:
421c6622
JS
18075 /* Determine the next fcf index to check */
18076 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18077 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18078 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18079 next_fcf_index);
18080
0c9ab6f5 18081 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18082 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18083 /*
18084 * If we have wrapped then we need to clear the bits that
18085 * have been tested so that we can detect when we should
18086 * change the priority level.
18087 */
0c9ab6f5
JS
18088 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18089 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18090 }
18091
3804dc84
JS
18092
18093 /* Check roundrobin failover list empty condition */
7d791df7
JS
18094 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18095 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18096 /*
18097 * If next fcf index is not found check if there are lower
18098 * Priority level fcf's in the fcf_priority list.
18099 * Set up the rr_bmask with all of the avaiable fcf bits
18100 * at that level and continue the selection process.
18101 */
18102 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18103 goto initial_priority;
3804dc84
JS
18104 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18105 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18106 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18107 return LPFC_FCOE_FCF_NEXT_NONE;
18108 else {
18109 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18110 "3063 Only FCF available idx %d, flag %x\n",
18111 next_fcf_index,
18112 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18113 return next_fcf_index;
18114 }
3804dc84
JS
18115 }
18116
7d791df7
JS
18117 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18118 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18119 LPFC_FCF_FLOGI_FAILED) {
18120 if (list_is_singular(&phba->fcf.fcf_pri_list))
18121 return LPFC_FCOE_FCF_NEXT_NONE;
18122
7d791df7 18123 goto next_priority;
f5cb5304 18124 }
7d791df7 18125
3804dc84 18126 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18127 "2845 Get next roundrobin failover FCF (x%x)\n",
18128 next_fcf_index);
18129
0c9ab6f5
JS
18130 return next_fcf_index;
18131}
18132
18133/**
18134 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18135 * @phba: pointer to lpfc hba data structure.
18136 *
18137 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18138 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18139 * does not go beyond the range of the driver allocated bmask dimension
18140 * before setting the bit.
18141 *
18142 * Returns 0 if the index bit successfully set, otherwise, it returns
18143 * -EINVAL.
18144 **/
18145int
18146lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18147{
18148 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18149 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18150 "2610 FCF (x%x) reached driver's book "
18151 "keeping dimension:x%x\n",
0c9ab6f5
JS
18152 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18153 return -EINVAL;
18154 }
18155 /* Set the eligible FCF record index bmask */
18156 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18157
3804dc84 18158 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18159 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18160 "bmask\n", fcf_index);
18161
0c9ab6f5
JS
18162 return 0;
18163}
18164
18165/**
3804dc84 18166 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18167 * @phba: pointer to lpfc hba data structure.
18168 *
18169 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18170 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18171 * does not go beyond the range of the driver allocated bmask dimension
18172 * before clearing the bit.
18173 **/
18174void
18175lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18176{
9a803a74 18177 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18178 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18179 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18180 "2762 FCF (x%x) reached driver's book "
18181 "keeping dimension:x%x\n",
0c9ab6f5
JS
18182 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18183 return;
18184 }
18185 /* Clear the eligible FCF record index bmask */
7d791df7 18186 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18187 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18188 list) {
7d791df7
JS
18189 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18190 list_del_init(&fcf_pri->list);
18191 break;
18192 }
18193 }
18194 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18195 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18196
18197 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18198 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18199 "bmask\n", fcf_index);
0c9ab6f5
JS
18200}
18201
ecfd03c6
JS
18202/**
18203 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18204 * @phba: pointer to lpfc hba data structure.
18205 *
18206 * This routine is the completion routine for the rediscover FCF table mailbox
18207 * command. If the mailbox command returned failure, it will try to stop the
18208 * FCF rediscover wait timer.
18209 **/
5d8b8167 18210static void
ecfd03c6
JS
18211lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18212{
18213 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18214 uint32_t shdr_status, shdr_add_status;
18215
18216 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18217
18218 shdr_status = bf_get(lpfc_mbox_hdr_status,
18219 &redisc_fcf->header.cfg_shdr.response);
18220 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18221 &redisc_fcf->header.cfg_shdr.response);
18222 if (shdr_status || shdr_add_status) {
0c9ab6f5 18223 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18224 "2746 Requesting for FCF rediscovery failed "
18225 "status x%x add_status x%x\n",
18226 shdr_status, shdr_add_status);
0c9ab6f5 18227 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18228 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18229 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18230 spin_unlock_irq(&phba->hbalock);
18231 /*
18232 * CVL event triggered FCF rediscover request failed,
18233 * last resort to re-try current registered FCF entry.
18234 */
18235 lpfc_retry_pport_discovery(phba);
18236 } else {
18237 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18238 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18239 spin_unlock_irq(&phba->hbalock);
18240 /*
18241 * DEAD FCF event triggered FCF rediscover request
18242 * failed, last resort to fail over as a link down
18243 * to FCF registration.
18244 */
18245 lpfc_sli4_fcf_dead_failthrough(phba);
18246 }
0c9ab6f5
JS
18247 } else {
18248 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18249 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18250 /*
18251 * Start FCF rediscovery wait timer for pending FCF
18252 * before rescan FCF record table.
18253 */
18254 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18255 }
ecfd03c6
JS
18256
18257 mempool_free(mbox, phba->mbox_mem_pool);
18258}
18259
18260/**
3804dc84 18261 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18262 * @phba: pointer to lpfc hba data structure.
18263 *
18264 * This routine is invoked to request for rediscovery of the entire FCF table
18265 * by the port.
18266 **/
18267int
18268lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18269{
18270 LPFC_MBOXQ_t *mbox;
18271 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18272 int rc, length;
18273
0c9ab6f5
JS
18274 /* Cancel retry delay timers to all vports before FCF rediscover */
18275 lpfc_cancel_all_vport_retry_delay_timer(phba);
18276
ecfd03c6
JS
18277 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18278 if (!mbox) {
18279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18280 "2745 Failed to allocate mbox for "
18281 "requesting FCF rediscover.\n");
18282 return -ENOMEM;
18283 }
18284
18285 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18286 sizeof(struct lpfc_sli4_cfg_mhdr));
18287 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18288 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18289 length, LPFC_SLI4_MBX_EMBED);
18290
18291 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18292 /* Set count to 0 for invalidating the entire FCF database */
18293 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18294
18295 /* Issue the mailbox command asynchronously */
18296 mbox->vport = phba->pport;
18297 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18298 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18299
18300 if (rc == MBX_NOT_FINISHED) {
18301 mempool_free(mbox, phba->mbox_mem_pool);
18302 return -EIO;
18303 }
18304 return 0;
18305}
18306
fc2b989b
JS
18307/**
18308 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18309 * @phba: pointer to lpfc hba data structure.
18310 *
18311 * This function is the failover routine as a last resort to the FCF DEAD
18312 * event when driver failed to perform fast FCF failover.
18313 **/
18314void
18315lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18316{
18317 uint32_t link_state;
18318
18319 /*
18320 * Last resort as FCF DEAD event failover will treat this as
18321 * a link down, but save the link state because we don't want
18322 * it to be changed to Link Down unless it is already down.
18323 */
18324 link_state = phba->link_state;
18325 lpfc_linkdown(phba);
18326 phba->link_state = link_state;
18327
18328 /* Unregister FCF if no devices connected to it */
18329 lpfc_unregister_unused_fcf(phba);
18330}
18331
a0c87cbd 18332/**
026abb87 18333 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18334 * @phba: pointer to lpfc hba data structure.
026abb87 18335 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18336 *
026abb87
JS
18337 * This function gets SLI3 port configure region 23 data through memory dump
18338 * mailbox command. When it successfully retrieves data, the size of the data
18339 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18340 **/
026abb87
JS
18341static uint32_t
18342lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18343{
18344 LPFC_MBOXQ_t *pmb = NULL;
18345 MAILBOX_t *mb;
026abb87 18346 uint32_t offset = 0;
a0c87cbd
JS
18347 int rc;
18348
026abb87
JS
18349 if (!rgn23_data)
18350 return 0;
18351
a0c87cbd
JS
18352 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18353 if (!pmb) {
18354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18355 "2600 failed to allocate mailbox memory\n");
18356 return 0;
a0c87cbd
JS
18357 }
18358 mb = &pmb->u.mb;
18359
a0c87cbd
JS
18360 do {
18361 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18362 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18363
18364 if (rc != MBX_SUCCESS) {
18365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18366 "2601 failed to read config "
18367 "region 23, rc 0x%x Status 0x%x\n",
18368 rc, mb->mbxStatus);
a0c87cbd
JS
18369 mb->un.varDmp.word_cnt = 0;
18370 }
18371 /*
18372 * dump mem may return a zero when finished or we got a
18373 * mailbox error, either way we are done.
18374 */
18375 if (mb->un.varDmp.word_cnt == 0)
18376 break;
18377 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18378 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18379
18380 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18381 rgn23_data + offset,
18382 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18383 offset += mb->un.varDmp.word_cnt;
18384 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18385
026abb87
JS
18386 mempool_free(pmb, phba->mbox_mem_pool);
18387 return offset;
18388}
18389
18390/**
18391 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18392 * @phba: pointer to lpfc hba data structure.
18393 * @rgn23_data: pointer to configure region 23 data.
18394 *
18395 * This function gets SLI4 port configure region 23 data through memory dump
18396 * mailbox command. When it successfully retrieves data, the size of the data
18397 * will be returned, otherwise, 0 will be returned.
18398 **/
18399static uint32_t
18400lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18401{
18402 LPFC_MBOXQ_t *mboxq = NULL;
18403 struct lpfc_dmabuf *mp = NULL;
18404 struct lpfc_mqe *mqe;
18405 uint32_t data_length = 0;
18406 int rc;
18407
18408 if (!rgn23_data)
18409 return 0;
18410
18411 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18412 if (!mboxq) {
18413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18414 "3105 failed to allocate mailbox memory\n");
18415 return 0;
18416 }
18417
18418 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18419 goto out;
18420 mqe = &mboxq->u.mqe;
18421 mp = (struct lpfc_dmabuf *) mboxq->context1;
18422 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18423 if (rc)
18424 goto out;
18425 data_length = mqe->un.mb_words[5];
18426 if (data_length == 0)
18427 goto out;
18428 if (data_length > DMP_RGN23_SIZE) {
18429 data_length = 0;
18430 goto out;
18431 }
18432 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18433out:
18434 mempool_free(mboxq, phba->mbox_mem_pool);
18435 if (mp) {
18436 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18437 kfree(mp);
18438 }
18439 return data_length;
18440}
18441
18442/**
18443 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18444 * @phba: pointer to lpfc hba data structure.
18445 *
18446 * This function read region 23 and parse TLV for port status to
18447 * decide if the user disaled the port. If the TLV indicates the
18448 * port is disabled, the hba_flag is set accordingly.
18449 **/
18450void
18451lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18452{
18453 uint8_t *rgn23_data = NULL;
18454 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18455 uint32_t offset = 0;
18456
18457 /* Get adapter Region 23 data */
18458 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18459 if (!rgn23_data)
18460 goto out;
18461
18462 if (phba->sli_rev < LPFC_SLI_REV4)
18463 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18464 else {
18465 if_type = bf_get(lpfc_sli_intf_if_type,
18466 &phba->sli4_hba.sli_intf);
18467 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18468 goto out;
18469 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18470 }
a0c87cbd
JS
18471
18472 if (!data_size)
18473 goto out;
18474
18475 /* Check the region signature first */
18476 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18478 "2619 Config region 23 has bad signature\n");
18479 goto out;
18480 }
18481 offset += 4;
18482
18483 /* Check the data structure version */
18484 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18486 "2620 Config region 23 has bad version\n");
18487 goto out;
18488 }
18489 offset += 4;
18490
18491 /* Parse TLV entries in the region */
18492 while (offset < data_size) {
18493 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18494 break;
18495 /*
18496 * If the TLV is not driver specific TLV or driver id is
18497 * not linux driver id, skip the record.
18498 */
18499 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18500 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18501 (rgn23_data[offset + 3] != 0)) {
18502 offset += rgn23_data[offset + 1] * 4 + 4;
18503 continue;
18504 }
18505
18506 /* Driver found a driver specific TLV in the config region */
18507 sub_tlv_len = rgn23_data[offset + 1] * 4;
18508 offset += 4;
18509 tlv_offset = 0;
18510
18511 /*
18512 * Search for configured port state sub-TLV.
18513 */
18514 while ((offset < data_size) &&
18515 (tlv_offset < sub_tlv_len)) {
18516 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18517 offset += 4;
18518 tlv_offset += 4;
18519 break;
18520 }
18521 if (rgn23_data[offset] != PORT_STE_TYPE) {
18522 offset += rgn23_data[offset + 1] * 4 + 4;
18523 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18524 continue;
18525 }
18526
18527 /* This HBA contains PORT_STE configured */
18528 if (!rgn23_data[offset + 2])
18529 phba->hba_flag |= LINK_DISABLED;
18530
18531 goto out;
18532 }
18533 }
026abb87 18534
a0c87cbd 18535out:
a0c87cbd
JS
18536 kfree(rgn23_data);
18537 return;
18538}
695a814e 18539
52d52440
JS
18540/**
18541 * lpfc_wr_object - write an object to the firmware
18542 * @phba: HBA structure that indicates port to create a queue on.
18543 * @dmabuf_list: list of dmabufs to write to the port.
18544 * @size: the total byte value of the objects to write to the port.
18545 * @offset: the current offset to be used to start the transfer.
18546 *
18547 * This routine will create a wr_object mailbox command to send to the port.
18548 * the mailbox command will be constructed using the dma buffers described in
18549 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18550 * BDEs that the imbedded mailbox can support. The @offset variable will be
18551 * used to indicate the starting offset of the transfer and will also return
18552 * the offset after the write object mailbox has completed. @size is used to
18553 * determine the end of the object and whether the eof bit should be set.
18554 *
18555 * Return 0 is successful and offset will contain the the new offset to use
18556 * for the next write.
18557 * Return negative value for error cases.
18558 **/
18559int
18560lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18561 uint32_t size, uint32_t *offset)
18562{
18563 struct lpfc_mbx_wr_object *wr_object;
18564 LPFC_MBOXQ_t *mbox;
18565 int rc = 0, i = 0;
18566 uint32_t shdr_status, shdr_add_status;
18567 uint32_t mbox_tmo;
18568 union lpfc_sli4_cfg_shdr *shdr;
18569 struct lpfc_dmabuf *dmabuf;
18570 uint32_t written = 0;
18571
18572 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18573 if (!mbox)
18574 return -ENOMEM;
18575
18576 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18577 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18578 sizeof(struct lpfc_mbx_wr_object) -
18579 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18580
18581 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18582 wr_object->u.request.write_offset = *offset;
18583 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18584 wr_object->u.request.object_name[0] =
18585 cpu_to_le32(wr_object->u.request.object_name[0]);
18586 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18587 list_for_each_entry(dmabuf, dmabuf_list, list) {
18588 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18589 break;
18590 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18591 wr_object->u.request.bde[i].addrHigh =
18592 putPaddrHigh(dmabuf->phys);
18593 if (written + SLI4_PAGE_SIZE >= size) {
18594 wr_object->u.request.bde[i].tus.f.bdeSize =
18595 (size - written);
18596 written += (size - written);
18597 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18598 } else {
18599 wr_object->u.request.bde[i].tus.f.bdeSize =
18600 SLI4_PAGE_SIZE;
18601 written += SLI4_PAGE_SIZE;
18602 }
18603 i++;
18604 }
18605 wr_object->u.request.bde_count = i;
18606 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18607 if (!phba->sli4_hba.intr_enable)
18608 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18609 else {
a183a15f 18610 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18611 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18612 }
18613 /* The IOCTL status is embedded in the mailbox subheader. */
18614 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18615 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18616 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18617 if (rc != MBX_TIMEOUT)
18618 mempool_free(mbox, phba->mbox_mem_pool);
18619 if (shdr_status || shdr_add_status || rc) {
18620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18621 "3025 Write Object mailbox failed with "
18622 "status x%x add_status x%x, mbx status x%x\n",
18623 shdr_status, shdr_add_status, rc);
18624 rc = -ENXIO;
18625 } else
18626 *offset += wr_object->u.response.actual_write_length;
18627 return rc;
18628}
18629
695a814e
JS
18630/**
18631 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18632 * @vport: pointer to vport data structure.
18633 *
18634 * This function iterate through the mailboxq and clean up all REG_LOGIN
18635 * and REG_VPI mailbox commands associated with the vport. This function
18636 * is called when driver want to restart discovery of the vport due to
18637 * a Clear Virtual Link event.
18638 **/
18639void
18640lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18641{
18642 struct lpfc_hba *phba = vport->phba;
18643 LPFC_MBOXQ_t *mb, *nextmb;
18644 struct lpfc_dmabuf *mp;
78730cfe 18645 struct lpfc_nodelist *ndlp;
d439d286 18646 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18647 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18648 LIST_HEAD(mbox_cmd_list);
63e801ce 18649 uint8_t restart_loop;
695a814e 18650
d439d286 18651 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18652 spin_lock_irq(&phba->hbalock);
18653 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18654 if (mb->vport != vport)
18655 continue;
18656
18657 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18658 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18659 continue;
18660
d439d286
JS
18661 list_del(&mb->list);
18662 list_add_tail(&mb->list, &mbox_cmd_list);
18663 }
18664 /* Clean up active mailbox command with the vport */
18665 mb = phba->sli.mbox_active;
18666 if (mb && (mb->vport == vport)) {
18667 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18668 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18669 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18670 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18671 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18672 /* Put reference count for delayed processing */
18673 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18674 /* Unregister the RPI when mailbox complete */
18675 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18676 }
18677 }
63e801ce
JS
18678 /* Cleanup any mailbox completions which are not yet processed */
18679 do {
18680 restart_loop = 0;
18681 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18682 /*
18683 * If this mailox is already processed or it is
18684 * for another vport ignore it.
18685 */
18686 if ((mb->vport != vport) ||
18687 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18688 continue;
18689
18690 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18691 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18692 continue;
18693
18694 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18695 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18696 ndlp = (struct lpfc_nodelist *)mb->context2;
18697 /* Unregister the RPI when mailbox complete */
18698 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18699 restart_loop = 1;
18700 spin_unlock_irq(&phba->hbalock);
18701 spin_lock(shost->host_lock);
18702 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18703 spin_unlock(shost->host_lock);
18704 spin_lock_irq(&phba->hbalock);
18705 break;
18706 }
18707 }
18708 } while (restart_loop);
18709
d439d286
JS
18710 spin_unlock_irq(&phba->hbalock);
18711
18712 /* Release the cleaned-up mailbox commands */
18713 while (!list_empty(&mbox_cmd_list)) {
18714 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18715 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18716 mp = (struct lpfc_dmabuf *) (mb->context1);
18717 if (mp) {
18718 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18719 kfree(mp);
18720 }
78730cfe 18721 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18722 mb->context2 = NULL;
78730cfe 18723 if (ndlp) {
ec21b3b0 18724 spin_lock(shost->host_lock);
589a52d6 18725 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18726 spin_unlock(shost->host_lock);
78730cfe 18727 lpfc_nlp_put(ndlp);
78730cfe 18728 }
695a814e 18729 }
695a814e
JS
18730 mempool_free(mb, phba->mbox_mem_pool);
18731 }
d439d286
JS
18732
18733 /* Release the ndlp with the cleaned-up active mailbox command */
18734 if (act_mbx_ndlp) {
18735 spin_lock(shost->host_lock);
18736 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18737 spin_unlock(shost->host_lock);
18738 lpfc_nlp_put(act_mbx_ndlp);
695a814e 18739 }
695a814e
JS
18740}
18741
2a9bf3d0
JS
18742/**
18743 * lpfc_drain_txq - Drain the txq
18744 * @phba: Pointer to HBA context object.
18745 *
18746 * This function attempt to submit IOCBs on the txq
18747 * to the adapter. For SLI4 adapters, the txq contains
18748 * ELS IOCBs that have been deferred because the there
18749 * are no SGLs. This congestion can occur with large
18750 * vport counts during node discovery.
18751 **/
18752
18753uint32_t
18754lpfc_drain_txq(struct lpfc_hba *phba)
18755{
18756 LIST_HEAD(completions);
895427bd 18757 struct lpfc_sli_ring *pring;
2e706377 18758 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
18759 unsigned long iflags = 0;
18760 char *fail_msg = NULL;
18761 struct lpfc_sglq *sglq;
2f07784f
JS
18762 union lpfc_wqe128 wqe128;
18763 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 18764 uint32_t txq_cnt = 0;
2a9bf3d0 18765
895427bd 18766 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
18767 if (unlikely(!pring))
18768 return 0;
895427bd 18769
398d81c9 18770 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
18771 list_for_each_entry(piocbq, &pring->txq, list) {
18772 txq_cnt++;
18773 }
18774
18775 if (txq_cnt > pring->txq_max)
18776 pring->txq_max = txq_cnt;
2a9bf3d0 18777
398d81c9 18778 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18779
0e9bb8d7 18780 while (!list_empty(&pring->txq)) {
398d81c9 18781 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 18782
19ca7609 18783 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 18784 if (!piocbq) {
398d81c9 18785 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
18786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18787 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 18788 txq_cnt);
a629852a
JS
18789 break;
18790 }
895427bd 18791 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 18792 if (!sglq) {
19ca7609 18793 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 18794 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18795 break;
2a9bf3d0 18796 }
0e9bb8d7 18797 txq_cnt--;
2a9bf3d0
JS
18798
18799 /* The xri and iocb resources secured,
18800 * attempt to issue request
18801 */
6d368e53 18802 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
18803 piocbq->sli4_xritag = sglq->sli4_xritag;
18804 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18805 fail_msg = "to convert bpl to sgl";
2f07784f 18806 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 18807 fail_msg = "to convert iocb to wqe";
2f07784f 18808 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
18809 fail_msg = " - Wq is full";
18810 else
18811 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18812
18813 if (fail_msg) {
18814 /* Failed means we can't issue and need to cancel */
18815 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18816 "2822 IOCB failed %s iotag 0x%x "
18817 "xri 0x%x\n",
18818 fail_msg,
18819 piocbq->iotag, piocbq->sli4_xritag);
18820 list_add_tail(&piocbq->list, &completions);
18821 }
398d81c9 18822 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
18823 }
18824
2a9bf3d0
JS
18825 /* Cancel all the IOCBs that cannot be issued */
18826 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18827 IOERR_SLI_ABORTED);
18828
0e9bb8d7 18829 return txq_cnt;
2a9bf3d0 18830}
895427bd
JS
18831
18832/**
18833 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18834 * @phba: Pointer to HBA context object.
18835 * @pwqe: Pointer to command WQE.
18836 * @sglq: Pointer to the scatter gather queue object.
18837 *
18838 * This routine converts the bpl or bde that is in the WQE
18839 * to a sgl list for the sli4 hardware. The physical address
18840 * of the bpl/bde is converted back to a virtual address.
18841 * If the WQE contains a BPL then the list of BDE's is
18842 * converted to sli4_sge's. If the WQE contains a single
18843 * BDE then it is converted to a single sli_sge.
18844 * The WQE is still in cpu endianness so the contents of
18845 * the bpl can be used without byte swapping.
18846 *
18847 * Returns valid XRI = Success, NO_XRI = Failure.
18848 */
18849static uint16_t
18850lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18851 struct lpfc_sglq *sglq)
18852{
18853 uint16_t xritag = NO_XRI;
18854 struct ulp_bde64 *bpl = NULL;
18855 struct ulp_bde64 bde;
18856 struct sli4_sge *sgl = NULL;
18857 struct lpfc_dmabuf *dmabuf;
18858 union lpfc_wqe *wqe;
18859 int numBdes = 0;
18860 int i = 0;
18861 uint32_t offset = 0; /* accumulated offset in the sg request list */
18862 int inbound = 0; /* number of sg reply entries inbound from firmware */
18863 uint32_t cmd;
18864
18865 if (!pwqeq || !sglq)
18866 return xritag;
18867
18868 sgl = (struct sli4_sge *)sglq->sgl;
18869 wqe = &pwqeq->wqe;
18870 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18871
18872 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18873 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18874 return sglq->sli4_xritag;
18875 numBdes = pwqeq->rsvd2;
18876 if (numBdes) {
18877 /* The addrHigh and addrLow fields within the WQE
18878 * have not been byteswapped yet so there is no
18879 * need to swap them back.
18880 */
18881 if (pwqeq->context3)
18882 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18883 else
18884 return xritag;
18885
18886 bpl = (struct ulp_bde64 *)dmabuf->virt;
18887 if (!bpl)
18888 return xritag;
18889
18890 for (i = 0; i < numBdes; i++) {
18891 /* Should already be byte swapped. */
18892 sgl->addr_hi = bpl->addrHigh;
18893 sgl->addr_lo = bpl->addrLow;
18894
18895 sgl->word2 = le32_to_cpu(sgl->word2);
18896 if ((i+1) == numBdes)
18897 bf_set(lpfc_sli4_sge_last, sgl, 1);
18898 else
18899 bf_set(lpfc_sli4_sge_last, sgl, 0);
18900 /* swap the size field back to the cpu so we
18901 * can assign it to the sgl.
18902 */
18903 bde.tus.w = le32_to_cpu(bpl->tus.w);
18904 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18905 /* The offsets in the sgl need to be accumulated
18906 * separately for the request and reply lists.
18907 * The request is always first, the reply follows.
18908 */
18909 switch (cmd) {
18910 case CMD_GEN_REQUEST64_WQE:
18911 /* add up the reply sg entries */
18912 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18913 inbound++;
18914 /* first inbound? reset the offset */
18915 if (inbound == 1)
18916 offset = 0;
18917 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18918 bf_set(lpfc_sli4_sge_type, sgl,
18919 LPFC_SGE_TYPE_DATA);
18920 offset += bde.tus.f.bdeSize;
18921 break;
18922 case CMD_FCP_TRSP64_WQE:
18923 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18924 bf_set(lpfc_sli4_sge_type, sgl,
18925 LPFC_SGE_TYPE_DATA);
18926 break;
18927 case CMD_FCP_TSEND64_WQE:
18928 case CMD_FCP_TRECEIVE64_WQE:
18929 bf_set(lpfc_sli4_sge_type, sgl,
18930 bpl->tus.f.bdeFlags);
18931 if (i < 3)
18932 offset = 0;
18933 else
18934 offset += bde.tus.f.bdeSize;
18935 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18936 break;
18937 }
18938 sgl->word2 = cpu_to_le32(sgl->word2);
18939 bpl++;
18940 sgl++;
18941 }
18942 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18943 /* The addrHigh and addrLow fields of the BDE have not
18944 * been byteswapped yet so they need to be swapped
18945 * before putting them in the sgl.
18946 */
18947 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18948 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18949 sgl->word2 = le32_to_cpu(sgl->word2);
18950 bf_set(lpfc_sli4_sge_last, sgl, 1);
18951 sgl->word2 = cpu_to_le32(sgl->word2);
18952 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18953 }
18954 return sglq->sli4_xritag;
18955}
18956
18957/**
18958 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18959 * @phba: Pointer to HBA context object.
18960 * @ring_number: Base sli ring number
18961 * @pwqe: Pointer to command WQE.
18962 **/
18963int
18964lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18965 struct lpfc_iocbq *pwqe)
18966{
18967 union lpfc_wqe *wqe = &pwqe->wqe;
f358dd0c 18968 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
18969 struct lpfc_queue *wq;
18970 struct lpfc_sglq *sglq;
18971 struct lpfc_sli_ring *pring;
18972 unsigned long iflags;
cd22d605 18973 uint32_t ret = 0;
895427bd
JS
18974
18975 /* NVME_LS and NVME_LS ABTS requests. */
18976 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18977 pring = phba->sli4_hba.nvmels_wq->pring;
18978 spin_lock_irqsave(&pring->ring_lock, iflags);
18979 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18980 if (!sglq) {
18981 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18982 return WQE_BUSY;
18983 }
18984 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18985 pwqe->sli4_xritag = sglq->sli4_xritag;
18986 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18987 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18988 return WQE_ERROR;
18989 }
18990 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18991 pwqe->sli4_xritag);
cd22d605
DK
18992 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
18993 if (ret) {
895427bd 18994 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 18995 return ret;
895427bd 18996 }
cd22d605 18997
895427bd
JS
18998 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18999 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19000 return 0;
19001 }
19002
19003 /* NVME_FCREQ and NVME_ABTS requests */
19004 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19005 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19006 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19007
19008 spin_lock_irqsave(&pring->ring_lock, iflags);
19009 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19010 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19011 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19012 ret = lpfc_sli4_wq_put(wq, wqe);
19013 if (ret) {
895427bd 19014 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19015 return ret;
895427bd
JS
19016 }
19017 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19018 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19019 return 0;
19020 }
19021
f358dd0c
JS
19022 /* NVMET requests */
19023 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19024 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19025 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19026
19027 spin_lock_irqsave(&pring->ring_lock, iflags);
19028 ctxp = pwqe->context2;
6c621a22 19029 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19030 if (pwqe->sli4_xritag == NO_XRI) {
19031 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19032 pwqe->sli4_xritag = sglq->sli4_xritag;
19033 }
19034 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19035 pwqe->sli4_xritag);
19036 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19037 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19038 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19039 ret = lpfc_sli4_wq_put(wq, wqe);
19040 if (ret) {
f358dd0c 19041 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19042 return ret;
f358dd0c
JS
19043 }
19044 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19045 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19046 return 0;
19047 }
895427bd
JS
19048 return WQE_ERROR;
19049}