]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix SCSI lun discovery when port configured for both SCSI and NVME
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
128bddac 4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
d080abe0 5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
50611577 6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
c44ce173 9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
10 * *
11 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea3101e
JB
22 *******************************************************************/
23
dea3101e
JB
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
5a0e3ad6 28#include <linux/slab.h>
1c2ba475 29#include <linux/lockdep.h>
dea3101e 30
91886523 31#include <scsi/scsi.h>
dea3101e
JB
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
f888ba3c 35#include <scsi/scsi_transport_fc.h>
da0436e9 36#include <scsi/fc/fc_fs.h>
0d878419 37#include <linux/aer.h>
1351e69f
JS
38#ifdef CONFIG_X86
39#include <asm/set_memory.h>
40#endif
dea3101e 41
895427bd
JS
42#include <linux/nvme-fc-driver.h>
43
da0436e9 44#include "lpfc_hw4.h"
dea3101e
JB
45#include "lpfc_hw.h"
46#include "lpfc_sli.h"
da0436e9 47#include "lpfc_sli4.h"
ea2151b4 48#include "lpfc_nl.h"
dea3101e 49#include "lpfc_disc.h"
dea3101e 50#include "lpfc.h"
895427bd
JS
51#include "lpfc_scsi.h"
52#include "lpfc_nvme.h"
f358dd0c 53#include "lpfc_nvmet.h"
dea3101e
JB
54#include "lpfc_crtn.h"
55#include "lpfc_logmsg.h"
56#include "lpfc_compat.h"
858c9f6c 57#include "lpfc_debugfs.h"
04c68496 58#include "lpfc_vport.h"
61bda8f7 59#include "lpfc_version.h"
dea3101e
JB
60
61/* There are only four IOCB completion types. */
62typedef enum _lpfc_iocb_type {
63 LPFC_UNKNOWN_IOCB,
64 LPFC_UNSOL_IOCB,
65 LPFC_SOL_IOCB,
66 LPFC_ABORT_IOCB
67} lpfc_iocb_type;
68
4f774513
JS
69
70/* Provide function prototypes local to this module. */
71static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
72 uint32_t);
73static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
74 uint8_t *, uint32_t *);
75static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
76 struct lpfc_iocbq *);
6669f9bb
JS
77static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
78 struct hbq_dmabuf *);
ae9e28f3
JS
79static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
80 struct hbq_dmabuf *dmabuf);
895427bd 81static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
0558056c 82 struct lpfc_cqe *);
895427bd 83static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
8a9d2e80 84 int);
f485c18d
DK
85static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
86 struct lpfc_eqe *eqe, uint32_t qidx);
e8d3c3b1
JS
87static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
88static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
895427bd
JS
89static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
90 struct lpfc_sli_ring *pring,
91 struct lpfc_iocbq *cmdiocb);
0558056c 92
4f774513
JS
93static IOCB_t *
94lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95{
96 return &iocbq->iocb;
97}
98
99/**
100 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
101 * @q: The Work Queue to operate on.
102 * @wqe: The work Queue Entry to put on the Work queue.
103 *
104 * This routine will copy the contents of @wqe to the next available entry on
105 * the @q. This function will then ring the Work Queue Doorbell to signal the
106 * HBA to start processing the Work Queue Entry. This function returns 0 if
107 * successful. If no entries are available on @q then this function will return
108 * -ENOMEM.
109 * The caller is expected to hold the hbalock when calling this routine.
110 **/
cd22d605 111static int
205e8240 112lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
4f774513 113{
2e90f4b5 114 union lpfc_wqe *temp_wqe;
4f774513
JS
115 struct lpfc_register doorbell;
116 uint32_t host_index;
027140ea 117 uint32_t idx;
1351e69f
JS
118 uint32_t i = 0;
119 uint8_t *tmp;
4f774513 120
2e90f4b5
JS
121 /* sanity check on queue memory */
122 if (unlikely(!q))
123 return -ENOMEM;
124 temp_wqe = q->qe[q->host_index].wqe;
125
4f774513 126 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
127 idx = ((q->host_index + 1) % q->entry_count);
128 if (idx == q->hba_index) {
b84daac9 129 q->WQ_overflow++;
cd22d605 130 return -EBUSY;
b84daac9
JS
131 }
132 q->WQ_posted++;
4f774513 133 /* set consumption flag every once in a while */
ff78d8f9 134 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 135 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
04673e38
JS
136 else
137 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
fedd3b7b
JS
138 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
139 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513 140 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
1351e69f
JS
141 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
142 /* write to DPP aperture taking advatage of Combined Writes */
4c06619f
JS
143 tmp = (uint8_t *)temp_wqe;
144#ifdef __raw_writeq
1351e69f 145 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
4c06619f
JS
146 __raw_writeq(*((uint64_t *)(tmp + i)),
147 q->dpp_regaddr + i);
148#else
149 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
150 __raw_writel(*((uint32_t *)(tmp + i)),
151 q->dpp_regaddr + i);
152#endif
1351e69f
JS
153 }
154 /* ensure WQE bcopy and DPP flushed before doorbell write */
6b3b3bdb 155 wmb();
4f774513
JS
156
157 /* Update the host index before invoking device */
158 host_index = q->host_index;
027140ea
JS
159
160 q->host_index = idx;
4f774513
JS
161
162 /* Ring Doorbell */
163 doorbell.word0 = 0;
962bc51b 164 if (q->db_format == LPFC_DB_LIST_FORMAT) {
1351e69f
JS
165 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
166 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
167 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
168 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
169 q->dpp_id);
170 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
171 q->queue_id);
172 } else {
173 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
174 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
175 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
176 }
962bc51b
JS
177 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
178 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
179 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
180 } else {
181 return -EINVAL;
182 }
183 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
184
185 return 0;
186}
187
188/**
189 * lpfc_sli4_wq_release - Updates internal hba index for WQ
190 * @q: The Work Queue to operate on.
191 * @index: The index to advance the hba index to.
192 *
193 * This routine will update the HBA index of a queue to reflect consumption of
194 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
195 * an entry the host calls this function to update the queue's internal
196 * pointers. This routine returns the number of entries that were consumed by
197 * the HBA.
198 **/
199static uint32_t
200lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
201{
202 uint32_t released = 0;
203
2e90f4b5
JS
204 /* sanity check on queue memory */
205 if (unlikely(!q))
206 return 0;
207
4f774513
JS
208 if (q->hba_index == index)
209 return 0;
210 do {
211 q->hba_index = ((q->hba_index + 1) % q->entry_count);
212 released++;
213 } while (q->hba_index != index);
214 return released;
215}
216
217/**
218 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
219 * @q: The Mailbox Queue to operate on.
220 * @wqe: The Mailbox Queue Entry to put on the Work queue.
221 *
222 * This routine will copy the contents of @mqe to the next available entry on
223 * the @q. This function will then ring the Work Queue Doorbell to signal the
224 * HBA to start processing the Work Queue Entry. This function returns 0 if
225 * successful. If no entries are available on @q then this function will return
226 * -ENOMEM.
227 * The caller is expected to hold the hbalock when calling this routine.
228 **/
229static uint32_t
230lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
231{
2e90f4b5 232 struct lpfc_mqe *temp_mqe;
4f774513 233 struct lpfc_register doorbell;
4f774513 234
2e90f4b5
JS
235 /* sanity check on queue memory */
236 if (unlikely(!q))
237 return -ENOMEM;
238 temp_mqe = q->qe[q->host_index].mqe;
239
4f774513
JS
240 /* If the host has not yet processed the next entry then we are done */
241 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
242 return -ENOMEM;
243 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
244 /* Save off the mailbox pointer for completion */
245 q->phba->mbox = (MAILBOX_t *)temp_mqe;
246
247 /* Update the host index before invoking device */
4f774513
JS
248 q->host_index = ((q->host_index + 1) % q->entry_count);
249
250 /* Ring Doorbell */
251 doorbell.word0 = 0;
252 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
253 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
254 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
255 return 0;
256}
257
258/**
259 * lpfc_sli4_mq_release - Updates internal hba index for MQ
260 * @q: The Mailbox Queue to operate on.
261 *
262 * This routine will update the HBA index of a queue to reflect consumption of
263 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
264 * an entry the host calls this function to update the queue's internal
265 * pointers. This routine returns the number of entries that were consumed by
266 * the HBA.
267 **/
268static uint32_t
269lpfc_sli4_mq_release(struct lpfc_queue *q)
270{
2e90f4b5
JS
271 /* sanity check on queue memory */
272 if (unlikely(!q))
273 return 0;
274
4f774513
JS
275 /* Clear the mailbox pointer for completion */
276 q->phba->mbox = NULL;
277 q->hba_index = ((q->hba_index + 1) % q->entry_count);
278 return 1;
279}
280
281/**
282 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
283 * @q: The Event Queue to get the first valid EQE from
284 *
285 * This routine will get the first valid Event Queue Entry from @q, update
286 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
287 * the Queue (no more work to do), or the Queue is full of EQEs that have been
288 * processed, but not popped back to the HBA then this routine will return NULL.
289 **/
290static struct lpfc_eqe *
291lpfc_sli4_eq_get(struct lpfc_queue *q)
292{
7365f6fd 293 struct lpfc_hba *phba;
2e90f4b5 294 struct lpfc_eqe *eqe;
027140ea 295 uint32_t idx;
2e90f4b5
JS
296
297 /* sanity check on queue memory */
298 if (unlikely(!q))
299 return NULL;
7365f6fd 300 phba = q->phba;
2e90f4b5 301 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
302
303 /* If the next EQE is not valid then we are done */
7365f6fd 304 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
4f774513
JS
305 return NULL;
306 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
307 idx = ((q->hba_index + 1) % q->entry_count);
308 if (idx == q->host_index)
4f774513
JS
309 return NULL;
310
027140ea 311 q->hba_index = idx;
7365f6fd
JS
312 /* if the index wrapped around, toggle the valid bit */
313 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
314 q->qe_valid = (q->qe_valid) ? 0 : 1;
315
27f344eb
JS
316
317 /*
318 * insert barrier for instruction interlock : data from the hardware
319 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
320 * upon. Speculative instructions were allowing a bcopy at the start
321 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
322 * after our return, to copy data before the valid bit check above
323 * was done. As such, some of the copied data was stale. The barrier
324 * ensures the check is before any data is copied.
27f344eb
JS
325 */
326 mb();
4f774513
JS
327 return eqe;
328}
329
ba20c853
JS
330/**
331 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
332 * @q: The Event Queue to disable interrupts
333 *
334 **/
b71413dd 335inline void
ba20c853
JS
336lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
337{
338 struct lpfc_register doorbell;
339
340 doorbell.word0 = 0;
341 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
342 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
343 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
344 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
345 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 346 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
ba20c853
JS
347}
348
27d6ac0a
JS
349/**
350 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
351 * @q: The Event Queue to disable interrupts
352 *
353 **/
354inline void
355lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
356{
357 struct lpfc_register doorbell;
358
359 doorbell.word0 = 0;
360 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
361 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
362 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
363 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
364 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
365 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
366}
367
4f774513
JS
368/**
369 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
370 * @q: The Event Queue that the host has completed processing for.
371 * @arm: Indicates whether the host wants to arms this CQ.
372 *
373 * This routine will mark all Event Queue Entries on @q, from the last
374 * known completed entry to the last entry that was processed, as completed
375 * by clearing the valid bit for each completion queue entry. Then it will
376 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
377 * The internal host index in the @q will be updated by this routine to indicate
378 * that the host has finished processing the entries. The @arm parameter
379 * indicates that the queue should be rearmed when ringing the doorbell.
380 *
381 * This function will return the number of EQEs that were popped.
382 **/
383uint32_t
384lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
385{
386 uint32_t released = 0;
7365f6fd 387 struct lpfc_hba *phba;
4f774513
JS
388 struct lpfc_eqe *temp_eqe;
389 struct lpfc_register doorbell;
390
2e90f4b5
JS
391 /* sanity check on queue memory */
392 if (unlikely(!q))
393 return 0;
7365f6fd 394 phba = q->phba;
2e90f4b5 395
4f774513
JS
396 /* while there are valid entries */
397 while (q->hba_index != q->host_index) {
7365f6fd
JS
398 if (!phba->sli4_hba.pc_sli4_params.eqav) {
399 temp_eqe = q->qe[q->host_index].eqe;
400 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
401 }
4f774513
JS
402 released++;
403 q->host_index = ((q->host_index + 1) % q->entry_count);
404 }
405 if (unlikely(released == 0 && !arm))
406 return 0;
407
408 /* ring doorbell for number popped */
409 doorbell.word0 = 0;
410 if (arm) {
411 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
412 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
413 }
414 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
415 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
416 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
417 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
418 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 419 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
a747c9ce
JS
420 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
421 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
9dd35425 422 readl(q->phba->sli4_hba.EQDBregaddr);
4f774513
JS
423 return released;
424}
425
27d6ac0a
JS
426/**
427 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
428 * @q: The Event Queue that the host has completed processing for.
429 * @arm: Indicates whether the host wants to arms this CQ.
430 *
431 * This routine will mark all Event Queue Entries on @q, from the last
432 * known completed entry to the last entry that was processed, as completed
433 * by clearing the valid bit for each completion queue entry. Then it will
434 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
435 * The internal host index in the @q will be updated by this routine to indicate
436 * that the host has finished processing the entries. The @arm parameter
437 * indicates that the queue should be rearmed when ringing the doorbell.
438 *
439 * This function will return the number of EQEs that were popped.
440 **/
441uint32_t
442lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
443{
444 uint32_t released = 0;
7365f6fd 445 struct lpfc_hba *phba;
27d6ac0a
JS
446 struct lpfc_eqe *temp_eqe;
447 struct lpfc_register doorbell;
448
449 /* sanity check on queue memory */
450 if (unlikely(!q))
451 return 0;
7365f6fd 452 phba = q->phba;
27d6ac0a
JS
453
454 /* while there are valid entries */
455 while (q->hba_index != q->host_index) {
7365f6fd
JS
456 if (!phba->sli4_hba.pc_sli4_params.eqav) {
457 temp_eqe = q->qe[q->host_index].eqe;
458 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
459 }
27d6ac0a
JS
460 released++;
461 q->host_index = ((q->host_index + 1) % q->entry_count);
462 }
463 if (unlikely(released == 0 && !arm))
464 return 0;
465
466 /* ring doorbell for number popped */
467 doorbell.word0 = 0;
468 if (arm)
469 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
470 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
471 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
472 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
473 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
474 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
475 readl(q->phba->sli4_hba.EQDBregaddr);
476 return released;
477}
478
4f774513
JS
479/**
480 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
481 * @q: The Completion Queue to get the first valid CQE from
482 *
483 * This routine will get the first valid Completion Queue Entry from @q, update
484 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
485 * the Queue (no more work to do), or the Queue is full of CQEs that have been
486 * processed, but not popped back to the HBA then this routine will return NULL.
487 **/
488static struct lpfc_cqe *
489lpfc_sli4_cq_get(struct lpfc_queue *q)
490{
7365f6fd 491 struct lpfc_hba *phba;
4f774513 492 struct lpfc_cqe *cqe;
027140ea 493 uint32_t idx;
4f774513 494
2e90f4b5
JS
495 /* sanity check on queue memory */
496 if (unlikely(!q))
497 return NULL;
7365f6fd
JS
498 phba = q->phba;
499 cqe = q->qe[q->hba_index].cqe;
2e90f4b5 500
4f774513 501 /* If the next CQE is not valid then we are done */
7365f6fd 502 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
4f774513
JS
503 return NULL;
504 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
505 idx = ((q->hba_index + 1) % q->entry_count);
506 if (idx == q->host_index)
4f774513
JS
507 return NULL;
508
027140ea 509 q->hba_index = idx;
7365f6fd
JS
510 /* if the index wrapped around, toggle the valid bit */
511 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
512 q->qe_valid = (q->qe_valid) ? 0 : 1;
27f344eb
JS
513
514 /*
515 * insert barrier for instruction interlock : data from the hardware
516 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
517 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
518 * instructions allowing action on content before valid bit checked,
519 * add barrier here as well. May not be needed as "content" is a
520 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
521 */
522 mb();
4f774513
JS
523 return cqe;
524}
525
526/**
527 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
528 * @q: The Completion Queue that the host has completed processing for.
529 * @arm: Indicates whether the host wants to arms this CQ.
530 *
531 * This routine will mark all Completion queue entries on @q, from the last
532 * known completed entry to the last entry that was processed, as completed
533 * by clearing the valid bit for each completion queue entry. Then it will
534 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
535 * The internal host index in the @q will be updated by this routine to indicate
536 * that the host has finished processing the entries. The @arm parameter
537 * indicates that the queue should be rearmed when ringing the doorbell.
538 *
539 * This function will return the number of CQEs that were released.
540 **/
541uint32_t
542lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
543{
544 uint32_t released = 0;
7365f6fd 545 struct lpfc_hba *phba;
4f774513
JS
546 struct lpfc_cqe *temp_qe;
547 struct lpfc_register doorbell;
548
2e90f4b5
JS
549 /* sanity check on queue memory */
550 if (unlikely(!q))
551 return 0;
7365f6fd
JS
552 phba = q->phba;
553
4f774513
JS
554 /* while there are valid entries */
555 while (q->hba_index != q->host_index) {
7365f6fd
JS
556 if (!phba->sli4_hba.pc_sli4_params.cqav) {
557 temp_qe = q->qe[q->host_index].cqe;
558 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
559 }
4f774513
JS
560 released++;
561 q->host_index = ((q->host_index + 1) % q->entry_count);
562 }
563 if (unlikely(released == 0 && !arm))
564 return 0;
565
566 /* ring doorbell for number popped */
567 doorbell.word0 = 0;
568 if (arm)
569 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
570 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
571 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
572 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
573 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
574 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
9dd35425 575 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
4f774513
JS
576 return released;
577}
578
27d6ac0a
JS
579/**
580 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
581 * @q: The Completion Queue that the host has completed processing for.
582 * @arm: Indicates whether the host wants to arms this CQ.
583 *
584 * This routine will mark all Completion queue entries on @q, from the last
585 * known completed entry to the last entry that was processed, as completed
586 * by clearing the valid bit for each completion queue entry. Then it will
587 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
588 * The internal host index in the @q will be updated by this routine to indicate
589 * that the host has finished processing the entries. The @arm parameter
590 * indicates that the queue should be rearmed when ringing the doorbell.
591 *
592 * This function will return the number of CQEs that were released.
593 **/
594uint32_t
595lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
596{
597 uint32_t released = 0;
7365f6fd 598 struct lpfc_hba *phba;
27d6ac0a
JS
599 struct lpfc_cqe *temp_qe;
600 struct lpfc_register doorbell;
601
602 /* sanity check on queue memory */
603 if (unlikely(!q))
604 return 0;
7365f6fd
JS
605 phba = q->phba;
606
27d6ac0a
JS
607 /* while there are valid entries */
608 while (q->hba_index != q->host_index) {
7365f6fd
JS
609 if (!phba->sli4_hba.pc_sli4_params.cqav) {
610 temp_qe = q->qe[q->host_index].cqe;
611 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
612 }
27d6ac0a
JS
613 released++;
614 q->host_index = ((q->host_index + 1) % q->entry_count);
615 }
616 if (unlikely(released == 0 && !arm))
617 return 0;
618
619 /* ring doorbell for number popped */
620 doorbell.word0 = 0;
621 if (arm)
622 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
623 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
624 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
625 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
626 return released;
627}
628
4f774513
JS
629/**
630 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
631 * @q: The Header Receive Queue to operate on.
632 * @wqe: The Receive Queue Entry to put on the Receive queue.
633 *
634 * This routine will copy the contents of @wqe to the next available entry on
635 * the @q. This function will then ring the Receive Queue Doorbell to signal the
636 * HBA to start processing the Receive Queue Entry. This function returns the
637 * index that the rqe was copied to if successful. If no entries are available
638 * on @q then this function will return -ENOMEM.
639 * The caller is expected to hold the hbalock when calling this routine.
640 **/
895427bd 641int
4f774513
JS
642lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
643 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
644{
2e90f4b5
JS
645 struct lpfc_rqe *temp_hrqe;
646 struct lpfc_rqe *temp_drqe;
4f774513 647 struct lpfc_register doorbell;
cbc5de1b
JS
648 int hq_put_index;
649 int dq_put_index;
4f774513 650
2e90f4b5
JS
651 /* sanity check on queue memory */
652 if (unlikely(!hq) || unlikely(!dq))
653 return -ENOMEM;
cbc5de1b
JS
654 hq_put_index = hq->host_index;
655 dq_put_index = dq->host_index;
656 temp_hrqe = hq->qe[hq_put_index].rqe;
657 temp_drqe = dq->qe[dq_put_index].rqe;
2e90f4b5 658
4f774513
JS
659 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
660 return -EINVAL;
cbc5de1b 661 if (hq_put_index != dq_put_index)
4f774513
JS
662 return -EINVAL;
663 /* If the host has not yet processed the next entry then we are done */
cbc5de1b 664 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513
JS
665 return -EBUSY;
666 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
667 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
668
669 /* Update the host index to point to the next slot */
cbc5de1b
JS
670 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
671 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
61f3d4bf 672 hq->RQ_buf_posted++;
4f774513
JS
673
674 /* Ring The Header Receive Queue Doorbell */
73d91e50 675 if (!(hq->host_index % hq->entry_repost)) {
4f774513 676 doorbell.word0 = 0;
962bc51b
JS
677 if (hq->db_format == LPFC_DB_RING_FORMAT) {
678 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
679 hq->entry_repost);
680 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
681 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
682 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
683 hq->entry_repost);
684 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
685 hq->host_index);
686 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
687 } else {
688 return -EINVAL;
689 }
690 writel(doorbell.word0, hq->db_regaddr);
4f774513 691 }
cbc5de1b 692 return hq_put_index;
4f774513
JS
693}
694
695/**
696 * lpfc_sli4_rq_release - Updates internal hba index for RQ
697 * @q: The Header Receive Queue to operate on.
698 *
699 * This routine will update the HBA index of a queue to reflect consumption of
700 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
701 * consumed an entry the host calls this function to update the queue's
702 * internal pointers. This routine returns the number of entries that were
703 * consumed by the HBA.
704 **/
705static uint32_t
706lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
707{
2e90f4b5
JS
708 /* sanity check on queue memory */
709 if (unlikely(!hq) || unlikely(!dq))
710 return 0;
711
4f774513
JS
712 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
713 return 0;
714 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
715 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
716 return 1;
717}
718
e59058c4 719/**
3621a710 720 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
721 * @phba: Pointer to HBA context object.
722 * @pring: Pointer to driver SLI ring object.
723 *
724 * This function returns pointer to next command iocb entry
725 * in the command ring. The caller must hold hbalock to prevent
726 * other threads consume the next command iocb.
727 * SLI-2/SLI-3 provide different sized iocbs.
728 **/
ed957684
JS
729static inline IOCB_t *
730lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
731{
7e56aa25
JS
732 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
733 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
734}
735
e59058c4 736/**
3621a710 737 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
738 * @phba: Pointer to HBA context object.
739 * @pring: Pointer to driver SLI ring object.
740 *
741 * This function returns pointer to next response iocb entry
742 * in the response ring. The caller must hold hbalock to make sure
743 * that no other thread consume the next response iocb.
744 * SLI-2/SLI-3 provide different sized iocbs.
745 **/
ed957684
JS
746static inline IOCB_t *
747lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
748{
7e56aa25
JS
749 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
750 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
751}
752
e59058c4 753/**
3621a710 754 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
755 * @phba: Pointer to HBA context object.
756 *
757 * This function is called with hbalock held. This function
758 * allocates a new driver iocb object from the iocb pool. If the
759 * allocation is successful, it returns pointer to the newly
760 * allocated iocb object else it returns NULL.
761 **/
4f2e66c6 762struct lpfc_iocbq *
2e0fef85 763__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
764{
765 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
766 struct lpfc_iocbq * iocbq = NULL;
767
1c2ba475
JT
768 lockdep_assert_held(&phba->hbalock);
769
0bd4ca25 770 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
771 if (iocbq)
772 phba->iocb_cnt++;
773 if (phba->iocb_cnt > phba->iocb_max)
774 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
775 return iocbq;
776}
777
da0436e9
JS
778/**
779 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
780 * @phba: Pointer to HBA context object.
781 * @xritag: XRI value.
782 *
783 * This function clears the sglq pointer from the array of acive
784 * sglq's. The xritag that is passed in is used to index into the
785 * array. Before the xritag can be used it needs to be adjusted
786 * by subtracting the xribase.
787 *
788 * Returns sglq ponter = success, NULL = Failure.
789 **/
895427bd 790struct lpfc_sglq *
da0436e9
JS
791__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
792{
da0436e9 793 struct lpfc_sglq *sglq;
6d368e53
JS
794
795 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
796 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
797 return sglq;
798}
799
800/**
801 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
802 * @phba: Pointer to HBA context object.
803 * @xritag: XRI value.
804 *
805 * This function returns the sglq pointer from the array of acive
806 * sglq's. The xritag that is passed in is used to index into the
807 * array. Before the xritag can be used it needs to be adjusted
808 * by subtracting the xribase.
809 *
810 * Returns sglq ponter = success, NULL = Failure.
811 **/
0f65ff68 812struct lpfc_sglq *
da0436e9
JS
813__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
814{
da0436e9 815 struct lpfc_sglq *sglq;
6d368e53
JS
816
817 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
818 return sglq;
819}
820
19ca7609 821/**
1151e3ec 822 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
823 * @phba: Pointer to HBA context object.
824 * @xritag: xri used in this exchange.
825 * @rrq: The RRQ to be cleared.
826 *
19ca7609 827 **/
1151e3ec
JS
828void
829lpfc_clr_rrq_active(struct lpfc_hba *phba,
830 uint16_t xritag,
831 struct lpfc_node_rrq *rrq)
19ca7609 832{
1151e3ec 833 struct lpfc_nodelist *ndlp = NULL;
19ca7609 834
1151e3ec
JS
835 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
836 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
837
838 /* The target DID could have been swapped (cable swap)
839 * we should use the ndlp from the findnode if it is
840 * available.
841 */
1151e3ec 842 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
843 ndlp = rrq->ndlp;
844
1151e3ec
JS
845 if (!ndlp)
846 goto out;
847
cff261f6 848 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
849 rrq->send_rrq = 0;
850 rrq->xritag = 0;
851 rrq->rrq_stop_time = 0;
852 }
1151e3ec 853out:
19ca7609
JS
854 mempool_free(rrq, phba->rrq_pool);
855}
856
857/**
858 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
859 * @phba: Pointer to HBA context object.
860 *
861 * This function is called with hbalock held. This function
862 * Checks if stop_time (ratov from setting rrq active) has
863 * been reached, if it has and the send_rrq flag is set then
864 * it will call lpfc_send_rrq. If the send_rrq flag is not set
865 * then it will just call the routine to clear the rrq and
866 * free the rrq resource.
867 * The timer is set to the next rrq that is going to expire before
868 * leaving the routine.
869 *
870 **/
871void
872lpfc_handle_rrq_active(struct lpfc_hba *phba)
873{
874 struct lpfc_node_rrq *rrq;
875 struct lpfc_node_rrq *nextrrq;
876 unsigned long next_time;
877 unsigned long iflags;
1151e3ec 878 LIST_HEAD(send_rrq);
19ca7609
JS
879
880 spin_lock_irqsave(&phba->hbalock, iflags);
881 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 882 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 883 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
884 &phba->active_rrq_list, list) {
885 if (time_after(jiffies, rrq->rrq_stop_time))
886 list_move(&rrq->list, &send_rrq);
887 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
888 next_time = rrq->rrq_stop_time;
889 }
890 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
891 if ((!list_empty(&phba->active_rrq_list)) &&
892 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 893 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
894 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
895 list_del(&rrq->list);
896 if (!rrq->send_rrq)
897 /* this call will free the rrq */
898 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
899 else if (lpfc_send_rrq(phba, rrq)) {
900 /* if we send the rrq then the completion handler
901 * will clear the bit in the xribitmap.
902 */
903 lpfc_clr_rrq_active(phba, rrq->xritag,
904 rrq);
905 }
906 }
19ca7609
JS
907}
908
909/**
910 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
911 * @vport: Pointer to vport context object.
912 * @xri: The xri used in the exchange.
913 * @did: The targets DID for this exchange.
914 *
915 * returns NULL = rrq not found in the phba->active_rrq_list.
916 * rrq = rrq for this xri and target.
917 **/
918struct lpfc_node_rrq *
919lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
920{
921 struct lpfc_hba *phba = vport->phba;
922 struct lpfc_node_rrq *rrq;
923 struct lpfc_node_rrq *nextrrq;
924 unsigned long iflags;
925
926 if (phba->sli_rev != LPFC_SLI_REV4)
927 return NULL;
928 spin_lock_irqsave(&phba->hbalock, iflags);
929 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
930 if (rrq->vport == vport && rrq->xritag == xri &&
931 rrq->nlp_DID == did){
932 list_del(&rrq->list);
933 spin_unlock_irqrestore(&phba->hbalock, iflags);
934 return rrq;
935 }
936 }
937 spin_unlock_irqrestore(&phba->hbalock, iflags);
938 return NULL;
939}
940
941/**
942 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
943 * @vport: Pointer to vport context object.
1151e3ec
JS
944 * @ndlp: Pointer to the lpfc_node_list structure.
945 * If ndlp is NULL Remove all active RRQs for this vport from the
946 * phba->active_rrq_list and clear the rrq.
947 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
948 **/
949void
1151e3ec 950lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
951
952{
953 struct lpfc_hba *phba = vport->phba;
954 struct lpfc_node_rrq *rrq;
955 struct lpfc_node_rrq *nextrrq;
956 unsigned long iflags;
1151e3ec 957 LIST_HEAD(rrq_list);
19ca7609
JS
958
959 if (phba->sli_rev != LPFC_SLI_REV4)
960 return;
1151e3ec
JS
961 if (!ndlp) {
962 lpfc_sli4_vport_delete_els_xri_aborted(vport);
963 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 964 }
1151e3ec
JS
965 spin_lock_irqsave(&phba->hbalock, iflags);
966 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
967 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
968 list_move(&rrq->list, &rrq_list);
19ca7609 969 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
970
971 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
972 list_del(&rrq->list);
973 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
974 }
19ca7609
JS
975}
976
19ca7609 977/**
1151e3ec 978 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
979 * @phba: Pointer to HBA context object.
980 * @ndlp: Targets nodelist pointer for this exchange.
981 * @xritag the xri in the bitmap to test.
982 *
983 * This function is called with hbalock held. This function
984 * returns 0 = rrq not active for this xri
985 * 1 = rrq is valid for this xri.
986 **/
1151e3ec
JS
987int
988lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
989 uint16_t xritag)
990{
1c2ba475 991 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
992 if (!ndlp)
993 return 0;
cff261f6
JS
994 if (!ndlp->active_rrqs_xri_bitmap)
995 return 0;
996 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
997 return 1;
998 else
999 return 0;
1000}
1001
1002/**
1003 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1004 * @phba: Pointer to HBA context object.
1005 * @ndlp: nodelist pointer for this target.
1006 * @xritag: xri used in this exchange.
1007 * @rxid: Remote Exchange ID.
1008 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1009 *
1010 * This function takes the hbalock.
1011 * The active bit is always set in the active rrq xri_bitmap even
1012 * if there is no slot avaiable for the other rrq information.
1013 *
1014 * returns 0 rrq actived for this xri
1015 * < 0 No memory or invalid ndlp.
1016 **/
1017int
1018lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 1019 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 1020{
19ca7609 1021 unsigned long iflags;
b42c07c8
JS
1022 struct lpfc_node_rrq *rrq;
1023 int empty;
1024
1025 if (!ndlp)
1026 return -EINVAL;
1027
1028 if (!phba->cfg_enable_rrq)
1029 return -EINVAL;
19ca7609
JS
1030
1031 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
1032 if (phba->pport->load_flag & FC_UNLOADING) {
1033 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1034 goto out;
1035 }
1036
1037 /*
1038 * set the active bit even if there is no mem available.
1039 */
1040 if (NLP_CHK_FREE_REQ(ndlp))
1041 goto out;
1042
1043 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1044 goto out;
1045
cff261f6
JS
1046 if (!ndlp->active_rrqs_xri_bitmap)
1047 goto out;
1048
1049 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
1050 goto out;
1051
19ca7609 1052 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
1053 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1054 if (!rrq) {
1055 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1056 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1057 " DID:0x%x Send:%d\n",
1058 xritag, rxid, ndlp->nlp_DID, send_rrq);
1059 return -EINVAL;
1060 }
e5771b4d
JS
1061 if (phba->cfg_enable_rrq == 1)
1062 rrq->send_rrq = send_rrq;
1063 else
1064 rrq->send_rrq = 0;
b42c07c8 1065 rrq->xritag = xritag;
256ec0d0
JS
1066 rrq->rrq_stop_time = jiffies +
1067 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
1068 rrq->ndlp = ndlp;
1069 rrq->nlp_DID = ndlp->nlp_DID;
1070 rrq->vport = ndlp->vport;
1071 rrq->rxid = rxid;
b42c07c8
JS
1072 spin_lock_irqsave(&phba->hbalock, iflags);
1073 empty = list_empty(&phba->active_rrq_list);
1074 list_add_tail(&rrq->list, &phba->active_rrq_list);
1075 phba->hba_flag |= HBA_RRQ_ACTIVE;
1076 if (empty)
1077 lpfc_worker_wake_up(phba);
1078 spin_unlock_irqrestore(&phba->hbalock, iflags);
1079 return 0;
1080out:
1081 spin_unlock_irqrestore(&phba->hbalock, iflags);
1082 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1083 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1084 " DID:0x%x Send:%d\n",
1085 xritag, rxid, ndlp->nlp_DID, send_rrq);
1086 return -EINVAL;
19ca7609
JS
1087}
1088
da0436e9 1089/**
895427bd 1090 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 1091 * @phba: Pointer to HBA context object.
19ca7609 1092 * @piocb: Pointer to the iocbq.
da0436e9 1093 *
dafe8cea 1094 * This function is called with the ring lock held. This function
6d368e53 1095 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
1096 * list is not empty then it is successful, it returns pointer to the newly
1097 * allocated sglq object else it returns NULL.
1098 **/
1099static struct lpfc_sglq *
895427bd 1100__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 1101{
895427bd 1102 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 1103 struct lpfc_sglq *sglq = NULL;
19ca7609 1104 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
1105 struct lpfc_scsi_buf *lpfc_cmd;
1106 struct lpfc_nodelist *ndlp;
1107 int found = 0;
1108
1c2ba475
JT
1109 lockdep_assert_held(&phba->hbalock);
1110
19ca7609
JS
1111 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1112 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1113 ndlp = lpfc_cmd->rdata->pnode;
be858b65 1114 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 1115 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 1116 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
1117 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1118 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1119 ndlp = NULL;
1120 else
1121 ndlp = piocbq->context_un.ndlp;
1122 } else {
19ca7609 1123 ndlp = piocbq->context1;
6c7cf486 1124 }
19ca7609 1125
895427bd
JS
1126 spin_lock(&phba->sli4_hba.sgl_list_lock);
1127 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
1128 start_sglq = sglq;
1129 while (!found) {
1130 if (!sglq)
d11f54b7 1131 break;
895427bd
JS
1132 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1133 test_bit(sglq->sli4_lxritag,
1134 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
1135 /* This xri has an rrq outstanding for this DID.
1136 * put it back in the list and get another xri.
1137 */
895427bd 1138 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 1139 sglq = NULL;
895427bd 1140 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
1141 struct lpfc_sglq, list);
1142 if (sglq == start_sglq) {
14041bd1 1143 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
1144 sglq = NULL;
1145 break;
1146 } else
1147 continue;
1148 }
1149 sglq->ndlp = ndlp;
1150 found = 1;
6d368e53 1151 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
1152 sglq->state = SGL_ALLOCATED;
1153 }
895427bd 1154 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
1155 return sglq;
1156}
1157
f358dd0c
JS
1158/**
1159 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1160 * @phba: Pointer to HBA context object.
1161 * @piocb: Pointer to the iocbq.
1162 *
1163 * This function is called with the sgl_list lock held. This function
1164 * gets a new driver sglq object from the sglq list. If the
1165 * list is not empty then it is successful, it returns pointer to the newly
1166 * allocated sglq object else it returns NULL.
1167 **/
1168struct lpfc_sglq *
1169__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1170{
1171 struct list_head *lpfc_nvmet_sgl_list;
1172 struct lpfc_sglq *sglq = NULL;
1173
1174 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1175
1176 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1177
1178 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1179 if (!sglq)
1180 return NULL;
1181 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1182 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1183 return sglq;
1184}
1185
e59058c4 1186/**
3621a710 1187 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1188 * @phba: Pointer to HBA context object.
1189 *
1190 * This function is called with no lock held. This function
1191 * allocates a new driver iocb object from the iocb pool. If the
1192 * allocation is successful, it returns pointer to the newly
1193 * allocated iocb object else it returns NULL.
1194 **/
2e0fef85
JS
1195struct lpfc_iocbq *
1196lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1197{
1198 struct lpfc_iocbq * iocbq = NULL;
1199 unsigned long iflags;
1200
1201 spin_lock_irqsave(&phba->hbalock, iflags);
1202 iocbq = __lpfc_sli_get_iocbq(phba);
1203 spin_unlock_irqrestore(&phba->hbalock, iflags);
1204 return iocbq;
1205}
1206
4f774513
JS
1207/**
1208 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1209 * @phba: Pointer to HBA context object.
1210 * @iocbq: Pointer to driver iocb object.
1211 *
1212 * This function is called with hbalock held to release driver
1213 * iocb object to the iocb pool. The iotag in the iocb object
1214 * does not change for each use of the iocb object. This function
1215 * clears all other fields of the iocb object when it is freed.
1216 * The sqlq structure that holds the xritag and phys and virtual
1217 * mappings for the scatter gather list is retrieved from the
1218 * active array of sglq. The get of the sglq pointer also clears
1219 * the entry in the array. If the status of the IO indiactes that
1220 * this IO was aborted then the sglq entry it put on the
1221 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1222 * IO has good status or fails for any other reason then the sglq
895427bd 1223 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1224 **/
1225static void
1226__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1227{
1228 struct lpfc_sglq *sglq;
1229 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1230 unsigned long iflag = 0;
895427bd 1231 struct lpfc_sli_ring *pring;
4f774513 1232
1c2ba475
JT
1233 lockdep_assert_held(&phba->hbalock);
1234
4f774513
JS
1235 if (iocbq->sli4_xritag == NO_XRI)
1236 sglq = NULL;
1237 else
6d368e53
JS
1238 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1239
0e9bb8d7 1240
4f774513 1241 if (sglq) {
f358dd0c
JS
1242 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1243 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1244 iflag);
1245 sglq->state = SGL_FREED;
1246 sglq->ndlp = NULL;
1247 list_add_tail(&sglq->list,
1248 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1249 spin_unlock_irqrestore(
1250 &phba->sli4_hba.sgl_list_lock, iflag);
1251 goto out;
1252 }
1253
895427bd 1254 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1255 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1256 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1257 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1258 iflag);
4f774513 1259 list_add(&sglq->list,
895427bd 1260 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1261 spin_unlock_irqrestore(
895427bd 1262 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1263 } else {
895427bd
JS
1264 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1265 iflag);
0f65ff68 1266 sglq->state = SGL_FREED;
19ca7609 1267 sglq->ndlp = NULL;
fedd3b7b 1268 list_add_tail(&sglq->list,
895427bd
JS
1269 &phba->sli4_hba.lpfc_els_sgl_list);
1270 spin_unlock_irqrestore(
1271 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1272
1273 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1274 if (!list_empty(&pring->txq))
2a9bf3d0 1275 lpfc_worker_wake_up(phba);
0f65ff68 1276 }
4f774513
JS
1277 }
1278
f358dd0c 1279out:
4f774513
JS
1280 /*
1281 * Clean all volatile data fields, preserve iotag and node struct.
1282 */
1283 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1284 iocbq->sli4_lxritag = NO_XRI;
4f774513 1285 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1286 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1287 LPFC_IO_NVME_LS);
4f774513
JS
1288 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1289}
1290
2a9bf3d0 1291
e59058c4 1292/**
3772a991 1293 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1294 * @phba: Pointer to HBA context object.
1295 * @iocbq: Pointer to driver iocb object.
1296 *
1297 * This function is called with hbalock held to release driver
1298 * iocb object to the iocb pool. The iotag in the iocb object
1299 * does not change for each use of the iocb object. This function
1300 * clears all other fields of the iocb object when it is freed.
1301 **/
a6ababd2 1302static void
3772a991 1303__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1304{
2e0fef85 1305 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1306
1c2ba475 1307 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1308
604a3e30
JB
1309 /*
1310 * Clean all volatile data fields, preserve iotag and node struct.
1311 */
1312 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1313 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1314 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1315}
1316
3772a991
JS
1317/**
1318 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1319 * @phba: Pointer to HBA context object.
1320 * @iocbq: Pointer to driver iocb object.
1321 *
1322 * This function is called with hbalock held to release driver
1323 * iocb object to the iocb pool. The iotag in the iocb object
1324 * does not change for each use of the iocb object. This function
1325 * clears all other fields of the iocb object when it is freed.
1326 **/
1327static void
1328__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1329{
1c2ba475
JT
1330 lockdep_assert_held(&phba->hbalock);
1331
3772a991 1332 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1333 phba->iocb_cnt--;
3772a991
JS
1334}
1335
e59058c4 1336/**
3621a710 1337 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1338 * @phba: Pointer to HBA context object.
1339 * @iocbq: Pointer to driver iocb object.
1340 *
1341 * This function is called with no lock held to release the iocb to
1342 * iocb pool.
1343 **/
2e0fef85
JS
1344void
1345lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1346{
1347 unsigned long iflags;
1348
1349 /*
1350 * Clean all volatile data fields, preserve iotag and node struct.
1351 */
1352 spin_lock_irqsave(&phba->hbalock, iflags);
1353 __lpfc_sli_release_iocbq(phba, iocbq);
1354 spin_unlock_irqrestore(&phba->hbalock, iflags);
1355}
1356
a257bf90
JS
1357/**
1358 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1359 * @phba: Pointer to HBA context object.
1360 * @iocblist: List of IOCBs.
1361 * @ulpstatus: ULP status in IOCB command field.
1362 * @ulpWord4: ULP word-4 in IOCB command field.
1363 *
1364 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1365 * on the list by invoking the complete callback function associated with the
1366 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1367 * fields.
1368 **/
1369void
1370lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1371 uint32_t ulpstatus, uint32_t ulpWord4)
1372{
1373 struct lpfc_iocbq *piocb;
1374
1375 while (!list_empty(iocblist)) {
1376 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1377 if (!piocb->iocb_cmpl)
1378 lpfc_sli_release_iocbq(phba, piocb);
1379 else {
1380 piocb->iocb.ulpStatus = ulpstatus;
1381 piocb->iocb.un.ulpWord[4] = ulpWord4;
1382 (piocb->iocb_cmpl) (phba, piocb, piocb);
1383 }
1384 }
1385 return;
1386}
1387
e59058c4 1388/**
3621a710
JS
1389 * lpfc_sli_iocb_cmd_type - Get the iocb type
1390 * @iocb_cmnd: iocb command code.
e59058c4
JS
1391 *
1392 * This function is called by ring event handler function to get the iocb type.
1393 * This function translates the iocb command to an iocb command type used to
1394 * decide the final disposition of each completed IOCB.
1395 * The function returns
1396 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1397 * LPFC_SOL_IOCB if it is a solicited iocb completion
1398 * LPFC_ABORT_IOCB if it is an abort iocb
1399 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1400 *
1401 * The caller is not required to hold any lock.
1402 **/
dea3101e
JB
1403static lpfc_iocb_type
1404lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1405{
1406 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1407
1408 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1409 return 0;
1410
1411 switch (iocb_cmnd) {
1412 case CMD_XMIT_SEQUENCE_CR:
1413 case CMD_XMIT_SEQUENCE_CX:
1414 case CMD_XMIT_BCAST_CN:
1415 case CMD_XMIT_BCAST_CX:
1416 case CMD_ELS_REQUEST_CR:
1417 case CMD_ELS_REQUEST_CX:
1418 case CMD_CREATE_XRI_CR:
1419 case CMD_CREATE_XRI_CX:
1420 case CMD_GET_RPI_CN:
1421 case CMD_XMIT_ELS_RSP_CX:
1422 case CMD_GET_RPI_CR:
1423 case CMD_FCP_IWRITE_CR:
1424 case CMD_FCP_IWRITE_CX:
1425 case CMD_FCP_IREAD_CR:
1426 case CMD_FCP_IREAD_CX:
1427 case CMD_FCP_ICMND_CR:
1428 case CMD_FCP_ICMND_CX:
f5603511
JS
1429 case CMD_FCP_TSEND_CX:
1430 case CMD_FCP_TRSP_CX:
1431 case CMD_FCP_TRECEIVE_CX:
1432 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1433 case CMD_ADAPTER_MSG:
1434 case CMD_ADAPTER_DUMP:
1435 case CMD_XMIT_SEQUENCE64_CR:
1436 case CMD_XMIT_SEQUENCE64_CX:
1437 case CMD_XMIT_BCAST64_CN:
1438 case CMD_XMIT_BCAST64_CX:
1439 case CMD_ELS_REQUEST64_CR:
1440 case CMD_ELS_REQUEST64_CX:
1441 case CMD_FCP_IWRITE64_CR:
1442 case CMD_FCP_IWRITE64_CX:
1443 case CMD_FCP_IREAD64_CR:
1444 case CMD_FCP_IREAD64_CX:
1445 case CMD_FCP_ICMND64_CR:
1446 case CMD_FCP_ICMND64_CX:
f5603511
JS
1447 case CMD_FCP_TSEND64_CX:
1448 case CMD_FCP_TRSP64_CX:
1449 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1450 case CMD_GEN_REQUEST64_CR:
1451 case CMD_GEN_REQUEST64_CX:
1452 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1453 case DSSCMD_IWRITE64_CR:
1454 case DSSCMD_IWRITE64_CX:
1455 case DSSCMD_IREAD64_CR:
1456 case DSSCMD_IREAD64_CX:
dea3101e
JB
1457 type = LPFC_SOL_IOCB;
1458 break;
1459 case CMD_ABORT_XRI_CN:
1460 case CMD_ABORT_XRI_CX:
1461 case CMD_CLOSE_XRI_CN:
1462 case CMD_CLOSE_XRI_CX:
1463 case CMD_XRI_ABORTED_CX:
1464 case CMD_ABORT_MXRI64_CN:
6669f9bb 1465 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1466 type = LPFC_ABORT_IOCB;
1467 break;
1468 case CMD_RCV_SEQUENCE_CX:
1469 case CMD_RCV_ELS_REQ_CX:
1470 case CMD_RCV_SEQUENCE64_CX:
1471 case CMD_RCV_ELS_REQ64_CX:
57127f15 1472 case CMD_ASYNC_STATUS:
ed957684
JS
1473 case CMD_IOCB_RCV_SEQ64_CX:
1474 case CMD_IOCB_RCV_ELS64_CX:
1475 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1476 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1477 type = LPFC_UNSOL_IOCB;
1478 break;
3163f725
JS
1479 case CMD_IOCB_XMIT_MSEQ64_CR:
1480 case CMD_IOCB_XMIT_MSEQ64_CX:
1481 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1482 case CMD_IOCB_RCV_ELS_LIST64_CX:
1483 case CMD_IOCB_CLOSE_EXTENDED_CN:
1484 case CMD_IOCB_ABORT_EXTENDED_CN:
1485 case CMD_IOCB_RET_HBQE64_CN:
1486 case CMD_IOCB_FCP_IBIDIR64_CR:
1487 case CMD_IOCB_FCP_IBIDIR64_CX:
1488 case CMD_IOCB_FCP_ITASKMGT64_CX:
1489 case CMD_IOCB_LOGENTRY_CN:
1490 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1491 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1492 __func__, iocb_cmnd);
3163f725
JS
1493 type = LPFC_UNKNOWN_IOCB;
1494 break;
dea3101e
JB
1495 default:
1496 type = LPFC_UNKNOWN_IOCB;
1497 break;
1498 }
1499
1500 return type;
1501}
1502
e59058c4 1503/**
3621a710 1504 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1505 * @phba: Pointer to HBA context object.
1506 *
1507 * This function is called from SLI initialization code
1508 * to configure every ring of the HBA's SLI interface. The
1509 * caller is not required to hold any lock. This function issues
1510 * a config_ring mailbox command for each ring.
1511 * This function returns zero if successful else returns a negative
1512 * error code.
1513 **/
dea3101e 1514static int
ed957684 1515lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1516{
1517 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1518 LPFC_MBOXQ_t *pmb;
1519 MAILBOX_t *pmbox;
1520 int i, rc, ret = 0;
dea3101e 1521
ed957684
JS
1522 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1523 if (!pmb)
1524 return -ENOMEM;
04c68496 1525 pmbox = &pmb->u.mb;
ed957684 1526 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1527 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1528 lpfc_config_ring(phba, i, pmb);
1529 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1530 if (rc != MBX_SUCCESS) {
92d7f7b0 1531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1532 "0446 Adapter failed to init (%d), "
dea3101e
JB
1533 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1534 "ring %d\n",
e8b62011
JS
1535 rc, pmbox->mbxCommand,
1536 pmbox->mbxStatus, i);
2e0fef85 1537 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1538 ret = -ENXIO;
1539 break;
dea3101e
JB
1540 }
1541 }
ed957684
JS
1542 mempool_free(pmb, phba->mbox_mem_pool);
1543 return ret;
dea3101e
JB
1544}
1545
e59058c4 1546/**
3621a710 1547 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 * @piocb: Pointer to the driver iocb object.
1551 *
1552 * This function is called with hbalock held. The function adds the
1553 * new iocb to txcmplq of the given ring. This function always returns
1554 * 0. If this function is called for ELS ring, this function checks if
1555 * there is a vport associated with the ELS command. This function also
1556 * starts els_tmofunc timer if this is an ELS command.
1557 **/
dea3101e 1558static int
2e0fef85
JS
1559lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1560 struct lpfc_iocbq *piocb)
dea3101e 1561{
1c2ba475
JT
1562 lockdep_assert_held(&phba->hbalock);
1563
2319f847 1564 BUG_ON(!piocb);
22466da5 1565
dea3101e 1566 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1567 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1568
92d7f7b0
JS
1569 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1570 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1571 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1572 BUG_ON(!piocb->vport);
1573 if (!(piocb->vport->load_flag & FC_UNLOADING))
1574 mod_timer(&piocb->vport->els_tmofunc,
1575 jiffies +
1576 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1577 }
dea3101e 1578
2e0fef85 1579 return 0;
dea3101e
JB
1580}
1581
e59058c4 1582/**
3621a710 1583 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1584 * @phba: Pointer to HBA context object.
1585 * @pring: Pointer to driver SLI ring object.
1586 *
1587 * This function is called with hbalock held to get next
1588 * iocb in txq of the given ring. If there is any iocb in
1589 * the txq, the function returns first iocb in the list after
1590 * removing the iocb from the list, else it returns NULL.
1591 **/
2a9bf3d0 1592struct lpfc_iocbq *
2e0fef85 1593lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1594{
dea3101e
JB
1595 struct lpfc_iocbq *cmd_iocb;
1596
1c2ba475
JT
1597 lockdep_assert_held(&phba->hbalock);
1598
858c9f6c 1599 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1600 return cmd_iocb;
dea3101e
JB
1601}
1602
e59058c4 1603/**
3621a710 1604 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1605 * @phba: Pointer to HBA context object.
1606 * @pring: Pointer to driver SLI ring object.
1607 *
1608 * This function is called with hbalock held and the caller must post the
1609 * iocb without releasing the lock. If the caller releases the lock,
1610 * iocb slot returned by the function is not guaranteed to be available.
1611 * The function returns pointer to the next available iocb slot if there
1612 * is available slot in the ring, else it returns NULL.
1613 * If the get index of the ring is ahead of the put index, the function
1614 * will post an error attention event to the worker thread to take the
1615 * HBA to offline state.
1616 **/
dea3101e
JB
1617static IOCB_t *
1618lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1619{
34b02dcd 1620 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1621 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1622
1623 lockdep_assert_held(&phba->hbalock);
1624
7e56aa25
JS
1625 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1626 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1627 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1628
7e56aa25
JS
1629 if (unlikely(pring->sli.sli3.local_getidx ==
1630 pring->sli.sli3.next_cmdidx)) {
dea3101e 1631
7e56aa25 1632 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1633
7e56aa25 1634 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1636 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1637 "is bigger than cmd ring %d\n",
e8b62011 1638 pring->ringno,
7e56aa25
JS
1639 pring->sli.sli3.local_getidx,
1640 max_cmd_idx);
dea3101e 1641
2e0fef85 1642 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1643 /*
1644 * All error attention handlers are posted to
1645 * worker thread
1646 */
1647 phba->work_ha |= HA_ERATT;
1648 phba->work_hs = HS_FFER3;
92d7f7b0 1649
5e9d9b82 1650 lpfc_worker_wake_up(phba);
dea3101e
JB
1651
1652 return NULL;
1653 }
1654
7e56aa25 1655 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1656 return NULL;
1657 }
1658
ed957684 1659 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1660}
1661
e59058c4 1662/**
3621a710 1663 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1664 * @phba: Pointer to HBA context object.
1665 * @iocbq: Pointer to driver iocb object.
1666 *
1667 * This function gets an iotag for the iocb. If there is no unused iotag and
1668 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1669 * array and assigns a new iotag.
1670 * The function returns the allocated iotag if successful, else returns zero.
1671 * Zero is not a valid iotag.
1672 * The caller is not required to hold any lock.
1673 **/
604a3e30 1674uint16_t
2e0fef85 1675lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1676{
2e0fef85
JS
1677 struct lpfc_iocbq **new_arr;
1678 struct lpfc_iocbq **old_arr;
604a3e30
JB
1679 size_t new_len;
1680 struct lpfc_sli *psli = &phba->sli;
1681 uint16_t iotag;
dea3101e 1682
2e0fef85 1683 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1684 iotag = psli->last_iotag;
1685 if(++iotag < psli->iocbq_lookup_len) {
1686 psli->last_iotag = iotag;
1687 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1688 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1689 iocbq->iotag = iotag;
1690 return iotag;
2e0fef85 1691 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1692 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1693 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1694 spin_unlock_irq(&phba->hbalock);
1695 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1696 GFP_KERNEL);
1697 if (new_arr) {
2e0fef85 1698 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1699 old_arr = psli->iocbq_lookup;
1700 if (new_len <= psli->iocbq_lookup_len) {
1701 /* highly unprobable case */
1702 kfree(new_arr);
1703 iotag = psli->last_iotag;
1704 if(++iotag < psli->iocbq_lookup_len) {
1705 psli->last_iotag = iotag;
1706 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1707 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1708 iocbq->iotag = iotag;
1709 return iotag;
1710 }
2e0fef85 1711 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1712 return 0;
1713 }
1714 if (psli->iocbq_lookup)
1715 memcpy(new_arr, old_arr,
1716 ((psli->last_iotag + 1) *
311464ec 1717 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1718 psli->iocbq_lookup = new_arr;
1719 psli->iocbq_lookup_len = new_len;
1720 psli->last_iotag = iotag;
1721 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1722 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1723 iocbq->iotag = iotag;
1724 kfree(old_arr);
1725 return iotag;
1726 }
8f6d98d2 1727 } else
2e0fef85 1728 spin_unlock_irq(&phba->hbalock);
dea3101e 1729
bc73905a 1730 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1731 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1732 psli->last_iotag);
dea3101e 1733
604a3e30 1734 return 0;
dea3101e
JB
1735}
1736
e59058c4 1737/**
3621a710 1738 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1739 * @phba: Pointer to HBA context object.
1740 * @pring: Pointer to driver SLI ring object.
1741 * @iocb: Pointer to iocb slot in the ring.
1742 * @nextiocb: Pointer to driver iocb object which need to be
1743 * posted to firmware.
1744 *
1745 * This function is called with hbalock held to post a new iocb to
1746 * the firmware. This function copies the new iocb to ring iocb slot and
1747 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1748 * a completion call back for this iocb else the function will free the
1749 * iocb object.
1750 **/
dea3101e
JB
1751static void
1752lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1753 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1754{
1c2ba475 1755 lockdep_assert_held(&phba->hbalock);
dea3101e 1756 /*
604a3e30 1757 * Set up an iotag
dea3101e 1758 */
604a3e30 1759 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1760
e2a0a9d6 1761
a58cbd52
JS
1762 if (pring->ringno == LPFC_ELS_RING) {
1763 lpfc_debugfs_slow_ring_trc(phba,
1764 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1765 *(((uint32_t *) &nextiocb->iocb) + 4),
1766 *(((uint32_t *) &nextiocb->iocb) + 6),
1767 *(((uint32_t *) &nextiocb->iocb) + 7));
1768 }
1769
dea3101e
JB
1770 /*
1771 * Issue iocb command to adapter
1772 */
92d7f7b0 1773 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1774 wmb();
1775 pring->stats.iocb_cmd++;
1776
1777 /*
1778 * If there is no completion routine to call, we can release the
1779 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1780 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1781 */
1782 if (nextiocb->iocb_cmpl)
1783 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1784 else
2e0fef85 1785 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1786
1787 /*
1788 * Let the HBA know what IOCB slot will be the next one the
1789 * driver will put a command into.
1790 */
7e56aa25
JS
1791 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1792 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1793}
1794
e59058c4 1795/**
3621a710 1796 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1797 * @phba: Pointer to HBA context object.
1798 * @pring: Pointer to driver SLI ring object.
1799 *
1800 * The caller is not required to hold any lock for calling this function.
1801 * This function updates the chip attention bits for the ring to inform firmware
1802 * that there are pending work to be done for this ring and requests an
1803 * interrupt when there is space available in the ring. This function is
1804 * called when the driver is unable to post more iocbs to the ring due
1805 * to unavailability of space in the ring.
1806 **/
dea3101e 1807static void
2e0fef85 1808lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1809{
1810 int ringno = pring->ringno;
1811
1812 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1813
1814 wmb();
1815
1816 /*
1817 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1818 * The HBA will tell us when an IOCB entry is available.
1819 */
1820 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1821 readl(phba->CAregaddr); /* flush */
1822
1823 pring->stats.iocb_cmd_full++;
1824}
1825
e59058c4 1826/**
3621a710 1827 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1828 * @phba: Pointer to HBA context object.
1829 * @pring: Pointer to driver SLI ring object.
1830 *
1831 * This function updates the chip attention register bit for the
1832 * given ring to inform HBA that there is more work to be done
1833 * in this ring. The caller is not required to hold any lock.
1834 **/
dea3101e 1835static void
2e0fef85 1836lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1837{
1838 int ringno = pring->ringno;
1839
1840 /*
1841 * Tell the HBA that there is work to do in this ring.
1842 */
34b02dcd
JS
1843 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1844 wmb();
1845 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1846 readl(phba->CAregaddr); /* flush */
1847 }
dea3101e
JB
1848}
1849
e59058c4 1850/**
3621a710 1851 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1852 * @phba: Pointer to HBA context object.
1853 * @pring: Pointer to driver SLI ring object.
1854 *
1855 * This function is called with hbalock held to post pending iocbs
1856 * in the txq to the firmware. This function is called when driver
1857 * detects space available in the ring.
1858 **/
dea3101e 1859static void
2e0fef85 1860lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1861{
1862 IOCB_t *iocb;
1863 struct lpfc_iocbq *nextiocb;
1864
1c2ba475
JT
1865 lockdep_assert_held(&phba->hbalock);
1866
dea3101e
JB
1867 /*
1868 * Check to see if:
1869 * (a) there is anything on the txq to send
1870 * (b) link is up
1871 * (c) link attention events can be processed (fcp ring only)
1872 * (d) IOCB processing is not blocked by the outstanding mbox command.
1873 */
0e9bb8d7
JS
1874
1875 if (lpfc_is_link_up(phba) &&
1876 (!list_empty(&pring->txq)) &&
895427bd 1877 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1878 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1879
1880 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1881 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1882 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1883
1884 if (iocb)
1885 lpfc_sli_update_ring(phba, pring);
1886 else
1887 lpfc_sli_update_full_ring(phba, pring);
1888 }
1889
1890 return;
1891}
1892
e59058c4 1893/**
3621a710 1894 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1895 * @phba: Pointer to HBA context object.
1896 * @hbqno: HBQ number.
1897 *
1898 * This function is called with hbalock held to get the next
1899 * available slot for the given HBQ. If there is free slot
1900 * available for the HBQ it will return pointer to the next available
1901 * HBQ entry else it will return NULL.
1902 **/
a6ababd2 1903static struct lpfc_hbq_entry *
ed957684
JS
1904lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1905{
1906 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1907
1c2ba475
JT
1908 lockdep_assert_held(&phba->hbalock);
1909
ed957684
JS
1910 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1911 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1912 hbqp->next_hbqPutIdx = 0;
1913
1914 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1915 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1916 uint32_t getidx = le32_to_cpu(raw_index);
1917
1918 hbqp->local_hbqGetIdx = getidx;
1919
1920 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1921 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1922 LOG_SLI | LOG_VPORT,
e8b62011 1923 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1924 "%u is > than hbqp->entry_count %u\n",
e8b62011 1925 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1926 hbqp->entry_count);
1927
1928 phba->link_state = LPFC_HBA_ERROR;
1929 return NULL;
1930 }
1931
1932 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1933 return NULL;
1934 }
1935
51ef4c26
JS
1936 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1937 hbqp->hbqPutIdx;
ed957684
JS
1938}
1939
e59058c4 1940/**
3621a710 1941 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1942 * @phba: Pointer to HBA context object.
1943 *
1944 * This function is called with no lock held to free all the
1945 * hbq buffers while uninitializing the SLI interface. It also
1946 * frees the HBQ buffers returned by the firmware but not yet
1947 * processed by the upper layers.
1948 **/
ed957684
JS
1949void
1950lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1951{
92d7f7b0
JS
1952 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1953 struct hbq_dmabuf *hbq_buf;
3163f725 1954 unsigned long flags;
51ef4c26 1955 int i, hbq_count;
ed957684 1956
51ef4c26 1957 hbq_count = lpfc_sli_hbq_count();
ed957684 1958 /* Return all memory used by all HBQs */
3163f725 1959 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1960 for (i = 0; i < hbq_count; ++i) {
1961 list_for_each_entry_safe(dmabuf, next_dmabuf,
1962 &phba->hbqs[i].hbq_buffer_list, list) {
1963 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1964 list_del(&hbq_buf->dbuf.list);
1965 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1966 }
a8adb832 1967 phba->hbqs[i].buffer_count = 0;
ed957684 1968 }
3163f725
JS
1969
1970 /* Mark the HBQs not in use */
1971 phba->hbq_in_use = 0;
1972 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1973}
1974
e59058c4 1975/**
3621a710 1976 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1977 * @phba: Pointer to HBA context object.
1978 * @hbqno: HBQ number.
1979 * @hbq_buf: Pointer to HBQ buffer.
1980 *
1981 * This function is called with the hbalock held to post a
1982 * hbq buffer to the firmware. If the function finds an empty
1983 * slot in the HBQ, it will post the buffer. The function will return
1984 * pointer to the hbq entry if it successfully post the buffer
1985 * else it will return NULL.
1986 **/
3772a991 1987static int
ed957684 1988lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1989 struct hbq_dmabuf *hbq_buf)
3772a991 1990{
1c2ba475 1991 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1992 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1993}
1994
1995/**
1996 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1997 * @phba: Pointer to HBA context object.
1998 * @hbqno: HBQ number.
1999 * @hbq_buf: Pointer to HBQ buffer.
2000 *
2001 * This function is called with the hbalock held to post a hbq buffer to the
2002 * firmware. If the function finds an empty slot in the HBQ, it will post the
2003 * buffer and place it on the hbq_buffer_list. The function will return zero if
2004 * it successfully post the buffer else it will return an error.
2005 **/
2006static int
2007lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2008 struct hbq_dmabuf *hbq_buf)
ed957684
JS
2009{
2010 struct lpfc_hbq_entry *hbqe;
92d7f7b0 2011 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 2012
1c2ba475 2013 lockdep_assert_held(&phba->hbalock);
ed957684
JS
2014 /* Get next HBQ entry slot to use */
2015 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2016 if (hbqe) {
2017 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2018
92d7f7b0
JS
2019 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2020 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 2021 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 2022 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
2023 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2024 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2025 /* Sync SLIM */
ed957684
JS
2026 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2027 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 2028 /* flush */
ed957684 2029 readl(phba->hbq_put + hbqno);
51ef4c26 2030 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
2031 return 0;
2032 } else
2033 return -ENOMEM;
ed957684
JS
2034}
2035
4f774513
JS
2036/**
2037 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2038 * @phba: Pointer to HBA context object.
2039 * @hbqno: HBQ number.
2040 * @hbq_buf: Pointer to HBQ buffer.
2041 *
2042 * This function is called with the hbalock held to post an RQE to the SLI4
2043 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2044 * the hbq_buffer_list and return zero, otherwise it will return an error.
2045 **/
2046static int
2047lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2048 struct hbq_dmabuf *hbq_buf)
2049{
2050 int rc;
2051 struct lpfc_rqe hrqe;
2052 struct lpfc_rqe drqe;
895427bd
JS
2053 struct lpfc_queue *hrq;
2054 struct lpfc_queue *drq;
2055
2056 if (hbqno != LPFC_ELS_HBQ)
2057 return 1;
2058 hrq = phba->sli4_hba.hdr_rq;
2059 drq = phba->sli4_hba.dat_rq;
4f774513 2060
1c2ba475 2061 lockdep_assert_held(&phba->hbalock);
4f774513
JS
2062 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2063 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2064 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2065 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 2066 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
2067 if (rc < 0)
2068 return rc;
895427bd 2069 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
2070 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2071 return 0;
2072}
2073
e59058c4 2074/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
2075static struct lpfc_hbq_init lpfc_els_hbq = {
2076 .rn = 1,
def9c7a9 2077 .entry_count = 256,
92d7f7b0
JS
2078 .mask_count = 0,
2079 .profile = 0,
51ef4c26 2080 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 2081 .buffer_count = 0,
a257bf90
JS
2082 .init_count = 40,
2083 .add_count = 40,
92d7f7b0 2084};
ed957684 2085
e59058c4 2086/* Array of HBQs */
78b2d852 2087struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
2088 &lpfc_els_hbq,
2089};
ed957684 2090
e59058c4 2091/**
3621a710 2092 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
2093 * @phba: Pointer to HBA context object.
2094 * @hbqno: HBQ number.
2095 * @count: Number of HBQ buffers to be posted.
2096 *
d7c255b2
JS
2097 * This function is called with no lock held to post more hbq buffers to the
2098 * given HBQ. The function returns the number of HBQ buffers successfully
2099 * posted.
e59058c4 2100 **/
311464ec 2101static int
92d7f7b0 2102lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 2103{
d7c255b2 2104 uint32_t i, posted = 0;
3163f725 2105 unsigned long flags;
92d7f7b0 2106 struct hbq_dmabuf *hbq_buffer;
d7c255b2 2107 LIST_HEAD(hbq_buf_list);
eafe1df9 2108 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 2109 return 0;
51ef4c26 2110
d7c255b2
JS
2111 if ((phba->hbqs[hbqno].buffer_count + count) >
2112 lpfc_hbq_defs[hbqno]->entry_count)
2113 count = lpfc_hbq_defs[hbqno]->entry_count -
2114 phba->hbqs[hbqno].buffer_count;
2115 if (!count)
2116 return 0;
2117 /* Allocate HBQ entries */
2118 for (i = 0; i < count; i++) {
2119 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2120 if (!hbq_buffer)
2121 break;
2122 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2123 }
3163f725
JS
2124 /* Check whether HBQ is still in use */
2125 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 2126 if (!phba->hbq_in_use)
d7c255b2
JS
2127 goto err;
2128 while (!list_empty(&hbq_buf_list)) {
2129 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2130 dbuf.list);
2131 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2132 (hbqno << 16));
3772a991 2133 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 2134 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
2135 posted++;
2136 } else
51ef4c26 2137 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 2138 }
3163f725 2139 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2140 return posted;
2141err:
eafe1df9 2142 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2143 while (!list_empty(&hbq_buf_list)) {
2144 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2145 dbuf.list);
2146 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2147 }
2148 return 0;
ed957684
JS
2149}
2150
e59058c4 2151/**
3621a710 2152 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
2153 * @phba: Pointer to HBA context object.
2154 * @qno: HBQ number.
2155 *
2156 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
2157 * is called with no lock held. The function returns the number of HBQ entries
2158 * successfully allocated.
e59058c4 2159 **/
92d7f7b0
JS
2160int
2161lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 2162{
def9c7a9
JS
2163 if (phba->sli_rev == LPFC_SLI_REV4)
2164 return 0;
2165 else
2166 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2167 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 2168}
ed957684 2169
e59058c4 2170/**
3621a710 2171 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2172 * @phba: Pointer to HBA context object.
2173 * @qno: HBQ queue number.
2174 *
2175 * This function is called from SLI initialization code path with
2176 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2177 * function returns the number of HBQ entries successfully allocated.
e59058c4 2178 **/
a6ababd2 2179static int
92d7f7b0
JS
2180lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2181{
def9c7a9
JS
2182 if (phba->sli_rev == LPFC_SLI_REV4)
2183 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2184 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2185 else
2186 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2187 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2188}
2189
3772a991
JS
2190/**
2191 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2192 * @phba: Pointer to HBA context object.
2193 * @hbqno: HBQ number.
2194 *
2195 * This function removes the first hbq buffer on an hbq list and returns a
2196 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2197 **/
2198static struct hbq_dmabuf *
2199lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2200{
2201 struct lpfc_dmabuf *d_buf;
2202
2203 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2204 if (!d_buf)
2205 return NULL;
2206 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2207}
2208
2d7dbc4c
JS
2209/**
2210 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2211 * @phba: Pointer to HBA context object.
2212 * @hbqno: HBQ number.
2213 *
2214 * This function removes the first RQ buffer on an RQ buffer list and returns a
2215 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2216 **/
2217static struct rqb_dmabuf *
2218lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2219{
2220 struct lpfc_dmabuf *h_buf;
2221 struct lpfc_rqb *rqbp;
2222
2223 rqbp = hrq->rqbp;
2224 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2225 struct lpfc_dmabuf, list);
2226 if (!h_buf)
2227 return NULL;
2228 rqbp->buffer_count--;
2229 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2230}
2231
e59058c4 2232/**
3621a710 2233 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2234 * @phba: Pointer to HBA context object.
2235 * @tag: Tag of the hbq buffer.
2236 *
71892418
SH
2237 * This function searches for the hbq buffer associated with the given tag in
2238 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2239 * otherwise it returns NULL.
e59058c4 2240 **/
a6ababd2 2241static struct hbq_dmabuf *
92d7f7b0 2242lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2243{
92d7f7b0
JS
2244 struct lpfc_dmabuf *d_buf;
2245 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2246 uint32_t hbqno;
2247
2248 hbqno = tag >> 16;
a0a74e45 2249 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2250 return NULL;
ed957684 2251
3772a991 2252 spin_lock_irq(&phba->hbalock);
51ef4c26 2253 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2254 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2255 if (hbq_buf->tag == tag) {
3772a991 2256 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2257 return hbq_buf;
ed957684
JS
2258 }
2259 }
3772a991 2260 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2262 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2263 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2264 return NULL;
ed957684
JS
2265}
2266
e59058c4 2267/**
3621a710 2268 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2269 * @phba: Pointer to HBA context object.
2270 * @hbq_buffer: Pointer to HBQ buffer.
2271 *
2272 * This function is called with hbalock. This function gives back
2273 * the hbq buffer to firmware. If the HBQ does not have space to
2274 * post the buffer, it will free the buffer.
2275 **/
ed957684 2276void
51ef4c26 2277lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2278{
2279 uint32_t hbqno;
2280
51ef4c26
JS
2281 if (hbq_buffer) {
2282 hbqno = hbq_buffer->tag >> 16;
3772a991 2283 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2284 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2285 }
2286}
2287
e59058c4 2288/**
3621a710 2289 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2290 * @mbxCommand: mailbox command code.
2291 *
2292 * This function is called by the mailbox event handler function to verify
2293 * that the completed mailbox command is a legitimate mailbox command. If the
2294 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2295 * and the mailbox event handler will take the HBA offline.
2296 **/
dea3101e
JB
2297static int
2298lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2299{
2300 uint8_t ret;
2301
2302 switch (mbxCommand) {
2303 case MBX_LOAD_SM:
2304 case MBX_READ_NV:
2305 case MBX_WRITE_NV:
a8adb832 2306 case MBX_WRITE_VPARMS:
dea3101e
JB
2307 case MBX_RUN_BIU_DIAG:
2308 case MBX_INIT_LINK:
2309 case MBX_DOWN_LINK:
2310 case MBX_CONFIG_LINK:
2311 case MBX_CONFIG_RING:
2312 case MBX_RESET_RING:
2313 case MBX_READ_CONFIG:
2314 case MBX_READ_RCONFIG:
2315 case MBX_READ_SPARM:
2316 case MBX_READ_STATUS:
2317 case MBX_READ_RPI:
2318 case MBX_READ_XRI:
2319 case MBX_READ_REV:
2320 case MBX_READ_LNK_STAT:
2321 case MBX_REG_LOGIN:
2322 case MBX_UNREG_LOGIN:
dea3101e
JB
2323 case MBX_CLEAR_LA:
2324 case MBX_DUMP_MEMORY:
2325 case MBX_DUMP_CONTEXT:
2326 case MBX_RUN_DIAGS:
2327 case MBX_RESTART:
2328 case MBX_UPDATE_CFG:
2329 case MBX_DOWN_LOAD:
2330 case MBX_DEL_LD_ENTRY:
2331 case MBX_RUN_PROGRAM:
2332 case MBX_SET_MASK:
09372820 2333 case MBX_SET_VARIABLE:
dea3101e 2334 case MBX_UNREG_D_ID:
41415862 2335 case MBX_KILL_BOARD:
dea3101e 2336 case MBX_CONFIG_FARP:
41415862 2337 case MBX_BEACON:
dea3101e
JB
2338 case MBX_LOAD_AREA:
2339 case MBX_RUN_BIU_DIAG64:
2340 case MBX_CONFIG_PORT:
2341 case MBX_READ_SPARM64:
2342 case MBX_READ_RPI64:
2343 case MBX_REG_LOGIN64:
76a95d75 2344 case MBX_READ_TOPOLOGY:
09372820 2345 case MBX_WRITE_WWN:
dea3101e
JB
2346 case MBX_SET_DEBUG:
2347 case MBX_LOAD_EXP_ROM:
57127f15 2348 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2349 case MBX_REG_VPI:
2350 case MBX_UNREG_VPI:
858c9f6c 2351 case MBX_HEARTBEAT:
84774a4d
JS
2352 case MBX_PORT_CAPABILITIES:
2353 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2354 case MBX_SLI4_CONFIG:
2355 case MBX_SLI4_REQ_FTRS:
2356 case MBX_REG_FCFI:
2357 case MBX_UNREG_FCFI:
2358 case MBX_REG_VFI:
2359 case MBX_UNREG_VFI:
2360 case MBX_INIT_VPI:
2361 case MBX_INIT_VFI:
2362 case MBX_RESUME_RPI:
c7495937
JS
2363 case MBX_READ_EVENT_LOG_STATUS:
2364 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2365 case MBX_SECURITY_MGMT:
2366 case MBX_AUTH_PORT:
940eb687 2367 case MBX_ACCESS_VDATA:
dea3101e
JB
2368 ret = mbxCommand;
2369 break;
2370 default:
2371 ret = MBX_SHUTDOWN;
2372 break;
2373 }
2e0fef85 2374 return ret;
dea3101e 2375}
e59058c4
JS
2376
2377/**
3621a710 2378 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2379 * @phba: Pointer to HBA context object.
2380 * @pmboxq: Pointer to mailbox command.
2381 *
2382 * This is completion handler function for mailbox commands issued from
2383 * lpfc_sli_issue_mbox_wait function. This function is called by the
2384 * mailbox event handler function with no lock held. This function
2385 * will wake up thread waiting on the wait queue pointed by context1
2386 * of the mailbox.
2387 **/
04c68496 2388void
2e0fef85 2389lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2390{
2391 wait_queue_head_t *pdone_q;
858c9f6c 2392 unsigned long drvr_flag;
dea3101e
JB
2393
2394 /*
2395 * If pdone_q is empty, the driver thread gave up waiting and
2396 * continued running.
2397 */
7054a606 2398 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2399 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2400 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2401 if (pdone_q)
2402 wake_up_interruptible(pdone_q);
858c9f6c 2403 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2404 return;
2405}
2406
e59058c4
JS
2407
2408/**
3621a710 2409 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2410 * @phba: Pointer to HBA context object.
2411 * @pmb: Pointer to mailbox object.
2412 *
2413 * This function is the default mailbox completion handler. It
2414 * frees the memory resources associated with the completed mailbox
2415 * command. If the completed command is a REG_LOGIN mailbox command,
2416 * this function will issue a UREG_LOGIN to re-claim the RPI.
2417 **/
dea3101e 2418void
2e0fef85 2419lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2420{
d439d286 2421 struct lpfc_vport *vport = pmb->vport;
dea3101e 2422 struct lpfc_dmabuf *mp;
d439d286 2423 struct lpfc_nodelist *ndlp;
5af5eee7 2424 struct Scsi_Host *shost;
04c68496 2425 uint16_t rpi, vpi;
7054a606
JS
2426 int rc;
2427
dea3101e 2428 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2429
dea3101e
JB
2430 if (mp) {
2431 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2432 kfree(mp);
2433 }
7054a606
JS
2434
2435 /*
2436 * If a REG_LOGIN succeeded after node is destroyed or node
2437 * is in re-discovery driver need to cleanup the RPI.
2438 */
2e0fef85 2439 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2440 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2441 !pmb->u.mb.mbxStatus) {
2442 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2443 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2444 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2445 pmb->vport = vport;
92d7f7b0 2446 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2447 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2448 if (rc != MBX_NOT_FINISHED)
2449 return;
2450 }
2451
695a814e
JS
2452 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2453 !(phba->pport->load_flag & FC_UNLOADING) &&
2454 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2455 shost = lpfc_shost_from_vport(vport);
2456 spin_lock_irq(shost->host_lock);
2457 vport->vpi_state |= LPFC_VPI_REGISTERED;
2458 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2459 spin_unlock_irq(shost->host_lock);
695a814e
JS
2460 }
2461
d439d286
JS
2462 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2463 ndlp = (struct lpfc_nodelist *)pmb->context2;
2464 lpfc_nlp_put(ndlp);
2465 pmb->context2 = NULL;
2466 }
2467
dcf2a4e0
JS
2468 /* Check security permission status on INIT_LINK mailbox command */
2469 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2470 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2472 "2860 SLI authentication is required "
2473 "for INIT_LINK but has not done yet\n");
2474
04c68496
JS
2475 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2476 lpfc_sli4_mbox_cmd_free(phba, pmb);
2477 else
2478 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2479}
be6bb941
JS
2480 /**
2481 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2482 * @phba: Pointer to HBA context object.
2483 * @pmb: Pointer to mailbox object.
2484 *
2485 * This function is the unreg rpi mailbox completion handler. It
2486 * frees the memory resources associated with the completed mailbox
2487 * command. An additional refrenece is put on the ndlp to prevent
2488 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2489 * the unreg mailbox command completes, this routine puts the
2490 * reference back.
2491 *
2492 **/
2493void
2494lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2495{
2496 struct lpfc_vport *vport = pmb->vport;
2497 struct lpfc_nodelist *ndlp;
2498
2499 ndlp = pmb->context1;
2500 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501 if (phba->sli_rev == LPFC_SLI_REV4 &&
2502 (bf_get(lpfc_sli_intf_if_type,
27d6ac0a 2503 &phba->sli4_hba.sli_intf) >=
be6bb941
JS
2504 LPFC_SLI_INTF_IF_TYPE_2)) {
2505 if (ndlp) {
2506 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2507 "0010 UNREG_LOGIN vpi:%x "
2508 "rpi:%x DID:%x map:%x %p\n",
2509 vport->vpi, ndlp->nlp_rpi,
2510 ndlp->nlp_DID,
2511 ndlp->nlp_usg_map, ndlp);
7c5e518c 2512 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2513 lpfc_nlp_put(ndlp);
2514 }
2515 }
2516 }
2517
2518 mempool_free(pmb, phba->mbox_mem_pool);
2519}
dea3101e 2520
e59058c4 2521/**
3621a710 2522 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2523 * @phba: Pointer to HBA context object.
2524 *
2525 * This function is called with no lock held. This function processes all
2526 * the completed mailbox commands and gives it to upper layers. The interrupt
2527 * service routine processes mailbox completion interrupt and adds completed
2528 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2529 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2530 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2531 * function returns the mailbox commands to the upper layer by calling the
2532 * completion handler function of each mailbox.
2533 **/
dea3101e 2534int
2e0fef85 2535lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2536{
92d7f7b0 2537 MAILBOX_t *pmbox;
dea3101e 2538 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2539 int rc;
2540 LIST_HEAD(cmplq);
dea3101e
JB
2541
2542 phba->sli.slistat.mbox_event++;
2543
92d7f7b0
JS
2544 /* Get all completed mailboxe buffers into the cmplq */
2545 spin_lock_irq(&phba->hbalock);
2546 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2547 spin_unlock_irq(&phba->hbalock);
dea3101e 2548
92d7f7b0
JS
2549 /* Get a Mailbox buffer to setup mailbox commands for callback */
2550 do {
2551 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2552 if (pmb == NULL)
2553 break;
2e0fef85 2554
04c68496 2555 pmbox = &pmb->u.mb;
dea3101e 2556
858c9f6c
JS
2557 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2558 if (pmb->vport) {
2559 lpfc_debugfs_disc_trc(pmb->vport,
2560 LPFC_DISC_TRC_MBOX_VPORT,
2561 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2562 (uint32_t)pmbox->mbxCommand,
2563 pmbox->un.varWords[0],
2564 pmbox->un.varWords[1]);
2565 }
2566 else {
2567 lpfc_debugfs_disc_trc(phba->pport,
2568 LPFC_DISC_TRC_MBOX,
2569 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2570 (uint32_t)pmbox->mbxCommand,
2571 pmbox->un.varWords[0],
2572 pmbox->un.varWords[1]);
2573 }
2574 }
2575
dea3101e
JB
2576 /*
2577 * It is a fatal error if unknown mbox command completion.
2578 */
2579 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2580 MBX_SHUTDOWN) {
af901ca1 2581 /* Unknown mailbox command compl */
92d7f7b0 2582 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2583 "(%d):0323 Unknown Mailbox command "
a183a15f 2584 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2585 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2586 pmbox->mbxCommand,
a183a15f
JS
2587 lpfc_sli_config_mbox_subsys_get(phba,
2588 pmb),
2589 lpfc_sli_config_mbox_opcode_get(phba,
2590 pmb));
2e0fef85 2591 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2592 phba->work_hs = HS_FFER3;
2593 lpfc_handle_eratt(phba);
92d7f7b0 2594 continue;
dea3101e
JB
2595 }
2596
dea3101e
JB
2597 if (pmbox->mbxStatus) {
2598 phba->sli.slistat.mbox_stat_err++;
2599 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2600 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2601 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2602 LOG_MBOX | LOG_SLI,
2603 "(%d):0305 Mbox cmd cmpl "
2604 "error - RETRYing Data: x%x "
2605 "(x%x/x%x) x%x x%x x%x\n",
2606 pmb->vport ? pmb->vport->vpi : 0,
2607 pmbox->mbxCommand,
2608 lpfc_sli_config_mbox_subsys_get(phba,
2609 pmb),
2610 lpfc_sli_config_mbox_opcode_get(phba,
2611 pmb),
2612 pmbox->mbxStatus,
2613 pmbox->un.varWords[0],
2614 pmb->vport->port_state);
dea3101e
JB
2615 pmbox->mbxStatus = 0;
2616 pmbox->mbxOwner = OWN_HOST;
dea3101e 2617 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2618 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2619 continue;
dea3101e
JB
2620 }
2621 }
2622
2623 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2624 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2625 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2626 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2627 "x%x x%x x%x\n",
92d7f7b0 2628 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2629 pmbox->mbxCommand,
a183a15f
JS
2630 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2631 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2632 pmb->mbox_cmpl,
2633 *((uint32_t *) pmbox),
2634 pmbox->un.varWords[0],
2635 pmbox->un.varWords[1],
2636 pmbox->un.varWords[2],
2637 pmbox->un.varWords[3],
2638 pmbox->un.varWords[4],
2639 pmbox->un.varWords[5],
2640 pmbox->un.varWords[6],
e74c03c8
JS
2641 pmbox->un.varWords[7],
2642 pmbox->un.varWords[8],
2643 pmbox->un.varWords[9],
2644 pmbox->un.varWords[10]);
dea3101e 2645
92d7f7b0 2646 if (pmb->mbox_cmpl)
dea3101e 2647 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2648 } while (1);
2649 return 0;
2650}
dea3101e 2651
e59058c4 2652/**
3621a710 2653 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2654 * @phba: Pointer to HBA context object.
2655 * @pring: Pointer to driver SLI ring object.
2656 * @tag: buffer tag.
2657 *
2658 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2659 * is set in the tag the buffer is posted for a particular exchange,
2660 * the function will return the buffer without replacing the buffer.
2661 * If the buffer is for unsolicited ELS or CT traffic, this function
2662 * returns the buffer and also posts another buffer to the firmware.
2663 **/
76bb24ef
JS
2664static struct lpfc_dmabuf *
2665lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2666 struct lpfc_sli_ring *pring,
2667 uint32_t tag)
76bb24ef 2668{
9f1e1b50
JS
2669 struct hbq_dmabuf *hbq_entry;
2670
76bb24ef
JS
2671 if (tag & QUE_BUFTAG_BIT)
2672 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2673 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2674 if (!hbq_entry)
2675 return NULL;
2676 return &hbq_entry->dbuf;
76bb24ef 2677}
57127f15 2678
3772a991
JS
2679/**
2680 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2681 * @phba: Pointer to HBA context object.
2682 * @pring: Pointer to driver SLI ring object.
2683 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2684 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2685 * @fch_type: the type for the first frame of the sequence.
2686 *
2687 * This function is called with no lock held. This function uses the r_ctl and
2688 * type of the received sequence to find the correct callback function to call
2689 * to process the sequence.
2690 **/
2691static int
2692lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2693 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2694 uint32_t fch_type)
2695{
2696 int i;
2697
f358dd0c
JS
2698 switch (fch_type) {
2699 case FC_TYPE_NVME:
d613b6a7 2700 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2701 return 1;
2702 default:
2703 break;
2704 }
2705
3772a991
JS
2706 /* unSolicited Responses */
2707 if (pring->prt[0].profile) {
2708 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2709 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2710 saveq);
2711 return 1;
2712 }
2713 /* We must search, based on rctl / type
2714 for the right routine */
2715 for (i = 0; i < pring->num_mask; i++) {
2716 if ((pring->prt[i].rctl == fch_r_ctl) &&
2717 (pring->prt[i].type == fch_type)) {
2718 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2719 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2720 (phba, pring, saveq);
2721 return 1;
2722 }
2723 }
2724 return 0;
2725}
e59058c4
JS
2726
2727/**
3621a710 2728 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2729 * @phba: Pointer to HBA context object.
2730 * @pring: Pointer to driver SLI ring object.
2731 * @saveq: Pointer to the unsolicited iocb.
2732 *
2733 * This function is called with no lock held by the ring event handler
2734 * when there is an unsolicited iocb posted to the response ring by the
2735 * firmware. This function gets the buffer associated with the iocbs
2736 * and calls the event handler for the ring. This function handles both
2737 * qring buffers and hbq buffers.
2738 * When the function returns 1 the caller can free the iocb object otherwise
2739 * upper layer functions will free the iocb objects.
2740 **/
dea3101e
JB
2741static int
2742lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2743 struct lpfc_iocbq *saveq)
2744{
2745 IOCB_t * irsp;
2746 WORD5 * w5p;
2747 uint32_t Rctl, Type;
76bb24ef 2748 struct lpfc_iocbq *iocbq;
3163f725 2749 struct lpfc_dmabuf *dmzbuf;
dea3101e 2750
dea3101e 2751 irsp = &(saveq->iocb);
57127f15
JS
2752
2753 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2754 if (pring->lpfc_sli_rcv_async_status)
2755 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2756 else
2757 lpfc_printf_log(phba,
2758 KERN_WARNING,
2759 LOG_SLI,
2760 "0316 Ring %d handler: unexpected "
2761 "ASYNC_STATUS iocb received evt_code "
2762 "0x%x\n",
2763 pring->ringno,
2764 irsp->un.asyncstat.evt_code);
2765 return 1;
2766 }
2767
3163f725
JS
2768 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2769 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2770 if (irsp->ulpBdeCount > 0) {
2771 dmzbuf = lpfc_sli_get_buff(phba, pring,
2772 irsp->un.ulpWord[3]);
2773 lpfc_in_buf_free(phba, dmzbuf);
2774 }
2775
2776 if (irsp->ulpBdeCount > 1) {
2777 dmzbuf = lpfc_sli_get_buff(phba, pring,
2778 irsp->unsli3.sli3Words[3]);
2779 lpfc_in_buf_free(phba, dmzbuf);
2780 }
2781
2782 if (irsp->ulpBdeCount > 2) {
2783 dmzbuf = lpfc_sli_get_buff(phba, pring,
2784 irsp->unsli3.sli3Words[7]);
2785 lpfc_in_buf_free(phba, dmzbuf);
2786 }
2787
2788 return 1;
2789 }
2790
92d7f7b0 2791 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2792 if (irsp->ulpBdeCount != 0) {
2793 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2794 irsp->un.ulpWord[3]);
2795 if (!saveq->context2)
2796 lpfc_printf_log(phba,
2797 KERN_ERR,
2798 LOG_SLI,
2799 "0341 Ring %d Cannot find buffer for "
2800 "an unsolicited iocb. tag 0x%x\n",
2801 pring->ringno,
2802 irsp->un.ulpWord[3]);
76bb24ef
JS
2803 }
2804 if (irsp->ulpBdeCount == 2) {
2805 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2806 irsp->unsli3.sli3Words[7]);
2807 if (!saveq->context3)
2808 lpfc_printf_log(phba,
2809 KERN_ERR,
2810 LOG_SLI,
2811 "0342 Ring %d Cannot find buffer for an"
2812 " unsolicited iocb. tag 0x%x\n",
2813 pring->ringno,
2814 irsp->unsli3.sli3Words[7]);
2815 }
2816 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2817 irsp = &(iocbq->iocb);
76bb24ef
JS
2818 if (irsp->ulpBdeCount != 0) {
2819 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2820 irsp->un.ulpWord[3]);
9c2face6 2821 if (!iocbq->context2)
76bb24ef
JS
2822 lpfc_printf_log(phba,
2823 KERN_ERR,
2824 LOG_SLI,
2825 "0343 Ring %d Cannot find "
2826 "buffer for an unsolicited iocb"
2827 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2828 irsp->un.ulpWord[3]);
76bb24ef
JS
2829 }
2830 if (irsp->ulpBdeCount == 2) {
2831 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2832 irsp->unsli3.sli3Words[7]);
9c2face6 2833 if (!iocbq->context3)
76bb24ef
JS
2834 lpfc_printf_log(phba,
2835 KERN_ERR,
2836 LOG_SLI,
2837 "0344 Ring %d Cannot find "
2838 "buffer for an unsolicited "
2839 "iocb. tag 0x%x\n",
2840 pring->ringno,
2841 irsp->unsli3.sli3Words[7]);
2842 }
2843 }
92d7f7b0 2844 }
9c2face6
JS
2845 if (irsp->ulpBdeCount != 0 &&
2846 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2847 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2848 int found = 0;
2849
2850 /* search continue save q for same XRI */
2851 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2852 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2853 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2854 list_add_tail(&saveq->list, &iocbq->list);
2855 found = 1;
2856 break;
2857 }
2858 }
2859 if (!found)
2860 list_add_tail(&saveq->clist,
2861 &pring->iocb_continue_saveq);
2862 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2863 list_del_init(&iocbq->clist);
2864 saveq = iocbq;
2865 irsp = &(saveq->iocb);
2866 } else
2867 return 0;
2868 }
2869 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2870 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2871 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2872 Rctl = FC_RCTL_ELS_REQ;
2873 Type = FC_TYPE_ELS;
9c2face6
JS
2874 } else {
2875 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2876 Rctl = w5p->hcsw.Rctl;
2877 Type = w5p->hcsw.Type;
2878
2879 /* Firmware Workaround */
2880 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2881 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2882 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2883 Rctl = FC_RCTL_ELS_REQ;
2884 Type = FC_TYPE_ELS;
9c2face6
JS
2885 w5p->hcsw.Rctl = Rctl;
2886 w5p->hcsw.Type = Type;
2887 }
2888 }
92d7f7b0 2889
3772a991 2890 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2891 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2892 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2893 "Type x%x received\n",
e8b62011 2894 pring->ringno, Rctl, Type);
3772a991 2895
92d7f7b0 2896 return 1;
dea3101e
JB
2897}
2898
e59058c4 2899/**
3621a710 2900 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2901 * @phba: Pointer to HBA context object.
2902 * @pring: Pointer to driver SLI ring object.
2903 * @prspiocb: Pointer to response iocb object.
2904 *
2905 * This function looks up the iocb_lookup table to get the command iocb
2906 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2907 * response iocb. This function is called with the hbalock held
2908 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2909 * This function returns the command iocb object if it finds the command
2910 * iocb else returns NULL.
2911 **/
dea3101e 2912static struct lpfc_iocbq *
2e0fef85
JS
2913lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2914 struct lpfc_sli_ring *pring,
2915 struct lpfc_iocbq *prspiocb)
dea3101e 2916{
dea3101e
JB
2917 struct lpfc_iocbq *cmd_iocb = NULL;
2918 uint16_t iotag;
1c2ba475 2919 lockdep_assert_held(&phba->hbalock);
dea3101e 2920
604a3e30
JB
2921 iotag = prspiocb->iocb.ulpIoTag;
2922
2923 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2924 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2925 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2926 /* remove from txcmpl queue list */
2927 list_del_init(&cmd_iocb->list);
4f2e66c6 2928 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2929 return cmd_iocb;
2a9bf3d0 2930 }
dea3101e
JB
2931 }
2932
dea3101e 2933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2934 "0317 iotag x%x is out of "
604a3e30 2935 "range: max iotag x%x wd0 x%x\n",
e8b62011 2936 iotag, phba->sli.last_iotag,
604a3e30 2937 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2938 return NULL;
2939}
2940
3772a991
JS
2941/**
2942 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2943 * @phba: Pointer to HBA context object.
2944 * @pring: Pointer to driver SLI ring object.
2945 * @iotag: IOCB tag.
2946 *
2947 * This function looks up the iocb_lookup table to get the command iocb
2948 * corresponding to the given iotag. This function is called with the
2949 * hbalock held.
2950 * This function returns the command iocb object if it finds the command
2951 * iocb else returns NULL.
2952 **/
2953static struct lpfc_iocbq *
2954lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2955 struct lpfc_sli_ring *pring, uint16_t iotag)
2956{
895427bd 2957 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2958
1c2ba475 2959 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2960 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2961 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2962 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2963 /* remove from txcmpl queue list */
2964 list_del_init(&cmd_iocb->list);
2965 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2966 return cmd_iocb;
2a9bf3d0 2967 }
3772a991 2968 }
89533e9b 2969
3772a991 2970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2971 "0372 iotag x%x lookup error: max iotag (x%x) "
2972 "iocb_flag x%x\n",
2973 iotag, phba->sli.last_iotag,
2974 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
2975 return NULL;
2976}
2977
e59058c4 2978/**
3621a710 2979 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2980 * @phba: Pointer to HBA context object.
2981 * @pring: Pointer to driver SLI ring object.
2982 * @saveq: Pointer to the response iocb to be processed.
2983 *
2984 * This function is called by the ring event handler for non-fcp
2985 * rings when there is a new response iocb in the response ring.
2986 * The caller is not required to hold any locks. This function
2987 * gets the command iocb associated with the response iocb and
2988 * calls the completion handler for the command iocb. If there
2989 * is no completion handler, the function will free the resources
2990 * associated with command iocb. If the response iocb is for
2991 * an already aborted command iocb, the status of the completion
2992 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2993 * This function always returns 1.
2994 **/
dea3101e 2995static int
2e0fef85 2996lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2997 struct lpfc_iocbq *saveq)
2998{
2e0fef85 2999 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
3000 int rc = 1;
3001 unsigned long iflag;
3002
3003 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
3004 if (phba->sli_rev == LPFC_SLI_REV4)
3005 spin_lock_irqsave(&pring->ring_lock, iflag);
3006 else
3007 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 3008 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
3009 if (phba->sli_rev == LPFC_SLI_REV4)
3010 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3011 else
3012 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 3013
dea3101e
JB
3014 if (cmdiocbp) {
3015 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
3016 /*
3017 * If an ELS command failed send an event to mgmt
3018 * application.
3019 */
3020 if (saveq->iocb.ulpStatus &&
3021 (pring->ringno == LPFC_ELS_RING) &&
3022 (cmdiocbp->iocb.ulpCommand ==
3023 CMD_ELS_REQUEST64_CR))
3024 lpfc_send_els_failure_event(phba,
3025 cmdiocbp, saveq);
3026
dea3101e
JB
3027 /*
3028 * Post all ELS completions to the worker thread.
3029 * All other are passed to the completion callback.
3030 */
3031 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
3032 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3033 (cmdiocbp->iocb_flag &
3034 LPFC_DRIVER_ABORTED)) {
3035 spin_lock_irqsave(&phba->hbalock,
3036 iflag);
07951076
JS
3037 cmdiocbp->iocb_flag &=
3038 ~LPFC_DRIVER_ABORTED;
341af102
JS
3039 spin_unlock_irqrestore(&phba->hbalock,
3040 iflag);
07951076
JS
3041 saveq->iocb.ulpStatus =
3042 IOSTAT_LOCAL_REJECT;
3043 saveq->iocb.un.ulpWord[4] =
3044 IOERR_SLI_ABORTED;
0ff10d46
JS
3045
3046 /* Firmware could still be in progress
3047 * of DMAing payload, so don't free data
3048 * buffer till after a hbeat.
3049 */
341af102
JS
3050 spin_lock_irqsave(&phba->hbalock,
3051 iflag);
0ff10d46 3052 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
3053 spin_unlock_irqrestore(&phba->hbalock,
3054 iflag);
3055 }
0f65ff68
JS
3056 if (phba->sli_rev == LPFC_SLI_REV4) {
3057 if (saveq->iocb_flag &
3058 LPFC_EXCHANGE_BUSY) {
3059 /* Set cmdiocb flag for the
3060 * exchange busy so sgl (xri)
3061 * will not be released until
3062 * the abort xri is received
3063 * from hba.
3064 */
3065 spin_lock_irqsave(
3066 &phba->hbalock, iflag);
3067 cmdiocbp->iocb_flag |=
3068 LPFC_EXCHANGE_BUSY;
3069 spin_unlock_irqrestore(
3070 &phba->hbalock, iflag);
3071 }
3072 if (cmdiocbp->iocb_flag &
3073 LPFC_DRIVER_ABORTED) {
3074 /*
3075 * Clear LPFC_DRIVER_ABORTED
3076 * bit in case it was driver
3077 * initiated abort.
3078 */
3079 spin_lock_irqsave(
3080 &phba->hbalock, iflag);
3081 cmdiocbp->iocb_flag &=
3082 ~LPFC_DRIVER_ABORTED;
3083 spin_unlock_irqrestore(
3084 &phba->hbalock, iflag);
3085 cmdiocbp->iocb.ulpStatus =
3086 IOSTAT_LOCAL_REJECT;
3087 cmdiocbp->iocb.un.ulpWord[4] =
3088 IOERR_ABORT_REQUESTED;
3089 /*
3090 * For SLI4, irsiocb contains
3091 * NO_XRI in sli_xritag, it
3092 * shall not affect releasing
3093 * sgl (xri) process.
3094 */
3095 saveq->iocb.ulpStatus =
3096 IOSTAT_LOCAL_REJECT;
3097 saveq->iocb.un.ulpWord[4] =
3098 IOERR_SLI_ABORTED;
3099 spin_lock_irqsave(
3100 &phba->hbalock, iflag);
3101 saveq->iocb_flag |=
3102 LPFC_DELAY_MEM_FREE;
3103 spin_unlock_irqrestore(
3104 &phba->hbalock, iflag);
3105 }
07951076 3106 }
dea3101e 3107 }
2e0fef85 3108 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
3109 } else
3110 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
3111 } else {
3112 /*
3113 * Unknown initiating command based on the response iotag.
3114 * This could be the case on the ELS ring because of
3115 * lpfc_els_abort().
3116 */
3117 if (pring->ringno != LPFC_ELS_RING) {
3118 /*
3119 * Ring <ringno> handler: unexpected completion IoTag
3120 * <IoTag>
3121 */
a257bf90 3122 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
3123 "0322 Ring %d handler: "
3124 "unexpected completion IoTag x%x "
3125 "Data: x%x x%x x%x x%x\n",
3126 pring->ringno,
3127 saveq->iocb.ulpIoTag,
3128 saveq->iocb.ulpStatus,
3129 saveq->iocb.un.ulpWord[4],
3130 saveq->iocb.ulpCommand,
3131 saveq->iocb.ulpContext);
dea3101e
JB
3132 }
3133 }
68876920 3134
dea3101e
JB
3135 return rc;
3136}
3137
e59058c4 3138/**
3621a710 3139 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
3140 * @phba: Pointer to HBA context object.
3141 * @pring: Pointer to driver SLI ring object.
3142 *
3143 * This function is called from the iocb ring event handlers when
3144 * put pointer is ahead of the get pointer for a ring. This function signal
3145 * an error attention condition to the worker thread and the worker
3146 * thread will transition the HBA to offline state.
3147 **/
2e0fef85
JS
3148static void
3149lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 3150{
34b02dcd 3151 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 3152 /*
025dfdaf 3153 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
3154 * rsp ring <portRspMax>
3155 */
3156 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3157 "0312 Ring %d handler: portRspPut %d "
025dfdaf 3158 "is bigger than rsp ring %d\n",
e8b62011 3159 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 3160 pring->sli.sli3.numRiocb);
875fbdfe 3161
2e0fef85 3162 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
3163
3164 /*
3165 * All error attention handlers are posted to
3166 * worker thread
3167 */
3168 phba->work_ha |= HA_ERATT;
3169 phba->work_hs = HS_FFER3;
92d7f7b0 3170
5e9d9b82 3171 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3172
3173 return;
3174}
3175
9399627f 3176/**
3621a710 3177 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3178 * @ptr: Pointer to address of HBA context object.
3179 *
3180 * This function is invoked by the Error Attention polling timer when the
3181 * timer times out. It will check the SLI Error Attention register for
3182 * possible attention events. If so, it will post an Error Attention event
3183 * and wake up worker thread to process it. Otherwise, it will set up the
3184 * Error Attention polling timer for the next poll.
3185 **/
f22eb4d3 3186void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3187{
3188 struct lpfc_hba *phba;
eb016566 3189 uint32_t eratt = 0;
aa6fbb75 3190 uint64_t sli_intr, cnt;
9399627f 3191
f22eb4d3 3192 phba = from_timer(phba, t, eratt_poll);
9399627f 3193
aa6fbb75
JS
3194 /* Here we will also keep track of interrupts per sec of the hba */
3195 sli_intr = phba->sli.slistat.sli_intr;
3196
3197 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3198 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3199 sli_intr);
3200 else
3201 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3202
65791f1f
JS
3203 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3204 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3205 phba->sli.slistat.sli_ips = cnt;
3206
3207 phba->sli.slistat.sli_prev_intr = sli_intr;
3208
9399627f
JS
3209 /* Check chip HA register for error event */
3210 eratt = lpfc_sli_check_eratt(phba);
3211
3212 if (eratt)
3213 /* Tell the worker thread there is work to do */
3214 lpfc_worker_wake_up(phba);
3215 else
3216 /* Restart the timer for next eratt poll */
256ec0d0
JS
3217 mod_timer(&phba->eratt_poll,
3218 jiffies +
65791f1f 3219 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3220 return;
3221}
3222
875fbdfe 3223
e59058c4 3224/**
3621a710 3225 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3226 * @phba: Pointer to HBA context object.
3227 * @pring: Pointer to driver SLI ring object.
3228 * @mask: Host attention register mask for this ring.
3229 *
3230 * This function is called from the interrupt context when there is a ring
3231 * event for the fcp ring. The caller does not hold any lock.
3232 * The function processes each response iocb in the response ring until it
25985edc 3233 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3234 * LE bit set. The function will call the completion handler of the command iocb
3235 * if the response iocb indicates a completion for a command iocb or it is
3236 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3237 * function if this is an unsolicited iocb.
dea3101e 3238 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3239 * to check it explicitly.
3240 */
3241int
2e0fef85
JS
3242lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3243 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3244{
34b02dcd 3245 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3246 IOCB_t *irsp = NULL;
87f6eaff 3247 IOCB_t *entry = NULL;
dea3101e
JB
3248 struct lpfc_iocbq *cmdiocbq = NULL;
3249 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3250 uint32_t status;
3251 uint32_t portRspPut, portRspMax;
3252 int rc = 1;
3253 lpfc_iocb_type type;
3254 unsigned long iflag;
3255 uint32_t rsp_cmpl = 0;
dea3101e 3256
2e0fef85 3257 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3258 pring->stats.iocb_event++;
3259
dea3101e
JB
3260 /*
3261 * The next available response entry should never exceed the maximum
3262 * entries. If it does, treat it as an adapter hardware error.
3263 */
7e56aa25 3264 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3265 portRspPut = le32_to_cpu(pgp->rspPutInx);
3266 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3267 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3268 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3269 return 1;
3270 }
45ed1190
JS
3271 if (phba->fcp_ring_in_use) {
3272 spin_unlock_irqrestore(&phba->hbalock, iflag);
3273 return 1;
3274 } else
3275 phba->fcp_ring_in_use = 1;
dea3101e
JB
3276
3277 rmb();
7e56aa25 3278 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3279 /*
3280 * Fetch an entry off the ring and copy it into a local data
3281 * structure. The copy involves a byte-swap since the
3282 * network byte order and pci byte orders are different.
3283 */
ed957684 3284 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3285 phba->last_completion_time = jiffies;
875fbdfe 3286
7e56aa25
JS
3287 if (++pring->sli.sli3.rspidx >= portRspMax)
3288 pring->sli.sli3.rspidx = 0;
875fbdfe 3289
87f6eaff
JSEC
3290 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3291 (uint32_t *) &rspiocbq.iocb,
ed957684 3292 phba->iocb_rsp_size);
a4bc3379 3293 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3294 irsp = &rspiocbq.iocb;
3295
dea3101e
JB
3296 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3297 pring->stats.iocb_rsp++;
3298 rsp_cmpl++;
3299
3300 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3301 /*
3302 * If resource errors reported from HBA, reduce
3303 * queuedepths of the SCSI device.
3304 */
3305 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3306 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3307 IOERR_NO_RESOURCES)) {
92d7f7b0 3308 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3309 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3310 spin_lock_irqsave(&phba->hbalock, iflag);
3311 }
3312
dea3101e
JB
3313 /* Rsp ring <ringno> error: IOCB */
3314 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3315 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3316 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3317 pring->ringno,
92d7f7b0
JS
3318 irsp->un.ulpWord[0],
3319 irsp->un.ulpWord[1],
3320 irsp->un.ulpWord[2],
3321 irsp->un.ulpWord[3],
3322 irsp->un.ulpWord[4],
3323 irsp->un.ulpWord[5],
d7c255b2
JS
3324 *(uint32_t *)&irsp->un1,
3325 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3326 }
3327
3328 switch (type) {
3329 case LPFC_ABORT_IOCB:
3330 case LPFC_SOL_IOCB:
3331 /*
3332 * Idle exchange closed via ABTS from port. No iocb
3333 * resources need to be recovered.
3334 */
3335 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3336 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3337 "0333 IOCB cmd 0x%x"
dca9479b 3338 " processed. Skipping"
92d7f7b0 3339 " completion\n",
dca9479b 3340 irsp->ulpCommand);
dea3101e
JB
3341 break;
3342 }
3343
604a3e30
JB
3344 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3345 &rspiocbq);
0f65ff68
JS
3346 if (unlikely(!cmdiocbq))
3347 break;
3348 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3349 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3350 if (cmdiocbq->iocb_cmpl) {
3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
3352 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3353 &rspiocbq);
3354 spin_lock_irqsave(&phba->hbalock, iflag);
3355 }
dea3101e 3356 break;
a4bc3379 3357 case LPFC_UNSOL_IOCB:
2e0fef85 3358 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3359 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3360 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3361 break;
dea3101e
JB
3362 default:
3363 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3364 char adaptermsg[LPFC_MAX_ADPTMSG];
3365 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3366 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3367 MAX_MSG_DATA);
898eb71c
JP
3368 dev_warn(&((phba->pcidev)->dev),
3369 "lpfc%d: %s\n",
dea3101e
JB
3370 phba->brd_no, adaptermsg);
3371 } else {
3372 /* Unknown IOCB command */
3373 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3374 "0334 Unknown IOCB command "
92d7f7b0 3375 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3376 type, irsp->ulpCommand,
92d7f7b0
JS
3377 irsp->ulpStatus,
3378 irsp->ulpIoTag,
3379 irsp->ulpContext);
dea3101e
JB
3380 }
3381 break;
3382 }
3383
3384 /*
3385 * The response IOCB has been processed. Update the ring
3386 * pointer in SLIM. If the port response put pointer has not
3387 * been updated, sync the pgp->rspPutInx and fetch the new port
3388 * response put pointer.
3389 */
7e56aa25
JS
3390 writel(pring->sli.sli3.rspidx,
3391 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3392
7e56aa25 3393 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3394 portRspPut = le32_to_cpu(pgp->rspPutInx);
3395 }
3396
3397 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3398 pring->stats.iocb_rsp_full++;
3399 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3400 writel(status, phba->CAregaddr);
3401 readl(phba->CAregaddr);
3402 }
3403 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3404 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3405 pring->stats.iocb_cmd_empty++;
3406
3407 /* Force update of the local copy of cmdGetInx */
7e56aa25 3408 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3409 lpfc_sli_resume_iocb(phba, pring);
3410
3411 if ((pring->lpfc_sli_cmd_available))
3412 (pring->lpfc_sli_cmd_available) (phba, pring);
3413
3414 }
3415
45ed1190 3416 phba->fcp_ring_in_use = 0;
2e0fef85 3417 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3418 return rc;
3419}
3420
e59058c4 3421/**
3772a991
JS
3422 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3423 * @phba: Pointer to HBA context object.
3424 * @pring: Pointer to driver SLI ring object.
3425 * @rspiocbp: Pointer to driver response IOCB object.
3426 *
3427 * This function is called from the worker thread when there is a slow-path
3428 * response IOCB to process. This function chains all the response iocbs until
3429 * seeing the iocb with the LE bit set. The function will call
3430 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3431 * completion of a command iocb. The function will call the
3432 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3433 * The function frees the resources or calls the completion handler if this
3434 * iocb is an abort completion. The function returns NULL when the response
3435 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3436 * this function shall chain the iocb on to the iocb_continueq and return the
3437 * response iocb passed in.
3438 **/
3439static struct lpfc_iocbq *
3440lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3441 struct lpfc_iocbq *rspiocbp)
3442{
3443 struct lpfc_iocbq *saveq;
3444 struct lpfc_iocbq *cmdiocbp;
3445 struct lpfc_iocbq *next_iocb;
3446 IOCB_t *irsp = NULL;
3447 uint32_t free_saveq;
3448 uint8_t iocb_cmd_type;
3449 lpfc_iocb_type type;
3450 unsigned long iflag;
3451 int rc;
3452
3453 spin_lock_irqsave(&phba->hbalock, iflag);
3454 /* First add the response iocb to the countinueq list */
3455 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3456 pring->iocb_continueq_cnt++;
3457
70f23fd6 3458 /* Now, determine whether the list is completed for processing */
3772a991
JS
3459 irsp = &rspiocbp->iocb;
3460 if (irsp->ulpLe) {
3461 /*
3462 * By default, the driver expects to free all resources
3463 * associated with this iocb completion.
3464 */
3465 free_saveq = 1;
3466 saveq = list_get_first(&pring->iocb_continueq,
3467 struct lpfc_iocbq, list);
3468 irsp = &(saveq->iocb);
3469 list_del_init(&pring->iocb_continueq);
3470 pring->iocb_continueq_cnt = 0;
3471
3472 pring->stats.iocb_rsp++;
3473
3474 /*
3475 * If resource errors reported from HBA, reduce
3476 * queuedepths of the SCSI device.
3477 */
3478 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3479 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3480 IOERR_NO_RESOURCES)) {
3772a991
JS
3481 spin_unlock_irqrestore(&phba->hbalock, iflag);
3482 phba->lpfc_rampdown_queue_depth(phba);
3483 spin_lock_irqsave(&phba->hbalock, iflag);
3484 }
3485
3486 if (irsp->ulpStatus) {
3487 /* Rsp ring <ringno> error: IOCB */
3488 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3489 "0328 Rsp Ring %d error: "
3490 "IOCB Data: "
3491 "x%x x%x x%x x%x "
3492 "x%x x%x x%x x%x "
3493 "x%x x%x x%x x%x "
3494 "x%x x%x x%x x%x\n",
3495 pring->ringno,
3496 irsp->un.ulpWord[0],
3497 irsp->un.ulpWord[1],
3498 irsp->un.ulpWord[2],
3499 irsp->un.ulpWord[3],
3500 irsp->un.ulpWord[4],
3501 irsp->un.ulpWord[5],
3502 *(((uint32_t *) irsp) + 6),
3503 *(((uint32_t *) irsp) + 7),
3504 *(((uint32_t *) irsp) + 8),
3505 *(((uint32_t *) irsp) + 9),
3506 *(((uint32_t *) irsp) + 10),
3507 *(((uint32_t *) irsp) + 11),
3508 *(((uint32_t *) irsp) + 12),
3509 *(((uint32_t *) irsp) + 13),
3510 *(((uint32_t *) irsp) + 14),
3511 *(((uint32_t *) irsp) + 15));
3512 }
3513
3514 /*
3515 * Fetch the IOCB command type and call the correct completion
3516 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3517 * get freed back to the lpfc_iocb_list by the discovery
3518 * kernel thread.
3519 */
3520 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3521 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3522 switch (type) {
3523 case LPFC_SOL_IOCB:
3524 spin_unlock_irqrestore(&phba->hbalock, iflag);
3525 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3526 spin_lock_irqsave(&phba->hbalock, iflag);
3527 break;
3528
3529 case LPFC_UNSOL_IOCB:
3530 spin_unlock_irqrestore(&phba->hbalock, iflag);
3531 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3532 spin_lock_irqsave(&phba->hbalock, iflag);
3533 if (!rc)
3534 free_saveq = 0;
3535 break;
3536
3537 case LPFC_ABORT_IOCB:
3538 cmdiocbp = NULL;
3539 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3540 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3541 saveq);
3542 if (cmdiocbp) {
3543 /* Call the specified completion routine */
3544 if (cmdiocbp->iocb_cmpl) {
3545 spin_unlock_irqrestore(&phba->hbalock,
3546 iflag);
3547 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3548 saveq);
3549 spin_lock_irqsave(&phba->hbalock,
3550 iflag);
3551 } else
3552 __lpfc_sli_release_iocbq(phba,
3553 cmdiocbp);
3554 }
3555 break;
3556
3557 case LPFC_UNKNOWN_IOCB:
3558 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3559 char adaptermsg[LPFC_MAX_ADPTMSG];
3560 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3561 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3562 MAX_MSG_DATA);
3563 dev_warn(&((phba->pcidev)->dev),
3564 "lpfc%d: %s\n",
3565 phba->brd_no, adaptermsg);
3566 } else {
3567 /* Unknown IOCB command */
3568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3569 "0335 Unknown IOCB "
3570 "command Data: x%x "
3571 "x%x x%x x%x\n",
3572 irsp->ulpCommand,
3573 irsp->ulpStatus,
3574 irsp->ulpIoTag,
3575 irsp->ulpContext);
3576 }
3577 break;
3578 }
3579
3580 if (free_saveq) {
3581 list_for_each_entry_safe(rspiocbp, next_iocb,
3582 &saveq->list, list) {
61f35bff 3583 list_del_init(&rspiocbp->list);
3772a991
JS
3584 __lpfc_sli_release_iocbq(phba, rspiocbp);
3585 }
3586 __lpfc_sli_release_iocbq(phba, saveq);
3587 }
3588 rspiocbp = NULL;
3589 }
3590 spin_unlock_irqrestore(&phba->hbalock, iflag);
3591 return rspiocbp;
3592}
3593
3594/**
3595 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3596 * @phba: Pointer to HBA context object.
3597 * @pring: Pointer to driver SLI ring object.
3598 * @mask: Host attention register mask for this ring.
3599 *
3772a991
JS
3600 * This routine wraps the actual slow_ring event process routine from the
3601 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3602 **/
3772a991 3603void
2e0fef85
JS
3604lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3605 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3606{
3607 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3608}
3609
3610/**
3611 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3612 * @phba: Pointer to HBA context object.
3613 * @pring: Pointer to driver SLI ring object.
3614 * @mask: Host attention register mask for this ring.
3615 *
3616 * This function is called from the worker thread when there is a ring event
3617 * for non-fcp rings. The caller does not hold any lock. The function will
3618 * remove each response iocb in the response ring and calls the handle
3619 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3620 **/
3621static void
3622lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3623 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3624{
34b02dcd 3625 struct lpfc_pgp *pgp;
dea3101e
JB
3626 IOCB_t *entry;
3627 IOCB_t *irsp = NULL;
3628 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3629 uint32_t portRspPut, portRspMax;
dea3101e 3630 unsigned long iflag;
3772a991 3631 uint32_t status;
dea3101e 3632
34b02dcd 3633 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3634 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3635 pring->stats.iocb_event++;
3636
dea3101e
JB
3637 /*
3638 * The next available response entry should never exceed the maximum
3639 * entries. If it does, treat it as an adapter hardware error.
3640 */
7e56aa25 3641 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3642 portRspPut = le32_to_cpu(pgp->rspPutInx);
3643 if (portRspPut >= portRspMax) {
3644 /*
025dfdaf 3645 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3646 * rsp ring <portRspMax>
3647 */
ed957684 3648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3649 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3650 "is bigger than rsp ring %d\n",
e8b62011 3651 pring->ringno, portRspPut, portRspMax);
dea3101e 3652
2e0fef85
JS
3653 phba->link_state = LPFC_HBA_ERROR;
3654 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3655
3656 phba->work_hs = HS_FFER3;
3657 lpfc_handle_eratt(phba);
3658
3772a991 3659 return;
dea3101e
JB
3660 }
3661
3662 rmb();
7e56aa25 3663 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3664 /*
3665 * Build a completion list and call the appropriate handler.
3666 * The process is to get the next available response iocb, get
3667 * a free iocb from the list, copy the response data into the
3668 * free iocb, insert to the continuation list, and update the
3669 * next response index to slim. This process makes response
3670 * iocb's in the ring available to DMA as fast as possible but
3671 * pays a penalty for a copy operation. Since the iocb is
3672 * only 32 bytes, this penalty is considered small relative to
3673 * the PCI reads for register values and a slim write. When
3674 * the ulpLe field is set, the entire Command has been
3675 * received.
3676 */
ed957684
JS
3677 entry = lpfc_resp_iocb(phba, pring);
3678
858c9f6c 3679 phba->last_completion_time = jiffies;
2e0fef85 3680 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3681 if (rspiocbp == NULL) {
3682 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3683 "completion.\n", __func__);
dea3101e
JB
3684 break;
3685 }
3686
ed957684
JS
3687 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3688 phba->iocb_rsp_size);
dea3101e
JB
3689 irsp = &rspiocbp->iocb;
3690
7e56aa25
JS
3691 if (++pring->sli.sli3.rspidx >= portRspMax)
3692 pring->sli.sli3.rspidx = 0;
dea3101e 3693
a58cbd52
JS
3694 if (pring->ringno == LPFC_ELS_RING) {
3695 lpfc_debugfs_slow_ring_trc(phba,
3696 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3697 *(((uint32_t *) irsp) + 4),
3698 *(((uint32_t *) irsp) + 6),
3699 *(((uint32_t *) irsp) + 7));
3700 }
3701
7e56aa25
JS
3702 writel(pring->sli.sli3.rspidx,
3703 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3704
3772a991
JS
3705 spin_unlock_irqrestore(&phba->hbalock, iflag);
3706 /* Handle the response IOCB */
3707 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3708 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3709
3710 /*
3711 * If the port response put pointer has not been updated, sync
3712 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3713 * response put pointer.
3714 */
7e56aa25 3715 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3716 portRspPut = le32_to_cpu(pgp->rspPutInx);
3717 }
7e56aa25 3718 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3719
92d7f7b0 3720 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3721 /* At least one response entry has been freed */
3722 pring->stats.iocb_rsp_full++;
3723 /* SET RxRE_RSP in Chip Att register */
3724 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3725 writel(status, phba->CAregaddr);
3726 readl(phba->CAregaddr); /* flush */
3727 }
3728 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3729 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3730 pring->stats.iocb_cmd_empty++;
3731
3732 /* Force update of the local copy of cmdGetInx */
7e56aa25 3733 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3734 lpfc_sli_resume_iocb(phba, pring);
3735
3736 if ((pring->lpfc_sli_cmd_available))
3737 (pring->lpfc_sli_cmd_available) (phba, pring);
3738
3739 }
3740
2e0fef85 3741 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3742 return;
dea3101e
JB
3743}
3744
4f774513
JS
3745/**
3746 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3747 * @phba: Pointer to HBA context object.
3748 * @pring: Pointer to driver SLI ring object.
3749 * @mask: Host attention register mask for this ring.
3750 *
3751 * This function is called from the worker thread when there is a pending
3752 * ELS response iocb on the driver internal slow-path response iocb worker
3753 * queue. The caller does not hold any lock. The function will remove each
3754 * response iocb from the response worker queue and calls the handle
3755 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3756 **/
3757static void
3758lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3759 struct lpfc_sli_ring *pring, uint32_t mask)
3760{
3761 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3762 struct hbq_dmabuf *dmabuf;
3763 struct lpfc_cq_event *cq_event;
4f774513
JS
3764 unsigned long iflag;
3765
45ed1190
JS
3766 spin_lock_irqsave(&phba->hbalock, iflag);
3767 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3768 spin_unlock_irqrestore(&phba->hbalock, iflag);
3769 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3770 /* Get the response iocb from the head of work queue */
3771 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3772 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3773 cq_event, struct lpfc_cq_event, list);
4f774513 3774 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3775
3776 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3777 case CQE_CODE_COMPL_WQE:
3778 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3779 cq_event);
45ed1190
JS
3780 /* Translate ELS WCQE to response IOCBQ */
3781 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3782 irspiocbq);
3783 if (irspiocbq)
3784 lpfc_sli_sp_handle_rspiocb(phba, pring,
3785 irspiocbq);
4d9ab994
JS
3786 break;
3787 case CQE_CODE_RECEIVE:
7851fe2c 3788 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3789 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3790 cq_event);
3791 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3792 break;
3793 default:
3794 break;
3795 }
4f774513
JS
3796 }
3797}
3798
e59058c4 3799/**
3621a710 3800 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3801 * @phba: Pointer to HBA context object.
3802 * @pring: Pointer to driver SLI ring object.
3803 *
3804 * This function aborts all iocbs in the given ring and frees all the iocb
3805 * objects in txq. This function issues an abort iocb for all the iocb commands
3806 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3807 * the return of this function. The caller is not required to hold any locks.
3808 **/
2e0fef85 3809void
dea3101e
JB
3810lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3811{
2534ba75 3812 LIST_HEAD(completions);
dea3101e 3813 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3814
92d7f7b0
JS
3815 if (pring->ringno == LPFC_ELS_RING) {
3816 lpfc_fabric_abort_hba(phba);
3817 }
3818
dea3101e
JB
3819 /* Error everything on txq and txcmplq
3820 * First do the txq.
3821 */
db55fba8
JS
3822 if (phba->sli_rev >= LPFC_SLI_REV4) {
3823 spin_lock_irq(&pring->ring_lock);
3824 list_splice_init(&pring->txq, &completions);
3825 pring->txq_cnt = 0;
3826 spin_unlock_irq(&pring->ring_lock);
dea3101e 3827
db55fba8
JS
3828 spin_lock_irq(&phba->hbalock);
3829 /* Next issue ABTS for everything on the txcmplq */
3830 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3831 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3832 spin_unlock_irq(&phba->hbalock);
3833 } else {
3834 spin_lock_irq(&phba->hbalock);
3835 list_splice_init(&pring->txq, &completions);
3836 pring->txq_cnt = 0;
dea3101e 3837
db55fba8
JS
3838 /* Next issue ABTS for everything on the txcmplq */
3839 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3840 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3841 spin_unlock_irq(&phba->hbalock);
3842 }
dea3101e 3843
a257bf90
JS
3844 /* Cancel all the IOCBs from the completions list */
3845 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3846 IOERR_SLI_ABORTED);
dea3101e
JB
3847}
3848
895427bd
JS
3849/**
3850 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3851 * @phba: Pointer to HBA context object.
3852 * @pring: Pointer to driver SLI ring object.
3853 *
3854 * This function aborts all iocbs in the given ring and frees all the iocb
3855 * objects in txq. This function issues an abort iocb for all the iocb commands
3856 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3857 * the return of this function. The caller is not required to hold any locks.
3858 **/
3859void
3860lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3861{
3862 LIST_HEAD(completions);
3863 struct lpfc_iocbq *iocb, *next_iocb;
3864
3865 if (pring->ringno == LPFC_ELS_RING)
3866 lpfc_fabric_abort_hba(phba);
3867
3868 spin_lock_irq(&phba->hbalock);
3869 /* Next issue ABTS for everything on the txcmplq */
3870 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3871 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3872 spin_unlock_irq(&phba->hbalock);
3873}
3874
3875
db55fba8
JS
3876/**
3877 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3878 * @phba: Pointer to HBA context object.
3879 * @pring: Pointer to driver SLI ring object.
3880 *
3881 * This function aborts all iocbs in FCP rings and frees all the iocb
3882 * objects in txq. This function issues an abort iocb for all the iocb commands
3883 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3884 * the return of this function. The caller is not required to hold any locks.
3885 **/
3886void
3887lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3888{
3889 struct lpfc_sli *psli = &phba->sli;
3890 struct lpfc_sli_ring *pring;
3891 uint32_t i;
3892
3893 /* Look on all the FCP Rings for the iotag */
3894 if (phba->sli_rev >= LPFC_SLI_REV4) {
3895 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3896 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3897 lpfc_sli_abort_iocb_ring(phba, pring);
3898 }
3899 } else {
895427bd 3900 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3901 lpfc_sli_abort_iocb_ring(phba, pring);
3902 }
3903}
3904
895427bd
JS
3905/**
3906 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3907 * @phba: Pointer to HBA context object.
3908 *
3909 * This function aborts all wqes in NVME rings. This function issues an
3910 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3911 * the txcmplq is not guaranteed to complete before the return of this
3912 * function. The caller is not required to hold any locks.
3913 **/
3914void
3915lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3916{
3917 struct lpfc_sli_ring *pring;
3918 uint32_t i;
3919
3920 if (phba->sli_rev < LPFC_SLI_REV4)
3921 return;
3922
3923 /* Abort all IO on each NVME ring. */
3924 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3925 pring = phba->sli4_hba.nvme_wq[i]->pring;
3926 lpfc_sli_abort_wqe_ring(phba, pring);
3927 }
3928}
3929
db55fba8 3930
a8e497d5 3931/**
3621a710 3932 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3933 * @phba: Pointer to HBA context object.
3934 *
3935 * This function flushes all iocbs in the fcp ring and frees all the iocb
3936 * objects in txq and txcmplq. This function will not issue abort iocbs
3937 * for all the iocb commands in txcmplq, they will just be returned with
3938 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3939 * slot has been permanently disabled.
3940 **/
3941void
3942lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3943{
3944 LIST_HEAD(txq);
3945 LIST_HEAD(txcmplq);
a8e497d5
JS
3946 struct lpfc_sli *psli = &phba->sli;
3947 struct lpfc_sli_ring *pring;
db55fba8 3948 uint32_t i;
c1dd9111 3949 struct lpfc_iocbq *piocb, *next_iocb;
a8e497d5
JS
3950
3951 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3952 /* Indicate the I/O queues are flushed */
3953 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3954 spin_unlock_irq(&phba->hbalock);
3955
db55fba8
JS
3956 /* Look on all the FCP Rings for the iotag */
3957 if (phba->sli_rev >= LPFC_SLI_REV4) {
3958 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3959 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3960
3961 spin_lock_irq(&pring->ring_lock);
3962 /* Retrieve everything on txq */
3963 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
3964 list_for_each_entry_safe(piocb, next_iocb,
3965 &pring->txcmplq, list)
3966 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
3967 /* Retrieve everything on the txcmplq */
3968 list_splice_init(&pring->txcmplq, &txcmplq);
3969 pring->txq_cnt = 0;
3970 pring->txcmplq_cnt = 0;
3971 spin_unlock_irq(&pring->ring_lock);
3972
3973 /* Flush the txq */
3974 lpfc_sli_cancel_iocbs(phba, &txq,
3975 IOSTAT_LOCAL_REJECT,
3976 IOERR_SLI_DOWN);
3977 /* Flush the txcmpq */
3978 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3979 IOSTAT_LOCAL_REJECT,
3980 IOERR_SLI_DOWN);
3981 }
3982 } else {
895427bd 3983 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 3984
db55fba8
JS
3985 spin_lock_irq(&phba->hbalock);
3986 /* Retrieve everything on txq */
3987 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
3988 list_for_each_entry_safe(piocb, next_iocb,
3989 &pring->txcmplq, list)
3990 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
3991 /* Retrieve everything on the txcmplq */
3992 list_splice_init(&pring->txcmplq, &txcmplq);
3993 pring->txq_cnt = 0;
3994 pring->txcmplq_cnt = 0;
3995 spin_unlock_irq(&phba->hbalock);
3996
3997 /* Flush the txq */
3998 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3999 IOERR_SLI_DOWN);
4000 /* Flush the txcmpq */
4001 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4002 IOERR_SLI_DOWN);
4003 }
a8e497d5
JS
4004}
4005
895427bd
JS
4006/**
4007 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4008 * @phba: Pointer to HBA context object.
4009 *
4010 * This function flushes all wqes in the nvme rings and frees all resources
4011 * in the txcmplq. This function does not issue abort wqes for the IO
4012 * commands in txcmplq, they will just be returned with
4013 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4014 * slot has been permanently disabled.
4015 **/
4016void
4017lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4018{
4019 LIST_HEAD(txcmplq);
4020 struct lpfc_sli_ring *pring;
4021 uint32_t i;
c1dd9111 4022 struct lpfc_iocbq *piocb, *next_iocb;
895427bd
JS
4023
4024 if (phba->sli_rev < LPFC_SLI_REV4)
4025 return;
4026
4027 /* Hint to other driver operations that a flush is in progress. */
4028 spin_lock_irq(&phba->hbalock);
4029 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4030 spin_unlock_irq(&phba->hbalock);
4031
4032 /* Cycle through all NVME rings and complete each IO with
4033 * a local driver reason code. This is a flush so no
4034 * abort exchange to FW.
4035 */
4036 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4037 pring = phba->sli4_hba.nvme_wq[i]->pring;
4038
895427bd 4039 spin_lock_irq(&pring->ring_lock);
c1dd9111
JS
4040 list_for_each_entry_safe(piocb, next_iocb,
4041 &pring->txcmplq, list)
4042 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4043 /* Retrieve everything on the txcmplq */
895427bd
JS
4044 list_splice_init(&pring->txcmplq, &txcmplq);
4045 pring->txcmplq_cnt = 0;
4046 spin_unlock_irq(&pring->ring_lock);
4047
4048 /* Flush the txcmpq &&&PAE */
4049 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4050 IOSTAT_LOCAL_REJECT,
4051 IOERR_SLI_DOWN);
4052 }
4053}
4054
e59058c4 4055/**
3772a991 4056 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
4057 * @phba: Pointer to HBA context object.
4058 * @mask: Bit mask to be checked.
4059 *
4060 * This function reads the host status register and compares
4061 * with the provided bit mask to check if HBA completed
4062 * the restart. This function will wait in a loop for the
4063 * HBA to complete restart. If the HBA does not restart within
4064 * 15 iterations, the function will reset the HBA again. The
4065 * function returns 1 when HBA fail to restart otherwise returns
4066 * zero.
4067 **/
3772a991
JS
4068static int
4069lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 4070{
41415862
JW
4071 uint32_t status;
4072 int i = 0;
4073 int retval = 0;
dea3101e 4074
41415862 4075 /* Read the HBA Host Status Register */
9940b97b
JS
4076 if (lpfc_readl(phba->HSregaddr, &status))
4077 return 1;
dea3101e 4078
41415862
JW
4079 /*
4080 * Check status register every 100ms for 5 retries, then every
4081 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4082 * every 2.5 sec for 4.
4083 * Break our of the loop if errors occurred during init.
4084 */
4085 while (((status & mask) != mask) &&
4086 !(status & HS_FFERM) &&
4087 i++ < 20) {
dea3101e 4088
41415862
JW
4089 if (i <= 5)
4090 msleep(10);
4091 else if (i <= 10)
4092 msleep(500);
4093 else
4094 msleep(2500);
dea3101e 4095
41415862 4096 if (i == 15) {
2e0fef85 4097 /* Do post */
92d7f7b0 4098 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
4099 lpfc_sli_brdrestart(phba);
4100 }
4101 /* Read the HBA Host Status Register */
9940b97b
JS
4102 if (lpfc_readl(phba->HSregaddr, &status)) {
4103 retval = 1;
4104 break;
4105 }
41415862 4106 }
dea3101e 4107
41415862
JW
4108 /* Check to see if any errors occurred during init */
4109 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
4110 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4111 "2751 Adapter failed to restart, "
4112 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4113 status,
4114 readl(phba->MBslimaddr + 0xa8),
4115 readl(phba->MBslimaddr + 0xac));
2e0fef85 4116 phba->link_state = LPFC_HBA_ERROR;
41415862 4117 retval = 1;
dea3101e 4118 }
dea3101e 4119
41415862
JW
4120 return retval;
4121}
dea3101e 4122
da0436e9
JS
4123/**
4124 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4125 * @phba: Pointer to HBA context object.
4126 * @mask: Bit mask to be checked.
4127 *
4128 * This function checks the host status register to check if HBA is
4129 * ready. This function will wait in a loop for the HBA to be ready
4130 * If the HBA is not ready , the function will will reset the HBA PCI
4131 * function again. The function returns 1 when HBA fail to be ready
4132 * otherwise returns zero.
4133 **/
4134static int
4135lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4136{
4137 uint32_t status;
4138 int retval = 0;
4139
4140 /* Read the HBA Host Status Register */
4141 status = lpfc_sli4_post_status_check(phba);
4142
4143 if (status) {
4144 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4145 lpfc_sli_brdrestart(phba);
4146 status = lpfc_sli4_post_status_check(phba);
4147 }
4148
4149 /* Check to see if any errors occurred during init */
4150 if (status) {
4151 phba->link_state = LPFC_HBA_ERROR;
4152 retval = 1;
4153 } else
4154 phba->sli4_hba.intr_enable = 0;
4155
4156 return retval;
4157}
4158
4159/**
4160 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4161 * @phba: Pointer to HBA context object.
4162 * @mask: Bit mask to be checked.
4163 *
4164 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4165 * from the API jump table function pointer from the lpfc_hba struct.
4166 **/
4167int
4168lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4169{
4170 return phba->lpfc_sli_brdready(phba, mask);
4171}
4172
9290831f
JS
4173#define BARRIER_TEST_PATTERN (0xdeadbeef)
4174
e59058c4 4175/**
3621a710 4176 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
4177 * @phba: Pointer to HBA context object.
4178 *
1b51197d
JS
4179 * This function is called before resetting an HBA. This function is called
4180 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 4181 **/
2e0fef85 4182void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4183{
65a29c16
JS
4184 uint32_t __iomem *resp_buf;
4185 uint32_t __iomem *mbox_buf;
9290831f 4186 volatile uint32_t mbox;
9940b97b 4187 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4188 int i;
4189 uint8_t hdrtype;
4190
1c2ba475
JT
4191 lockdep_assert_held(&phba->hbalock);
4192
9290831f
JS
4193 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4194 if (hdrtype != 0x80 ||
4195 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4196 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4197 return;
4198
4199 /*
4200 * Tell the other part of the chip to suspend temporarily all
4201 * its DMA activity.
4202 */
65a29c16 4203 resp_buf = phba->MBslimaddr;
9290831f
JS
4204
4205 /* Disable the error attention */
9940b97b
JS
4206 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4207 return;
9290831f
JS
4208 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4209 readl(phba->HCregaddr); /* flush */
2e0fef85 4210 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4211
9940b97b
JS
4212 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4213 return;
4214 if (ha_copy & HA_ERATT) {
9290831f
JS
4215 /* Clear Chip error bit */
4216 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4217 phba->pport->stopped = 1;
9290831f
JS
4218 }
4219
4220 mbox = 0;
4221 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4222 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4223
4224 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4225 mbox_buf = phba->MBslimaddr;
9290831f
JS
4226 writel(mbox, mbox_buf);
4227
9940b97b
JS
4228 for (i = 0; i < 50; i++) {
4229 if (lpfc_readl((resp_buf + 1), &resp_data))
4230 return;
4231 if (resp_data != ~(BARRIER_TEST_PATTERN))
4232 mdelay(1);
4233 else
4234 break;
4235 }
4236 resp_data = 0;
4237 if (lpfc_readl((resp_buf + 1), &resp_data))
4238 return;
4239 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4240 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4241 phba->pport->stopped)
9290831f
JS
4242 goto restore_hc;
4243 else
4244 goto clear_errat;
4245 }
4246
4247 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4248 resp_data = 0;
4249 for (i = 0; i < 500; i++) {
4250 if (lpfc_readl(resp_buf, &resp_data))
4251 return;
4252 if (resp_data != mbox)
4253 mdelay(1);
4254 else
4255 break;
4256 }
9290831f
JS
4257
4258clear_errat:
4259
9940b97b
JS
4260 while (++i < 500) {
4261 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4262 return;
4263 if (!(ha_copy & HA_ERATT))
4264 mdelay(1);
4265 else
4266 break;
4267 }
9290831f
JS
4268
4269 if (readl(phba->HAregaddr) & HA_ERATT) {
4270 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4271 phba->pport->stopped = 1;
9290831f
JS
4272 }
4273
4274restore_hc:
2e0fef85 4275 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4276 writel(hc_copy, phba->HCregaddr);
4277 readl(phba->HCregaddr); /* flush */
4278}
4279
e59058c4 4280/**
3621a710 4281 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4282 * @phba: Pointer to HBA context object.
4283 *
4284 * This function issues a kill_board mailbox command and waits for
4285 * the error attention interrupt. This function is called for stopping
4286 * the firmware processing. The caller is not required to hold any
4287 * locks. This function calls lpfc_hba_down_post function to free
4288 * any pending commands after the kill. The function will return 1 when it
4289 * fails to kill the board else will return 0.
4290 **/
41415862 4291int
2e0fef85 4292lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4293{
4294 struct lpfc_sli *psli;
4295 LPFC_MBOXQ_t *pmb;
4296 uint32_t status;
4297 uint32_t ha_copy;
4298 int retval;
4299 int i = 0;
dea3101e 4300
41415862 4301 psli = &phba->sli;
dea3101e 4302
41415862 4303 /* Kill HBA */
ed957684 4304 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4305 "0329 Kill HBA Data: x%x x%x\n",
4306 phba->pport->port_state, psli->sli_flag);
41415862 4307
98c9ea5c
JS
4308 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4309 if (!pmb)
41415862 4310 return 1;
41415862
JW
4311
4312 /* Disable the error attention */
2e0fef85 4313 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4314 if (lpfc_readl(phba->HCregaddr, &status)) {
4315 spin_unlock_irq(&phba->hbalock);
4316 mempool_free(pmb, phba->mbox_mem_pool);
4317 return 1;
4318 }
41415862
JW
4319 status &= ~HC_ERINT_ENA;
4320 writel(status, phba->HCregaddr);
4321 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4322 phba->link_flag |= LS_IGNORE_ERATT;
4323 spin_unlock_irq(&phba->hbalock);
41415862
JW
4324
4325 lpfc_kill_board(phba, pmb);
4326 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4327 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4328
4329 if (retval != MBX_SUCCESS) {
4330 if (retval != MBX_BUSY)
4331 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4332 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4333 "2752 KILL_BOARD command failed retval %d\n",
4334 retval);
2e0fef85
JS
4335 spin_lock_irq(&phba->hbalock);
4336 phba->link_flag &= ~LS_IGNORE_ERATT;
4337 spin_unlock_irq(&phba->hbalock);
41415862
JW
4338 return 1;
4339 }
4340
f4b4c68f
JS
4341 spin_lock_irq(&phba->hbalock);
4342 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4343 spin_unlock_irq(&phba->hbalock);
9290831f 4344
41415862
JW
4345 mempool_free(pmb, phba->mbox_mem_pool);
4346
4347 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4348 * attention every 100ms for 3 seconds. If we don't get ERATT after
4349 * 3 seconds we still set HBA_ERROR state because the status of the
4350 * board is now undefined.
4351 */
9940b97b
JS
4352 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4353 return 1;
41415862
JW
4354 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4355 mdelay(100);
9940b97b
JS
4356 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4357 return 1;
41415862
JW
4358 }
4359
4360 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4361 if (ha_copy & HA_ERATT) {
4362 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4363 phba->pport->stopped = 1;
9290831f 4364 }
2e0fef85 4365 spin_lock_irq(&phba->hbalock);
41415862 4366 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4367 psli->mbox_active = NULL;
2e0fef85
JS
4368 phba->link_flag &= ~LS_IGNORE_ERATT;
4369 spin_unlock_irq(&phba->hbalock);
41415862 4370
41415862 4371 lpfc_hba_down_post(phba);
2e0fef85 4372 phba->link_state = LPFC_HBA_ERROR;
41415862 4373
2e0fef85 4374 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4375}
4376
e59058c4 4377/**
3772a991 4378 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4379 * @phba: Pointer to HBA context object.
4380 *
4381 * This function resets the HBA by writing HC_INITFF to the control
4382 * register. After the HBA resets, this function resets all the iocb ring
4383 * indices. This function disables PCI layer parity checking during
4384 * the reset.
4385 * This function returns 0 always.
4386 * The caller is not required to hold any locks.
4387 **/
41415862 4388int
2e0fef85 4389lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4390{
41415862 4391 struct lpfc_sli *psli;
dea3101e 4392 struct lpfc_sli_ring *pring;
41415862 4393 uint16_t cfg_value;
dea3101e 4394 int i;
dea3101e 4395
41415862 4396 psli = &phba->sli;
dea3101e 4397
41415862
JW
4398 /* Reset HBA */
4399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4400 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4401 (phba->pport) ? phba->pport->port_state : 0,
4402 psli->sli_flag);
dea3101e
JB
4403
4404 /* perform board reset */
4405 phba->fc_eventTag = 0;
4d9ab994 4406 phba->link_events = 0;
4492b739
JS
4407 if (phba->pport) {
4408 phba->pport->fc_myDID = 0;
4409 phba->pport->fc_prevDID = 0;
4410 }
dea3101e 4411
41415862
JW
4412 /* Turn off parity checking and serr during the physical reset */
4413 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4414 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4415 (cfg_value &
4416 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4417
3772a991
JS
4418 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4419
41415862
JW
4420 /* Now toggle INITFF bit in the Host Control Register */
4421 writel(HC_INITFF, phba->HCregaddr);
4422 mdelay(1);
4423 readl(phba->HCregaddr); /* flush */
4424 writel(0, phba->HCregaddr);
4425 readl(phba->HCregaddr); /* flush */
4426
4427 /* Restore PCI cmd register */
4428 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4429
4430 /* Initialize relevant SLI info */
41415862 4431 for (i = 0; i < psli->num_rings; i++) {
895427bd 4432 pring = &psli->sli3_ring[i];
dea3101e 4433 pring->flag = 0;
7e56aa25
JS
4434 pring->sli.sli3.rspidx = 0;
4435 pring->sli.sli3.next_cmdidx = 0;
4436 pring->sli.sli3.local_getidx = 0;
4437 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4438 pring->missbufcnt = 0;
4439 }
dea3101e 4440
2e0fef85 4441 phba->link_state = LPFC_WARM_START;
41415862
JW
4442 return 0;
4443}
4444
e59058c4 4445/**
da0436e9
JS
4446 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4447 * @phba: Pointer to HBA context object.
4448 *
4449 * This function resets a SLI4 HBA. This function disables PCI layer parity
4450 * checking during resets the device. The caller is not required to hold
4451 * any locks.
4452 *
4453 * This function returns 0 always.
4454 **/
4455int
4456lpfc_sli4_brdreset(struct lpfc_hba *phba)
4457{
4458 struct lpfc_sli *psli = &phba->sli;
4459 uint16_t cfg_value;
0293635e 4460 int rc = 0;
da0436e9
JS
4461
4462 /* Reset HBA */
4463 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4464 "0295 Reset HBA Data: x%x x%x x%x\n",
4465 phba->pport->port_state, psli->sli_flag,
4466 phba->hba_flag);
da0436e9
JS
4467
4468 /* perform board reset */
4469 phba->fc_eventTag = 0;
4d9ab994 4470 phba->link_events = 0;
da0436e9
JS
4471 phba->pport->fc_myDID = 0;
4472 phba->pport->fc_prevDID = 0;
4473
da0436e9
JS
4474 spin_lock_irq(&phba->hbalock);
4475 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4476 phba->fcf.fcf_flag = 0;
da0436e9
JS
4477 spin_unlock_irq(&phba->hbalock);
4478
0293635e
JS
4479 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4480 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4481 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4482 return rc;
4483 }
4484
da0436e9
JS
4485 /* Now physically reset the device */
4486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4487 "0389 Performing PCI function reset!\n");
be858b65
JS
4488
4489 /* Turn off parity checking and serr during the physical reset */
4490 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4491 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4492 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4493
88318816 4494 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4495 rc = lpfc_pci_function_reset(phba);
da0436e9 4496
be858b65
JS
4497 /* Restore PCI cmd register */
4498 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4499
27b01b82 4500 return rc;
da0436e9
JS
4501}
4502
4503/**
4504 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4505 * @phba: Pointer to HBA context object.
4506 *
4507 * This function is called in the SLI initialization code path to
4508 * restart the HBA. The caller is not required to hold any lock.
4509 * This function writes MBX_RESTART mailbox command to the SLIM and
4510 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4511 * function to free any pending commands. The function enables
4512 * POST only during the first initialization. The function returns zero.
4513 * The function does not guarantee completion of MBX_RESTART mailbox
4514 * command before the return of this function.
4515 **/
da0436e9
JS
4516static int
4517lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4518{
4519 MAILBOX_t *mb;
4520 struct lpfc_sli *psli;
41415862
JW
4521 volatile uint32_t word0;
4522 void __iomem *to_slim;
0d878419 4523 uint32_t hba_aer_enabled;
41415862 4524
2e0fef85 4525 spin_lock_irq(&phba->hbalock);
41415862 4526
0d878419
JS
4527 /* Take PCIe device Advanced Error Reporting (AER) state */
4528 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4529
41415862
JW
4530 psli = &phba->sli;
4531
4532 /* Restart HBA */
4533 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4534 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4535 (phba->pport) ? phba->pport->port_state : 0,
4536 psli->sli_flag);
41415862
JW
4537
4538 word0 = 0;
4539 mb = (MAILBOX_t *) &word0;
4540 mb->mbxCommand = MBX_RESTART;
4541 mb->mbxHc = 1;
4542
9290831f
JS
4543 lpfc_reset_barrier(phba);
4544
41415862
JW
4545 to_slim = phba->MBslimaddr;
4546 writel(*(uint32_t *) mb, to_slim);
4547 readl(to_slim); /* flush */
4548
4549 /* Only skip post after fc_ffinit is completed */
4492b739 4550 if (phba->pport && phba->pport->port_state)
41415862 4551 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4552 else
41415862 4553 word0 = 0; /* This is really setting up word1 */
65a29c16 4554 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4555 writel(*(uint32_t *) mb, to_slim);
4556 readl(to_slim); /* flush */
dea3101e 4557
41415862 4558 lpfc_sli_brdreset(phba);
4492b739
JS
4559 if (phba->pport)
4560 phba->pport->stopped = 0;
2e0fef85 4561 phba->link_state = LPFC_INIT_START;
da0436e9 4562 phba->hba_flag = 0;
2e0fef85 4563 spin_unlock_irq(&phba->hbalock);
41415862 4564
64ba8818
JS
4565 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4566 psli->stats_start = get_seconds();
4567
eaf15d5b
JS
4568 /* Give the INITFF and Post time to settle. */
4569 mdelay(100);
41415862 4570
0d878419
JS
4571 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4572 if (hba_aer_enabled)
4573 pci_disable_pcie_error_reporting(phba->pcidev);
4574
41415862 4575 lpfc_hba_down_post(phba);
dea3101e
JB
4576
4577 return 0;
4578}
4579
da0436e9
JS
4580/**
4581 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4582 * @phba: Pointer to HBA context object.
4583 *
4584 * This function is called in the SLI initialization code path to restart
4585 * a SLI4 HBA. The caller is not required to hold any lock.
4586 * At the end of the function, it calls lpfc_hba_down_post function to
4587 * free any pending commands.
4588 **/
4589static int
4590lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4591{
4592 struct lpfc_sli *psli = &phba->sli;
75baf696 4593 uint32_t hba_aer_enabled;
27b01b82 4594 int rc;
da0436e9
JS
4595
4596 /* Restart HBA */
4597 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4598 "0296 Restart HBA Data: x%x x%x\n",
4599 phba->pport->port_state, psli->sli_flag);
4600
75baf696
JS
4601 /* Take PCIe device Advanced Error Reporting (AER) state */
4602 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4603
27b01b82 4604 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4605
4606 spin_lock_irq(&phba->hbalock);
4607 phba->pport->stopped = 0;
4608 phba->link_state = LPFC_INIT_START;
4609 phba->hba_flag = 0;
4610 spin_unlock_irq(&phba->hbalock);
4611
4612 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4613 psli->stats_start = get_seconds();
4614
75baf696
JS
4615 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4616 if (hba_aer_enabled)
4617 pci_disable_pcie_error_reporting(phba->pcidev);
4618
da0436e9 4619 lpfc_hba_down_post(phba);
569dbe84 4620 lpfc_sli4_queue_destroy(phba);
da0436e9 4621
27b01b82 4622 return rc;
da0436e9
JS
4623}
4624
4625/**
4626 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4627 * @phba: Pointer to HBA context object.
4628 *
4629 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4630 * API jump table function pointer from the lpfc_hba struct.
4631**/
4632int
4633lpfc_sli_brdrestart(struct lpfc_hba *phba)
4634{
4635 return phba->lpfc_sli_brdrestart(phba);
4636}
4637
e59058c4 4638/**
3621a710 4639 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4640 * @phba: Pointer to HBA context object.
4641 *
4642 * This function is called after a HBA restart to wait for successful
4643 * restart of the HBA. Successful restart of the HBA is indicated by
4644 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4645 * iteration, the function will restart the HBA again. The function returns
4646 * zero if HBA successfully restarted else returns negative error code.
4647 **/
4492b739 4648int
dea3101e
JB
4649lpfc_sli_chipset_init(struct lpfc_hba *phba)
4650{
4651 uint32_t status, i = 0;
4652
4653 /* Read the HBA Host Status Register */
9940b97b
JS
4654 if (lpfc_readl(phba->HSregaddr, &status))
4655 return -EIO;
dea3101e
JB
4656
4657 /* Check status register to see what current state is */
4658 i = 0;
4659 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4660
dcf2a4e0
JS
4661 /* Check every 10ms for 10 retries, then every 100ms for 90
4662 * retries, then every 1 sec for 50 retires for a total of
4663 * ~60 seconds before reset the board again and check every
4664 * 1 sec for 50 retries. The up to 60 seconds before the
4665 * board ready is required by the Falcon FIPS zeroization
4666 * complete, and any reset the board in between shall cause
4667 * restart of zeroization, further delay the board ready.
dea3101e 4668 */
dcf2a4e0 4669 if (i++ >= 200) {
dea3101e
JB
4670 /* Adapter failed to init, timeout, status reg
4671 <status> */
ed957684 4672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4673 "0436 Adapter failed to init, "
09372820
JS
4674 "timeout, status reg x%x, "
4675 "FW Data: A8 x%x AC x%x\n", status,
4676 readl(phba->MBslimaddr + 0xa8),
4677 readl(phba->MBslimaddr + 0xac));
2e0fef85 4678 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4679 return -ETIMEDOUT;
4680 }
4681
4682 /* Check to see if any errors occurred during init */
4683 if (status & HS_FFERM) {
4684 /* ERROR: During chipset initialization */
4685 /* Adapter failed to init, chipset, status reg
4686 <status> */
ed957684 4687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4688 "0437 Adapter failed to init, "
09372820
JS
4689 "chipset, status reg x%x, "
4690 "FW Data: A8 x%x AC x%x\n", status,
4691 readl(phba->MBslimaddr + 0xa8),
4692 readl(phba->MBslimaddr + 0xac));
2e0fef85 4693 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4694 return -EIO;
4695 }
4696
dcf2a4e0 4697 if (i <= 10)
dea3101e 4698 msleep(10);
dcf2a4e0
JS
4699 else if (i <= 100)
4700 msleep(100);
4701 else
4702 msleep(1000);
dea3101e 4703
dcf2a4e0
JS
4704 if (i == 150) {
4705 /* Do post */
92d7f7b0 4706 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4707 lpfc_sli_brdrestart(phba);
dea3101e
JB
4708 }
4709 /* Read the HBA Host Status Register */
9940b97b
JS
4710 if (lpfc_readl(phba->HSregaddr, &status))
4711 return -EIO;
dea3101e
JB
4712 }
4713
4714 /* Check to see if any errors occurred during init */
4715 if (status & HS_FFERM) {
4716 /* ERROR: During chipset initialization */
4717 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4719 "0438 Adapter failed to init, chipset, "
09372820
JS
4720 "status reg x%x, "
4721 "FW Data: A8 x%x AC x%x\n", status,
4722 readl(phba->MBslimaddr + 0xa8),
4723 readl(phba->MBslimaddr + 0xac));
2e0fef85 4724 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4725 return -EIO;
4726 }
4727
4728 /* Clear all interrupt enable conditions */
4729 writel(0, phba->HCregaddr);
4730 readl(phba->HCregaddr); /* flush */
4731
4732 /* setup host attn register */
4733 writel(0xffffffff, phba->HAregaddr);
4734 readl(phba->HAregaddr); /* flush */
4735 return 0;
4736}
4737
e59058c4 4738/**
3621a710 4739 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4740 *
4741 * This function calculates and returns the number of HBQs required to be
4742 * configured.
4743 **/
78b2d852 4744int
ed957684
JS
4745lpfc_sli_hbq_count(void)
4746{
92d7f7b0 4747 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4748}
4749
e59058c4 4750/**
3621a710 4751 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4752 *
4753 * This function adds the number of hbq entries in every HBQ to get
4754 * the total number of hbq entries required for the HBA and returns
4755 * the total count.
4756 **/
ed957684
JS
4757static int
4758lpfc_sli_hbq_entry_count(void)
4759{
4760 int hbq_count = lpfc_sli_hbq_count();
4761 int count = 0;
4762 int i;
4763
4764 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4765 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4766 return count;
4767}
4768
e59058c4 4769/**
3621a710 4770 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4771 *
4772 * This function calculates amount of memory required for all hbq entries
4773 * to be configured and returns the total memory required.
4774 **/
dea3101e 4775int
ed957684
JS
4776lpfc_sli_hbq_size(void)
4777{
4778 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4779}
4780
e59058c4 4781/**
3621a710 4782 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4783 * @phba: Pointer to HBA context object.
4784 *
4785 * This function is called during the SLI initialization to configure
4786 * all the HBQs and post buffers to the HBQ. The caller is not
4787 * required to hold any locks. This function will return zero if successful
4788 * else it will return negative error code.
4789 **/
ed957684
JS
4790static int
4791lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4792{
4793 int hbq_count = lpfc_sli_hbq_count();
4794 LPFC_MBOXQ_t *pmb;
4795 MAILBOX_t *pmbox;
4796 uint32_t hbqno;
4797 uint32_t hbq_entry_index;
ed957684 4798
92d7f7b0
JS
4799 /* Get a Mailbox buffer to setup mailbox
4800 * commands for HBA initialization
4801 */
ed957684
JS
4802 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4803
4804 if (!pmb)
4805 return -ENOMEM;
4806
04c68496 4807 pmbox = &pmb->u.mb;
ed957684
JS
4808
4809 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4810 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4811 phba->hbq_in_use = 1;
ed957684
JS
4812
4813 hbq_entry_index = 0;
4814 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4815 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4816 phba->hbqs[hbqno].hbqPutIdx = 0;
4817 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4818 phba->hbqs[hbqno].entry_count =
92d7f7b0 4819 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4820 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4821 hbq_entry_index, pmb);
ed957684
JS
4822 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4823
4824 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4825 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4826 mbxStatus <status>, ring <num> */
4827
4828 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4829 LOG_SLI | LOG_VPORT,
e8b62011 4830 "1805 Adapter failed to init. "
ed957684 4831 "Data: x%x x%x x%x\n",
e8b62011 4832 pmbox->mbxCommand,
ed957684
JS
4833 pmbox->mbxStatus, hbqno);
4834
4835 phba->link_state = LPFC_HBA_ERROR;
4836 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4837 return -ENXIO;
ed957684
JS
4838 }
4839 }
4840 phba->hbq_count = hbq_count;
4841
ed957684
JS
4842 mempool_free(pmb, phba->mbox_mem_pool);
4843
92d7f7b0 4844 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4845 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4846 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4847 return 0;
4848}
4849
4f774513
JS
4850/**
4851 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4852 * @phba: Pointer to HBA context object.
4853 *
4854 * This function is called during the SLI initialization to configure
4855 * all the HBQs and post buffers to the HBQ. The caller is not
4856 * required to hold any locks. This function will return zero if successful
4857 * else it will return negative error code.
4858 **/
4859static int
4860lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4861{
4862 phba->hbq_in_use = 1;
895427bd
JS
4863 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4864 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4865 phba->hbq_count = 1;
895427bd 4866 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4867 /* Initially populate or replenish the HBQs */
4f774513
JS
4868 return 0;
4869}
4870
e59058c4 4871/**
3621a710 4872 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4873 * @phba: Pointer to HBA context object.
4874 * @sli_mode: sli mode - 2/3
4875 *
183b8021 4876 * This function is called by the sli initialization code path
e59058c4
JS
4877 * to issue config_port mailbox command. This function restarts the
4878 * HBA firmware and issues a config_port mailbox command to configure
4879 * the SLI interface in the sli mode specified by sli_mode
4880 * variable. The caller is not required to hold any locks.
4881 * The function returns 0 if successful, else returns negative error
4882 * code.
4883 **/
9399627f
JS
4884int
4885lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4886{
4887 LPFC_MBOXQ_t *pmb;
4888 uint32_t resetcount = 0, rc = 0, done = 0;
4889
4890 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4891 if (!pmb) {
2e0fef85 4892 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4893 return -ENOMEM;
4894 }
4895
ed957684 4896 phba->sli_rev = sli_mode;
dea3101e 4897 while (resetcount < 2 && !done) {
2e0fef85 4898 spin_lock_irq(&phba->hbalock);
1c067a42 4899 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4900 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4901 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4902 lpfc_sli_brdrestart(phba);
dea3101e
JB
4903 rc = lpfc_sli_chipset_init(phba);
4904 if (rc)
4905 break;
4906
2e0fef85 4907 spin_lock_irq(&phba->hbalock);
1c067a42 4908 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4909 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4910 resetcount++;
4911
ed957684
JS
4912 /* Call pre CONFIG_PORT mailbox command initialization. A
4913 * value of 0 means the call was successful. Any other
4914 * nonzero value is a failure, but if ERESTART is returned,
4915 * the driver may reset the HBA and try again.
4916 */
dea3101e
JB
4917 rc = lpfc_config_port_prep(phba);
4918 if (rc == -ERESTART) {
ed957684 4919 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4920 continue;
34b02dcd 4921 } else if (rc)
dea3101e 4922 break;
6d368e53 4923
2e0fef85 4924 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4925 lpfc_config_port(phba, pmb);
4926 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4927 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4928 LPFC_SLI3_HBQ_ENABLED |
4929 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4930 LPFC_SLI3_BG_ENABLED |
4931 LPFC_SLI3_DSS_ENABLED);
ed957684 4932 if (rc != MBX_SUCCESS) {
dea3101e 4933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4934 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4935 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4936 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4937 spin_lock_irq(&phba->hbalock);
04c68496 4938 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4939 spin_unlock_irq(&phba->hbalock);
4940 rc = -ENXIO;
04c68496
JS
4941 } else {
4942 /* Allow asynchronous mailbox command to go through */
4943 spin_lock_irq(&phba->hbalock);
4944 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4945 spin_unlock_irq(&phba->hbalock);
ed957684 4946 done = 1;
cb69f7de
JS
4947
4948 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4949 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4950 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4951 "3110 Port did not grant ASABT\n");
04c68496 4952 }
dea3101e 4953 }
ed957684
JS
4954 if (!done) {
4955 rc = -EINVAL;
4956 goto do_prep_failed;
4957 }
04c68496
JS
4958 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4959 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4960 rc = -ENXIO;
4961 goto do_prep_failed;
4962 }
04c68496 4963 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4964 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4965 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4966 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4967 phba->max_vpi : phba->max_vports;
4968
34b02dcd
JS
4969 } else
4970 phba->max_vpi = 0;
bc73905a
JS
4971 phba->fips_level = 0;
4972 phba->fips_spec_rev = 0;
4973 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4974 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4975 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4976 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4977 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4978 "2850 Security Crypto Active. FIPS x%d "
4979 "(Spec Rev: x%d)",
4980 phba->fips_level, phba->fips_spec_rev);
4981 }
4982 if (pmb->u.mb.un.varCfgPort.sec_err) {
4983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4984 "2856 Config Port Security Crypto "
4985 "Error: x%x ",
4986 pmb->u.mb.un.varCfgPort.sec_err);
4987 }
04c68496 4988 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4989 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4990 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4991 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4992
4993 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4994 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4995
4996 if (phba->cfg_enable_bg) {
04c68496 4997 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4998 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4999 else
5000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5001 "0443 Adapter did not grant "
5002 "BlockGuard\n");
5003 }
34b02dcd 5004 } else {
8f34f4ce 5005 phba->hbq_get = NULL;
34b02dcd 5006 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 5007 phba->max_vpi = 0;
ed957684 5008 }
92d7f7b0 5009do_prep_failed:
ed957684
JS
5010 mempool_free(pmb, phba->mbox_mem_pool);
5011 return rc;
5012}
5013
e59058c4
JS
5014
5015/**
183b8021 5016 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
5017 * @phba: Pointer to HBA context object.
5018 *
183b8021
MY
5019 * This function is the main SLI initialization function. This function
5020 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
5021 * error attention handler code. Caller is not required to hold any
5022 * locks. This function issues config_port mailbox command to configure
5023 * the SLI, setup iocb rings and HBQ rings. In the end the function
5024 * calls the config_port_post function to issue init_link mailbox
5025 * command and to start the discovery. The function will return zero
5026 * if successful, else it will return negative error code.
5027 **/
ed957684
JS
5028int
5029lpfc_sli_hba_setup(struct lpfc_hba *phba)
5030{
5031 uint32_t rc;
6d368e53
JS
5032 int mode = 3, i;
5033 int longs;
ed957684 5034
12247e81 5035 switch (phba->cfg_sli_mode) {
ed957684 5036 case 2:
78b2d852 5037 if (phba->cfg_enable_npiv) {
92d7f7b0 5038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 5039 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 5040 "parameter (%d) to auto (0).\n",
12247e81 5041 phba->cfg_sli_mode);
92d7f7b0
JS
5042 break;
5043 }
ed957684
JS
5044 mode = 2;
5045 break;
5046 case 0:
5047 case 3:
5048 break;
5049 default:
92d7f7b0 5050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
5051 "1819 Unrecognized sli_mode parameter: %d.\n",
5052 phba->cfg_sli_mode);
ed957684
JS
5053
5054 break;
5055 }
b5c53958 5056 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 5057
9399627f
JS
5058 rc = lpfc_sli_config_port(phba, mode);
5059
12247e81 5060 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 5061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
5062 "1820 Unable to select SLI-3. "
5063 "Not supported by adapter.\n");
ed957684 5064 if (rc && mode != 2)
9399627f 5065 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
5066 else if (rc && mode == 2)
5067 rc = lpfc_sli_config_port(phba, 3);
ed957684 5068 if (rc)
dea3101e
JB
5069 goto lpfc_sli_hba_setup_error;
5070
0d878419
JS
5071 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5072 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5073 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5074 if (!rc) {
5075 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5076 "2709 This device supports "
5077 "Advanced Error Reporting (AER)\n");
5078 spin_lock_irq(&phba->hbalock);
5079 phba->hba_flag |= HBA_AER_ENABLED;
5080 spin_unlock_irq(&phba->hbalock);
5081 } else {
5082 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5083 "2708 This device does not support "
b069d7eb
JS
5084 "Advanced Error Reporting (AER): %d\n",
5085 rc);
0d878419
JS
5086 phba->cfg_aer_support = 0;
5087 }
5088 }
5089
ed957684
JS
5090 if (phba->sli_rev == 3) {
5091 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5092 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
5093 } else {
5094 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5095 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 5096 phba->sli3_options = 0;
ed957684
JS
5097 }
5098
5099 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
5100 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5101 phba->sli_rev, phba->max_vpi);
ed957684 5102 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
5103
5104 if (rc)
5105 goto lpfc_sli_hba_setup_error;
5106
6d368e53
JS
5107 /* Initialize VPIs. */
5108 if (phba->sli_rev == LPFC_SLI_REV3) {
5109 /*
5110 * The VPI bitmask and physical ID array are allocated
5111 * and initialized once only - at driver load. A port
5112 * reset doesn't need to reinitialize this memory.
5113 */
5114 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5115 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5116 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5117 GFP_KERNEL);
5118 if (!phba->vpi_bmask) {
5119 rc = -ENOMEM;
5120 goto lpfc_sli_hba_setup_error;
5121 }
5122
5123 phba->vpi_ids = kzalloc(
5124 (phba->max_vpi+1) * sizeof(uint16_t),
5125 GFP_KERNEL);
5126 if (!phba->vpi_ids) {
5127 kfree(phba->vpi_bmask);
5128 rc = -ENOMEM;
5129 goto lpfc_sli_hba_setup_error;
5130 }
5131 for (i = 0; i < phba->max_vpi; i++)
5132 phba->vpi_ids[i] = i;
5133 }
5134 }
5135
9399627f 5136 /* Init HBQs */
ed957684
JS
5137 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5138 rc = lpfc_sli_hbq_setup(phba);
5139 if (rc)
5140 goto lpfc_sli_hba_setup_error;
5141 }
04c68496 5142 spin_lock_irq(&phba->hbalock);
dea3101e 5143 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 5144 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
5145
5146 rc = lpfc_config_port_post(phba);
5147 if (rc)
5148 goto lpfc_sli_hba_setup_error;
5149
ed957684
JS
5150 return rc;
5151
92d7f7b0 5152lpfc_sli_hba_setup_error:
2e0fef85 5153 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 5154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 5155 "0445 Firmware initialization failed\n");
dea3101e
JB
5156 return rc;
5157}
5158
e59058c4 5159/**
da0436e9
JS
5160 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5161 * @phba: Pointer to HBA context object.
5162 * @mboxq: mailbox pointer.
5163 * This function issue a dump mailbox command to read config region
5164 * 23 and parse the records in the region and populate driver
5165 * data structure.
e59058c4 5166 **/
da0436e9 5167static int
ff78d8f9 5168lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 5169{
ff78d8f9 5170 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
5171 struct lpfc_dmabuf *mp;
5172 struct lpfc_mqe *mqe;
5173 uint32_t data_length;
5174 int rc;
dea3101e 5175
da0436e9
JS
5176 /* Program the default value of vlan_id and fc_map */
5177 phba->valid_vlan = 0;
5178 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5179 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5180 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 5181
ff78d8f9
JS
5182 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5183 if (!mboxq)
da0436e9
JS
5184 return -ENOMEM;
5185
ff78d8f9
JS
5186 mqe = &mboxq->u.mqe;
5187 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5188 rc = -ENOMEM;
5189 goto out_free_mboxq;
5190 }
5191
da0436e9
JS
5192 mp = (struct lpfc_dmabuf *) mboxq->context1;
5193 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5194
5195 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5196 "(%d):2571 Mailbox cmd x%x Status x%x "
5197 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5198 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5199 "CQ: x%x x%x x%x x%x\n",
5200 mboxq->vport ? mboxq->vport->vpi : 0,
5201 bf_get(lpfc_mqe_command, mqe),
5202 bf_get(lpfc_mqe_status, mqe),
5203 mqe->un.mb_words[0], mqe->un.mb_words[1],
5204 mqe->un.mb_words[2], mqe->un.mb_words[3],
5205 mqe->un.mb_words[4], mqe->un.mb_words[5],
5206 mqe->un.mb_words[6], mqe->un.mb_words[7],
5207 mqe->un.mb_words[8], mqe->un.mb_words[9],
5208 mqe->un.mb_words[10], mqe->un.mb_words[11],
5209 mqe->un.mb_words[12], mqe->un.mb_words[13],
5210 mqe->un.mb_words[14], mqe->un.mb_words[15],
5211 mqe->un.mb_words[16], mqe->un.mb_words[50],
5212 mboxq->mcqe.word0,
5213 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5214 mboxq->mcqe.trailer);
5215
5216 if (rc) {
5217 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5218 kfree(mp);
ff78d8f9
JS
5219 rc = -EIO;
5220 goto out_free_mboxq;
da0436e9
JS
5221 }
5222 data_length = mqe->un.mb_words[5];
a0c87cbd 5223 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5224 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5225 kfree(mp);
ff78d8f9
JS
5226 rc = -EIO;
5227 goto out_free_mboxq;
d11e31dd 5228 }
dea3101e 5229
da0436e9
JS
5230 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5231 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5232 kfree(mp);
ff78d8f9
JS
5233 rc = 0;
5234
5235out_free_mboxq:
5236 mempool_free(mboxq, phba->mbox_mem_pool);
5237 return rc;
da0436e9 5238}
e59058c4
JS
5239
5240/**
da0436e9
JS
5241 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5242 * @phba: pointer to lpfc hba data structure.
5243 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5244 * @vpd: pointer to the memory to hold resulting port vpd data.
5245 * @vpd_size: On input, the number of bytes allocated to @vpd.
5246 * On output, the number of data bytes in @vpd.
e59058c4 5247 *
da0436e9
JS
5248 * This routine executes a READ_REV SLI4 mailbox command. In
5249 * addition, this routine gets the port vpd data.
5250 *
5251 * Return codes
af901ca1 5252 * 0 - successful
d439d286 5253 * -ENOMEM - could not allocated memory.
e59058c4 5254 **/
da0436e9
JS
5255static int
5256lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5257 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5258{
da0436e9
JS
5259 int rc = 0;
5260 uint32_t dma_size;
5261 struct lpfc_dmabuf *dmabuf;
5262 struct lpfc_mqe *mqe;
dea3101e 5263
da0436e9
JS
5264 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5265 if (!dmabuf)
5266 return -ENOMEM;
5267
5268 /*
5269 * Get a DMA buffer for the vpd data resulting from the READ_REV
5270 * mailbox command.
a257bf90 5271 */
da0436e9 5272 dma_size = *vpd_size;
1aee383d
JP
5273 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5274 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5275 if (!dmabuf->virt) {
5276 kfree(dmabuf);
5277 return -ENOMEM;
a257bf90
JS
5278 }
5279
da0436e9
JS
5280 /*
5281 * The SLI4 implementation of READ_REV conflicts at word1,
5282 * bits 31:16 and SLI4 adds vpd functionality not present
5283 * in SLI3. This code corrects the conflicts.
1dcb58e5 5284 */
da0436e9
JS
5285 lpfc_read_rev(phba, mboxq);
5286 mqe = &mboxq->u.mqe;
5287 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5288 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5289 mqe->un.read_rev.word1 &= 0x0000FFFF;
5290 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5291 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5292
5293 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5294 if (rc) {
5295 dma_free_coherent(&phba->pcidev->dev, dma_size,
5296 dmabuf->virt, dmabuf->phys);
def9c7a9 5297 kfree(dmabuf);
da0436e9
JS
5298 return -EIO;
5299 }
1dcb58e5 5300
da0436e9
JS
5301 /*
5302 * The available vpd length cannot be bigger than the
5303 * DMA buffer passed to the port. Catch the less than
5304 * case and update the caller's size.
5305 */
5306 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5307 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5308
d7c47992
JS
5309 memcpy(vpd, dmabuf->virt, *vpd_size);
5310
da0436e9
JS
5311 dma_free_coherent(&phba->pcidev->dev, dma_size,
5312 dmabuf->virt, dmabuf->phys);
5313 kfree(dmabuf);
5314 return 0;
dea3101e
JB
5315}
5316
cd1c8301
JS
5317/**
5318 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5319 * @phba: pointer to lpfc hba data structure.
5320 *
5321 * This routine retrieves SLI4 device physical port name this PCI function
5322 * is attached to.
5323 *
5324 * Return codes
4907cb7b 5325 * 0 - successful
cd1c8301
JS
5326 * otherwise - failed to retrieve physical port name
5327 **/
5328static int
5329lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5330{
5331 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5332 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5333 struct lpfc_controller_attribute *cntl_attr;
5334 struct lpfc_mbx_get_port_name *get_port_name;
5335 void *virtaddr = NULL;
5336 uint32_t alloclen, reqlen;
5337 uint32_t shdr_status, shdr_add_status;
5338 union lpfc_sli4_cfg_shdr *shdr;
5339 char cport_name = 0;
5340 int rc;
5341
5342 /* We assume nothing at this point */
5343 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5344 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5345
5346 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5347 if (!mboxq)
5348 return -ENOMEM;
cd1c8301 5349 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5350 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5351 lpfc_sli4_read_config(phba);
5352 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5353 goto retrieve_ppname;
cd1c8301
JS
5354
5355 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5356 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5357 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5358 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5359 LPFC_SLI4_MBX_NEMBED);
5360 if (alloclen < reqlen) {
5361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5362 "3084 Allocated DMA memory size (%d) is "
5363 "less than the requested DMA memory size "
5364 "(%d)\n", alloclen, reqlen);
5365 rc = -ENOMEM;
5366 goto out_free_mboxq;
5367 }
5368 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5369 virtaddr = mboxq->sge_array->addr[0];
5370 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5371 shdr = &mbx_cntl_attr->cfg_shdr;
5372 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5373 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5374 if (shdr_status || shdr_add_status || rc) {
5375 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5376 "3085 Mailbox x%x (x%x/x%x) failed, "
5377 "rc:x%x, status:x%x, add_status:x%x\n",
5378 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5379 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5380 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5381 rc, shdr_status, shdr_add_status);
5382 rc = -ENXIO;
5383 goto out_free_mboxq;
5384 }
5385 cntl_attr = &mbx_cntl_attr->cntl_attr;
5386 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5387 phba->sli4_hba.lnk_info.lnk_tp =
5388 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5389 phba->sli4_hba.lnk_info.lnk_no =
5390 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5392 "3086 lnk_type:%d, lnk_numb:%d\n",
5393 phba->sli4_hba.lnk_info.lnk_tp,
5394 phba->sli4_hba.lnk_info.lnk_no);
5395
5396retrieve_ppname:
5397 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5398 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5399 sizeof(struct lpfc_mbx_get_port_name) -
5400 sizeof(struct lpfc_sli4_cfg_mhdr),
5401 LPFC_SLI4_MBX_EMBED);
5402 get_port_name = &mboxq->u.mqe.un.get_port_name;
5403 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5404 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5405 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5406 phba->sli4_hba.lnk_info.lnk_tp);
5407 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5408 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5409 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5410 if (shdr_status || shdr_add_status || rc) {
5411 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5412 "3087 Mailbox x%x (x%x/x%x) failed: "
5413 "rc:x%x, status:x%x, add_status:x%x\n",
5414 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5415 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5416 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5417 rc, shdr_status, shdr_add_status);
5418 rc = -ENXIO;
5419 goto out_free_mboxq;
5420 }
5421 switch (phba->sli4_hba.lnk_info.lnk_no) {
5422 case LPFC_LINK_NUMBER_0:
5423 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5424 &get_port_name->u.response);
5425 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5426 break;
5427 case LPFC_LINK_NUMBER_1:
5428 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5429 &get_port_name->u.response);
5430 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5431 break;
5432 case LPFC_LINK_NUMBER_2:
5433 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5434 &get_port_name->u.response);
5435 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5436 break;
5437 case LPFC_LINK_NUMBER_3:
5438 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5439 &get_port_name->u.response);
5440 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5441 break;
5442 default:
5443 break;
5444 }
5445
5446 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5447 phba->Port[0] = cport_name;
5448 phba->Port[1] = '\0';
5449 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5450 "3091 SLI get port name: %s\n", phba->Port);
5451 }
5452
5453out_free_mboxq:
5454 if (rc != MBX_TIMEOUT) {
5455 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5456 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5457 else
5458 mempool_free(mboxq, phba->mbox_mem_pool);
5459 }
5460 return rc;
5461}
5462
e59058c4 5463/**
da0436e9
JS
5464 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5465 * @phba: pointer to lpfc hba data structure.
e59058c4 5466 *
da0436e9
JS
5467 * This routine is called to explicitly arm the SLI4 device's completion and
5468 * event queues
5469 **/
5470static void
5471lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5472{
895427bd 5473 int qidx;
b71413dd 5474 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
da0436e9 5475
b71413dd
JS
5476 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5477 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5478 if (sli4_hba->nvmels_cq)
5479 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
895427bd
JS
5480 LPFC_QUEUE_REARM);
5481
b71413dd 5482 if (sli4_hba->fcp_cq)
895427bd 5483 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
b71413dd 5484 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
895427bd
JS
5485 LPFC_QUEUE_REARM);
5486
b71413dd 5487 if (sli4_hba->nvme_cq)
895427bd 5488 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
b71413dd 5489 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
895427bd 5490 LPFC_QUEUE_REARM);
1ba981fd 5491
f38fa0bb 5492 if (phba->cfg_fof)
b71413dd 5493 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
1ba981fd 5494
b71413dd 5495 if (sli4_hba->hba_eq)
895427bd 5496 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
b71413dd
JS
5497 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5498 LPFC_QUEUE_REARM);
1ba981fd 5499
2d7dbc4c
JS
5500 if (phba->nvmet_support) {
5501 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
b71413dd
JS
5502 sli4_hba->sli4_cq_release(
5503 sli4_hba->nvmet_cqset[qidx],
2d7dbc4c
JS
5504 LPFC_QUEUE_REARM);
5505 }
2e90f4b5 5506 }
1ba981fd
JS
5507
5508 if (phba->cfg_fof)
b71413dd 5509 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5510}
5511
6d368e53
JS
5512/**
5513 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5514 * @phba: Pointer to HBA context object.
5515 * @type: The resource extent type.
b76f2dc9
JS
5516 * @extnt_count: buffer to hold port available extent count.
5517 * @extnt_size: buffer to hold element count per extent.
6d368e53 5518 *
b76f2dc9
JS
5519 * This function calls the port and retrievs the number of available
5520 * extents and their size for a particular extent type.
5521 *
5522 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5523 **/
b76f2dc9 5524int
6d368e53
JS
5525lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5526 uint16_t *extnt_count, uint16_t *extnt_size)
5527{
5528 int rc = 0;
5529 uint32_t length;
5530 uint32_t mbox_tmo;
5531 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5532 LPFC_MBOXQ_t *mbox;
5533
5534 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5535 if (!mbox)
5536 return -ENOMEM;
5537
5538 /* Find out how many extents are available for this resource type */
5539 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5540 sizeof(struct lpfc_sli4_cfg_mhdr));
5541 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5542 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5543 length, LPFC_SLI4_MBX_EMBED);
5544
5545 /* Send an extents count of 0 - the GET doesn't use it. */
5546 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5547 LPFC_SLI4_MBX_EMBED);
5548 if (unlikely(rc)) {
5549 rc = -EIO;
5550 goto err_exit;
5551 }
5552
5553 if (!phba->sli4_hba.intr_enable)
5554 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5555 else {
a183a15f 5556 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5557 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5558 }
5559 if (unlikely(rc)) {
5560 rc = -EIO;
5561 goto err_exit;
5562 }
5563
5564 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5565 if (bf_get(lpfc_mbox_hdr_status,
5566 &rsrc_info->header.cfg_shdr.response)) {
5567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5568 "2930 Failed to get resource extents "
5569 "Status 0x%x Add'l Status 0x%x\n",
5570 bf_get(lpfc_mbox_hdr_status,
5571 &rsrc_info->header.cfg_shdr.response),
5572 bf_get(lpfc_mbox_hdr_add_status,
5573 &rsrc_info->header.cfg_shdr.response));
5574 rc = -EIO;
5575 goto err_exit;
5576 }
5577
5578 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5579 &rsrc_info->u.rsp);
5580 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5581 &rsrc_info->u.rsp);
8a9d2e80
JS
5582
5583 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5584 "3162 Retrieved extents type-%d from port: count:%d, "
5585 "size:%d\n", type, *extnt_count, *extnt_size);
5586
5587err_exit:
6d368e53
JS
5588 mempool_free(mbox, phba->mbox_mem_pool);
5589 return rc;
5590}
5591
5592/**
5593 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5594 * @phba: Pointer to HBA context object.
5595 * @type: The extent type to check.
5596 *
5597 * This function reads the current available extents from the port and checks
5598 * if the extent count or extent size has changed since the last access.
5599 * Callers use this routine post port reset to understand if there is a
5600 * extent reprovisioning requirement.
5601 *
5602 * Returns:
5603 * -Error: error indicates problem.
5604 * 1: Extent count or size has changed.
5605 * 0: No changes.
5606 **/
5607static int
5608lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5609{
5610 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5611 uint16_t size_diff, rsrc_ext_size;
5612 int rc = 0;
5613 struct lpfc_rsrc_blks *rsrc_entry;
5614 struct list_head *rsrc_blk_list = NULL;
5615
5616 size_diff = 0;
5617 curr_ext_cnt = 0;
5618 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5619 &rsrc_ext_cnt,
5620 &rsrc_ext_size);
5621 if (unlikely(rc))
5622 return -EIO;
5623
5624 switch (type) {
5625 case LPFC_RSC_TYPE_FCOE_RPI:
5626 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5627 break;
5628 case LPFC_RSC_TYPE_FCOE_VPI:
5629 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5630 break;
5631 case LPFC_RSC_TYPE_FCOE_XRI:
5632 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5633 break;
5634 case LPFC_RSC_TYPE_FCOE_VFI:
5635 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5636 break;
5637 default:
5638 break;
5639 }
5640
5641 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5642 curr_ext_cnt++;
5643 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5644 size_diff++;
5645 }
5646
5647 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5648 rc = 1;
5649
5650 return rc;
5651}
5652
5653/**
5654 * lpfc_sli4_cfg_post_extnts -
5655 * @phba: Pointer to HBA context object.
5656 * @extnt_cnt - number of available extents.
5657 * @type - the extent type (rpi, xri, vfi, vpi).
5658 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5659 * @mbox - pointer to the caller's allocated mailbox structure.
5660 *
5661 * This function executes the extents allocation request. It also
5662 * takes care of the amount of memory needed to allocate or get the
5663 * allocated extents. It is the caller's responsibility to evaluate
5664 * the response.
5665 *
5666 * Returns:
5667 * -Error: Error value describes the condition found.
5668 * 0: if successful
5669 **/
5670static int
8a9d2e80 5671lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5672 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5673{
5674 int rc = 0;
5675 uint32_t req_len;
5676 uint32_t emb_len;
5677 uint32_t alloc_len, mbox_tmo;
5678
5679 /* Calculate the total requested length of the dma memory */
8a9d2e80 5680 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5681
5682 /*
5683 * Calculate the size of an embedded mailbox. The uint32_t
5684 * accounts for extents-specific word.
5685 */
5686 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5687 sizeof(uint32_t);
5688
5689 /*
5690 * Presume the allocation and response will fit into an embedded
5691 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5692 */
5693 *emb = LPFC_SLI4_MBX_EMBED;
5694 if (req_len > emb_len) {
8a9d2e80 5695 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5696 sizeof(union lpfc_sli4_cfg_shdr) +
5697 sizeof(uint32_t);
5698 *emb = LPFC_SLI4_MBX_NEMBED;
5699 }
5700
5701 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5702 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5703 req_len, *emb);
5704 if (alloc_len < req_len) {
5705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5706 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5707 "less than the requested DMA memory "
5708 "size (x%x)\n", alloc_len, req_len);
5709 return -ENOMEM;
5710 }
8a9d2e80 5711 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5712 if (unlikely(rc))
5713 return -EIO;
5714
5715 if (!phba->sli4_hba.intr_enable)
5716 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5717 else {
a183a15f 5718 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5719 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5720 }
5721
5722 if (unlikely(rc))
5723 rc = -EIO;
5724 return rc;
5725}
5726
5727/**
5728 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5729 * @phba: Pointer to HBA context object.
5730 * @type: The resource extent type to allocate.
5731 *
5732 * This function allocates the number of elements for the specified
5733 * resource type.
5734 **/
5735static int
5736lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5737{
5738 bool emb = false;
5739 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5740 uint16_t rsrc_id, rsrc_start, j, k;
5741 uint16_t *ids;
5742 int i, rc;
5743 unsigned long longs;
5744 unsigned long *bmask;
5745 struct lpfc_rsrc_blks *rsrc_blks;
5746 LPFC_MBOXQ_t *mbox;
5747 uint32_t length;
5748 struct lpfc_id_range *id_array = NULL;
5749 void *virtaddr = NULL;
5750 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5751 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5752 struct list_head *ext_blk_list;
5753
5754 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5755 &rsrc_cnt,
5756 &rsrc_size);
5757 if (unlikely(rc))
5758 return -EIO;
5759
5760 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5761 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5762 "3009 No available Resource Extents "
5763 "for resource type 0x%x: Count: 0x%x, "
5764 "Size 0x%x\n", type, rsrc_cnt,
5765 rsrc_size);
5766 return -ENOMEM;
5767 }
5768
8a9d2e80
JS
5769 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5770 "2903 Post resource extents type-0x%x: "
5771 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5772
5773 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5774 if (!mbox)
5775 return -ENOMEM;
5776
8a9d2e80 5777 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5778 if (unlikely(rc)) {
5779 rc = -EIO;
5780 goto err_exit;
5781 }
5782
5783 /*
5784 * Figure out where the response is located. Then get local pointers
5785 * to the response data. The port does not guarantee to respond to
5786 * all extents counts request so update the local variable with the
5787 * allocated count from the port.
5788 */
5789 if (emb == LPFC_SLI4_MBX_EMBED) {
5790 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5791 id_array = &rsrc_ext->u.rsp.id[0];
5792 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5793 } else {
5794 virtaddr = mbox->sge_array->addr[0];
5795 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5796 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5797 id_array = &n_rsrc->id;
5798 }
5799
5800 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5801 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5802
5803 /*
5804 * Based on the resource size and count, correct the base and max
5805 * resource values.
5806 */
5807 length = sizeof(struct lpfc_rsrc_blks);
5808 switch (type) {
5809 case LPFC_RSC_TYPE_FCOE_RPI:
5810 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5811 sizeof(unsigned long),
5812 GFP_KERNEL);
5813 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5814 rc = -ENOMEM;
5815 goto err_exit;
5816 }
5817 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5818 sizeof(uint16_t),
5819 GFP_KERNEL);
5820 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5821 kfree(phba->sli4_hba.rpi_bmask);
5822 rc = -ENOMEM;
5823 goto err_exit;
5824 }
5825
5826 /*
5827 * The next_rpi was initialized with the maximum available
5828 * count but the port may allocate a smaller number. Catch
5829 * that case and update the next_rpi.
5830 */
5831 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5832
5833 /* Initialize local ptrs for common extent processing later. */
5834 bmask = phba->sli4_hba.rpi_bmask;
5835 ids = phba->sli4_hba.rpi_ids;
5836 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5837 break;
5838 case LPFC_RSC_TYPE_FCOE_VPI:
5839 phba->vpi_bmask = kzalloc(longs *
5840 sizeof(unsigned long),
5841 GFP_KERNEL);
5842 if (unlikely(!phba->vpi_bmask)) {
5843 rc = -ENOMEM;
5844 goto err_exit;
5845 }
5846 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5847 sizeof(uint16_t),
5848 GFP_KERNEL);
5849 if (unlikely(!phba->vpi_ids)) {
5850 kfree(phba->vpi_bmask);
5851 rc = -ENOMEM;
5852 goto err_exit;
5853 }
5854
5855 /* Initialize local ptrs for common extent processing later. */
5856 bmask = phba->vpi_bmask;
5857 ids = phba->vpi_ids;
5858 ext_blk_list = &phba->lpfc_vpi_blk_list;
5859 break;
5860 case LPFC_RSC_TYPE_FCOE_XRI:
5861 phba->sli4_hba.xri_bmask = kzalloc(longs *
5862 sizeof(unsigned long),
5863 GFP_KERNEL);
5864 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5865 rc = -ENOMEM;
5866 goto err_exit;
5867 }
8a9d2e80 5868 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5869 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5870 sizeof(uint16_t),
5871 GFP_KERNEL);
5872 if (unlikely(!phba->sli4_hba.xri_ids)) {
5873 kfree(phba->sli4_hba.xri_bmask);
5874 rc = -ENOMEM;
5875 goto err_exit;
5876 }
5877
5878 /* Initialize local ptrs for common extent processing later. */
5879 bmask = phba->sli4_hba.xri_bmask;
5880 ids = phba->sli4_hba.xri_ids;
5881 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5882 break;
5883 case LPFC_RSC_TYPE_FCOE_VFI:
5884 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5885 sizeof(unsigned long),
5886 GFP_KERNEL);
5887 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5888 rc = -ENOMEM;
5889 goto err_exit;
5890 }
5891 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5892 sizeof(uint16_t),
5893 GFP_KERNEL);
5894 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5895 kfree(phba->sli4_hba.vfi_bmask);
5896 rc = -ENOMEM;
5897 goto err_exit;
5898 }
5899
5900 /* Initialize local ptrs for common extent processing later. */
5901 bmask = phba->sli4_hba.vfi_bmask;
5902 ids = phba->sli4_hba.vfi_ids;
5903 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5904 break;
5905 default:
5906 /* Unsupported Opcode. Fail call. */
5907 id_array = NULL;
5908 bmask = NULL;
5909 ids = NULL;
5910 ext_blk_list = NULL;
5911 goto err_exit;
5912 }
5913
5914 /*
5915 * Complete initializing the extent configuration with the
5916 * allocated ids assigned to this function. The bitmask serves
5917 * as an index into the array and manages the available ids. The
5918 * array just stores the ids communicated to the port via the wqes.
5919 */
5920 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5921 if ((i % 2) == 0)
5922 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5923 &id_array[k]);
5924 else
5925 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5926 &id_array[k]);
5927
5928 rsrc_blks = kzalloc(length, GFP_KERNEL);
5929 if (unlikely(!rsrc_blks)) {
5930 rc = -ENOMEM;
5931 kfree(bmask);
5932 kfree(ids);
5933 goto err_exit;
5934 }
5935 rsrc_blks->rsrc_start = rsrc_id;
5936 rsrc_blks->rsrc_size = rsrc_size;
5937 list_add_tail(&rsrc_blks->list, ext_blk_list);
5938 rsrc_start = rsrc_id;
895427bd 5939 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5940 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5941 lpfc_sli4_get_iocb_cnt(phba);
5942 phba->sli4_hba.nvme_xri_start =
5943 phba->sli4_hba.scsi_xri_start +
5944 phba->sli4_hba.scsi_xri_max;
5945 }
6d368e53
JS
5946
5947 while (rsrc_id < (rsrc_start + rsrc_size)) {
5948 ids[j] = rsrc_id;
5949 rsrc_id++;
5950 j++;
5951 }
5952 /* Entire word processed. Get next word.*/
5953 if ((i % 2) == 1)
5954 k++;
5955 }
5956 err_exit:
5957 lpfc_sli4_mbox_cmd_free(phba, mbox);
5958 return rc;
5959}
5960
895427bd
JS
5961
5962
6d368e53
JS
5963/**
5964 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5965 * @phba: Pointer to HBA context object.
5966 * @type: the extent's type.
5967 *
5968 * This function deallocates all extents of a particular resource type.
5969 * SLI4 does not allow for deallocating a particular extent range. It
5970 * is the caller's responsibility to release all kernel memory resources.
5971 **/
5972static int
5973lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5974{
5975 int rc;
5976 uint32_t length, mbox_tmo = 0;
5977 LPFC_MBOXQ_t *mbox;
5978 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5979 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5980
5981 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5982 if (!mbox)
5983 return -ENOMEM;
5984
5985 /*
5986 * This function sends an embedded mailbox because it only sends the
5987 * the resource type. All extents of this type are released by the
5988 * port.
5989 */
5990 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5991 sizeof(struct lpfc_sli4_cfg_mhdr));
5992 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5993 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5994 length, LPFC_SLI4_MBX_EMBED);
5995
5996 /* Send an extents count of 0 - the dealloc doesn't use it. */
5997 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5998 LPFC_SLI4_MBX_EMBED);
5999 if (unlikely(rc)) {
6000 rc = -EIO;
6001 goto out_free_mbox;
6002 }
6003 if (!phba->sli4_hba.intr_enable)
6004 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6005 else {
a183a15f 6006 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
6007 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6008 }
6009 if (unlikely(rc)) {
6010 rc = -EIO;
6011 goto out_free_mbox;
6012 }
6013
6014 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6015 if (bf_get(lpfc_mbox_hdr_status,
6016 &dealloc_rsrc->header.cfg_shdr.response)) {
6017 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6018 "2919 Failed to release resource extents "
6019 "for type %d - Status 0x%x Add'l Status 0x%x. "
6020 "Resource memory not released.\n",
6021 type,
6022 bf_get(lpfc_mbox_hdr_status,
6023 &dealloc_rsrc->header.cfg_shdr.response),
6024 bf_get(lpfc_mbox_hdr_add_status,
6025 &dealloc_rsrc->header.cfg_shdr.response));
6026 rc = -EIO;
6027 goto out_free_mbox;
6028 }
6029
6030 /* Release kernel memory resources for the specific type. */
6031 switch (type) {
6032 case LPFC_RSC_TYPE_FCOE_VPI:
6033 kfree(phba->vpi_bmask);
6034 kfree(phba->vpi_ids);
6035 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6036 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6037 &phba->lpfc_vpi_blk_list, list) {
6038 list_del_init(&rsrc_blk->list);
6039 kfree(rsrc_blk);
6040 }
16a3a208 6041 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6042 break;
6043 case LPFC_RSC_TYPE_FCOE_XRI:
6044 kfree(phba->sli4_hba.xri_bmask);
6045 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6046 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6047 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6048 list_del_init(&rsrc_blk->list);
6049 kfree(rsrc_blk);
6050 }
6051 break;
6052 case LPFC_RSC_TYPE_FCOE_VFI:
6053 kfree(phba->sli4_hba.vfi_bmask);
6054 kfree(phba->sli4_hba.vfi_ids);
6055 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6056 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6057 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6058 list_del_init(&rsrc_blk->list);
6059 kfree(rsrc_blk);
6060 }
6061 break;
6062 case LPFC_RSC_TYPE_FCOE_RPI:
6063 /* RPI bitmask and physical id array are cleaned up earlier. */
6064 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6065 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6066 list_del_init(&rsrc_blk->list);
6067 kfree(rsrc_blk);
6068 }
6069 break;
6070 default:
6071 break;
6072 }
6073
6074 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6075
6076 out_free_mbox:
6077 mempool_free(mbox, phba->mbox_mem_pool);
6078 return rc;
6079}
6080
bd4b3e5c 6081static void
7bdedb34
JS
6082lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6083 uint32_t feature)
65791f1f 6084{
65791f1f 6085 uint32_t len;
65791f1f 6086
65791f1f
JS
6087 len = sizeof(struct lpfc_mbx_set_feature) -
6088 sizeof(struct lpfc_sli4_cfg_mhdr);
6089 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6090 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6091 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
6092
6093 switch (feature) {
6094 case LPFC_SET_UE_RECOVERY:
6095 bf_set(lpfc_mbx_set_feature_UER,
6096 &mbox->u.mqe.un.set_feature, 1);
6097 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6098 mbox->u.mqe.un.set_feature.param_len = 8;
6099 break;
6100 case LPFC_SET_MDS_DIAGS:
6101 bf_set(lpfc_mbx_set_feature_mds,
6102 &mbox->u.mqe.un.set_feature, 1);
6103 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 6104 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
6105 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6106 mbox->u.mqe.un.set_feature.param_len = 8;
6107 break;
65791f1f 6108 }
7bdedb34
JS
6109
6110 return;
65791f1f
JS
6111}
6112
6d368e53
JS
6113/**
6114 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6115 * @phba: Pointer to HBA context object.
6116 *
6117 * This function allocates all SLI4 resource identifiers.
6118 **/
6119int
6120lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6121{
6122 int i, rc, error = 0;
6123 uint16_t count, base;
6124 unsigned long longs;
6125
ff78d8f9
JS
6126 if (!phba->sli4_hba.rpi_hdrs_in_use)
6127 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
6128 if (phba->sli4_hba.extents_in_use) {
6129 /*
6130 * The port supports resource extents. The XRI, VPI, VFI, RPI
6131 * resource extent count must be read and allocated before
6132 * provisioning the resource id arrays.
6133 */
6134 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6135 LPFC_IDX_RSRC_RDY) {
6136 /*
6137 * Extent-based resources are set - the driver could
6138 * be in a port reset. Figure out if any corrective
6139 * actions need to be taken.
6140 */
6141 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6142 LPFC_RSC_TYPE_FCOE_VFI);
6143 if (rc != 0)
6144 error++;
6145 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6146 LPFC_RSC_TYPE_FCOE_VPI);
6147 if (rc != 0)
6148 error++;
6149 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6150 LPFC_RSC_TYPE_FCOE_XRI);
6151 if (rc != 0)
6152 error++;
6153 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6154 LPFC_RSC_TYPE_FCOE_RPI);
6155 if (rc != 0)
6156 error++;
6157
6158 /*
6159 * It's possible that the number of resources
6160 * provided to this port instance changed between
6161 * resets. Detect this condition and reallocate
6162 * resources. Otherwise, there is no action.
6163 */
6164 if (error) {
6165 lpfc_printf_log(phba, KERN_INFO,
6166 LOG_MBOX | LOG_INIT,
6167 "2931 Detected extent resource "
6168 "change. Reallocating all "
6169 "extents.\n");
6170 rc = lpfc_sli4_dealloc_extent(phba,
6171 LPFC_RSC_TYPE_FCOE_VFI);
6172 rc = lpfc_sli4_dealloc_extent(phba,
6173 LPFC_RSC_TYPE_FCOE_VPI);
6174 rc = lpfc_sli4_dealloc_extent(phba,
6175 LPFC_RSC_TYPE_FCOE_XRI);
6176 rc = lpfc_sli4_dealloc_extent(phba,
6177 LPFC_RSC_TYPE_FCOE_RPI);
6178 } else
6179 return 0;
6180 }
6181
6182 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6183 if (unlikely(rc))
6184 goto err_exit;
6185
6186 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6187 if (unlikely(rc))
6188 goto err_exit;
6189
6190 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6191 if (unlikely(rc))
6192 goto err_exit;
6193
6194 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6195 if (unlikely(rc))
6196 goto err_exit;
6197 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6198 LPFC_IDX_RSRC_RDY);
6199 return rc;
6200 } else {
6201 /*
6202 * The port does not support resource extents. The XRI, VPI,
6203 * VFI, RPI resource ids were determined from READ_CONFIG.
6204 * Just allocate the bitmasks and provision the resource id
6205 * arrays. If a port reset is active, the resources don't
6206 * need any action - just exit.
6207 */
6208 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6209 LPFC_IDX_RSRC_RDY) {
6210 lpfc_sli4_dealloc_resource_identifiers(phba);
6211 lpfc_sli4_remove_rpis(phba);
6212 }
6d368e53
JS
6213 /* RPIs. */
6214 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6215 if (count <= 0) {
6216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6217 "3279 Invalid provisioning of "
6218 "rpi:%d\n", count);
6219 rc = -EINVAL;
6220 goto err_exit;
6221 }
6d368e53
JS
6222 base = phba->sli4_hba.max_cfg_param.rpi_base;
6223 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6224 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6225 sizeof(unsigned long),
6226 GFP_KERNEL);
6227 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6228 rc = -ENOMEM;
6229 goto err_exit;
6230 }
6231 phba->sli4_hba.rpi_ids = kzalloc(count *
6232 sizeof(uint16_t),
6233 GFP_KERNEL);
6234 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6235 rc = -ENOMEM;
6236 goto free_rpi_bmask;
6237 }
6238
6239 for (i = 0; i < count; i++)
6240 phba->sli4_hba.rpi_ids[i] = base + i;
6241
6242 /* VPIs. */
6243 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6244 if (count <= 0) {
6245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6246 "3280 Invalid provisioning of "
6247 "vpi:%d\n", count);
6248 rc = -EINVAL;
6249 goto free_rpi_ids;
6250 }
6d368e53
JS
6251 base = phba->sli4_hba.max_cfg_param.vpi_base;
6252 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6253 phba->vpi_bmask = kzalloc(longs *
6254 sizeof(unsigned long),
6255 GFP_KERNEL);
6256 if (unlikely(!phba->vpi_bmask)) {
6257 rc = -ENOMEM;
6258 goto free_rpi_ids;
6259 }
6260 phba->vpi_ids = kzalloc(count *
6261 sizeof(uint16_t),
6262 GFP_KERNEL);
6263 if (unlikely(!phba->vpi_ids)) {
6264 rc = -ENOMEM;
6265 goto free_vpi_bmask;
6266 }
6267
6268 for (i = 0; i < count; i++)
6269 phba->vpi_ids[i] = base + i;
6270
6271 /* XRIs. */
6272 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6273 if (count <= 0) {
6274 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6275 "3281 Invalid provisioning of "
6276 "xri:%d\n", count);
6277 rc = -EINVAL;
6278 goto free_vpi_ids;
6279 }
6d368e53
JS
6280 base = phba->sli4_hba.max_cfg_param.xri_base;
6281 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6282 phba->sli4_hba.xri_bmask = kzalloc(longs *
6283 sizeof(unsigned long),
6284 GFP_KERNEL);
6285 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6286 rc = -ENOMEM;
6287 goto free_vpi_ids;
6288 }
41899be7 6289 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6290 phba->sli4_hba.xri_ids = kzalloc(count *
6291 sizeof(uint16_t),
6292 GFP_KERNEL);
6293 if (unlikely(!phba->sli4_hba.xri_ids)) {
6294 rc = -ENOMEM;
6295 goto free_xri_bmask;
6296 }
6297
6298 for (i = 0; i < count; i++)
6299 phba->sli4_hba.xri_ids[i] = base + i;
6300
6301 /* VFIs. */
6302 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6303 if (count <= 0) {
6304 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6305 "3282 Invalid provisioning of "
6306 "vfi:%d\n", count);
6307 rc = -EINVAL;
6308 goto free_xri_ids;
6309 }
6d368e53
JS
6310 base = phba->sli4_hba.max_cfg_param.vfi_base;
6311 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6312 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6313 sizeof(unsigned long),
6314 GFP_KERNEL);
6315 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6316 rc = -ENOMEM;
6317 goto free_xri_ids;
6318 }
6319 phba->sli4_hba.vfi_ids = kzalloc(count *
6320 sizeof(uint16_t),
6321 GFP_KERNEL);
6322 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6323 rc = -ENOMEM;
6324 goto free_vfi_bmask;
6325 }
6326
6327 for (i = 0; i < count; i++)
6328 phba->sli4_hba.vfi_ids[i] = base + i;
6329
6330 /*
6331 * Mark all resources ready. An HBA reset doesn't need
6332 * to reset the initialization.
6333 */
6334 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6335 LPFC_IDX_RSRC_RDY);
6336 return 0;
6337 }
6338
6339 free_vfi_bmask:
6340 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6341 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6342 free_xri_ids:
6343 kfree(phba->sli4_hba.xri_ids);
cd60be49 6344 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6345 free_xri_bmask:
6346 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6347 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6348 free_vpi_ids:
6349 kfree(phba->vpi_ids);
cd60be49 6350 phba->vpi_ids = NULL;
6d368e53
JS
6351 free_vpi_bmask:
6352 kfree(phba->vpi_bmask);
cd60be49 6353 phba->vpi_bmask = NULL;
6d368e53
JS
6354 free_rpi_ids:
6355 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6356 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6357 free_rpi_bmask:
6358 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6359 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6360 err_exit:
6361 return rc;
6362}
6363
6364/**
6365 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6366 * @phba: Pointer to HBA context object.
6367 *
6368 * This function allocates the number of elements for the specified
6369 * resource type.
6370 **/
6371int
6372lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6373{
6374 if (phba->sli4_hba.extents_in_use) {
6375 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6376 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6377 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6378 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6379 } else {
6380 kfree(phba->vpi_bmask);
16a3a208 6381 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6382 kfree(phba->vpi_ids);
6383 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6384 kfree(phba->sli4_hba.xri_bmask);
6385 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6386 kfree(phba->sli4_hba.vfi_bmask);
6387 kfree(phba->sli4_hba.vfi_ids);
6388 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6389 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6390 }
6391
6392 return 0;
6393}
6394
b76f2dc9
JS
6395/**
6396 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6397 * @phba: Pointer to HBA context object.
6398 * @type: The resource extent type.
6399 * @extnt_count: buffer to hold port extent count response
6400 * @extnt_size: buffer to hold port extent size response.
6401 *
6402 * This function calls the port to read the host allocated extents
6403 * for a particular type.
6404 **/
6405int
6406lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6407 uint16_t *extnt_cnt, uint16_t *extnt_size)
6408{
6409 bool emb;
6410 int rc = 0;
6411 uint16_t curr_blks = 0;
6412 uint32_t req_len, emb_len;
6413 uint32_t alloc_len, mbox_tmo;
6414 struct list_head *blk_list_head;
6415 struct lpfc_rsrc_blks *rsrc_blk;
6416 LPFC_MBOXQ_t *mbox;
6417 void *virtaddr = NULL;
6418 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6419 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6420 union lpfc_sli4_cfg_shdr *shdr;
6421
6422 switch (type) {
6423 case LPFC_RSC_TYPE_FCOE_VPI:
6424 blk_list_head = &phba->lpfc_vpi_blk_list;
6425 break;
6426 case LPFC_RSC_TYPE_FCOE_XRI:
6427 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6428 break;
6429 case LPFC_RSC_TYPE_FCOE_VFI:
6430 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6431 break;
6432 case LPFC_RSC_TYPE_FCOE_RPI:
6433 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6434 break;
6435 default:
6436 return -EIO;
6437 }
6438
6439 /* Count the number of extents currently allocatd for this type. */
6440 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6441 if (curr_blks == 0) {
6442 /*
6443 * The GET_ALLOCATED mailbox does not return the size,
6444 * just the count. The size should be just the size
6445 * stored in the current allocated block and all sizes
6446 * for an extent type are the same so set the return
6447 * value now.
6448 */
6449 *extnt_size = rsrc_blk->rsrc_size;
6450 }
6451 curr_blks++;
6452 }
6453
b76f2dc9
JS
6454 /*
6455 * Calculate the size of an embedded mailbox. The uint32_t
6456 * accounts for extents-specific word.
6457 */
6458 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6459 sizeof(uint32_t);
6460
6461 /*
6462 * Presume the allocation and response will fit into an embedded
6463 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6464 */
6465 emb = LPFC_SLI4_MBX_EMBED;
6466 req_len = emb_len;
6467 if (req_len > emb_len) {
6468 req_len = curr_blks * sizeof(uint16_t) +
6469 sizeof(union lpfc_sli4_cfg_shdr) +
6470 sizeof(uint32_t);
6471 emb = LPFC_SLI4_MBX_NEMBED;
6472 }
6473
6474 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6475 if (!mbox)
6476 return -ENOMEM;
6477 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6478
6479 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6480 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6481 req_len, emb);
6482 if (alloc_len < req_len) {
6483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6484 "2983 Allocated DMA memory size (x%x) is "
6485 "less than the requested DMA memory "
6486 "size (x%x)\n", alloc_len, req_len);
6487 rc = -ENOMEM;
6488 goto err_exit;
6489 }
6490 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6491 if (unlikely(rc)) {
6492 rc = -EIO;
6493 goto err_exit;
6494 }
6495
6496 if (!phba->sli4_hba.intr_enable)
6497 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6498 else {
a183a15f 6499 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6500 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6501 }
6502
6503 if (unlikely(rc)) {
6504 rc = -EIO;
6505 goto err_exit;
6506 }
6507
6508 /*
6509 * Figure out where the response is located. Then get local pointers
6510 * to the response data. The port does not guarantee to respond to
6511 * all extents counts request so update the local variable with the
6512 * allocated count from the port.
6513 */
6514 if (emb == LPFC_SLI4_MBX_EMBED) {
6515 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6516 shdr = &rsrc_ext->header.cfg_shdr;
6517 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6518 } else {
6519 virtaddr = mbox->sge_array->addr[0];
6520 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6521 shdr = &n_rsrc->cfg_shdr;
6522 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6523 }
6524
6525 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6527 "2984 Failed to read allocated resources "
6528 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6529 type,
6530 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6531 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6532 rc = -EIO;
6533 goto err_exit;
6534 }
6535 err_exit:
6536 lpfc_sli4_mbox_cmd_free(phba, mbox);
6537 return rc;
6538}
6539
8a9d2e80 6540/**
0ef69968 6541 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6542 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6543 * @pring: Pointer to driver SLI ring object.
6544 * @sgl_list: linked link of sgl buffers to post
6545 * @cnt: number of linked list buffers
8a9d2e80 6546 *
895427bd 6547 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6548 * repost them to the port by using SGL block post. This is needed after a
6549 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6550 * of buffer sgls which contains contiguous xris and uses the non-embedded
6551 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6552 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6553 * mailbox command for posting.
6554 *
6555 * Returns: 0 = success, non-zero failure.
6556 **/
6557static int
895427bd
JS
6558lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6559 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6560{
6561 struct lpfc_sglq *sglq_entry = NULL;
6562 struct lpfc_sglq *sglq_entry_next = NULL;
6563 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6564 int status, total_cnt;
6565 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6566 int last_xritag = NO_XRI;
6567 LIST_HEAD(prep_sgl_list);
6568 LIST_HEAD(blck_sgl_list);
6569 LIST_HEAD(allc_sgl_list);
6570 LIST_HEAD(post_sgl_list);
6571 LIST_HEAD(free_sgl_list);
6572
38c20673 6573 spin_lock_irq(&phba->hbalock);
895427bd
JS
6574 spin_lock(&phba->sli4_hba.sgl_list_lock);
6575 list_splice_init(sgl_list, &allc_sgl_list);
6576 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6577 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6578
895427bd 6579 total_cnt = cnt;
8a9d2e80
JS
6580 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6581 &allc_sgl_list, list) {
6582 list_del_init(&sglq_entry->list);
6583 block_cnt++;
6584 if ((last_xritag != NO_XRI) &&
6585 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6586 /* a hole in xri block, form a sgl posting block */
6587 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6588 post_cnt = block_cnt - 1;
6589 /* prepare list for next posting block */
6590 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6591 block_cnt = 1;
6592 } else {
6593 /* prepare list for next posting block */
6594 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6595 /* enough sgls for non-embed sgl mbox command */
6596 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6597 list_splice_init(&prep_sgl_list,
6598 &blck_sgl_list);
6599 post_cnt = block_cnt;
6600 block_cnt = 0;
6601 }
6602 }
6603 num_posted++;
6604
6605 /* keep track of last sgl's xritag */
6606 last_xritag = sglq_entry->sli4_xritag;
6607
895427bd
JS
6608 /* end of repost sgl list condition for buffers */
6609 if (num_posted == total_cnt) {
8a9d2e80
JS
6610 if (post_cnt == 0) {
6611 list_splice_init(&prep_sgl_list,
6612 &blck_sgl_list);
6613 post_cnt = block_cnt;
6614 } else if (block_cnt == 1) {
6615 status = lpfc_sli4_post_sgl(phba,
6616 sglq_entry->phys, 0,
6617 sglq_entry->sli4_xritag);
6618 if (!status) {
6619 /* successful, put sgl to posted list */
6620 list_add_tail(&sglq_entry->list,
6621 &post_sgl_list);
6622 } else {
6623 /* Failure, put sgl to free list */
6624 lpfc_printf_log(phba, KERN_WARNING,
6625 LOG_SLI,
895427bd 6626 "3159 Failed to post "
8a9d2e80
JS
6627 "sgl, xritag:x%x\n",
6628 sglq_entry->sli4_xritag);
6629 list_add_tail(&sglq_entry->list,
6630 &free_sgl_list);
711ea882 6631 total_cnt--;
8a9d2e80
JS
6632 }
6633 }
6634 }
6635
6636 /* continue until a nembed page worth of sgls */
6637 if (post_cnt == 0)
6638 continue;
6639
895427bd
JS
6640 /* post the buffer list sgls as a block */
6641 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6642 post_cnt);
8a9d2e80
JS
6643
6644 if (!status) {
6645 /* success, put sgl list to posted sgl list */
6646 list_splice_init(&blck_sgl_list, &post_sgl_list);
6647 } else {
6648 /* Failure, put sgl list to free sgl list */
6649 sglq_entry_first = list_first_entry(&blck_sgl_list,
6650 struct lpfc_sglq,
6651 list);
6652 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6653 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6654 "xritag:x%x-x%x\n",
6655 sglq_entry_first->sli4_xritag,
6656 (sglq_entry_first->sli4_xritag +
6657 post_cnt - 1));
6658 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6659 total_cnt -= post_cnt;
8a9d2e80
JS
6660 }
6661
6662 /* don't reset xirtag due to hole in xri block */
6663 if (block_cnt == 0)
6664 last_xritag = NO_XRI;
6665
895427bd 6666 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6667 post_cnt = 0;
6668 }
6669
895427bd 6670 /* free the sgls failed to post */
8a9d2e80
JS
6671 lpfc_free_sgl_list(phba, &free_sgl_list);
6672
895427bd 6673 /* push sgls posted to the available list */
8a9d2e80 6674 if (!list_empty(&post_sgl_list)) {
38c20673 6675 spin_lock_irq(&phba->hbalock);
895427bd
JS
6676 spin_lock(&phba->sli4_hba.sgl_list_lock);
6677 list_splice_init(&post_sgl_list, sgl_list);
6678 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6679 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6680 } else {
6681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6682 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6683 return -EIO;
6684 }
895427bd
JS
6685
6686 /* return the number of XRIs actually posted */
6687 return total_cnt;
8a9d2e80
JS
6688}
6689
61bda8f7
JS
6690void
6691lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6692{
6693 uint32_t len;
6694
6695 len = sizeof(struct lpfc_mbx_set_host_data) -
6696 sizeof(struct lpfc_sli4_cfg_mhdr);
6697 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6698 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6699 LPFC_SLI4_MBX_EMBED);
6700
6701 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6702 mbox->u.mqe.un.set_host_data.param_len =
6703 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6704 snprintf(mbox->u.mqe.un.set_host_data.data,
6705 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6706 "Linux %s v"LPFC_DRIVER_VERSION,
6707 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6708}
6709
a8cf5dfe 6710int
6c621a22 6711lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 6712 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
6713{
6714 int rc, i;
6715 struct lpfc_rqe hrqe;
6716 struct lpfc_rqe drqe;
6717 struct lpfc_rqb *rqbp;
411de511 6718 unsigned long flags;
6c621a22
JS
6719 struct rqb_dmabuf *rqb_buffer;
6720 LIST_HEAD(rqb_buf_list);
6721
411de511 6722 spin_lock_irqsave(&phba->hbalock, flags);
6c621a22
JS
6723 rqbp = hrq->rqbp;
6724 for (i = 0; i < count; i++) {
6725 /* IF RQ is already full, don't bother */
6726 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6727 break;
6728 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6729 if (!rqb_buffer)
6730 break;
6731 rqb_buffer->hrq = hrq;
6732 rqb_buffer->drq = drq;
a8cf5dfe 6733 rqb_buffer->idx = idx;
6c621a22
JS
6734 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6735 }
6736 while (!list_empty(&rqb_buf_list)) {
6737 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6738 hbuf.list);
6739
6740 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6741 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6742 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6743 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6744 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6745 if (rc < 0) {
411de511
JS
6746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6747 "6421 Cannot post to HRQ %d: %x %x %x "
6748 "DRQ %x %x\n",
6749 hrq->queue_id,
6750 hrq->host_index,
6751 hrq->hba_index,
6752 hrq->entry_count,
6753 drq->host_index,
6754 drq->hba_index);
6c621a22
JS
6755 rqbp->rqb_free_buffer(phba, rqb_buffer);
6756 } else {
6757 list_add_tail(&rqb_buffer->hbuf.list,
6758 &rqbp->rqb_buffer_list);
6759 rqbp->buffer_count++;
6760 }
6761 }
411de511 6762 spin_unlock_irqrestore(&phba->hbalock, flags);
6c621a22
JS
6763 return 1;
6764}
6765
da0436e9 6766/**
183b8021 6767 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
6768 * @phba: Pointer to HBA context object.
6769 *
183b8021
MY
6770 * This function is the main SLI4 device initialization PCI function. This
6771 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
6772 * HBA error attention handler code. Caller is not required to hold any
6773 * locks.
6774 **/
6775int
6776lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6777{
6c621a22 6778 int rc, i, cnt;
da0436e9
JS
6779 LPFC_MBOXQ_t *mboxq;
6780 struct lpfc_mqe *mqe;
6781 uint8_t *vpd;
6782 uint32_t vpd_size;
6783 uint32_t ftr_rsp = 0;
6784 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6785 struct lpfc_vport *vport = phba->pport;
6786 struct lpfc_dmabuf *mp;
2d7dbc4c 6787 struct lpfc_rqb *rqbp;
da0436e9
JS
6788
6789 /* Perform a PCI function reset to start from clean */
6790 rc = lpfc_pci_function_reset(phba);
6791 if (unlikely(rc))
6792 return -ENODEV;
6793
6794 /* Check the HBA Host Status Register for readyness */
6795 rc = lpfc_sli4_post_status_check(phba);
6796 if (unlikely(rc))
6797 return -ENODEV;
6798 else {
6799 spin_lock_irq(&phba->hbalock);
6800 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6801 spin_unlock_irq(&phba->hbalock);
6802 }
6803
6804 /*
6805 * Allocate a single mailbox container for initializing the
6806 * port.
6807 */
6808 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6809 if (!mboxq)
6810 return -ENOMEM;
6811
da0436e9 6812 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6813 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6814 vpd = kzalloc(vpd_size, GFP_KERNEL);
6815 if (!vpd) {
6816 rc = -ENOMEM;
6817 goto out_free_mbox;
6818 }
6819
6820 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6821 if (unlikely(rc)) {
6822 kfree(vpd);
6823 goto out_free_mbox;
6824 }
572709e2 6825
da0436e9 6826 mqe = &mboxq->u.mqe;
f1126688 6827 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6828 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6829 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6830 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6831 } else {
76a95d75 6832 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6833 }
45ed1190
JS
6834
6835 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6836 LPFC_DCBX_CEE_MODE)
6837 phba->hba_flag |= HBA_FIP_SUPPORT;
6838 else
6839 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6840
4f2e66c6
JS
6841 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6842
c31098ce 6843 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6844 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6845 "0376 READ_REV Error. SLI Level %d "
6846 "FCoE enabled %d\n",
76a95d75 6847 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6848 rc = -EIO;
76a95d75
JS
6849 kfree(vpd);
6850 goto out_free_mbox;
da0436e9 6851 }
cd1c8301 6852
ff78d8f9
JS
6853 /*
6854 * Continue initialization with default values even if driver failed
6855 * to read FCoE param config regions, only read parameters if the
6856 * board is FCoE
6857 */
6858 if (phba->hba_flag & HBA_FCOE_MODE &&
6859 lpfc_sli4_read_fcoe_params(phba))
6860 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6861 "2570 Failed to read FCoE parameters\n");
6862
cd1c8301
JS
6863 /*
6864 * Retrieve sli4 device physical port name, failure of doing it
6865 * is considered as non-fatal.
6866 */
6867 rc = lpfc_sli4_retrieve_pport_name(phba);
6868 if (!rc)
6869 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6870 "3080 Successful retrieving SLI4 device "
6871 "physical port name: %s.\n", phba->Port);
6872
da0436e9
JS
6873 /*
6874 * Evaluate the read rev and vpd data. Populate the driver
6875 * state with the results. If this routine fails, the failure
6876 * is not fatal as the driver will use generic values.
6877 */
6878 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6879 if (unlikely(!rc)) {
6880 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6881 "0377 Error %d parsing vpd. "
6882 "Using defaults.\n", rc);
6883 rc = 0;
6884 }
76a95d75 6885 kfree(vpd);
da0436e9 6886
f1126688
JS
6887 /* Save information as VPD data */
6888 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6889 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4e565cf0
JS
6890
6891 /*
6892 * This is because first G7 ASIC doesn't support the standard
6893 * 0x5a NVME cmd descriptor type/subtype
6894 */
6895 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6896 LPFC_SLI_INTF_IF_TYPE_6) &&
6897 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6898 (phba->vpd.rev.smRev == 0) &&
6899 (phba->cfg_nvme_embed_cmd == 1))
6900 phba->cfg_nvme_embed_cmd = 0;
6901
f1126688
JS
6902 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6903 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6904 &mqe->un.read_rev);
6905 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6906 &mqe->un.read_rev);
6907 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6908 &mqe->un.read_rev);
6909 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6910 &mqe->un.read_rev);
6911 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6912 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6913 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6914 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6915 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6916 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6917 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6918 "(%d):0380 READ_REV Status x%x "
6919 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6920 mboxq->vport ? mboxq->vport->vpi : 0,
6921 bf_get(lpfc_mqe_status, mqe),
6922 phba->vpd.rev.opFwName,
6923 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6924 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6925
572709e2
JS
6926 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6927 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6928 if (phba->pport->cfg_lun_queue_depth > rc) {
6929 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6930 "3362 LUN queue depth changed from %d to %d\n",
6931 phba->pport->cfg_lun_queue_depth, rc);
6932 phba->pport->cfg_lun_queue_depth = rc;
6933 }
6934
65791f1f 6935 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6936 LPFC_SLI_INTF_IF_TYPE_0) {
6937 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6938 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6939 if (rc == MBX_SUCCESS) {
6940 phba->hba_flag |= HBA_RECOVERABLE_UE;
6941 /* Set 1Sec interval to detect UE */
6942 phba->eratt_poll_interval = 1;
6943 phba->sli4_hba.ue_to_sr = bf_get(
6944 lpfc_mbx_set_feature_UESR,
6945 &mboxq->u.mqe.un.set_feature);
6946 phba->sli4_hba.ue_to_rp = bf_get(
6947 lpfc_mbx_set_feature_UERP,
6948 &mboxq->u.mqe.un.set_feature);
6949 }
6950 }
6951
6952 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6953 /* Enable MDS Diagnostics only if the SLI Port supports it */
6954 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6955 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6956 if (rc != MBX_SUCCESS)
6957 phba->mds_diags_support = 0;
6958 }
572709e2 6959
da0436e9
JS
6960 /*
6961 * Discover the port's supported feature set and match it against the
6962 * hosts requests.
6963 */
6964 lpfc_request_features(phba, mboxq);
6965 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6966 if (unlikely(rc)) {
6967 rc = -EIO;
76a95d75 6968 goto out_free_mbox;
da0436e9
JS
6969 }
6970
6971 /*
6972 * The port must support FCP initiator mode as this is the
6973 * only mode running in the host.
6974 */
6975 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6976 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6977 "0378 No support for fcpi mode.\n");
6978 ftr_rsp++;
6979 }
0bc2b7c5
JS
6980
6981 /* Performance Hints are ONLY for FCoE */
6982 if (phba->hba_flag & HBA_FCOE_MODE) {
6983 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6984 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6985 else
6986 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6987 }
6988
da0436e9
JS
6989 /*
6990 * If the port cannot support the host's requested features
6991 * then turn off the global config parameters to disable the
6992 * feature in the driver. This is not a fatal error.
6993 */
bf08611b
JS
6994 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6995 if (phba->cfg_enable_bg) {
6996 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6997 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6998 else
6999 ftr_rsp++;
7000 }
da0436e9
JS
7001
7002 if (phba->max_vpi && phba->cfg_enable_npiv &&
7003 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7004 ftr_rsp++;
7005
7006 if (ftr_rsp) {
7007 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7008 "0379 Feature Mismatch Data: x%08x %08x "
7009 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7010 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7011 phba->cfg_enable_npiv, phba->max_vpi);
7012 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7013 phba->cfg_enable_bg = 0;
7014 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7015 phba->cfg_enable_npiv = 0;
7016 }
7017
7018 /* These SLI3 features are assumed in SLI4 */
7019 spin_lock_irq(&phba->hbalock);
7020 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7021 spin_unlock_irq(&phba->hbalock);
7022
6d368e53
JS
7023 /*
7024 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7025 * calls depends on these resources to complete port setup.
7026 */
7027 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7028 if (rc) {
7029 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7030 "2920 Failed to alloc Resource IDs "
7031 "rc = x%x\n", rc);
7032 goto out_free_mbox;
7033 }
7034
61bda8f7
JS
7035 lpfc_set_host_data(phba, mboxq);
7036
7037 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7038 if (rc) {
7039 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7040 "2134 Failed to set host os driver version %x",
7041 rc);
7042 }
7043
da0436e9 7044 /* Read the port's service parameters. */
9f1177a3
JS
7045 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7046 if (rc) {
7047 phba->link_state = LPFC_HBA_ERROR;
7048 rc = -ENOMEM;
76a95d75 7049 goto out_free_mbox;
9f1177a3
JS
7050 }
7051
da0436e9
JS
7052 mboxq->vport = vport;
7053 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7054 mp = (struct lpfc_dmabuf *) mboxq->context1;
7055 if (rc == MBX_SUCCESS) {
7056 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7057 rc = 0;
7058 }
7059
7060 /*
7061 * This memory was allocated by the lpfc_read_sparam routine. Release
7062 * it to the mbuf pool.
7063 */
7064 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7065 kfree(mp);
7066 mboxq->context1 = NULL;
7067 if (unlikely(rc)) {
7068 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7069 "0382 READ_SPARAM command failed "
7070 "status %d, mbxStatus x%x\n",
7071 rc, bf_get(lpfc_mqe_status, mqe));
7072 phba->link_state = LPFC_HBA_ERROR;
7073 rc = -EIO;
76a95d75 7074 goto out_free_mbox;
da0436e9
JS
7075 }
7076
0558056c 7077 lpfc_update_vport_wwn(vport);
da0436e9
JS
7078
7079 /* Update the fc_host data structures with new wwn. */
7080 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7081 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7082
895427bd
JS
7083 /* Create all the SLI4 queues */
7084 rc = lpfc_sli4_queue_create(phba);
7085 if (rc) {
7086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7087 "3089 Failed to allocate queues\n");
7088 rc = -ENODEV;
7089 goto out_free_mbox;
7090 }
7091 /* Set up all the queues to the device */
7092 rc = lpfc_sli4_queue_setup(phba);
7093 if (unlikely(rc)) {
7094 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7095 "0381 Error %d during queue setup.\n ", rc);
7096 goto out_stop_timers;
7097 }
7098 /* Initialize the driver internal SLI layer lists. */
7099 lpfc_sli4_setup(phba);
7100 lpfc_sli4_queue_init(phba);
7101
7102 /* update host els xri-sgl sizes and mappings */
7103 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
7104 if (unlikely(rc)) {
7105 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7106 "1400 Failed to update xri-sgl size and "
7107 "mapping: %d\n", rc);
895427bd 7108 goto out_destroy_queue;
da0436e9
JS
7109 }
7110
8a9d2e80 7111 /* register the els sgl pool to the port */
895427bd
JS
7112 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7113 phba->sli4_hba.els_xri_cnt);
7114 if (unlikely(rc < 0)) {
8a9d2e80
JS
7115 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7116 "0582 Error %d during els sgl post "
7117 "operation\n", rc);
7118 rc = -ENODEV;
895427bd 7119 goto out_destroy_queue;
8a9d2e80 7120 }
895427bd 7121 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 7122
f358dd0c
JS
7123 if (phba->nvmet_support) {
7124 /* update host nvmet xri-sgl sizes and mappings */
7125 rc = lpfc_sli4_nvmet_sgl_update(phba);
7126 if (unlikely(rc)) {
7127 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7128 "6308 Failed to update nvmet-sgl size "
7129 "and mapping: %d\n", rc);
7130 goto out_destroy_queue;
7131 }
7132
7133 /* register the nvmet sgl pool to the port */
7134 rc = lpfc_sli4_repost_sgl_list(
7135 phba,
7136 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7137 phba->sli4_hba.nvmet_xri_cnt);
7138 if (unlikely(rc < 0)) {
7139 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7140 "3117 Error %d during nvmet "
7141 "sgl post\n", rc);
7142 rc = -ENODEV;
7143 goto out_destroy_queue;
7144 }
7145 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
7146
7147 cnt = phba->cfg_iocb_cnt * 1024;
7148 /* We need 1 iocbq for every SGL, for IO processing */
7149 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 7150 } else {
895427bd
JS
7151 /* update host scsi xri-sgl sizes and mappings */
7152 rc = lpfc_sli4_scsi_sgl_update(phba);
7153 if (unlikely(rc)) {
7154 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7155 "6309 Failed to update scsi-sgl size "
7156 "and mapping: %d\n", rc);
7157 goto out_destroy_queue;
7158 }
7159
7160 /* update host nvme xri-sgl sizes and mappings */
7161 rc = lpfc_sli4_nvme_sgl_update(phba);
7162 if (unlikely(rc)) {
7163 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7164 "6082 Failed to update nvme-sgl size "
7165 "and mapping: %d\n", rc);
7166 goto out_destroy_queue;
7167 }
6c621a22
JS
7168
7169 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
7170 }
7171
7172 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
7173 /* Initialize and populate the iocb list per host */
7174 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 7175 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
7176 phba->cfg_iocb_cnt, cnt);
7177 rc = lpfc_init_iocb_list(phba, cnt);
7178 if (rc) {
7179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 7180 "1413 Failed to init iocb list.\n");
6c621a22
JS
7181 goto out_destroy_queue;
7182 }
895427bd
JS
7183 }
7184
11e644e2
JS
7185 if (phba->nvmet_support)
7186 lpfc_nvmet_create_targetport(phba);
7187
2d7dbc4c 7188 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
7189 /* Post initial buffers to all RQs created */
7190 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7191 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7192 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7193 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7194 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 7195 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
7196 rqbp->buffer_count = 0;
7197
2d7dbc4c
JS
7198 lpfc_post_rq_buffer(
7199 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7200 phba->sli4_hba.nvmet_mrq_data[i],
a8cf5dfe 7201 LPFC_NVMET_RQE_DEF_COUNT, i);
2d7dbc4c
JS
7202 }
7203 }
7204
895427bd
JS
7205 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7206 /* register the allocated scsi sgl pool to the port */
7207 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7208 if (unlikely(rc)) {
7209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7210 "0383 Error %d during scsi sgl post "
7211 "operation\n", rc);
7212 /* Some Scsi buffers were moved to abort scsi list */
7213 /* A pci function reset will repost them */
7214 rc = -ENODEV;
7215 goto out_destroy_queue;
7216 }
da0436e9
JS
7217 }
7218
01649561
JS
7219 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7220 (phba->nvmet_support == 0)) {
7221
7222 /* register the allocated nvme sgl pool to the port */
7223 rc = lpfc_repost_nvme_sgl_list(phba);
7224 if (unlikely(rc)) {
7225 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7226 "6116 Error %d during nvme sgl post "
7227 "operation\n", rc);
7228 /* Some NVME buffers were moved to abort nvme list */
7229 /* A pci function reset will repost them */
7230 rc = -ENODEV;
7231 goto out_destroy_queue;
7232 }
da0436e9
JS
7233 }
7234
7235 /* Post the rpi header region to the device. */
7236 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7237 if (unlikely(rc)) {
7238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7239 "0393 Error %d during rpi post operation\n",
7240 rc);
7241 rc = -ENODEV;
895427bd 7242 goto out_destroy_queue;
da0436e9 7243 }
97f2ecf1 7244 lpfc_sli4_node_prep(phba);
da0436e9 7245
895427bd 7246 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7247 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7248 /*
7249 * The FC Port needs to register FCFI (index 0)
7250 */
7251 lpfc_reg_fcfi(phba, mboxq);
7252 mboxq->vport = phba->pport;
7253 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7254 if (rc != MBX_SUCCESS)
7255 goto out_unset_queue;
7256 rc = 0;
7257 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7258 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7259 } else {
7260 /* We are a NVME Target mode with MRQ > 1 */
7261
7262 /* First register the FCFI */
7263 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7264 mboxq->vport = phba->pport;
7265 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7266 if (rc != MBX_SUCCESS)
7267 goto out_unset_queue;
7268 rc = 0;
7269 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7270 &mboxq->u.mqe.un.reg_fcfi_mrq);
7271
7272 /* Next register the MRQs */
7273 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7274 mboxq->vport = phba->pport;
7275 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7276 if (rc != MBX_SUCCESS)
7277 goto out_unset_queue;
7278 rc = 0;
895427bd
JS
7279 }
7280 /* Check if the port is configured to be disabled */
7281 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7282 }
7283
7284 /* Arm the CQs and then EQs on device */
7285 lpfc_sli4_arm_cqeq_intr(phba);
7286
7287 /* Indicate device interrupt mode */
7288 phba->sli4_hba.intr_enable = 1;
7289
7290 /* Allow asynchronous mailbox command to go through */
7291 spin_lock_irq(&phba->hbalock);
7292 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7293 spin_unlock_irq(&phba->hbalock);
7294
7295 /* Post receive buffers to the device */
7296 lpfc_sli4_rb_setup(phba);
7297
fc2b989b
JS
7298 /* Reset HBA FCF states after HBA reset */
7299 phba->fcf.fcf_flag = 0;
7300 phba->fcf.current_rec.flag = 0;
7301
da0436e9 7302 /* Start the ELS watchdog timer */
8fa38513 7303 mod_timer(&vport->els_tmofunc,
256ec0d0 7304 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7305
7306 /* Start heart beat timer */
7307 mod_timer(&phba->hb_tmofunc,
256ec0d0 7308 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7309 phba->hb_outstanding = 0;
7310 phba->last_completion_time = jiffies;
7311
7312 /* Start error attention (ERATT) polling timer */
256ec0d0 7313 mod_timer(&phba->eratt_poll,
65791f1f 7314 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7315
75baf696
JS
7316 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7317 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7318 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7319 if (!rc) {
7320 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7321 "2829 This device supports "
7322 "Advanced Error Reporting (AER)\n");
7323 spin_lock_irq(&phba->hbalock);
7324 phba->hba_flag |= HBA_AER_ENABLED;
7325 spin_unlock_irq(&phba->hbalock);
7326 } else {
7327 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7328 "2830 This device does not support "
7329 "Advanced Error Reporting (AER)\n");
7330 phba->cfg_aer_support = 0;
7331 }
0a96e975 7332 rc = 0;
75baf696
JS
7333 }
7334
da0436e9
JS
7335 /*
7336 * The port is ready, set the host's link state to LINK_DOWN
7337 * in preparation for link interrupts.
7338 */
da0436e9
JS
7339 spin_lock_irq(&phba->hbalock);
7340 phba->link_state = LPFC_LINK_DOWN;
7341 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7342 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7343 (phba->hba_flag & LINK_DISABLED)) {
7344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7345 "3103 Adapter Link is disabled.\n");
7346 lpfc_down_link(phba, mboxq);
7347 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7348 if (rc != MBX_SUCCESS) {
7349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7350 "3104 Adapter failed to issue "
7351 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7352 goto out_unset_queue;
7353 }
7354 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7355 /* don't perform init_link on SLI4 FC port loopback test */
7356 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7357 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7358 if (rc)
7359 goto out_unset_queue;
7360 }
5350d872
JS
7361 }
7362 mempool_free(mboxq, phba->mbox_mem_pool);
7363 return rc;
76a95d75 7364out_unset_queue:
da0436e9 7365 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7366 lpfc_sli4_queue_unset(phba);
7367out_destroy_queue:
6c621a22 7368 lpfc_free_iocb_list(phba);
5350d872 7369 lpfc_sli4_queue_destroy(phba);
da0436e9 7370out_stop_timers:
5350d872 7371 lpfc_stop_hba_timers(phba);
da0436e9
JS
7372out_free_mbox:
7373 mempool_free(mboxq, phba->mbox_mem_pool);
7374 return rc;
7375}
7376
7377/**
7378 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7379 * @ptr: context object - pointer to hba structure.
7380 *
7381 * This is the callback function for mailbox timer. The mailbox
7382 * timer is armed when a new mailbox command is issued and the timer
7383 * is deleted when the mailbox complete. The function is called by
7384 * the kernel timer code when a mailbox does not complete within
7385 * expected time. This function wakes up the worker thread to
7386 * process the mailbox timeout and returns. All the processing is
7387 * done by the worker thread function lpfc_mbox_timeout_handler.
7388 **/
7389void
f22eb4d3 7390lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7391{
f22eb4d3 7392 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7393 unsigned long iflag;
7394 uint32_t tmo_posted;
7395
7396 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7397 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7398 if (!tmo_posted)
7399 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7400 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7401
7402 if (!tmo_posted)
7403 lpfc_worker_wake_up(phba);
7404 return;
7405}
7406
e8d3c3b1
JS
7407/**
7408 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7409 * are pending
7410 * @phba: Pointer to HBA context object.
7411 *
7412 * This function checks if any mailbox completions are present on the mailbox
7413 * completion queue.
7414 **/
3bb11fc5 7415static bool
e8d3c3b1
JS
7416lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7417{
7418
7419 uint32_t idx;
7420 struct lpfc_queue *mcq;
7421 struct lpfc_mcqe *mcqe;
7422 bool pending_completions = false;
7365f6fd 7423 uint8_t qe_valid;
e8d3c3b1
JS
7424
7425 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7426 return false;
7427
7428 /* Check for completions on mailbox completion queue */
7429
7430 mcq = phba->sli4_hba.mbx_cq;
7431 idx = mcq->hba_index;
7365f6fd
JS
7432 qe_valid = mcq->qe_valid;
7433 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
e8d3c3b1
JS
7434 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7435 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7436 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7437 pending_completions = true;
7438 break;
7439 }
7440 idx = (idx + 1) % mcq->entry_count;
7441 if (mcq->hba_index == idx)
7442 break;
7365f6fd
JS
7443
7444 /* if the index wrapped around, toggle the valid bit */
7445 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7446 qe_valid = (qe_valid) ? 0 : 1;
e8d3c3b1
JS
7447 }
7448 return pending_completions;
7449
7450}
7451
7452/**
7453 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7454 * that were missed.
7455 * @phba: Pointer to HBA context object.
7456 *
7457 * For sli4, it is possible to miss an interrupt. As such mbox completions
7458 * maybe missed causing erroneous mailbox timeouts to occur. This function
7459 * checks to see if mbox completions are on the mailbox completion queue
7460 * and will process all the completions associated with the eq for the
7461 * mailbox completion queue.
7462 **/
7463bool
7464lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7465{
b71413dd 7466 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
e8d3c3b1
JS
7467 uint32_t eqidx;
7468 struct lpfc_queue *fpeq = NULL;
7469 struct lpfc_eqe *eqe;
7470 bool mbox_pending;
7471
7472 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7473 return false;
7474
7475 /* Find the eq associated with the mcq */
7476
b71413dd 7477 if (sli4_hba->hba_eq)
895427bd 7478 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
b71413dd
JS
7479 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7480 sli4_hba->mbx_cq->assoc_qid) {
7481 fpeq = sli4_hba->hba_eq[eqidx];
e8d3c3b1
JS
7482 break;
7483 }
7484 if (!fpeq)
7485 return false;
7486
7487 /* Turn off interrupts from this EQ */
7488
b71413dd 7489 sli4_hba->sli4_eq_clr_intr(fpeq);
e8d3c3b1
JS
7490
7491 /* Check to see if a mbox completion is pending */
7492
7493 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7494
7495 /*
7496 * If a mbox completion is pending, process all the events on EQ
7497 * associated with the mbox completion queue (this could include
7498 * mailbox commands, async events, els commands, receive queue data
7499 * and fcp commands)
7500 */
7501
7502 if (mbox_pending)
7503 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7504 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7505 fpeq->EQ_processed++;
7506 }
7507
7508 /* Always clear and re-arm the EQ */
7509
b71413dd 7510 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
e8d3c3b1
JS
7511
7512 return mbox_pending;
7513
7514}
da0436e9
JS
7515
7516/**
7517 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7518 * @phba: Pointer to HBA context object.
7519 *
7520 * This function is called from worker thread when a mailbox command times out.
7521 * The caller is not required to hold any locks. This function will reset the
7522 * HBA and recover all the pending commands.
7523 **/
7524void
7525lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7526{
7527 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7528 MAILBOX_t *mb = NULL;
7529
da0436e9 7530 struct lpfc_sli *psli = &phba->sli;
da0436e9 7531
e8d3c3b1
JS
7532 /* If the mailbox completed, process the completion and return */
7533 if (lpfc_sli4_process_missed_mbox_completions(phba))
7534 return;
7535
eb016566
JS
7536 if (pmbox != NULL)
7537 mb = &pmbox->u.mb;
da0436e9
JS
7538 /* Check the pmbox pointer first. There is a race condition
7539 * between the mbox timeout handler getting executed in the
7540 * worklist and the mailbox actually completing. When this
7541 * race condition occurs, the mbox_active will be NULL.
7542 */
7543 spin_lock_irq(&phba->hbalock);
7544 if (pmbox == NULL) {
7545 lpfc_printf_log(phba, KERN_WARNING,
7546 LOG_MBOX | LOG_SLI,
7547 "0353 Active Mailbox cleared - mailbox timeout "
7548 "exiting\n");
7549 spin_unlock_irq(&phba->hbalock);
7550 return;
7551 }
7552
7553 /* Mbox cmd <mbxCommand> timeout */
7554 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7555 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7556 mb->mbxCommand,
7557 phba->pport->port_state,
7558 phba->sli.sli_flag,
7559 phba->sli.mbox_active);
7560 spin_unlock_irq(&phba->hbalock);
7561
7562 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7563 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7564 * it to fail all outstanding SCSI IO.
da0436e9
JS
7565 */
7566 spin_lock_irq(&phba->pport->work_port_lock);
7567 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7568 spin_unlock_irq(&phba->pport->work_port_lock);
7569 spin_lock_irq(&phba->hbalock);
7570 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7571 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7572 spin_unlock_irq(&phba->hbalock);
7573
db55fba8 7574 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7575
7576 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7577 "0345 Resetting board due to mailbox timeout\n");
7578
7579 /* Reset the HBA device */
7580 lpfc_reset_hba(phba);
7581}
7582
7583/**
7584 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7585 * @phba: Pointer to HBA context object.
7586 * @pmbox: Pointer to mailbox object.
7587 * @flag: Flag indicating how the mailbox need to be processed.
7588 *
7589 * This function is called by discovery code and HBA management code
7590 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7591 * function gets the hbalock to protect the data structures.
7592 * The mailbox command can be submitted in polling mode, in which case
7593 * this function will wait in a polling loop for the completion of the
7594 * mailbox.
7595 * If the mailbox is submitted in no_wait mode (not polling) the
7596 * function will submit the command and returns immediately without waiting
7597 * for the mailbox completion. The no_wait is supported only when HBA
7598 * is in SLI2/SLI3 mode - interrupts are enabled.
7599 * The SLI interface allows only one mailbox pending at a time. If the
7600 * mailbox is issued in polling mode and there is already a mailbox
7601 * pending, then the function will return an error. If the mailbox is issued
7602 * in NO_WAIT mode and there is a mailbox pending already, the function
7603 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7604 * The sli layer owns the mailbox object until the completion of mailbox
7605 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7606 * return codes the caller owns the mailbox command after the return of
7607 * the function.
e59058c4 7608 **/
3772a991
JS
7609static int
7610lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7611 uint32_t flag)
dea3101e 7612{
bf07bdea 7613 MAILBOX_t *mbx;
2e0fef85 7614 struct lpfc_sli *psli = &phba->sli;
dea3101e 7615 uint32_t status, evtctr;
9940b97b 7616 uint32_t ha_copy, hc_copy;
dea3101e 7617 int i;
09372820 7618 unsigned long timeout;
dea3101e 7619 unsigned long drvr_flag = 0;
34b02dcd 7620 uint32_t word0, ldata;
dea3101e 7621 void __iomem *to_slim;
58da1ffb
JS
7622 int processing_queue = 0;
7623
7624 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7625 if (!pmbox) {
8568a4d2 7626 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7627 /* processing mbox queue from intr_handler */
3772a991
JS
7628 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7629 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7630 return MBX_SUCCESS;
7631 }
58da1ffb 7632 processing_queue = 1;
58da1ffb
JS
7633 pmbox = lpfc_mbox_get(phba);
7634 if (!pmbox) {
7635 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7636 return MBX_SUCCESS;
7637 }
7638 }
dea3101e 7639
ed957684 7640 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7641 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7642 if(!pmbox->vport) {
58da1ffb 7643 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7644 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7645 LOG_MBOX | LOG_VPORT,
e8b62011 7646 "1806 Mbox x%x failed. No vport\n",
3772a991 7647 pmbox->u.mb.mbxCommand);
ed957684 7648 dump_stack();
58da1ffb 7649 goto out_not_finished;
ed957684
JS
7650 }
7651 }
7652
8d63f375 7653 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7654 if (unlikely(pci_channel_offline(phba->pcidev))) {
7655 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7656 goto out_not_finished;
7657 }
8d63f375 7658
a257bf90
JS
7659 /* If HBA has a deferred error attention, fail the iocb. */
7660 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7661 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7662 goto out_not_finished;
7663 }
7664
dea3101e 7665 psli = &phba->sli;
92d7f7b0 7666
bf07bdea 7667 mbx = &pmbox->u.mb;
dea3101e
JB
7668 status = MBX_SUCCESS;
7669
2e0fef85
JS
7670 if (phba->link_state == LPFC_HBA_ERROR) {
7671 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7672
7673 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7675 "(%d):0311 Mailbox command x%x cannot "
7676 "issue Data: x%x x%x\n",
7677 pmbox->vport ? pmbox->vport->vpi : 0,
7678 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7679 goto out_not_finished;
41415862
JW
7680 }
7681
bf07bdea 7682 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7683 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7684 !(hc_copy & HC_MBINT_ENA)) {
7685 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7686 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7687 "(%d):2528 Mailbox command x%x cannot "
7688 "issue Data: x%x x%x\n",
7689 pmbox->vport ? pmbox->vport->vpi : 0,
7690 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7691 goto out_not_finished;
7692 }
9290831f
JS
7693 }
7694
dea3101e
JB
7695 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7696 /* Polling for a mbox command when another one is already active
7697 * is not allowed in SLI. Also, the driver must have established
7698 * SLI2 mode to queue and process multiple mbox commands.
7699 */
7700
7701 if (flag & MBX_POLL) {
2e0fef85 7702 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7703
7704 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7705 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7706 "(%d):2529 Mailbox command x%x "
7707 "cannot issue Data: x%x x%x\n",
7708 pmbox->vport ? pmbox->vport->vpi : 0,
7709 pmbox->u.mb.mbxCommand,
7710 psli->sli_flag, flag);
58da1ffb 7711 goto out_not_finished;
dea3101e
JB
7712 }
7713
3772a991 7714 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7715 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7716 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7718 "(%d):2530 Mailbox command x%x "
7719 "cannot issue Data: x%x x%x\n",
7720 pmbox->vport ? pmbox->vport->vpi : 0,
7721 pmbox->u.mb.mbxCommand,
7722 psli->sli_flag, flag);
58da1ffb 7723 goto out_not_finished;
dea3101e
JB
7724 }
7725
dea3101e
JB
7726 /* Another mailbox command is still being processed, queue this
7727 * command to be processed later.
7728 */
7729 lpfc_mbox_put(phba, pmbox);
7730
7731 /* Mbox cmd issue - BUSY */
ed957684 7732 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7733 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7734 "x%x x%x x%x x%x\n",
92d7f7b0 7735 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
7736 mbx->mbxCommand,
7737 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7738 psli->sli_flag, flag);
dea3101e
JB
7739
7740 psli->slistat.mbox_busy++;
2e0fef85 7741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7742
858c9f6c
JS
7743 if (pmbox->vport) {
7744 lpfc_debugfs_disc_trc(pmbox->vport,
7745 LPFC_DISC_TRC_MBOX_VPORT,
7746 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7747 (uint32_t)mbx->mbxCommand,
7748 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7749 }
7750 else {
7751 lpfc_debugfs_disc_trc(phba->pport,
7752 LPFC_DISC_TRC_MBOX,
7753 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7754 (uint32_t)mbx->mbxCommand,
7755 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7756 }
7757
2e0fef85 7758 return MBX_BUSY;
dea3101e
JB
7759 }
7760
dea3101e
JB
7761 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7762
7763 /* If we are not polling, we MUST be in SLI2 mode */
7764 if (flag != MBX_POLL) {
3772a991 7765 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7766 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7767 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7768 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7769 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7770 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7771 "(%d):2531 Mailbox command x%x "
7772 "cannot issue Data: x%x x%x\n",
7773 pmbox->vport ? pmbox->vport->vpi : 0,
7774 pmbox->u.mb.mbxCommand,
7775 psli->sli_flag, flag);
58da1ffb 7776 goto out_not_finished;
dea3101e
JB
7777 }
7778 /* timeout active mbox command */
256ec0d0
JS
7779 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7780 1000);
7781 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7782 }
7783
7784 /* Mailbox cmd <cmd> issue */
ed957684 7785 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7786 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7787 "x%x\n",
e8b62011 7788 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
7789 mbx->mbxCommand,
7790 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7791 psli->sli_flag, flag);
dea3101e 7792
bf07bdea 7793 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7794 if (pmbox->vport) {
7795 lpfc_debugfs_disc_trc(pmbox->vport,
7796 LPFC_DISC_TRC_MBOX_VPORT,
7797 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7798 (uint32_t)mbx->mbxCommand,
7799 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7800 }
7801 else {
7802 lpfc_debugfs_disc_trc(phba->pport,
7803 LPFC_DISC_TRC_MBOX,
7804 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7805 (uint32_t)mbx->mbxCommand,
7806 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7807 }
7808 }
7809
dea3101e
JB
7810 psli->slistat.mbox_cmd++;
7811 evtctr = psli->slistat.mbox_event;
7812
7813 /* next set own bit for the adapter and copy over command word */
bf07bdea 7814 mbx->mbxOwner = OWN_CHIP;
dea3101e 7815
3772a991 7816 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7817 /* Populate mbox extension offset word. */
7818 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7819 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7820 = (uint8_t *)phba->mbox_ext
7821 - (uint8_t *)phba->mbox;
7822 }
7823
7824 /* Copy the mailbox extension data */
7825 if (pmbox->in_ext_byte_len && pmbox->context2) {
7826 lpfc_sli_pcimem_bcopy(pmbox->context2,
7827 (uint8_t *)phba->mbox_ext,
7828 pmbox->in_ext_byte_len);
7829 }
7830 /* Copy command data to host SLIM area */
bf07bdea 7831 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7832 } else {
7a470277
JS
7833 /* Populate mbox extension offset word. */
7834 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7835 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7836 = MAILBOX_HBA_EXT_OFFSET;
7837
7838 /* Copy the mailbox extension data */
895427bd 7839 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7840 lpfc_memcpy_to_slim(phba->MBslimaddr +
7841 MAILBOX_HBA_EXT_OFFSET,
7842 pmbox->context2, pmbox->in_ext_byte_len);
7843
895427bd 7844 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7845 /* copy command data into host mbox for cmpl */
895427bd
JS
7846 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7847 MAILBOX_CMD_SIZE);
dea3101e
JB
7848
7849 /* First copy mbox command data to HBA SLIM, skip past first
7850 word */
7851 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7852 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7853 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7854
7855 /* Next copy over first word, with mbxOwner set */
bf07bdea 7856 ldata = *((uint32_t *)mbx);
dea3101e
JB
7857 to_slim = phba->MBslimaddr;
7858 writel(ldata, to_slim);
7859 readl(to_slim); /* flush */
7860
895427bd 7861 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7862 /* switch over to host mailbox */
3772a991 7863 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7864 }
7865
7866 wmb();
dea3101e
JB
7867
7868 switch (flag) {
7869 case MBX_NOWAIT:
09372820 7870 /* Set up reference to mailbox command */
dea3101e 7871 psli->mbox_active = pmbox;
09372820
JS
7872 /* Interrupt board to do it */
7873 writel(CA_MBATT, phba->CAregaddr);
7874 readl(phba->CAregaddr); /* flush */
7875 /* Don't wait for it to finish, just return */
dea3101e
JB
7876 break;
7877
7878 case MBX_POLL:
09372820 7879 /* Set up null reference to mailbox command */
dea3101e 7880 psli->mbox_active = NULL;
09372820
JS
7881 /* Interrupt board to do it */
7882 writel(CA_MBATT, phba->CAregaddr);
7883 readl(phba->CAregaddr); /* flush */
7884
3772a991 7885 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7886 /* First read mbox status word */
34b02dcd 7887 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7888 word0 = le32_to_cpu(word0);
7889 } else {
7890 /* First read mbox status word */
9940b97b
JS
7891 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7892 spin_unlock_irqrestore(&phba->hbalock,
7893 drvr_flag);
7894 goto out_not_finished;
7895 }
dea3101e
JB
7896 }
7897
7898 /* Read the HBA Host Attention Register */
9940b97b
JS
7899 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7900 spin_unlock_irqrestore(&phba->hbalock,
7901 drvr_flag);
7902 goto out_not_finished;
7903 }
a183a15f
JS
7904 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7905 1000) + jiffies;
09372820 7906 i = 0;
dea3101e 7907 /* Wait for command to complete */
41415862
JW
7908 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7909 (!(ha_copy & HA_MBATT) &&
2e0fef85 7910 (phba->link_state > LPFC_WARM_START))) {
09372820 7911 if (time_after(jiffies, timeout)) {
dea3101e 7912 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7913 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7914 drvr_flag);
58da1ffb 7915 goto out_not_finished;
dea3101e
JB
7916 }
7917
7918 /* Check if we took a mbox interrupt while we were
7919 polling */
7920 if (((word0 & OWN_CHIP) != OWN_CHIP)
7921 && (evtctr != psli->slistat.mbox_event))
7922 break;
7923
09372820
JS
7924 if (i++ > 10) {
7925 spin_unlock_irqrestore(&phba->hbalock,
7926 drvr_flag);
7927 msleep(1);
7928 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7929 }
dea3101e 7930
3772a991 7931 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7932 /* First copy command data */
34b02dcd 7933 word0 = *((uint32_t *)phba->mbox);
dea3101e 7934 word0 = le32_to_cpu(word0);
bf07bdea 7935 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7936 MAILBOX_t *slimmb;
34b02dcd 7937 uint32_t slimword0;
dea3101e
JB
7938 /* Check real SLIM for any errors */
7939 slimword0 = readl(phba->MBslimaddr);
7940 slimmb = (MAILBOX_t *) & slimword0;
7941 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7942 && slimmb->mbxStatus) {
7943 psli->sli_flag &=
3772a991 7944 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7945 word0 = slimword0;
7946 }
7947 }
7948 } else {
7949 /* First copy command data */
7950 word0 = readl(phba->MBslimaddr);
7951 }
7952 /* Read the HBA Host Attention Register */
9940b97b
JS
7953 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7954 spin_unlock_irqrestore(&phba->hbalock,
7955 drvr_flag);
7956 goto out_not_finished;
7957 }
dea3101e
JB
7958 }
7959
3772a991 7960 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7961 /* copy results back to user */
2ea259ee
JS
7962 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7963 MAILBOX_CMD_SIZE);
7a470277
JS
7964 /* Copy the mailbox extension data */
7965 if (pmbox->out_ext_byte_len && pmbox->context2) {
7966 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7967 pmbox->context2,
7968 pmbox->out_ext_byte_len);
7969 }
dea3101e
JB
7970 } else {
7971 /* First copy command data */
bf07bdea 7972 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 7973 MAILBOX_CMD_SIZE);
7a470277
JS
7974 /* Copy the mailbox extension data */
7975 if (pmbox->out_ext_byte_len && pmbox->context2) {
7976 lpfc_memcpy_from_slim(pmbox->context2,
7977 phba->MBslimaddr +
7978 MAILBOX_HBA_EXT_OFFSET,
7979 pmbox->out_ext_byte_len);
dea3101e
JB
7980 }
7981 }
7982
7983 writel(HA_MBATT, phba->HAregaddr);
7984 readl(phba->HAregaddr); /* flush */
7985
7986 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7987 status = mbx->mbxStatus;
dea3101e
JB
7988 }
7989
2e0fef85
JS
7990 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7991 return status;
58da1ffb
JS
7992
7993out_not_finished:
7994 if (processing_queue) {
da0436e9 7995 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7996 lpfc_mbox_cmpl_put(phba, pmbox);
7997 }
7998 return MBX_NOT_FINISHED;
dea3101e
JB
7999}
8000
f1126688
JS
8001/**
8002 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8003 * @phba: Pointer to HBA context object.
8004 *
8005 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8006 * the driver internal pending mailbox queue. It will then try to wait out the
8007 * possible outstanding mailbox command before return.
8008 *
8009 * Returns:
8010 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8011 * the outstanding mailbox command timed out.
8012 **/
8013static int
8014lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8015{
8016 struct lpfc_sli *psli = &phba->sli;
f1126688 8017 int rc = 0;
a183a15f 8018 unsigned long timeout = 0;
f1126688
JS
8019
8020 /* Mark the asynchronous mailbox command posting as blocked */
8021 spin_lock_irq(&phba->hbalock);
8022 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
8023 /* Determine how long we might wait for the active mailbox
8024 * command to be gracefully completed by firmware.
8025 */
a183a15f
JS
8026 if (phba->sli.mbox_active)
8027 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8028 phba->sli.mbox_active) *
8029 1000) + jiffies;
8030 spin_unlock_irq(&phba->hbalock);
8031
e8d3c3b1
JS
8032 /* Make sure the mailbox is really active */
8033 if (timeout)
8034 lpfc_sli4_process_missed_mbox_completions(phba);
8035
f1126688
JS
8036 /* Wait for the outstnading mailbox command to complete */
8037 while (phba->sli.mbox_active) {
8038 /* Check active mailbox complete status every 2ms */
8039 msleep(2);
8040 if (time_after(jiffies, timeout)) {
8041 /* Timeout, marked the outstanding cmd not complete */
8042 rc = 1;
8043 break;
8044 }
8045 }
8046
8047 /* Can not cleanly block async mailbox command, fails it */
8048 if (rc) {
8049 spin_lock_irq(&phba->hbalock);
8050 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8051 spin_unlock_irq(&phba->hbalock);
8052 }
8053 return rc;
8054}
8055
8056/**
8057 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8058 * @phba: Pointer to HBA context object.
8059 *
8060 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8061 * commands from the driver internal pending mailbox queue. It makes sure
8062 * that there is no outstanding mailbox command before resuming posting
8063 * asynchronous mailbox commands. If, for any reason, there is outstanding
8064 * mailbox command, it will try to wait it out before resuming asynchronous
8065 * mailbox command posting.
8066 **/
8067static void
8068lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8069{
8070 struct lpfc_sli *psli = &phba->sli;
8071
8072 spin_lock_irq(&phba->hbalock);
8073 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8074 /* Asynchronous mailbox posting is not blocked, do nothing */
8075 spin_unlock_irq(&phba->hbalock);
8076 return;
8077 }
8078
8079 /* Outstanding synchronous mailbox command is guaranteed to be done,
8080 * successful or timeout, after timing-out the outstanding mailbox
8081 * command shall always be removed, so just unblock posting async
8082 * mailbox command and resume
8083 */
8084 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8085 spin_unlock_irq(&phba->hbalock);
8086
8087 /* wake up worker thread to post asynchronlous mailbox command */
8088 lpfc_worker_wake_up(phba);
8089}
8090
2d843edc
JS
8091/**
8092 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8093 * @phba: Pointer to HBA context object.
8094 * @mboxq: Pointer to mailbox object.
8095 *
8096 * The function waits for the bootstrap mailbox register ready bit from
8097 * port for twice the regular mailbox command timeout value.
8098 *
8099 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8100 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8101 **/
8102static int
8103lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8104{
8105 uint32_t db_ready;
8106 unsigned long timeout;
8107 struct lpfc_register bmbx_reg;
8108
8109 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8110 * 1000) + jiffies;
8111
8112 do {
8113 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8114 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8115 if (!db_ready)
8116 msleep(2);
8117
8118 if (time_after(jiffies, timeout))
8119 return MBXERR_ERROR;
8120 } while (!db_ready);
8121
8122 return 0;
8123}
8124
da0436e9
JS
8125/**
8126 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8127 * @phba: Pointer to HBA context object.
8128 * @mboxq: Pointer to mailbox object.
8129 *
8130 * The function posts a mailbox to the port. The mailbox is expected
8131 * to be comletely filled in and ready for the port to operate on it.
8132 * This routine executes a synchronous completion operation on the
8133 * mailbox by polling for its completion.
8134 *
8135 * The caller must not be holding any locks when calling this routine.
8136 *
8137 * Returns:
8138 * MBX_SUCCESS - mailbox posted successfully
8139 * Any of the MBX error values.
8140 **/
8141static int
8142lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8143{
8144 int rc = MBX_SUCCESS;
8145 unsigned long iflag;
da0436e9
JS
8146 uint32_t mcqe_status;
8147 uint32_t mbx_cmnd;
da0436e9
JS
8148 struct lpfc_sli *psli = &phba->sli;
8149 struct lpfc_mqe *mb = &mboxq->u.mqe;
8150 struct lpfc_bmbx_create *mbox_rgn;
8151 struct dma_address *dma_address;
da0436e9
JS
8152
8153 /*
8154 * Only one mailbox can be active to the bootstrap mailbox region
8155 * at a time and there is no queueing provided.
8156 */
8157 spin_lock_irqsave(&phba->hbalock, iflag);
8158 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8159 spin_unlock_irqrestore(&phba->hbalock, iflag);
8160 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8161 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8162 "cannot issue Data: x%x x%x\n",
8163 mboxq->vport ? mboxq->vport->vpi : 0,
8164 mboxq->u.mb.mbxCommand,
a183a15f
JS
8165 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8166 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8167 psli->sli_flag, MBX_POLL);
8168 return MBXERR_ERROR;
8169 }
8170 /* The server grabs the token and owns it until release */
8171 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8172 phba->sli.mbox_active = mboxq;
8173 spin_unlock_irqrestore(&phba->hbalock, iflag);
8174
2d843edc
JS
8175 /* wait for bootstrap mbox register for readyness */
8176 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8177 if (rc)
8178 goto exit;
8179
da0436e9
JS
8180 /*
8181 * Initialize the bootstrap memory region to avoid stale data areas
8182 * in the mailbox post. Then copy the caller's mailbox contents to
8183 * the bmbx mailbox region.
8184 */
8185 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8186 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8187 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8188 sizeof(struct lpfc_mqe));
8189
8190 /* Post the high mailbox dma address to the port and wait for ready. */
8191 dma_address = &phba->sli4_hba.bmbx.dma_address;
8192 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8193
2d843edc
JS
8194 /* wait for bootstrap mbox register for hi-address write done */
8195 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8196 if (rc)
8197 goto exit;
da0436e9
JS
8198
8199 /* Post the low mailbox dma address to the port. */
8200 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 8201
2d843edc
JS
8202 /* wait for bootstrap mbox register for low address write done */
8203 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8204 if (rc)
8205 goto exit;
da0436e9
JS
8206
8207 /*
8208 * Read the CQ to ensure the mailbox has completed.
8209 * If so, update the mailbox status so that the upper layers
8210 * can complete the request normally.
8211 */
8212 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8213 sizeof(struct lpfc_mqe));
8214 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8215 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8216 sizeof(struct lpfc_mcqe));
8217 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
8218 /*
8219 * When the CQE status indicates a failure and the mailbox status
8220 * indicates success then copy the CQE status into the mailbox status
8221 * (and prefix it with x4000).
8222 */
da0436e9 8223 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8224 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8225 bf_set(lpfc_mqe_status, mb,
8226 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8227 rc = MBXERR_ERROR;
d7c47992
JS
8228 } else
8229 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8230
8231 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8232 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8233 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8234 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8235 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8236 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8237 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8238 bf_get(lpfc_mqe_status, mb),
8239 mb->un.mb_words[0], mb->un.mb_words[1],
8240 mb->un.mb_words[2], mb->un.mb_words[3],
8241 mb->un.mb_words[4], mb->un.mb_words[5],
8242 mb->un.mb_words[6], mb->un.mb_words[7],
8243 mb->un.mb_words[8], mb->un.mb_words[9],
8244 mb->un.mb_words[10], mb->un.mb_words[11],
8245 mb->un.mb_words[12], mboxq->mcqe.word0,
8246 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8247 mboxq->mcqe.trailer);
8248exit:
8249 /* We are holding the token, no needed for lock when release */
8250 spin_lock_irqsave(&phba->hbalock, iflag);
8251 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8252 phba->sli.mbox_active = NULL;
8253 spin_unlock_irqrestore(&phba->hbalock, iflag);
8254 return rc;
8255}
8256
8257/**
8258 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8259 * @phba: Pointer to HBA context object.
8260 * @pmbox: Pointer to mailbox object.
8261 * @flag: Flag indicating how the mailbox need to be processed.
8262 *
8263 * This function is called by discovery code and HBA management code to submit
8264 * a mailbox command to firmware with SLI-4 interface spec.
8265 *
8266 * Return codes the caller owns the mailbox command after the return of the
8267 * function.
8268 **/
8269static int
8270lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8271 uint32_t flag)
8272{
8273 struct lpfc_sli *psli = &phba->sli;
8274 unsigned long iflags;
8275 int rc;
8276
b76f2dc9
JS
8277 /* dump from issue mailbox command if setup */
8278 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8279
8fa38513
JS
8280 rc = lpfc_mbox_dev_check(phba);
8281 if (unlikely(rc)) {
8282 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8283 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8284 "cannot issue Data: x%x x%x\n",
8285 mboxq->vport ? mboxq->vport->vpi : 0,
8286 mboxq->u.mb.mbxCommand,
a183a15f
JS
8287 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8288 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8289 psli->sli_flag, flag);
8290 goto out_not_finished;
8291 }
8292
da0436e9
JS
8293 /* Detect polling mode and jump to a handler */
8294 if (!phba->sli4_hba.intr_enable) {
8295 if (flag == MBX_POLL)
8296 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8297 else
8298 rc = -EIO;
8299 if (rc != MBX_SUCCESS)
0558056c 8300 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8301 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8302 "(x%x/x%x) failure: "
8303 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8304 "Data: x%x x%x\n,",
da0436e9
JS
8305 mboxq->vport ? mboxq->vport->vpi : 0,
8306 mboxq->u.mb.mbxCommand,
a183a15f
JS
8307 lpfc_sli_config_mbox_subsys_get(phba,
8308 mboxq),
8309 lpfc_sli_config_mbox_opcode_get(phba,
8310 mboxq),
cc459f19
JS
8311 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8312 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8313 bf_get(lpfc_mcqe_ext_status,
8314 &mboxq->mcqe),
da0436e9
JS
8315 psli->sli_flag, flag);
8316 return rc;
8317 } else if (flag == MBX_POLL) {
f1126688
JS
8318 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8319 "(%d):2542 Try to issue mailbox command "
7365f6fd 8320 "x%x (x%x/x%x) synchronously ahead of async "
f1126688 8321 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8322 mboxq->vport ? mboxq->vport->vpi : 0,
8323 mboxq->u.mb.mbxCommand,
a183a15f
JS
8324 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8325 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8326 psli->sli_flag, flag);
f1126688
JS
8327 /* Try to block the asynchronous mailbox posting */
8328 rc = lpfc_sli4_async_mbox_block(phba);
8329 if (!rc) {
8330 /* Successfully blocked, now issue sync mbox cmd */
8331 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8332 if (rc != MBX_SUCCESS)
cc459f19 8333 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8334 LOG_MBOX | LOG_SLI,
cc459f19
JS
8335 "(%d):2597 Sync Mailbox command "
8336 "x%x (x%x/x%x) failure: "
8337 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8338 "Data: x%x x%x\n,",
8339 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8340 mboxq->u.mb.mbxCommand,
8341 lpfc_sli_config_mbox_subsys_get(phba,
8342 mboxq),
8343 lpfc_sli_config_mbox_opcode_get(phba,
8344 mboxq),
cc459f19
JS
8345 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8346 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8347 bf_get(lpfc_mcqe_ext_status,
8348 &mboxq->mcqe),
a183a15f 8349 psli->sli_flag, flag);
f1126688
JS
8350 /* Unblock the async mailbox posting afterward */
8351 lpfc_sli4_async_mbox_unblock(phba);
8352 }
8353 return rc;
da0436e9
JS
8354 }
8355
8356 /* Now, interrupt mode asynchrous mailbox command */
8357 rc = lpfc_mbox_cmd_check(phba, mboxq);
8358 if (rc) {
8359 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8360 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8361 "cannot issue Data: x%x x%x\n",
8362 mboxq->vport ? mboxq->vport->vpi : 0,
8363 mboxq->u.mb.mbxCommand,
a183a15f
JS
8364 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8365 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8366 psli->sli_flag, flag);
8367 goto out_not_finished;
8368 }
da0436e9
JS
8369
8370 /* Put the mailbox command to the driver internal FIFO */
8371 psli->slistat.mbox_busy++;
8372 spin_lock_irqsave(&phba->hbalock, iflags);
8373 lpfc_mbox_put(phba, mboxq);
8374 spin_unlock_irqrestore(&phba->hbalock, iflags);
8375 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8376 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8377 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8378 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8379 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8380 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8381 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8382 phba->pport->port_state,
8383 psli->sli_flag, MBX_NOWAIT);
8384 /* Wake up worker thread to transport mailbox command from head */
8385 lpfc_worker_wake_up(phba);
8386
8387 return MBX_BUSY;
8388
8389out_not_finished:
8390 return MBX_NOT_FINISHED;
8391}
8392
8393/**
8394 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8395 * @phba: Pointer to HBA context object.
8396 *
8397 * This function is called by worker thread to send a mailbox command to
8398 * SLI4 HBA firmware.
8399 *
8400 **/
8401int
8402lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8403{
8404 struct lpfc_sli *psli = &phba->sli;
8405 LPFC_MBOXQ_t *mboxq;
8406 int rc = MBX_SUCCESS;
8407 unsigned long iflags;
8408 struct lpfc_mqe *mqe;
8409 uint32_t mbx_cmnd;
8410
8411 /* Check interrupt mode before post async mailbox command */
8412 if (unlikely(!phba->sli4_hba.intr_enable))
8413 return MBX_NOT_FINISHED;
8414
8415 /* Check for mailbox command service token */
8416 spin_lock_irqsave(&phba->hbalock, iflags);
8417 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8418 spin_unlock_irqrestore(&phba->hbalock, iflags);
8419 return MBX_NOT_FINISHED;
8420 }
8421 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8422 spin_unlock_irqrestore(&phba->hbalock, iflags);
8423 return MBX_NOT_FINISHED;
8424 }
8425 if (unlikely(phba->sli.mbox_active)) {
8426 spin_unlock_irqrestore(&phba->hbalock, iflags);
8427 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8428 "0384 There is pending active mailbox cmd\n");
8429 return MBX_NOT_FINISHED;
8430 }
8431 /* Take the mailbox command service token */
8432 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8433
8434 /* Get the next mailbox command from head of queue */
8435 mboxq = lpfc_mbox_get(phba);
8436
8437 /* If no more mailbox command waiting for post, we're done */
8438 if (!mboxq) {
8439 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8440 spin_unlock_irqrestore(&phba->hbalock, iflags);
8441 return MBX_SUCCESS;
8442 }
8443 phba->sli.mbox_active = mboxq;
8444 spin_unlock_irqrestore(&phba->hbalock, iflags);
8445
8446 /* Check device readiness for posting mailbox command */
8447 rc = lpfc_mbox_dev_check(phba);
8448 if (unlikely(rc))
8449 /* Driver clean routine will clean up pending mailbox */
8450 goto out_not_finished;
8451
8452 /* Prepare the mbox command to be posted */
8453 mqe = &mboxq->u.mqe;
8454 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8455
8456 /* Start timer for the mbox_tmo and log some mailbox post messages */
8457 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8458 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8459
8460 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8461 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8462 "x%x x%x\n",
8463 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8464 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8465 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8466 phba->pport->port_state, psli->sli_flag);
8467
8468 if (mbx_cmnd != MBX_HEARTBEAT) {
8469 if (mboxq->vport) {
8470 lpfc_debugfs_disc_trc(mboxq->vport,
8471 LPFC_DISC_TRC_MBOX_VPORT,
8472 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8473 mbx_cmnd, mqe->un.mb_words[0],
8474 mqe->un.mb_words[1]);
8475 } else {
8476 lpfc_debugfs_disc_trc(phba->pport,
8477 LPFC_DISC_TRC_MBOX,
8478 "MBOX Send: cmd:x%x mb:x%x x%x",
8479 mbx_cmnd, mqe->un.mb_words[0],
8480 mqe->un.mb_words[1]);
8481 }
8482 }
8483 psli->slistat.mbox_cmd++;
8484
8485 /* Post the mailbox command to the port */
8486 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8487 if (rc != MBX_SUCCESS) {
8488 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8489 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8490 "cannot issue Data: x%x x%x\n",
8491 mboxq->vport ? mboxq->vport->vpi : 0,
8492 mboxq->u.mb.mbxCommand,
a183a15f
JS
8493 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8494 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8495 psli->sli_flag, MBX_NOWAIT);
8496 goto out_not_finished;
8497 }
8498
8499 return rc;
8500
8501out_not_finished:
8502 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8503 if (phba->sli.mbox_active) {
8504 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8505 __lpfc_mbox_cmpl_put(phba, mboxq);
8506 /* Release the token */
8507 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8508 phba->sli.mbox_active = NULL;
8509 }
da0436e9
JS
8510 spin_unlock_irqrestore(&phba->hbalock, iflags);
8511
8512 return MBX_NOT_FINISHED;
8513}
8514
8515/**
8516 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8517 * @phba: Pointer to HBA context object.
8518 * @pmbox: Pointer to mailbox object.
8519 * @flag: Flag indicating how the mailbox need to be processed.
8520 *
8521 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8522 * the API jump table function pointer from the lpfc_hba struct.
8523 *
8524 * Return codes the caller owns the mailbox command after the return of the
8525 * function.
8526 **/
8527int
8528lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8529{
8530 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8531}
8532
8533/**
25985edc 8534 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8535 * @phba: The hba struct for which this call is being executed.
8536 * @dev_grp: The HBA PCI-Device group number.
8537 *
8538 * This routine sets up the mbox interface API function jump table in @phba
8539 * struct.
8540 * Returns: 0 - success, -ENODEV - failure.
8541 **/
8542int
8543lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8544{
8545
8546 switch (dev_grp) {
8547 case LPFC_PCI_DEV_LP:
8548 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8549 phba->lpfc_sli_handle_slow_ring_event =
8550 lpfc_sli_handle_slow_ring_event_s3;
8551 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8552 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8553 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8554 break;
8555 case LPFC_PCI_DEV_OC:
8556 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8557 phba->lpfc_sli_handle_slow_ring_event =
8558 lpfc_sli_handle_slow_ring_event_s4;
8559 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8560 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8561 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8562 break;
8563 default:
8564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8565 "1420 Invalid HBA PCI-device group: 0x%x\n",
8566 dev_grp);
8567 return -ENODEV;
8568 break;
8569 }
8570 return 0;
8571}
8572
e59058c4 8573/**
3621a710 8574 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8575 * @phba: Pointer to HBA context object.
8576 * @pring: Pointer to driver SLI ring object.
8577 * @piocb: Pointer to address of newly added command iocb.
8578 *
8579 * This function is called with hbalock held to add a command
8580 * iocb to the txq when SLI layer cannot submit the command iocb
8581 * to the ring.
8582 **/
2a9bf3d0 8583void
92d7f7b0 8584__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8585 struct lpfc_iocbq *piocb)
dea3101e 8586{
1c2ba475 8587 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8588 /* Insert the caller's iocb in the txq tail for later processing. */
8589 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8590}
8591
e59058c4 8592/**
3621a710 8593 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8594 * @phba: Pointer to HBA context object.
8595 * @pring: Pointer to driver SLI ring object.
8596 * @piocb: Pointer to address of newly added command iocb.
8597 *
8598 * This function is called with hbalock held before a new
8599 * iocb is submitted to the firmware. This function checks
8600 * txq to flush the iocbs in txq to Firmware before
8601 * submitting new iocbs to the Firmware.
8602 * If there are iocbs in the txq which need to be submitted
8603 * to firmware, lpfc_sli_next_iocb returns the first element
8604 * of the txq after dequeuing it from txq.
8605 * If there is no iocb in the txq then the function will return
8606 * *piocb and *piocb is set to NULL. Caller needs to check
8607 * *piocb to find if there are more commands in the txq.
8608 **/
dea3101e
JB
8609static struct lpfc_iocbq *
8610lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8611 struct lpfc_iocbq **piocb)
dea3101e
JB
8612{
8613 struct lpfc_iocbq * nextiocb;
8614
1c2ba475
JT
8615 lockdep_assert_held(&phba->hbalock);
8616
dea3101e
JB
8617 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8618 if (!nextiocb) {
8619 nextiocb = *piocb;
8620 *piocb = NULL;
8621 }
8622
8623 return nextiocb;
8624}
8625
e59058c4 8626/**
3772a991 8627 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8628 * @phba: Pointer to HBA context object.
3772a991 8629 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8630 * @piocb: Pointer to command iocb.
8631 * @flag: Flag indicating if this command can be put into txq.
8632 *
3772a991
JS
8633 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8634 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8635 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8636 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8637 * this function allows only iocbs for posting buffers. This function finds
8638 * next available slot in the command ring and posts the command to the
8639 * available slot and writes the port attention register to request HBA start
8640 * processing new iocb. If there is no slot available in the ring and
8641 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8642 * the function returns IOCB_BUSY.
e59058c4 8643 *
3772a991
JS
8644 * This function is called with hbalock held. The function will return success
8645 * after it successfully submit the iocb to firmware or after adding to the
8646 * txq.
e59058c4 8647 **/
98c9ea5c 8648static int
3772a991 8649__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8650 struct lpfc_iocbq *piocb, uint32_t flag)
8651{
8652 struct lpfc_iocbq *nextiocb;
8653 IOCB_t *iocb;
895427bd 8654 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8655
1c2ba475
JT
8656 lockdep_assert_held(&phba->hbalock);
8657
92d7f7b0
JS
8658 if (piocb->iocb_cmpl && (!piocb->vport) &&
8659 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8660 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8661 lpfc_printf_log(phba, KERN_ERR,
8662 LOG_SLI | LOG_VPORT,
e8b62011 8663 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8664 piocb->iocb.ulpCommand);
8665 dump_stack();
8666 return IOCB_ERROR;
8667 }
8668
8669
8d63f375
LV
8670 /* If the PCI channel is in offline state, do not post iocbs. */
8671 if (unlikely(pci_channel_offline(phba->pcidev)))
8672 return IOCB_ERROR;
8673
a257bf90
JS
8674 /* If HBA has a deferred error attention, fail the iocb. */
8675 if (unlikely(phba->hba_flag & DEFER_ERATT))
8676 return IOCB_ERROR;
8677
dea3101e
JB
8678 /*
8679 * We should never get an IOCB if we are in a < LINK_DOWN state
8680 */
2e0fef85 8681 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8682 return IOCB_ERROR;
8683
8684 /*
8685 * Check to see if we are blocking IOCB processing because of a
0b727fea 8686 * outstanding event.
dea3101e 8687 */
0b727fea 8688 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8689 goto iocb_busy;
8690
2e0fef85 8691 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8692 /*
2680eeaa 8693 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8694 * can be issued if the link is not up.
8695 */
8696 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8697 case CMD_GEN_REQUEST64_CR:
8698 case CMD_GEN_REQUEST64_CX:
8699 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8700 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8701 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8702 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8703 MENLO_TRANSPORT_TYPE))
8704
8705 goto iocb_busy;
8706 break;
dea3101e
JB
8707 case CMD_QUE_RING_BUF_CN:
8708 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8709 /*
8710 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8711 * completion, iocb_cmpl MUST be 0.
8712 */
8713 if (piocb->iocb_cmpl)
8714 piocb->iocb_cmpl = NULL;
8715 /*FALLTHROUGH*/
8716 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8717 case CMD_CLOSE_XRI_CN:
8718 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8719 break;
8720 default:
8721 goto iocb_busy;
8722 }
8723
8724 /*
8725 * For FCP commands, we must be in a state where we can process link
8726 * attention events.
8727 */
895427bd 8728 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8729 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8730 goto iocb_busy;
92d7f7b0 8731 }
dea3101e 8732
dea3101e
JB
8733 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8734 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8735 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8736
8737 if (iocb)
8738 lpfc_sli_update_ring(phba, pring);
8739 else
8740 lpfc_sli_update_full_ring(phba, pring);
8741
8742 if (!piocb)
8743 return IOCB_SUCCESS;
8744
8745 goto out_busy;
8746
8747 iocb_busy:
8748 pring->stats.iocb_cmd_delay++;
8749
8750 out_busy:
8751
8752 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8753 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8754 return IOCB_SUCCESS;
8755 }
8756
8757 return IOCB_BUSY;
8758}
8759
3772a991 8760/**
4f774513
JS
8761 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8762 * @phba: Pointer to HBA context object.
8763 * @piocb: Pointer to command iocb.
8764 * @sglq: Pointer to the scatter gather queue object.
8765 *
8766 * This routine converts the bpl or bde that is in the IOCB
8767 * to a sgl list for the sli4 hardware. The physical address
8768 * of the bpl/bde is converted back to a virtual address.
8769 * If the IOCB contains a BPL then the list of BDE's is
8770 * converted to sli4_sge's. If the IOCB contains a single
8771 * BDE then it is converted to a single sli_sge.
8772 * The IOCB is still in cpu endianess so the contents of
8773 * the bpl can be used without byte swapping.
8774 *
8775 * Returns valid XRI = Success, NO_XRI = Failure.
8776**/
8777static uint16_t
8778lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8779 struct lpfc_sglq *sglq)
3772a991 8780{
4f774513
JS
8781 uint16_t xritag = NO_XRI;
8782 struct ulp_bde64 *bpl = NULL;
8783 struct ulp_bde64 bde;
8784 struct sli4_sge *sgl = NULL;
1b51197d 8785 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8786 IOCB_t *icmd;
8787 int numBdes = 0;
8788 int i = 0;
63e801ce
JS
8789 uint32_t offset = 0; /* accumulated offset in the sg request list */
8790 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8791
4f774513
JS
8792 if (!piocbq || !sglq)
8793 return xritag;
8794
8795 sgl = (struct sli4_sge *)sglq->sgl;
8796 icmd = &piocbq->iocb;
6b5151fd
JS
8797 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8798 return sglq->sli4_xritag;
4f774513
JS
8799 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8800 numBdes = icmd->un.genreq64.bdl.bdeSize /
8801 sizeof(struct ulp_bde64);
8802 /* The addrHigh and addrLow fields within the IOCB
8803 * have not been byteswapped yet so there is no
8804 * need to swap them back.
8805 */
1b51197d
JS
8806 if (piocbq->context3)
8807 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8808 else
8809 return xritag;
4f774513 8810
1b51197d 8811 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8812 if (!bpl)
8813 return xritag;
8814
8815 for (i = 0; i < numBdes; i++) {
8816 /* Should already be byte swapped. */
28baac74
JS
8817 sgl->addr_hi = bpl->addrHigh;
8818 sgl->addr_lo = bpl->addrLow;
8819
0558056c 8820 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8821 if ((i+1) == numBdes)
8822 bf_set(lpfc_sli4_sge_last, sgl, 1);
8823 else
8824 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8825 /* swap the size field back to the cpu so we
8826 * can assign it to the sgl.
8827 */
8828 bde.tus.w = le32_to_cpu(bpl->tus.w);
8829 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8830 /* The offsets in the sgl need to be accumulated
8831 * separately for the request and reply lists.
8832 * The request is always first, the reply follows.
8833 */
8834 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8835 /* add up the reply sg entries */
8836 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8837 inbound++;
8838 /* first inbound? reset the offset */
8839 if (inbound == 1)
8840 offset = 0;
8841 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8842 bf_set(lpfc_sli4_sge_type, sgl,
8843 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8844 offset += bde.tus.f.bdeSize;
8845 }
546fc854 8846 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8847 bpl++;
8848 sgl++;
8849 }
8850 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8851 /* The addrHigh and addrLow fields of the BDE have not
8852 * been byteswapped yet so they need to be swapped
8853 * before putting them in the sgl.
8854 */
8855 sgl->addr_hi =
8856 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8857 sgl->addr_lo =
8858 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8859 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8860 bf_set(lpfc_sli4_sge_last, sgl, 1);
8861 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8862 sgl->sge_len =
8863 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8864 }
8865 return sglq->sli4_xritag;
3772a991 8866}
92d7f7b0 8867
e59058c4 8868/**
4f774513 8869 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8870 * @phba: Pointer to HBA context object.
4f774513
JS
8871 * @piocb: Pointer to command iocb.
8872 * @wqe: Pointer to the work queue entry.
e59058c4 8873 *
4f774513
JS
8874 * This routine converts the iocb command to its Work Queue Entry
8875 * equivalent. The wqe pointer should not have any fields set when
8876 * this routine is called because it will memcpy over them.
8877 * This routine does not set the CQ_ID or the WQEC bits in the
8878 * wqe.
e59058c4 8879 *
4f774513 8880 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8881 **/
cf5bf97e 8882static int
4f774513 8883lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
205e8240 8884 union lpfc_wqe128 *wqe)
cf5bf97e 8885{
5ffc266e 8886 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8887 uint8_t ct = 0;
8888 uint32_t fip;
8889 uint32_t abort_tag;
8890 uint8_t command_type = ELS_COMMAND_NON_FIP;
8891 uint8_t cmnd;
8892 uint16_t xritag;
dcf2a4e0
JS
8893 uint16_t abrt_iotag;
8894 struct lpfc_iocbq *abrtiocbq;
4f774513 8895 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8896 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8897 int numBdes, i;
8898 struct ulp_bde64 bde;
c31098ce 8899 struct lpfc_nodelist *ndlp;
ff78d8f9 8900 uint32_t *pcmd;
1b51197d 8901 uint32_t if_type;
4f774513 8902
45ed1190 8903 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8904 /* The fcp commands will set command type */
0c287589 8905 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8906 command_type = FCP_COMMAND;
c868595d 8907 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8908 command_type = ELS_COMMAND_FIP;
8909 else
8910 command_type = ELS_COMMAND_NON_FIP;
8911
b5c53958
JS
8912 if (phba->fcp_embed_io)
8913 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8914 /* Some of the fields are in the right position already */
8915 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
8916 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8917 /* The ct field has moved so reset */
8918 wqe->generic.wqe_com.word7 = 0;
8919 wqe->generic.wqe_com.word10 = 0;
8920 }
b5c53958
JS
8921
8922 abort_tag = (uint32_t) iocbq->iotag;
8923 xritag = iocbq->sli4_xritag;
4f774513
JS
8924 /* words0-2 bpl convert bde */
8925 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8926 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8927 sizeof(struct ulp_bde64);
4f774513
JS
8928 bpl = (struct ulp_bde64 *)
8929 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8930 if (!bpl)
8931 return IOCB_ERROR;
cf5bf97e 8932
4f774513
JS
8933 /* Should already be byte swapped. */
8934 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8935 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8936 /* swap the size field back to the cpu so we
8937 * can assign it to the sgl.
8938 */
8939 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8940 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8941 total_len = 0;
8942 for (i = 0; i < numBdes; i++) {
8943 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8944 total_len += bde.tus.f.bdeSize;
8945 }
4f774513 8946 } else
5ffc266e 8947 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8948
4f774513
JS
8949 iocbq->iocb.ulpIoTag = iocbq->iotag;
8950 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8951
4f774513
JS
8952 switch (iocbq->iocb.ulpCommand) {
8953 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8954 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8955 ndlp = iocbq->context_un.ndlp;
8956 else
8957 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8958 if (!iocbq->iocb.ulpLe) {
8959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8960 "2007 Only Limited Edition cmd Format"
8961 " supported 0x%x\n",
8962 iocbq->iocb.ulpCommand);
8963 return IOCB_ERROR;
8964 }
ff78d8f9 8965
5ffc266e 8966 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8967 /* Els_reguest64 has a TMO */
8968 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8969 iocbq->iocb.ulpTimeout);
8970 /* Need a VF for word 4 set the vf bit*/
8971 bf_set(els_req64_vf, &wqe->els_req, 0);
8972 /* And a VFID for word 12 */
8973 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8974 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8975 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8976 iocbq->iocb.ulpContext);
8977 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8978 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8979 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8980 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8981 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8982 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8983 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8984 iocbq->context2)->virt);
1b51197d
JS
8985 if_type = bf_get(lpfc_sli_intf_if_type,
8986 &phba->sli4_hba.sli_intf);
27d6ac0a 8987 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8988 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8989 *pcmd == ELS_CMD_SCR ||
6b5151fd 8990 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8991 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8992 *pcmd == ELS_CMD_PLOGI)) {
8993 bf_set(els_req64_sp, &wqe->els_req, 1);
8994 bf_set(els_req64_sid, &wqe->els_req,
8995 iocbq->vport->fc_myDID);
939723a4
JS
8996 if ((*pcmd == ELS_CMD_FLOGI) &&
8997 !(phba->fc_topology ==
8998 LPFC_TOPOLOGY_LOOP))
8999 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
9000 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9001 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 9002 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 9003 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
9004 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9005 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9006 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9007 }
c868595d 9008 }
6d368e53
JS
9009 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9010 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
9011 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9012 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9013 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9014 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9015 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9016 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 9017 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 9018 break;
5ffc266e 9019 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
9020 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9021 iocbq->iocb.un.ulpWord[3]);
9022 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 9023 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
9024 /* The entire sequence is transmitted for this IOCB */
9025 xmit_len = total_len;
9026 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
9027 if (phba->link_flag & LS_LOOPBACK_MODE)
9028 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 9029 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
9030 /* word3 iocb=io_tag32 wqe=reserved */
9031 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
9032 /* word4 relative_offset memcpy */
9033 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
9034 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9035 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9036 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9037 LPFC_WQE_IOD_WRITE);
9038 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9039 LPFC_WQE_LENLOC_WORD12);
9040 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
9041 wqe->xmit_sequence.xmit_len = xmit_len;
9042 command_type = OTHER_COMMAND;
7851fe2c 9043 break;
4f774513 9044 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
9045 /* word3 iocb=iotag32 wqe=seq_payload_len */
9046 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
9047 /* word4 iocb=rsvd wqe=rsvd */
9048 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9049 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 9050 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 9051 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
9052 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9053 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9054 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9055 LPFC_WQE_LENLOC_WORD3);
9056 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 9057 break;
4f774513
JS
9058 case CMD_FCP_IWRITE64_CR:
9059 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
9060 /* word3 iocb=iotag wqe=payload_offset_len */
9061 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9062 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9063 xmit_len + sizeof(struct fcp_rsp));
9064 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9065 0);
f0d9bccc
JS
9066 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9067 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9068 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9069 iocbq->iocb.ulpFCP2Rcvy);
9070 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9071 /* Always open the exchange */
f0d9bccc
JS
9072 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9073 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9074 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9075 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9076 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
9077 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9078 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
9079 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9080 if (iocbq->priority) {
9081 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9082 (iocbq->priority << 1));
9083 } else {
1ba981fd
JS
9084 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9085 (phba->cfg_XLanePriority << 1));
9086 }
9087 }
b5c53958
JS
9088 /* Note, word 10 is already initialized to 0 */
9089
0bc2b7c5
JS
9090 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9091 if (phba->fcp_embed_pbde)
9092 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9093 else
9094 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9095
b5c53958
JS
9096 if (phba->fcp_embed_io) {
9097 struct lpfc_scsi_buf *lpfc_cmd;
9098 struct sli4_sge *sgl;
b5c53958
JS
9099 struct fcp_cmnd *fcp_cmnd;
9100 uint32_t *ptr;
9101
9102 /* 128 byte wqe support here */
b5c53958
JS
9103
9104 lpfc_cmd = iocbq->context1;
9105 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9106 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9107
9108 /* Word 0-2 - FCP_CMND */
205e8240 9109 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9110 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9111 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9112 wqe->generic.bde.addrHigh = 0;
9113 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9114
205e8240
JS
9115 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9116 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
b5c53958
JS
9117
9118 /* Word 22-29 FCP CMND Payload */
205e8240 9119 ptr = &wqe->words[22];
b5c53958
JS
9120 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9121 }
7851fe2c 9122 break;
4f774513 9123 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
9124 /* word3 iocb=iotag wqe=payload_offset_len */
9125 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9126 bf_set(payload_offset_len, &wqe->fcp_iread,
9127 xmit_len + sizeof(struct fcp_rsp));
9128 bf_set(cmd_buff_len, &wqe->fcp_iread,
9129 0);
f0d9bccc
JS
9130 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9131 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9132 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9133 iocbq->iocb.ulpFCP2Rcvy);
9134 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 9135 /* Always open the exchange */
f0d9bccc
JS
9136 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9137 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9138 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9139 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9140 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
9141 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9142 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
9143 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9144 if (iocbq->priority) {
9145 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9146 (iocbq->priority << 1));
9147 } else {
1ba981fd
JS
9148 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9149 (phba->cfg_XLanePriority << 1));
9150 }
9151 }
b5c53958
JS
9152 /* Note, word 10 is already initialized to 0 */
9153
0bc2b7c5
JS
9154 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9155 if (phba->fcp_embed_pbde)
9156 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9157 else
9158 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9159
b5c53958
JS
9160 if (phba->fcp_embed_io) {
9161 struct lpfc_scsi_buf *lpfc_cmd;
9162 struct sli4_sge *sgl;
b5c53958
JS
9163 struct fcp_cmnd *fcp_cmnd;
9164 uint32_t *ptr;
9165
9166 /* 128 byte wqe support here */
b5c53958
JS
9167
9168 lpfc_cmd = iocbq->context1;
9169 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9170 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9171
9172 /* Word 0-2 - FCP_CMND */
205e8240 9173 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9174 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9175 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9176 wqe->generic.bde.addrHigh = 0;
9177 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9178
205e8240
JS
9179 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9180 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
b5c53958
JS
9181
9182 /* Word 22-29 FCP CMND Payload */
205e8240 9183 ptr = &wqe->words[22];
b5c53958
JS
9184 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9185 }
7851fe2c 9186 break;
4f774513 9187 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
9188 /* word3 iocb=iotag wqe=payload_offset_len */
9189 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9190 bf_set(payload_offset_len, &wqe->fcp_icmd,
9191 xmit_len + sizeof(struct fcp_rsp));
9192 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9193 0);
f0d9bccc 9194 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 9195 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 9196 /* Always open the exchange */
f0d9bccc
JS
9197 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9198 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9199 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9200 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9201 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
9202 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9203 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
9204 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9205 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
9206 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9207 if (iocbq->priority) {
9208 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9209 (iocbq->priority << 1));
9210 } else {
1ba981fd
JS
9211 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9212 (phba->cfg_XLanePriority << 1));
9213 }
9214 }
b5c53958
JS
9215 /* Note, word 10 is already initialized to 0 */
9216
9217 if (phba->fcp_embed_io) {
9218 struct lpfc_scsi_buf *lpfc_cmd;
9219 struct sli4_sge *sgl;
b5c53958
JS
9220 struct fcp_cmnd *fcp_cmnd;
9221 uint32_t *ptr;
9222
9223 /* 128 byte wqe support here */
b5c53958
JS
9224
9225 lpfc_cmd = iocbq->context1;
9226 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9227 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9228
9229 /* Word 0-2 - FCP_CMND */
205e8240 9230 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9231 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9232 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9233 wqe->generic.bde.addrHigh = 0;
9234 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9235
205e8240
JS
9236 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9237 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
b5c53958
JS
9238
9239 /* Word 22-29 FCP CMND Payload */
205e8240 9240 ptr = &wqe->words[22];
b5c53958
JS
9241 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9242 }
7851fe2c 9243 break;
4f774513 9244 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9245 /* For this command calculate the xmit length of the
9246 * request bde.
9247 */
9248 xmit_len = 0;
9249 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9250 sizeof(struct ulp_bde64);
9251 for (i = 0; i < numBdes; i++) {
63e801ce 9252 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9253 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9254 break;
63e801ce
JS
9255 xmit_len += bde.tus.f.bdeSize;
9256 }
f0d9bccc
JS
9257 /* word3 iocb=IO_TAG wqe=request_payload_len */
9258 wqe->gen_req.request_payload_len = xmit_len;
9259 /* word4 iocb=parameter wqe=relative_offset memcpy */
9260 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9261 /* word6 context tag copied in memcpy */
9262 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9263 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9265 "2015 Invalid CT %x command 0x%x\n",
9266 ct, iocbq->iocb.ulpCommand);
9267 return IOCB_ERROR;
9268 }
f0d9bccc
JS
9269 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9270 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9271 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9272 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9273 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9274 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9275 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9276 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9277 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9278 command_type = OTHER_COMMAND;
7851fe2c 9279 break;
4f774513 9280 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9281 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9282 /* words0-2 BDE memcpy */
f0d9bccc
JS
9283 /* word3 iocb=iotag32 wqe=response_payload_len */
9284 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9285 /* word4 */
9286 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9287 /* word5 iocb=rsvd wge=did */
9288 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9289 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9290
9291 if_type = bf_get(lpfc_sli_intf_if_type,
9292 &phba->sli4_hba.sli_intf);
27d6ac0a 9293 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
939723a4
JS
9294 if (iocbq->vport->fc_flag & FC_PT2PT) {
9295 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9296 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9297 iocbq->vport->fc_myDID);
9298 if (iocbq->vport->fc_myDID == Fabric_DID) {
9299 bf_set(wqe_els_did,
9300 &wqe->xmit_els_rsp.wqe_dest, 0);
9301 }
9302 }
9303 }
f0d9bccc
JS
9304 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9305 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9306 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9307 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9308 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9309 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9310 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9311 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9312 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9313 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9314 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9315 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9316 LPFC_WQE_LENLOC_WORD3);
9317 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9318 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9319 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9320 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9321 iocbq->context2)->virt);
9322 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9323 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9324 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9325 iocbq->vport->fc_myDID);
939723a4
JS
9326 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9327 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9328 phba->vpi_ids[phba->pport->vpi]);
9329 }
4f774513 9330 command_type = OTHER_COMMAND;
7851fe2c 9331 break;
4f774513
JS
9332 case CMD_CLOSE_XRI_CN:
9333 case CMD_ABORT_XRI_CN:
9334 case CMD_ABORT_XRI_CX:
9335 /* words 0-2 memcpy should be 0 rserved */
9336 /* port will send abts */
dcf2a4e0
JS
9337 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9338 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9339 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9340 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9341 } else
9342 fip = 0;
9343
9344 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9345 /*
dcf2a4e0
JS
9346 * The link is down, or the command was ELS_FIP
9347 * so the fw does not need to send abts
4f774513
JS
9348 * on the wire.
9349 */
9350 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9351 else
9352 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9353 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9354 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9355 wqe->abort_cmd.rsrvd5 = 0;
9356 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9357 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9358 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9359 /*
9360 * The abort handler will send us CMD_ABORT_XRI_CN or
9361 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9362 */
f0d9bccc
JS
9363 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9364 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9365 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9366 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9367 cmnd = CMD_ABORT_XRI_CX;
9368 command_type = OTHER_COMMAND;
9369 xritag = 0;
7851fe2c 9370 break;
6669f9bb 9371 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9372 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9373 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9374 * we re-construct this WQE here based on information in
9375 * iocbq from scratch.
9376 */
9377 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9378 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9379 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9380 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9381 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9382 LPFC_ABTS_UNSOL_INT) {
9383 /* ABTS sent by initiator to CT exchange, the
9384 * RX_ID field will be filled with the newly
9385 * allocated responder XRI.
9386 */
9387 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9388 iocbq->sli4_xritag);
9389 } else {
9390 /* ABTS sent by responder to CT exchange, the
9391 * RX_ID field will be filled with the responder
9392 * RX_ID from ABTS.
9393 */
9394 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9395 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9396 }
6669f9bb
JS
9397 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9398 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9399
9400 /* Use CT=VPI */
9401 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9402 ndlp->nlp_DID);
9403 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9404 iocbq->iocb.ulpContext);
9405 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9406 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9407 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9408 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9409 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9410 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9411 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9412 command_type = OTHER_COMMAND;
546fc854
JS
9413 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9414 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9415 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9416 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9417 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9418 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9419 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9420 }
9421
7851fe2c 9422 break;
ae9e28f3
JS
9423 case CMD_SEND_FRAME:
9424 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9425 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9426 return 0;
4f774513
JS
9427 case CMD_XRI_ABORTED_CX:
9428 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9429 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9430 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9431 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9432 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9433 default:
9434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9435 "2014 Invalid command 0x%x\n",
9436 iocbq->iocb.ulpCommand);
9437 return IOCB_ERROR;
7851fe2c 9438 break;
4f774513 9439 }
6d368e53 9440
8012cc38
JS
9441 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9442 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9443 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9444 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9445 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9446 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9447 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9448 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9449 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9450 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9451 wqe->generic.wqe_com.abort_tag = abort_tag;
9452 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9453 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9454 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9455 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9456 return 0;
9457}
9458
9459/**
9460 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9461 * @phba: Pointer to HBA context object.
9462 * @ring_number: SLI ring number to issue iocb on.
9463 * @piocb: Pointer to command iocb.
9464 * @flag: Flag indicating if this command can be put into txq.
9465 *
9466 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9467 * an iocb command to an HBA with SLI-4 interface spec.
9468 *
9469 * This function is called with hbalock held. The function will return success
9470 * after it successfully submit the iocb to firmware or after adding to the
9471 * txq.
9472 **/
9473static int
9474__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9475 struct lpfc_iocbq *piocb, uint32_t flag)
9476{
9477 struct lpfc_sglq *sglq;
205e8240 9478 union lpfc_wqe128 wqe;
1ba981fd 9479 struct lpfc_queue *wq;
895427bd 9480 struct lpfc_sli_ring *pring;
4f774513 9481
895427bd
JS
9482 /* Get the WQ */
9483 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9484 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9485 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9486 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9487 else
9488 wq = phba->sli4_hba.oas_wq;
9489 } else {
9490 wq = phba->sli4_hba.els_wq;
9491 }
9492
9493 /* Get corresponding ring */
9494 pring = wq->pring;
1c2ba475 9495
b5c53958
JS
9496 /*
9497 * The WQE can be either 64 or 128 bytes,
b5c53958 9498 */
b5c53958 9499
895427bd
JS
9500 lockdep_assert_held(&phba->hbalock);
9501
4f774513
JS
9502 if (piocb->sli4_xritag == NO_XRI) {
9503 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9504 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9505 sglq = NULL;
9506 else {
0e9bb8d7 9507 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9508 if (!(flag & SLI_IOCB_RET_IOCB)) {
9509 __lpfc_sli_ringtx_put(phba,
9510 pring, piocb);
9511 return IOCB_SUCCESS;
9512 } else {
9513 return IOCB_BUSY;
9514 }
9515 } else {
895427bd 9516 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9517 if (!sglq) {
9518 if (!(flag & SLI_IOCB_RET_IOCB)) {
9519 __lpfc_sli_ringtx_put(phba,
9520 pring,
9521 piocb);
9522 return IOCB_SUCCESS;
9523 } else
9524 return IOCB_BUSY;
9525 }
9526 }
4f774513 9527 }
2ea259ee 9528 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9529 /* These IO's already have an XRI and a mapped sgl. */
9530 sglq = NULL;
2ea259ee 9531 else {
6d368e53
JS
9532 /*
9533 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9534 * sglq is on the active list
9535 */
edccdc17 9536 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9537 if (!sglq)
9538 return IOCB_ERROR;
9539 }
9540
9541 if (sglq) {
6d368e53 9542 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9543 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9544 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9545 return IOCB_ERROR;
9546 }
9547
205e8240 9548 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
4f774513
JS
9549 return IOCB_ERROR;
9550
205e8240 9551 if (lpfc_sli4_wq_put(wq, &wqe))
895427bd 9552 return IOCB_ERROR;
4f774513
JS
9553 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9554
9555 return 0;
9556}
9557
9558/**
9559 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9560 *
9561 * This routine wraps the actual lockless version for issusing IOCB function
9562 * pointer from the lpfc_hba struct.
9563 *
9564 * Return codes:
b5c53958
JS
9565 * IOCB_ERROR - Error
9566 * IOCB_SUCCESS - Success
9567 * IOCB_BUSY - Busy
4f774513 9568 **/
2a9bf3d0 9569int
4f774513
JS
9570__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9571 struct lpfc_iocbq *piocb, uint32_t flag)
9572{
9573 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9574}
9575
9576/**
25985edc 9577 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9578 * @phba: The hba struct for which this call is being executed.
9579 * @dev_grp: The HBA PCI-Device group number.
9580 *
9581 * This routine sets up the SLI interface API function jump table in @phba
9582 * struct.
9583 * Returns: 0 - success, -ENODEV - failure.
9584 **/
9585int
9586lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9587{
9588
9589 switch (dev_grp) {
9590 case LPFC_PCI_DEV_LP:
9591 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9592 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9593 break;
9594 case LPFC_PCI_DEV_OC:
9595 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9596 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9597 break;
9598 default:
9599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9600 "1419 Invalid HBA PCI-device group: 0x%x\n",
9601 dev_grp);
9602 return -ENODEV;
9603 break;
9604 }
9605 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9606 return 0;
9607}
9608
a1efe163 9609/**
895427bd 9610 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9611 * @phba: Pointer to HBA context object.
a1efe163
JS
9612 * @piocb: Pointer to command iocb.
9613 *
895427bd
JS
9614 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9615 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9616 * Since ABORTS must go on the same WQ of the command they are
895427bd 9617 * aborting, we use command's hba_wqidx.
a1efe163 9618 */
895427bd
JS
9619struct lpfc_sli_ring *
9620lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9621{
895427bd 9622 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9623 if (!(phba->cfg_fof) ||
895427bd 9624 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9625 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9626 return NULL;
8b0dff14 9627 /*
895427bd 9628 * for abort iocb hba_wqidx should already
8b0dff14
JS
9629 * be setup based on what work queue we used.
9630 */
8e036a94 9631 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9632 piocb->hba_wqidx =
8b0dff14
JS
9633 lpfc_sli4_scmd_to_wqidx_distr(phba,
9634 piocb->context1);
8e036a94
DK
9635 piocb->hba_wqidx = piocb->hba_wqidx %
9636 phba->cfg_fcp_io_channel;
9637 }
895427bd 9638 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9639 } else {
9640 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9641 return NULL;
9642 piocb->hba_wqidx = 0;
9643 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9644 }
895427bd
JS
9645 } else {
9646 if (unlikely(!phba->sli4_hba.els_wq))
9647 return NULL;
9648 piocb->hba_wqidx = 0;
9649 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9650 }
9bd2bff5
JS
9651}
9652
4f774513
JS
9653/**
9654 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9655 * @phba: Pointer to HBA context object.
9656 * @pring: Pointer to driver SLI ring object.
9657 * @piocb: Pointer to command iocb.
9658 * @flag: Flag indicating if this command can be put into txq.
9659 *
9660 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9661 * function. This function gets the hbalock and calls
9662 * __lpfc_sli_issue_iocb function and will return the error returned
9663 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9664 * functions which do not hold hbalock.
9665 **/
9666int
9667lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9668 struct lpfc_iocbq *piocb, uint32_t flag)
9669{
895427bd 9670 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9671 struct lpfc_sli_ring *pring;
ba20c853
JS
9672 struct lpfc_queue *fpeq;
9673 struct lpfc_eqe *eqe;
4f774513 9674 unsigned long iflags;
2a76a283 9675 int rc, idx;
4f774513 9676
7e56aa25 9677 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9678 pring = lpfc_sli4_calc_ring(phba, piocb);
9679 if (unlikely(pring == NULL))
9bd2bff5 9680 return IOCB_ERROR;
ba20c853 9681
9bd2bff5
JS
9682 spin_lock_irqsave(&pring->ring_lock, iflags);
9683 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9684 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9685
9bd2bff5 9686 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9687 idx = piocb->hba_wqidx;
9688 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9689
895427bd 9690 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9691
9bd2bff5
JS
9692 /* Get associated EQ with this index */
9693 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9694
9bd2bff5 9695 /* Turn off interrupts from this EQ */
b71413dd 9696 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 9697
9bd2bff5
JS
9698 /*
9699 * Process all the events on FCP EQ
9700 */
9701 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9702 lpfc_sli4_hba_handle_eqe(phba,
9703 eqe, idx);
9704 fpeq->EQ_processed++;
ba20c853 9705 }
ba20c853 9706
9bd2bff5 9707 /* Always clear and re-arm the EQ */
b71413dd 9708 phba->sli4_hba.sli4_eq_release(fpeq,
9bd2bff5
JS
9709 LPFC_QUEUE_REARM);
9710 }
895427bd 9711 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9712 }
7e56aa25
JS
9713 } else {
9714 /* For now, SLI2/3 will still use hbalock */
9715 spin_lock_irqsave(&phba->hbalock, iflags);
9716 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9717 spin_unlock_irqrestore(&phba->hbalock, iflags);
9718 }
4f774513
JS
9719 return rc;
9720}
9721
9722/**
9723 * lpfc_extra_ring_setup - Extra ring setup function
9724 * @phba: Pointer to HBA context object.
9725 *
9726 * This function is called while driver attaches with the
9727 * HBA to setup the extra ring. The extra ring is used
9728 * only when driver needs to support target mode functionality
9729 * or IP over FC functionalities.
9730 *
895427bd 9731 * This function is called with no lock held. SLI3 only.
4f774513
JS
9732 **/
9733static int
9734lpfc_extra_ring_setup( struct lpfc_hba *phba)
9735{
9736 struct lpfc_sli *psli;
9737 struct lpfc_sli_ring *pring;
9738
9739 psli = &phba->sli;
9740
9741 /* Adjust cmd/rsp ring iocb entries more evenly */
9742
9743 /* Take some away from the FCP ring */
895427bd 9744 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9745 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9746 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9747 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9748 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9749
a4bc3379 9750 /* and give them to the extra ring */
895427bd 9751 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9752
7e56aa25
JS
9753 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9754 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9755 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9756 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9757
9758 /* Setup default profile for this ring */
9759 pring->iotag_max = 4096;
9760 pring->num_mask = 1;
9761 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9762 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9763 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9764 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9765 return 0;
9766}
9767
cb69f7de
JS
9768/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9769 * @phba: Pointer to HBA context object.
9770 * @iocbq: Pointer to iocb object.
9771 *
9772 * The async_event handler calls this routine when it receives
9773 * an ASYNC_STATUS_CN event from the port. The port generates
9774 * this event when an Abort Sequence request to an rport fails
9775 * twice in succession. The abort could be originated by the
9776 * driver or by the port. The ABTS could have been for an ELS
9777 * or FCP IO. The port only generates this event when an ABTS
9778 * fails to complete after one retry.
9779 */
9780static void
9781lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9782 struct lpfc_iocbq *iocbq)
9783{
9784 struct lpfc_nodelist *ndlp = NULL;
9785 uint16_t rpi = 0, vpi = 0;
9786 struct lpfc_vport *vport = NULL;
9787
9788 /* The rpi in the ulpContext is vport-sensitive. */
9789 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9790 rpi = iocbq->iocb.ulpContext;
9791
9792 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9793 "3092 Port generated ABTS async event "
9794 "on vpi %d rpi %d status 0x%x\n",
9795 vpi, rpi, iocbq->iocb.ulpStatus);
9796
9797 vport = lpfc_find_vport_by_vpid(phba, vpi);
9798 if (!vport)
9799 goto err_exit;
9800 ndlp = lpfc_findnode_rpi(vport, rpi);
9801 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9802 goto err_exit;
9803
9804 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9805 lpfc_sli_abts_recover_port(vport, ndlp);
9806 return;
9807
9808 err_exit:
9809 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9810 "3095 Event Context not found, no "
9811 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9812 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9813 vpi, rpi);
9814}
9815
9816/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9817 * @phba: pointer to HBA context object.
9818 * @ndlp: nodelist pointer for the impacted rport.
9819 * @axri: pointer to the wcqe containing the failed exchange.
9820 *
9821 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9822 * port. The port generates this event when an abort exchange request to an
9823 * rport fails twice in succession with no reply. The abort could be originated
9824 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9825 */
9826void
9827lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9828 struct lpfc_nodelist *ndlp,
9829 struct sli4_wcqe_xri_aborted *axri)
9830{
9831 struct lpfc_vport *vport;
5c1db2ac 9832 uint32_t ext_status = 0;
cb69f7de 9833
6b5151fd 9834 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9835 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9836 "3115 Node Context not found, driver "
9837 "ignoring abts err event\n");
6b5151fd
JS
9838 return;
9839 }
9840
cb69f7de
JS
9841 vport = ndlp->vport;
9842 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9843 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9844 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9845 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9846 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9847 bf_get(lpfc_wcqe_xa_status, axri),
9848 axri->parameter);
cb69f7de 9849
5c1db2ac
JS
9850 /*
9851 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9852 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9853 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9854 */
e3d2b802 9855 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9856 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9857 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9858 lpfc_sli_abts_recover_port(vport, ndlp);
9859}
9860
e59058c4 9861/**
3621a710 9862 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9863 * @phba: Pointer to HBA context object.
9864 * @pring: Pointer to driver SLI ring object.
9865 * @iocbq: Pointer to iocb object.
9866 *
9867 * This function is called by the slow ring event handler
9868 * function when there is an ASYNC event iocb in the ring.
9869 * This function is called with no lock held.
9870 * Currently this function handles only temperature related
9871 * ASYNC events. The function decodes the temperature sensor
9872 * event message and posts events for the management applications.
9873 **/
98c9ea5c 9874static void
57127f15
JS
9875lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9876 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9877{
9878 IOCB_t *icmd;
9879 uint16_t evt_code;
57127f15
JS
9880 struct temp_event temp_event_data;
9881 struct Scsi_Host *shost;
a257bf90 9882 uint32_t *iocb_w;
57127f15
JS
9883
9884 icmd = &iocbq->iocb;
9885 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9886
cb69f7de
JS
9887 switch (evt_code) {
9888 case ASYNC_TEMP_WARN:
9889 case ASYNC_TEMP_SAFE:
9890 temp_event_data.data = (uint32_t) icmd->ulpContext;
9891 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9892 if (evt_code == ASYNC_TEMP_WARN) {
9893 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9894 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9895 "0347 Adapter is very hot, please take "
9896 "corrective action. temperature : %d Celsius\n",
9897 (uint32_t) icmd->ulpContext);
9898 } else {
9899 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9900 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9901 "0340 Adapter temperature is OK now. "
9902 "temperature : %d Celsius\n",
9903 (uint32_t) icmd->ulpContext);
9904 }
9905
9906 /* Send temperature change event to applications */
9907 shost = lpfc_shost_from_vport(phba->pport);
9908 fc_host_post_vendor_event(shost, fc_get_event_number(),
9909 sizeof(temp_event_data), (char *) &temp_event_data,
9910 LPFC_NL_VENDOR_ID);
9911 break;
9912 case ASYNC_STATUS_CN:
9913 lpfc_sli_abts_err_handler(phba, iocbq);
9914 break;
9915 default:
a257bf90 9916 iocb_w = (uint32_t *) icmd;
cb69f7de 9917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9918 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9919 " evt_code 0x%x\n"
a257bf90
JS
9920 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9921 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9922 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9923 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9924 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9925 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9926 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9927 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9928 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9929
cb69f7de 9930 break;
57127f15 9931 }
57127f15
JS
9932}
9933
9934
e59058c4 9935/**
895427bd 9936 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9937 * @phba: Pointer to HBA context object.
9938 *
9939 * lpfc_sli_setup sets up rings of the SLI interface with
9940 * number of iocbs per ring and iotags. This function is
9941 * called while driver attach to the HBA and before the
9942 * interrupts are enabled. So there is no need for locking.
9943 *
9944 * This function always returns 0.
9945 **/
dea3101e 9946int
895427bd
JS
9947lpfc_sli4_setup(struct lpfc_hba *phba)
9948{
9949 struct lpfc_sli_ring *pring;
9950
9951 pring = phba->sli4_hba.els_wq->pring;
9952 pring->num_mask = LPFC_MAX_RING_MASK;
9953 pring->prt[0].profile = 0; /* Mask 0 */
9954 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9955 pring->prt[0].type = FC_TYPE_ELS;
9956 pring->prt[0].lpfc_sli_rcv_unsol_event =
9957 lpfc_els_unsol_event;
9958 pring->prt[1].profile = 0; /* Mask 1 */
9959 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9960 pring->prt[1].type = FC_TYPE_ELS;
9961 pring->prt[1].lpfc_sli_rcv_unsol_event =
9962 lpfc_els_unsol_event;
9963 pring->prt[2].profile = 0; /* Mask 2 */
9964 /* NameServer Inquiry */
9965 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9966 /* NameServer */
9967 pring->prt[2].type = FC_TYPE_CT;
9968 pring->prt[2].lpfc_sli_rcv_unsol_event =
9969 lpfc_ct_unsol_event;
9970 pring->prt[3].profile = 0; /* Mask 3 */
9971 /* NameServer response */
9972 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9973 /* NameServer */
9974 pring->prt[3].type = FC_TYPE_CT;
9975 pring->prt[3].lpfc_sli_rcv_unsol_event =
9976 lpfc_ct_unsol_event;
9977 return 0;
9978}
9979
9980/**
9981 * lpfc_sli_setup - SLI ring setup function
9982 * @phba: Pointer to HBA context object.
9983 *
9984 * lpfc_sli_setup sets up rings of the SLI interface with
9985 * number of iocbs per ring and iotags. This function is
9986 * called while driver attach to the HBA and before the
9987 * interrupts are enabled. So there is no need for locking.
9988 *
9989 * This function always returns 0. SLI3 only.
9990 **/
9991int
dea3101e
JB
9992lpfc_sli_setup(struct lpfc_hba *phba)
9993{
ed957684 9994 int i, totiocbsize = 0;
dea3101e
JB
9995 struct lpfc_sli *psli = &phba->sli;
9996 struct lpfc_sli_ring *pring;
9997
2a76a283 9998 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 9999 psli->sli_flag = 0;
dea3101e 10000
604a3e30
JB
10001 psli->iocbq_lookup = NULL;
10002 psli->iocbq_lookup_len = 0;
10003 psli->last_iotag = 0;
10004
dea3101e 10005 for (i = 0; i < psli->num_rings; i++) {
895427bd 10006 pring = &psli->sli3_ring[i];
dea3101e
JB
10007 switch (i) {
10008 case LPFC_FCP_RING: /* ring 0 - FCP */
10009 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10010 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10011 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10012 pring->sli.sli3.numCiocb +=
10013 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10014 pring->sli.sli3.numRiocb +=
10015 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10016 pring->sli.sli3.numCiocb +=
10017 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10018 pring->sli.sli3.numRiocb +=
10019 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10020 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10021 SLI3_IOCB_CMD_SIZE :
10022 SLI2_IOCB_CMD_SIZE;
7e56aa25 10023 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10024 SLI3_IOCB_RSP_SIZE :
10025 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
10026 pring->iotag_ctr = 0;
10027 pring->iotag_max =
92d7f7b0 10028 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
10029 pring->fast_iotag = pring->iotag_max;
10030 pring->num_mask = 0;
10031 break;
a4bc3379 10032 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 10033 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10034 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10035 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10036 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10037 SLI3_IOCB_CMD_SIZE :
10038 SLI2_IOCB_CMD_SIZE;
7e56aa25 10039 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10040 SLI3_IOCB_RSP_SIZE :
10041 SLI2_IOCB_RSP_SIZE;
2e0fef85 10042 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
10043 pring->num_mask = 0;
10044 break;
10045 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10046 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10047 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10048 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10049 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10050 SLI3_IOCB_CMD_SIZE :
10051 SLI2_IOCB_CMD_SIZE;
7e56aa25 10052 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10053 SLI3_IOCB_RSP_SIZE :
10054 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
10055 pring->fast_iotag = 0;
10056 pring->iotag_ctr = 0;
10057 pring->iotag_max = 4096;
57127f15
JS
10058 pring->lpfc_sli_rcv_async_status =
10059 lpfc_sli_async_event_handler;
6669f9bb 10060 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 10061 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
10062 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10063 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 10064 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 10065 lpfc_els_unsol_event;
dea3101e 10066 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
10067 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10068 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 10069 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 10070 lpfc_els_unsol_event;
dea3101e
JB
10071 pring->prt[2].profile = 0; /* Mask 2 */
10072 /* NameServer Inquiry */
6a9c52cf 10073 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 10074 /* NameServer */
6a9c52cf 10075 pring->prt[2].type = FC_TYPE_CT;
dea3101e 10076 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 10077 lpfc_ct_unsol_event;
dea3101e
JB
10078 pring->prt[3].profile = 0; /* Mask 3 */
10079 /* NameServer response */
6a9c52cf 10080 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 10081 /* NameServer */
6a9c52cf 10082 pring->prt[3].type = FC_TYPE_CT;
dea3101e 10083 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 10084 lpfc_ct_unsol_event;
dea3101e
JB
10085 break;
10086 }
7e56aa25
JS
10087 totiocbsize += (pring->sli.sli3.numCiocb *
10088 pring->sli.sli3.sizeCiocb) +
10089 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 10090 }
ed957684 10091 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 10092 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
10093 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10094 "SLI2 SLIM Data: x%x x%lx\n",
10095 phba->brd_no, totiocbsize,
10096 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 10097 }
cf5bf97e
JW
10098 if (phba->cfg_multi_ring_support == 2)
10099 lpfc_extra_ring_setup(phba);
dea3101e
JB
10100
10101 return 0;
10102}
10103
e59058c4 10104/**
895427bd 10105 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
10106 * @phba: Pointer to HBA context object.
10107 *
895427bd 10108 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
10109 * ring. This function also initializes ring indices of each ring.
10110 * This function is called during the initialization of the SLI
10111 * interface of an HBA.
10112 * This function is called with no lock held and always returns
10113 * 1.
10114 **/
895427bd
JS
10115void
10116lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10117{
10118 struct lpfc_sli *psli;
10119 struct lpfc_sli_ring *pring;
604a3e30 10120 int i;
dea3101e
JB
10121
10122 psli = &phba->sli;
2e0fef85 10123 spin_lock_irq(&phba->hbalock);
dea3101e 10124 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10125 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 10126 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
10127 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10128 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 10129 pring->flag = 0;
895427bd 10130 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
10131 INIT_LIST_HEAD(&pring->txq);
10132 INIT_LIST_HEAD(&pring->txcmplq);
10133 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 10134 spin_lock_init(&pring->ring_lock);
dea3101e 10135 }
895427bd
JS
10136 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10137 pring = phba->sli4_hba.nvme_wq[i]->pring;
10138 pring->flag = 0;
10139 pring->ringno = LPFC_FCP_RING;
10140 INIT_LIST_HEAD(&pring->txq);
10141 INIT_LIST_HEAD(&pring->txcmplq);
10142 INIT_LIST_HEAD(&pring->iocb_continueq);
10143 spin_lock_init(&pring->ring_lock);
10144 }
10145 pring = phba->sli4_hba.els_wq->pring;
10146 pring->flag = 0;
10147 pring->ringno = LPFC_ELS_RING;
10148 INIT_LIST_HEAD(&pring->txq);
10149 INIT_LIST_HEAD(&pring->txcmplq);
10150 INIT_LIST_HEAD(&pring->iocb_continueq);
10151 spin_lock_init(&pring->ring_lock);
dea3101e 10152
895427bd
JS
10153 if (phba->cfg_nvme_io_channel) {
10154 pring = phba->sli4_hba.nvmels_wq->pring;
10155 pring->flag = 0;
10156 pring->ringno = LPFC_ELS_RING;
10157 INIT_LIST_HEAD(&pring->txq);
10158 INIT_LIST_HEAD(&pring->txcmplq);
10159 INIT_LIST_HEAD(&pring->iocb_continueq);
10160 spin_lock_init(&pring->ring_lock);
10161 }
10162
10163 if (phba->cfg_fof) {
10164 pring = phba->sli4_hba.oas_wq->pring;
10165 pring->flag = 0;
10166 pring->ringno = LPFC_FCP_RING;
10167 INIT_LIST_HEAD(&pring->txq);
10168 INIT_LIST_HEAD(&pring->txcmplq);
10169 INIT_LIST_HEAD(&pring->iocb_continueq);
10170 spin_lock_init(&pring->ring_lock);
10171 }
10172
10173 spin_unlock_irq(&phba->hbalock);
10174}
10175
10176/**
10177 * lpfc_sli_queue_init - Queue initialization function
10178 * @phba: Pointer to HBA context object.
10179 *
10180 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10181 * ring. This function also initializes ring indices of each ring.
10182 * This function is called during the initialization of the SLI
10183 * interface of an HBA.
10184 * This function is called with no lock held and always returns
10185 * 1.
10186 **/
10187void
10188lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10189{
10190 struct lpfc_sli *psli;
10191 struct lpfc_sli_ring *pring;
604a3e30 10192 int i;
dea3101e
JB
10193
10194 psli = &phba->sli;
2e0fef85 10195 spin_lock_irq(&phba->hbalock);
dea3101e 10196 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10197 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
10198 /* Initialize list headers for txq and txcmplq as double linked lists */
10199 for (i = 0; i < psli->num_rings; i++) {
895427bd 10200 pring = &psli->sli3_ring[i];
dea3101e 10201 pring->ringno = i;
7e56aa25
JS
10202 pring->sli.sli3.next_cmdidx = 0;
10203 pring->sli.sli3.local_getidx = 0;
10204 pring->sli.sli3.cmdidx = 0;
dea3101e 10205 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 10206 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 10207 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
10208 pring->flag = 0;
10209 INIT_LIST_HEAD(&pring->txq);
10210 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 10211 spin_lock_init(&pring->ring_lock);
dea3101e 10212 }
2e0fef85 10213 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10214}
10215
04c68496
JS
10216/**
10217 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10218 * @phba: Pointer to HBA context object.
10219 *
10220 * This routine flushes the mailbox command subsystem. It will unconditionally
10221 * flush all the mailbox commands in the three possible stages in the mailbox
10222 * command sub-system: pending mailbox command queue; the outstanding mailbox
10223 * command; and completed mailbox command queue. It is caller's responsibility
10224 * to make sure that the driver is in the proper state to flush the mailbox
10225 * command sub-system. Namely, the posting of mailbox commands into the
10226 * pending mailbox command queue from the various clients must be stopped;
10227 * either the HBA is in a state that it will never works on the outstanding
10228 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10229 * mailbox command has been completed.
10230 **/
10231static void
10232lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10233{
10234 LIST_HEAD(completions);
10235 struct lpfc_sli *psli = &phba->sli;
10236 LPFC_MBOXQ_t *pmb;
10237 unsigned long iflag;
10238
10239 /* Flush all the mailbox commands in the mbox system */
10240 spin_lock_irqsave(&phba->hbalock, iflag);
10241 /* The pending mailbox command queue */
10242 list_splice_init(&phba->sli.mboxq, &completions);
10243 /* The outstanding active mailbox command */
10244 if (psli->mbox_active) {
10245 list_add_tail(&psli->mbox_active->list, &completions);
10246 psli->mbox_active = NULL;
10247 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10248 }
10249 /* The completed mailbox command queue */
10250 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10251 spin_unlock_irqrestore(&phba->hbalock, iflag);
10252
10253 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10254 while (!list_empty(&completions)) {
10255 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10256 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10257 if (pmb->mbox_cmpl)
10258 pmb->mbox_cmpl(phba, pmb);
10259 }
10260}
10261
e59058c4 10262/**
3621a710 10263 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10264 * @vport: Pointer to virtual port object.
10265 *
10266 * lpfc_sli_host_down is called to clean up the resources
10267 * associated with a vport before destroying virtual
10268 * port data structures.
10269 * This function does following operations:
10270 * - Free discovery resources associated with this virtual
10271 * port.
10272 * - Free iocbs associated with this virtual port in
10273 * the txq.
10274 * - Send abort for all iocb commands associated with this
10275 * vport in txcmplq.
10276 *
10277 * This function is called with no lock held and always returns 1.
10278 **/
92d7f7b0
JS
10279int
10280lpfc_sli_host_down(struct lpfc_vport *vport)
10281{
858c9f6c 10282 LIST_HEAD(completions);
92d7f7b0
JS
10283 struct lpfc_hba *phba = vport->phba;
10284 struct lpfc_sli *psli = &phba->sli;
895427bd 10285 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10286 struct lpfc_sli_ring *pring;
10287 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10288 int i;
10289 unsigned long flags = 0;
10290 uint16_t prev_pring_flag;
10291
10292 lpfc_cleanup_discovery_resources(vport);
10293
10294 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10295
895427bd
JS
10296 /*
10297 * Error everything on the txq since these iocbs
10298 * have not been given to the FW yet.
10299 * Also issue ABTS for everything on the txcmplq
10300 */
10301 if (phba->sli_rev != LPFC_SLI_REV4) {
10302 for (i = 0; i < psli->num_rings; i++) {
10303 pring = &psli->sli3_ring[i];
10304 prev_pring_flag = pring->flag;
10305 /* Only slow rings */
10306 if (pring->ringno == LPFC_ELS_RING) {
10307 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10308 /* Set the lpfc data pending flag */
10309 set_bit(LPFC_DATA_READY, &phba->data_flags);
10310 }
10311 list_for_each_entry_safe(iocb, next_iocb,
10312 &pring->txq, list) {
10313 if (iocb->vport != vport)
10314 continue;
10315 list_move_tail(&iocb->list, &completions);
10316 }
10317 list_for_each_entry_safe(iocb, next_iocb,
10318 &pring->txcmplq, list) {
10319 if (iocb->vport != vport)
10320 continue;
10321 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10322 }
10323 pring->flag = prev_pring_flag;
10324 }
10325 } else {
10326 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10327 pring = qp->pring;
10328 if (!pring)
92d7f7b0 10329 continue;
895427bd
JS
10330 if (pring == phba->sli4_hba.els_wq->pring) {
10331 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10332 /* Set the lpfc data pending flag */
10333 set_bit(LPFC_DATA_READY, &phba->data_flags);
10334 }
10335 prev_pring_flag = pring->flag;
10336 spin_lock_irq(&pring->ring_lock);
10337 list_for_each_entry_safe(iocb, next_iocb,
10338 &pring->txq, list) {
10339 if (iocb->vport != vport)
10340 continue;
10341 list_move_tail(&iocb->list, &completions);
10342 }
10343 spin_unlock_irq(&pring->ring_lock);
10344 list_for_each_entry_safe(iocb, next_iocb,
10345 &pring->txcmplq, list) {
10346 if (iocb->vport != vport)
10347 continue;
10348 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10349 }
10350 pring->flag = prev_pring_flag;
92d7f7b0 10351 }
92d7f7b0 10352 }
92d7f7b0
JS
10353 spin_unlock_irqrestore(&phba->hbalock, flags);
10354
a257bf90
JS
10355 /* Cancel all the IOCBs from the completions list */
10356 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10357 IOERR_SLI_DOWN);
92d7f7b0
JS
10358 return 1;
10359}
10360
e59058c4 10361/**
3621a710 10362 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10363 * @phba: Pointer to HBA context object.
10364 *
10365 * This function cleans up all iocb, buffers, mailbox commands
10366 * while shutting down the HBA. This function is called with no
10367 * lock held and always returns 1.
10368 * This function does the following to cleanup driver resources:
10369 * - Free discovery resources for each virtual port
10370 * - Cleanup any pending fabric iocbs
10371 * - Iterate through the iocb txq and free each entry
10372 * in the list.
10373 * - Free up any buffer posted to the HBA
10374 * - Free mailbox commands in the mailbox queue.
10375 **/
dea3101e 10376int
2e0fef85 10377lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10378{
2534ba75 10379 LIST_HEAD(completions);
2e0fef85 10380 struct lpfc_sli *psli = &phba->sli;
895427bd 10381 struct lpfc_queue *qp = NULL;
dea3101e 10382 struct lpfc_sli_ring *pring;
0ff10d46 10383 struct lpfc_dmabuf *buf_ptr;
dea3101e 10384 unsigned long flags = 0;
04c68496
JS
10385 int i;
10386
10387 /* Shutdown the mailbox command sub-system */
618a5230 10388 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10389
dea3101e
JB
10390 lpfc_hba_down_prep(phba);
10391
92d7f7b0
JS
10392 lpfc_fabric_abort_hba(phba);
10393
2e0fef85 10394 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10395
895427bd
JS
10396 /*
10397 * Error everything on the txq since these iocbs
10398 * have not been given to the FW yet.
10399 */
10400 if (phba->sli_rev != LPFC_SLI_REV4) {
10401 for (i = 0; i < psli->num_rings; i++) {
10402 pring = &psli->sli3_ring[i];
10403 /* Only slow rings */
10404 if (pring->ringno == LPFC_ELS_RING) {
10405 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10406 /* Set the lpfc data pending flag */
10407 set_bit(LPFC_DATA_READY, &phba->data_flags);
10408 }
10409 list_splice_init(&pring->txq, &completions);
10410 }
10411 } else {
10412 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10413 pring = qp->pring;
10414 if (!pring)
10415 continue;
10416 spin_lock_irq(&pring->ring_lock);
10417 list_splice_init(&pring->txq, &completions);
10418 spin_unlock_irq(&pring->ring_lock);
10419 if (pring == phba->sli4_hba.els_wq->pring) {
10420 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10421 /* Set the lpfc data pending flag */
10422 set_bit(LPFC_DATA_READY, &phba->data_flags);
10423 }
10424 }
2534ba75 10425 }
2e0fef85 10426 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10427
a257bf90
JS
10428 /* Cancel all the IOCBs from the completions list */
10429 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10430 IOERR_SLI_DOWN);
dea3101e 10431
0ff10d46
JS
10432 spin_lock_irqsave(&phba->hbalock, flags);
10433 list_splice_init(&phba->elsbuf, &completions);
10434 phba->elsbuf_cnt = 0;
10435 phba->elsbuf_prev_cnt = 0;
10436 spin_unlock_irqrestore(&phba->hbalock, flags);
10437
10438 while (!list_empty(&completions)) {
10439 list_remove_head(&completions, buf_ptr,
10440 struct lpfc_dmabuf, list);
10441 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10442 kfree(buf_ptr);
10443 }
10444
dea3101e
JB
10445 /* Return any active mbox cmds */
10446 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10447
da0436e9 10448 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10449 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10450 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10451
da0436e9
JS
10452 return 1;
10453}
10454
e59058c4 10455/**
3621a710 10456 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10457 * @srcp: Source memory pointer.
10458 * @destp: Destination memory pointer.
10459 * @cnt: Number of words required to be copied.
10460 *
10461 * This function is used for copying data between driver memory
10462 * and the SLI memory. This function also changes the endianness
10463 * of each word if native endianness is different from SLI
10464 * endianness. This function can be called with or without
10465 * lock.
10466 **/
dea3101e
JB
10467void
10468lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10469{
10470 uint32_t *src = srcp;
10471 uint32_t *dest = destp;
10472 uint32_t ldata;
10473 int i;
10474
10475 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10476 ldata = *src;
10477 ldata = le32_to_cpu(ldata);
10478 *dest = ldata;
10479 src++;
10480 dest++;
10481 }
10482}
10483
e59058c4 10484
a0c87cbd
JS
10485/**
10486 * lpfc_sli_bemem_bcopy - SLI memory copy function
10487 * @srcp: Source memory pointer.
10488 * @destp: Destination memory pointer.
10489 * @cnt: Number of words required to be copied.
10490 *
10491 * This function is used for copying data between a data structure
10492 * with big endian representation to local endianness.
10493 * This function can be called with or without lock.
10494 **/
10495void
10496lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10497{
10498 uint32_t *src = srcp;
10499 uint32_t *dest = destp;
10500 uint32_t ldata;
10501 int i;
10502
10503 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10504 ldata = *src;
10505 ldata = be32_to_cpu(ldata);
10506 *dest = ldata;
10507 src++;
10508 dest++;
10509 }
10510}
10511
e59058c4 10512/**
3621a710 10513 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10514 * @phba: Pointer to HBA context object.
10515 * @pring: Pointer to driver SLI ring object.
10516 * @mp: Pointer to driver buffer object.
10517 *
10518 * This function is called with no lock held.
10519 * It always return zero after adding the buffer to the postbufq
10520 * buffer list.
10521 **/
dea3101e 10522int
2e0fef85
JS
10523lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10524 struct lpfc_dmabuf *mp)
dea3101e
JB
10525{
10526 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10527 later */
2e0fef85 10528 spin_lock_irq(&phba->hbalock);
dea3101e 10529 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10530 pring->postbufq_cnt++;
2e0fef85 10531 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10532 return 0;
10533}
10534
e59058c4 10535/**
3621a710 10536 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10537 * @phba: Pointer to HBA context object.
10538 *
10539 * When HBQ is enabled, buffers are searched based on tags. This function
10540 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10541 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10542 * does not conflict with tags of buffer posted for unsolicited events.
10543 * The function returns the allocated tag. The function is called with
10544 * no locks held.
10545 **/
76bb24ef
JS
10546uint32_t
10547lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10548{
10549 spin_lock_irq(&phba->hbalock);
10550 phba->buffer_tag_count++;
10551 /*
10552 * Always set the QUE_BUFTAG_BIT to distiguish between
10553 * a tag assigned by HBQ.
10554 */
10555 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10556 spin_unlock_irq(&phba->hbalock);
10557 return phba->buffer_tag_count;
10558}
10559
e59058c4 10560/**
3621a710 10561 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10562 * @phba: Pointer to HBA context object.
10563 * @pring: Pointer to driver SLI ring object.
10564 * @tag: Buffer tag.
10565 *
10566 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10567 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10568 * iocb is posted to the response ring with the tag of the buffer.
10569 * This function searches the pring->postbufq list using the tag
10570 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10571 * iocb. If the buffer is found then lpfc_dmabuf object of the
10572 * buffer is returned to the caller else NULL is returned.
10573 * This function is called with no lock held.
10574 **/
76bb24ef
JS
10575struct lpfc_dmabuf *
10576lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10577 uint32_t tag)
10578{
10579 struct lpfc_dmabuf *mp, *next_mp;
10580 struct list_head *slp = &pring->postbufq;
10581
25985edc 10582 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10583 spin_lock_irq(&phba->hbalock);
10584 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10585 if (mp->buffer_tag == tag) {
10586 list_del_init(&mp->list);
10587 pring->postbufq_cnt--;
10588 spin_unlock_irq(&phba->hbalock);
10589 return mp;
10590 }
10591 }
10592
10593 spin_unlock_irq(&phba->hbalock);
10594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10595 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10596 "ring %d Data x%lx x%p x%p x%x\n",
10597 pring->ringno, (unsigned long) tag,
10598 slp->next, slp->prev, pring->postbufq_cnt);
10599
10600 return NULL;
10601}
dea3101e 10602
e59058c4 10603/**
3621a710 10604 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10605 * @phba: Pointer to HBA context object.
10606 * @pring: Pointer to driver SLI ring object.
10607 * @phys: DMA address of the buffer.
10608 *
10609 * This function searches the buffer list using the dma_address
10610 * of unsolicited event to find the driver's lpfc_dmabuf object
10611 * corresponding to the dma_address. The function returns the
10612 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10613 * This function is called by the ct and els unsolicited event
10614 * handlers to get the buffer associated with the unsolicited
10615 * event.
10616 *
10617 * This function is called with no lock held.
10618 **/
dea3101e
JB
10619struct lpfc_dmabuf *
10620lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10621 dma_addr_t phys)
10622{
10623 struct lpfc_dmabuf *mp, *next_mp;
10624 struct list_head *slp = &pring->postbufq;
10625
25985edc 10626 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10627 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10628 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10629 if (mp->phys == phys) {
10630 list_del_init(&mp->list);
10631 pring->postbufq_cnt--;
2e0fef85 10632 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10633 return mp;
10634 }
10635 }
10636
2e0fef85 10637 spin_unlock_irq(&phba->hbalock);
dea3101e 10638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10639 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10640 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10641 pring->ringno, (unsigned long long)phys,
dea3101e
JB
10642 slp->next, slp->prev, pring->postbufq_cnt);
10643 return NULL;
10644}
10645
e59058c4 10646/**
3621a710 10647 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10648 * @phba: Pointer to HBA context object.
10649 * @cmdiocb: Pointer to driver command iocb object.
10650 * @rspiocb: Pointer to driver response iocb object.
10651 *
10652 * This function is the completion handler for the abort iocbs for
10653 * ELS commands. This function is called from the ELS ring event
10654 * handler with no lock held. This function frees memory resources
10655 * associated with the abort iocb.
10656 **/
dea3101e 10657static void
2e0fef85
JS
10658lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10659 struct lpfc_iocbq *rspiocb)
dea3101e 10660{
2e0fef85 10661 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10662 uint16_t abort_iotag, abort_context;
ff78d8f9 10663 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10664
10665 if (irsp->ulpStatus) {
ff78d8f9
JS
10666
10667 /*
10668 * Assume that the port already completed and returned, or
10669 * will return the iocb. Just Log the message.
10670 */
2680eeaa
JS
10671 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10672 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10673
2e0fef85 10674 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10675 if (phba->sli_rev < LPFC_SLI_REV4) {
10676 if (abort_iotag != 0 &&
10677 abort_iotag <= phba->sli.last_iotag)
10678 abort_iocb =
10679 phba->sli.iocbq_lookup[abort_iotag];
10680 } else
10681 /* For sli4 the abort_tag is the XRI,
10682 * so the abort routine puts the iotag of the iocb
10683 * being aborted in the context field of the abort
10684 * IOCB.
10685 */
10686 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10687
2a9bf3d0
JS
10688 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10689 "0327 Cannot abort els iocb %p "
10690 "with tag %x context %x, abort status %x, "
10691 "abort code %x\n",
10692 abort_iocb, abort_iotag, abort_context,
10693 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10694
ff78d8f9 10695 spin_unlock_irq(&phba->hbalock);
2680eeaa 10696 }
604a3e30 10697 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
10698 return;
10699}
10700
e59058c4 10701/**
3621a710 10702 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10703 * @phba: Pointer to HBA context object.
10704 * @cmdiocb: Pointer to driver command iocb object.
10705 * @rspiocb: Pointer to driver response iocb object.
10706 *
10707 * The function is called from SLI ring event handler with no
10708 * lock held. This function is the completion handler for ELS commands
10709 * which are aborted. The function frees memory resources used for
10710 * the aborted ELS commands.
10711 **/
92d7f7b0
JS
10712static void
10713lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10714 struct lpfc_iocbq *rspiocb)
10715{
10716 IOCB_t *irsp = &rspiocb->iocb;
10717
10718 /* ELS cmd tag <ulpIoTag> completes */
10719 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10720 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10721 "x%x x%x x%x\n",
e8b62011 10722 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10723 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10724 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10725 lpfc_ct_free_iocb(phba, cmdiocb);
10726 else
10727 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10728 return;
10729}
10730
e59058c4 10731/**
5af5eee7 10732 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10733 * @phba: Pointer to HBA context object.
10734 * @pring: Pointer to driver SLI ring object.
10735 * @cmdiocb: Pointer to driver command iocb object.
10736 *
5af5eee7
JS
10737 * This function issues an abort iocb for the provided command iocb down to
10738 * the port. Other than the case the outstanding command iocb is an abort
10739 * request, this function issues abort out unconditionally. This function is
10740 * called with hbalock held. The function returns 0 when it fails due to
10741 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10742 **/
5af5eee7
JS
10743static int
10744lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10745 struct lpfc_iocbq *cmdiocb)
dea3101e 10746{
2e0fef85 10747 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10748 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
10749 IOCB_t *icmd = NULL;
10750 IOCB_t *iabt = NULL;
5af5eee7 10751 int retval;
7e56aa25 10752 unsigned long iflags;
07951076 10753
1c2ba475
JT
10754 lockdep_assert_held(&phba->hbalock);
10755
92d7f7b0
JS
10756 /*
10757 * There are certain command types we don't want to abort. And we
10758 * don't want to abort commands that are already in the process of
10759 * being aborted.
07951076
JS
10760 */
10761 icmd = &cmdiocb->iocb;
2e0fef85 10762 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10763 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10764 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10765 return 0;
10766
dea3101e 10767 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10768 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
10769 if (abtsiocbp == NULL)
10770 return 0;
dea3101e 10771
07951076 10772 /* This signals the response to set the correct status
341af102 10773 * before calling the completion handler
07951076
JS
10774 */
10775 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10776
dea3101e 10777 iabt = &abtsiocbp->iocb;
07951076
JS
10778 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10779 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10780 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10781 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10782 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10783 }
da0436e9
JS
10784 else
10785 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10786 iabt->ulpLe = 1;
10787 iabt->ulpClass = icmd->ulpClass;
dea3101e 10788
5ffc266e 10789 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10790 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10791 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10792 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10793 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10794 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10795
2e0fef85 10796 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10797 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10798 else
10799 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10800
07951076 10801 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10802 abtsiocbp->vport = vport;
5b8bd0c9 10803
e8b62011
JS
10804 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10805 "0339 Abort xri x%x, original iotag x%x, "
10806 "abort cmd iotag x%x\n",
2a9bf3d0 10807 iabt->un.acxri.abortIoTag,
e8b62011 10808 iabt->un.acxri.abortContextTag,
2a9bf3d0 10809 abtsiocbp->iotag);
7e56aa25
JS
10810
10811 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10812 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10813 if (unlikely(pring == NULL))
9bd2bff5 10814 return 0;
7e56aa25
JS
10815 /* Note: both hbalock and ring_lock need to be set here */
10816 spin_lock_irqsave(&pring->ring_lock, iflags);
10817 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10818 abtsiocbp, 0);
10819 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10820 } else {
10821 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10822 abtsiocbp, 0);
10823 }
dea3101e 10824
d7c255b2
JS
10825 if (retval)
10826 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10827
10828 /*
10829 * Caller to this routine should check for IOCB_ERROR
10830 * and handle it properly. This routine no longer removes
10831 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10832 */
10833 return retval;
10834}
10835
10836/**
10837 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10838 * @phba: Pointer to HBA context object.
10839 * @pring: Pointer to driver SLI ring object.
10840 * @cmdiocb: Pointer to driver command iocb object.
10841 *
10842 * This function issues an abort iocb for the provided command iocb. In case
10843 * of unloading, the abort iocb will not be issued to commands on the ELS
10844 * ring. Instead, the callback function shall be changed to those commands
10845 * so that nothing happens when them finishes. This function is called with
10846 * hbalock held. The function returns 0 when the command iocb is an abort
10847 * request.
10848 **/
10849int
10850lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10851 struct lpfc_iocbq *cmdiocb)
10852{
10853 struct lpfc_vport *vport = cmdiocb->vport;
10854 int retval = IOCB_ERROR;
10855 IOCB_t *icmd = NULL;
10856
1c2ba475
JT
10857 lockdep_assert_held(&phba->hbalock);
10858
5af5eee7
JS
10859 /*
10860 * There are certain command types we don't want to abort. And we
10861 * don't want to abort commands that are already in the process of
10862 * being aborted.
10863 */
10864 icmd = &cmdiocb->iocb;
10865 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10866 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10867 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10868 return 0;
10869
1234a6d5
DK
10870 if (!pring) {
10871 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10872 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10873 else
10874 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10875 goto abort_iotag_exit;
10876 }
10877
5af5eee7
JS
10878 /*
10879 * If we're unloading, don't abort iocb on the ELS ring, but change
10880 * the callback so that nothing happens when it finishes.
10881 */
10882 if ((vport->load_flag & FC_UNLOADING) &&
10883 (pring->ringno == LPFC_ELS_RING)) {
10884 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10885 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10886 else
10887 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10888 goto abort_iotag_exit;
10889 }
10890
10891 /* Now, we try to issue the abort to the cmdiocb out */
10892 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10893
07951076 10894abort_iotag_exit:
2e0fef85
JS
10895 /*
10896 * Caller to this routine should check for IOCB_ERROR
10897 * and handle it properly. This routine no longer removes
10898 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10899 */
2e0fef85 10900 return retval;
dea3101e
JB
10901}
10902
895427bd
JS
10903/**
10904 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10905 * @phba: Pointer to HBA context object.
10906 * @pring: Pointer to driver SLI ring object.
10907 * @cmdiocb: Pointer to driver command iocb object.
10908 *
10909 * This function issues an abort iocb for the provided command iocb down to
10910 * the port. Other than the case the outstanding command iocb is an abort
10911 * request, this function issues abort out unconditionally. This function is
10912 * called with hbalock held. The function returns 0 when it fails due to
10913 * memory allocation failure or when the command iocb is an abort request.
10914 **/
10915static int
10916lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10917 struct lpfc_iocbq *cmdiocb)
10918{
10919 struct lpfc_vport *vport = cmdiocb->vport;
10920 struct lpfc_iocbq *abtsiocbp;
205e8240 10921 union lpfc_wqe128 *abts_wqe;
895427bd
JS
10922 int retval;
10923
10924 /*
10925 * There are certain command types we don't want to abort. And we
10926 * don't want to abort commands that are already in the process of
10927 * being aborted.
10928 */
10929 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10930 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10931 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10932 return 0;
10933
10934 /* issue ABTS for this io based on iotag */
10935 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10936 if (abtsiocbp == NULL)
10937 return 0;
10938
10939 /* This signals the response to set the correct status
10940 * before calling the completion handler
10941 */
10942 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10943
10944 /* Complete prepping the abort wqe and issue to the FW. */
10945 abts_wqe = &abtsiocbp->wqe;
10946 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10947 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10948
10949 /* Explicitly set reserved fields to zero.*/
10950 abts_wqe->abort_cmd.rsrvd4 = 0;
10951 abts_wqe->abort_cmd.rsrvd5 = 0;
10952
10953 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10954 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10955 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10956
10957 /* word 7 */
10958 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10959 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10960 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10961 cmdiocb->iocb.ulpClass);
10962
10963 /* word 8 - tell the FW to abort the IO associated with this
10964 * outstanding exchange ID.
10965 */
10966 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10967
10968 /* word 9 - this is the iotag for the abts_wqe completion. */
10969 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10970 abtsiocbp->iotag);
10971
10972 /* word 10 */
10973 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10974 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10975 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10976
10977 /* word 11 */
10978 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10979 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10980 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10981
10982 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10983 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10984 abtsiocbp->vport = vport;
01649561 10985 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 10986 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 10987 if (retval) {
895427bd
JS
10988 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10989 "6147 Failed abts issue_wqe with status x%x "
10990 "for oxid x%x\n",
10991 retval, cmdiocb->sli4_xritag);
10992 lpfc_sli_release_iocbq(phba, abtsiocbp);
10993 return retval;
10994 }
10995
10996 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10997 "6148 Drv Abort NVME Request Issued for "
10998 "ox_id x%x on reqtag x%x\n",
10999 cmdiocb->sli4_xritag,
11000 abtsiocbp->iotag);
11001
11002 return retval;
11003}
11004
5af5eee7
JS
11005/**
11006 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11007 * @phba: pointer to lpfc HBA data structure.
11008 *
11009 * This routine will abort all pending and outstanding iocbs to an HBA.
11010 **/
11011void
11012lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11013{
11014 struct lpfc_sli *psli = &phba->sli;
11015 struct lpfc_sli_ring *pring;
895427bd 11016 struct lpfc_queue *qp = NULL;
5af5eee7
JS
11017 int i;
11018
895427bd
JS
11019 if (phba->sli_rev != LPFC_SLI_REV4) {
11020 for (i = 0; i < psli->num_rings; i++) {
11021 pring = &psli->sli3_ring[i];
11022 lpfc_sli_abort_iocb_ring(phba, pring);
11023 }
11024 return;
11025 }
11026 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11027 pring = qp->pring;
11028 if (!pring)
11029 continue;
db55fba8 11030 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
11031 }
11032}
11033
e59058c4 11034/**
3621a710 11035 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
11036 * @iocbq: Pointer to driver iocb object.
11037 * @vport: Pointer to driver virtual port object.
11038 * @tgt_id: SCSI ID of the target.
11039 * @lun_id: LUN ID of the scsi device.
11040 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11041 *
3621a710 11042 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
11043 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11044 * 0 if the filtering criteria is met for the given iocb and will return
11045 * 1 if the filtering criteria is not met.
11046 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11047 * given iocb is for the SCSI device specified by vport, tgt_id and
11048 * lun_id parameter.
11049 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11050 * given iocb is for the SCSI target specified by vport and tgt_id
11051 * parameters.
11052 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11053 * given iocb is for the SCSI host associated with the given vport.
11054 * This function is called with no locks held.
11055 **/
dea3101e 11056static int
51ef4c26
JS
11057lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11058 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 11059 lpfc_ctx_cmd ctx_cmd)
dea3101e 11060{
0bd4ca25 11061 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
11062 int rc = 1;
11063
0bd4ca25
JSEC
11064 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11065 return rc;
11066
51ef4c26
JS
11067 if (iocbq->vport != vport)
11068 return rc;
11069
0bd4ca25 11070 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 11071
495a714c 11072 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
11073 return rc;
11074
11075 switch (ctx_cmd) {
11076 case LPFC_CTX_LUN:
495a714c
JS
11077 if ((lpfc_cmd->rdata->pnode) &&
11078 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11079 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
11080 rc = 0;
11081 break;
11082 case LPFC_CTX_TGT:
495a714c
JS
11083 if ((lpfc_cmd->rdata->pnode) &&
11084 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
11085 rc = 0;
11086 break;
dea3101e
JB
11087 case LPFC_CTX_HOST:
11088 rc = 0;
11089 break;
11090 default:
11091 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 11092 __func__, ctx_cmd);
dea3101e
JB
11093 break;
11094 }
11095
11096 return rc;
11097}
11098
e59058c4 11099/**
3621a710 11100 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
11101 * @vport: Pointer to virtual port.
11102 * @tgt_id: SCSI ID of the target.
11103 * @lun_id: LUN ID of the scsi device.
11104 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11105 *
11106 * This function returns number of FCP commands pending for the vport.
11107 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11108 * commands pending on the vport associated with SCSI device specified
11109 * by tgt_id and lun_id parameters.
11110 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11111 * commands pending on the vport associated with SCSI target specified
11112 * by tgt_id parameter.
11113 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11114 * commands pending on the vport.
11115 * This function returns the number of iocbs which satisfy the filter.
11116 * This function is called without any lock held.
11117 **/
dea3101e 11118int
51ef4c26
JS
11119lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11120 lpfc_ctx_cmd ctx_cmd)
dea3101e 11121{
51ef4c26 11122 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11123 struct lpfc_iocbq *iocbq;
11124 int sum, i;
dea3101e 11125
31979008 11126 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
11127 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11128 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11129
51ef4c26
JS
11130 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11131 ctx_cmd) == 0)
0bd4ca25 11132 sum++;
dea3101e 11133 }
31979008 11134 spin_unlock_irq(&phba->hbalock);
0bd4ca25 11135
dea3101e
JB
11136 return sum;
11137}
11138
e59058c4 11139/**
3621a710 11140 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
11141 * @phba: Pointer to HBA context object
11142 * @cmdiocb: Pointer to command iocb object.
11143 * @rspiocb: Pointer to response iocb object.
11144 *
11145 * This function is called when an aborted FCP iocb completes. This
11146 * function is called by the ring event handler with no lock held.
11147 * This function frees the iocb.
11148 **/
5eb95af0 11149void
2e0fef85
JS
11150lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11151 struct lpfc_iocbq *rspiocb)
5eb95af0 11152{
cb69f7de 11153 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 11154 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
11155 "original iotag x%x, abort cmd iotag x%x "
11156 "status 0x%x, reason 0x%x\n",
11157 cmdiocb->iocb.un.acxri.abortContextTag,
11158 cmdiocb->iocb.un.acxri.abortIoTag,
11159 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11160 rspiocb->iocb.un.ulpWord[4]);
604a3e30 11161 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
11162 return;
11163}
11164
e59058c4 11165/**
3621a710 11166 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
11167 * @vport: Pointer to virtual port.
11168 * @pring: Pointer to driver SLI ring object.
11169 * @tgt_id: SCSI ID of the target.
11170 * @lun_id: LUN ID of the scsi device.
11171 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11172 *
11173 * This function sends an abort command for every SCSI command
11174 * associated with the given virtual port pending on the ring
11175 * filtered by lpfc_sli_validate_fcp_iocb function.
11176 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11177 * FCP iocbs associated with lun specified by tgt_id and lun_id
11178 * parameters
11179 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11180 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11181 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11182 * FCP iocbs associated with virtual port.
11183 * This function returns number of iocbs it failed to abort.
11184 * This function is called with no locks held.
11185 **/
dea3101e 11186int
51ef4c26
JS
11187lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11188 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 11189{
51ef4c26 11190 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11191 struct lpfc_iocbq *iocbq;
11192 struct lpfc_iocbq *abtsiocb;
ecbb227e 11193 struct lpfc_sli_ring *pring_s4;
dea3101e 11194 IOCB_t *cmd = NULL;
dea3101e 11195 int errcnt = 0, ret_val = 0;
0bd4ca25 11196 int i;
dea3101e 11197
0bd4ca25
JSEC
11198 for (i = 1; i <= phba->sli.last_iotag; i++) {
11199 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11200
51ef4c26 11201 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 11202 abort_cmd) != 0)
dea3101e
JB
11203 continue;
11204
afbd8d88
JS
11205 /*
11206 * If the iocbq is already being aborted, don't take a second
11207 * action, but do count it.
11208 */
11209 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11210 continue;
11211
dea3101e 11212 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 11213 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
11214 if (abtsiocb == NULL) {
11215 errcnt++;
11216 continue;
11217 }
dea3101e 11218
afbd8d88
JS
11219 /* indicate the IO is being aborted by the driver. */
11220 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11221
0bd4ca25 11222 cmd = &iocbq->iocb;
dea3101e
JB
11223 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11224 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11225 if (phba->sli_rev == LPFC_SLI_REV4)
11226 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11227 else
11228 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
11229 abtsiocb->iocb.ulpLe = 1;
11230 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11231 abtsiocb->vport = vport;
dea3101e 11232
5ffc266e 11233 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11234 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11235 if (iocbq->iocb_flag & LPFC_IO_FCP)
11236 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11237 if (iocbq->iocb_flag & LPFC_IO_FOF)
11238 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11239
2e0fef85 11240 if (lpfc_is_link_up(phba))
dea3101e
JB
11241 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11242 else
11243 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11244
5eb95af0
JSEC
11245 /* Setup callback routine and issue the command. */
11246 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11247 if (phba->sli_rev == LPFC_SLI_REV4) {
11248 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11249 if (!pring_s4)
11250 continue;
11251 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11252 abtsiocb, 0);
11253 } else
11254 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11255 abtsiocb, 0);
dea3101e 11256 if (ret_val == IOCB_ERROR) {
604a3e30 11257 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
11258 errcnt++;
11259 continue;
11260 }
11261 }
11262
11263 return errcnt;
11264}
11265
98912dda
JS
11266/**
11267 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11268 * @vport: Pointer to virtual port.
11269 * @pring: Pointer to driver SLI ring object.
11270 * @tgt_id: SCSI ID of the target.
11271 * @lun_id: LUN ID of the scsi device.
11272 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11273 *
11274 * This function sends an abort command for every SCSI command
11275 * associated with the given virtual port pending on the ring
11276 * filtered by lpfc_sli_validate_fcp_iocb function.
11277 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11278 * FCP iocbs associated with lun specified by tgt_id and lun_id
11279 * parameters
11280 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11281 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11282 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11283 * FCP iocbs associated with virtual port.
11284 * This function returns number of iocbs it aborted .
11285 * This function is called with no locks held right after a taskmgmt
11286 * command is sent.
11287 **/
11288int
11289lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11290 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11291{
11292 struct lpfc_hba *phba = vport->phba;
8c50d25c 11293 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11294 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11295 struct lpfc_nodelist *ndlp;
98912dda
JS
11296 struct lpfc_iocbq *iocbq;
11297 IOCB_t *icmd;
11298 int sum, i, ret_val;
11299 unsigned long iflags;
11300 struct lpfc_sli_ring *pring_s4;
98912dda
JS
11301
11302 spin_lock_irq(&phba->hbalock);
11303
11304 /* all I/Os are in process of being flushed */
11305 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11306 spin_unlock_irq(&phba->hbalock);
11307 return 0;
11308 }
11309 sum = 0;
11310
11311 for (i = 1; i <= phba->sli.last_iotag; i++) {
11312 iocbq = phba->sli.iocbq_lookup[i];
11313
11314 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11315 cmd) != 0)
11316 continue;
11317
11318 /*
11319 * If the iocbq is already being aborted, don't take a second
11320 * action, but do count it.
11321 */
11322 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11323 continue;
11324
11325 /* issue ABTS for this IOCB based on iotag */
11326 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11327 if (abtsiocbq == NULL)
11328 continue;
11329
11330 icmd = &iocbq->iocb;
11331 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11332 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11333 if (phba->sli_rev == LPFC_SLI_REV4)
11334 abtsiocbq->iocb.un.acxri.abortIoTag =
11335 iocbq->sli4_xritag;
11336 else
11337 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11338 abtsiocbq->iocb.ulpLe = 1;
11339 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11340 abtsiocbq->vport = vport;
11341
11342 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11343 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11344 if (iocbq->iocb_flag & LPFC_IO_FCP)
11345 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11346 if (iocbq->iocb_flag & LPFC_IO_FOF)
11347 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11348
8c50d25c
JS
11349 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11350 ndlp = lpfc_cmd->rdata->pnode;
11351
11352 if (lpfc_is_link_up(phba) &&
11353 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11354 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11355 else
11356 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11357
11358 /* Setup callback routine and issue the command. */
11359 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11360
11361 /*
11362 * Indicate the IO is being aborted by the driver and set
11363 * the caller's flag into the aborted IO.
11364 */
11365 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11366
11367 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11368 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11369 if (pring_s4 == NULL)
11370 continue;
98912dda
JS
11371 /* Note: both hbalock and ring_lock must be set here */
11372 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11373 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11374 abtsiocbq, 0);
11375 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11376 } else {
11377 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11378 abtsiocbq, 0);
11379 }
11380
11381
11382 if (ret_val == IOCB_ERROR)
11383 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11384 else
11385 sum++;
11386 }
11387 spin_unlock_irq(&phba->hbalock);
11388 return sum;
11389}
11390
e59058c4 11391/**
3621a710 11392 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11393 * @phba: Pointer to HBA context object.
11394 * @cmdiocbq: Pointer to command iocb.
11395 * @rspiocbq: Pointer to response iocb.
11396 *
11397 * This function is the completion handler for iocbs issued using
11398 * lpfc_sli_issue_iocb_wait function. This function is called by the
11399 * ring event handler function without any lock held. This function
11400 * can be called from both worker thread context and interrupt
11401 * context. This function also can be called from other thread which
11402 * cleans up the SLI layer objects.
11403 * This function copy the contents of the response iocb to the
11404 * response iocb memory object provided by the caller of
11405 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11406 * sleeps for the iocb completion.
11407 **/
68876920
JSEC
11408static void
11409lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11410 struct lpfc_iocbq *cmdiocbq,
11411 struct lpfc_iocbq *rspiocbq)
dea3101e 11412{
68876920
JSEC
11413 wait_queue_head_t *pdone_q;
11414 unsigned long iflags;
0f65ff68 11415 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11416
2e0fef85 11417 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11418 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11419
11420 /*
11421 * A time out has occurred for the iocb. If a time out
11422 * completion handler has been supplied, call it. Otherwise,
11423 * just free the iocbq.
11424 */
11425
11426 spin_unlock_irqrestore(&phba->hbalock, iflags);
11427 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11428 cmdiocbq->wait_iocb_cmpl = NULL;
11429 if (cmdiocbq->iocb_cmpl)
11430 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11431 else
11432 lpfc_sli_release_iocbq(phba, cmdiocbq);
11433 return;
11434 }
11435
68876920
JSEC
11436 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11437 if (cmdiocbq->context2 && rspiocbq)
11438 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11439 &rspiocbq->iocb, sizeof(IOCB_t));
11440
0f65ff68
JS
11441 /* Set the exchange busy flag for task management commands */
11442 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11443 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11444 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11445 cur_iocbq);
11446 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11447 }
11448
68876920 11449 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11450 if (pdone_q)
11451 wake_up(pdone_q);
858c9f6c 11452 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11453 return;
11454}
11455
d11e31dd
JS
11456/**
11457 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11458 * @phba: Pointer to HBA context object..
11459 * @piocbq: Pointer to command iocb.
11460 * @flag: Flag to test.
11461 *
11462 * This routine grabs the hbalock and then test the iocb_flag to
11463 * see if the passed in flag is set.
11464 * Returns:
11465 * 1 if flag is set.
11466 * 0 if flag is not set.
11467 **/
11468static int
11469lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11470 struct lpfc_iocbq *piocbq, uint32_t flag)
11471{
11472 unsigned long iflags;
11473 int ret;
11474
11475 spin_lock_irqsave(&phba->hbalock, iflags);
11476 ret = piocbq->iocb_flag & flag;
11477 spin_unlock_irqrestore(&phba->hbalock, iflags);
11478 return ret;
11479
11480}
11481
e59058c4 11482/**
3621a710 11483 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11484 * @phba: Pointer to HBA context object..
11485 * @pring: Pointer to sli ring.
11486 * @piocb: Pointer to command iocb.
11487 * @prspiocbq: Pointer to response iocb.
11488 * @timeout: Timeout in number of seconds.
11489 *
11490 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11491 * iocb to complete. The iocb_cmpl field of the shall be used
11492 * to handle iocbs which time out. If the field is NULL, the
11493 * function shall free the iocbq structure. If more clean up is
11494 * needed, the caller is expected to provide a completion function
11495 * that will provide the needed clean up. If the iocb command is
11496 * not completed within timeout seconds, the function will either
11497 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11498 * completion function set in the iocb_cmpl field and then return
11499 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11500 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11501 * The function waits for the iocb completion using an
11502 * non-interruptible wait.
11503 * This function will sleep while waiting for iocb completion.
11504 * So, this function should not be called from any context which
11505 * does not allow sleeping. Due to the same reason, this function
11506 * cannot be called with interrupt disabled.
11507 * This function assumes that the iocb completions occur while
11508 * this function sleep. So, this function cannot be called from
11509 * the thread which process iocb completion for this ring.
11510 * This function clears the iocb_flag of the iocb object before
11511 * issuing the iocb and the iocb completion handler sets this
11512 * flag and wakes this thread when the iocb completes.
11513 * The contents of the response iocb will be copied to prspiocbq
11514 * by the completion handler when the command completes.
11515 * This function returns IOCB_SUCCESS when success.
11516 * This function is called with no lock held.
11517 **/
dea3101e 11518int
2e0fef85 11519lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11520 uint32_t ring_number,
2e0fef85
JS
11521 struct lpfc_iocbq *piocb,
11522 struct lpfc_iocbq *prspiocbq,
68876920 11523 uint32_t timeout)
dea3101e 11524{
7259f0d0 11525 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11526 long timeleft, timeout_req = 0;
11527 int retval = IOCB_SUCCESS;
875fbdfe 11528 uint32_t creg_val;
0e9bb8d7
JS
11529 struct lpfc_iocbq *iocb;
11530 int txq_cnt = 0;
11531 int txcmplq_cnt = 0;
895427bd 11532 struct lpfc_sli_ring *pring;
5a0916b4
JS
11533 unsigned long iflags;
11534 bool iocb_completed = true;
11535
895427bd
JS
11536 if (phba->sli_rev >= LPFC_SLI_REV4)
11537 pring = lpfc_sli4_calc_ring(phba, piocb);
11538 else
11539 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11540 /*
68876920
JSEC
11541 * If the caller has provided a response iocbq buffer, then context2
11542 * is NULL or its an error.
dea3101e 11543 */
68876920
JSEC
11544 if (prspiocbq) {
11545 if (piocb->context2)
11546 return IOCB_ERROR;
11547 piocb->context2 = prspiocbq;
dea3101e
JB
11548 }
11549
5a0916b4 11550 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11551 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11552 piocb->context_un.wait_queue = &done_q;
5a0916b4 11553 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11554
875fbdfe 11555 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11556 if (lpfc_readl(phba->HCregaddr, &creg_val))
11557 return IOCB_ERROR;
875fbdfe
JSEC
11558 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11559 writel(creg_val, phba->HCregaddr);
11560 readl(phba->HCregaddr); /* flush */
11561 }
11562
2a9bf3d0
JS
11563 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11564 SLI_IOCB_RET_IOCB);
68876920 11565 if (retval == IOCB_SUCCESS) {
256ec0d0 11566 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11567 timeleft = wait_event_timeout(done_q,
d11e31dd 11568 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11569 timeout_req);
5a0916b4
JS
11570 spin_lock_irqsave(&phba->hbalock, iflags);
11571 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11572
11573 /*
11574 * IOCB timed out. Inform the wake iocb wait
11575 * completion function and set local status
11576 */
dea3101e 11577
5a0916b4
JS
11578 iocb_completed = false;
11579 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11580 }
11581 spin_unlock_irqrestore(&phba->hbalock, iflags);
11582 if (iocb_completed) {
7054a606 11583 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11584 "0331 IOCB wake signaled\n");
53151bbb
JS
11585 /* Note: we are not indicating if the IOCB has a success
11586 * status or not - that's for the caller to check.
11587 * IOCB_SUCCESS means just that the command was sent and
11588 * completed. Not that it completed successfully.
11589 * */
7054a606 11590 } else if (timeleft == 0) {
68876920 11591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11592 "0338 IOCB wait timeout error - no "
11593 "wake response Data x%x\n", timeout);
68876920 11594 retval = IOCB_TIMEDOUT;
7054a606 11595 } else {
68876920 11596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11597 "0330 IOCB wake NOT set, "
11598 "Data x%x x%lx\n",
68876920
JSEC
11599 timeout, (timeleft / jiffies));
11600 retval = IOCB_TIMEDOUT;
dea3101e 11601 }
2a9bf3d0 11602 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11603 if (phba->cfg_log_verbose & LOG_SLI) {
11604 list_for_each_entry(iocb, &pring->txq, list) {
11605 txq_cnt++;
11606 }
11607 list_for_each_entry(iocb, &pring->txcmplq, list) {
11608 txcmplq_cnt++;
11609 }
11610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11611 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11612 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11613 }
2a9bf3d0 11614 return retval;
68876920
JSEC
11615 } else {
11616 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11617 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11618 retval);
68876920 11619 retval = IOCB_ERROR;
dea3101e
JB
11620 }
11621
875fbdfe 11622 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11623 if (lpfc_readl(phba->HCregaddr, &creg_val))
11624 return IOCB_ERROR;
875fbdfe
JSEC
11625 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11626 writel(creg_val, phba->HCregaddr);
11627 readl(phba->HCregaddr); /* flush */
11628 }
11629
68876920
JSEC
11630 if (prspiocbq)
11631 piocb->context2 = NULL;
11632
11633 piocb->context_un.wait_queue = NULL;
11634 piocb->iocb_cmpl = NULL;
dea3101e
JB
11635 return retval;
11636}
68876920 11637
e59058c4 11638/**
3621a710 11639 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11640 * @phba: Pointer to HBA context object.
11641 * @pmboxq: Pointer to driver mailbox object.
11642 * @timeout: Timeout in number of seconds.
11643 *
11644 * This function issues the mailbox to firmware and waits for the
11645 * mailbox command to complete. If the mailbox command is not
11646 * completed within timeout seconds, it returns MBX_TIMEOUT.
11647 * The function waits for the mailbox completion using an
11648 * interruptible wait. If the thread is woken up due to a
11649 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11650 * should not free the mailbox resources, if this function returns
11651 * MBX_TIMEOUT.
11652 * This function will sleep while waiting for mailbox completion.
11653 * So, this function should not be called from any context which
11654 * does not allow sleeping. Due to the same reason, this function
11655 * cannot be called with interrupt disabled.
11656 * This function assumes that the mailbox completion occurs while
11657 * this function sleep. So, this function cannot be called from
11658 * the worker thread which processes mailbox completion.
11659 * This function is called in the context of HBA management
11660 * applications.
11661 * This function returns MBX_SUCCESS when successful.
11662 * This function is called with no lock held.
11663 **/
dea3101e 11664int
2e0fef85 11665lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
11666 uint32_t timeout)
11667{
7259f0d0 11668 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 11669 MAILBOX_t *mb = NULL;
dea3101e 11670 int retval;
858c9f6c 11671 unsigned long flag;
dea3101e 11672
b230b8a2 11673 /* The caller might set context1 for extended buffer */
98c9ea5c 11674 if (pmboxq->context1)
b230b8a2 11675 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 11676
495a714c 11677 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11678 /* setup wake call as IOCB callback */
11679 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11680 /* setup context field to pass wait_queue pointer to wake function */
11681 pmboxq->context1 = &done_q;
11682
dea3101e
JB
11683 /* now issue the command */
11684 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11685 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
11686 wait_event_interruptible_timeout(done_q,
11687 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 11688 msecs_to_jiffies(timeout * 1000));
7054a606 11689
858c9f6c 11690 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
11691 /* restore the possible extended buffer for free resource */
11692 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
11693 /*
11694 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11695 * else do not free the resources.
11696 */
d7c47992 11697 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11698 retval = MBX_SUCCESS;
d7c47992 11699 } else {
7054a606 11700 retval = MBX_TIMEOUT;
858c9f6c
JS
11701 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11702 }
11703 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
11704 } else {
11705 /* restore the possible extended buffer for free resource */
11706 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
11707 }
11708
dea3101e
JB
11709 return retval;
11710}
11711
e59058c4 11712/**
3772a991 11713 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11714 * @phba: Pointer to HBA context.
11715 *
3772a991
JS
11716 * This function is called to shutdown the driver's mailbox sub-system.
11717 * It first marks the mailbox sub-system is in a block state to prevent
11718 * the asynchronous mailbox command from issued off the pending mailbox
11719 * command queue. If the mailbox command sub-system shutdown is due to
11720 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11721 * the mailbox sub-system flush routine to forcefully bring down the
11722 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11723 * as with offline or HBA function reset), this routine will wait for the
11724 * outstanding mailbox command to complete before invoking the mailbox
11725 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11726 **/
3772a991 11727void
618a5230 11728lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11729{
3772a991 11730 struct lpfc_sli *psli = &phba->sli;
3772a991 11731 unsigned long timeout;
b4c02652 11732
618a5230
JS
11733 if (mbx_action == LPFC_MBX_NO_WAIT) {
11734 /* delay 100ms for port state */
11735 msleep(100);
11736 lpfc_sli_mbox_sys_flush(phba);
11737 return;
11738 }
a183a15f 11739 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11740
3772a991
JS
11741 spin_lock_irq(&phba->hbalock);
11742 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11743
3772a991 11744 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11745 /* Determine how long we might wait for the active mailbox
11746 * command to be gracefully completed by firmware.
11747 */
a183a15f
JS
11748 if (phba->sli.mbox_active)
11749 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11750 phba->sli.mbox_active) *
11751 1000) + jiffies;
11752 spin_unlock_irq(&phba->hbalock);
11753
3772a991
JS
11754 while (phba->sli.mbox_active) {
11755 /* Check active mailbox complete status every 2ms */
11756 msleep(2);
11757 if (time_after(jiffies, timeout))
11758 /* Timeout, let the mailbox flush routine to
11759 * forcefully release active mailbox command
11760 */
11761 break;
11762 }
d7069f09
JS
11763 } else
11764 spin_unlock_irq(&phba->hbalock);
11765
3772a991
JS
11766 lpfc_sli_mbox_sys_flush(phba);
11767}
ed957684 11768
3772a991
JS
11769/**
11770 * lpfc_sli_eratt_read - read sli-3 error attention events
11771 * @phba: Pointer to HBA context.
11772 *
11773 * This function is called to read the SLI3 device error attention registers
11774 * for possible error attention events. The caller must hold the hostlock
11775 * with spin_lock_irq().
11776 *
25985edc 11777 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11778 * Register and returns 0 otherwise.
11779 **/
11780static int
11781lpfc_sli_eratt_read(struct lpfc_hba *phba)
11782{
11783 uint32_t ha_copy;
b4c02652 11784
3772a991 11785 /* Read chip Host Attention (HA) register */
9940b97b
JS
11786 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11787 goto unplug_err;
11788
3772a991
JS
11789 if (ha_copy & HA_ERATT) {
11790 /* Read host status register to retrieve error event */
9940b97b
JS
11791 if (lpfc_sli_read_hs(phba))
11792 goto unplug_err;
b4c02652 11793
3772a991
JS
11794 /* Check if there is a deferred error condition is active */
11795 if ((HS_FFER1 & phba->work_hs) &&
11796 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11797 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11798 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11799 /* Clear all interrupt enable conditions */
11800 writel(0, phba->HCregaddr);
11801 readl(phba->HCregaddr);
11802 }
11803
11804 /* Set the driver HA work bitmap */
3772a991
JS
11805 phba->work_ha |= HA_ERATT;
11806 /* Indicate polling handles this ERATT */
11807 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11808 return 1;
11809 }
11810 return 0;
9940b97b
JS
11811
11812unplug_err:
11813 /* Set the driver HS work bitmap */
11814 phba->work_hs |= UNPLUG_ERR;
11815 /* Set the driver HA work bitmap */
11816 phba->work_ha |= HA_ERATT;
11817 /* Indicate polling handles this ERATT */
11818 phba->hba_flag |= HBA_ERATT_HANDLED;
11819 return 1;
b4c02652
JS
11820}
11821
da0436e9
JS
11822/**
11823 * lpfc_sli4_eratt_read - read sli-4 error attention events
11824 * @phba: Pointer to HBA context.
11825 *
11826 * This function is called to read the SLI4 device error attention registers
11827 * for possible error attention events. The caller must hold the hostlock
11828 * with spin_lock_irq().
11829 *
25985edc 11830 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11831 * Register and returns 0 otherwise.
11832 **/
11833static int
11834lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11835{
11836 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11837 uint32_t if_type, portsmphr;
11838 struct lpfc_register portstat_reg;
da0436e9 11839
2fcee4bf
JS
11840 /*
11841 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11842 * registers for error attention. This can be changed later.
11843 */
2fcee4bf
JS
11844 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11845 switch (if_type) {
11846 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11847 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11848 &uerr_sta_lo) ||
11849 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11850 &uerr_sta_hi)) {
11851 phba->work_hs |= UNPLUG_ERR;
11852 phba->work_ha |= HA_ERATT;
11853 phba->hba_flag |= HBA_ERATT_HANDLED;
11854 return 1;
11855 }
2fcee4bf
JS
11856 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11857 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11859 "1423 HBA Unrecoverable error: "
11860 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11861 "ue_mask_lo_reg=0x%x, "
11862 "ue_mask_hi_reg=0x%x\n",
11863 uerr_sta_lo, uerr_sta_hi,
11864 phba->sli4_hba.ue_mask_lo,
11865 phba->sli4_hba.ue_mask_hi);
11866 phba->work_status[0] = uerr_sta_lo;
11867 phba->work_status[1] = uerr_sta_hi;
11868 phba->work_ha |= HA_ERATT;
11869 phba->hba_flag |= HBA_ERATT_HANDLED;
11870 return 1;
11871 }
11872 break;
11873 case LPFC_SLI_INTF_IF_TYPE_2:
27d6ac0a 11874 case LPFC_SLI_INTF_IF_TYPE_6:
9940b97b
JS
11875 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11876 &portstat_reg.word0) ||
11877 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11878 &portsmphr)){
11879 phba->work_hs |= UNPLUG_ERR;
11880 phba->work_ha |= HA_ERATT;
11881 phba->hba_flag |= HBA_ERATT_HANDLED;
11882 return 1;
11883 }
2fcee4bf
JS
11884 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11885 phba->work_status[0] =
11886 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11887 phba->work_status[1] =
11888 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11890 "2885 Port Status Event: "
2fcee4bf
JS
11891 "port status reg 0x%x, "
11892 "port smphr reg 0x%x, "
11893 "error 1=0x%x, error 2=0x%x\n",
11894 portstat_reg.word0,
11895 portsmphr,
11896 phba->work_status[0],
11897 phba->work_status[1]);
11898 phba->work_ha |= HA_ERATT;
11899 phba->hba_flag |= HBA_ERATT_HANDLED;
11900 return 1;
11901 }
11902 break;
11903 case LPFC_SLI_INTF_IF_TYPE_1:
11904 default:
a747c9ce 11905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11906 "2886 HBA Error Attention on unsupported "
11907 "if type %d.", if_type);
a747c9ce 11908 return 1;
da0436e9 11909 }
2fcee4bf 11910
da0436e9
JS
11911 return 0;
11912}
11913
e59058c4 11914/**
3621a710 11915 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11916 * @phba: Pointer to HBA context.
11917 *
3772a991 11918 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11919 * error attention register bit for error attention events.
11920 *
25985edc 11921 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11922 * Register and returns 0 otherwise.
11923 **/
11924int
11925lpfc_sli_check_eratt(struct lpfc_hba *phba)
11926{
11927 uint32_t ha_copy;
11928
11929 /* If somebody is waiting to handle an eratt, don't process it
11930 * here. The brdkill function will do this.
11931 */
11932 if (phba->link_flag & LS_IGNORE_ERATT)
11933 return 0;
11934
11935 /* Check if interrupt handler handles this ERATT */
11936 spin_lock_irq(&phba->hbalock);
11937 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11938 /* Interrupt handler has handled ERATT */
11939 spin_unlock_irq(&phba->hbalock);
11940 return 0;
11941 }
11942
a257bf90
JS
11943 /*
11944 * If there is deferred error attention, do not check for error
11945 * attention
11946 */
11947 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11948 spin_unlock_irq(&phba->hbalock);
11949 return 0;
11950 }
11951
3772a991
JS
11952 /* If PCI channel is offline, don't process it */
11953 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11954 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11955 return 0;
11956 }
11957
11958 switch (phba->sli_rev) {
11959 case LPFC_SLI_REV2:
11960 case LPFC_SLI_REV3:
11961 /* Read chip Host Attention (HA) register */
11962 ha_copy = lpfc_sli_eratt_read(phba);
11963 break;
da0436e9 11964 case LPFC_SLI_REV4:
2fcee4bf 11965 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11966 ha_copy = lpfc_sli4_eratt_read(phba);
11967 break;
3772a991
JS
11968 default:
11969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11970 "0299 Invalid SLI revision (%d)\n",
11971 phba->sli_rev);
11972 ha_copy = 0;
11973 break;
9399627f
JS
11974 }
11975 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11976
11977 return ha_copy;
11978}
11979
11980/**
11981 * lpfc_intr_state_check - Check device state for interrupt handling
11982 * @phba: Pointer to HBA context.
11983 *
11984 * This inline routine checks whether a device or its PCI slot is in a state
11985 * that the interrupt should be handled.
11986 *
11987 * This function returns 0 if the device or the PCI slot is in a state that
11988 * interrupt should be handled, otherwise -EIO.
11989 */
11990static inline int
11991lpfc_intr_state_check(struct lpfc_hba *phba)
11992{
11993 /* If the pci channel is offline, ignore all the interrupts */
11994 if (unlikely(pci_channel_offline(phba->pcidev)))
11995 return -EIO;
11996
11997 /* Update device level interrupt statistics */
11998 phba->sli.slistat.sli_intr++;
11999
12000 /* Ignore all interrupts during initialization. */
12001 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12002 return -EIO;
12003
9399627f
JS
12004 return 0;
12005}
12006
12007/**
3772a991 12008 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
12009 * @irq: Interrupt number.
12010 * @dev_id: The device context pointer.
12011 *
9399627f 12012 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12013 * service routine when device with SLI-3 interface spec is enabled with
12014 * MSI-X multi-message interrupt mode and there are slow-path events in
12015 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12016 * interrupt mode, this function is called as part of the device-level
12017 * interrupt handler. When the PCI slot is in error recovery or the HBA
12018 * is undergoing initialization, the interrupt handler will not process
12019 * the interrupt. The link attention and ELS ring attention events are
12020 * handled by the worker thread. The interrupt handler signals the worker
12021 * thread and returns for these events. This function is called without
12022 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
12023 * structures.
12024 *
12025 * This function returns IRQ_HANDLED when interrupt is handled else it
12026 * returns IRQ_NONE.
e59058c4 12027 **/
dea3101e 12028irqreturn_t
3772a991 12029lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 12030{
2e0fef85 12031 struct lpfc_hba *phba;
a747c9ce 12032 uint32_t ha_copy, hc_copy;
dea3101e
JB
12033 uint32_t work_ha_copy;
12034 unsigned long status;
5b75da2f 12035 unsigned long iflag;
dea3101e
JB
12036 uint32_t control;
12037
92d7f7b0 12038 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
12039 struct lpfc_vport *vport;
12040 struct lpfc_nodelist *ndlp;
12041 struct lpfc_dmabuf *mp;
92d7f7b0
JS
12042 LPFC_MBOXQ_t *pmb;
12043 int rc;
12044
dea3101e
JB
12045 /*
12046 * Get the driver's phba structure from the dev_id and
12047 * assume the HBA is not interrupting.
12048 */
9399627f 12049 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
12050
12051 if (unlikely(!phba))
12052 return IRQ_NONE;
12053
dea3101e 12054 /*
9399627f
JS
12055 * Stuff needs to be attented to when this function is invoked as an
12056 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 12057 */
9399627f 12058 if (phba->intr_type == MSIX) {
3772a991
JS
12059 /* Check device state for handling interrupt */
12060 if (lpfc_intr_state_check(phba))
9399627f
JS
12061 return IRQ_NONE;
12062 /* Need to read HA REG for slow-path events */
5b75da2f 12063 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12064 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12065 goto unplug_error;
9399627f
JS
12066 /* If somebody is waiting to handle an eratt don't process it
12067 * here. The brdkill function will do this.
12068 */
12069 if (phba->link_flag & LS_IGNORE_ERATT)
12070 ha_copy &= ~HA_ERATT;
12071 /* Check the need for handling ERATT in interrupt handler */
12072 if (ha_copy & HA_ERATT) {
12073 if (phba->hba_flag & HBA_ERATT_HANDLED)
12074 /* ERATT polling has handled ERATT */
12075 ha_copy &= ~HA_ERATT;
12076 else
12077 /* Indicate interrupt handler handles ERATT */
12078 phba->hba_flag |= HBA_ERATT_HANDLED;
12079 }
a257bf90
JS
12080
12081 /*
12082 * If there is deferred error attention, do not check for any
12083 * interrupt.
12084 */
12085 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12086 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12087 return IRQ_NONE;
12088 }
12089
9399627f 12090 /* Clear up only attention source related to slow-path */
9940b97b
JS
12091 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12092 goto unplug_error;
12093
a747c9ce
JS
12094 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12095 HC_LAINT_ENA | HC_ERINT_ENA),
12096 phba->HCregaddr);
9399627f
JS
12097 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12098 phba->HAregaddr);
a747c9ce 12099 writel(hc_copy, phba->HCregaddr);
9399627f 12100 readl(phba->HAregaddr); /* flush */
5b75da2f 12101 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12102 } else
12103 ha_copy = phba->ha_copy;
dea3101e 12104
dea3101e
JB
12105 work_ha_copy = ha_copy & phba->work_ha_mask;
12106
9399627f 12107 if (work_ha_copy) {
dea3101e
JB
12108 if (work_ha_copy & HA_LATT) {
12109 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12110 /*
12111 * Turn off Link Attention interrupts
12112 * until CLEAR_LA done
12113 */
5b75da2f 12114 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12115 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
12116 if (lpfc_readl(phba->HCregaddr, &control))
12117 goto unplug_error;
dea3101e
JB
12118 control &= ~HC_LAINT_ENA;
12119 writel(control, phba->HCregaddr);
12120 readl(phba->HCregaddr); /* flush */
5b75da2f 12121 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12122 }
12123 else
12124 work_ha_copy &= ~HA_LATT;
12125 }
12126
9399627f 12127 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
12128 /*
12129 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12130 * the only slow ring.
12131 */
12132 status = (work_ha_copy &
12133 (HA_RXMASK << (4*LPFC_ELS_RING)));
12134 status >>= (4*LPFC_ELS_RING);
12135 if (status & HA_RXMASK) {
5b75da2f 12136 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12137 if (lpfc_readl(phba->HCregaddr, &control))
12138 goto unplug_error;
a58cbd52
JS
12139
12140 lpfc_debugfs_slow_ring_trc(phba,
12141 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12142 control, status,
12143 (uint32_t)phba->sli.slistat.sli_intr);
12144
858c9f6c 12145 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
12146 lpfc_debugfs_slow_ring_trc(phba,
12147 "ISR Disable ring:"
12148 "pwork:x%x hawork:x%x wait:x%x",
12149 phba->work_ha, work_ha_copy,
12150 (uint32_t)((unsigned long)
5e9d9b82 12151 &phba->work_waitq));
a58cbd52 12152
858c9f6c
JS
12153 control &=
12154 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
12155 writel(control, phba->HCregaddr);
12156 readl(phba->HCregaddr); /* flush */
dea3101e 12157 }
a58cbd52
JS
12158 else {
12159 lpfc_debugfs_slow_ring_trc(phba,
12160 "ISR slow ring: pwork:"
12161 "x%x hawork:x%x wait:x%x",
12162 phba->work_ha, work_ha_copy,
12163 (uint32_t)((unsigned long)
5e9d9b82 12164 &phba->work_waitq));
a58cbd52 12165 }
5b75da2f 12166 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12167 }
12168 }
5b75da2f 12169 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 12170 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
12171 if (lpfc_sli_read_hs(phba))
12172 goto unplug_error;
a257bf90
JS
12173 /*
12174 * Check if there is a deferred error condition
12175 * is active
12176 */
12177 if ((HS_FFER1 & phba->work_hs) &&
12178 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
12179 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12180 phba->work_hs)) {
a257bf90
JS
12181 phba->hba_flag |= DEFER_ERATT;
12182 /* Clear all interrupt enable conditions */
12183 writel(0, phba->HCregaddr);
12184 readl(phba->HCregaddr);
12185 }
12186 }
12187
9399627f 12188 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 12189 pmb = phba->sli.mbox_active;
04c68496 12190 pmbox = &pmb->u.mb;
34b02dcd 12191 mbox = phba->mbox;
858c9f6c 12192 vport = pmb->vport;
92d7f7b0
JS
12193
12194 /* First check out the status word */
12195 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12196 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 12197 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
12198 /*
12199 * Stray Mailbox Interrupt, mbxCommand <cmd>
12200 * mbxStatus <status>
12201 */
09372820 12202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 12203 LOG_SLI,
e8b62011 12204 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
12205 "Interrupt mbxCommand x%x "
12206 "mbxStatus x%x\n",
e8b62011 12207 (vport ? vport->vpi : 0),
92d7f7b0
JS
12208 pmbox->mbxCommand,
12209 pmbox->mbxStatus);
09372820
JS
12210 /* clear mailbox attention bit */
12211 work_ha_copy &= ~HA_MBATT;
12212 } else {
97eab634 12213 phba->sli.mbox_active = NULL;
5b75da2f 12214 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
12215 phba->last_completion_time = jiffies;
12216 del_timer(&phba->sli.mbox_tmo);
09372820
JS
12217 if (pmb->mbox_cmpl) {
12218 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12219 MAILBOX_CMD_SIZE);
7a470277
JS
12220 if (pmb->out_ext_byte_len &&
12221 pmb->context2)
12222 lpfc_sli_pcimem_bcopy(
12223 phba->mbox_ext,
12224 pmb->context2,
12225 pmb->out_ext_byte_len);
09372820
JS
12226 }
12227 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12228 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12229
12230 lpfc_debugfs_disc_trc(vport,
12231 LPFC_DISC_TRC_MBOX_VPORT,
12232 "MBOX dflt rpi: : "
12233 "status:x%x rpi:x%x",
12234 (uint32_t)pmbox->mbxStatus,
12235 pmbox->un.varWords[0], 0);
12236
12237 if (!pmbox->mbxStatus) {
12238 mp = (struct lpfc_dmabuf *)
12239 (pmb->context1);
12240 ndlp = (struct lpfc_nodelist *)
12241 pmb->context2;
12242
12243 /* Reg_LOGIN of dflt RPI was
12244 * successful. new lets get
12245 * rid of the RPI using the
12246 * same mbox buffer.
12247 */
12248 lpfc_unreg_login(phba,
12249 vport->vpi,
12250 pmbox->un.varWords[0],
12251 pmb);
12252 pmb->mbox_cmpl =
12253 lpfc_mbx_cmpl_dflt_rpi;
12254 pmb->context1 = mp;
12255 pmb->context2 = ndlp;
12256 pmb->vport = vport;
58da1ffb
JS
12257 rc = lpfc_sli_issue_mbox(phba,
12258 pmb,
12259 MBX_NOWAIT);
12260 if (rc != MBX_BUSY)
12261 lpfc_printf_log(phba,
12262 KERN_ERR,
12263 LOG_MBOX | LOG_SLI,
d7c255b2 12264 "0350 rc should have"
6a9c52cf 12265 "been MBX_BUSY\n");
3772a991
JS
12266 if (rc != MBX_NOT_FINISHED)
12267 goto send_current_mbox;
09372820 12268 }
858c9f6c 12269 }
5b75da2f
JS
12270 spin_lock_irqsave(
12271 &phba->pport->work_port_lock,
12272 iflag);
09372820
JS
12273 phba->pport->work_port_events &=
12274 ~WORKER_MBOX_TMO;
5b75da2f
JS
12275 spin_unlock_irqrestore(
12276 &phba->pport->work_port_lock,
12277 iflag);
09372820 12278 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12279 }
97eab634 12280 } else
5b75da2f 12281 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12282
92d7f7b0
JS
12283 if ((work_ha_copy & HA_MBATT) &&
12284 (phba->sli.mbox_active == NULL)) {
858c9f6c 12285send_current_mbox:
92d7f7b0 12286 /* Process next mailbox command if there is one */
58da1ffb
JS
12287 do {
12288 rc = lpfc_sli_issue_mbox(phba, NULL,
12289 MBX_NOWAIT);
12290 } while (rc == MBX_NOT_FINISHED);
12291 if (rc != MBX_SUCCESS)
12292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12293 LOG_SLI, "0349 rc should be "
6a9c52cf 12294 "MBX_SUCCESS\n");
92d7f7b0
JS
12295 }
12296
5b75da2f 12297 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12298 phba->work_ha |= work_ha_copy;
5b75da2f 12299 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12300 lpfc_worker_wake_up(phba);
dea3101e 12301 }
9399627f 12302 return IRQ_HANDLED;
9940b97b
JS
12303unplug_error:
12304 spin_unlock_irqrestore(&phba->hbalock, iflag);
12305 return IRQ_HANDLED;
dea3101e 12306
3772a991 12307} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12308
12309/**
3772a991 12310 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12311 * @irq: Interrupt number.
12312 * @dev_id: The device context pointer.
12313 *
12314 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12315 * service routine when device with SLI-3 interface spec is enabled with
12316 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12317 * ring event in the HBA. However, when the device is enabled with either
12318 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12319 * device-level interrupt handler. When the PCI slot is in error recovery
12320 * or the HBA is undergoing initialization, the interrupt handler will not
12321 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12322 * the intrrupt context. This function is called without any lock held.
12323 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12324 *
12325 * This function returns IRQ_HANDLED when interrupt is handled else it
12326 * returns IRQ_NONE.
12327 **/
12328irqreturn_t
3772a991 12329lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12330{
12331 struct lpfc_hba *phba;
12332 uint32_t ha_copy;
12333 unsigned long status;
5b75da2f 12334 unsigned long iflag;
895427bd 12335 struct lpfc_sli_ring *pring;
9399627f
JS
12336
12337 /* Get the driver's phba structure from the dev_id and
12338 * assume the HBA is not interrupting.
12339 */
12340 phba = (struct lpfc_hba *) dev_id;
12341
12342 if (unlikely(!phba))
12343 return IRQ_NONE;
12344
12345 /*
12346 * Stuff needs to be attented to when this function is invoked as an
12347 * individual interrupt handler in MSI-X multi-message interrupt mode
12348 */
12349 if (phba->intr_type == MSIX) {
3772a991
JS
12350 /* Check device state for handling interrupt */
12351 if (lpfc_intr_state_check(phba))
9399627f
JS
12352 return IRQ_NONE;
12353 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12354 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12355 return IRQ_HANDLED;
9399627f 12356 /* Clear up only attention source related to fast-path */
5b75da2f 12357 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12358 /*
12359 * If there is deferred error attention, do not check for
12360 * any interrupt.
12361 */
12362 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12363 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12364 return IRQ_NONE;
12365 }
9399627f
JS
12366 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12367 phba->HAregaddr);
12368 readl(phba->HAregaddr); /* flush */
5b75da2f 12369 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12370 } else
12371 ha_copy = phba->ha_copy;
dea3101e
JB
12372
12373 /*
9399627f 12374 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12375 */
9399627f
JS
12376 ha_copy &= ~(phba->work_ha_mask);
12377
12378 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12379 status >>= (4*LPFC_FCP_RING);
895427bd 12380 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12381 if (status & HA_RXMASK)
895427bd 12382 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12383
12384 if (phba->cfg_multi_ring_support == 2) {
12385 /*
9399627f
JS
12386 * Process all events on extra ring. Take the optimized path
12387 * for extra ring IO.
a4bc3379 12388 */
9399627f 12389 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12390 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12391 if (status & HA_RXMASK) {
a4bc3379 12392 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12393 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12394 status);
12395 }
12396 }
dea3101e 12397 return IRQ_HANDLED;
3772a991 12398} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12399
12400/**
3772a991 12401 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12402 * @irq: Interrupt number.
12403 * @dev_id: The device context pointer.
12404 *
3772a991
JS
12405 * This function is the HBA device-level interrupt handler to device with
12406 * SLI-3 interface spec, called from the PCI layer when either MSI or
12407 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12408 * requires driver attention. This function invokes the slow-path interrupt
12409 * attention handling function and fast-path interrupt attention handling
12410 * function in turn to process the relevant HBA attention events. This
12411 * function is called without any lock held. It gets the hbalock to access
12412 * and update SLI data structures.
9399627f
JS
12413 *
12414 * This function returns IRQ_HANDLED when interrupt is handled, else it
12415 * returns IRQ_NONE.
12416 **/
12417irqreturn_t
3772a991 12418lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12419{
12420 struct lpfc_hba *phba;
12421 irqreturn_t sp_irq_rc, fp_irq_rc;
12422 unsigned long status1, status2;
a747c9ce 12423 uint32_t hc_copy;
9399627f
JS
12424
12425 /*
12426 * Get the driver's phba structure from the dev_id and
12427 * assume the HBA is not interrupting.
12428 */
12429 phba = (struct lpfc_hba *) dev_id;
12430
12431 if (unlikely(!phba))
12432 return IRQ_NONE;
12433
3772a991
JS
12434 /* Check device state for handling interrupt */
12435 if (lpfc_intr_state_check(phba))
9399627f
JS
12436 return IRQ_NONE;
12437
12438 spin_lock(&phba->hbalock);
9940b97b
JS
12439 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12440 spin_unlock(&phba->hbalock);
12441 return IRQ_HANDLED;
12442 }
12443
9399627f
JS
12444 if (unlikely(!phba->ha_copy)) {
12445 spin_unlock(&phba->hbalock);
12446 return IRQ_NONE;
12447 } else if (phba->ha_copy & HA_ERATT) {
12448 if (phba->hba_flag & HBA_ERATT_HANDLED)
12449 /* ERATT polling has handled ERATT */
12450 phba->ha_copy &= ~HA_ERATT;
12451 else
12452 /* Indicate interrupt handler handles ERATT */
12453 phba->hba_flag |= HBA_ERATT_HANDLED;
12454 }
12455
a257bf90
JS
12456 /*
12457 * If there is deferred error attention, do not check for any interrupt.
12458 */
12459 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12460 spin_unlock(&phba->hbalock);
a257bf90
JS
12461 return IRQ_NONE;
12462 }
12463
9399627f 12464 /* Clear attention sources except link and error attentions */
9940b97b
JS
12465 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12466 spin_unlock(&phba->hbalock);
12467 return IRQ_HANDLED;
12468 }
a747c9ce
JS
12469 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12470 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12471 phba->HCregaddr);
9399627f 12472 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12473 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12474 readl(phba->HAregaddr); /* flush */
12475 spin_unlock(&phba->hbalock);
12476
12477 /*
12478 * Invokes slow-path host attention interrupt handling as appropriate.
12479 */
12480
12481 /* status of events with mailbox and link attention */
12482 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12483
12484 /* status of events with ELS ring */
12485 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12486 status2 >>= (4*LPFC_ELS_RING);
12487
12488 if (status1 || (status2 & HA_RXMASK))
3772a991 12489 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12490 else
12491 sp_irq_rc = IRQ_NONE;
12492
12493 /*
12494 * Invoke fast-path host attention interrupt handling as appropriate.
12495 */
12496
12497 /* status of events with FCP ring */
12498 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12499 status1 >>= (4*LPFC_FCP_RING);
12500
12501 /* status of events with extra ring */
12502 if (phba->cfg_multi_ring_support == 2) {
12503 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12504 status2 >>= (4*LPFC_EXTRA_RING);
12505 } else
12506 status2 = 0;
12507
12508 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12509 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12510 else
12511 fp_irq_rc = IRQ_NONE;
dea3101e 12512
9399627f
JS
12513 /* Return device-level interrupt handling status */
12514 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12515} /* lpfc_sli_intr_handler */
4f774513
JS
12516
12517/**
12518 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12519 * @phba: pointer to lpfc hba data structure.
12520 *
12521 * This routine is invoked by the worker thread to process all the pending
12522 * SLI4 FCP abort XRI events.
12523 **/
12524void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12525{
12526 struct lpfc_cq_event *cq_event;
12527
12528 /* First, declare the fcp xri abort event has been handled */
12529 spin_lock_irq(&phba->hbalock);
12530 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12531 spin_unlock_irq(&phba->hbalock);
12532 /* Now, handle all the fcp xri abort events */
12533 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12534 /* Get the first event from the head of the event queue */
12535 spin_lock_irq(&phba->hbalock);
12536 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12537 cq_event, struct lpfc_cq_event, list);
12538 spin_unlock_irq(&phba->hbalock);
12539 /* Notify aborted XRI for FCP work queue */
12540 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12541 /* Free the event processed back to the free pool */
12542 lpfc_sli4_cq_event_release(phba, cq_event);
12543 }
12544}
12545
12546/**
12547 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12548 * @phba: pointer to lpfc hba data structure.
12549 *
12550 * This routine is invoked by the worker thread to process all the pending
12551 * SLI4 els abort xri events.
12552 **/
12553void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12554{
12555 struct lpfc_cq_event *cq_event;
12556
12557 /* First, declare the els xri abort event has been handled */
12558 spin_lock_irq(&phba->hbalock);
12559 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12560 spin_unlock_irq(&phba->hbalock);
12561 /* Now, handle all the els xri abort events */
12562 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12563 /* Get the first event from the head of the event queue */
12564 spin_lock_irq(&phba->hbalock);
12565 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12566 cq_event, struct lpfc_cq_event, list);
12567 spin_unlock_irq(&phba->hbalock);
12568 /* Notify aborted XRI for ELS work queue */
12569 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12570 /* Free the event processed back to the free pool */
12571 lpfc_sli4_cq_event_release(phba, cq_event);
12572 }
12573}
12574
341af102
JS
12575/**
12576 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12577 * @phba: pointer to lpfc hba data structure
12578 * @pIocbIn: pointer to the rspiocbq
12579 * @pIocbOut: pointer to the cmdiocbq
12580 * @wcqe: pointer to the complete wcqe
12581 *
12582 * This routine transfers the fields of a command iocbq to a response iocbq
12583 * by copying all the IOCB fields from command iocbq and transferring the
12584 * completion status information from the complete wcqe.
12585 **/
4f774513 12586static void
341af102
JS
12587lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12588 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12589 struct lpfc_iocbq *pIocbOut,
12590 struct lpfc_wcqe_complete *wcqe)
12591{
af22741c 12592 int numBdes, i;
341af102 12593 unsigned long iflags;
af22741c
JS
12594 uint32_t status, max_response;
12595 struct lpfc_dmabuf *dmabuf;
12596 struct ulp_bde64 *bpl, bde;
4f774513
JS
12597 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12598
12599 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12600 sizeof(struct lpfc_iocbq) - offset);
4f774513 12601 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12602 status = bf_get(lpfc_wcqe_c_status, wcqe);
12603 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12604 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12605 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12606 pIocbIn->iocb.un.fcpi.fcpi_parm =
12607 pIocbOut->iocb.un.fcpi.fcpi_parm -
12608 wcqe->total_data_placed;
12609 else
12610 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12611 else {
4f774513 12612 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12613 switch (pIocbOut->iocb.ulpCommand) {
12614 case CMD_ELS_REQUEST64_CR:
12615 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12616 bpl = (struct ulp_bde64 *)dmabuf->virt;
12617 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12618 max_response = bde.tus.f.bdeSize;
12619 break;
12620 case CMD_GEN_REQUEST64_CR:
12621 max_response = 0;
12622 if (!pIocbOut->context3)
12623 break;
12624 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12625 sizeof(struct ulp_bde64);
12626 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12627 bpl = (struct ulp_bde64 *)dmabuf->virt;
12628 for (i = 0; i < numBdes; i++) {
12629 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12630 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12631 max_response += bde.tus.f.bdeSize;
12632 }
12633 break;
12634 default:
12635 max_response = wcqe->total_data_placed;
12636 break;
12637 }
12638 if (max_response < wcqe->total_data_placed)
12639 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12640 else
12641 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12642 wcqe->total_data_placed;
695a814e 12643 }
341af102 12644
acd6859b
JS
12645 /* Convert BG errors for completion status */
12646 if (status == CQE_STATUS_DI_ERROR) {
12647 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12648
12649 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12650 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12651 else
12652 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12653
12654 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12655 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12656 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12657 BGS_GUARD_ERR_MASK;
12658 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12659 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12660 BGS_APPTAG_ERR_MASK;
12661 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12662 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12663 BGS_REFTAG_ERR_MASK;
12664
12665 /* Check to see if there was any good data before the error */
12666 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12667 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12668 BGS_HI_WATER_MARK_PRESENT_MASK;
12669 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12670 wcqe->total_data_placed;
12671 }
12672
12673 /*
12674 * Set ALL the error bits to indicate we don't know what
12675 * type of error it is.
12676 */
12677 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12678 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12679 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12680 BGS_GUARD_ERR_MASK);
12681 }
12682
341af102
JS
12683 /* Pick up HBA exchange busy condition */
12684 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12685 spin_lock_irqsave(&phba->hbalock, iflags);
12686 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12687 spin_unlock_irqrestore(&phba->hbalock, iflags);
12688 }
4f774513
JS
12689}
12690
45ed1190
JS
12691/**
12692 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12693 * @phba: Pointer to HBA context object.
12694 * @wcqe: Pointer to work-queue completion queue entry.
12695 *
12696 * This routine handles an ELS work-queue completion event and construct
12697 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12698 * discovery engine to handle.
12699 *
12700 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12701 **/
12702static struct lpfc_iocbq *
12703lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12704 struct lpfc_iocbq *irspiocbq)
12705{
895427bd 12706 struct lpfc_sli_ring *pring;
45ed1190
JS
12707 struct lpfc_iocbq *cmdiocbq;
12708 struct lpfc_wcqe_complete *wcqe;
12709 unsigned long iflags;
12710
895427bd 12711 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12712 if (unlikely(!pring))
12713 return NULL;
895427bd 12714
45ed1190 12715 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12716 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12717 pring->stats.iocb_event++;
12718 /* Look up the ELS command IOCB and create pseudo response IOCB */
12719 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12720 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12721 if (unlikely(!cmdiocbq)) {
401bb416 12722 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12723 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12724 "0386 ELS complete with no corresponding "
401bb416
DK
12725 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12726 wcqe->word0, wcqe->total_data_placed,
12727 wcqe->parameter, wcqe->word3);
45ed1190
JS
12728 lpfc_sli_release_iocbq(phba, irspiocbq);
12729 return NULL;
12730 }
12731
401bb416
DK
12732 /* Put the iocb back on the txcmplq */
12733 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12734 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12735
45ed1190 12736 /* Fake the irspiocbq and copy necessary response information */
341af102 12737 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12738
12739 return irspiocbq;
12740}
12741
8a5ca109
JS
12742inline struct lpfc_cq_event *
12743lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12744{
12745 struct lpfc_cq_event *cq_event;
12746
12747 /* Allocate a new internal CQ_EVENT entry */
12748 cq_event = lpfc_sli4_cq_event_alloc(phba);
12749 if (!cq_event) {
12750 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12751 "0602 Failed to alloc CQ_EVENT entry\n");
12752 return NULL;
12753 }
12754
12755 /* Move the CQE into the event */
12756 memcpy(&cq_event->cqe, entry, size);
12757 return cq_event;
12758}
12759
04c68496
JS
12760/**
12761 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12762 * @phba: Pointer to HBA context object.
12763 * @cqe: Pointer to mailbox completion queue entry.
12764 *
12765 * This routine process a mailbox completion queue entry with asynchrous
12766 * event.
12767 *
12768 * Return: true if work posted to worker thread, otherwise false.
12769 **/
12770static bool
12771lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12772{
12773 struct lpfc_cq_event *cq_event;
12774 unsigned long iflags;
12775
12776 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12777 "0392 Async Event: word0:x%x, word1:x%x, "
12778 "word2:x%x, word3:x%x\n", mcqe->word0,
12779 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12780
8a5ca109
JS
12781 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12782 if (!cq_event)
04c68496 12783 return false;
04c68496
JS
12784 spin_lock_irqsave(&phba->hbalock, iflags);
12785 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12786 /* Set the async event flag */
12787 phba->hba_flag |= ASYNC_EVENT;
12788 spin_unlock_irqrestore(&phba->hbalock, iflags);
12789
12790 return true;
12791}
12792
12793/**
12794 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12795 * @phba: Pointer to HBA context object.
12796 * @cqe: Pointer to mailbox completion queue entry.
12797 *
12798 * This routine process a mailbox completion queue entry with mailbox
12799 * completion event.
12800 *
12801 * Return: true if work posted to worker thread, otherwise false.
12802 **/
12803static bool
12804lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12805{
12806 uint32_t mcqe_status;
12807 MAILBOX_t *mbox, *pmbox;
12808 struct lpfc_mqe *mqe;
12809 struct lpfc_vport *vport;
12810 struct lpfc_nodelist *ndlp;
12811 struct lpfc_dmabuf *mp;
12812 unsigned long iflags;
12813 LPFC_MBOXQ_t *pmb;
12814 bool workposted = false;
12815 int rc;
12816
12817 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12818 if (!bf_get(lpfc_trailer_completed, mcqe))
12819 goto out_no_mqe_complete;
12820
12821 /* Get the reference to the active mbox command */
12822 spin_lock_irqsave(&phba->hbalock, iflags);
12823 pmb = phba->sli.mbox_active;
12824 if (unlikely(!pmb)) {
12825 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12826 "1832 No pending MBOX command to handle\n");
12827 spin_unlock_irqrestore(&phba->hbalock, iflags);
12828 goto out_no_mqe_complete;
12829 }
12830 spin_unlock_irqrestore(&phba->hbalock, iflags);
12831 mqe = &pmb->u.mqe;
12832 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12833 mbox = phba->mbox;
12834 vport = pmb->vport;
12835
12836 /* Reset heartbeat timer */
12837 phba->last_completion_time = jiffies;
12838 del_timer(&phba->sli.mbox_tmo);
12839
12840 /* Move mbox data to caller's mailbox region, do endian swapping */
12841 if (pmb->mbox_cmpl && mbox)
12842 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12843
73d91e50
JS
12844 /*
12845 * For mcqe errors, conditionally move a modified error code to
12846 * the mbox so that the error will not be missed.
12847 */
12848 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12849 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12850 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12851 bf_set(lpfc_mqe_status, mqe,
12852 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12853 }
04c68496
JS
12854 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12855 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12856 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12857 "MBOX dflt rpi: status:x%x rpi:x%x",
12858 mcqe_status,
12859 pmbox->un.varWords[0], 0);
12860 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12861 mp = (struct lpfc_dmabuf *)(pmb->context1);
12862 ndlp = (struct lpfc_nodelist *)pmb->context2;
12863 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12864 * RID of the PPI using the same mbox buffer.
12865 */
12866 lpfc_unreg_login(phba, vport->vpi,
12867 pmbox->un.varWords[0], pmb);
12868 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12869 pmb->context1 = mp;
12870 pmb->context2 = ndlp;
12871 pmb->vport = vport;
12872 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12873 if (rc != MBX_BUSY)
12874 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12875 LOG_SLI, "0385 rc should "
12876 "have been MBX_BUSY\n");
12877 if (rc != MBX_NOT_FINISHED)
12878 goto send_current_mbox;
12879 }
12880 }
12881 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12882 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12883 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12884
12885 /* There is mailbox completion work to do */
12886 spin_lock_irqsave(&phba->hbalock, iflags);
12887 __lpfc_mbox_cmpl_put(phba, pmb);
12888 phba->work_ha |= HA_MBATT;
12889 spin_unlock_irqrestore(&phba->hbalock, iflags);
12890 workposted = true;
12891
12892send_current_mbox:
12893 spin_lock_irqsave(&phba->hbalock, iflags);
12894 /* Release the mailbox command posting token */
12895 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12896 /* Setting active mailbox pointer need to be in sync to flag clear */
12897 phba->sli.mbox_active = NULL;
12898 spin_unlock_irqrestore(&phba->hbalock, iflags);
12899 /* Wake up worker thread to post the next pending mailbox command */
12900 lpfc_worker_wake_up(phba);
12901out_no_mqe_complete:
12902 if (bf_get(lpfc_trailer_consumed, mcqe))
12903 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12904 return workposted;
12905}
12906
12907/**
12908 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12909 * @phba: Pointer to HBA context object.
12910 * @cqe: Pointer to mailbox completion queue entry.
12911 *
12912 * This routine process a mailbox completion queue entry, it invokes the
12913 * proper mailbox complete handling or asynchrous event handling routine
12914 * according to the MCQE's async bit.
12915 *
12916 * Return: true if work posted to worker thread, otherwise false.
12917 **/
12918static bool
12919lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12920{
12921 struct lpfc_mcqe mcqe;
12922 bool workposted;
12923
12924 /* Copy the mailbox MCQE and convert endian order as needed */
12925 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12926
12927 /* Invoke the proper event handling routine */
12928 if (!bf_get(lpfc_trailer_async, &mcqe))
12929 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12930 else
12931 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12932 return workposted;
12933}
12934
4f774513
JS
12935/**
12936 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12937 * @phba: Pointer to HBA context object.
2a76a283 12938 * @cq: Pointer to associated CQ
4f774513
JS
12939 * @wcqe: Pointer to work-queue completion queue entry.
12940 *
12941 * This routine handles an ELS work-queue completion event.
12942 *
12943 * Return: true if work posted to worker thread, otherwise false.
12944 **/
12945static bool
2a76a283 12946lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12947 struct lpfc_wcqe_complete *wcqe)
12948{
4f774513
JS
12949 struct lpfc_iocbq *irspiocbq;
12950 unsigned long iflags;
2a76a283 12951 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12952 int txq_cnt = 0;
12953 int txcmplq_cnt = 0;
12954 int fcp_txcmplq_cnt = 0;
4f774513 12955
45ed1190 12956 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12957 irspiocbq = lpfc_sli_get_iocbq(phba);
12958 if (!irspiocbq) {
0e9bb8d7
JS
12959 if (!list_empty(&pring->txq))
12960 txq_cnt++;
12961 if (!list_empty(&pring->txcmplq))
12962 txcmplq_cnt++;
4f774513 12963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12964 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12965 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12966 txq_cnt, phba->iocb_cnt,
12967 fcp_txcmplq_cnt,
12968 txcmplq_cnt);
45ed1190 12969 return false;
4f774513 12970 }
4f774513 12971
45ed1190
JS
12972 /* Save off the slow-path queue event for work thread to process */
12973 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12974 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12975 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12976 &phba->sli4_hba.sp_queue_event);
12977 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12978 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12979
45ed1190 12980 return true;
4f774513
JS
12981}
12982
12983/**
12984 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12985 * @phba: Pointer to HBA context object.
12986 * @wcqe: Pointer to work-queue completion queue entry.
12987 *
3f8b6fb7 12988 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
12989 * proper WQ release routine to the slow-path WQ.
12990 **/
12991static void
12992lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12993 struct lpfc_wcqe_release *wcqe)
12994{
2e90f4b5
JS
12995 /* sanity check on queue memory */
12996 if (unlikely(!phba->sli4_hba.els_wq))
12997 return;
4f774513
JS
12998 /* Check for the slow-path ELS work queue */
12999 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13000 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13001 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13002 else
13003 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13004 "2579 Slow-path wqe consume event carries "
13005 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13006 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13007 phba->sli4_hba.els_wq->queue_id);
13008}
13009
13010/**
13011 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13012 * @phba: Pointer to HBA context object.
13013 * @cq: Pointer to a WQ completion queue.
13014 * @wcqe: Pointer to work-queue completion queue entry.
13015 *
13016 * This routine handles an XRI abort event.
13017 *
13018 * Return: true if work posted to worker thread, otherwise false.
13019 **/
13020static bool
13021lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13022 struct lpfc_queue *cq,
13023 struct sli4_wcqe_xri_aborted *wcqe)
13024{
13025 bool workposted = false;
13026 struct lpfc_cq_event *cq_event;
13027 unsigned long iflags;
13028
4f774513
JS
13029 switch (cq->subtype) {
13030 case LPFC_FCP:
8a5ca109
JS
13031 cq_event = lpfc_cq_event_setup(
13032 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13033 if (!cq_event)
13034 return false;
4f774513
JS
13035 spin_lock_irqsave(&phba->hbalock, iflags);
13036 list_add_tail(&cq_event->list,
13037 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13038 /* Set the fcp xri abort event flag */
13039 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13040 spin_unlock_irqrestore(&phba->hbalock, iflags);
13041 workposted = true;
13042 break;
422c4cb7 13043 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 13044 case LPFC_ELS:
8a5ca109
JS
13045 cq_event = lpfc_cq_event_setup(
13046 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13047 if (!cq_event)
13048 return false;
4f774513
JS
13049 spin_lock_irqsave(&phba->hbalock, iflags);
13050 list_add_tail(&cq_event->list,
13051 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13052 /* Set the els xri abort event flag */
13053 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13054 spin_unlock_irqrestore(&phba->hbalock, iflags);
13055 workposted = true;
13056 break;
318083ad 13057 case LPFC_NVME:
8a5ca109
JS
13058 /* Notify aborted XRI for NVME work queue */
13059 if (phba->nvmet_support)
13060 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13061 else
13062 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13063
13064 workposted = false;
318083ad 13065 break;
4f774513
JS
13066 default:
13067 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
13068 "0603 Invalid CQ subtype %d: "
13069 "%08x %08x %08x %08x\n",
13070 cq->subtype, wcqe->word0, wcqe->parameter,
13071 wcqe->word2, wcqe->word3);
4f774513
JS
13072 workposted = false;
13073 break;
13074 }
13075 return workposted;
13076}
13077
4f774513
JS
13078/**
13079 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13080 * @phba: Pointer to HBA context object.
13081 * @rcqe: Pointer to receive-queue completion queue entry.
13082 *
13083 * This routine process a receive-queue completion queue entry.
13084 *
13085 * Return: true if work posted to worker thread, otherwise false.
13086 **/
13087static bool
4d9ab994 13088lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 13089{
4f774513 13090 bool workposted = false;
895427bd 13091 struct fc_frame_header *fc_hdr;
4f774513
JS
13092 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13093 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 13094 struct lpfc_nvmet_tgtport *tgtp;
4f774513 13095 struct hbq_dmabuf *dma_buf;
7851fe2c 13096 uint32_t status, rq_id;
4f774513
JS
13097 unsigned long iflags;
13098
2e90f4b5
JS
13099 /* sanity check on queue memory */
13100 if (unlikely(!hrq) || unlikely(!drq))
13101 return workposted;
13102
7851fe2c
JS
13103 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13104 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13105 else
13106 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13107 if (rq_id != hrq->queue_id)
4f774513
JS
13108 goto out;
13109
4d9ab994 13110 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
13111 switch (status) {
13112 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13114 "2537 Receive Frame Truncated!!\n");
13115 case FC_STATUS_RQ_SUCCESS:
13116 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13117 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
13118 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13119 if (!dma_buf) {
b84daac9 13120 hrq->RQ_no_buf_found++;
4f774513
JS
13121 spin_unlock_irqrestore(&phba->hbalock, iflags);
13122 goto out;
13123 }
b84daac9 13124 hrq->RQ_rcv_buf++;
547077a4 13125 hrq->RQ_buf_posted--;
4d9ab994 13126 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
13127
13128 /* If a NVME LS event (type 0x28), treat it as Fast path */
13129 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13130
4f774513 13131 /* save off the frame for the word thread to process */
4d9ab994 13132 list_add_tail(&dma_buf->cq_event.list,
45ed1190 13133 &phba->sli4_hba.sp_queue_event);
4f774513 13134 /* Frame received */
45ed1190 13135 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
13136 spin_unlock_irqrestore(&phba->hbalock, iflags);
13137 workposted = true;
13138 break;
4f774513 13139 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13140 if (phba->nvmet_support) {
13141 tgtp = phba->targetport->private;
13142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13143 "6402 RQE Error x%x, posted %d err_cnt "
13144 "%d: %x %x %x\n",
13145 status, hrq->RQ_buf_posted,
13146 hrq->RQ_no_posted_buf,
13147 atomic_read(&tgtp->rcv_fcp_cmd_in),
13148 atomic_read(&tgtp->rcv_fcp_cmd_out),
13149 atomic_read(&tgtp->xmt_fcp_release));
13150 }
13151 /* fallthrough */
13152
13153 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 13154 hrq->RQ_no_posted_buf++;
4f774513
JS
13155 /* Post more buffers if possible */
13156 spin_lock_irqsave(&phba->hbalock, iflags);
13157 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13158 spin_unlock_irqrestore(&phba->hbalock, iflags);
13159 workposted = true;
13160 break;
13161 }
13162out:
13163 return workposted;
4f774513
JS
13164}
13165
4d9ab994
JS
13166/**
13167 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13168 * @phba: Pointer to HBA context object.
13169 * @cq: Pointer to the completion queue.
13170 * @wcqe: Pointer to a completion queue entry.
13171 *
25985edc 13172 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
13173 * entry.
13174 *
13175 * Return: true if work posted to worker thread, otherwise false.
13176 **/
13177static bool
13178lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13179 struct lpfc_cqe *cqe)
13180{
45ed1190 13181 struct lpfc_cqe cqevt;
4d9ab994
JS
13182 bool workposted = false;
13183
13184 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 13185 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
13186
13187 /* Check and process for different type of WCQE and dispatch */
45ed1190 13188 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 13189 case CQE_CODE_COMPL_WQE:
45ed1190 13190 /* Process the WQ/RQ complete event */
bc73905a 13191 phba->last_completion_time = jiffies;
2a76a283 13192 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 13193 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
13194 break;
13195 case CQE_CODE_RELEASE_WQE:
13196 /* Process the WQ release event */
13197 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 13198 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
13199 break;
13200 case CQE_CODE_XRI_ABORTED:
13201 /* Process the WQ XRI abort event */
bc73905a 13202 phba->last_completion_time = jiffies;
4d9ab994 13203 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 13204 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
13205 break;
13206 case CQE_CODE_RECEIVE:
7851fe2c 13207 case CQE_CODE_RECEIVE_V1:
4d9ab994 13208 /* Process the RQ event */
bc73905a 13209 phba->last_completion_time = jiffies;
4d9ab994 13210 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 13211 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
13212 break;
13213 default:
13214 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13215 "0388 Not a valid WCQE code: x%x\n",
45ed1190 13216 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
13217 break;
13218 }
13219 return workposted;
13220}
13221
4f774513
JS
13222/**
13223 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13224 * @phba: Pointer to HBA context object.
13225 * @eqe: Pointer to fast-path event queue entry.
13226 *
13227 * This routine process a event queue entry from the slow-path event queue.
13228 * It will check the MajorCode and MinorCode to determine this is for a
13229 * completion event on a completion queue, if not, an error shall be logged
13230 * and just return. Otherwise, it will get to the corresponding completion
13231 * queue and process all the entries on that completion queue, rearm the
13232 * completion queue, and then return.
13233 *
13234 **/
f485c18d 13235static void
67d12733
JS
13236lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13237 struct lpfc_queue *speq)
4f774513 13238{
67d12733 13239 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13240 uint16_t cqid;
13241
4f774513 13242 /* Get the reference to the corresponding CQ */
cb5172ea 13243 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13244
4f774513
JS
13245 list_for_each_entry(childq, &speq->child_list, list) {
13246 if (childq->queue_id == cqid) {
13247 cq = childq;
13248 break;
13249 }
13250 }
13251 if (unlikely(!cq)) {
75baf696
JS
13252 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13254 "0365 Slow-path CQ identifier "
13255 "(%d) does not exist\n", cqid);
f485c18d 13256 return;
4f774513
JS
13257 }
13258
895427bd
JS
13259 /* Save EQ associated with this CQ */
13260 cq->assoc_qp = speq;
13261
f485c18d
DK
13262 if (!queue_work(phba->wq, &cq->spwork))
13263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13264 "0390 Cannot schedule soft IRQ "
13265 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13266 cqid, cq->queue_id, smp_processor_id());
13267}
13268
13269/**
13270 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13271 * @phba: Pointer to HBA context object.
13272 *
13273 * This routine process a event queue entry from the slow-path event queue.
13274 * It will check the MajorCode and MinorCode to determine this is for a
13275 * completion event on a completion queue, if not, an error shall be logged
13276 * and just return. Otherwise, it will get to the corresponding completion
13277 * queue and process all the entries on that completion queue, rearm the
13278 * completion queue, and then return.
13279 *
13280 **/
13281static void
13282lpfc_sli4_sp_process_cq(struct work_struct *work)
13283{
13284 struct lpfc_queue *cq =
13285 container_of(work, struct lpfc_queue, spwork);
13286 struct lpfc_hba *phba = cq->phba;
13287 struct lpfc_cqe *cqe;
13288 bool workposted = false;
13289 int ccount = 0;
13290
4f774513
JS
13291 /* Process all the entries to the CQ */
13292 switch (cq->type) {
13293 case LPFC_MCQ:
13294 while ((cqe = lpfc_sli4_cq_get(cq))) {
13295 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13296 if (!(++ccount % cq->entry_repost))
7869da18 13297 break;
b84daac9 13298 cq->CQ_mbox++;
4f774513
JS
13299 }
13300 break;
13301 case LPFC_WCQ:
13302 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13303 if (cq->subtype == LPFC_FCP ||
13304 cq->subtype == LPFC_NVME) {
13305#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13306 if (phba->ktime_on)
13307 cq->isr_timestamp = ktime_get_ns();
13308 else
13309 cq->isr_timestamp = 0;
13310#endif
895427bd 13311 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13312 cqe);
c8a4ce0b 13313 } else {
0558056c
JS
13314 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13315 cqe);
c8a4ce0b 13316 }
f485c18d 13317 if (!(++ccount % cq->entry_repost))
7869da18 13318 break;
4f774513 13319 }
b84daac9
JS
13320
13321 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13322 if (ccount > cq->CQ_max_cqe)
13323 cq->CQ_max_cqe = ccount;
4f774513
JS
13324 break;
13325 default:
13326 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13327 "0370 Invalid completion queue type (%d)\n",
13328 cq->type);
f485c18d 13329 return;
4f774513
JS
13330 }
13331
13332 /* Catch the no cq entry condition, log an error */
f485c18d 13333 if (unlikely(ccount == 0))
4f774513
JS
13334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13335 "0371 No entry from the CQ: identifier "
13336 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13337
13338 /* In any case, flash and re-arm the RCQ */
b71413dd 13339 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13340
13341 /* wake up worker thread if there are works to be done */
13342 if (workposted)
13343 lpfc_worker_wake_up(phba);
13344}
13345
13346/**
13347 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13348 * @phba: Pointer to HBA context object.
13349 * @cq: Pointer to associated CQ
13350 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13351 *
13352 * This routine process a fast-path work queue completion entry from fast-path
13353 * event queue for FCP command response completion.
13354 **/
13355static void
2a76a283 13356lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13357 struct lpfc_wcqe_complete *wcqe)
13358{
2a76a283 13359 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13360 struct lpfc_iocbq *cmdiocbq;
13361 struct lpfc_iocbq irspiocbq;
13362 unsigned long iflags;
13363
4f774513
JS
13364 /* Check for response status */
13365 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13366 /* If resource errors reported from HBA, reduce queue
13367 * depth of the SCSI device.
13368 */
e3d2b802
JS
13369 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13370 IOSTAT_LOCAL_REJECT)) &&
13371 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13372 IOERR_NO_RESOURCES))
4f774513 13373 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13374
4f774513
JS
13375 /* Log the error status */
13376 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13377 "0373 FCP complete error: status=x%x, "
13378 "hw_status=x%x, total_data_specified=%d, "
13379 "parameter=x%x, word3=x%x\n",
13380 bf_get(lpfc_wcqe_c_status, wcqe),
13381 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13382 wcqe->total_data_placed, wcqe->parameter,
13383 wcqe->word3);
13384 }
13385
13386 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13387 spin_lock_irqsave(&pring->ring_lock, iflags);
13388 pring->stats.iocb_event++;
4f774513
JS
13389 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13390 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13391 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13392 if (unlikely(!cmdiocbq)) {
13393 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13394 "0374 FCP complete with no corresponding "
13395 "cmdiocb: iotag (%d)\n",
13396 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13397 return;
13398 }
c8a4ce0b
DK
13399#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13400 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13401#endif
895427bd
JS
13402 if (cmdiocbq->iocb_cmpl == NULL) {
13403 if (cmdiocbq->wqe_cmpl) {
13404 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13405 spin_lock_irqsave(&phba->hbalock, iflags);
13406 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13407 spin_unlock_irqrestore(&phba->hbalock, iflags);
13408 }
13409
13410 /* Pass the cmd_iocb and the wcqe to the upper layer */
13411 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13412 return;
13413 }
4f774513
JS
13414 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13415 "0375 FCP cmdiocb not callback function "
13416 "iotag: (%d)\n",
13417 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13418 return;
13419 }
13420
13421 /* Fake the irspiocb and copy necessary response information */
341af102 13422 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13423
0f65ff68
JS
13424 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13425 spin_lock_irqsave(&phba->hbalock, iflags);
13426 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13427 spin_unlock_irqrestore(&phba->hbalock, iflags);
13428 }
13429
4f774513
JS
13430 /* Pass the cmd_iocb and the rsp state to the upper layer */
13431 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13432}
13433
13434/**
13435 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13436 * @phba: Pointer to HBA context object.
13437 * @cq: Pointer to completion queue.
13438 * @wcqe: Pointer to work-queue completion queue entry.
13439 *
3f8b6fb7 13440 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13441 * proper WQ release routine to the slow-path WQ.
13442 **/
13443static void
13444lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13445 struct lpfc_wcqe_release *wcqe)
13446{
13447 struct lpfc_queue *childwq;
13448 bool wqid_matched = false;
895427bd 13449 uint16_t hba_wqid;
4f774513
JS
13450
13451 /* Check for fast-path FCP work queue release */
895427bd 13452 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13453 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13454 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13455 lpfc_sli4_wq_release(childwq,
13456 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
6e8e1c14
JS
13457 if (childwq->q_flag & HBA_NVMET_WQFULL)
13458 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13459 wqid_matched = true;
13460 break;
13461 }
13462 }
13463 /* Report warning log message if no match found */
13464 if (wqid_matched != true)
13465 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13466 "2580 Fast-path wqe consume event carries "
895427bd 13467 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13468}
13469
13470/**
2d7dbc4c
JS
13471 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13472 * @phba: Pointer to HBA context object.
13473 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13474 *
2d7dbc4c
JS
13475 * This routine process a receive-queue completion queue entry.
13476 *
13477 * Return: true if work posted to worker thread, otherwise false.
13478 **/
13479static bool
13480lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13481 struct lpfc_rcqe *rcqe)
13482{
13483 bool workposted = false;
13484 struct lpfc_queue *hrq;
13485 struct lpfc_queue *drq;
13486 struct rqb_dmabuf *dma_buf;
13487 struct fc_frame_header *fc_hdr;
547077a4 13488 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13489 uint32_t status, rq_id;
13490 unsigned long iflags;
13491 uint32_t fctl, idx;
13492
13493 if ((phba->nvmet_support == 0) ||
13494 (phba->sli4_hba.nvmet_cqset == NULL))
13495 return workposted;
13496
13497 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13498 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13499 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13500
13501 /* sanity check on queue memory */
13502 if (unlikely(!hrq) || unlikely(!drq))
13503 return workposted;
13504
13505 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13506 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13507 else
13508 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13509
13510 if ((phba->nvmet_support == 0) ||
13511 (rq_id != hrq->queue_id))
13512 return workposted;
13513
13514 status = bf_get(lpfc_rcqe_status, rcqe);
13515 switch (status) {
13516 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13517 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13518 "6126 Receive Frame Truncated!!\n");
78e1d200 13519 /* Drop thru */
2d7dbc4c 13520 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13521 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13522 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13523 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13524 if (!dma_buf) {
13525 hrq->RQ_no_buf_found++;
13526 spin_unlock_irqrestore(&phba->hbalock, iflags);
13527 goto out;
13528 }
13529 spin_unlock_irqrestore(&phba->hbalock, iflags);
13530 hrq->RQ_rcv_buf++;
547077a4 13531 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13532 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13533
13534 /* Just some basic sanity checks on FCP Command frame */
13535 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13536 fc_hdr->fh_f_ctl[1] << 8 |
13537 fc_hdr->fh_f_ctl[2]);
13538 if (((fctl &
13539 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13540 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13541 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13542 goto drop;
13543
13544 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13545 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13546 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13547 phba, idx, dma_buf,
c8a4ce0b 13548 cq->isr_timestamp);
2d7dbc4c
JS
13549 return false;
13550 }
13551drop:
13552 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13553 break;
2d7dbc4c 13554 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13555 if (phba->nvmet_support) {
13556 tgtp = phba->targetport->private;
13557 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13558 "6401 RQE Error x%x, posted %d err_cnt "
13559 "%d: %x %x %x\n",
13560 status, hrq->RQ_buf_posted,
13561 hrq->RQ_no_posted_buf,
13562 atomic_read(&tgtp->rcv_fcp_cmd_in),
13563 atomic_read(&tgtp->rcv_fcp_cmd_out),
13564 atomic_read(&tgtp->xmt_fcp_release));
13565 }
13566 /* fallthrough */
13567
13568 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13569 hrq->RQ_no_posted_buf++;
13570 /* Post more buffers if possible */
2d7dbc4c
JS
13571 break;
13572 }
13573out:
13574 return workposted;
13575}
13576
4f774513 13577/**
895427bd 13578 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13579 * @cq: Pointer to the completion queue.
13580 * @eqe: Pointer to fast-path completion queue entry.
13581 *
13582 * This routine process a fast-path work queue completion entry from fast-path
13583 * event queue for FCP command response completion.
13584 **/
13585static int
895427bd 13586lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13587 struct lpfc_cqe *cqe)
13588{
13589 struct lpfc_wcqe_release wcqe;
13590 bool workposted = false;
13591
13592 /* Copy the work queue CQE and convert endian order if needed */
13593 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13594
13595 /* Check and process for different type of WCQE and dispatch */
13596 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13597 case CQE_CODE_COMPL_WQE:
895427bd 13598 case CQE_CODE_NVME_ERSP:
b84daac9 13599 cq->CQ_wq++;
4f774513 13600 /* Process the WQ complete event */
98fc5dd9 13601 phba->last_completion_time = jiffies;
895427bd
JS
13602 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13603 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13604 (struct lpfc_wcqe_complete *)&wcqe);
13605 if (cq->subtype == LPFC_NVME_LS)
13606 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13607 (struct lpfc_wcqe_complete *)&wcqe);
13608 break;
13609 case CQE_CODE_RELEASE_WQE:
b84daac9 13610 cq->CQ_release_wqe++;
4f774513
JS
13611 /* Process the WQ release event */
13612 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13613 (struct lpfc_wcqe_release *)&wcqe);
13614 break;
13615 case CQE_CODE_XRI_ABORTED:
b84daac9 13616 cq->CQ_xri_aborted++;
4f774513 13617 /* Process the WQ XRI abort event */
bc73905a 13618 phba->last_completion_time = jiffies;
4f774513
JS
13619 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13620 (struct sli4_wcqe_xri_aborted *)&wcqe);
13621 break;
895427bd
JS
13622 case CQE_CODE_RECEIVE_V1:
13623 case CQE_CODE_RECEIVE:
13624 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13625 if (cq->subtype == LPFC_NVMET) {
13626 workposted = lpfc_sli4_nvmet_handle_rcqe(
13627 phba, cq, (struct lpfc_rcqe *)&wcqe);
13628 }
895427bd 13629 break;
4f774513
JS
13630 default:
13631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13632 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13633 bf_get(lpfc_wcqe_c_code, &wcqe));
13634 break;
13635 }
13636 return workposted;
13637}
13638
13639/**
67d12733 13640 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13641 * @phba: Pointer to HBA context object.
13642 * @eqe: Pointer to fast-path event queue entry.
13643 *
13644 * This routine process a event queue entry from the fast-path event queue.
13645 * It will check the MajorCode and MinorCode to determine this is for a
13646 * completion event on a completion queue, if not, an error shall be logged
13647 * and just return. Otherwise, it will get to the corresponding completion
13648 * queue and process all the entries on the completion queue, rearm the
13649 * completion queue, and then return.
13650 **/
f485c18d 13651static void
67d12733
JS
13652lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13653 uint32_t qidx)
4f774513 13654{
895427bd 13655 struct lpfc_queue *cq = NULL;
2d7dbc4c 13656 uint16_t cqid, id;
4f774513 13657
cb5172ea 13658 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13660 "0366 Not a valid completion "
4f774513 13661 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13662 bf_get_le32(lpfc_eqe_major_code, eqe),
13663 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13664 return;
4f774513
JS
13665 }
13666
67d12733
JS
13667 /* Get the reference to the corresponding CQ */
13668 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13669
2d7dbc4c
JS
13670 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13671 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13672 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13673 /* Process NVMET unsol rcv */
13674 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13675 goto process_cq;
13676 }
67d12733
JS
13677 }
13678
895427bd
JS
13679 if (phba->sli4_hba.nvme_cq_map &&
13680 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13681 /* Process NVME / NVMET command completion */
895427bd
JS
13682 cq = phba->sli4_hba.nvme_cq[qidx];
13683 goto process_cq;
2e90f4b5 13684 }
67d12733 13685
895427bd
JS
13686 if (phba->sli4_hba.fcp_cq_map &&
13687 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13688 /* Process FCP command completion */
13689 cq = phba->sli4_hba.fcp_cq[qidx];
13690 goto process_cq;
2e90f4b5 13691 }
895427bd
JS
13692
13693 if (phba->sli4_hba.nvmels_cq &&
13694 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13695 /* Process NVME unsol rcv */
13696 cq = phba->sli4_hba.nvmels_cq;
13697 }
13698
13699 /* Otherwise this is a Slow path event */
13700 if (cq == NULL) {
f485c18d
DK
13701 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13702 return;
4f774513
JS
13703 }
13704
895427bd 13705process_cq:
4f774513
JS
13706 if (unlikely(cqid != cq->queue_id)) {
13707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13708 "0368 Miss-matched fast-path completion "
13709 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13710 cqid, cq->queue_id);
f485c18d 13711 return;
4f774513
JS
13712 }
13713
895427bd
JS
13714 /* Save EQ associated with this CQ */
13715 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13716
f485c18d
DK
13717 if (!queue_work(phba->wq, &cq->irqwork))
13718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13719 "0363 Cannot schedule soft IRQ "
13720 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13721 cqid, cq->queue_id, smp_processor_id());
13722}
13723
13724/**
13725 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13726 * @phba: Pointer to HBA context object.
13727 * @eqe: Pointer to fast-path event queue entry.
13728 *
13729 * This routine process a event queue entry from the fast-path event queue.
13730 * It will check the MajorCode and MinorCode to determine this is for a
13731 * completion event on a completion queue, if not, an error shall be logged
13732 * and just return. Otherwise, it will get to the corresponding completion
13733 * queue and process all the entries on the completion queue, rearm the
13734 * completion queue, and then return.
13735 **/
13736static void
13737lpfc_sli4_hba_process_cq(struct work_struct *work)
13738{
13739 struct lpfc_queue *cq =
13740 container_of(work, struct lpfc_queue, irqwork);
13741 struct lpfc_hba *phba = cq->phba;
13742 struct lpfc_cqe *cqe;
13743 bool workposted = false;
13744 int ccount = 0;
13745
4f774513
JS
13746 /* Process all the entries to the CQ */
13747 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13748#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13749 if (phba->ktime_on)
13750 cq->isr_timestamp = ktime_get_ns();
13751 else
13752 cq->isr_timestamp = 0;
13753#endif
895427bd 13754 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13755 if (!(++ccount % cq->entry_repost))
7869da18 13756 break;
4f774513
JS
13757 }
13758
b84daac9 13759 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13760 if (ccount > cq->CQ_max_cqe)
13761 cq->CQ_max_cqe = ccount;
13762 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13763
4f774513 13764 /* Catch the no cq entry condition */
f485c18d 13765 if (unlikely(ccount == 0))
4f774513
JS
13766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13767 "0369 No entry from fast-path completion "
13768 "queue fcpcqid=%d\n", cq->queue_id);
13769
13770 /* In any case, flash and re-arm the CQ */
b71413dd 13771 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13772
13773 /* wake up worker thread if there are works to be done */
13774 if (workposted)
13775 lpfc_worker_wake_up(phba);
13776}
13777
13778static void
13779lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13780{
13781 struct lpfc_eqe *eqe;
13782
13783 /* walk all the EQ entries and drop on the floor */
13784 while ((eqe = lpfc_sli4_eq_get(eq)))
13785 ;
13786
13787 /* Clear and re-arm the EQ */
b71413dd 13788 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
4f774513
JS
13789}
13790
1ba981fd
JS
13791
13792/**
13793 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13794 * entry
13795 * @phba: Pointer to HBA context object.
13796 * @eqe: Pointer to fast-path event queue entry.
13797 *
13798 * This routine process a event queue entry from the Flash Optimized Fabric
13799 * event queue. It will check the MajorCode and MinorCode to determine this
13800 * is for a completion event on a completion queue, if not, an error shall be
13801 * logged and just return. Otherwise, it will get to the corresponding
13802 * completion queue and process all the entries on the completion queue, rearm
13803 * the completion queue, and then return.
13804 **/
13805static void
13806lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13807{
13808 struct lpfc_queue *cq;
1ba981fd 13809 uint16_t cqid;
1ba981fd
JS
13810
13811 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13813 "9147 Not a valid completion "
13814 "event: majorcode=x%x, minorcode=x%x\n",
13815 bf_get_le32(lpfc_eqe_major_code, eqe),
13816 bf_get_le32(lpfc_eqe_minor_code, eqe));
13817 return;
13818 }
13819
13820 /* Get the reference to the corresponding CQ */
13821 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13822
13823 /* Next check for OAS */
13824 cq = phba->sli4_hba.oas_cq;
13825 if (unlikely(!cq)) {
13826 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13828 "9148 OAS completion queue "
13829 "does not exist\n");
13830 return;
13831 }
13832
13833 if (unlikely(cqid != cq->queue_id)) {
13834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13835 "9149 Miss-matched fast-path compl "
13836 "queue id: eqcqid=%d, fcpcqid=%d\n",
13837 cqid, cq->queue_id);
13838 return;
13839 }
13840
d41b65bc
JS
13841 /* Save EQ associated with this CQ */
13842 cq->assoc_qp = phba->sli4_hba.fof_eq;
13843
f485c18d
DK
13844 /* CQ work will be processed on CPU affinitized to this IRQ */
13845 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13846 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13847 "0367 Cannot schedule soft IRQ "
13848 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13849 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13850}
13851
13852/**
13853 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13854 * @irq: Interrupt number.
13855 * @dev_id: The device context pointer.
13856 *
13857 * This function is directly called from the PCI layer as an interrupt
13858 * service routine when device with SLI-4 interface spec is enabled with
13859 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13860 * IOCB ring event in the HBA. However, when the device is enabled with either
13861 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13862 * device-level interrupt handler. When the PCI slot is in error recovery
13863 * or the HBA is undergoing initialization, the interrupt handler will not
13864 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13865 * the intrrupt context. This function is called without any lock held.
13866 * It gets the hbalock to access and update SLI data structures. Note that,
13867 * the EQ to CQ are one-to-one map such that the EQ index is
13868 * equal to that of CQ index.
13869 *
13870 * This function returns IRQ_HANDLED when interrupt is handled else it
13871 * returns IRQ_NONE.
13872 **/
13873irqreturn_t
13874lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13875{
13876 struct lpfc_hba *phba;
895427bd 13877 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13878 struct lpfc_queue *eq;
13879 struct lpfc_eqe *eqe;
13880 unsigned long iflag;
13881 int ecount = 0;
1ba981fd
JS
13882
13883 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13884 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13885 phba = hba_eq_hdl->phba;
1ba981fd
JS
13886
13887 if (unlikely(!phba))
13888 return IRQ_NONE;
13889
13890 /* Get to the EQ struct associated with this vector */
13891 eq = phba->sli4_hba.fof_eq;
13892 if (unlikely(!eq))
13893 return IRQ_NONE;
13894
13895 /* Check device state for handling interrupt */
13896 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13897 /* Check again for link_state with lock held */
13898 spin_lock_irqsave(&phba->hbalock, iflag);
13899 if (phba->link_state < LPFC_LINK_DOWN)
13900 /* Flush, clear interrupt, and rearm the EQ */
13901 lpfc_sli4_eq_flush(phba, eq);
13902 spin_unlock_irqrestore(&phba->hbalock, iflag);
13903 return IRQ_NONE;
13904 }
13905
13906 /*
13907 * Process all the event on FCP fast-path EQ
13908 */
13909 while ((eqe = lpfc_sli4_eq_get(eq))) {
13910 lpfc_sli4_fof_handle_eqe(phba, eqe);
13911 if (!(++ecount % eq->entry_repost))
7869da18 13912 break;
1ba981fd
JS
13913 eq->EQ_processed++;
13914 }
13915
13916 /* Track the max number of EQEs processed in 1 intr */
13917 if (ecount > eq->EQ_max_eqe)
13918 eq->EQ_max_eqe = ecount;
13919
13920
13921 if (unlikely(ecount == 0)) {
13922 eq->EQ_no_entry++;
13923
13924 if (phba->intr_type == MSIX)
13925 /* MSI-X treated interrupt served as no EQ share INT */
13926 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13927 "9145 MSI-X interrupt with no EQE\n");
13928 else {
13929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13930 "9146 ISR interrupt with no EQE\n");
13931 /* Non MSI-X treated on interrupt as EQ share INT */
13932 return IRQ_NONE;
13933 }
13934 }
13935 /* Always clear and re-arm the fast-path EQ */
b71413dd 13936 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
1ba981fd
JS
13937 return IRQ_HANDLED;
13938}
13939
4f774513 13940/**
67d12733 13941 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13942 * @irq: Interrupt number.
13943 * @dev_id: The device context pointer.
13944 *
13945 * This function is directly called from the PCI layer as an interrupt
13946 * service routine when device with SLI-4 interface spec is enabled with
13947 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13948 * ring event in the HBA. However, when the device is enabled with either
13949 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13950 * device-level interrupt handler. When the PCI slot is in error recovery
13951 * or the HBA is undergoing initialization, the interrupt handler will not
13952 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13953 * the intrrupt context. This function is called without any lock held.
13954 * It gets the hbalock to access and update SLI data structures. Note that,
13955 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13956 * equal to that of FCP CQ index.
13957 *
67d12733
JS
13958 * The link attention and ELS ring attention events are handled
13959 * by the worker thread. The interrupt handler signals the worker thread
13960 * and returns for these events. This function is called without any lock
13961 * held. It gets the hbalock to access and update SLI data structures.
13962 *
4f774513
JS
13963 * This function returns IRQ_HANDLED when interrupt is handled else it
13964 * returns IRQ_NONE.
13965 **/
13966irqreturn_t
67d12733 13967lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13968{
13969 struct lpfc_hba *phba;
895427bd 13970 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13971 struct lpfc_queue *fpeq;
13972 struct lpfc_eqe *eqe;
13973 unsigned long iflag;
13974 int ecount = 0;
895427bd 13975 int hba_eqidx;
4f774513
JS
13976
13977 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13978 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13979 phba = hba_eq_hdl->phba;
13980 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13981
13982 if (unlikely(!phba))
13983 return IRQ_NONE;
67d12733 13984 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13985 return IRQ_NONE;
4f774513
JS
13986
13987 /* Get to the EQ struct associated with this vector */
895427bd 13988 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13989 if (unlikely(!fpeq))
13990 return IRQ_NONE;
4f774513 13991
ba20c853 13992 if (lpfc_fcp_look_ahead) {
895427bd 13993 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
b71413dd 13994 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 13995 else {
895427bd 13996 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13997 return IRQ_NONE;
13998 }
13999 }
14000
4f774513
JS
14001 /* Check device state for handling interrupt */
14002 if (unlikely(lpfc_intr_state_check(phba))) {
14003 /* Check again for link_state with lock held */
14004 spin_lock_irqsave(&phba->hbalock, iflag);
14005 if (phba->link_state < LPFC_LINK_DOWN)
14006 /* Flush, clear interrupt, and rearm the EQ */
14007 lpfc_sli4_eq_flush(phba, fpeq);
14008 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 14009 if (lpfc_fcp_look_ahead)
895427bd 14010 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
14011 return IRQ_NONE;
14012 }
14013
14014 /*
14015 * Process all the event on FCP fast-path EQ
14016 */
14017 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
14018 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14019 if (!(++ecount % fpeq->entry_repost))
7869da18 14020 break;
b84daac9 14021 fpeq->EQ_processed++;
4f774513
JS
14022 }
14023
b84daac9
JS
14024 /* Track the max number of EQEs processed in 1 intr */
14025 if (ecount > fpeq->EQ_max_eqe)
14026 fpeq->EQ_max_eqe = ecount;
14027
4f774513 14028 /* Always clear and re-arm the fast-path EQ */
b71413dd 14029 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
4f774513
JS
14030
14031 if (unlikely(ecount == 0)) {
b84daac9 14032 fpeq->EQ_no_entry++;
ba20c853
JS
14033
14034 if (lpfc_fcp_look_ahead) {
895427bd 14035 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14036 return IRQ_NONE;
14037 }
14038
4f774513
JS
14039 if (phba->intr_type == MSIX)
14040 /* MSI-X treated interrupt served as no EQ share INT */
14041 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14042 "0358 MSI-X interrupt with no EQE\n");
14043 else
14044 /* Non MSI-X treated on interrupt as EQ share INT */
14045 return IRQ_NONE;
14046 }
14047
ba20c853 14048 if (lpfc_fcp_look_ahead)
895427bd
JS
14049 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14050
4f774513
JS
14051 return IRQ_HANDLED;
14052} /* lpfc_sli4_fp_intr_handler */
14053
14054/**
14055 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14056 * @irq: Interrupt number.
14057 * @dev_id: The device context pointer.
14058 *
14059 * This function is the device-level interrupt handler to device with SLI-4
14060 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14061 * interrupt mode is enabled and there is an event in the HBA which requires
14062 * driver attention. This function invokes the slow-path interrupt attention
14063 * handling function and fast-path interrupt attention handling function in
14064 * turn to process the relevant HBA attention events. This function is called
14065 * without any lock held. It gets the hbalock to access and update SLI data
14066 * structures.
14067 *
14068 * This function returns IRQ_HANDLED when interrupt is handled, else it
14069 * returns IRQ_NONE.
14070 **/
14071irqreturn_t
14072lpfc_sli4_intr_handler(int irq, void *dev_id)
14073{
14074 struct lpfc_hba *phba;
67d12733
JS
14075 irqreturn_t hba_irq_rc;
14076 bool hba_handled = false;
895427bd 14077 int qidx;
4f774513
JS
14078
14079 /* Get the driver's phba structure from the dev_id */
14080 phba = (struct lpfc_hba *)dev_id;
14081
14082 if (unlikely(!phba))
14083 return IRQ_NONE;
14084
4f774513
JS
14085 /*
14086 * Invoke fast-path host attention interrupt handling as appropriate.
14087 */
895427bd 14088 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 14089 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 14090 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
14091 if (hba_irq_rc == IRQ_HANDLED)
14092 hba_handled |= true;
4f774513
JS
14093 }
14094
1ba981fd
JS
14095 if (phba->cfg_fof) {
14096 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 14097 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
14098 if (hba_irq_rc == IRQ_HANDLED)
14099 hba_handled |= true;
14100 }
14101
67d12733 14102 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
14103} /* lpfc_sli4_intr_handler */
14104
14105/**
14106 * lpfc_sli4_queue_free - free a queue structure and associated memory
14107 * @queue: The queue structure to free.
14108 *
b595076a 14109 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
14110 * the host resident queue. This function must be called after destroying the
14111 * queue on the HBA.
14112 **/
14113void
14114lpfc_sli4_queue_free(struct lpfc_queue *queue)
14115{
14116 struct lpfc_dmabuf *dmabuf;
14117
14118 if (!queue)
14119 return;
14120
14121 while (!list_empty(&queue->page_list)) {
14122 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14123 list);
81b96eda 14124 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
14125 dmabuf->virt, dmabuf->phys);
14126 kfree(dmabuf);
14127 }
895427bd
JS
14128 if (queue->rqbp) {
14129 lpfc_free_rq_buffer(queue->phba, queue);
14130 kfree(queue->rqbp);
14131 }
d1f525aa
JS
14132
14133 if (!list_empty(&queue->wq_list))
14134 list_del(&queue->wq_list);
14135
4f774513
JS
14136 kfree(queue);
14137 return;
14138}
14139
14140/**
14141 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14142 * @phba: The HBA that this queue is being created on.
81b96eda 14143 * @page_size: The size of a queue page
4f774513
JS
14144 * @entry_size: The size of each queue entry for this queue.
14145 * @entry count: The number of entries that this queue will handle.
14146 *
14147 * This function allocates a queue structure and the DMAable memory used for
14148 * the host resident queue. This function must be called before creating the
14149 * queue on the HBA.
14150 **/
14151struct lpfc_queue *
81b96eda
JS
14152lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14153 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
14154{
14155 struct lpfc_queue *queue;
14156 struct lpfc_dmabuf *dmabuf;
14157 int x, total_qe_count;
14158 void *dma_pointer;
cb5172ea 14159 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 14160
cb5172ea 14161 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14162 hw_page_size = page_size;
cb5172ea 14163
4f774513
JS
14164 queue = kzalloc(sizeof(struct lpfc_queue) +
14165 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14166 if (!queue)
14167 return NULL;
cb5172ea
JS
14168 queue->page_count = (ALIGN(entry_size * entry_count,
14169 hw_page_size))/hw_page_size;
895427bd
JS
14170
14171 /* If needed, Adjust page count to match the max the adapter supports */
14172 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14173 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14174
4f774513 14175 INIT_LIST_HEAD(&queue->list);
895427bd 14176 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 14177 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
14178 INIT_LIST_HEAD(&queue->page_list);
14179 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
14180
14181 /* Set queue parameters now. If the system cannot provide memory
14182 * resources, the free routine needs to know what was allocated.
14183 */
14184 queue->entry_size = entry_size;
14185 queue->entry_count = entry_count;
14186 queue->page_size = hw_page_size;
14187 queue->phba = phba;
14188
4f774513
JS
14189 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14190 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14191 if (!dmabuf)
14192 goto out_fail;
1aee383d
JP
14193 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14194 hw_page_size, &dmabuf->phys,
14195 GFP_KERNEL);
4f774513
JS
14196 if (!dmabuf->virt) {
14197 kfree(dmabuf);
14198 goto out_fail;
14199 }
14200 dmabuf->buffer_tag = x;
14201 list_add_tail(&dmabuf->list, &queue->page_list);
14202 /* initialize queue's entry array */
14203 dma_pointer = dmabuf->virt;
14204 for (; total_qe_count < entry_count &&
cb5172ea 14205 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
14206 total_qe_count++, dma_pointer += entry_size) {
14207 queue->qe[total_qe_count].address = dma_pointer;
14208 }
14209 }
f485c18d
DK
14210 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14211 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 14212
64eb4dcb
JS
14213 /* entry_repost will be set during q creation */
14214
4f774513
JS
14215 return queue;
14216out_fail:
14217 lpfc_sli4_queue_free(queue);
14218 return NULL;
14219}
14220
962bc51b
JS
14221/**
14222 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14223 * @phba: HBA structure that indicates port to create a queue on.
14224 * @pci_barset: PCI BAR set flag.
14225 *
14226 * This function shall perform iomap of the specified PCI BAR address to host
14227 * memory address if not already done so and return it. The returned host
14228 * memory address can be NULL.
14229 */
14230static void __iomem *
14231lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14232{
962bc51b
JS
14233 if (!phba->pcidev)
14234 return NULL;
962bc51b
JS
14235
14236 switch (pci_barset) {
14237 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14238 return phba->pci_bar0_memmap_p;
14239 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14240 return phba->pci_bar2_memmap_p;
14241 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14242 return phba->pci_bar4_memmap_p;
14243 default:
14244 break;
14245 }
14246 return NULL;
14247}
14248
173edbb2 14249/**
895427bd 14250 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14251 * @phba: HBA structure that indicates port to create a queue on.
14252 * @startq: The starting FCP EQ to modify
14253 *
14254 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14255 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14256 * updated in one mailbox command.
173edbb2
JS
14257 *
14258 * The @phba struct is used to send mailbox command to HBA. The @startq
14259 * is used to get the starting FCP EQ to change.
14260 * This function is asynchronous and will wait for the mailbox
14261 * command to finish before continuing.
14262 *
14263 * On success this function will return a zero. If unable to allocate enough
14264 * memory this function will return -ENOMEM. If the queue create mailbox command
14265 * fails this function will return -ENXIO.
14266 **/
a2fc4aef 14267int
0cf07f84
JS
14268lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14269 uint32_t numq, uint32_t imax)
173edbb2
JS
14270{
14271 struct lpfc_mbx_modify_eq_delay *eq_delay;
14272 LPFC_MBOXQ_t *mbox;
14273 struct lpfc_queue *eq;
14274 int cnt, rc, length, status = 0;
14275 uint32_t shdr_status, shdr_add_status;
0cf07f84 14276 uint32_t result, val;
895427bd 14277 int qidx;
173edbb2
JS
14278 union lpfc_sli4_cfg_shdr *shdr;
14279 uint16_t dmult;
14280
895427bd 14281 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14282 return 0;
14283
14284 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14285 if (!mbox)
14286 return -ENOMEM;
14287 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14288 sizeof(struct lpfc_sli4_cfg_mhdr));
14289 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14290 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14291 length, LPFC_SLI4_MBX_EMBED);
14292 eq_delay = &mbox->u.mqe.un.eq_delay;
14293
14294 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14295 result = imax / phba->io_channel_irqs;
895427bd 14296 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14297 dmult = 0;
14298 else
14299 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14300 if (dmult > LPFC_DMULT_MAX)
14301 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14302
14303 cnt = 0;
895427bd
JS
14304 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14305 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14306 if (!eq)
14307 continue;
0cf07f84 14308 eq->q_mode = imax;
173edbb2
JS
14309 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14310 eq_delay->u.request.eq[cnt].phase = 0;
14311 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14312 cnt++;
0cf07f84
JS
14313
14314 /* q_mode is only used for auto_imax */
14315 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14316 /* Use EQ Delay Register method for q_mode */
14317
14318 /* Convert for EQ Delay register */
14319 val = phba->cfg_fcp_imax;
14320 if (val) {
14321 /* First, interrupts per sec per EQ */
14322 val = phba->cfg_fcp_imax /
14323 phba->io_channel_irqs;
14324
14325 /* us delay between each interrupt */
14326 val = LPFC_SEC_TO_USEC / val;
14327 }
14328 eq->q_mode = val;
14329 } else {
14330 eq->q_mode = imax;
14331 }
14332
14333 if (cnt >= numq)
173edbb2
JS
14334 break;
14335 }
14336 eq_delay->u.request.num_eq = cnt;
14337
14338 mbox->vport = phba->pport;
14339 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14340 mbox->context1 = NULL;
14341 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14342 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14343 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14344 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14345 if (shdr_status || shdr_add_status || rc) {
14346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14347 "2512 MODIFY_EQ_DELAY mailbox failed with "
14348 "status x%x add_status x%x, mbx status x%x\n",
14349 shdr_status, shdr_add_status, rc);
14350 status = -ENXIO;
14351 }
14352 mempool_free(mbox, phba->mbox_mem_pool);
14353 return status;
14354}
14355
4f774513
JS
14356/**
14357 * lpfc_eq_create - Create an Event Queue on the HBA
14358 * @phba: HBA structure that indicates port to create a queue on.
14359 * @eq: The queue structure to use to create the event queue.
14360 * @imax: The maximum interrupt per second limit.
14361 *
14362 * This function creates an event queue, as detailed in @eq, on a port,
14363 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14364 *
14365 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14366 * is used to get the entry count and entry size that are necessary to
14367 * determine the number of pages to allocate and use for this queue. This
14368 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14369 * event queue. This function is asynchronous and will wait for the mailbox
14370 * command to finish before continuing.
14371 *
14372 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14373 * memory this function will return -ENOMEM. If the queue create mailbox command
14374 * fails this function will return -ENXIO.
4f774513 14375 **/
a2fc4aef 14376int
ee02006b 14377lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14378{
14379 struct lpfc_mbx_eq_create *eq_create;
14380 LPFC_MBOXQ_t *mbox;
14381 int rc, length, status = 0;
14382 struct lpfc_dmabuf *dmabuf;
14383 uint32_t shdr_status, shdr_add_status;
14384 union lpfc_sli4_cfg_shdr *shdr;
14385 uint16_t dmult;
49198b37
JS
14386 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14387
2e90f4b5
JS
14388 /* sanity check on queue memory */
14389 if (!eq)
14390 return -ENODEV;
49198b37
JS
14391 if (!phba->sli4_hba.pc_sli4_params.supported)
14392 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14393
14394 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14395 if (!mbox)
14396 return -ENOMEM;
14397 length = (sizeof(struct lpfc_mbx_eq_create) -
14398 sizeof(struct lpfc_sli4_cfg_mhdr));
14399 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14400 LPFC_MBOX_OPCODE_EQ_CREATE,
14401 length, LPFC_SLI4_MBX_EMBED);
14402 eq_create = &mbox->u.mqe.un.eq_create;
7365f6fd 14403 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
4f774513
JS
14404 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14405 eq->page_count);
14406 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14407 LPFC_EQE_SIZE);
14408 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
7365f6fd
JS
14409
14410 /* Use version 2 of CREATE_EQ if eqav is set */
14411 if (phba->sli4_hba.pc_sli4_params.eqav) {
14412 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14413 LPFC_Q_CREATE_VERSION_2);
14414 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14415 phba->sli4_hba.pc_sli4_params.eqav);
14416 }
14417
2c9c5a00
JS
14418 /* don't setup delay multiplier using EQ_CREATE */
14419 dmult = 0;
4f774513
JS
14420 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14421 dmult);
14422 switch (eq->entry_count) {
14423 default:
14424 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14425 "0360 Unsupported EQ count. (%d)\n",
14426 eq->entry_count);
14427 if (eq->entry_count < 256)
14428 return -EINVAL;
14429 /* otherwise default to smallest count (drop through) */
14430 case 256:
14431 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14432 LPFC_EQ_CNT_256);
14433 break;
14434 case 512:
14435 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14436 LPFC_EQ_CNT_512);
14437 break;
14438 case 1024:
14439 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14440 LPFC_EQ_CNT_1024);
14441 break;
14442 case 2048:
14443 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14444 LPFC_EQ_CNT_2048);
14445 break;
14446 case 4096:
14447 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14448 LPFC_EQ_CNT_4096);
14449 break;
14450 }
14451 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14452 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14453 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14454 putPaddrLow(dmabuf->phys);
14455 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14456 putPaddrHigh(dmabuf->phys);
14457 }
14458 mbox->vport = phba->pport;
14459 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14460 mbox->context1 = NULL;
14461 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4f774513
JS
14462 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14463 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14464 if (shdr_status || shdr_add_status || rc) {
14465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14466 "2500 EQ_CREATE mailbox failed with "
14467 "status x%x add_status x%x, mbx status x%x\n",
14468 shdr_status, shdr_add_status, rc);
14469 status = -ENXIO;
14470 }
14471 eq->type = LPFC_EQ;
14472 eq->subtype = LPFC_NONE;
14473 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14474 if (eq->queue_id == 0xFFFF)
14475 status = -ENXIO;
14476 eq->host_index = 0;
14477 eq->hba_index = 0;
64eb4dcb 14478 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14479
8fa38513 14480 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14481 return status;
14482}
14483
14484/**
14485 * lpfc_cq_create - Create a Completion Queue on the HBA
14486 * @phba: HBA structure that indicates port to create a queue on.
14487 * @cq: The queue structure to use to create the completion queue.
14488 * @eq: The event queue to bind this completion queue to.
14489 *
14490 * This function creates a completion queue, as detailed in @wq, on a port,
14491 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14492 *
14493 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14494 * is used to get the entry count and entry size that are necessary to
14495 * determine the number of pages to allocate and use for this queue. The @eq
14496 * is used to indicate which event queue to bind this completion queue to. This
14497 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14498 * completion queue. This function is asynchronous and will wait for the mailbox
14499 * command to finish before continuing.
14500 *
14501 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14502 * memory this function will return -ENOMEM. If the queue create mailbox command
14503 * fails this function will return -ENXIO.
4f774513 14504 **/
a2fc4aef 14505int
4f774513
JS
14506lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14507 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14508{
14509 struct lpfc_mbx_cq_create *cq_create;
14510 struct lpfc_dmabuf *dmabuf;
14511 LPFC_MBOXQ_t *mbox;
14512 int rc, length, status = 0;
14513 uint32_t shdr_status, shdr_add_status;
14514 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14515 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14516
2e90f4b5
JS
14517 /* sanity check on queue memory */
14518 if (!cq || !eq)
14519 return -ENODEV;
49198b37 14520 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14521 hw_page_size = cq->page_size;
49198b37 14522
4f774513
JS
14523 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14524 if (!mbox)
14525 return -ENOMEM;
14526 length = (sizeof(struct lpfc_mbx_cq_create) -
14527 sizeof(struct lpfc_sli4_cfg_mhdr));
14528 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14529 LPFC_MBOX_OPCODE_CQ_CREATE,
14530 length, LPFC_SLI4_MBX_EMBED);
14531 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14532 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14533 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14534 cq->page_count);
14535 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14536 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14537 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14538 phba->sli4_hba.pc_sli4_params.cqv);
14539 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14540 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14541 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14542 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14543 eq->queue_id);
7365f6fd
JS
14544 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14545 phba->sli4_hba.pc_sli4_params.cqav);
5a6f133e
JS
14546 } else {
14547 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14548 eq->queue_id);
14549 }
4f774513 14550 switch (cq->entry_count) {
81b96eda
JS
14551 case 2048:
14552 case 4096:
14553 if (phba->sli4_hba.pc_sli4_params.cqv ==
14554 LPFC_Q_CREATE_VERSION_2) {
14555 cq_create->u.request.context.lpfc_cq_context_count =
14556 cq->entry_count;
14557 bf_set(lpfc_cq_context_count,
14558 &cq_create->u.request.context,
14559 LPFC_CQ_CNT_WORD7);
14560 break;
14561 }
14562 /* Fall Thru */
4f774513
JS
14563 default:
14564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14565 "0361 Unsupported CQ count: "
64eb4dcb 14566 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14567 cq->entry_count, cq->entry_size,
64eb4dcb 14568 cq->page_count);
4f4c1863
JS
14569 if (cq->entry_count < 256) {
14570 status = -EINVAL;
14571 goto out;
14572 }
4f774513
JS
14573 /* otherwise default to smallest count (drop through) */
14574 case 256:
14575 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14576 LPFC_CQ_CNT_256);
14577 break;
14578 case 512:
14579 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14580 LPFC_CQ_CNT_512);
14581 break;
14582 case 1024:
14583 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14584 LPFC_CQ_CNT_1024);
14585 break;
14586 }
14587 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14588 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14589 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14590 putPaddrLow(dmabuf->phys);
14591 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14592 putPaddrHigh(dmabuf->phys);
14593 }
14594 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14595
14596 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14597 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14598 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14599 if (shdr_status || shdr_add_status || rc) {
14600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14601 "2501 CQ_CREATE mailbox failed with "
14602 "status x%x add_status x%x, mbx status x%x\n",
14603 shdr_status, shdr_add_status, rc);
14604 status = -ENXIO;
14605 goto out;
14606 }
14607 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14608 if (cq->queue_id == 0xFFFF) {
14609 status = -ENXIO;
14610 goto out;
14611 }
14612 /* link the cq onto the parent eq child list */
14613 list_add_tail(&cq->list, &eq->child_list);
14614 /* Set up completion queue's type and subtype */
14615 cq->type = type;
14616 cq->subtype = subtype;
14617 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14618 cq->assoc_qid = eq->queue_id;
4f774513
JS
14619 cq->host_index = 0;
14620 cq->hba_index = 0;
64eb4dcb 14621 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14622
8fa38513
JS
14623out:
14624 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14625 return status;
14626}
14627
2d7dbc4c
JS
14628/**
14629 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14630 * @phba: HBA structure that indicates port to create a queue on.
14631 * @cqp: The queue structure array to use to create the completion queues.
14632 * @eqp: The event queue array to bind these completion queues to.
14633 *
14634 * This function creates a set of completion queue, s to support MRQ
14635 * as detailed in @cqp, on a port,
14636 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14637 *
14638 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14639 * is used to get the entry count and entry size that are necessary to
14640 * determine the number of pages to allocate and use for this queue. The @eq
14641 * is used to indicate which event queue to bind this completion queue to. This
14642 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14643 * completion queue. This function is asynchronous and will wait for the mailbox
14644 * command to finish before continuing.
14645 *
14646 * On success this function will return a zero. If unable to allocate enough
14647 * memory this function will return -ENOMEM. If the queue create mailbox command
14648 * fails this function will return -ENXIO.
14649 **/
14650int
14651lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14652 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14653{
14654 struct lpfc_queue *cq;
14655 struct lpfc_queue *eq;
14656 struct lpfc_mbx_cq_create_set *cq_set;
14657 struct lpfc_dmabuf *dmabuf;
14658 LPFC_MBOXQ_t *mbox;
14659 int rc, length, alloclen, status = 0;
14660 int cnt, idx, numcq, page_idx = 0;
14661 uint32_t shdr_status, shdr_add_status;
14662 union lpfc_sli4_cfg_shdr *shdr;
14663 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14664
14665 /* sanity check on queue memory */
14666 numcq = phba->cfg_nvmet_mrq;
14667 if (!cqp || !eqp || !numcq)
14668 return -ENODEV;
2d7dbc4c
JS
14669
14670 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14671 if (!mbox)
14672 return -ENOMEM;
14673
14674 length = sizeof(struct lpfc_mbx_cq_create_set);
14675 length += ((numcq * cqp[0]->page_count) *
14676 sizeof(struct dma_address));
14677 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14678 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14679 LPFC_SLI4_MBX_NEMBED);
14680 if (alloclen < length) {
14681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14682 "3098 Allocated DMA memory size (%d) is "
14683 "less than the requested DMA memory size "
14684 "(%d)\n", alloclen, length);
14685 status = -ENOMEM;
14686 goto out;
14687 }
14688 cq_set = mbox->sge_array->addr[0];
14689 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14690 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14691
14692 for (idx = 0; idx < numcq; idx++) {
14693 cq = cqp[idx];
14694 eq = eqp[idx];
14695 if (!cq || !eq) {
14696 status = -ENOMEM;
14697 goto out;
14698 }
81b96eda
JS
14699 if (!phba->sli4_hba.pc_sli4_params.supported)
14700 hw_page_size = cq->page_size;
2d7dbc4c
JS
14701
14702 switch (idx) {
14703 case 0:
14704 bf_set(lpfc_mbx_cq_create_set_page_size,
14705 &cq_set->u.request,
14706 (hw_page_size / SLI4_PAGE_SIZE));
14707 bf_set(lpfc_mbx_cq_create_set_num_pages,
14708 &cq_set->u.request, cq->page_count);
14709 bf_set(lpfc_mbx_cq_create_set_evt,
14710 &cq_set->u.request, 1);
14711 bf_set(lpfc_mbx_cq_create_set_valid,
14712 &cq_set->u.request, 1);
14713 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14714 &cq_set->u.request, 0);
14715 bf_set(lpfc_mbx_cq_create_set_num_cq,
14716 &cq_set->u.request, numcq);
7365f6fd
JS
14717 bf_set(lpfc_mbx_cq_create_set_autovalid,
14718 &cq_set->u.request,
14719 phba->sli4_hba.pc_sli4_params.cqav);
2d7dbc4c 14720 switch (cq->entry_count) {
81b96eda
JS
14721 case 2048:
14722 case 4096:
14723 if (phba->sli4_hba.pc_sli4_params.cqv ==
14724 LPFC_Q_CREATE_VERSION_2) {
14725 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14726 &cq_set->u.request,
14727 cq->entry_count);
14728 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14729 &cq_set->u.request,
14730 LPFC_CQ_CNT_WORD7);
14731 break;
14732 }
14733 /* Fall Thru */
2d7dbc4c
JS
14734 default:
14735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14736 "3118 Bad CQ count. (%d)\n",
14737 cq->entry_count);
14738 if (cq->entry_count < 256) {
14739 status = -EINVAL;
14740 goto out;
14741 }
14742 /* otherwise default to smallest (drop thru) */
14743 case 256:
14744 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14745 &cq_set->u.request, LPFC_CQ_CNT_256);
14746 break;
14747 case 512:
14748 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14749 &cq_set->u.request, LPFC_CQ_CNT_512);
14750 break;
14751 case 1024:
14752 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14753 &cq_set->u.request, LPFC_CQ_CNT_1024);
14754 break;
14755 }
14756 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14757 &cq_set->u.request, eq->queue_id);
14758 break;
14759 case 1:
14760 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14761 &cq_set->u.request, eq->queue_id);
14762 break;
14763 case 2:
14764 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14765 &cq_set->u.request, eq->queue_id);
14766 break;
14767 case 3:
14768 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14769 &cq_set->u.request, eq->queue_id);
14770 break;
14771 case 4:
14772 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14773 &cq_set->u.request, eq->queue_id);
14774 break;
14775 case 5:
14776 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14777 &cq_set->u.request, eq->queue_id);
14778 break;
14779 case 6:
14780 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14781 &cq_set->u.request, eq->queue_id);
14782 break;
14783 case 7:
14784 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14785 &cq_set->u.request, eq->queue_id);
14786 break;
14787 case 8:
14788 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14789 &cq_set->u.request, eq->queue_id);
14790 break;
14791 case 9:
14792 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14793 &cq_set->u.request, eq->queue_id);
14794 break;
14795 case 10:
14796 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14797 &cq_set->u.request, eq->queue_id);
14798 break;
14799 case 11:
14800 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14801 &cq_set->u.request, eq->queue_id);
14802 break;
14803 case 12:
14804 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14805 &cq_set->u.request, eq->queue_id);
14806 break;
14807 case 13:
14808 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14809 &cq_set->u.request, eq->queue_id);
14810 break;
14811 case 14:
14812 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14813 &cq_set->u.request, eq->queue_id);
14814 break;
14815 case 15:
14816 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14817 &cq_set->u.request, eq->queue_id);
14818 break;
14819 }
14820
14821 /* link the cq onto the parent eq child list */
14822 list_add_tail(&cq->list, &eq->child_list);
14823 /* Set up completion queue's type and subtype */
14824 cq->type = type;
14825 cq->subtype = subtype;
14826 cq->assoc_qid = eq->queue_id;
14827 cq->host_index = 0;
14828 cq->hba_index = 0;
64eb4dcb 14829 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14830 cq->chann = idx;
2d7dbc4c
JS
14831
14832 rc = 0;
14833 list_for_each_entry(dmabuf, &cq->page_list, list) {
14834 memset(dmabuf->virt, 0, hw_page_size);
14835 cnt = page_idx + dmabuf->buffer_tag;
14836 cq_set->u.request.page[cnt].addr_lo =
14837 putPaddrLow(dmabuf->phys);
14838 cq_set->u.request.page[cnt].addr_hi =
14839 putPaddrHigh(dmabuf->phys);
14840 rc++;
14841 }
14842 page_idx += rc;
14843 }
14844
14845 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14846
14847 /* The IOCTL status is embedded in the mailbox subheader. */
14848 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14849 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14850 if (shdr_status || shdr_add_status || rc) {
14851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14852 "3119 CQ_CREATE_SET mailbox failed with "
14853 "status x%x add_status x%x, mbx status x%x\n",
14854 shdr_status, shdr_add_status, rc);
14855 status = -ENXIO;
14856 goto out;
14857 }
14858 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14859 if (rc == 0xFFFF) {
14860 status = -ENXIO;
14861 goto out;
14862 }
14863
14864 for (idx = 0; idx < numcq; idx++) {
14865 cq = cqp[idx];
14866 cq->queue_id = rc + idx;
14867 }
14868
14869out:
14870 lpfc_sli4_mbox_cmd_free(phba, mbox);
14871 return status;
14872}
14873
b19a061a
JS
14874/**
14875 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14876 * @phba: HBA structure that indicates port to create a queue on.
14877 * @mq: The queue structure to use to create the mailbox queue.
14878 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14879 * @cq: The completion queue to associate with this cq.
14880 *
14881 * This function provides failback (fb) functionality when the
14882 * mq_create_ext fails on older FW generations. It's purpose is identical
14883 * to mq_create_ext otherwise.
14884 *
14885 * This routine cannot fail as all attributes were previously accessed and
14886 * initialized in mq_create_ext.
14887 **/
14888static void
14889lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14890 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14891{
14892 struct lpfc_mbx_mq_create *mq_create;
14893 struct lpfc_dmabuf *dmabuf;
14894 int length;
14895
14896 length = (sizeof(struct lpfc_mbx_mq_create) -
14897 sizeof(struct lpfc_sli4_cfg_mhdr));
14898 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14899 LPFC_MBOX_OPCODE_MQ_CREATE,
14900 length, LPFC_SLI4_MBX_EMBED);
14901 mq_create = &mbox->u.mqe.un.mq_create;
14902 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14903 mq->page_count);
14904 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14905 cq->queue_id);
14906 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14907 switch (mq->entry_count) {
14908 case 16:
5a6f133e
JS
14909 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14910 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14911 break;
14912 case 32:
5a6f133e
JS
14913 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14914 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14915 break;
14916 case 64:
5a6f133e
JS
14917 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14918 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14919 break;
14920 case 128:
5a6f133e
JS
14921 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14922 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14923 break;
14924 }
14925 list_for_each_entry(dmabuf, &mq->page_list, list) {
14926 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14927 putPaddrLow(dmabuf->phys);
14928 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14929 putPaddrHigh(dmabuf->phys);
14930 }
14931}
14932
04c68496
JS
14933/**
14934 * lpfc_mq_create - Create a mailbox Queue on the HBA
14935 * @phba: HBA structure that indicates port to create a queue on.
14936 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14937 * @cq: The completion queue to associate with this cq.
14938 * @subtype: The queue's subtype.
04c68496
JS
14939 *
14940 * This function creates a mailbox queue, as detailed in @mq, on a port,
14941 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14942 *
14943 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14944 * is used to get the entry count and entry size that are necessary to
14945 * determine the number of pages to allocate and use for this queue. This
14946 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14947 * mailbox queue. This function is asynchronous and will wait for the mailbox
14948 * command to finish before continuing.
14949 *
14950 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14951 * memory this function will return -ENOMEM. If the queue create mailbox command
14952 * fails this function will return -ENXIO.
04c68496 14953 **/
b19a061a 14954int32_t
04c68496
JS
14955lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14956 struct lpfc_queue *cq, uint32_t subtype)
14957{
14958 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14959 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14960 struct lpfc_dmabuf *dmabuf;
14961 LPFC_MBOXQ_t *mbox;
14962 int rc, length, status = 0;
14963 uint32_t shdr_status, shdr_add_status;
14964 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14965 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14966
2e90f4b5
JS
14967 /* sanity check on queue memory */
14968 if (!mq || !cq)
14969 return -ENODEV;
49198b37
JS
14970 if (!phba->sli4_hba.pc_sli4_params.supported)
14971 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14972
04c68496
JS
14973 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14974 if (!mbox)
14975 return -ENOMEM;
b19a061a 14976 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14977 sizeof(struct lpfc_sli4_cfg_mhdr));
14978 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14979 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14980 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14981
14982 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14983 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14984 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14985 &mq_create_ext->u.request, mq->page_count);
14986 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14987 &mq_create_ext->u.request, 1);
14988 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14989 &mq_create_ext->u.request, 1);
14990 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14991 &mq_create_ext->u.request, 1);
70f3c073
JS
14992 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14993 &mq_create_ext->u.request, 1);
14994 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14995 &mq_create_ext->u.request, 1);
b19a061a 14996 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14997 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14998 phba->sli4_hba.pc_sli4_params.mqv);
14999 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15000 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15001 cq->queue_id);
15002 else
15003 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15004 cq->queue_id);
04c68496
JS
15005 switch (mq->entry_count) {
15006 default:
15007 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15008 "0362 Unsupported MQ count. (%d)\n",
15009 mq->entry_count);
4f4c1863
JS
15010 if (mq->entry_count < 16) {
15011 status = -EINVAL;
15012 goto out;
15013 }
04c68496
JS
15014 /* otherwise default to smallest count (drop through) */
15015 case 16:
5a6f133e
JS
15016 bf_set(lpfc_mq_context_ring_size,
15017 &mq_create_ext->u.request.context,
15018 LPFC_MQ_RING_SIZE_16);
04c68496
JS
15019 break;
15020 case 32:
5a6f133e
JS
15021 bf_set(lpfc_mq_context_ring_size,
15022 &mq_create_ext->u.request.context,
15023 LPFC_MQ_RING_SIZE_32);
04c68496
JS
15024 break;
15025 case 64:
5a6f133e
JS
15026 bf_set(lpfc_mq_context_ring_size,
15027 &mq_create_ext->u.request.context,
15028 LPFC_MQ_RING_SIZE_64);
04c68496
JS
15029 break;
15030 case 128:
5a6f133e
JS
15031 bf_set(lpfc_mq_context_ring_size,
15032 &mq_create_ext->u.request.context,
15033 LPFC_MQ_RING_SIZE_128);
04c68496
JS
15034 break;
15035 }
15036 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 15037 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 15038 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 15039 putPaddrLow(dmabuf->phys);
b19a061a 15040 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
15041 putPaddrHigh(dmabuf->phys);
15042 }
15043 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
15044 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15045 &mq_create_ext->u.response);
15046 if (rc != MBX_SUCCESS) {
15047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15048 "2795 MQ_CREATE_EXT failed with "
15049 "status x%x. Failback to MQ_CREATE.\n",
15050 rc);
15051 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15052 mq_create = &mbox->u.mqe.un.mq_create;
15053 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15054 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15055 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15056 &mq_create->u.response);
15057 }
15058
04c68496 15059 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
15060 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15061 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15062 if (shdr_status || shdr_add_status || rc) {
15063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15064 "2502 MQ_CREATE mailbox failed with "
15065 "status x%x add_status x%x, mbx status x%x\n",
15066 shdr_status, shdr_add_status, rc);
15067 status = -ENXIO;
15068 goto out;
15069 }
04c68496
JS
15070 if (mq->queue_id == 0xFFFF) {
15071 status = -ENXIO;
15072 goto out;
15073 }
15074 mq->type = LPFC_MQ;
2a622bfb 15075 mq->assoc_qid = cq->queue_id;
04c68496
JS
15076 mq->subtype = subtype;
15077 mq->host_index = 0;
15078 mq->hba_index = 0;
64eb4dcb 15079 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
15080
15081 /* link the mq onto the parent cq child list */
15082 list_add_tail(&mq->list, &cq->child_list);
15083out:
8fa38513 15084 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
15085 return status;
15086}
15087
4f774513
JS
15088/**
15089 * lpfc_wq_create - Create a Work Queue on the HBA
15090 * @phba: HBA structure that indicates port to create a queue on.
15091 * @wq: The queue structure to use to create the work queue.
15092 * @cq: The completion queue to bind this work queue to.
15093 * @subtype: The subtype of the work queue indicating its functionality.
15094 *
15095 * This function creates a work queue, as detailed in @wq, on a port, described
15096 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15097 *
15098 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15099 * is used to get the entry count and entry size that are necessary to
15100 * determine the number of pages to allocate and use for this queue. The @cq
15101 * is used to indicate which completion queue to bind this work queue to. This
15102 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15103 * work queue. This function is asynchronous and will wait for the mailbox
15104 * command to finish before continuing.
15105 *
15106 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15107 * memory this function will return -ENOMEM. If the queue create mailbox command
15108 * fails this function will return -ENXIO.
4f774513 15109 **/
a2fc4aef 15110int
4f774513
JS
15111lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15112 struct lpfc_queue *cq, uint32_t subtype)
15113{
15114 struct lpfc_mbx_wq_create *wq_create;
15115 struct lpfc_dmabuf *dmabuf;
15116 LPFC_MBOXQ_t *mbox;
15117 int rc, length, status = 0;
15118 uint32_t shdr_status, shdr_add_status;
15119 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15120 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 15121 struct dma_address *page;
962bc51b
JS
15122 void __iomem *bar_memmap_p;
15123 uint32_t db_offset;
15124 uint16_t pci_barset;
1351e69f
JS
15125 uint8_t dpp_barset;
15126 uint32_t dpp_offset;
15127 unsigned long pg_addr;
81b96eda 15128 uint8_t wq_create_version;
49198b37 15129
2e90f4b5
JS
15130 /* sanity check on queue memory */
15131 if (!wq || !cq)
15132 return -ENODEV;
49198b37 15133 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 15134 hw_page_size = wq->page_size;
4f774513
JS
15135
15136 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15137 if (!mbox)
15138 return -ENOMEM;
15139 length = (sizeof(struct lpfc_mbx_wq_create) -
15140 sizeof(struct lpfc_sli4_cfg_mhdr));
15141 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15142 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15143 length, LPFC_SLI4_MBX_EMBED);
15144 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 15145 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
15146 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15147 wq->page_count);
15148 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15149 cq->queue_id);
0c651878
JS
15150
15151 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
15152 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15153 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 15154
c176ffa0
JS
15155 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15156 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
15157 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15158 else
15159 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15160
0c651878 15161
1351e69f
JS
15162 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15163 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15164 else
15165 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15166
15167 switch (wq_create_version) {
0c651878 15168 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
15169 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15170 wq->entry_count);
3f247de7
JS
15171 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15172 LPFC_Q_CREATE_VERSION_1);
15173
5a6f133e
JS
15174 switch (wq->entry_size) {
15175 default:
15176 case 64:
15177 bf_set(lpfc_mbx_wq_create_wqe_size,
15178 &wq_create->u.request_1,
15179 LPFC_WQ_WQE_SIZE_64);
15180 break;
15181 case 128:
15182 bf_set(lpfc_mbx_wq_create_wqe_size,
15183 &wq_create->u.request_1,
15184 LPFC_WQ_WQE_SIZE_128);
15185 break;
15186 }
1351e69f
JS
15187 /* Request DPP by default */
15188 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
8ea73db4
JS
15189 bf_set(lpfc_mbx_wq_create_page_size,
15190 &wq_create->u.request_1,
81b96eda 15191 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 15192 page = wq_create->u.request_1.page;
0c651878
JS
15193 break;
15194 default:
1351e69f
JS
15195 page = wq_create->u.request.page;
15196 break;
5a6f133e 15197 }
0c651878 15198
4f774513 15199 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 15200 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
15201 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15202 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 15203 }
962bc51b
JS
15204
15205 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15206 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15207
4f774513
JS
15208 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15209 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15210 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15211 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15212 if (shdr_status || shdr_add_status || rc) {
15213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15214 "2503 WQ_CREATE mailbox failed with "
15215 "status x%x add_status x%x, mbx status x%x\n",
15216 shdr_status, shdr_add_status, rc);
15217 status = -ENXIO;
15218 goto out;
15219 }
1351e69f
JS
15220
15221 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15222 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15223 &wq_create->u.response);
15224 else
15225 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15226 &wq_create->u.response_1);
15227
4f774513
JS
15228 if (wq->queue_id == 0xFFFF) {
15229 status = -ENXIO;
15230 goto out;
15231 }
1351e69f
JS
15232
15233 wq->db_format = LPFC_DB_LIST_FORMAT;
15234 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15235 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15236 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15237 &wq_create->u.response);
15238 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15239 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15241 "3265 WQ[%d] doorbell format "
15242 "not supported: x%x\n",
15243 wq->queue_id, wq->db_format);
15244 status = -EINVAL;
15245 goto out;
15246 }
15247 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15248 &wq_create->u.response);
15249 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15250 pci_barset);
15251 if (!bar_memmap_p) {
15252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15253 "3263 WQ[%d] failed to memmap "
15254 "pci barset:x%x\n",
15255 wq->queue_id, pci_barset);
15256 status = -ENOMEM;
15257 goto out;
15258 }
15259 db_offset = wq_create->u.response.doorbell_offset;
15260 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15261 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15263 "3252 WQ[%d] doorbell offset "
15264 "not supported: x%x\n",
15265 wq->queue_id, db_offset);
15266 status = -EINVAL;
15267 goto out;
15268 }
15269 wq->db_regaddr = bar_memmap_p + db_offset;
15270 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15271 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15272 "format:x%x\n", wq->queue_id,
15273 pci_barset, db_offset, wq->db_format);
15274 } else
15275 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15276 } else {
1351e69f
JS
15277 /* Check if DPP was honored by the firmware */
15278 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15279 &wq_create->u.response_1);
15280 if (wq->dpp_enable) {
15281 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15282 &wq_create->u.response_1);
15283 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15284 pci_barset);
15285 if (!bar_memmap_p) {
15286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15287 "3267 WQ[%d] failed to memmap "
15288 "pci barset:x%x\n",
15289 wq->queue_id, pci_barset);
15290 status = -ENOMEM;
15291 goto out;
15292 }
15293 db_offset = wq_create->u.response_1.doorbell_offset;
15294 wq->db_regaddr = bar_memmap_p + db_offset;
15295 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15296 &wq_create->u.response_1);
15297 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15298 &wq_create->u.response_1);
15299 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15300 dpp_barset);
15301 if (!bar_memmap_p) {
15302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15303 "3268 WQ[%d] failed to memmap "
15304 "pci barset:x%x\n",
15305 wq->queue_id, dpp_barset);
15306 status = -ENOMEM;
15307 goto out;
15308 }
15309 dpp_offset = wq_create->u.response_1.dpp_offset;
15310 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15311 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15312 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15313 "dpp_id:x%x dpp_barset:x%x "
15314 "dpp_offset:x%x\n",
15315 wq->queue_id, pci_barset, db_offset,
15316 wq->dpp_id, dpp_barset, dpp_offset);
15317
15318 /* Enable combined writes for DPP aperture */
15319 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15320#ifdef CONFIG_X86
15321 rc = set_memory_wc(pg_addr, 1);
15322 if (rc) {
15323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15324 "3272 Cannot setup Combined "
15325 "Write on WQ[%d] - disable DPP\n",
15326 wq->queue_id);
15327 phba->cfg_enable_dpp = 0;
15328 }
15329#else
15330 phba->cfg_enable_dpp = 0;
15331#endif
15332 } else
15333 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15334 }
895427bd
JS
15335 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15336 if (wq->pring == NULL) {
15337 status = -ENOMEM;
15338 goto out;
15339 }
4f774513 15340 wq->type = LPFC_WQ;
2a622bfb 15341 wq->assoc_qid = cq->queue_id;
4f774513
JS
15342 wq->subtype = subtype;
15343 wq->host_index = 0;
15344 wq->hba_index = 0;
ff78d8f9 15345 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15346
15347 /* link the wq onto the parent cq child list */
15348 list_add_tail(&wq->list, &cq->child_list);
15349out:
8fa38513 15350 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15351 return status;
15352}
15353
15354/**
15355 * lpfc_rq_create - Create a Receive Queue on the HBA
15356 * @phba: HBA structure that indicates port to create a queue on.
15357 * @hrq: The queue structure to use to create the header receive queue.
15358 * @drq: The queue structure to use to create the data receive queue.
15359 * @cq: The completion queue to bind this work queue to.
15360 *
15361 * This function creates a receive buffer queue pair , as detailed in @hrq and
15362 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15363 * to the HBA.
15364 *
15365 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15366 * struct is used to get the entry count that is necessary to determine the
15367 * number of pages to use for this queue. The @cq is used to indicate which
15368 * completion queue to bind received buffers that are posted to these queues to.
15369 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15370 * receive queue pair. This function is asynchronous and will wait for the
15371 * mailbox command to finish before continuing.
15372 *
15373 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15374 * memory this function will return -ENOMEM. If the queue create mailbox command
15375 * fails this function will return -ENXIO.
4f774513 15376 **/
a2fc4aef 15377int
4f774513
JS
15378lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15379 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15380{
15381 struct lpfc_mbx_rq_create *rq_create;
15382 struct lpfc_dmabuf *dmabuf;
15383 LPFC_MBOXQ_t *mbox;
15384 int rc, length, status = 0;
15385 uint32_t shdr_status, shdr_add_status;
15386 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15387 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15388 void __iomem *bar_memmap_p;
15389 uint32_t db_offset;
15390 uint16_t pci_barset;
49198b37 15391
2e90f4b5
JS
15392 /* sanity check on queue memory */
15393 if (!hrq || !drq || !cq)
15394 return -ENODEV;
49198b37
JS
15395 if (!phba->sli4_hba.pc_sli4_params.supported)
15396 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15397
15398 if (hrq->entry_count != drq->entry_count)
15399 return -EINVAL;
15400 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15401 if (!mbox)
15402 return -ENOMEM;
15403 length = (sizeof(struct lpfc_mbx_rq_create) -
15404 sizeof(struct lpfc_sli4_cfg_mhdr));
15405 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15406 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15407 length, LPFC_SLI4_MBX_EMBED);
15408 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15409 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15410 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15411 phba->sli4_hba.pc_sli4_params.rqv);
15412 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15413 bf_set(lpfc_rq_context_rqe_count_1,
15414 &rq_create->u.request.context,
15415 hrq->entry_count);
15416 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15417 bf_set(lpfc_rq_context_rqe_size,
15418 &rq_create->u.request.context,
15419 LPFC_RQE_SIZE_8);
15420 bf_set(lpfc_rq_context_page_size,
15421 &rq_create->u.request.context,
8ea73db4 15422 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15423 } else {
15424 switch (hrq->entry_count) {
15425 default:
15426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15427 "2535 Unsupported RQ count. (%d)\n",
15428 hrq->entry_count);
4f4c1863
JS
15429 if (hrq->entry_count < 512) {
15430 status = -EINVAL;
15431 goto out;
15432 }
5a6f133e
JS
15433 /* otherwise default to smallest count (drop through) */
15434 case 512:
15435 bf_set(lpfc_rq_context_rqe_count,
15436 &rq_create->u.request.context,
15437 LPFC_RQ_RING_SIZE_512);
15438 break;
15439 case 1024:
15440 bf_set(lpfc_rq_context_rqe_count,
15441 &rq_create->u.request.context,
15442 LPFC_RQ_RING_SIZE_1024);
15443 break;
15444 case 2048:
15445 bf_set(lpfc_rq_context_rqe_count,
15446 &rq_create->u.request.context,
15447 LPFC_RQ_RING_SIZE_2048);
15448 break;
15449 case 4096:
15450 bf_set(lpfc_rq_context_rqe_count,
15451 &rq_create->u.request.context,
15452 LPFC_RQ_RING_SIZE_4096);
15453 break;
15454 }
15455 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15456 LPFC_HDR_BUF_SIZE);
4f774513
JS
15457 }
15458 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15459 cq->queue_id);
15460 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15461 hrq->page_count);
4f774513 15462 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15463 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15464 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15465 putPaddrLow(dmabuf->phys);
15466 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15467 putPaddrHigh(dmabuf->phys);
15468 }
962bc51b
JS
15469 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15470 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15471
4f774513
JS
15472 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15473 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15474 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15475 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15476 if (shdr_status || shdr_add_status || rc) {
15477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15478 "2504 RQ_CREATE mailbox failed with "
15479 "status x%x add_status x%x, mbx status x%x\n",
15480 shdr_status, shdr_add_status, rc);
15481 status = -ENXIO;
15482 goto out;
15483 }
15484 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15485 if (hrq->queue_id == 0xFFFF) {
15486 status = -ENXIO;
15487 goto out;
15488 }
962bc51b
JS
15489
15490 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15491 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15492 &rq_create->u.response);
15493 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15494 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15496 "3262 RQ [%d] doorbell format not "
15497 "supported: x%x\n", hrq->queue_id,
15498 hrq->db_format);
15499 status = -EINVAL;
15500 goto out;
15501 }
15502
15503 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15504 &rq_create->u.response);
15505 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15506 if (!bar_memmap_p) {
15507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15508 "3269 RQ[%d] failed to memmap pci "
15509 "barset:x%x\n", hrq->queue_id,
15510 pci_barset);
15511 status = -ENOMEM;
15512 goto out;
15513 }
15514
15515 db_offset = rq_create->u.response.doorbell_offset;
15516 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15517 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15519 "3270 RQ[%d] doorbell offset not "
15520 "supported: x%x\n", hrq->queue_id,
15521 db_offset);
15522 status = -EINVAL;
15523 goto out;
15524 }
15525 hrq->db_regaddr = bar_memmap_p + db_offset;
15526 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15527 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15528 "format:x%x\n", hrq->queue_id, pci_barset,
15529 db_offset, hrq->db_format);
962bc51b
JS
15530 } else {
15531 hrq->db_format = LPFC_DB_RING_FORMAT;
15532 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15533 }
4f774513 15534 hrq->type = LPFC_HRQ;
2a622bfb 15535 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15536 hrq->subtype = subtype;
15537 hrq->host_index = 0;
15538 hrq->hba_index = 0;
61f3d4bf 15539 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15540
15541 /* now create the data queue */
15542 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15543 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15544 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15545 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15546 phba->sli4_hba.pc_sli4_params.rqv);
15547 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15548 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15549 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15550 if (subtype == LPFC_NVMET)
15551 rq_create->u.request.context.buffer_size =
15552 LPFC_NVMET_DATA_BUF_SIZE;
15553 else
15554 rq_create->u.request.context.buffer_size =
15555 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15556 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15557 LPFC_RQE_SIZE_8);
15558 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15559 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15560 } else {
15561 switch (drq->entry_count) {
15562 default:
15563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15564 "2536 Unsupported RQ count. (%d)\n",
15565 drq->entry_count);
4f4c1863
JS
15566 if (drq->entry_count < 512) {
15567 status = -EINVAL;
15568 goto out;
15569 }
5a6f133e
JS
15570 /* otherwise default to smallest count (drop through) */
15571 case 512:
15572 bf_set(lpfc_rq_context_rqe_count,
15573 &rq_create->u.request.context,
15574 LPFC_RQ_RING_SIZE_512);
15575 break;
15576 case 1024:
15577 bf_set(lpfc_rq_context_rqe_count,
15578 &rq_create->u.request.context,
15579 LPFC_RQ_RING_SIZE_1024);
15580 break;
15581 case 2048:
15582 bf_set(lpfc_rq_context_rqe_count,
15583 &rq_create->u.request.context,
15584 LPFC_RQ_RING_SIZE_2048);
15585 break;
15586 case 4096:
15587 bf_set(lpfc_rq_context_rqe_count,
15588 &rq_create->u.request.context,
15589 LPFC_RQ_RING_SIZE_4096);
15590 break;
15591 }
3c603be9
JS
15592 if (subtype == LPFC_NVMET)
15593 bf_set(lpfc_rq_context_buf_size,
15594 &rq_create->u.request.context,
15595 LPFC_NVMET_DATA_BUF_SIZE);
15596 else
15597 bf_set(lpfc_rq_context_buf_size,
15598 &rq_create->u.request.context,
15599 LPFC_DATA_BUF_SIZE);
4f774513
JS
15600 }
15601 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15602 cq->queue_id);
15603 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15604 drq->page_count);
4f774513
JS
15605 list_for_each_entry(dmabuf, &drq->page_list, list) {
15606 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15607 putPaddrLow(dmabuf->phys);
15608 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15609 putPaddrHigh(dmabuf->phys);
15610 }
962bc51b
JS
15611 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15612 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15613 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15614 /* The IOCTL status is embedded in the mailbox subheader. */
15615 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15616 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15617 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15618 if (shdr_status || shdr_add_status || rc) {
15619 status = -ENXIO;
15620 goto out;
15621 }
15622 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15623 if (drq->queue_id == 0xFFFF) {
15624 status = -ENXIO;
15625 goto out;
15626 }
15627 drq->type = LPFC_DRQ;
2a622bfb 15628 drq->assoc_qid = cq->queue_id;
4f774513
JS
15629 drq->subtype = subtype;
15630 drq->host_index = 0;
15631 drq->hba_index = 0;
61f3d4bf 15632 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15633
15634 /* link the header and data RQs onto the parent cq child list */
15635 list_add_tail(&hrq->list, &cq->child_list);
15636 list_add_tail(&drq->list, &cq->child_list);
15637
15638out:
8fa38513 15639 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15640 return status;
15641}
15642
2d7dbc4c
JS
15643/**
15644 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15645 * @phba: HBA structure that indicates port to create a queue on.
15646 * @hrqp: The queue structure array to use to create the header receive queues.
15647 * @drqp: The queue structure array to use to create the data receive queues.
15648 * @cqp: The completion queue array to bind these receive queues to.
15649 *
15650 * This function creates a receive buffer queue pair , as detailed in @hrq and
15651 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15652 * to the HBA.
15653 *
15654 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15655 * struct is used to get the entry count that is necessary to determine the
15656 * number of pages to use for this queue. The @cq is used to indicate which
15657 * completion queue to bind received buffers that are posted to these queues to.
15658 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15659 * receive queue pair. This function is asynchronous and will wait for the
15660 * mailbox command to finish before continuing.
15661 *
15662 * On success this function will return a zero. If unable to allocate enough
15663 * memory this function will return -ENOMEM. If the queue create mailbox command
15664 * fails this function will return -ENXIO.
15665 **/
15666int
15667lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15668 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15669 uint32_t subtype)
15670{
15671 struct lpfc_queue *hrq, *drq, *cq;
15672 struct lpfc_mbx_rq_create_v2 *rq_create;
15673 struct lpfc_dmabuf *dmabuf;
15674 LPFC_MBOXQ_t *mbox;
15675 int rc, length, alloclen, status = 0;
15676 int cnt, idx, numrq, page_idx = 0;
15677 uint32_t shdr_status, shdr_add_status;
15678 union lpfc_sli4_cfg_shdr *shdr;
15679 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15680
15681 numrq = phba->cfg_nvmet_mrq;
15682 /* sanity check on array memory */
15683 if (!hrqp || !drqp || !cqp || !numrq)
15684 return -ENODEV;
15685 if (!phba->sli4_hba.pc_sli4_params.supported)
15686 hw_page_size = SLI4_PAGE_SIZE;
15687
15688 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15689 if (!mbox)
15690 return -ENOMEM;
15691
15692 length = sizeof(struct lpfc_mbx_rq_create_v2);
15693 length += ((2 * numrq * hrqp[0]->page_count) *
15694 sizeof(struct dma_address));
15695
15696 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15697 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15698 LPFC_SLI4_MBX_NEMBED);
15699 if (alloclen < length) {
15700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15701 "3099 Allocated DMA memory size (%d) is "
15702 "less than the requested DMA memory size "
15703 "(%d)\n", alloclen, length);
15704 status = -ENOMEM;
15705 goto out;
15706 }
15707
15708
15709
15710 rq_create = mbox->sge_array->addr[0];
15711 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15712
15713 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15714 cnt = 0;
15715
15716 for (idx = 0; idx < numrq; idx++) {
15717 hrq = hrqp[idx];
15718 drq = drqp[idx];
15719 cq = cqp[idx];
15720
2d7dbc4c
JS
15721 /* sanity check on queue memory */
15722 if (!hrq || !drq || !cq) {
15723 status = -ENODEV;
15724 goto out;
15725 }
15726
7aabe84b
JS
15727 if (hrq->entry_count != drq->entry_count) {
15728 status = -EINVAL;
15729 goto out;
15730 }
15731
2d7dbc4c
JS
15732 if (idx == 0) {
15733 bf_set(lpfc_mbx_rq_create_num_pages,
15734 &rq_create->u.request,
15735 hrq->page_count);
15736 bf_set(lpfc_mbx_rq_create_rq_cnt,
15737 &rq_create->u.request, (numrq * 2));
15738 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15739 1);
15740 bf_set(lpfc_rq_context_base_cq,
15741 &rq_create->u.request.context,
15742 cq->queue_id);
15743 bf_set(lpfc_rq_context_data_size,
15744 &rq_create->u.request.context,
3c603be9 15745 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15746 bf_set(lpfc_rq_context_hdr_size,
15747 &rq_create->u.request.context,
15748 LPFC_HDR_BUF_SIZE);
15749 bf_set(lpfc_rq_context_rqe_count_1,
15750 &rq_create->u.request.context,
15751 hrq->entry_count);
15752 bf_set(lpfc_rq_context_rqe_size,
15753 &rq_create->u.request.context,
15754 LPFC_RQE_SIZE_8);
15755 bf_set(lpfc_rq_context_page_size,
15756 &rq_create->u.request.context,
15757 (PAGE_SIZE/SLI4_PAGE_SIZE));
15758 }
15759 rc = 0;
15760 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15761 memset(dmabuf->virt, 0, hw_page_size);
15762 cnt = page_idx + dmabuf->buffer_tag;
15763 rq_create->u.request.page[cnt].addr_lo =
15764 putPaddrLow(dmabuf->phys);
15765 rq_create->u.request.page[cnt].addr_hi =
15766 putPaddrHigh(dmabuf->phys);
15767 rc++;
15768 }
15769 page_idx += rc;
15770
15771 rc = 0;
15772 list_for_each_entry(dmabuf, &drq->page_list, list) {
15773 memset(dmabuf->virt, 0, hw_page_size);
15774 cnt = page_idx + dmabuf->buffer_tag;
15775 rq_create->u.request.page[cnt].addr_lo =
15776 putPaddrLow(dmabuf->phys);
15777 rq_create->u.request.page[cnt].addr_hi =
15778 putPaddrHigh(dmabuf->phys);
15779 rc++;
15780 }
15781 page_idx += rc;
15782
15783 hrq->db_format = LPFC_DB_RING_FORMAT;
15784 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15785 hrq->type = LPFC_HRQ;
15786 hrq->assoc_qid = cq->queue_id;
15787 hrq->subtype = subtype;
15788 hrq->host_index = 0;
15789 hrq->hba_index = 0;
61f3d4bf 15790 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15791
15792 drq->db_format = LPFC_DB_RING_FORMAT;
15793 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15794 drq->type = LPFC_DRQ;
15795 drq->assoc_qid = cq->queue_id;
15796 drq->subtype = subtype;
15797 drq->host_index = 0;
15798 drq->hba_index = 0;
61f3d4bf 15799 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15800
15801 list_add_tail(&hrq->list, &cq->child_list);
15802 list_add_tail(&drq->list, &cq->child_list);
15803 }
15804
15805 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15806 /* The IOCTL status is embedded in the mailbox subheader. */
15807 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15808 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15809 if (shdr_status || shdr_add_status || rc) {
15810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15811 "3120 RQ_CREATE mailbox failed with "
15812 "status x%x add_status x%x, mbx status x%x\n",
15813 shdr_status, shdr_add_status, rc);
15814 status = -ENXIO;
15815 goto out;
15816 }
15817 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15818 if (rc == 0xFFFF) {
15819 status = -ENXIO;
15820 goto out;
15821 }
15822
15823 /* Initialize all RQs with associated queue id */
15824 for (idx = 0; idx < numrq; idx++) {
15825 hrq = hrqp[idx];
15826 hrq->queue_id = rc + (2 * idx);
15827 drq = drqp[idx];
15828 drq->queue_id = rc + (2 * idx) + 1;
15829 }
15830
15831out:
15832 lpfc_sli4_mbox_cmd_free(phba, mbox);
15833 return status;
15834}
15835
4f774513
JS
15836/**
15837 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15838 * @eq: The queue structure associated with the queue to destroy.
15839 *
15840 * This function destroys a queue, as detailed in @eq by sending an mailbox
15841 * command, specific to the type of queue, to the HBA.
15842 *
15843 * The @eq struct is used to get the queue ID of the queue to destroy.
15844 *
15845 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15846 * command fails this function will return -ENXIO.
4f774513 15847 **/
a2fc4aef 15848int
4f774513
JS
15849lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15850{
15851 LPFC_MBOXQ_t *mbox;
15852 int rc, length, status = 0;
15853 uint32_t shdr_status, shdr_add_status;
15854 union lpfc_sli4_cfg_shdr *shdr;
15855
2e90f4b5 15856 /* sanity check on queue memory */
4f774513
JS
15857 if (!eq)
15858 return -ENODEV;
15859 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15860 if (!mbox)
15861 return -ENOMEM;
15862 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15863 sizeof(struct lpfc_sli4_cfg_mhdr));
15864 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15865 LPFC_MBOX_OPCODE_EQ_DESTROY,
15866 length, LPFC_SLI4_MBX_EMBED);
15867 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15868 eq->queue_id);
15869 mbox->vport = eq->phba->pport;
15870 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15871
15872 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15873 /* The IOCTL status is embedded in the mailbox subheader. */
15874 shdr = (union lpfc_sli4_cfg_shdr *)
15875 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15876 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15877 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15878 if (shdr_status || shdr_add_status || rc) {
15879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15880 "2505 EQ_DESTROY mailbox failed with "
15881 "status x%x add_status x%x, mbx status x%x\n",
15882 shdr_status, shdr_add_status, rc);
15883 status = -ENXIO;
15884 }
15885
15886 /* Remove eq from any list */
15887 list_del_init(&eq->list);
8fa38513 15888 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15889 return status;
15890}
15891
15892/**
15893 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15894 * @cq: The queue structure associated with the queue to destroy.
15895 *
15896 * This function destroys a queue, as detailed in @cq by sending an mailbox
15897 * command, specific to the type of queue, to the HBA.
15898 *
15899 * The @cq struct is used to get the queue ID of the queue to destroy.
15900 *
15901 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15902 * command fails this function will return -ENXIO.
4f774513 15903 **/
a2fc4aef 15904int
4f774513
JS
15905lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15906{
15907 LPFC_MBOXQ_t *mbox;
15908 int rc, length, status = 0;
15909 uint32_t shdr_status, shdr_add_status;
15910 union lpfc_sli4_cfg_shdr *shdr;
15911
2e90f4b5 15912 /* sanity check on queue memory */
4f774513
JS
15913 if (!cq)
15914 return -ENODEV;
15915 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15916 if (!mbox)
15917 return -ENOMEM;
15918 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15919 sizeof(struct lpfc_sli4_cfg_mhdr));
15920 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15921 LPFC_MBOX_OPCODE_CQ_DESTROY,
15922 length, LPFC_SLI4_MBX_EMBED);
15923 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15924 cq->queue_id);
15925 mbox->vport = cq->phba->pport;
15926 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15927 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15928 /* The IOCTL status is embedded in the mailbox subheader. */
15929 shdr = (union lpfc_sli4_cfg_shdr *)
15930 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15931 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15932 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15933 if (shdr_status || shdr_add_status || rc) {
15934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15935 "2506 CQ_DESTROY mailbox failed with "
15936 "status x%x add_status x%x, mbx status x%x\n",
15937 shdr_status, shdr_add_status, rc);
15938 status = -ENXIO;
15939 }
15940 /* Remove cq from any list */
15941 list_del_init(&cq->list);
8fa38513 15942 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15943 return status;
15944}
15945
04c68496
JS
15946/**
15947 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15948 * @qm: The queue structure associated with the queue to destroy.
15949 *
15950 * This function destroys a queue, as detailed in @mq by sending an mailbox
15951 * command, specific to the type of queue, to the HBA.
15952 *
15953 * The @mq struct is used to get the queue ID of the queue to destroy.
15954 *
15955 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15956 * command fails this function will return -ENXIO.
04c68496 15957 **/
a2fc4aef 15958int
04c68496
JS
15959lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15960{
15961 LPFC_MBOXQ_t *mbox;
15962 int rc, length, status = 0;
15963 uint32_t shdr_status, shdr_add_status;
15964 union lpfc_sli4_cfg_shdr *shdr;
15965
2e90f4b5 15966 /* sanity check on queue memory */
04c68496
JS
15967 if (!mq)
15968 return -ENODEV;
15969 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15970 if (!mbox)
15971 return -ENOMEM;
15972 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15973 sizeof(struct lpfc_sli4_cfg_mhdr));
15974 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15975 LPFC_MBOX_OPCODE_MQ_DESTROY,
15976 length, LPFC_SLI4_MBX_EMBED);
15977 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15978 mq->queue_id);
15979 mbox->vport = mq->phba->pport;
15980 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15981 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15982 /* The IOCTL status is embedded in the mailbox subheader. */
15983 shdr = (union lpfc_sli4_cfg_shdr *)
15984 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15985 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15986 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15987 if (shdr_status || shdr_add_status || rc) {
15988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15989 "2507 MQ_DESTROY mailbox failed with "
15990 "status x%x add_status x%x, mbx status x%x\n",
15991 shdr_status, shdr_add_status, rc);
15992 status = -ENXIO;
15993 }
15994 /* Remove mq from any list */
15995 list_del_init(&mq->list);
8fa38513 15996 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15997 return status;
15998}
15999
4f774513
JS
16000/**
16001 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16002 * @wq: The queue structure associated with the queue to destroy.
16003 *
16004 * This function destroys a queue, as detailed in @wq by sending an mailbox
16005 * command, specific to the type of queue, to the HBA.
16006 *
16007 * The @wq struct is used to get the queue ID of the queue to destroy.
16008 *
16009 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16010 * command fails this function will return -ENXIO.
4f774513 16011 **/
a2fc4aef 16012int
4f774513
JS
16013lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16014{
16015 LPFC_MBOXQ_t *mbox;
16016 int rc, length, status = 0;
16017 uint32_t shdr_status, shdr_add_status;
16018 union lpfc_sli4_cfg_shdr *shdr;
16019
2e90f4b5 16020 /* sanity check on queue memory */
4f774513
JS
16021 if (!wq)
16022 return -ENODEV;
16023 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16024 if (!mbox)
16025 return -ENOMEM;
16026 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16027 sizeof(struct lpfc_sli4_cfg_mhdr));
16028 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16029 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16030 length, LPFC_SLI4_MBX_EMBED);
16031 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16032 wq->queue_id);
16033 mbox->vport = wq->phba->pport;
16034 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16035 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16036 shdr = (union lpfc_sli4_cfg_shdr *)
16037 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16038 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16039 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16040 if (shdr_status || shdr_add_status || rc) {
16041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16042 "2508 WQ_DESTROY mailbox failed with "
16043 "status x%x add_status x%x, mbx status x%x\n",
16044 shdr_status, shdr_add_status, rc);
16045 status = -ENXIO;
16046 }
16047 /* Remove wq from any list */
16048 list_del_init(&wq->list);
d1f525aa
JS
16049 kfree(wq->pring);
16050 wq->pring = NULL;
8fa38513 16051 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
16052 return status;
16053}
16054
16055/**
16056 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16057 * @rq: The queue structure associated with the queue to destroy.
16058 *
16059 * This function destroys a queue, as detailed in @rq by sending an mailbox
16060 * command, specific to the type of queue, to the HBA.
16061 *
16062 * The @rq struct is used to get the queue ID of the queue to destroy.
16063 *
16064 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16065 * command fails this function will return -ENXIO.
4f774513 16066 **/
a2fc4aef 16067int
4f774513
JS
16068lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16069 struct lpfc_queue *drq)
16070{
16071 LPFC_MBOXQ_t *mbox;
16072 int rc, length, status = 0;
16073 uint32_t shdr_status, shdr_add_status;
16074 union lpfc_sli4_cfg_shdr *shdr;
16075
2e90f4b5 16076 /* sanity check on queue memory */
4f774513
JS
16077 if (!hrq || !drq)
16078 return -ENODEV;
16079 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16080 if (!mbox)
16081 return -ENOMEM;
16082 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 16083 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
16084 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16085 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16086 length, LPFC_SLI4_MBX_EMBED);
16087 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16088 hrq->queue_id);
16089 mbox->vport = hrq->phba->pport;
16090 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16091 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16092 /* The IOCTL status is embedded in the mailbox subheader. */
16093 shdr = (union lpfc_sli4_cfg_shdr *)
16094 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16095 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16096 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16097 if (shdr_status || shdr_add_status || rc) {
16098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16099 "2509 RQ_DESTROY mailbox failed with "
16100 "status x%x add_status x%x, mbx status x%x\n",
16101 shdr_status, shdr_add_status, rc);
16102 if (rc != MBX_TIMEOUT)
16103 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16104 return -ENXIO;
16105 }
16106 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16107 drq->queue_id);
16108 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16109 shdr = (union lpfc_sli4_cfg_shdr *)
16110 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16111 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16112 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16113 if (shdr_status || shdr_add_status || rc) {
16114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16115 "2510 RQ_DESTROY mailbox failed with "
16116 "status x%x add_status x%x, mbx status x%x\n",
16117 shdr_status, shdr_add_status, rc);
16118 status = -ENXIO;
16119 }
16120 list_del_init(&hrq->list);
16121 list_del_init(&drq->list);
8fa38513 16122 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
16123 return status;
16124}
16125
16126/**
16127 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16128 * @phba: The virtual port for which this call being executed.
16129 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16130 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16131 * @xritag: the xritag that ties this io to the SGL pages.
16132 *
16133 * This routine will post the sgl pages for the IO that has the xritag
16134 * that is in the iocbq structure. The xritag is assigned during iocbq
16135 * creation and persists for as long as the driver is loaded.
16136 * if the caller has fewer than 256 scatter gather segments to map then
16137 * pdma_phys_addr1 should be 0.
16138 * If the caller needs to map more than 256 scatter gather segment then
16139 * pdma_phys_addr1 should be a valid physical address.
16140 * physical address for SGLs must be 64 byte aligned.
16141 * If you are going to map 2 SGL's then the first one must have 256 entries
16142 * the second sgl can have between 1 and 256 entries.
16143 *
16144 * Return codes:
16145 * 0 - Success
16146 * -ENXIO, -ENOMEM - Failure
16147 **/
16148int
16149lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16150 dma_addr_t pdma_phys_addr0,
16151 dma_addr_t pdma_phys_addr1,
16152 uint16_t xritag)
16153{
16154 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16155 LPFC_MBOXQ_t *mbox;
16156 int rc;
16157 uint32_t shdr_status, shdr_add_status;
6d368e53 16158 uint32_t mbox_tmo;
4f774513
JS
16159 union lpfc_sli4_cfg_shdr *shdr;
16160
16161 if (xritag == NO_XRI) {
16162 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16163 "0364 Invalid param:\n");
16164 return -EINVAL;
16165 }
16166
16167 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16168 if (!mbox)
16169 return -ENOMEM;
16170
16171 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16172 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16173 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 16174 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
16175
16176 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16177 &mbox->u.mqe.un.post_sgl_pages;
16178 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16179 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16180
16181 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16182 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16183 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16184 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16185
16186 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16187 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16188 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16189 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16190 if (!phba->sli4_hba.intr_enable)
16191 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 16192 else {
a183a15f 16193 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
16194 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16195 }
4f774513
JS
16196 /* The IOCTL status is embedded in the mailbox subheader. */
16197 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16198 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16199 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16200 if (rc != MBX_TIMEOUT)
16201 mempool_free(mbox, phba->mbox_mem_pool);
16202 if (shdr_status || shdr_add_status || rc) {
16203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16204 "2511 POST_SGL mailbox failed with "
16205 "status x%x add_status x%x, mbx status x%x\n",
16206 shdr_status, shdr_add_status, rc);
4f774513
JS
16207 }
16208 return 0;
16209}
4f774513 16210
6d368e53 16211/**
88a2cfbb 16212 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
16213 * @phba: pointer to lpfc hba data structure.
16214 *
16215 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
16216 * HBA consistent with the SLI-4 interface spec. This routine
16217 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16218 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 16219 *
88a2cfbb
JS
16220 * Returns
16221 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16222 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16223 **/
5d8b8167 16224static uint16_t
6d368e53
JS
16225lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16226{
16227 unsigned long xri;
16228
16229 /*
16230 * Fetch the next logical xri. Because this index is logical,
16231 * the driver starts at 0 each time.
16232 */
16233 spin_lock_irq(&phba->hbalock);
16234 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16235 phba->sli4_hba.max_cfg_param.max_xri, 0);
16236 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16237 spin_unlock_irq(&phba->hbalock);
16238 return NO_XRI;
16239 } else {
16240 set_bit(xri, phba->sli4_hba.xri_bmask);
16241 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 16242 }
6d368e53
JS
16243 spin_unlock_irq(&phba->hbalock);
16244 return xri;
16245}
16246
16247/**
16248 * lpfc_sli4_free_xri - Release an xri for reuse.
16249 * @phba: pointer to lpfc hba data structure.
16250 *
16251 * This routine is invoked to release an xri to the pool of
16252 * available rpis maintained by the driver.
16253 **/
5d8b8167 16254static void
6d368e53
JS
16255__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16256{
16257 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
16258 phba->sli4_hba.max_cfg_param.xri_used--;
16259 }
16260}
16261
16262/**
16263 * lpfc_sli4_free_xri - Release an xri for reuse.
16264 * @phba: pointer to lpfc hba data structure.
16265 *
16266 * This routine is invoked to release an xri to the pool of
16267 * available rpis maintained by the driver.
16268 **/
16269void
16270lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16271{
16272 spin_lock_irq(&phba->hbalock);
16273 __lpfc_sli4_free_xri(phba, xri);
16274 spin_unlock_irq(&phba->hbalock);
16275}
16276
4f774513
JS
16277/**
16278 * lpfc_sli4_next_xritag - Get an xritag for the io
16279 * @phba: Pointer to HBA context object.
16280 *
16281 * This function gets an xritag for the iocb. If there is no unused xritag
16282 * it will return 0xffff.
16283 * The function returns the allocated xritag if successful, else returns zero.
16284 * Zero is not a valid xritag.
16285 * The caller is not required to hold any lock.
16286 **/
16287uint16_t
16288lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16289{
6d368e53 16290 uint16_t xri_index;
4f774513 16291
6d368e53 16292 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16293 if (xri_index == NO_XRI)
16294 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16295 "2004 Failed to allocate XRI.last XRITAG is %d"
16296 " Max XRI is %d, Used XRI is %d\n",
16297 xri_index,
16298 phba->sli4_hba.max_cfg_param.max_xri,
16299 phba->sli4_hba.max_cfg_param.xri_used);
16300 return xri_index;
4f774513
JS
16301}
16302
16303/**
895427bd 16304 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16305 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16306 * @post_sgl_list: pointer to els sgl entry list.
16307 * @count: number of els sgl entries on the list.
4f774513
JS
16308 *
16309 * This routine is invoked to post a block of driver's sgl pages to the
16310 * HBA using non-embedded mailbox command. No Lock is held. This routine
16311 * is only called when the driver is loading and after all IO has been
16312 * stopped.
16313 **/
8a9d2e80 16314static int
895427bd 16315lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16316 struct list_head *post_sgl_list,
16317 int post_cnt)
4f774513 16318{
8a9d2e80 16319 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16320 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16321 struct sgl_page_pairs *sgl_pg_pairs;
16322 void *viraddr;
16323 LPFC_MBOXQ_t *mbox;
16324 uint32_t reqlen, alloclen, pg_pairs;
16325 uint32_t mbox_tmo;
8a9d2e80
JS
16326 uint16_t xritag_start = 0;
16327 int rc = 0;
4f774513
JS
16328 uint32_t shdr_status, shdr_add_status;
16329 union lpfc_sli4_cfg_shdr *shdr;
16330
895427bd 16331 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16332 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16333 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16335 "2559 Block sgl registration required DMA "
16336 "size (%d) great than a page\n", reqlen);
16337 return -ENOMEM;
16338 }
895427bd 16339
4f774513 16340 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16341 if (!mbox)
4f774513 16342 return -ENOMEM;
4f774513
JS
16343
16344 /* Allocate DMA memory and set up the non-embedded mailbox command */
16345 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16346 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16347 LPFC_SLI4_MBX_NEMBED);
16348
16349 if (alloclen < reqlen) {
16350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16351 "0285 Allocated DMA memory size (%d) is "
16352 "less than the requested DMA memory "
16353 "size (%d)\n", alloclen, reqlen);
16354 lpfc_sli4_mbox_cmd_free(phba, mbox);
16355 return -ENOMEM;
16356 }
4f774513 16357 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16358 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16359 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16360 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16361
8a9d2e80
JS
16362 pg_pairs = 0;
16363 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16364 /* Set up the sge entry */
16365 sgl_pg_pairs->sgl_pg0_addr_lo =
16366 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16367 sgl_pg_pairs->sgl_pg0_addr_hi =
16368 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16369 sgl_pg_pairs->sgl_pg1_addr_lo =
16370 cpu_to_le32(putPaddrLow(0));
16371 sgl_pg_pairs->sgl_pg1_addr_hi =
16372 cpu_to_le32(putPaddrHigh(0));
6d368e53 16373
4f774513
JS
16374 /* Keep the first xritag on the list */
16375 if (pg_pairs == 0)
16376 xritag_start = sglq_entry->sli4_xritag;
16377 sgl_pg_pairs++;
8a9d2e80 16378 pg_pairs++;
4f774513 16379 }
6d368e53
JS
16380
16381 /* Complete initialization and perform endian conversion. */
4f774513 16382 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16383 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16384 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16385
4f774513
JS
16386 if (!phba->sli4_hba.intr_enable)
16387 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16388 else {
a183a15f 16389 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16390 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16391 }
16392 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16395 if (rc != MBX_TIMEOUT)
16396 lpfc_sli4_mbox_cmd_free(phba, mbox);
16397 if (shdr_status || shdr_add_status || rc) {
16398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16399 "2513 POST_SGL_BLOCK mailbox command failed "
16400 "status x%x add_status x%x mbx status x%x\n",
16401 shdr_status, shdr_add_status, rc);
16402 rc = -ENXIO;
16403 }
16404 return rc;
16405}
16406
16407/**
16408 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16409 * @phba: pointer to lpfc hba data structure.
16410 * @sblist: pointer to scsi buffer list.
16411 * @count: number of scsi buffers on the list.
16412 *
16413 * This routine is invoked to post a block of @count scsi sgl pages from a
16414 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16415 * No Lock is held.
16416 *
16417 **/
16418int
8a9d2e80
JS
16419lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16420 struct list_head *sblist,
16421 int count)
4f774513
JS
16422{
16423 struct lpfc_scsi_buf *psb;
16424 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16425 struct sgl_page_pairs *sgl_pg_pairs;
16426 void *viraddr;
16427 LPFC_MBOXQ_t *mbox;
16428 uint32_t reqlen, alloclen, pg_pairs;
16429 uint32_t mbox_tmo;
16430 uint16_t xritag_start = 0;
16431 int rc = 0;
16432 uint32_t shdr_status, shdr_add_status;
16433 dma_addr_t pdma_phys_bpl1;
16434 union lpfc_sli4_cfg_shdr *shdr;
16435
16436 /* Calculate the requested length of the dma memory */
8a9d2e80 16437 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16438 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16439 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16440 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16441 "0217 Block sgl registration required DMA "
16442 "size (%d) great than a page\n", reqlen);
16443 return -ENOMEM;
16444 }
16445 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16446 if (!mbox) {
16447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16448 "0283 Failed to allocate mbox cmd memory\n");
16449 return -ENOMEM;
16450 }
16451
16452 /* Allocate DMA memory and set up the non-embedded mailbox command */
16453 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16454 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16455 LPFC_SLI4_MBX_NEMBED);
16456
16457 if (alloclen < reqlen) {
16458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16459 "2561 Allocated DMA memory size (%d) is "
16460 "less than the requested DMA memory "
16461 "size (%d)\n", alloclen, reqlen);
16462 lpfc_sli4_mbox_cmd_free(phba, mbox);
16463 return -ENOMEM;
16464 }
6d368e53 16465
4f774513 16466 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16467 viraddr = mbox->sge_array->addr[0];
16468
16469 /* Set up the SGL pages in the non-embedded DMA pages */
16470 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16471 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16472
16473 pg_pairs = 0;
16474 list_for_each_entry(psb, sblist, list) {
16475 /* Set up the sge entry */
16476 sgl_pg_pairs->sgl_pg0_addr_lo =
16477 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16478 sgl_pg_pairs->sgl_pg0_addr_hi =
16479 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16480 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16481 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16482 else
16483 pdma_phys_bpl1 = 0;
16484 sgl_pg_pairs->sgl_pg1_addr_lo =
16485 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16486 sgl_pg_pairs->sgl_pg1_addr_hi =
16487 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16488 /* Keep the first xritag on the list */
16489 if (pg_pairs == 0)
16490 xritag_start = psb->cur_iocbq.sli4_xritag;
16491 sgl_pg_pairs++;
16492 pg_pairs++;
16493 }
16494 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16495 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16496 /* Perform endian conversion if necessary */
16497 sgl->word0 = cpu_to_le32(sgl->word0);
16498
16499 if (!phba->sli4_hba.intr_enable)
16500 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16501 else {
a183a15f 16502 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16503 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16504 }
16505 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16506 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16507 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16508 if (rc != MBX_TIMEOUT)
16509 lpfc_sli4_mbox_cmd_free(phba, mbox);
16510 if (shdr_status || shdr_add_status || rc) {
16511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16512 "2564 POST_SGL_BLOCK mailbox command failed "
16513 "status x%x add_status x%x mbx status x%x\n",
16514 shdr_status, shdr_add_status, rc);
16515 rc = -ENXIO;
16516 }
16517 return rc;
16518}
16519
16520/**
16521 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16522 * @phba: pointer to lpfc_hba struct that the frame was received on
16523 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16524 *
16525 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16526 * valid type of frame that the LPFC driver will handle. This function will
16527 * return a zero if the frame is a valid frame or a non zero value when the
16528 * frame does not pass the check.
16529 **/
16530static int
16531lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16532{
474ffb74 16533 /* make rctl_names static to save stack space */
4f774513 16534 struct fc_vft_header *fc_vft_hdr;
546fc854 16535 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16536
ae9e28f3
JS
16537#define FC_RCTL_MDS_DIAGS 0xF4
16538
4f774513
JS
16539 switch (fc_hdr->fh_r_ctl) {
16540 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16541 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16542 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16543 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16544 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16545 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16546 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16547 case FC_RCTL_DD_CMD_STATUS: /* command status */
16548 case FC_RCTL_ELS_REQ: /* extended link services request */
16549 case FC_RCTL_ELS_REP: /* extended link services reply */
16550 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16551 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16552 case FC_RCTL_BA_NOP: /* basic link service NOP */
16553 case FC_RCTL_BA_ABTS: /* basic link service abort */
16554 case FC_RCTL_BA_RMC: /* remove connection */
16555 case FC_RCTL_BA_ACC: /* basic accept */
16556 case FC_RCTL_BA_RJT: /* basic reject */
16557 case FC_RCTL_BA_PRMT:
16558 case FC_RCTL_ACK_1: /* acknowledge_1 */
16559 case FC_RCTL_ACK_0: /* acknowledge_0 */
16560 case FC_RCTL_P_RJT: /* port reject */
16561 case FC_RCTL_F_RJT: /* fabric reject */
16562 case FC_RCTL_P_BSY: /* port busy */
16563 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16564 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16565 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16566 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16567 case FC_RCTL_END: /* end */
16568 break;
16569 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16570 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16571 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16572 return lpfc_fc_frame_check(phba, fc_hdr);
16573 default:
16574 goto drop;
16575 }
ae9e28f3
JS
16576
16577#define FC_TYPE_VENDOR_UNIQUE 0xFF
16578
4f774513
JS
16579 switch (fc_hdr->fh_type) {
16580 case FC_TYPE_BLS:
16581 case FC_TYPE_ELS:
16582 case FC_TYPE_FCP:
16583 case FC_TYPE_CT:
895427bd 16584 case FC_TYPE_NVME:
ae9e28f3 16585 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16586 break;
16587 case FC_TYPE_IP:
16588 case FC_TYPE_ILS:
16589 default:
16590 goto drop;
16591 }
546fc854 16592
4f774513 16593 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16594 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16595 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16596 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16597 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16598 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16599 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16600 be32_to_cpu(header[6]));
4f774513
JS
16601 return 0;
16602drop:
16603 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16604 "2539 Dropped frame rctl:x%x type:x%x\n",
16605 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16606 return 1;
16607}
16608
16609/**
16610 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16611 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16612 *
16613 * This function processes the FC header to retrieve the VFI from the VF
16614 * header, if one exists. This function will return the VFI if one exists
16615 * or 0 if no VSAN Header exists.
16616 **/
16617static uint32_t
16618lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16619{
16620 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16621
16622 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16623 return 0;
16624 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16625}
16626
16627/**
16628 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16629 * @phba: Pointer to the HBA structure to search for the vport on
16630 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16631 * @fcfi: The FC Fabric ID that the frame came from
16632 *
16633 * This function searches the @phba for a vport that matches the content of the
16634 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16635 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16636 * returns the matching vport pointer or NULL if unable to match frame to a
16637 * vport.
16638 **/
16639static struct lpfc_vport *
16640lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16641 uint16_t fcfi, uint32_t did)
4f774513
JS
16642{
16643 struct lpfc_vport **vports;
16644 struct lpfc_vport *vport = NULL;
16645 int i;
939723a4 16646
bf08611b
JS
16647 if (did == Fabric_DID)
16648 return phba->pport;
939723a4
JS
16649 if ((phba->pport->fc_flag & FC_PT2PT) &&
16650 !(phba->link_state == LPFC_HBA_READY))
16651 return phba->pport;
16652
4f774513 16653 vports = lpfc_create_vport_work_array(phba);
895427bd 16654 if (vports != NULL) {
4f774513
JS
16655 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16656 if (phba->fcf.fcfi == fcfi &&
16657 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16658 vports[i]->fc_myDID == did) {
16659 vport = vports[i];
16660 break;
16661 }
16662 }
895427bd 16663 }
4f774513
JS
16664 lpfc_destroy_vport_work_array(phba, vports);
16665 return vport;
16666}
16667
45ed1190
JS
16668/**
16669 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16670 * @vport: The vport to work on.
16671 *
16672 * This function updates the receive sequence time stamp for this vport. The
16673 * receive sequence time stamp indicates the time that the last frame of the
16674 * the sequence that has been idle for the longest amount of time was received.
16675 * the driver uses this time stamp to indicate if any received sequences have
16676 * timed out.
16677 **/
5d8b8167 16678static void
45ed1190
JS
16679lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16680{
16681 struct lpfc_dmabuf *h_buf;
16682 struct hbq_dmabuf *dmabuf = NULL;
16683
16684 /* get the oldest sequence on the rcv list */
16685 h_buf = list_get_first(&vport->rcv_buffer_list,
16686 struct lpfc_dmabuf, list);
16687 if (!h_buf)
16688 return;
16689 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16690 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16691}
16692
16693/**
16694 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16695 * @vport: The vport that the received sequences were sent to.
16696 *
16697 * This function cleans up all outstanding received sequences. This is called
16698 * by the driver when a link event or user action invalidates all the received
16699 * sequences.
16700 **/
16701void
16702lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16703{
16704 struct lpfc_dmabuf *h_buf, *hnext;
16705 struct lpfc_dmabuf *d_buf, *dnext;
16706 struct hbq_dmabuf *dmabuf = NULL;
16707
16708 /* start with the oldest sequence on the rcv list */
16709 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16710 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16711 list_del_init(&dmabuf->hbuf.list);
16712 list_for_each_entry_safe(d_buf, dnext,
16713 &dmabuf->dbuf.list, list) {
16714 list_del_init(&d_buf->list);
16715 lpfc_in_buf_free(vport->phba, d_buf);
16716 }
16717 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16718 }
16719}
16720
16721/**
16722 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16723 * @vport: The vport that the received sequences were sent to.
16724 *
16725 * This function determines whether any received sequences have timed out by
16726 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16727 * indicates that there is at least one timed out sequence this routine will
16728 * go through the received sequences one at a time from most inactive to most
16729 * active to determine which ones need to be cleaned up. Once it has determined
16730 * that a sequence needs to be cleaned up it will simply free up the resources
16731 * without sending an abort.
16732 **/
16733void
16734lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16735{
16736 struct lpfc_dmabuf *h_buf, *hnext;
16737 struct lpfc_dmabuf *d_buf, *dnext;
16738 struct hbq_dmabuf *dmabuf = NULL;
16739 unsigned long timeout;
16740 int abort_count = 0;
16741
16742 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16743 vport->rcv_buffer_time_stamp);
16744 if (list_empty(&vport->rcv_buffer_list) ||
16745 time_before(jiffies, timeout))
16746 return;
16747 /* start with the oldest sequence on the rcv list */
16748 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16749 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16750 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16751 dmabuf->time_stamp);
16752 if (time_before(jiffies, timeout))
16753 break;
16754 abort_count++;
16755 list_del_init(&dmabuf->hbuf.list);
16756 list_for_each_entry_safe(d_buf, dnext,
16757 &dmabuf->dbuf.list, list) {
16758 list_del_init(&d_buf->list);
16759 lpfc_in_buf_free(vport->phba, d_buf);
16760 }
16761 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16762 }
16763 if (abort_count)
16764 lpfc_update_rcv_time_stamp(vport);
16765}
16766
4f774513
JS
16767/**
16768 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16769 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16770 *
16771 * This function searches through the existing incomplete sequences that have
16772 * been sent to this @vport. If the frame matches one of the incomplete
16773 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16774 * make up that sequence. If no sequence is found that matches this frame then
16775 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16776 * This function returns a pointer to the first dmabuf in the sequence list that
16777 * the frame was linked to.
16778 **/
16779static struct hbq_dmabuf *
16780lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16781{
16782 struct fc_frame_header *new_hdr;
16783 struct fc_frame_header *temp_hdr;
16784 struct lpfc_dmabuf *d_buf;
16785 struct lpfc_dmabuf *h_buf;
16786 struct hbq_dmabuf *seq_dmabuf = NULL;
16787 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16788 uint8_t found = 0;
4f774513 16789
4d9ab994 16790 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16791 dmabuf->time_stamp = jiffies;
4f774513 16792 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16793
4f774513
JS
16794 /* Use the hdr_buf to find the sequence that this frame belongs to */
16795 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16796 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16797 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16798 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16799 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16800 continue;
16801 /* found a pending sequence that matches this frame */
16802 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16803 break;
16804 }
16805 if (!seq_dmabuf) {
16806 /*
16807 * This indicates first frame received for this sequence.
16808 * Queue the buffer on the vport's rcv_buffer_list.
16809 */
16810 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16811 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16812 return dmabuf;
16813 }
16814 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16815 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16816 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16817 list_del_init(&seq_dmabuf->hbuf.list);
16818 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16819 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16820 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16821 return dmabuf;
16822 }
45ed1190
JS
16823 /* move this sequence to the tail to indicate a young sequence */
16824 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16825 seq_dmabuf->time_stamp = jiffies;
16826 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16827 if (list_empty(&seq_dmabuf->dbuf.list)) {
16828 temp_hdr = dmabuf->hbuf.virt;
16829 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16830 return seq_dmabuf;
16831 }
4f774513 16832 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16833 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16834 while (!found) {
4f774513
JS
16835 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16836 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16837 /*
16838 * If the frame's sequence count is greater than the frame on
16839 * the list then insert the frame right after this frame
16840 */
eeead811
JS
16841 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16842 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16843 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16844 found = 1;
16845 break;
4f774513 16846 }
4360ca9c
JS
16847
16848 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16849 break;
16850 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16851 }
4360ca9c
JS
16852
16853 if (found)
16854 return seq_dmabuf;
4f774513
JS
16855 return NULL;
16856}
16857
6669f9bb
JS
16858/**
16859 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16860 * @vport: pointer to a vitural port
16861 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16862 *
16863 * This function tries to abort from the partially assembed sequence, described
16864 * by the information from basic abbort @dmabuf. It checks to see whether such
16865 * partially assembled sequence held by the driver. If so, it shall free up all
16866 * the frames from the partially assembled sequence.
16867 *
16868 * Return
16869 * true -- if there is matching partially assembled sequence present and all
16870 * the frames freed with the sequence;
16871 * false -- if there is no matching partially assembled sequence present so
16872 * nothing got aborted in the lower layer driver
16873 **/
16874static bool
16875lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16876 struct hbq_dmabuf *dmabuf)
16877{
16878 struct fc_frame_header *new_hdr;
16879 struct fc_frame_header *temp_hdr;
16880 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16881 struct hbq_dmabuf *seq_dmabuf = NULL;
16882
16883 /* Use the hdr_buf to find the sequence that matches this frame */
16884 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16885 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16886 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16887 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16888 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16889 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16890 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16891 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16892 continue;
16893 /* found a pending sequence that matches this frame */
16894 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16895 break;
16896 }
16897
16898 /* Free up all the frames from the partially assembled sequence */
16899 if (seq_dmabuf) {
16900 list_for_each_entry_safe(d_buf, n_buf,
16901 &seq_dmabuf->dbuf.list, list) {
16902 list_del_init(&d_buf->list);
16903 lpfc_in_buf_free(vport->phba, d_buf);
16904 }
16905 return true;
16906 }
16907 return false;
16908}
16909
6dd9e31c
JS
16910/**
16911 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16912 * @vport: pointer to a vitural port
16913 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16914 *
16915 * This function tries to abort from the assembed sequence from upper level
16916 * protocol, described by the information from basic abbort @dmabuf. It
16917 * checks to see whether such pending context exists at upper level protocol.
16918 * If so, it shall clean up the pending context.
16919 *
16920 * Return
16921 * true -- if there is matching pending context of the sequence cleaned
16922 * at ulp;
16923 * false -- if there is no matching pending context of the sequence present
16924 * at ulp.
16925 **/
16926static bool
16927lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16928{
16929 struct lpfc_hba *phba = vport->phba;
16930 int handled;
16931
16932 /* Accepting abort at ulp with SLI4 only */
16933 if (phba->sli_rev < LPFC_SLI_REV4)
16934 return false;
16935
16936 /* Register all caring upper level protocols to attend abort */
16937 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16938 if (handled)
16939 return true;
16940
16941 return false;
16942}
16943
6669f9bb 16944/**
546fc854 16945 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16946 * @phba: Pointer to HBA context object.
16947 * @cmd_iocbq: pointer to the command iocbq structure.
16948 * @rsp_iocbq: pointer to the response iocbq structure.
16949 *
546fc854 16950 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16951 * event. It properly releases the memory allocated to the sequence abort
16952 * accept iocb.
16953 **/
16954static void
546fc854 16955lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16956 struct lpfc_iocbq *cmd_iocbq,
16957 struct lpfc_iocbq *rsp_iocbq)
16958{
6dd9e31c
JS
16959 struct lpfc_nodelist *ndlp;
16960
16961 if (cmd_iocbq) {
16962 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16963 lpfc_nlp_put(ndlp);
16964 lpfc_nlp_not_used(ndlp);
6669f9bb 16965 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16966 }
6b5151fd
JS
16967
16968 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16969 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16971 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16972 rsp_iocbq->iocb.ulpStatus,
16973 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16974}
16975
6d368e53
JS
16976/**
16977 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16978 * @phba: Pointer to HBA context object.
16979 * @xri: xri id in transaction.
16980 *
16981 * This function validates the xri maps to the known range of XRIs allocated an
16982 * used by the driver.
16983 **/
7851fe2c 16984uint16_t
6d368e53
JS
16985lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16986 uint16_t xri)
16987{
a2fc4aef 16988 uint16_t i;
6d368e53
JS
16989
16990 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16991 if (xri == phba->sli4_hba.xri_ids[i])
16992 return i;
16993 }
16994 return NO_XRI;
16995}
16996
6669f9bb 16997/**
546fc854 16998 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16999 * @phba: Pointer to HBA context object.
17000 * @fc_hdr: pointer to a FC frame header.
17001 *
546fc854 17002 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
17003 * event after aborting the sequence handling.
17004 **/
86c67379 17005void
6dd9e31c
JS
17006lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17007 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 17008{
6dd9e31c 17009 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
17010 struct lpfc_iocbq *ctiocb = NULL;
17011 struct lpfc_nodelist *ndlp;
ee0f4fe1 17012 uint16_t oxid, rxid, xri, lxri;
5ffc266e 17013 uint32_t sid, fctl;
6669f9bb 17014 IOCB_t *icmd;
546fc854 17015 int rc;
6669f9bb
JS
17016
17017 if (!lpfc_is_link_up(phba))
17018 return;
17019
17020 sid = sli4_sid_from_fc_hdr(fc_hdr);
17021 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 17022 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 17023
6dd9e31c 17024 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 17025 if (!ndlp) {
9d3d340d 17026 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
17027 if (!ndlp) {
17028 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17029 "1268 Failed to allocate ndlp for "
17030 "oxid:x%x SID:x%x\n", oxid, sid);
17031 return;
17032 }
6dd9e31c
JS
17033 /* Put ndlp onto pport node list */
17034 lpfc_enqueue_node(vport, ndlp);
17035 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17036 /* re-setup ndlp without removing from node list */
17037 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17038 if (!ndlp) {
17039 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17040 "3275 Failed to active ndlp found "
17041 "for oxid:x%x SID:x%x\n", oxid, sid);
17042 return;
17043 }
6669f9bb
JS
17044 }
17045
546fc854 17046 /* Allocate buffer for rsp iocb */
6669f9bb
JS
17047 ctiocb = lpfc_sli_get_iocbq(phba);
17048 if (!ctiocb)
17049 return;
17050
5ffc266e
JS
17051 /* Extract the F_CTL field from FC_HDR */
17052 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17053
6669f9bb 17054 icmd = &ctiocb->iocb;
6669f9bb 17055 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 17056 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
17057 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17058 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17059 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17060
17061 /* Fill in the rest of iocb fields */
17062 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17063 icmd->ulpBdeCount = 0;
17064 icmd->ulpLe = 1;
17065 icmd->ulpClass = CLASS3;
6d368e53 17066 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 17067 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 17068
6669f9bb
JS
17069 ctiocb->iocb_cmpl = NULL;
17070 ctiocb->vport = phba->pport;
546fc854 17071 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 17072 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
17073 ctiocb->sli4_xritag = NO_XRI;
17074
ee0f4fe1
JS
17075 if (fctl & FC_FC_EX_CTX)
17076 /* Exchange responder sent the abort so we
17077 * own the oxid.
17078 */
17079 xri = oxid;
17080 else
17081 xri = rxid;
17082 lxri = lpfc_sli4_xri_inrange(phba, xri);
17083 if (lxri != NO_XRI)
17084 lpfc_set_rrq_active(phba, ndlp, lxri,
17085 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
17086 /* For BA_ABTS from exchange responder, if the logical xri with
17087 * the oxid maps to the FCP XRI range, the port no longer has
17088 * that exchange context, send a BLS_RJT. Override the IOCB for
17089 * a BA_RJT.
17090 */
17091 if ((fctl & FC_FC_EX_CTX) &&
895427bd 17092 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
17093 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17094 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17095 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17096 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17097 }
17098
17099 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17100 * the driver no longer has that exchange, send a BLS_RJT. Override
17101 * the IOCB for a BA_RJT.
546fc854 17102 */
6dd9e31c 17103 if (aborted == false) {
546fc854
JS
17104 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17105 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17106 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17107 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17108 }
6669f9bb 17109
5ffc266e
JS
17110 if (fctl & FC_FC_EX_CTX) {
17111 /* ABTS sent by responder to CT exchange, construction
17112 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17113 * field and RX_ID from ABTS for RX_ID field.
17114 */
546fc854 17115 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
17116 } else {
17117 /* ABTS sent by initiator to CT exchange, construction
17118 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 17119 * XRI_TAG field.
5ffc266e 17120 */
546fc854 17121 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 17122 }
f09c3acc 17123 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 17124 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 17125
546fc854 17126 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
17127 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17128 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17129 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
17130
17131 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17132 if (rc == IOCB_ERROR) {
6dd9e31c
JS
17133 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17134 "2925 Failed to issue CT ABTS RSP x%x on "
17135 "xri x%x, Data x%x\n",
17136 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17137 phba->link_state);
17138 lpfc_nlp_put(ndlp);
17139 ctiocb->context1 = NULL;
546fc854
JS
17140 lpfc_sli_release_iocbq(phba, ctiocb);
17141 }
6669f9bb
JS
17142}
17143
17144/**
17145 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17146 * @vport: Pointer to the vport on which this sequence was received
17147 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17148 *
17149 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17150 * receive sequence is only partially assembed by the driver, it shall abort
17151 * the partially assembled frames for the sequence. Otherwise, if the
17152 * unsolicited receive sequence has been completely assembled and passed to
17153 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17154 * unsolicited sequence has been aborted. After that, it will issue a basic
17155 * accept to accept the abort.
17156 **/
5d8b8167 17157static void
6669f9bb
JS
17158lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17159 struct hbq_dmabuf *dmabuf)
17160{
17161 struct lpfc_hba *phba = vport->phba;
17162 struct fc_frame_header fc_hdr;
5ffc266e 17163 uint32_t fctl;
6dd9e31c 17164 bool aborted;
6669f9bb 17165
6669f9bb
JS
17166 /* Make a copy of fc_hdr before the dmabuf being released */
17167 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 17168 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 17169
5ffc266e 17170 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
17171 /* ABTS by responder to exchange, no cleanup needed */
17172 aborted = true;
5ffc266e 17173 } else {
6dd9e31c
JS
17174 /* ABTS by initiator to exchange, need to do cleanup */
17175 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17176 if (aborted == false)
17177 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 17178 }
6dd9e31c
JS
17179 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17180
86c67379
JS
17181 if (phba->nvmet_support) {
17182 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17183 return;
17184 }
17185
6dd9e31c
JS
17186 /* Respond with BA_ACC or BA_RJT accordingly */
17187 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
17188}
17189
4f774513
JS
17190/**
17191 * lpfc_seq_complete - Indicates if a sequence is complete
17192 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17193 *
17194 * This function checks the sequence, starting with the frame described by
17195 * @dmabuf, to see if all the frames associated with this sequence are present.
17196 * the frames associated with this sequence are linked to the @dmabuf using the
17197 * dbuf list. This function looks for two major things. 1) That the first frame
17198 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17199 * set. 3) That there are no holes in the sequence count. The function will
17200 * return 1 when the sequence is complete, otherwise it will return 0.
17201 **/
17202static int
17203lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17204{
17205 struct fc_frame_header *hdr;
17206 struct lpfc_dmabuf *d_buf;
17207 struct hbq_dmabuf *seq_dmabuf;
17208 uint32_t fctl;
17209 int seq_count = 0;
17210
17211 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17212 /* make sure first fame of sequence has a sequence count of zero */
17213 if (hdr->fh_seq_cnt != seq_count)
17214 return 0;
17215 fctl = (hdr->fh_f_ctl[0] << 16 |
17216 hdr->fh_f_ctl[1] << 8 |
17217 hdr->fh_f_ctl[2]);
17218 /* If last frame of sequence we can return success. */
17219 if (fctl & FC_FC_END_SEQ)
17220 return 1;
17221 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17222 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17223 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17224 /* If there is a hole in the sequence count then fail. */
eeead811 17225 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
17226 return 0;
17227 fctl = (hdr->fh_f_ctl[0] << 16 |
17228 hdr->fh_f_ctl[1] << 8 |
17229 hdr->fh_f_ctl[2]);
17230 /* If last frame of sequence we can return success. */
17231 if (fctl & FC_FC_END_SEQ)
17232 return 1;
17233 }
17234 return 0;
17235}
17236
17237/**
17238 * lpfc_prep_seq - Prep sequence for ULP processing
17239 * @vport: Pointer to the vport on which this sequence was received
17240 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17241 *
17242 * This function takes a sequence, described by a list of frames, and creates
17243 * a list of iocbq structures to describe the sequence. This iocbq list will be
17244 * used to issue to the generic unsolicited sequence handler. This routine
17245 * returns a pointer to the first iocbq in the list. If the function is unable
17246 * to allocate an iocbq then it throw out the received frames that were not
17247 * able to be described and return a pointer to the first iocbq. If unable to
17248 * allocate any iocbqs (including the first) this function will return NULL.
17249 **/
17250static struct lpfc_iocbq *
17251lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17252{
7851fe2c 17253 struct hbq_dmabuf *hbq_buf;
4f774513
JS
17254 struct lpfc_dmabuf *d_buf, *n_buf;
17255 struct lpfc_iocbq *first_iocbq, *iocbq;
17256 struct fc_frame_header *fc_hdr;
17257 uint32_t sid;
7851fe2c 17258 uint32_t len, tot_len;
eeead811 17259 struct ulp_bde64 *pbde;
4f774513
JS
17260
17261 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17262 /* remove from receive buffer list */
17263 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 17264 lpfc_update_rcv_time_stamp(vport);
4f774513 17265 /* get the Remote Port's SID */
6669f9bb 17266 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 17267 tot_len = 0;
4f774513
JS
17268 /* Get an iocbq struct to fill in. */
17269 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17270 if (first_iocbq) {
17271 /* Initialize the first IOCB. */
8fa38513 17272 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 17273 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 17274 first_iocbq->vport = vport;
939723a4
JS
17275
17276 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17277 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17278 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17279 first_iocbq->iocb.un.rcvels.parmRo =
17280 sli4_did_from_fc_hdr(fc_hdr);
17281 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17282 } else
17283 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17284 first_iocbq->iocb.ulpContext = NO_XRI;
17285 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17286 be16_to_cpu(fc_hdr->fh_ox_id);
17287 /* iocbq is prepped for internal consumption. Physical vpi. */
17288 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17289 vport->phba->vpi_ids[vport->vpi];
4f774513 17290 /* put the first buffer into the first IOCBq */
48a5a664
JS
17291 tot_len = bf_get(lpfc_rcqe_length,
17292 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17293
4f774513
JS
17294 first_iocbq->context2 = &seq_dmabuf->dbuf;
17295 first_iocbq->context3 = NULL;
17296 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17297 if (tot_len > LPFC_DATA_BUF_SIZE)
17298 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17299 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17300 else
17301 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17302
4f774513 17303 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17304
7851fe2c 17305 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17306 }
17307 iocbq = first_iocbq;
17308 /*
17309 * Each IOCBq can have two Buffers assigned, so go through the list
17310 * of buffers for this sequence and save two buffers in each IOCBq
17311 */
17312 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17313 if (!iocbq) {
17314 lpfc_in_buf_free(vport->phba, d_buf);
17315 continue;
17316 }
17317 if (!iocbq->context3) {
17318 iocbq->context3 = d_buf;
17319 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17320 /* We need to get the size out of the right CQE */
17321 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17322 len = bf_get(lpfc_rcqe_length,
17323 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17324 pbde = (struct ulp_bde64 *)
17325 &iocbq->iocb.unsli3.sli3Words[4];
17326 if (len > LPFC_DATA_BUF_SIZE)
17327 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17328 else
17329 pbde->tus.f.bdeSize = len;
17330
7851fe2c
JS
17331 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17332 tot_len += len;
4f774513
JS
17333 } else {
17334 iocbq = lpfc_sli_get_iocbq(vport->phba);
17335 if (!iocbq) {
17336 if (first_iocbq) {
17337 first_iocbq->iocb.ulpStatus =
17338 IOSTAT_FCP_RSP_ERROR;
17339 first_iocbq->iocb.un.ulpWord[4] =
17340 IOERR_NO_RESOURCES;
17341 }
17342 lpfc_in_buf_free(vport->phba, d_buf);
17343 continue;
17344 }
48a5a664
JS
17345 /* We need to get the size out of the right CQE */
17346 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17347 len = bf_get(lpfc_rcqe_length,
17348 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17349 iocbq->context2 = d_buf;
17350 iocbq->context3 = NULL;
17351 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17352 if (len > LPFC_DATA_BUF_SIZE)
17353 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17354 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17355 else
17356 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17357
7851fe2c
JS
17358 tot_len += len;
17359 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17360
4f774513
JS
17361 iocbq->iocb.un.rcvels.remoteID = sid;
17362 list_add_tail(&iocbq->list, &first_iocbq->list);
17363 }
17364 }
17365 return first_iocbq;
17366}
17367
6669f9bb
JS
17368static void
17369lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17370 struct hbq_dmabuf *seq_dmabuf)
17371{
17372 struct fc_frame_header *fc_hdr;
17373 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17374 struct lpfc_hba *phba = vport->phba;
17375
17376 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17377 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17378 if (!iocbq) {
17379 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17380 "2707 Ring %d handler: Failed to allocate "
17381 "iocb Rctl x%x Type x%x received\n",
17382 LPFC_ELS_RING,
17383 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17384 return;
17385 }
17386 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17387 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17388 iocbq, fc_hdr->fh_r_ctl,
17389 fc_hdr->fh_type))
6d368e53 17390 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17391 "2540 Ring %d handler: unexpected Rctl "
17392 "x%x Type x%x received\n",
17393 LPFC_ELS_RING,
17394 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17395
17396 /* Free iocb created in lpfc_prep_seq */
17397 list_for_each_entry_safe(curr_iocb, next_iocb,
17398 &iocbq->list, list) {
17399 list_del_init(&curr_iocb->list);
17400 lpfc_sli_release_iocbq(phba, curr_iocb);
17401 }
17402 lpfc_sli_release_iocbq(phba, iocbq);
17403}
17404
ae9e28f3
JS
17405static void
17406lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17407 struct lpfc_iocbq *rspiocb)
17408{
17409 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17410
17411 if (pcmd && pcmd->virt)
771db5c0 17412 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17413 kfree(pcmd);
17414 lpfc_sli_release_iocbq(phba, cmdiocb);
17415}
17416
17417static void
17418lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17419 struct hbq_dmabuf *dmabuf)
17420{
17421 struct fc_frame_header *fc_hdr;
17422 struct lpfc_hba *phba = vport->phba;
17423 struct lpfc_iocbq *iocbq = NULL;
17424 union lpfc_wqe *wqe;
17425 struct lpfc_dmabuf *pcmd = NULL;
17426 uint32_t frame_len;
17427 int rc;
17428
17429 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17430 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17431
17432 /* Send the received frame back */
17433 iocbq = lpfc_sli_get_iocbq(phba);
17434 if (!iocbq)
17435 goto exit;
17436
17437 /* Allocate buffer for command payload */
17438 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17439 if (pcmd)
771db5c0 17440 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17441 &pcmd->phys);
17442 if (!pcmd || !pcmd->virt)
17443 goto exit;
17444
17445 INIT_LIST_HEAD(&pcmd->list);
17446
17447 /* copyin the payload */
17448 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17449
17450 /* fill in BDE's for command */
17451 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17452 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17453 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17454 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17455
17456 iocbq->context2 = pcmd;
17457 iocbq->vport = vport;
17458 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17459 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17460
17461 /*
17462 * Setup rest of the iocb as though it were a WQE
17463 * Build the SEND_FRAME WQE
17464 */
17465 wqe = (union lpfc_wqe *)&iocbq->iocb;
17466
17467 wqe->send_frame.frame_len = frame_len;
17468 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17469 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17470 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17471 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17472 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17473 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17474
17475 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17476 iocbq->iocb.ulpLe = 1;
17477 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17478 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17479 if (rc == IOCB_ERROR)
17480 goto exit;
17481
17482 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17483 return;
17484
17485exit:
17486 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17487 "2023 Unable to process MDS loopback frame\n");
17488 if (pcmd && pcmd->virt)
771db5c0 17489 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17490 kfree(pcmd);
401bb416
DK
17491 if (iocbq)
17492 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17493 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17494}
17495
4f774513
JS
17496/**
17497 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17498 * @phba: Pointer to HBA context object.
17499 *
17500 * This function is called with no lock held. This function processes all
17501 * the received buffers and gives it to upper layers when a received buffer
17502 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17503 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17504 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17505 * appropriate receive function when the final frame in a sequence is received.
17506 **/
4d9ab994
JS
17507void
17508lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17509 struct hbq_dmabuf *dmabuf)
4f774513 17510{
4d9ab994 17511 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17512 struct fc_frame_header *fc_hdr;
17513 struct lpfc_vport *vport;
17514 uint32_t fcfi;
939723a4 17515 uint32_t did;
4f774513 17516
4f774513 17517 /* Process each received buffer */
4d9ab994 17518 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17519
4d9ab994
JS
17520 /* check to see if this a valid type of frame */
17521 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17522 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17523 return;
17524 }
2ea259ee 17525
7851fe2c
JS
17526 if ((bf_get(lpfc_cqe_code,
17527 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17528 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17529 &dmabuf->cq_event.cqe.rcqe_cmpl);
17530 else
17531 fcfi = bf_get(lpfc_rcqe_fcf_id,
17532 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17533
ae9e28f3
JS
17534 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17535 vport = phba->pport;
17536 /* Handle MDS Loopback frames */
17537 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17538 return;
17539 }
17540
895427bd
JS
17541 /* d_id this frame is directed to */
17542 did = sli4_did_from_fc_hdr(fc_hdr);
17543
17544 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17545 if (!vport) {
4d9ab994
JS
17546 /* throw out the frame */
17547 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17548 return;
17549 }
939723a4 17550
939723a4
JS
17551 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17552 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17553 (did != Fabric_DID)) {
17554 /*
17555 * Throw out the frame if we are not pt2pt.
17556 * The pt2pt protocol allows for discovery frames
17557 * to be received without a registered VPI.
17558 */
17559 if (!(vport->fc_flag & FC_PT2PT) ||
17560 (phba->link_state == LPFC_HBA_READY)) {
17561 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17562 return;
17563 }
17564 }
17565
6669f9bb
JS
17566 /* Handle the basic abort sequence (BA_ABTS) event */
17567 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17568 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17569 return;
17570 }
17571
4d9ab994
JS
17572 /* Link this frame */
17573 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17574 if (!seq_dmabuf) {
17575 /* unable to add frame to vport - throw it out */
17576 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17577 return;
17578 }
17579 /* If not last frame in sequence continue processing frames. */
def9c7a9 17580 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17581 return;
def9c7a9 17582
6669f9bb
JS
17583 /* Send the complete sequence to the upper layer protocol */
17584 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17585}
6fb120a7
JS
17586
17587/**
17588 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17589 * @phba: pointer to lpfc hba data structure.
17590 *
17591 * This routine is invoked to post rpi header templates to the
17592 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17593 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17594 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17595 *
17596 * This routine does not require any locks. It's usage is expected
17597 * to be driver load or reset recovery when the driver is
17598 * sequential.
17599 *
17600 * Return codes
af901ca1 17601 * 0 - successful
d439d286 17602 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17603 * When this error occurs, the driver is not guaranteed
17604 * to have any rpi regions posted to the device and
17605 * must either attempt to repost the regions or take a
17606 * fatal error.
17607 **/
17608int
17609lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17610{
17611 struct lpfc_rpi_hdr *rpi_page;
17612 uint32_t rc = 0;
6d368e53
JS
17613 uint16_t lrpi = 0;
17614
17615 /* SLI4 ports that support extents do not require RPI headers. */
17616 if (!phba->sli4_hba.rpi_hdrs_in_use)
17617 goto exit;
17618 if (phba->sli4_hba.extents_in_use)
17619 return -EIO;
6fb120a7 17620
6fb120a7 17621 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17622 /*
17623 * Assign the rpi headers a physical rpi only if the driver
17624 * has not initialized those resources. A port reset only
17625 * needs the headers posted.
17626 */
17627 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17628 LPFC_RPI_RSRC_RDY)
17629 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17630
6fb120a7
JS
17631 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17632 if (rc != MBX_SUCCESS) {
17633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17634 "2008 Error %d posting all rpi "
17635 "headers\n", rc);
17636 rc = -EIO;
17637 break;
17638 }
17639 }
17640
6d368e53
JS
17641 exit:
17642 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17643 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17644 return rc;
17645}
17646
17647/**
17648 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17649 * @phba: pointer to lpfc hba data structure.
17650 * @rpi_page: pointer to the rpi memory region.
17651 *
17652 * This routine is invoked to post a single rpi header to the
17653 * HBA consistent with the SLI-4 interface spec. This memory region
17654 * maps up to 64 rpi context regions.
17655 *
17656 * Return codes
af901ca1 17657 * 0 - successful
d439d286
JS
17658 * -ENOMEM - No available memory
17659 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17660 **/
17661int
17662lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17663{
17664 LPFC_MBOXQ_t *mboxq;
17665 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17666 uint32_t rc = 0;
6fb120a7
JS
17667 uint32_t shdr_status, shdr_add_status;
17668 union lpfc_sli4_cfg_shdr *shdr;
17669
6d368e53
JS
17670 /* SLI4 ports that support extents do not require RPI headers. */
17671 if (!phba->sli4_hba.rpi_hdrs_in_use)
17672 return rc;
17673 if (phba->sli4_hba.extents_in_use)
17674 return -EIO;
17675
6fb120a7
JS
17676 /* The port is notified of the header region via a mailbox command. */
17677 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17678 if (!mboxq) {
17679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17680 "2001 Unable to allocate memory for issuing "
17681 "SLI_CONFIG_SPECIAL mailbox command\n");
17682 return -ENOMEM;
17683 }
17684
17685 /* Post all rpi memory regions to the port. */
17686 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17687 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17688 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17689 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17690 sizeof(struct lpfc_sli4_cfg_mhdr),
17691 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17692
17693
17694 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17695 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17696 rpi_page->start_rpi);
6d368e53
JS
17697 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17698 hdr_tmpl, rpi_page->page_count);
17699
6fb120a7
JS
17700 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17701 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17702 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17703 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17704 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17705 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17706 if (rc != MBX_TIMEOUT)
17707 mempool_free(mboxq, phba->mbox_mem_pool);
17708 if (shdr_status || shdr_add_status || rc) {
17709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17710 "2514 POST_RPI_HDR mailbox failed with "
17711 "status x%x add_status x%x, mbx status x%x\n",
17712 shdr_status, shdr_add_status, rc);
17713 rc = -ENXIO;
845d9e8d
JS
17714 } else {
17715 /*
17716 * The next_rpi stores the next logical module-64 rpi value used
17717 * to post physical rpis in subsequent rpi postings.
17718 */
17719 spin_lock_irq(&phba->hbalock);
17720 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17721 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17722 }
17723 return rc;
17724}
17725
17726/**
17727 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17728 * @phba: pointer to lpfc hba data structure.
17729 *
17730 * This routine is invoked to post rpi header templates to the
17731 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17732 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17733 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17734 *
17735 * Returns
af901ca1 17736 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17737 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17738 **/
17739int
17740lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17741{
6d368e53
JS
17742 unsigned long rpi;
17743 uint16_t max_rpi, rpi_limit;
17744 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17745 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17746 unsigned long iflag;
6fb120a7 17747
6fb120a7 17748 /*
6d368e53
JS
17749 * Fetch the next logical rpi. Because this index is logical,
17750 * the driver starts at 0 each time.
6fb120a7 17751 */
4902b381 17752 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17753 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17754 rpi_limit = phba->sli4_hba.next_rpi;
17755
6d368e53
JS
17756 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17757 if (rpi >= rpi_limit)
6fb120a7
JS
17758 rpi = LPFC_RPI_ALLOC_ERROR;
17759 else {
17760 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17761 phba->sli4_hba.max_cfg_param.rpi_used++;
17762 phba->sli4_hba.rpi_count++;
17763 }
be6bb941
JS
17764 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17765 "0001 rpi:%x max:%x lim:%x\n",
17766 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17767
17768 /*
17769 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17770 * has been exhausted.
6fb120a7
JS
17771 */
17772 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17773 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17774 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17775 return rpi;
17776 }
17777
6d368e53
JS
17778 /*
17779 * RPI header postings are not required for SLI4 ports capable of
17780 * extents.
17781 */
17782 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17783 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17784 return rpi;
17785 }
17786
6fb120a7
JS
17787 /*
17788 * If the driver is running low on rpi resources, allocate another
17789 * page now. Note that the next_rpi value is used because
17790 * it represents how many are actually in use whereas max_rpi notes
17791 * how many are supported max by the device.
17792 */
6d368e53 17793 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17794 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17795 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17796 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17797 if (!rpi_hdr) {
17798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17799 "2002 Error Could not grow rpi "
17800 "count\n");
17801 } else {
6d368e53
JS
17802 lrpi = rpi_hdr->start_rpi;
17803 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17804 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17805 }
17806 }
17807
17808 return rpi;
17809}
17810
d7c47992
JS
17811/**
17812 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17813 * @phba: pointer to lpfc hba data structure.
17814 *
17815 * This routine is invoked to release an rpi to the pool of
17816 * available rpis maintained by the driver.
17817 **/
5d8b8167 17818static void
d7c47992
JS
17819__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17820{
17821 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17822 phba->sli4_hba.rpi_count--;
17823 phba->sli4_hba.max_cfg_param.rpi_used--;
17824 }
17825}
17826
6fb120a7
JS
17827/**
17828 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17829 * @phba: pointer to lpfc hba data structure.
17830 *
17831 * This routine is invoked to release an rpi to the pool of
17832 * available rpis maintained by the driver.
17833 **/
17834void
17835lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17836{
17837 spin_lock_irq(&phba->hbalock);
d7c47992 17838 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17839 spin_unlock_irq(&phba->hbalock);
17840}
17841
17842/**
17843 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17844 * @phba: pointer to lpfc hba data structure.
17845 *
17846 * This routine is invoked to remove the memory region that
17847 * provided rpi via a bitmask.
17848 **/
17849void
17850lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17851{
17852 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17853 kfree(phba->sli4_hba.rpi_ids);
17854 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17855}
17856
17857/**
17858 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17859 * @phba: pointer to lpfc hba data structure.
17860 *
17861 * This routine is invoked to remove the memory region that
17862 * provided rpi via a bitmask.
17863 **/
17864int
6b5151fd
JS
17865lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17866 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17867{
17868 LPFC_MBOXQ_t *mboxq;
17869 struct lpfc_hba *phba = ndlp->phba;
17870 int rc;
17871
17872 /* The port is notified of the header region via a mailbox command. */
17873 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17874 if (!mboxq)
17875 return -ENOMEM;
17876
17877 /* Post all rpi memory regions to the port. */
17878 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17879 if (cmpl) {
17880 mboxq->mbox_cmpl = cmpl;
17881 mboxq->context1 = arg;
17882 mboxq->context2 = ndlp;
72859909
JS
17883 } else
17884 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17885 mboxq->vport = ndlp->vport;
6fb120a7
JS
17886 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17887 if (rc == MBX_NOT_FINISHED) {
17888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17889 "2010 Resume RPI Mailbox failed "
17890 "status %d, mbxStatus x%x\n", rc,
17891 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17892 mempool_free(mboxq, phba->mbox_mem_pool);
17893 return -EIO;
17894 }
17895 return 0;
17896}
17897
17898/**
17899 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17900 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17901 *
76a95d75 17902 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17903 *
17904 * Returns:
17905 * 0 success
17906 * -Evalue otherwise
17907 **/
17908int
76a95d75 17909lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17910{
17911 LPFC_MBOXQ_t *mboxq;
17912 int rc = 0;
6a9c52cf 17913 int retval = MBX_SUCCESS;
6fb120a7 17914 uint32_t mbox_tmo;
76a95d75 17915 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17916 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17917 if (!mboxq)
17918 return -ENOMEM;
76a95d75 17919 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17920 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17921 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17922 if (rc != MBX_SUCCESS) {
76a95d75 17923 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17924 "2022 INIT VPI Mailbox failed "
17925 "status %d, mbxStatus x%x\n", rc,
17926 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17927 retval = -EIO;
6fb120a7 17928 }
6a9c52cf 17929 if (rc != MBX_TIMEOUT)
76a95d75 17930 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17931
17932 return retval;
6fb120a7
JS
17933}
17934
17935/**
17936 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17937 * @phba: pointer to lpfc hba data structure.
17938 * @mboxq: Pointer to mailbox object.
17939 *
17940 * This routine is invoked to manually add a single FCF record. The caller
17941 * must pass a completely initialized FCF_Record. This routine takes
17942 * care of the nonembedded mailbox operations.
17943 **/
17944static void
17945lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17946{
17947 void *virt_addr;
17948 union lpfc_sli4_cfg_shdr *shdr;
17949 uint32_t shdr_status, shdr_add_status;
17950
17951 virt_addr = mboxq->sge_array->addr[0];
17952 /* The IOCTL status is embedded in the mailbox subheader. */
17953 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17954 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17955 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17956
17957 if ((shdr_status || shdr_add_status) &&
17958 (shdr_status != STATUS_FCF_IN_USE))
17959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17960 "2558 ADD_FCF_RECORD mailbox failed with "
17961 "status x%x add_status x%x\n",
17962 shdr_status, shdr_add_status);
17963
17964 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17965}
17966
17967/**
17968 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17969 * @phba: pointer to lpfc hba data structure.
17970 * @fcf_record: pointer to the initialized fcf record to add.
17971 *
17972 * This routine is invoked to manually add a single FCF record. The caller
17973 * must pass a completely initialized FCF_Record. This routine takes
17974 * care of the nonembedded mailbox operations.
17975 **/
17976int
17977lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17978{
17979 int rc = 0;
17980 LPFC_MBOXQ_t *mboxq;
17981 uint8_t *bytep;
17982 void *virt_addr;
6fb120a7
JS
17983 struct lpfc_mbx_sge sge;
17984 uint32_t alloc_len, req_len;
17985 uint32_t fcfindex;
17986
17987 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17988 if (!mboxq) {
17989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17990 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17991 return -ENOMEM;
17992 }
17993
17994 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17995 sizeof(uint32_t);
17996
17997 /* Allocate DMA memory and set up the non-embedded mailbox command */
17998 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17999 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18000 req_len, LPFC_SLI4_MBX_NEMBED);
18001 if (alloc_len < req_len) {
18002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18003 "2523 Allocated DMA memory size (x%x) is "
18004 "less than the requested DMA memory "
18005 "size (x%x)\n", alloc_len, req_len);
18006 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18007 return -ENOMEM;
18008 }
18009
18010 /*
18011 * Get the first SGE entry from the non-embedded DMA memory. This
18012 * routine only uses a single SGE.
18013 */
18014 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
18015 virt_addr = mboxq->sge_array->addr[0];
18016 /*
18017 * Configure the FCF record for FCFI 0. This is the driver's
18018 * hardcoded default and gets used in nonFIP mode.
18019 */
18020 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18021 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18022 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18023
18024 /*
18025 * Copy the fcf_index and the FCF Record Data. The data starts after
18026 * the FCoE header plus word10. The data copy needs to be endian
18027 * correct.
18028 */
18029 bytep += sizeof(uint32_t);
18030 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18031 mboxq->vport = phba->pport;
18032 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18033 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18034 if (rc == MBX_NOT_FINISHED) {
18035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18036 "2515 ADD_FCF_RECORD mailbox failed with "
18037 "status 0x%x\n", rc);
18038 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18039 rc = -EIO;
18040 } else
18041 rc = 0;
18042
18043 return rc;
18044}
18045
18046/**
18047 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18048 * @phba: pointer to lpfc hba data structure.
18049 * @fcf_record: pointer to the fcf record to write the default data.
18050 * @fcf_index: FCF table entry index.
18051 *
18052 * This routine is invoked to build the driver's default FCF record. The
18053 * values used are hardcoded. This routine handles memory initialization.
18054 *
18055 **/
18056void
18057lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18058 struct fcf_record *fcf_record,
18059 uint16_t fcf_index)
18060{
18061 memset(fcf_record, 0, sizeof(struct fcf_record));
18062 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18063 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18064 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18065 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18066 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18067 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18068 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18069 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18070 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18071 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18072 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18073 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18074 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 18075 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
18076 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18077 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18078 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18079 /* Set the VLAN bit map */
18080 if (phba->valid_vlan) {
18081 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18082 = 1 << (phba->vlan_id % 8);
18083 }
18084}
18085
18086/**
0c9ab6f5 18087 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
18088 * @phba: pointer to lpfc hba data structure.
18089 * @fcf_index: FCF table entry offset.
18090 *
0c9ab6f5
JS
18091 * This routine is invoked to scan the entire FCF table by reading FCF
18092 * record and processing it one at a time starting from the @fcf_index
18093 * for initial FCF discovery or fast FCF failover rediscovery.
18094 *
25985edc 18095 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 18096 * otherwise.
6fb120a7
JS
18097 **/
18098int
0c9ab6f5 18099lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
18100{
18101 int rc = 0, error;
18102 LPFC_MBOXQ_t *mboxq;
6fb120a7 18103
32b9793f 18104 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 18105 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
18106 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18107 if (!mboxq) {
18108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18109 "2000 Failed to allocate mbox for "
18110 "READ_FCF cmd\n");
4d9ab994 18111 error = -ENOMEM;
0c9ab6f5 18112 goto fail_fcf_scan;
6fb120a7 18113 }
ecfd03c6 18114 /* Construct the read FCF record mailbox command */
0c9ab6f5 18115 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
18116 if (rc) {
18117 error = -EINVAL;
0c9ab6f5 18118 goto fail_fcf_scan;
6fb120a7 18119 }
ecfd03c6 18120 /* Issue the mailbox command asynchronously */
6fb120a7 18121 mboxq->vport = phba->pport;
0c9ab6f5 18122 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
18123
18124 spin_lock_irq(&phba->hbalock);
18125 phba->hba_flag |= FCF_TS_INPROG;
18126 spin_unlock_irq(&phba->hbalock);
18127
6fb120a7 18128 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 18129 if (rc == MBX_NOT_FINISHED)
6fb120a7 18130 error = -EIO;
ecfd03c6 18131 else {
38b92ef8
JS
18132 /* Reset eligible FCF count for new scan */
18133 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 18134 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 18135 error = 0;
32b9793f 18136 }
0c9ab6f5 18137fail_fcf_scan:
4d9ab994
JS
18138 if (error) {
18139 if (mboxq)
18140 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 18141 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 18142 spin_lock_irq(&phba->hbalock);
a93ff37a 18143 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
18144 spin_unlock_irq(&phba->hbalock);
18145 }
6fb120a7
JS
18146 return error;
18147}
a0c87cbd 18148
0c9ab6f5 18149/**
a93ff37a 18150 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
18151 * @phba: pointer to lpfc hba data structure.
18152 * @fcf_index: FCF table entry offset.
18153 *
18154 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 18155 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 18156 *
25985edc 18157 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18158 * otherwise.
18159 **/
18160int
18161lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18162{
18163 int rc = 0, error;
18164 LPFC_MBOXQ_t *mboxq;
18165
18166 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18167 if (!mboxq) {
18168 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18169 "2763 Failed to allocate mbox for "
18170 "READ_FCF cmd\n");
18171 error = -ENOMEM;
18172 goto fail_fcf_read;
18173 }
18174 /* Construct the read FCF record mailbox command */
18175 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18176 if (rc) {
18177 error = -EINVAL;
18178 goto fail_fcf_read;
18179 }
18180 /* Issue the mailbox command asynchronously */
18181 mboxq->vport = phba->pport;
18182 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18183 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18184 if (rc == MBX_NOT_FINISHED)
18185 error = -EIO;
18186 else
18187 error = 0;
18188
18189fail_fcf_read:
18190 if (error && mboxq)
18191 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18192 return error;
18193}
18194
18195/**
18196 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18197 * @phba: pointer to lpfc hba data structure.
18198 * @fcf_index: FCF table entry offset.
18199 *
18200 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 18201 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 18202 *
25985edc 18203 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18204 * otherwise.
18205 **/
18206int
18207lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18208{
18209 int rc = 0, error;
18210 LPFC_MBOXQ_t *mboxq;
18211
18212 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18213 if (!mboxq) {
18214 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18215 "2758 Failed to allocate mbox for "
18216 "READ_FCF cmd\n");
18217 error = -ENOMEM;
18218 goto fail_fcf_read;
18219 }
18220 /* Construct the read FCF record mailbox command */
18221 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18222 if (rc) {
18223 error = -EINVAL;
18224 goto fail_fcf_read;
18225 }
18226 /* Issue the mailbox command asynchronously */
18227 mboxq->vport = phba->pport;
18228 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18229 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18230 if (rc == MBX_NOT_FINISHED)
18231 error = -EIO;
18232 else
18233 error = 0;
18234
18235fail_fcf_read:
18236 if (error && mboxq)
18237 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18238 return error;
18239}
18240
7d791df7 18241/**
f5cb5304 18242 * lpfc_check_next_fcf_pri_level
7d791df7
JS
18243 * phba pointer to the lpfc_hba struct for this port.
18244 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18245 * routine when the rr_bmask is empty. The FCF indecies are put into the
18246 * rr_bmask based on their priority level. Starting from the highest priority
18247 * to the lowest. The most likely FCF candidate will be in the highest
18248 * priority group. When this routine is called it searches the fcf_pri list for
18249 * next lowest priority group and repopulates the rr_bmask with only those
18250 * fcf_indexes.
18251 * returns:
18252 * 1=success 0=failure
18253 **/
5d8b8167 18254static int
7d791df7
JS
18255lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18256{
18257 uint16_t next_fcf_pri;
18258 uint16_t last_index;
18259 struct lpfc_fcf_pri *fcf_pri;
18260 int rc;
18261 int ret = 0;
18262
18263 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18264 LPFC_SLI4_FCF_TBL_INDX_MAX);
18265 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18266 "3060 Last IDX %d\n", last_index);
2562669c
JS
18267
18268 /* Verify the priority list has 2 or more entries */
18269 spin_lock_irq(&phba->hbalock);
18270 if (list_empty(&phba->fcf.fcf_pri_list) ||
18271 list_is_singular(&phba->fcf.fcf_pri_list)) {
18272 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
18273 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18274 "3061 Last IDX %d\n", last_index);
18275 return 0; /* Empty rr list */
18276 }
2562669c
JS
18277 spin_unlock_irq(&phba->hbalock);
18278
7d791df7
JS
18279 next_fcf_pri = 0;
18280 /*
18281 * Clear the rr_bmask and set all of the bits that are at this
18282 * priority.
18283 */
18284 memset(phba->fcf.fcf_rr_bmask, 0,
18285 sizeof(*phba->fcf.fcf_rr_bmask));
18286 spin_lock_irq(&phba->hbalock);
18287 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18288 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18289 continue;
18290 /*
18291 * the 1st priority that has not FLOGI failed
18292 * will be the highest.
18293 */
18294 if (!next_fcf_pri)
18295 next_fcf_pri = fcf_pri->fcf_rec.priority;
18296 spin_unlock_irq(&phba->hbalock);
18297 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18298 rc = lpfc_sli4_fcf_rr_index_set(phba,
18299 fcf_pri->fcf_rec.fcf_index);
18300 if (rc)
18301 return 0;
18302 }
18303 spin_lock_irq(&phba->hbalock);
18304 }
18305 /*
18306 * if next_fcf_pri was not set above and the list is not empty then
18307 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18308 * and start at the beginning.
7d791df7
JS
18309 */
18310 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18311 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18312 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18313 /*
18314 * the 1st priority that has not FLOGI failed
18315 * will be the highest.
18316 */
18317 if (!next_fcf_pri)
18318 next_fcf_pri = fcf_pri->fcf_rec.priority;
18319 spin_unlock_irq(&phba->hbalock);
18320 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18321 rc = lpfc_sli4_fcf_rr_index_set(phba,
18322 fcf_pri->fcf_rec.fcf_index);
18323 if (rc)
18324 return 0;
18325 }
18326 spin_lock_irq(&phba->hbalock);
18327 }
18328 } else
18329 ret = 1;
18330 spin_unlock_irq(&phba->hbalock);
18331
18332 return ret;
18333}
0c9ab6f5
JS
18334/**
18335 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18336 * @phba: pointer to lpfc hba data structure.
18337 *
18338 * This routine is to get the next eligible FCF record index in a round
18339 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18340 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18341 * shall be returned, otherwise, the next eligible FCF record's index
18342 * shall be returned.
18343 **/
18344uint16_t
18345lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18346{
18347 uint16_t next_fcf_index;
18348
421c6622 18349initial_priority:
3804dc84 18350 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18351 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18352
7d791df7 18353next_priority:
421c6622
JS
18354 /* Determine the next fcf index to check */
18355 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18356 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18357 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18358 next_fcf_index);
18359
0c9ab6f5 18360 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18361 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18362 /*
18363 * If we have wrapped then we need to clear the bits that
18364 * have been tested so that we can detect when we should
18365 * change the priority level.
18366 */
0c9ab6f5
JS
18367 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18368 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18369 }
18370
3804dc84
JS
18371
18372 /* Check roundrobin failover list empty condition */
7d791df7
JS
18373 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18374 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18375 /*
18376 * If next fcf index is not found check if there are lower
18377 * Priority level fcf's in the fcf_priority list.
18378 * Set up the rr_bmask with all of the avaiable fcf bits
18379 * at that level and continue the selection process.
18380 */
18381 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18382 goto initial_priority;
3804dc84
JS
18383 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18384 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18385 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18386 return LPFC_FCOE_FCF_NEXT_NONE;
18387 else {
18388 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18389 "3063 Only FCF available idx %d, flag %x\n",
18390 next_fcf_index,
18391 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18392 return next_fcf_index;
18393 }
3804dc84
JS
18394 }
18395
7d791df7
JS
18396 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18397 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18398 LPFC_FCF_FLOGI_FAILED) {
18399 if (list_is_singular(&phba->fcf.fcf_pri_list))
18400 return LPFC_FCOE_FCF_NEXT_NONE;
18401
7d791df7 18402 goto next_priority;
f5cb5304 18403 }
7d791df7 18404
3804dc84 18405 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18406 "2845 Get next roundrobin failover FCF (x%x)\n",
18407 next_fcf_index);
18408
0c9ab6f5
JS
18409 return next_fcf_index;
18410}
18411
18412/**
18413 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18414 * @phba: pointer to lpfc hba data structure.
18415 *
18416 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18417 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18418 * does not go beyond the range of the driver allocated bmask dimension
18419 * before setting the bit.
18420 *
18421 * Returns 0 if the index bit successfully set, otherwise, it returns
18422 * -EINVAL.
18423 **/
18424int
18425lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18426{
18427 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18428 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18429 "2610 FCF (x%x) reached driver's book "
18430 "keeping dimension:x%x\n",
0c9ab6f5
JS
18431 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18432 return -EINVAL;
18433 }
18434 /* Set the eligible FCF record index bmask */
18435 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18436
3804dc84 18437 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18438 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18439 "bmask\n", fcf_index);
18440
0c9ab6f5
JS
18441 return 0;
18442}
18443
18444/**
3804dc84 18445 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18446 * @phba: pointer to lpfc hba data structure.
18447 *
18448 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18449 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18450 * does not go beyond the range of the driver allocated bmask dimension
18451 * before clearing the bit.
18452 **/
18453void
18454lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18455{
9a803a74 18456 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18457 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18458 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18459 "2762 FCF (x%x) reached driver's book "
18460 "keeping dimension:x%x\n",
0c9ab6f5
JS
18461 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18462 return;
18463 }
18464 /* Clear the eligible FCF record index bmask */
7d791df7 18465 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18466 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18467 list) {
7d791df7
JS
18468 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18469 list_del_init(&fcf_pri->list);
18470 break;
18471 }
18472 }
18473 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18474 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18475
18476 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18477 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18478 "bmask\n", fcf_index);
0c9ab6f5
JS
18479}
18480
ecfd03c6
JS
18481/**
18482 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18483 * @phba: pointer to lpfc hba data structure.
18484 *
18485 * This routine is the completion routine for the rediscover FCF table mailbox
18486 * command. If the mailbox command returned failure, it will try to stop the
18487 * FCF rediscover wait timer.
18488 **/
5d8b8167 18489static void
ecfd03c6
JS
18490lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18491{
18492 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18493 uint32_t shdr_status, shdr_add_status;
18494
18495 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18496
18497 shdr_status = bf_get(lpfc_mbox_hdr_status,
18498 &redisc_fcf->header.cfg_shdr.response);
18499 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18500 &redisc_fcf->header.cfg_shdr.response);
18501 if (shdr_status || shdr_add_status) {
0c9ab6f5 18502 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18503 "2746 Requesting for FCF rediscovery failed "
18504 "status x%x add_status x%x\n",
18505 shdr_status, shdr_add_status);
0c9ab6f5 18506 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18507 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18508 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18509 spin_unlock_irq(&phba->hbalock);
18510 /*
18511 * CVL event triggered FCF rediscover request failed,
18512 * last resort to re-try current registered FCF entry.
18513 */
18514 lpfc_retry_pport_discovery(phba);
18515 } else {
18516 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18517 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18518 spin_unlock_irq(&phba->hbalock);
18519 /*
18520 * DEAD FCF event triggered FCF rediscover request
18521 * failed, last resort to fail over as a link down
18522 * to FCF registration.
18523 */
18524 lpfc_sli4_fcf_dead_failthrough(phba);
18525 }
0c9ab6f5
JS
18526 } else {
18527 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18528 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18529 /*
18530 * Start FCF rediscovery wait timer for pending FCF
18531 * before rescan FCF record table.
18532 */
18533 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18534 }
ecfd03c6
JS
18535
18536 mempool_free(mbox, phba->mbox_mem_pool);
18537}
18538
18539/**
3804dc84 18540 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18541 * @phba: pointer to lpfc hba data structure.
18542 *
18543 * This routine is invoked to request for rediscovery of the entire FCF table
18544 * by the port.
18545 **/
18546int
18547lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18548{
18549 LPFC_MBOXQ_t *mbox;
18550 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18551 int rc, length;
18552
0c9ab6f5
JS
18553 /* Cancel retry delay timers to all vports before FCF rediscover */
18554 lpfc_cancel_all_vport_retry_delay_timer(phba);
18555
ecfd03c6
JS
18556 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18557 if (!mbox) {
18558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18559 "2745 Failed to allocate mbox for "
18560 "requesting FCF rediscover.\n");
18561 return -ENOMEM;
18562 }
18563
18564 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18565 sizeof(struct lpfc_sli4_cfg_mhdr));
18566 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18567 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18568 length, LPFC_SLI4_MBX_EMBED);
18569
18570 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18571 /* Set count to 0 for invalidating the entire FCF database */
18572 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18573
18574 /* Issue the mailbox command asynchronously */
18575 mbox->vport = phba->pport;
18576 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18577 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18578
18579 if (rc == MBX_NOT_FINISHED) {
18580 mempool_free(mbox, phba->mbox_mem_pool);
18581 return -EIO;
18582 }
18583 return 0;
18584}
18585
fc2b989b
JS
18586/**
18587 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18588 * @phba: pointer to lpfc hba data structure.
18589 *
18590 * This function is the failover routine as a last resort to the FCF DEAD
18591 * event when driver failed to perform fast FCF failover.
18592 **/
18593void
18594lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18595{
18596 uint32_t link_state;
18597
18598 /*
18599 * Last resort as FCF DEAD event failover will treat this as
18600 * a link down, but save the link state because we don't want
18601 * it to be changed to Link Down unless it is already down.
18602 */
18603 link_state = phba->link_state;
18604 lpfc_linkdown(phba);
18605 phba->link_state = link_state;
18606
18607 /* Unregister FCF if no devices connected to it */
18608 lpfc_unregister_unused_fcf(phba);
18609}
18610
a0c87cbd 18611/**
026abb87 18612 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18613 * @phba: pointer to lpfc hba data structure.
026abb87 18614 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18615 *
026abb87
JS
18616 * This function gets SLI3 port configure region 23 data through memory dump
18617 * mailbox command. When it successfully retrieves data, the size of the data
18618 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18619 **/
026abb87
JS
18620static uint32_t
18621lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18622{
18623 LPFC_MBOXQ_t *pmb = NULL;
18624 MAILBOX_t *mb;
026abb87 18625 uint32_t offset = 0;
a0c87cbd
JS
18626 int rc;
18627
026abb87
JS
18628 if (!rgn23_data)
18629 return 0;
18630
a0c87cbd
JS
18631 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18632 if (!pmb) {
18633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18634 "2600 failed to allocate mailbox memory\n");
18635 return 0;
a0c87cbd
JS
18636 }
18637 mb = &pmb->u.mb;
18638
a0c87cbd
JS
18639 do {
18640 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18641 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18642
18643 if (rc != MBX_SUCCESS) {
18644 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18645 "2601 failed to read config "
18646 "region 23, rc 0x%x Status 0x%x\n",
18647 rc, mb->mbxStatus);
a0c87cbd
JS
18648 mb->un.varDmp.word_cnt = 0;
18649 }
18650 /*
18651 * dump mem may return a zero when finished or we got a
18652 * mailbox error, either way we are done.
18653 */
18654 if (mb->un.varDmp.word_cnt == 0)
18655 break;
18656 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18657 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18658
18659 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18660 rgn23_data + offset,
18661 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18662 offset += mb->un.varDmp.word_cnt;
18663 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18664
026abb87
JS
18665 mempool_free(pmb, phba->mbox_mem_pool);
18666 return offset;
18667}
18668
18669/**
18670 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18671 * @phba: pointer to lpfc hba data structure.
18672 * @rgn23_data: pointer to configure region 23 data.
18673 *
18674 * This function gets SLI4 port configure region 23 data through memory dump
18675 * mailbox command. When it successfully retrieves data, the size of the data
18676 * will be returned, otherwise, 0 will be returned.
18677 **/
18678static uint32_t
18679lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18680{
18681 LPFC_MBOXQ_t *mboxq = NULL;
18682 struct lpfc_dmabuf *mp = NULL;
18683 struct lpfc_mqe *mqe;
18684 uint32_t data_length = 0;
18685 int rc;
18686
18687 if (!rgn23_data)
18688 return 0;
18689
18690 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18691 if (!mboxq) {
18692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18693 "3105 failed to allocate mailbox memory\n");
18694 return 0;
18695 }
18696
18697 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18698 goto out;
18699 mqe = &mboxq->u.mqe;
18700 mp = (struct lpfc_dmabuf *) mboxq->context1;
18701 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18702 if (rc)
18703 goto out;
18704 data_length = mqe->un.mb_words[5];
18705 if (data_length == 0)
18706 goto out;
18707 if (data_length > DMP_RGN23_SIZE) {
18708 data_length = 0;
18709 goto out;
18710 }
18711 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18712out:
18713 mempool_free(mboxq, phba->mbox_mem_pool);
18714 if (mp) {
18715 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18716 kfree(mp);
18717 }
18718 return data_length;
18719}
18720
18721/**
18722 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18723 * @phba: pointer to lpfc hba data structure.
18724 *
18725 * This function read region 23 and parse TLV for port status to
18726 * decide if the user disaled the port. If the TLV indicates the
18727 * port is disabled, the hba_flag is set accordingly.
18728 **/
18729void
18730lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18731{
18732 uint8_t *rgn23_data = NULL;
18733 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18734 uint32_t offset = 0;
18735
18736 /* Get adapter Region 23 data */
18737 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18738 if (!rgn23_data)
18739 goto out;
18740
18741 if (phba->sli_rev < LPFC_SLI_REV4)
18742 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18743 else {
18744 if_type = bf_get(lpfc_sli_intf_if_type,
18745 &phba->sli4_hba.sli_intf);
18746 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18747 goto out;
18748 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18749 }
a0c87cbd
JS
18750
18751 if (!data_size)
18752 goto out;
18753
18754 /* Check the region signature first */
18755 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18757 "2619 Config region 23 has bad signature\n");
18758 goto out;
18759 }
18760 offset += 4;
18761
18762 /* Check the data structure version */
18763 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18765 "2620 Config region 23 has bad version\n");
18766 goto out;
18767 }
18768 offset += 4;
18769
18770 /* Parse TLV entries in the region */
18771 while (offset < data_size) {
18772 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18773 break;
18774 /*
18775 * If the TLV is not driver specific TLV or driver id is
18776 * not linux driver id, skip the record.
18777 */
18778 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18779 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18780 (rgn23_data[offset + 3] != 0)) {
18781 offset += rgn23_data[offset + 1] * 4 + 4;
18782 continue;
18783 }
18784
18785 /* Driver found a driver specific TLV in the config region */
18786 sub_tlv_len = rgn23_data[offset + 1] * 4;
18787 offset += 4;
18788 tlv_offset = 0;
18789
18790 /*
18791 * Search for configured port state sub-TLV.
18792 */
18793 while ((offset < data_size) &&
18794 (tlv_offset < sub_tlv_len)) {
18795 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18796 offset += 4;
18797 tlv_offset += 4;
18798 break;
18799 }
18800 if (rgn23_data[offset] != PORT_STE_TYPE) {
18801 offset += rgn23_data[offset + 1] * 4 + 4;
18802 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18803 continue;
18804 }
18805
18806 /* This HBA contains PORT_STE configured */
18807 if (!rgn23_data[offset + 2])
18808 phba->hba_flag |= LINK_DISABLED;
18809
18810 goto out;
18811 }
18812 }
026abb87 18813
a0c87cbd 18814out:
a0c87cbd
JS
18815 kfree(rgn23_data);
18816 return;
18817}
695a814e 18818
52d52440
JS
18819/**
18820 * lpfc_wr_object - write an object to the firmware
18821 * @phba: HBA structure that indicates port to create a queue on.
18822 * @dmabuf_list: list of dmabufs to write to the port.
18823 * @size: the total byte value of the objects to write to the port.
18824 * @offset: the current offset to be used to start the transfer.
18825 *
18826 * This routine will create a wr_object mailbox command to send to the port.
18827 * the mailbox command will be constructed using the dma buffers described in
18828 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18829 * BDEs that the imbedded mailbox can support. The @offset variable will be
18830 * used to indicate the starting offset of the transfer and will also return
18831 * the offset after the write object mailbox has completed. @size is used to
18832 * determine the end of the object and whether the eof bit should be set.
18833 *
18834 * Return 0 is successful and offset will contain the the new offset to use
18835 * for the next write.
18836 * Return negative value for error cases.
18837 **/
18838int
18839lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18840 uint32_t size, uint32_t *offset)
18841{
18842 struct lpfc_mbx_wr_object *wr_object;
18843 LPFC_MBOXQ_t *mbox;
18844 int rc = 0, i = 0;
18845 uint32_t shdr_status, shdr_add_status;
18846 uint32_t mbox_tmo;
18847 union lpfc_sli4_cfg_shdr *shdr;
18848 struct lpfc_dmabuf *dmabuf;
18849 uint32_t written = 0;
18850
18851 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18852 if (!mbox)
18853 return -ENOMEM;
18854
18855 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18856 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18857 sizeof(struct lpfc_mbx_wr_object) -
18858 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18859
18860 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18861 wr_object->u.request.write_offset = *offset;
18862 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18863 wr_object->u.request.object_name[0] =
18864 cpu_to_le32(wr_object->u.request.object_name[0]);
18865 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18866 list_for_each_entry(dmabuf, dmabuf_list, list) {
18867 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18868 break;
18869 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18870 wr_object->u.request.bde[i].addrHigh =
18871 putPaddrHigh(dmabuf->phys);
18872 if (written + SLI4_PAGE_SIZE >= size) {
18873 wr_object->u.request.bde[i].tus.f.bdeSize =
18874 (size - written);
18875 written += (size - written);
18876 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18877 } else {
18878 wr_object->u.request.bde[i].tus.f.bdeSize =
18879 SLI4_PAGE_SIZE;
18880 written += SLI4_PAGE_SIZE;
18881 }
18882 i++;
18883 }
18884 wr_object->u.request.bde_count = i;
18885 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18886 if (!phba->sli4_hba.intr_enable)
18887 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18888 else {
a183a15f 18889 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18890 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18891 }
18892 /* The IOCTL status is embedded in the mailbox subheader. */
18893 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18894 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18895 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18896 if (rc != MBX_TIMEOUT)
18897 mempool_free(mbox, phba->mbox_mem_pool);
18898 if (shdr_status || shdr_add_status || rc) {
18899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18900 "3025 Write Object mailbox failed with "
18901 "status x%x add_status x%x, mbx status x%x\n",
18902 shdr_status, shdr_add_status, rc);
18903 rc = -ENXIO;
1feb8204 18904 *offset = shdr_add_status;
52d52440
JS
18905 } else
18906 *offset += wr_object->u.response.actual_write_length;
18907 return rc;
18908}
18909
695a814e
JS
18910/**
18911 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18912 * @vport: pointer to vport data structure.
18913 *
18914 * This function iterate through the mailboxq and clean up all REG_LOGIN
18915 * and REG_VPI mailbox commands associated with the vport. This function
18916 * is called when driver want to restart discovery of the vport due to
18917 * a Clear Virtual Link event.
18918 **/
18919void
18920lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18921{
18922 struct lpfc_hba *phba = vport->phba;
18923 LPFC_MBOXQ_t *mb, *nextmb;
18924 struct lpfc_dmabuf *mp;
78730cfe 18925 struct lpfc_nodelist *ndlp;
d439d286 18926 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18927 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18928 LIST_HEAD(mbox_cmd_list);
63e801ce 18929 uint8_t restart_loop;
695a814e 18930
d439d286 18931 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18932 spin_lock_irq(&phba->hbalock);
18933 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18934 if (mb->vport != vport)
18935 continue;
18936
18937 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18938 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18939 continue;
18940
d439d286
JS
18941 list_del(&mb->list);
18942 list_add_tail(&mb->list, &mbox_cmd_list);
18943 }
18944 /* Clean up active mailbox command with the vport */
18945 mb = phba->sli.mbox_active;
18946 if (mb && (mb->vport == vport)) {
18947 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18948 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18949 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18950 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18951 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18952 /* Put reference count for delayed processing */
18953 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18954 /* Unregister the RPI when mailbox complete */
18955 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18956 }
18957 }
63e801ce
JS
18958 /* Cleanup any mailbox completions which are not yet processed */
18959 do {
18960 restart_loop = 0;
18961 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18962 /*
18963 * If this mailox is already processed or it is
18964 * for another vport ignore it.
18965 */
18966 if ((mb->vport != vport) ||
18967 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18968 continue;
18969
18970 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18971 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18972 continue;
18973
18974 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18975 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18976 ndlp = (struct lpfc_nodelist *)mb->context2;
18977 /* Unregister the RPI when mailbox complete */
18978 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18979 restart_loop = 1;
18980 spin_unlock_irq(&phba->hbalock);
18981 spin_lock(shost->host_lock);
18982 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18983 spin_unlock(shost->host_lock);
18984 spin_lock_irq(&phba->hbalock);
18985 break;
18986 }
18987 }
18988 } while (restart_loop);
18989
d439d286
JS
18990 spin_unlock_irq(&phba->hbalock);
18991
18992 /* Release the cleaned-up mailbox commands */
18993 while (!list_empty(&mbox_cmd_list)) {
18994 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18995 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18996 mp = (struct lpfc_dmabuf *) (mb->context1);
18997 if (mp) {
18998 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18999 kfree(mp);
19000 }
78730cfe 19001 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 19002 mb->context2 = NULL;
78730cfe 19003 if (ndlp) {
ec21b3b0 19004 spin_lock(shost->host_lock);
589a52d6 19005 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 19006 spin_unlock(shost->host_lock);
78730cfe 19007 lpfc_nlp_put(ndlp);
78730cfe 19008 }
695a814e 19009 }
695a814e
JS
19010 mempool_free(mb, phba->mbox_mem_pool);
19011 }
d439d286
JS
19012
19013 /* Release the ndlp with the cleaned-up active mailbox command */
19014 if (act_mbx_ndlp) {
19015 spin_lock(shost->host_lock);
19016 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19017 spin_unlock(shost->host_lock);
19018 lpfc_nlp_put(act_mbx_ndlp);
695a814e 19019 }
695a814e
JS
19020}
19021
2a9bf3d0
JS
19022/**
19023 * lpfc_drain_txq - Drain the txq
19024 * @phba: Pointer to HBA context object.
19025 *
19026 * This function attempt to submit IOCBs on the txq
19027 * to the adapter. For SLI4 adapters, the txq contains
19028 * ELS IOCBs that have been deferred because the there
19029 * are no SGLs. This congestion can occur with large
19030 * vport counts during node discovery.
19031 **/
19032
19033uint32_t
19034lpfc_drain_txq(struct lpfc_hba *phba)
19035{
19036 LIST_HEAD(completions);
895427bd 19037 struct lpfc_sli_ring *pring;
2e706377 19038 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
19039 unsigned long iflags = 0;
19040 char *fail_msg = NULL;
19041 struct lpfc_sglq *sglq;
205e8240 19042 union lpfc_wqe128 wqe;
a2fc4aef 19043 uint32_t txq_cnt = 0;
2a9bf3d0 19044
895427bd 19045 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
19046 if (unlikely(!pring))
19047 return 0;
895427bd 19048
398d81c9 19049 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
19050 list_for_each_entry(piocbq, &pring->txq, list) {
19051 txq_cnt++;
19052 }
19053
19054 if (txq_cnt > pring->txq_max)
19055 pring->txq_max = txq_cnt;
2a9bf3d0 19056
398d81c9 19057 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19058
0e9bb8d7 19059 while (!list_empty(&pring->txq)) {
398d81c9 19060 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 19061
19ca7609 19062 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 19063 if (!piocbq) {
398d81c9 19064 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
19065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19066 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 19067 txq_cnt);
a629852a
JS
19068 break;
19069 }
895427bd 19070 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 19071 if (!sglq) {
19ca7609 19072 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 19073 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19074 break;
2a9bf3d0 19075 }
0e9bb8d7 19076 txq_cnt--;
2a9bf3d0
JS
19077
19078 /* The xri and iocb resources secured,
19079 * attempt to issue request
19080 */
6d368e53 19081 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
19082 piocbq->sli4_xritag = sglq->sli4_xritag;
19083 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19084 fail_msg = "to convert bpl to sgl";
205e8240 19085 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
2a9bf3d0 19086 fail_msg = "to convert iocb to wqe";
205e8240 19087 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
2a9bf3d0
JS
19088 fail_msg = " - Wq is full";
19089 else
19090 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19091
19092 if (fail_msg) {
19093 /* Failed means we can't issue and need to cancel */
19094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19095 "2822 IOCB failed %s iotag 0x%x "
19096 "xri 0x%x\n",
19097 fail_msg,
19098 piocbq->iotag, piocbq->sli4_xritag);
19099 list_add_tail(&piocbq->list, &completions);
19100 }
398d81c9 19101 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
19102 }
19103
2a9bf3d0
JS
19104 /* Cancel all the IOCBs that cannot be issued */
19105 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19106 IOERR_SLI_ABORTED);
19107
0e9bb8d7 19108 return txq_cnt;
2a9bf3d0 19109}
895427bd
JS
19110
19111/**
19112 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19113 * @phba: Pointer to HBA context object.
19114 * @pwqe: Pointer to command WQE.
19115 * @sglq: Pointer to the scatter gather queue object.
19116 *
19117 * This routine converts the bpl or bde that is in the WQE
19118 * to a sgl list for the sli4 hardware. The physical address
19119 * of the bpl/bde is converted back to a virtual address.
19120 * If the WQE contains a BPL then the list of BDE's is
19121 * converted to sli4_sge's. If the WQE contains a single
19122 * BDE then it is converted to a single sli_sge.
19123 * The WQE is still in cpu endianness so the contents of
19124 * the bpl can be used without byte swapping.
19125 *
19126 * Returns valid XRI = Success, NO_XRI = Failure.
19127 */
19128static uint16_t
19129lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19130 struct lpfc_sglq *sglq)
19131{
19132 uint16_t xritag = NO_XRI;
19133 struct ulp_bde64 *bpl = NULL;
19134 struct ulp_bde64 bde;
19135 struct sli4_sge *sgl = NULL;
19136 struct lpfc_dmabuf *dmabuf;
205e8240 19137 union lpfc_wqe128 *wqe;
895427bd
JS
19138 int numBdes = 0;
19139 int i = 0;
19140 uint32_t offset = 0; /* accumulated offset in the sg request list */
19141 int inbound = 0; /* number of sg reply entries inbound from firmware */
19142 uint32_t cmd;
19143
19144 if (!pwqeq || !sglq)
19145 return xritag;
19146
19147 sgl = (struct sli4_sge *)sglq->sgl;
19148 wqe = &pwqeq->wqe;
19149 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19150
19151 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19152 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19153 return sglq->sli4_xritag;
19154 numBdes = pwqeq->rsvd2;
19155 if (numBdes) {
19156 /* The addrHigh and addrLow fields within the WQE
19157 * have not been byteswapped yet so there is no
19158 * need to swap them back.
19159 */
19160 if (pwqeq->context3)
19161 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19162 else
19163 return xritag;
19164
19165 bpl = (struct ulp_bde64 *)dmabuf->virt;
19166 if (!bpl)
19167 return xritag;
19168
19169 for (i = 0; i < numBdes; i++) {
19170 /* Should already be byte swapped. */
19171 sgl->addr_hi = bpl->addrHigh;
19172 sgl->addr_lo = bpl->addrLow;
19173
19174 sgl->word2 = le32_to_cpu(sgl->word2);
19175 if ((i+1) == numBdes)
19176 bf_set(lpfc_sli4_sge_last, sgl, 1);
19177 else
19178 bf_set(lpfc_sli4_sge_last, sgl, 0);
19179 /* swap the size field back to the cpu so we
19180 * can assign it to the sgl.
19181 */
19182 bde.tus.w = le32_to_cpu(bpl->tus.w);
19183 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19184 /* The offsets in the sgl need to be accumulated
19185 * separately for the request and reply lists.
19186 * The request is always first, the reply follows.
19187 */
19188 switch (cmd) {
19189 case CMD_GEN_REQUEST64_WQE:
19190 /* add up the reply sg entries */
19191 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19192 inbound++;
19193 /* first inbound? reset the offset */
19194 if (inbound == 1)
19195 offset = 0;
19196 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19197 bf_set(lpfc_sli4_sge_type, sgl,
19198 LPFC_SGE_TYPE_DATA);
19199 offset += bde.tus.f.bdeSize;
19200 break;
19201 case CMD_FCP_TRSP64_WQE:
19202 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19203 bf_set(lpfc_sli4_sge_type, sgl,
19204 LPFC_SGE_TYPE_DATA);
19205 break;
19206 case CMD_FCP_TSEND64_WQE:
19207 case CMD_FCP_TRECEIVE64_WQE:
19208 bf_set(lpfc_sli4_sge_type, sgl,
19209 bpl->tus.f.bdeFlags);
19210 if (i < 3)
19211 offset = 0;
19212 else
19213 offset += bde.tus.f.bdeSize;
19214 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19215 break;
19216 }
19217 sgl->word2 = cpu_to_le32(sgl->word2);
19218 bpl++;
19219 sgl++;
19220 }
19221 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19222 /* The addrHigh and addrLow fields of the BDE have not
19223 * been byteswapped yet so they need to be swapped
19224 * before putting them in the sgl.
19225 */
19226 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19227 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19228 sgl->word2 = le32_to_cpu(sgl->word2);
19229 bf_set(lpfc_sli4_sge_last, sgl, 1);
19230 sgl->word2 = cpu_to_le32(sgl->word2);
19231 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19232 }
19233 return sglq->sli4_xritag;
19234}
19235
19236/**
19237 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19238 * @phba: Pointer to HBA context object.
19239 * @ring_number: Base sli ring number
19240 * @pwqe: Pointer to command WQE.
19241 **/
19242int
19243lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19244 struct lpfc_iocbq *pwqe)
19245{
205e8240 19246 union lpfc_wqe128 *wqe = &pwqe->wqe;
f358dd0c 19247 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
19248 struct lpfc_queue *wq;
19249 struct lpfc_sglq *sglq;
19250 struct lpfc_sli_ring *pring;
19251 unsigned long iflags;
cd22d605 19252 uint32_t ret = 0;
895427bd
JS
19253
19254 /* NVME_LS and NVME_LS ABTS requests. */
19255 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19256 pring = phba->sli4_hba.nvmels_wq->pring;
19257 spin_lock_irqsave(&pring->ring_lock, iflags);
19258 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19259 if (!sglq) {
19260 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19261 return WQE_BUSY;
19262 }
19263 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19264 pwqe->sli4_xritag = sglq->sli4_xritag;
19265 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19266 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19267 return WQE_ERROR;
19268 }
19269 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19270 pwqe->sli4_xritag);
cd22d605
DK
19271 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19272 if (ret) {
895427bd 19273 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19274 return ret;
895427bd 19275 }
cd22d605 19276
895427bd
JS
19277 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19278 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19279 return 0;
19280 }
19281
19282 /* NVME_FCREQ and NVME_ABTS requests */
19283 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19284 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19285 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19286
19287 spin_lock_irqsave(&pring->ring_lock, iflags);
19288 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19289 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19290 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19291 ret = lpfc_sli4_wq_put(wq, wqe);
19292 if (ret) {
895427bd 19293 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19294 return ret;
895427bd
JS
19295 }
19296 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19297 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19298 return 0;
19299 }
19300
f358dd0c
JS
19301 /* NVMET requests */
19302 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19303 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19304 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19305
19306 spin_lock_irqsave(&pring->ring_lock, iflags);
19307 ctxp = pwqe->context2;
6c621a22 19308 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19309 if (pwqe->sli4_xritag == NO_XRI) {
19310 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19311 pwqe->sli4_xritag = sglq->sli4_xritag;
19312 }
19313 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19314 pwqe->sli4_xritag);
19315 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19316 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19317 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19318 ret = lpfc_sli4_wq_put(wq, wqe);
19319 if (ret) {
f358dd0c 19320 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19321 return ret;
f358dd0c
JS
19322 }
19323 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19324 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19325 return 0;
19326 }
895427bd
JS
19327 return WQE_ERROR;
19328}