]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix mailbox wait for POST_SGL mbox command
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
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 2390{
858c9f6c 2391 unsigned long drvr_flag;
e29d74f8 2392 struct completion *pmbox_done;
dea3101e
JB
2393
2394 /*
e29d74f8 2395 * If pmbox_done is empty, the driver thread gave up waiting and
dea3101e
JB
2396 * continued running.
2397 */
7054a606 2398 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2399 spin_lock_irqsave(&phba->hbalock, drvr_flag);
e29d74f8
JS
2400 pmbox_done = (struct completion *)pmboxq->context3;
2401 if (pmbox_done)
2402 complete(pmbox_done);
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{
e29d74f8 11668 struct completion mbox_done;
dea3101e 11669 int retval;
858c9f6c 11670 unsigned long flag;
dea3101e 11671
495a714c 11672 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11673 /* setup wake call as IOCB callback */
11674 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea3101e 11675
e29d74f8
JS
11676 /* setup context3 field to pass wait_queue pointer to wake function */
11677 init_completion(&mbox_done);
11678 pmboxq->context3 = &mbox_done;
dea3101e
JB
11679 /* now issue the command */
11680 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11681 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
e29d74f8
JS
11682 wait_for_completion_timeout(&mbox_done,
11683 msecs_to_jiffies(timeout * 1000));
7054a606 11684
858c9f6c 11685 spin_lock_irqsave(&phba->hbalock, flag);
e29d74f8 11686 pmboxq->context3 = NULL;
7054a606
JS
11687 /*
11688 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11689 * else do not free the resources.
11690 */
d7c47992 11691 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11692 retval = MBX_SUCCESS;
d7c47992 11693 } else {
7054a606 11694 retval = MBX_TIMEOUT;
858c9f6c
JS
11695 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11696 }
11697 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 11698 }
dea3101e
JB
11699 return retval;
11700}
11701
e59058c4 11702/**
3772a991 11703 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11704 * @phba: Pointer to HBA context.
11705 *
3772a991
JS
11706 * This function is called to shutdown the driver's mailbox sub-system.
11707 * It first marks the mailbox sub-system is in a block state to prevent
11708 * the asynchronous mailbox command from issued off the pending mailbox
11709 * command queue. If the mailbox command sub-system shutdown is due to
11710 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11711 * the mailbox sub-system flush routine to forcefully bring down the
11712 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11713 * as with offline or HBA function reset), this routine will wait for the
11714 * outstanding mailbox command to complete before invoking the mailbox
11715 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11716 **/
3772a991 11717void
618a5230 11718lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11719{
3772a991 11720 struct lpfc_sli *psli = &phba->sli;
3772a991 11721 unsigned long timeout;
b4c02652 11722
618a5230
JS
11723 if (mbx_action == LPFC_MBX_NO_WAIT) {
11724 /* delay 100ms for port state */
11725 msleep(100);
11726 lpfc_sli_mbox_sys_flush(phba);
11727 return;
11728 }
a183a15f 11729 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11730
3772a991
JS
11731 spin_lock_irq(&phba->hbalock);
11732 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11733
3772a991 11734 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11735 /* Determine how long we might wait for the active mailbox
11736 * command to be gracefully completed by firmware.
11737 */
a183a15f
JS
11738 if (phba->sli.mbox_active)
11739 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11740 phba->sli.mbox_active) *
11741 1000) + jiffies;
11742 spin_unlock_irq(&phba->hbalock);
11743
3772a991
JS
11744 while (phba->sli.mbox_active) {
11745 /* Check active mailbox complete status every 2ms */
11746 msleep(2);
11747 if (time_after(jiffies, timeout))
11748 /* Timeout, let the mailbox flush routine to
11749 * forcefully release active mailbox command
11750 */
11751 break;
11752 }
d7069f09
JS
11753 } else
11754 spin_unlock_irq(&phba->hbalock);
11755
3772a991
JS
11756 lpfc_sli_mbox_sys_flush(phba);
11757}
ed957684 11758
3772a991
JS
11759/**
11760 * lpfc_sli_eratt_read - read sli-3 error attention events
11761 * @phba: Pointer to HBA context.
11762 *
11763 * This function is called to read the SLI3 device error attention registers
11764 * for possible error attention events. The caller must hold the hostlock
11765 * with spin_lock_irq().
11766 *
25985edc 11767 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11768 * Register and returns 0 otherwise.
11769 **/
11770static int
11771lpfc_sli_eratt_read(struct lpfc_hba *phba)
11772{
11773 uint32_t ha_copy;
b4c02652 11774
3772a991 11775 /* Read chip Host Attention (HA) register */
9940b97b
JS
11776 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11777 goto unplug_err;
11778
3772a991
JS
11779 if (ha_copy & HA_ERATT) {
11780 /* Read host status register to retrieve error event */
9940b97b
JS
11781 if (lpfc_sli_read_hs(phba))
11782 goto unplug_err;
b4c02652 11783
3772a991
JS
11784 /* Check if there is a deferred error condition is active */
11785 if ((HS_FFER1 & phba->work_hs) &&
11786 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11787 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11788 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11789 /* Clear all interrupt enable conditions */
11790 writel(0, phba->HCregaddr);
11791 readl(phba->HCregaddr);
11792 }
11793
11794 /* Set the driver HA work bitmap */
3772a991
JS
11795 phba->work_ha |= HA_ERATT;
11796 /* Indicate polling handles this ERATT */
11797 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11798 return 1;
11799 }
11800 return 0;
9940b97b
JS
11801
11802unplug_err:
11803 /* Set the driver HS work bitmap */
11804 phba->work_hs |= UNPLUG_ERR;
11805 /* Set the driver HA work bitmap */
11806 phba->work_ha |= HA_ERATT;
11807 /* Indicate polling handles this ERATT */
11808 phba->hba_flag |= HBA_ERATT_HANDLED;
11809 return 1;
b4c02652
JS
11810}
11811
da0436e9
JS
11812/**
11813 * lpfc_sli4_eratt_read - read sli-4 error attention events
11814 * @phba: Pointer to HBA context.
11815 *
11816 * This function is called to read the SLI4 device error attention registers
11817 * for possible error attention events. The caller must hold the hostlock
11818 * with spin_lock_irq().
11819 *
25985edc 11820 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11821 * Register and returns 0 otherwise.
11822 **/
11823static int
11824lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11825{
11826 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11827 uint32_t if_type, portsmphr;
11828 struct lpfc_register portstat_reg;
da0436e9 11829
2fcee4bf
JS
11830 /*
11831 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11832 * registers for error attention. This can be changed later.
11833 */
2fcee4bf
JS
11834 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11835 switch (if_type) {
11836 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11837 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11838 &uerr_sta_lo) ||
11839 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11840 &uerr_sta_hi)) {
11841 phba->work_hs |= UNPLUG_ERR;
11842 phba->work_ha |= HA_ERATT;
11843 phba->hba_flag |= HBA_ERATT_HANDLED;
11844 return 1;
11845 }
2fcee4bf
JS
11846 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11847 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11849 "1423 HBA Unrecoverable error: "
11850 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11851 "ue_mask_lo_reg=0x%x, "
11852 "ue_mask_hi_reg=0x%x\n",
11853 uerr_sta_lo, uerr_sta_hi,
11854 phba->sli4_hba.ue_mask_lo,
11855 phba->sli4_hba.ue_mask_hi);
11856 phba->work_status[0] = uerr_sta_lo;
11857 phba->work_status[1] = uerr_sta_hi;
11858 phba->work_ha |= HA_ERATT;
11859 phba->hba_flag |= HBA_ERATT_HANDLED;
11860 return 1;
11861 }
11862 break;
11863 case LPFC_SLI_INTF_IF_TYPE_2:
27d6ac0a 11864 case LPFC_SLI_INTF_IF_TYPE_6:
9940b97b
JS
11865 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11866 &portstat_reg.word0) ||
11867 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11868 &portsmphr)){
11869 phba->work_hs |= UNPLUG_ERR;
11870 phba->work_ha |= HA_ERATT;
11871 phba->hba_flag |= HBA_ERATT_HANDLED;
11872 return 1;
11873 }
2fcee4bf
JS
11874 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11875 phba->work_status[0] =
11876 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11877 phba->work_status[1] =
11878 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11879 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11880 "2885 Port Status Event: "
2fcee4bf
JS
11881 "port status reg 0x%x, "
11882 "port smphr reg 0x%x, "
11883 "error 1=0x%x, error 2=0x%x\n",
11884 portstat_reg.word0,
11885 portsmphr,
11886 phba->work_status[0],
11887 phba->work_status[1]);
11888 phba->work_ha |= HA_ERATT;
11889 phba->hba_flag |= HBA_ERATT_HANDLED;
11890 return 1;
11891 }
11892 break;
11893 case LPFC_SLI_INTF_IF_TYPE_1:
11894 default:
a747c9ce 11895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11896 "2886 HBA Error Attention on unsupported "
11897 "if type %d.", if_type);
a747c9ce 11898 return 1;
da0436e9 11899 }
2fcee4bf 11900
da0436e9
JS
11901 return 0;
11902}
11903
e59058c4 11904/**
3621a710 11905 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11906 * @phba: Pointer to HBA context.
11907 *
3772a991 11908 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11909 * error attention register bit for error attention events.
11910 *
25985edc 11911 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11912 * Register and returns 0 otherwise.
11913 **/
11914int
11915lpfc_sli_check_eratt(struct lpfc_hba *phba)
11916{
11917 uint32_t ha_copy;
11918
11919 /* If somebody is waiting to handle an eratt, don't process it
11920 * here. The brdkill function will do this.
11921 */
11922 if (phba->link_flag & LS_IGNORE_ERATT)
11923 return 0;
11924
11925 /* Check if interrupt handler handles this ERATT */
11926 spin_lock_irq(&phba->hbalock);
11927 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11928 /* Interrupt handler has handled ERATT */
11929 spin_unlock_irq(&phba->hbalock);
11930 return 0;
11931 }
11932
a257bf90
JS
11933 /*
11934 * If there is deferred error attention, do not check for error
11935 * attention
11936 */
11937 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11938 spin_unlock_irq(&phba->hbalock);
11939 return 0;
11940 }
11941
3772a991
JS
11942 /* If PCI channel is offline, don't process it */
11943 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11944 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11945 return 0;
11946 }
11947
11948 switch (phba->sli_rev) {
11949 case LPFC_SLI_REV2:
11950 case LPFC_SLI_REV3:
11951 /* Read chip Host Attention (HA) register */
11952 ha_copy = lpfc_sli_eratt_read(phba);
11953 break;
da0436e9 11954 case LPFC_SLI_REV4:
2fcee4bf 11955 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11956 ha_copy = lpfc_sli4_eratt_read(phba);
11957 break;
3772a991
JS
11958 default:
11959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11960 "0299 Invalid SLI revision (%d)\n",
11961 phba->sli_rev);
11962 ha_copy = 0;
11963 break;
9399627f
JS
11964 }
11965 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11966
11967 return ha_copy;
11968}
11969
11970/**
11971 * lpfc_intr_state_check - Check device state for interrupt handling
11972 * @phba: Pointer to HBA context.
11973 *
11974 * This inline routine checks whether a device or its PCI slot is in a state
11975 * that the interrupt should be handled.
11976 *
11977 * This function returns 0 if the device or the PCI slot is in a state that
11978 * interrupt should be handled, otherwise -EIO.
11979 */
11980static inline int
11981lpfc_intr_state_check(struct lpfc_hba *phba)
11982{
11983 /* If the pci channel is offline, ignore all the interrupts */
11984 if (unlikely(pci_channel_offline(phba->pcidev)))
11985 return -EIO;
11986
11987 /* Update device level interrupt statistics */
11988 phba->sli.slistat.sli_intr++;
11989
11990 /* Ignore all interrupts during initialization. */
11991 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11992 return -EIO;
11993
9399627f
JS
11994 return 0;
11995}
11996
11997/**
3772a991 11998 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11999 * @irq: Interrupt number.
12000 * @dev_id: The device context pointer.
12001 *
9399627f 12002 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12003 * service routine when device with SLI-3 interface spec is enabled with
12004 * MSI-X multi-message interrupt mode and there are slow-path events in
12005 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12006 * interrupt mode, this function is called as part of the device-level
12007 * interrupt handler. When the PCI slot is in error recovery or the HBA
12008 * is undergoing initialization, the interrupt handler will not process
12009 * the interrupt. The link attention and ELS ring attention events are
12010 * handled by the worker thread. The interrupt handler signals the worker
12011 * thread and returns for these events. This function is called without
12012 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
12013 * structures.
12014 *
12015 * This function returns IRQ_HANDLED when interrupt is handled else it
12016 * returns IRQ_NONE.
e59058c4 12017 **/
dea3101e 12018irqreturn_t
3772a991 12019lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 12020{
2e0fef85 12021 struct lpfc_hba *phba;
a747c9ce 12022 uint32_t ha_copy, hc_copy;
dea3101e
JB
12023 uint32_t work_ha_copy;
12024 unsigned long status;
5b75da2f 12025 unsigned long iflag;
dea3101e
JB
12026 uint32_t control;
12027
92d7f7b0 12028 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
12029 struct lpfc_vport *vport;
12030 struct lpfc_nodelist *ndlp;
12031 struct lpfc_dmabuf *mp;
92d7f7b0
JS
12032 LPFC_MBOXQ_t *pmb;
12033 int rc;
12034
dea3101e
JB
12035 /*
12036 * Get the driver's phba structure from the dev_id and
12037 * assume the HBA is not interrupting.
12038 */
9399627f 12039 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
12040
12041 if (unlikely(!phba))
12042 return IRQ_NONE;
12043
dea3101e 12044 /*
9399627f
JS
12045 * Stuff needs to be attented to when this function is invoked as an
12046 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 12047 */
9399627f 12048 if (phba->intr_type == MSIX) {
3772a991
JS
12049 /* Check device state for handling interrupt */
12050 if (lpfc_intr_state_check(phba))
9399627f
JS
12051 return IRQ_NONE;
12052 /* Need to read HA REG for slow-path events */
5b75da2f 12053 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12054 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12055 goto unplug_error;
9399627f
JS
12056 /* If somebody is waiting to handle an eratt don't process it
12057 * here. The brdkill function will do this.
12058 */
12059 if (phba->link_flag & LS_IGNORE_ERATT)
12060 ha_copy &= ~HA_ERATT;
12061 /* Check the need for handling ERATT in interrupt handler */
12062 if (ha_copy & HA_ERATT) {
12063 if (phba->hba_flag & HBA_ERATT_HANDLED)
12064 /* ERATT polling has handled ERATT */
12065 ha_copy &= ~HA_ERATT;
12066 else
12067 /* Indicate interrupt handler handles ERATT */
12068 phba->hba_flag |= HBA_ERATT_HANDLED;
12069 }
a257bf90
JS
12070
12071 /*
12072 * If there is deferred error attention, do not check for any
12073 * interrupt.
12074 */
12075 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12076 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12077 return IRQ_NONE;
12078 }
12079
9399627f 12080 /* Clear up only attention source related to slow-path */
9940b97b
JS
12081 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12082 goto unplug_error;
12083
a747c9ce
JS
12084 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12085 HC_LAINT_ENA | HC_ERINT_ENA),
12086 phba->HCregaddr);
9399627f
JS
12087 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12088 phba->HAregaddr);
a747c9ce 12089 writel(hc_copy, phba->HCregaddr);
9399627f 12090 readl(phba->HAregaddr); /* flush */
5b75da2f 12091 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12092 } else
12093 ha_copy = phba->ha_copy;
dea3101e 12094
dea3101e
JB
12095 work_ha_copy = ha_copy & phba->work_ha_mask;
12096
9399627f 12097 if (work_ha_copy) {
dea3101e
JB
12098 if (work_ha_copy & HA_LATT) {
12099 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12100 /*
12101 * Turn off Link Attention interrupts
12102 * until CLEAR_LA done
12103 */
5b75da2f 12104 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12105 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
12106 if (lpfc_readl(phba->HCregaddr, &control))
12107 goto unplug_error;
dea3101e
JB
12108 control &= ~HC_LAINT_ENA;
12109 writel(control, phba->HCregaddr);
12110 readl(phba->HCregaddr); /* flush */
5b75da2f 12111 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12112 }
12113 else
12114 work_ha_copy &= ~HA_LATT;
12115 }
12116
9399627f 12117 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
12118 /*
12119 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12120 * the only slow ring.
12121 */
12122 status = (work_ha_copy &
12123 (HA_RXMASK << (4*LPFC_ELS_RING)));
12124 status >>= (4*LPFC_ELS_RING);
12125 if (status & HA_RXMASK) {
5b75da2f 12126 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12127 if (lpfc_readl(phba->HCregaddr, &control))
12128 goto unplug_error;
a58cbd52
JS
12129
12130 lpfc_debugfs_slow_ring_trc(phba,
12131 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12132 control, status,
12133 (uint32_t)phba->sli.slistat.sli_intr);
12134
858c9f6c 12135 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
12136 lpfc_debugfs_slow_ring_trc(phba,
12137 "ISR Disable ring:"
12138 "pwork:x%x hawork:x%x wait:x%x",
12139 phba->work_ha, work_ha_copy,
12140 (uint32_t)((unsigned long)
5e9d9b82 12141 &phba->work_waitq));
a58cbd52 12142
858c9f6c
JS
12143 control &=
12144 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
12145 writel(control, phba->HCregaddr);
12146 readl(phba->HCregaddr); /* flush */
dea3101e 12147 }
a58cbd52
JS
12148 else {
12149 lpfc_debugfs_slow_ring_trc(phba,
12150 "ISR slow ring: pwork:"
12151 "x%x hawork:x%x wait:x%x",
12152 phba->work_ha, work_ha_copy,
12153 (uint32_t)((unsigned long)
5e9d9b82 12154 &phba->work_waitq));
a58cbd52 12155 }
5b75da2f 12156 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12157 }
12158 }
5b75da2f 12159 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 12160 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
12161 if (lpfc_sli_read_hs(phba))
12162 goto unplug_error;
a257bf90
JS
12163 /*
12164 * Check if there is a deferred error condition
12165 * is active
12166 */
12167 if ((HS_FFER1 & phba->work_hs) &&
12168 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
12169 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12170 phba->work_hs)) {
a257bf90
JS
12171 phba->hba_flag |= DEFER_ERATT;
12172 /* Clear all interrupt enable conditions */
12173 writel(0, phba->HCregaddr);
12174 readl(phba->HCregaddr);
12175 }
12176 }
12177
9399627f 12178 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 12179 pmb = phba->sli.mbox_active;
04c68496 12180 pmbox = &pmb->u.mb;
34b02dcd 12181 mbox = phba->mbox;
858c9f6c 12182 vport = pmb->vport;
92d7f7b0
JS
12183
12184 /* First check out the status word */
12185 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12186 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 12187 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
12188 /*
12189 * Stray Mailbox Interrupt, mbxCommand <cmd>
12190 * mbxStatus <status>
12191 */
09372820 12192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 12193 LOG_SLI,
e8b62011 12194 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
12195 "Interrupt mbxCommand x%x "
12196 "mbxStatus x%x\n",
e8b62011 12197 (vport ? vport->vpi : 0),
92d7f7b0
JS
12198 pmbox->mbxCommand,
12199 pmbox->mbxStatus);
09372820
JS
12200 /* clear mailbox attention bit */
12201 work_ha_copy &= ~HA_MBATT;
12202 } else {
97eab634 12203 phba->sli.mbox_active = NULL;
5b75da2f 12204 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
12205 phba->last_completion_time = jiffies;
12206 del_timer(&phba->sli.mbox_tmo);
09372820
JS
12207 if (pmb->mbox_cmpl) {
12208 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12209 MAILBOX_CMD_SIZE);
7a470277
JS
12210 if (pmb->out_ext_byte_len &&
12211 pmb->context2)
12212 lpfc_sli_pcimem_bcopy(
12213 phba->mbox_ext,
12214 pmb->context2,
12215 pmb->out_ext_byte_len);
09372820
JS
12216 }
12217 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12218 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12219
12220 lpfc_debugfs_disc_trc(vport,
12221 LPFC_DISC_TRC_MBOX_VPORT,
12222 "MBOX dflt rpi: : "
12223 "status:x%x rpi:x%x",
12224 (uint32_t)pmbox->mbxStatus,
12225 pmbox->un.varWords[0], 0);
12226
12227 if (!pmbox->mbxStatus) {
12228 mp = (struct lpfc_dmabuf *)
12229 (pmb->context1);
12230 ndlp = (struct lpfc_nodelist *)
12231 pmb->context2;
12232
12233 /* Reg_LOGIN of dflt RPI was
12234 * successful. new lets get
12235 * rid of the RPI using the
12236 * same mbox buffer.
12237 */
12238 lpfc_unreg_login(phba,
12239 vport->vpi,
12240 pmbox->un.varWords[0],
12241 pmb);
12242 pmb->mbox_cmpl =
12243 lpfc_mbx_cmpl_dflt_rpi;
12244 pmb->context1 = mp;
12245 pmb->context2 = ndlp;
12246 pmb->vport = vport;
58da1ffb
JS
12247 rc = lpfc_sli_issue_mbox(phba,
12248 pmb,
12249 MBX_NOWAIT);
12250 if (rc != MBX_BUSY)
12251 lpfc_printf_log(phba,
12252 KERN_ERR,
12253 LOG_MBOX | LOG_SLI,
d7c255b2 12254 "0350 rc should have"
6a9c52cf 12255 "been MBX_BUSY\n");
3772a991
JS
12256 if (rc != MBX_NOT_FINISHED)
12257 goto send_current_mbox;
09372820 12258 }
858c9f6c 12259 }
5b75da2f
JS
12260 spin_lock_irqsave(
12261 &phba->pport->work_port_lock,
12262 iflag);
09372820
JS
12263 phba->pport->work_port_events &=
12264 ~WORKER_MBOX_TMO;
5b75da2f
JS
12265 spin_unlock_irqrestore(
12266 &phba->pport->work_port_lock,
12267 iflag);
09372820 12268 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12269 }
97eab634 12270 } else
5b75da2f 12271 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12272
92d7f7b0
JS
12273 if ((work_ha_copy & HA_MBATT) &&
12274 (phba->sli.mbox_active == NULL)) {
858c9f6c 12275send_current_mbox:
92d7f7b0 12276 /* Process next mailbox command if there is one */
58da1ffb
JS
12277 do {
12278 rc = lpfc_sli_issue_mbox(phba, NULL,
12279 MBX_NOWAIT);
12280 } while (rc == MBX_NOT_FINISHED);
12281 if (rc != MBX_SUCCESS)
12282 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12283 LOG_SLI, "0349 rc should be "
6a9c52cf 12284 "MBX_SUCCESS\n");
92d7f7b0
JS
12285 }
12286
5b75da2f 12287 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12288 phba->work_ha |= work_ha_copy;
5b75da2f 12289 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12290 lpfc_worker_wake_up(phba);
dea3101e 12291 }
9399627f 12292 return IRQ_HANDLED;
9940b97b
JS
12293unplug_error:
12294 spin_unlock_irqrestore(&phba->hbalock, iflag);
12295 return IRQ_HANDLED;
dea3101e 12296
3772a991 12297} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12298
12299/**
3772a991 12300 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12301 * @irq: Interrupt number.
12302 * @dev_id: The device context pointer.
12303 *
12304 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12305 * service routine when device with SLI-3 interface spec is enabled with
12306 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12307 * ring event in the HBA. However, when the device is enabled with either
12308 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12309 * device-level interrupt handler. When the PCI slot is in error recovery
12310 * or the HBA is undergoing initialization, the interrupt handler will not
12311 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12312 * the intrrupt context. This function is called without any lock held.
12313 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12314 *
12315 * This function returns IRQ_HANDLED when interrupt is handled else it
12316 * returns IRQ_NONE.
12317 **/
12318irqreturn_t
3772a991 12319lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12320{
12321 struct lpfc_hba *phba;
12322 uint32_t ha_copy;
12323 unsigned long status;
5b75da2f 12324 unsigned long iflag;
895427bd 12325 struct lpfc_sli_ring *pring;
9399627f
JS
12326
12327 /* Get the driver's phba structure from the dev_id and
12328 * assume the HBA is not interrupting.
12329 */
12330 phba = (struct lpfc_hba *) dev_id;
12331
12332 if (unlikely(!phba))
12333 return IRQ_NONE;
12334
12335 /*
12336 * Stuff needs to be attented to when this function is invoked as an
12337 * individual interrupt handler in MSI-X multi-message interrupt mode
12338 */
12339 if (phba->intr_type == MSIX) {
3772a991
JS
12340 /* Check device state for handling interrupt */
12341 if (lpfc_intr_state_check(phba))
9399627f
JS
12342 return IRQ_NONE;
12343 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12344 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12345 return IRQ_HANDLED;
9399627f 12346 /* Clear up only attention source related to fast-path */
5b75da2f 12347 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12348 /*
12349 * If there is deferred error attention, do not check for
12350 * any interrupt.
12351 */
12352 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12353 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12354 return IRQ_NONE;
12355 }
9399627f
JS
12356 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12357 phba->HAregaddr);
12358 readl(phba->HAregaddr); /* flush */
5b75da2f 12359 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12360 } else
12361 ha_copy = phba->ha_copy;
dea3101e
JB
12362
12363 /*
9399627f 12364 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12365 */
9399627f
JS
12366 ha_copy &= ~(phba->work_ha_mask);
12367
12368 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12369 status >>= (4*LPFC_FCP_RING);
895427bd 12370 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12371 if (status & HA_RXMASK)
895427bd 12372 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12373
12374 if (phba->cfg_multi_ring_support == 2) {
12375 /*
9399627f
JS
12376 * Process all events on extra ring. Take the optimized path
12377 * for extra ring IO.
a4bc3379 12378 */
9399627f 12379 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12380 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12381 if (status & HA_RXMASK) {
a4bc3379 12382 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12383 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12384 status);
12385 }
12386 }
dea3101e 12387 return IRQ_HANDLED;
3772a991 12388} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12389
12390/**
3772a991 12391 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12392 * @irq: Interrupt number.
12393 * @dev_id: The device context pointer.
12394 *
3772a991
JS
12395 * This function is the HBA device-level interrupt handler to device with
12396 * SLI-3 interface spec, called from the PCI layer when either MSI or
12397 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12398 * requires driver attention. This function invokes the slow-path interrupt
12399 * attention handling function and fast-path interrupt attention handling
12400 * function in turn to process the relevant HBA attention events. This
12401 * function is called without any lock held. It gets the hbalock to access
12402 * and update SLI data structures.
9399627f
JS
12403 *
12404 * This function returns IRQ_HANDLED when interrupt is handled, else it
12405 * returns IRQ_NONE.
12406 **/
12407irqreturn_t
3772a991 12408lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12409{
12410 struct lpfc_hba *phba;
12411 irqreturn_t sp_irq_rc, fp_irq_rc;
12412 unsigned long status1, status2;
a747c9ce 12413 uint32_t hc_copy;
9399627f
JS
12414
12415 /*
12416 * Get the driver's phba structure from the dev_id and
12417 * assume the HBA is not interrupting.
12418 */
12419 phba = (struct lpfc_hba *) dev_id;
12420
12421 if (unlikely(!phba))
12422 return IRQ_NONE;
12423
3772a991
JS
12424 /* Check device state for handling interrupt */
12425 if (lpfc_intr_state_check(phba))
9399627f
JS
12426 return IRQ_NONE;
12427
12428 spin_lock(&phba->hbalock);
9940b97b
JS
12429 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12430 spin_unlock(&phba->hbalock);
12431 return IRQ_HANDLED;
12432 }
12433
9399627f
JS
12434 if (unlikely(!phba->ha_copy)) {
12435 spin_unlock(&phba->hbalock);
12436 return IRQ_NONE;
12437 } else if (phba->ha_copy & HA_ERATT) {
12438 if (phba->hba_flag & HBA_ERATT_HANDLED)
12439 /* ERATT polling has handled ERATT */
12440 phba->ha_copy &= ~HA_ERATT;
12441 else
12442 /* Indicate interrupt handler handles ERATT */
12443 phba->hba_flag |= HBA_ERATT_HANDLED;
12444 }
12445
a257bf90
JS
12446 /*
12447 * If there is deferred error attention, do not check for any interrupt.
12448 */
12449 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12450 spin_unlock(&phba->hbalock);
a257bf90
JS
12451 return IRQ_NONE;
12452 }
12453
9399627f 12454 /* Clear attention sources except link and error attentions */
9940b97b
JS
12455 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12456 spin_unlock(&phba->hbalock);
12457 return IRQ_HANDLED;
12458 }
a747c9ce
JS
12459 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12460 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12461 phba->HCregaddr);
9399627f 12462 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12463 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12464 readl(phba->HAregaddr); /* flush */
12465 spin_unlock(&phba->hbalock);
12466
12467 /*
12468 * Invokes slow-path host attention interrupt handling as appropriate.
12469 */
12470
12471 /* status of events with mailbox and link attention */
12472 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12473
12474 /* status of events with ELS ring */
12475 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12476 status2 >>= (4*LPFC_ELS_RING);
12477
12478 if (status1 || (status2 & HA_RXMASK))
3772a991 12479 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12480 else
12481 sp_irq_rc = IRQ_NONE;
12482
12483 /*
12484 * Invoke fast-path host attention interrupt handling as appropriate.
12485 */
12486
12487 /* status of events with FCP ring */
12488 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12489 status1 >>= (4*LPFC_FCP_RING);
12490
12491 /* status of events with extra ring */
12492 if (phba->cfg_multi_ring_support == 2) {
12493 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12494 status2 >>= (4*LPFC_EXTRA_RING);
12495 } else
12496 status2 = 0;
12497
12498 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12499 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12500 else
12501 fp_irq_rc = IRQ_NONE;
dea3101e 12502
9399627f
JS
12503 /* Return device-level interrupt handling status */
12504 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12505} /* lpfc_sli_intr_handler */
4f774513
JS
12506
12507/**
12508 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12509 * @phba: pointer to lpfc hba data structure.
12510 *
12511 * This routine is invoked by the worker thread to process all the pending
12512 * SLI4 FCP abort XRI events.
12513 **/
12514void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12515{
12516 struct lpfc_cq_event *cq_event;
12517
12518 /* First, declare the fcp xri abort event has been handled */
12519 spin_lock_irq(&phba->hbalock);
12520 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12521 spin_unlock_irq(&phba->hbalock);
12522 /* Now, handle all the fcp xri abort events */
12523 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12524 /* Get the first event from the head of the event queue */
12525 spin_lock_irq(&phba->hbalock);
12526 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12527 cq_event, struct lpfc_cq_event, list);
12528 spin_unlock_irq(&phba->hbalock);
12529 /* Notify aborted XRI for FCP work queue */
12530 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12531 /* Free the event processed back to the free pool */
12532 lpfc_sli4_cq_event_release(phba, cq_event);
12533 }
12534}
12535
12536/**
12537 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12538 * @phba: pointer to lpfc hba data structure.
12539 *
12540 * This routine is invoked by the worker thread to process all the pending
12541 * SLI4 els abort xri events.
12542 **/
12543void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12544{
12545 struct lpfc_cq_event *cq_event;
12546
12547 /* First, declare the els xri abort event has been handled */
12548 spin_lock_irq(&phba->hbalock);
12549 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12550 spin_unlock_irq(&phba->hbalock);
12551 /* Now, handle all the els xri abort events */
12552 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12553 /* Get the first event from the head of the event queue */
12554 spin_lock_irq(&phba->hbalock);
12555 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12556 cq_event, struct lpfc_cq_event, list);
12557 spin_unlock_irq(&phba->hbalock);
12558 /* Notify aborted XRI for ELS work queue */
12559 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12560 /* Free the event processed back to the free pool */
12561 lpfc_sli4_cq_event_release(phba, cq_event);
12562 }
12563}
12564
341af102
JS
12565/**
12566 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12567 * @phba: pointer to lpfc hba data structure
12568 * @pIocbIn: pointer to the rspiocbq
12569 * @pIocbOut: pointer to the cmdiocbq
12570 * @wcqe: pointer to the complete wcqe
12571 *
12572 * This routine transfers the fields of a command iocbq to a response iocbq
12573 * by copying all the IOCB fields from command iocbq and transferring the
12574 * completion status information from the complete wcqe.
12575 **/
4f774513 12576static void
341af102
JS
12577lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12578 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12579 struct lpfc_iocbq *pIocbOut,
12580 struct lpfc_wcqe_complete *wcqe)
12581{
af22741c 12582 int numBdes, i;
341af102 12583 unsigned long iflags;
af22741c
JS
12584 uint32_t status, max_response;
12585 struct lpfc_dmabuf *dmabuf;
12586 struct ulp_bde64 *bpl, bde;
4f774513
JS
12587 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12588
12589 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12590 sizeof(struct lpfc_iocbq) - offset);
4f774513 12591 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12592 status = bf_get(lpfc_wcqe_c_status, wcqe);
12593 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12594 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12595 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12596 pIocbIn->iocb.un.fcpi.fcpi_parm =
12597 pIocbOut->iocb.un.fcpi.fcpi_parm -
12598 wcqe->total_data_placed;
12599 else
12600 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12601 else {
4f774513 12602 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12603 switch (pIocbOut->iocb.ulpCommand) {
12604 case CMD_ELS_REQUEST64_CR:
12605 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12606 bpl = (struct ulp_bde64 *)dmabuf->virt;
12607 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12608 max_response = bde.tus.f.bdeSize;
12609 break;
12610 case CMD_GEN_REQUEST64_CR:
12611 max_response = 0;
12612 if (!pIocbOut->context3)
12613 break;
12614 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12615 sizeof(struct ulp_bde64);
12616 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12617 bpl = (struct ulp_bde64 *)dmabuf->virt;
12618 for (i = 0; i < numBdes; i++) {
12619 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12620 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12621 max_response += bde.tus.f.bdeSize;
12622 }
12623 break;
12624 default:
12625 max_response = wcqe->total_data_placed;
12626 break;
12627 }
12628 if (max_response < wcqe->total_data_placed)
12629 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12630 else
12631 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12632 wcqe->total_data_placed;
695a814e 12633 }
341af102 12634
acd6859b
JS
12635 /* Convert BG errors for completion status */
12636 if (status == CQE_STATUS_DI_ERROR) {
12637 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12638
12639 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12640 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12641 else
12642 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12643
12644 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12645 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12646 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12647 BGS_GUARD_ERR_MASK;
12648 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12649 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12650 BGS_APPTAG_ERR_MASK;
12651 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12652 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12653 BGS_REFTAG_ERR_MASK;
12654
12655 /* Check to see if there was any good data before the error */
12656 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12657 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12658 BGS_HI_WATER_MARK_PRESENT_MASK;
12659 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12660 wcqe->total_data_placed;
12661 }
12662
12663 /*
12664 * Set ALL the error bits to indicate we don't know what
12665 * type of error it is.
12666 */
12667 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12668 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12669 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12670 BGS_GUARD_ERR_MASK);
12671 }
12672
341af102
JS
12673 /* Pick up HBA exchange busy condition */
12674 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12675 spin_lock_irqsave(&phba->hbalock, iflags);
12676 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12677 spin_unlock_irqrestore(&phba->hbalock, iflags);
12678 }
4f774513
JS
12679}
12680
45ed1190
JS
12681/**
12682 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12683 * @phba: Pointer to HBA context object.
12684 * @wcqe: Pointer to work-queue completion queue entry.
12685 *
12686 * This routine handles an ELS work-queue completion event and construct
12687 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12688 * discovery engine to handle.
12689 *
12690 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12691 **/
12692static struct lpfc_iocbq *
12693lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12694 struct lpfc_iocbq *irspiocbq)
12695{
895427bd 12696 struct lpfc_sli_ring *pring;
45ed1190
JS
12697 struct lpfc_iocbq *cmdiocbq;
12698 struct lpfc_wcqe_complete *wcqe;
12699 unsigned long iflags;
12700
895427bd 12701 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12702 if (unlikely(!pring))
12703 return NULL;
895427bd 12704
45ed1190 12705 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12706 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12707 pring->stats.iocb_event++;
12708 /* Look up the ELS command IOCB and create pseudo response IOCB */
12709 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12710 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12711 if (unlikely(!cmdiocbq)) {
401bb416 12712 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12713 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12714 "0386 ELS complete with no corresponding "
401bb416
DK
12715 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12716 wcqe->word0, wcqe->total_data_placed,
12717 wcqe->parameter, wcqe->word3);
45ed1190
JS
12718 lpfc_sli_release_iocbq(phba, irspiocbq);
12719 return NULL;
12720 }
12721
401bb416
DK
12722 /* Put the iocb back on the txcmplq */
12723 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12724 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12725
45ed1190 12726 /* Fake the irspiocbq and copy necessary response information */
341af102 12727 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12728
12729 return irspiocbq;
12730}
12731
8a5ca109
JS
12732inline struct lpfc_cq_event *
12733lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12734{
12735 struct lpfc_cq_event *cq_event;
12736
12737 /* Allocate a new internal CQ_EVENT entry */
12738 cq_event = lpfc_sli4_cq_event_alloc(phba);
12739 if (!cq_event) {
12740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12741 "0602 Failed to alloc CQ_EVENT entry\n");
12742 return NULL;
12743 }
12744
12745 /* Move the CQE into the event */
12746 memcpy(&cq_event->cqe, entry, size);
12747 return cq_event;
12748}
12749
04c68496
JS
12750/**
12751 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12752 * @phba: Pointer to HBA context object.
12753 * @cqe: Pointer to mailbox completion queue entry.
12754 *
12755 * This routine process a mailbox completion queue entry with asynchrous
12756 * event.
12757 *
12758 * Return: true if work posted to worker thread, otherwise false.
12759 **/
12760static bool
12761lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12762{
12763 struct lpfc_cq_event *cq_event;
12764 unsigned long iflags;
12765
12766 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12767 "0392 Async Event: word0:x%x, word1:x%x, "
12768 "word2:x%x, word3:x%x\n", mcqe->word0,
12769 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12770
8a5ca109
JS
12771 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12772 if (!cq_event)
04c68496 12773 return false;
04c68496
JS
12774 spin_lock_irqsave(&phba->hbalock, iflags);
12775 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12776 /* Set the async event flag */
12777 phba->hba_flag |= ASYNC_EVENT;
12778 spin_unlock_irqrestore(&phba->hbalock, iflags);
12779
12780 return true;
12781}
12782
12783/**
12784 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12785 * @phba: Pointer to HBA context object.
12786 * @cqe: Pointer to mailbox completion queue entry.
12787 *
12788 * This routine process a mailbox completion queue entry with mailbox
12789 * completion event.
12790 *
12791 * Return: true if work posted to worker thread, otherwise false.
12792 **/
12793static bool
12794lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12795{
12796 uint32_t mcqe_status;
12797 MAILBOX_t *mbox, *pmbox;
12798 struct lpfc_mqe *mqe;
12799 struct lpfc_vport *vport;
12800 struct lpfc_nodelist *ndlp;
12801 struct lpfc_dmabuf *mp;
12802 unsigned long iflags;
12803 LPFC_MBOXQ_t *pmb;
12804 bool workposted = false;
12805 int rc;
12806
12807 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12808 if (!bf_get(lpfc_trailer_completed, mcqe))
12809 goto out_no_mqe_complete;
12810
12811 /* Get the reference to the active mbox command */
12812 spin_lock_irqsave(&phba->hbalock, iflags);
12813 pmb = phba->sli.mbox_active;
12814 if (unlikely(!pmb)) {
12815 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12816 "1832 No pending MBOX command to handle\n");
12817 spin_unlock_irqrestore(&phba->hbalock, iflags);
12818 goto out_no_mqe_complete;
12819 }
12820 spin_unlock_irqrestore(&phba->hbalock, iflags);
12821 mqe = &pmb->u.mqe;
12822 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12823 mbox = phba->mbox;
12824 vport = pmb->vport;
12825
12826 /* Reset heartbeat timer */
12827 phba->last_completion_time = jiffies;
12828 del_timer(&phba->sli.mbox_tmo);
12829
12830 /* Move mbox data to caller's mailbox region, do endian swapping */
12831 if (pmb->mbox_cmpl && mbox)
12832 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12833
73d91e50
JS
12834 /*
12835 * For mcqe errors, conditionally move a modified error code to
12836 * the mbox so that the error will not be missed.
12837 */
12838 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12839 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12840 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12841 bf_set(lpfc_mqe_status, mqe,
12842 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12843 }
04c68496
JS
12844 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12845 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12846 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12847 "MBOX dflt rpi: status:x%x rpi:x%x",
12848 mcqe_status,
12849 pmbox->un.varWords[0], 0);
12850 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12851 mp = (struct lpfc_dmabuf *)(pmb->context1);
12852 ndlp = (struct lpfc_nodelist *)pmb->context2;
12853 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12854 * RID of the PPI using the same mbox buffer.
12855 */
12856 lpfc_unreg_login(phba, vport->vpi,
12857 pmbox->un.varWords[0], pmb);
12858 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12859 pmb->context1 = mp;
12860 pmb->context2 = ndlp;
12861 pmb->vport = vport;
12862 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12863 if (rc != MBX_BUSY)
12864 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12865 LOG_SLI, "0385 rc should "
12866 "have been MBX_BUSY\n");
12867 if (rc != MBX_NOT_FINISHED)
12868 goto send_current_mbox;
12869 }
12870 }
12871 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12872 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12873 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12874
12875 /* There is mailbox completion work to do */
12876 spin_lock_irqsave(&phba->hbalock, iflags);
12877 __lpfc_mbox_cmpl_put(phba, pmb);
12878 phba->work_ha |= HA_MBATT;
12879 spin_unlock_irqrestore(&phba->hbalock, iflags);
12880 workposted = true;
12881
12882send_current_mbox:
12883 spin_lock_irqsave(&phba->hbalock, iflags);
12884 /* Release the mailbox command posting token */
12885 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12886 /* Setting active mailbox pointer need to be in sync to flag clear */
12887 phba->sli.mbox_active = NULL;
12888 spin_unlock_irqrestore(&phba->hbalock, iflags);
12889 /* Wake up worker thread to post the next pending mailbox command */
12890 lpfc_worker_wake_up(phba);
12891out_no_mqe_complete:
12892 if (bf_get(lpfc_trailer_consumed, mcqe))
12893 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12894 return workposted;
12895}
12896
12897/**
12898 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12899 * @phba: Pointer to HBA context object.
12900 * @cqe: Pointer to mailbox completion queue entry.
12901 *
12902 * This routine process a mailbox completion queue entry, it invokes the
12903 * proper mailbox complete handling or asynchrous event handling routine
12904 * according to the MCQE's async bit.
12905 *
12906 * Return: true if work posted to worker thread, otherwise false.
12907 **/
12908static bool
12909lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12910{
12911 struct lpfc_mcqe mcqe;
12912 bool workposted;
12913
12914 /* Copy the mailbox MCQE and convert endian order as needed */
12915 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12916
12917 /* Invoke the proper event handling routine */
12918 if (!bf_get(lpfc_trailer_async, &mcqe))
12919 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12920 else
12921 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12922 return workposted;
12923}
12924
4f774513
JS
12925/**
12926 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12927 * @phba: Pointer to HBA context object.
2a76a283 12928 * @cq: Pointer to associated CQ
4f774513
JS
12929 * @wcqe: Pointer to work-queue completion queue entry.
12930 *
12931 * This routine handles an ELS work-queue completion event.
12932 *
12933 * Return: true if work posted to worker thread, otherwise false.
12934 **/
12935static bool
2a76a283 12936lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12937 struct lpfc_wcqe_complete *wcqe)
12938{
4f774513
JS
12939 struct lpfc_iocbq *irspiocbq;
12940 unsigned long iflags;
2a76a283 12941 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12942 int txq_cnt = 0;
12943 int txcmplq_cnt = 0;
12944 int fcp_txcmplq_cnt = 0;
4f774513 12945
45ed1190 12946 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12947 irspiocbq = lpfc_sli_get_iocbq(phba);
12948 if (!irspiocbq) {
0e9bb8d7
JS
12949 if (!list_empty(&pring->txq))
12950 txq_cnt++;
12951 if (!list_empty(&pring->txcmplq))
12952 txcmplq_cnt++;
4f774513 12953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12954 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12955 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12956 txq_cnt, phba->iocb_cnt,
12957 fcp_txcmplq_cnt,
12958 txcmplq_cnt);
45ed1190 12959 return false;
4f774513 12960 }
4f774513 12961
45ed1190
JS
12962 /* Save off the slow-path queue event for work thread to process */
12963 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12964 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12965 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12966 &phba->sli4_hba.sp_queue_event);
12967 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12968 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12969
45ed1190 12970 return true;
4f774513
JS
12971}
12972
12973/**
12974 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12975 * @phba: Pointer to HBA context object.
12976 * @wcqe: Pointer to work-queue completion queue entry.
12977 *
3f8b6fb7 12978 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
12979 * proper WQ release routine to the slow-path WQ.
12980 **/
12981static void
12982lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12983 struct lpfc_wcqe_release *wcqe)
12984{
2e90f4b5
JS
12985 /* sanity check on queue memory */
12986 if (unlikely(!phba->sli4_hba.els_wq))
12987 return;
4f774513
JS
12988 /* Check for the slow-path ELS work queue */
12989 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12990 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12991 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12992 else
12993 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12994 "2579 Slow-path wqe consume event carries "
12995 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12996 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12997 phba->sli4_hba.els_wq->queue_id);
12998}
12999
13000/**
13001 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13002 * @phba: Pointer to HBA context object.
13003 * @cq: Pointer to a WQ completion queue.
13004 * @wcqe: Pointer to work-queue completion queue entry.
13005 *
13006 * This routine handles an XRI abort event.
13007 *
13008 * Return: true if work posted to worker thread, otherwise false.
13009 **/
13010static bool
13011lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13012 struct lpfc_queue *cq,
13013 struct sli4_wcqe_xri_aborted *wcqe)
13014{
13015 bool workposted = false;
13016 struct lpfc_cq_event *cq_event;
13017 unsigned long iflags;
13018
4f774513
JS
13019 switch (cq->subtype) {
13020 case LPFC_FCP:
8a5ca109
JS
13021 cq_event = lpfc_cq_event_setup(
13022 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13023 if (!cq_event)
13024 return false;
4f774513
JS
13025 spin_lock_irqsave(&phba->hbalock, iflags);
13026 list_add_tail(&cq_event->list,
13027 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13028 /* Set the fcp xri abort event flag */
13029 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13030 spin_unlock_irqrestore(&phba->hbalock, iflags);
13031 workposted = true;
13032 break;
422c4cb7 13033 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 13034 case LPFC_ELS:
8a5ca109
JS
13035 cq_event = lpfc_cq_event_setup(
13036 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13037 if (!cq_event)
13038 return false;
4f774513
JS
13039 spin_lock_irqsave(&phba->hbalock, iflags);
13040 list_add_tail(&cq_event->list,
13041 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13042 /* Set the els xri abort event flag */
13043 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13044 spin_unlock_irqrestore(&phba->hbalock, iflags);
13045 workposted = true;
13046 break;
318083ad 13047 case LPFC_NVME:
8a5ca109
JS
13048 /* Notify aborted XRI for NVME work queue */
13049 if (phba->nvmet_support)
13050 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13051 else
13052 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13053
13054 workposted = false;
318083ad 13055 break;
4f774513
JS
13056 default:
13057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
13058 "0603 Invalid CQ subtype %d: "
13059 "%08x %08x %08x %08x\n",
13060 cq->subtype, wcqe->word0, wcqe->parameter,
13061 wcqe->word2, wcqe->word3);
4f774513
JS
13062 workposted = false;
13063 break;
13064 }
13065 return workposted;
13066}
13067
4f774513
JS
13068/**
13069 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13070 * @phba: Pointer to HBA context object.
13071 * @rcqe: Pointer to receive-queue completion queue entry.
13072 *
13073 * This routine process a receive-queue completion queue entry.
13074 *
13075 * Return: true if work posted to worker thread, otherwise false.
13076 **/
13077static bool
4d9ab994 13078lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 13079{
4f774513 13080 bool workposted = false;
895427bd 13081 struct fc_frame_header *fc_hdr;
4f774513
JS
13082 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13083 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 13084 struct lpfc_nvmet_tgtport *tgtp;
4f774513 13085 struct hbq_dmabuf *dma_buf;
7851fe2c 13086 uint32_t status, rq_id;
4f774513
JS
13087 unsigned long iflags;
13088
2e90f4b5
JS
13089 /* sanity check on queue memory */
13090 if (unlikely(!hrq) || unlikely(!drq))
13091 return workposted;
13092
7851fe2c
JS
13093 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13094 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13095 else
13096 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13097 if (rq_id != hrq->queue_id)
4f774513
JS
13098 goto out;
13099
4d9ab994 13100 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
13101 switch (status) {
13102 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13104 "2537 Receive Frame Truncated!!\n");
13105 case FC_STATUS_RQ_SUCCESS:
13106 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13107 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
13108 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13109 if (!dma_buf) {
b84daac9 13110 hrq->RQ_no_buf_found++;
4f774513
JS
13111 spin_unlock_irqrestore(&phba->hbalock, iflags);
13112 goto out;
13113 }
b84daac9 13114 hrq->RQ_rcv_buf++;
547077a4 13115 hrq->RQ_buf_posted--;
4d9ab994 13116 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
13117
13118 /* If a NVME LS event (type 0x28), treat it as Fast path */
13119 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13120
4f774513 13121 /* save off the frame for the word thread to process */
4d9ab994 13122 list_add_tail(&dma_buf->cq_event.list,
45ed1190 13123 &phba->sli4_hba.sp_queue_event);
4f774513 13124 /* Frame received */
45ed1190 13125 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
13126 spin_unlock_irqrestore(&phba->hbalock, iflags);
13127 workposted = true;
13128 break;
4f774513 13129 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13130 if (phba->nvmet_support) {
13131 tgtp = phba->targetport->private;
13132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13133 "6402 RQE Error x%x, posted %d err_cnt "
13134 "%d: %x %x %x\n",
13135 status, hrq->RQ_buf_posted,
13136 hrq->RQ_no_posted_buf,
13137 atomic_read(&tgtp->rcv_fcp_cmd_in),
13138 atomic_read(&tgtp->rcv_fcp_cmd_out),
13139 atomic_read(&tgtp->xmt_fcp_release));
13140 }
13141 /* fallthrough */
13142
13143 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 13144 hrq->RQ_no_posted_buf++;
4f774513
JS
13145 /* Post more buffers if possible */
13146 spin_lock_irqsave(&phba->hbalock, iflags);
13147 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13148 spin_unlock_irqrestore(&phba->hbalock, iflags);
13149 workposted = true;
13150 break;
13151 }
13152out:
13153 return workposted;
4f774513
JS
13154}
13155
4d9ab994
JS
13156/**
13157 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13158 * @phba: Pointer to HBA context object.
13159 * @cq: Pointer to the completion queue.
13160 * @wcqe: Pointer to a completion queue entry.
13161 *
25985edc 13162 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
13163 * entry.
13164 *
13165 * Return: true if work posted to worker thread, otherwise false.
13166 **/
13167static bool
13168lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13169 struct lpfc_cqe *cqe)
13170{
45ed1190 13171 struct lpfc_cqe cqevt;
4d9ab994
JS
13172 bool workposted = false;
13173
13174 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 13175 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
13176
13177 /* Check and process for different type of WCQE and dispatch */
45ed1190 13178 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 13179 case CQE_CODE_COMPL_WQE:
45ed1190 13180 /* Process the WQ/RQ complete event */
bc73905a 13181 phba->last_completion_time = jiffies;
2a76a283 13182 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 13183 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
13184 break;
13185 case CQE_CODE_RELEASE_WQE:
13186 /* Process the WQ release event */
13187 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 13188 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
13189 break;
13190 case CQE_CODE_XRI_ABORTED:
13191 /* Process the WQ XRI abort event */
bc73905a 13192 phba->last_completion_time = jiffies;
4d9ab994 13193 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 13194 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
13195 break;
13196 case CQE_CODE_RECEIVE:
7851fe2c 13197 case CQE_CODE_RECEIVE_V1:
4d9ab994 13198 /* Process the RQ event */
bc73905a 13199 phba->last_completion_time = jiffies;
4d9ab994 13200 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 13201 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
13202 break;
13203 default:
13204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13205 "0388 Not a valid WCQE code: x%x\n",
45ed1190 13206 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
13207 break;
13208 }
13209 return workposted;
13210}
13211
4f774513
JS
13212/**
13213 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13214 * @phba: Pointer to HBA context object.
13215 * @eqe: Pointer to fast-path event queue entry.
13216 *
13217 * This routine process a event queue entry from the slow-path event queue.
13218 * It will check the MajorCode and MinorCode to determine this is for a
13219 * completion event on a completion queue, if not, an error shall be logged
13220 * and just return. Otherwise, it will get to the corresponding completion
13221 * queue and process all the entries on that completion queue, rearm the
13222 * completion queue, and then return.
13223 *
13224 **/
f485c18d 13225static void
67d12733
JS
13226lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13227 struct lpfc_queue *speq)
4f774513 13228{
67d12733 13229 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13230 uint16_t cqid;
13231
4f774513 13232 /* Get the reference to the corresponding CQ */
cb5172ea 13233 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13234
4f774513
JS
13235 list_for_each_entry(childq, &speq->child_list, list) {
13236 if (childq->queue_id == cqid) {
13237 cq = childq;
13238 break;
13239 }
13240 }
13241 if (unlikely(!cq)) {
75baf696
JS
13242 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13243 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13244 "0365 Slow-path CQ identifier "
13245 "(%d) does not exist\n", cqid);
f485c18d 13246 return;
4f774513
JS
13247 }
13248
895427bd
JS
13249 /* Save EQ associated with this CQ */
13250 cq->assoc_qp = speq;
13251
f485c18d
DK
13252 if (!queue_work(phba->wq, &cq->spwork))
13253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13254 "0390 Cannot schedule soft IRQ "
13255 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13256 cqid, cq->queue_id, smp_processor_id());
13257}
13258
13259/**
13260 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13261 * @phba: Pointer to HBA context object.
13262 *
13263 * This routine process a event queue entry from the slow-path event queue.
13264 * It will check the MajorCode and MinorCode to determine this is for a
13265 * completion event on a completion queue, if not, an error shall be logged
13266 * and just return. Otherwise, it will get to the corresponding completion
13267 * queue and process all the entries on that completion queue, rearm the
13268 * completion queue, and then return.
13269 *
13270 **/
13271static void
13272lpfc_sli4_sp_process_cq(struct work_struct *work)
13273{
13274 struct lpfc_queue *cq =
13275 container_of(work, struct lpfc_queue, spwork);
13276 struct lpfc_hba *phba = cq->phba;
13277 struct lpfc_cqe *cqe;
13278 bool workposted = false;
13279 int ccount = 0;
13280
4f774513
JS
13281 /* Process all the entries to the CQ */
13282 switch (cq->type) {
13283 case LPFC_MCQ:
13284 while ((cqe = lpfc_sli4_cq_get(cq))) {
13285 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13286 if (!(++ccount % cq->entry_repost))
7869da18 13287 break;
b84daac9 13288 cq->CQ_mbox++;
4f774513
JS
13289 }
13290 break;
13291 case LPFC_WCQ:
13292 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13293 if (cq->subtype == LPFC_FCP ||
13294 cq->subtype == LPFC_NVME) {
13295#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13296 if (phba->ktime_on)
13297 cq->isr_timestamp = ktime_get_ns();
13298 else
13299 cq->isr_timestamp = 0;
13300#endif
895427bd 13301 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13302 cqe);
c8a4ce0b 13303 } else {
0558056c
JS
13304 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13305 cqe);
c8a4ce0b 13306 }
f485c18d 13307 if (!(++ccount % cq->entry_repost))
7869da18 13308 break;
4f774513 13309 }
b84daac9
JS
13310
13311 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13312 if (ccount > cq->CQ_max_cqe)
13313 cq->CQ_max_cqe = ccount;
4f774513
JS
13314 break;
13315 default:
13316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13317 "0370 Invalid completion queue type (%d)\n",
13318 cq->type);
f485c18d 13319 return;
4f774513
JS
13320 }
13321
13322 /* Catch the no cq entry condition, log an error */
f485c18d 13323 if (unlikely(ccount == 0))
4f774513
JS
13324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13325 "0371 No entry from the CQ: identifier "
13326 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13327
13328 /* In any case, flash and re-arm the RCQ */
b71413dd 13329 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13330
13331 /* wake up worker thread if there are works to be done */
13332 if (workposted)
13333 lpfc_worker_wake_up(phba);
13334}
13335
13336/**
13337 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13338 * @phba: Pointer to HBA context object.
13339 * @cq: Pointer to associated CQ
13340 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13341 *
13342 * This routine process a fast-path work queue completion entry from fast-path
13343 * event queue for FCP command response completion.
13344 **/
13345static void
2a76a283 13346lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13347 struct lpfc_wcqe_complete *wcqe)
13348{
2a76a283 13349 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13350 struct lpfc_iocbq *cmdiocbq;
13351 struct lpfc_iocbq irspiocbq;
13352 unsigned long iflags;
13353
4f774513
JS
13354 /* Check for response status */
13355 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13356 /* If resource errors reported from HBA, reduce queue
13357 * depth of the SCSI device.
13358 */
e3d2b802
JS
13359 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13360 IOSTAT_LOCAL_REJECT)) &&
13361 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13362 IOERR_NO_RESOURCES))
4f774513 13363 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13364
4f774513
JS
13365 /* Log the error status */
13366 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13367 "0373 FCP complete error: status=x%x, "
13368 "hw_status=x%x, total_data_specified=%d, "
13369 "parameter=x%x, word3=x%x\n",
13370 bf_get(lpfc_wcqe_c_status, wcqe),
13371 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13372 wcqe->total_data_placed, wcqe->parameter,
13373 wcqe->word3);
13374 }
13375
13376 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13377 spin_lock_irqsave(&pring->ring_lock, iflags);
13378 pring->stats.iocb_event++;
4f774513
JS
13379 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13380 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13381 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13382 if (unlikely(!cmdiocbq)) {
13383 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13384 "0374 FCP complete with no corresponding "
13385 "cmdiocb: iotag (%d)\n",
13386 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13387 return;
13388 }
c8a4ce0b
DK
13389#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13390 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13391#endif
895427bd
JS
13392 if (cmdiocbq->iocb_cmpl == NULL) {
13393 if (cmdiocbq->wqe_cmpl) {
13394 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13395 spin_lock_irqsave(&phba->hbalock, iflags);
13396 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13397 spin_unlock_irqrestore(&phba->hbalock, iflags);
13398 }
13399
13400 /* Pass the cmd_iocb and the wcqe to the upper layer */
13401 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13402 return;
13403 }
4f774513
JS
13404 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13405 "0375 FCP cmdiocb not callback function "
13406 "iotag: (%d)\n",
13407 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13408 return;
13409 }
13410
13411 /* Fake the irspiocb and copy necessary response information */
341af102 13412 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13413
0f65ff68
JS
13414 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13415 spin_lock_irqsave(&phba->hbalock, iflags);
13416 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13417 spin_unlock_irqrestore(&phba->hbalock, iflags);
13418 }
13419
4f774513
JS
13420 /* Pass the cmd_iocb and the rsp state to the upper layer */
13421 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13422}
13423
13424/**
13425 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13426 * @phba: Pointer to HBA context object.
13427 * @cq: Pointer to completion queue.
13428 * @wcqe: Pointer to work-queue completion queue entry.
13429 *
3f8b6fb7 13430 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13431 * proper WQ release routine to the slow-path WQ.
13432 **/
13433static void
13434lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13435 struct lpfc_wcqe_release *wcqe)
13436{
13437 struct lpfc_queue *childwq;
13438 bool wqid_matched = false;
895427bd 13439 uint16_t hba_wqid;
4f774513
JS
13440
13441 /* Check for fast-path FCP work queue release */
895427bd 13442 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13443 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13444 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13445 lpfc_sli4_wq_release(childwq,
13446 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
6e8e1c14
JS
13447 if (childwq->q_flag & HBA_NVMET_WQFULL)
13448 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13449 wqid_matched = true;
13450 break;
13451 }
13452 }
13453 /* Report warning log message if no match found */
13454 if (wqid_matched != true)
13455 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13456 "2580 Fast-path wqe consume event carries "
895427bd 13457 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13458}
13459
13460/**
2d7dbc4c
JS
13461 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13462 * @phba: Pointer to HBA context object.
13463 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13464 *
2d7dbc4c
JS
13465 * This routine process a receive-queue completion queue entry.
13466 *
13467 * Return: true if work posted to worker thread, otherwise false.
13468 **/
13469static bool
13470lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13471 struct lpfc_rcqe *rcqe)
13472{
13473 bool workposted = false;
13474 struct lpfc_queue *hrq;
13475 struct lpfc_queue *drq;
13476 struct rqb_dmabuf *dma_buf;
13477 struct fc_frame_header *fc_hdr;
547077a4 13478 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13479 uint32_t status, rq_id;
13480 unsigned long iflags;
13481 uint32_t fctl, idx;
13482
13483 if ((phba->nvmet_support == 0) ||
13484 (phba->sli4_hba.nvmet_cqset == NULL))
13485 return workposted;
13486
13487 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13488 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13489 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13490
13491 /* sanity check on queue memory */
13492 if (unlikely(!hrq) || unlikely(!drq))
13493 return workposted;
13494
13495 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13496 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13497 else
13498 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13499
13500 if ((phba->nvmet_support == 0) ||
13501 (rq_id != hrq->queue_id))
13502 return workposted;
13503
13504 status = bf_get(lpfc_rcqe_status, rcqe);
13505 switch (status) {
13506 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13507 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13508 "6126 Receive Frame Truncated!!\n");
78e1d200 13509 /* Drop thru */
2d7dbc4c 13510 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13511 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13512 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13513 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13514 if (!dma_buf) {
13515 hrq->RQ_no_buf_found++;
13516 spin_unlock_irqrestore(&phba->hbalock, iflags);
13517 goto out;
13518 }
13519 spin_unlock_irqrestore(&phba->hbalock, iflags);
13520 hrq->RQ_rcv_buf++;
547077a4 13521 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13522 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13523
13524 /* Just some basic sanity checks on FCP Command frame */
13525 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13526 fc_hdr->fh_f_ctl[1] << 8 |
13527 fc_hdr->fh_f_ctl[2]);
13528 if (((fctl &
13529 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13530 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13531 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13532 goto drop;
13533
13534 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13535 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13536 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13537 phba, idx, dma_buf,
c8a4ce0b 13538 cq->isr_timestamp);
2d7dbc4c
JS
13539 return false;
13540 }
13541drop:
13542 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13543 break;
2d7dbc4c 13544 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13545 if (phba->nvmet_support) {
13546 tgtp = phba->targetport->private;
13547 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13548 "6401 RQE Error x%x, posted %d err_cnt "
13549 "%d: %x %x %x\n",
13550 status, hrq->RQ_buf_posted,
13551 hrq->RQ_no_posted_buf,
13552 atomic_read(&tgtp->rcv_fcp_cmd_in),
13553 atomic_read(&tgtp->rcv_fcp_cmd_out),
13554 atomic_read(&tgtp->xmt_fcp_release));
13555 }
13556 /* fallthrough */
13557
13558 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13559 hrq->RQ_no_posted_buf++;
13560 /* Post more buffers if possible */
2d7dbc4c
JS
13561 break;
13562 }
13563out:
13564 return workposted;
13565}
13566
4f774513 13567/**
895427bd 13568 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13569 * @cq: Pointer to the completion queue.
13570 * @eqe: Pointer to fast-path completion queue entry.
13571 *
13572 * This routine process a fast-path work queue completion entry from fast-path
13573 * event queue for FCP command response completion.
13574 **/
13575static int
895427bd 13576lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13577 struct lpfc_cqe *cqe)
13578{
13579 struct lpfc_wcqe_release wcqe;
13580 bool workposted = false;
13581
13582 /* Copy the work queue CQE and convert endian order if needed */
13583 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13584
13585 /* Check and process for different type of WCQE and dispatch */
13586 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13587 case CQE_CODE_COMPL_WQE:
895427bd 13588 case CQE_CODE_NVME_ERSP:
b84daac9 13589 cq->CQ_wq++;
4f774513 13590 /* Process the WQ complete event */
98fc5dd9 13591 phba->last_completion_time = jiffies;
895427bd
JS
13592 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13593 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13594 (struct lpfc_wcqe_complete *)&wcqe);
13595 if (cq->subtype == LPFC_NVME_LS)
13596 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13597 (struct lpfc_wcqe_complete *)&wcqe);
13598 break;
13599 case CQE_CODE_RELEASE_WQE:
b84daac9 13600 cq->CQ_release_wqe++;
4f774513
JS
13601 /* Process the WQ release event */
13602 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13603 (struct lpfc_wcqe_release *)&wcqe);
13604 break;
13605 case CQE_CODE_XRI_ABORTED:
b84daac9 13606 cq->CQ_xri_aborted++;
4f774513 13607 /* Process the WQ XRI abort event */
bc73905a 13608 phba->last_completion_time = jiffies;
4f774513
JS
13609 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13610 (struct sli4_wcqe_xri_aborted *)&wcqe);
13611 break;
895427bd
JS
13612 case CQE_CODE_RECEIVE_V1:
13613 case CQE_CODE_RECEIVE:
13614 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13615 if (cq->subtype == LPFC_NVMET) {
13616 workposted = lpfc_sli4_nvmet_handle_rcqe(
13617 phba, cq, (struct lpfc_rcqe *)&wcqe);
13618 }
895427bd 13619 break;
4f774513
JS
13620 default:
13621 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13622 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13623 bf_get(lpfc_wcqe_c_code, &wcqe));
13624 break;
13625 }
13626 return workposted;
13627}
13628
13629/**
67d12733 13630 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13631 * @phba: Pointer to HBA context object.
13632 * @eqe: Pointer to fast-path event queue entry.
13633 *
13634 * This routine process a event queue entry from the fast-path event queue.
13635 * It will check the MajorCode and MinorCode to determine this is for a
13636 * completion event on a completion queue, if not, an error shall be logged
13637 * and just return. Otherwise, it will get to the corresponding completion
13638 * queue and process all the entries on the completion queue, rearm the
13639 * completion queue, and then return.
13640 **/
f485c18d 13641static void
67d12733
JS
13642lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13643 uint32_t qidx)
4f774513 13644{
895427bd 13645 struct lpfc_queue *cq = NULL;
2d7dbc4c 13646 uint16_t cqid, id;
4f774513 13647
cb5172ea 13648 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13649 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13650 "0366 Not a valid completion "
4f774513 13651 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13652 bf_get_le32(lpfc_eqe_major_code, eqe),
13653 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13654 return;
4f774513
JS
13655 }
13656
67d12733
JS
13657 /* Get the reference to the corresponding CQ */
13658 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13659
2d7dbc4c
JS
13660 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13661 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13662 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13663 /* Process NVMET unsol rcv */
13664 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13665 goto process_cq;
13666 }
67d12733
JS
13667 }
13668
895427bd
JS
13669 if (phba->sli4_hba.nvme_cq_map &&
13670 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13671 /* Process NVME / NVMET command completion */
895427bd
JS
13672 cq = phba->sli4_hba.nvme_cq[qidx];
13673 goto process_cq;
2e90f4b5 13674 }
67d12733 13675
895427bd
JS
13676 if (phba->sli4_hba.fcp_cq_map &&
13677 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13678 /* Process FCP command completion */
13679 cq = phba->sli4_hba.fcp_cq[qidx];
13680 goto process_cq;
2e90f4b5 13681 }
895427bd
JS
13682
13683 if (phba->sli4_hba.nvmels_cq &&
13684 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13685 /* Process NVME unsol rcv */
13686 cq = phba->sli4_hba.nvmels_cq;
13687 }
13688
13689 /* Otherwise this is a Slow path event */
13690 if (cq == NULL) {
f485c18d
DK
13691 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13692 return;
4f774513
JS
13693 }
13694
895427bd 13695process_cq:
4f774513
JS
13696 if (unlikely(cqid != cq->queue_id)) {
13697 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13698 "0368 Miss-matched fast-path completion "
13699 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13700 cqid, cq->queue_id);
f485c18d 13701 return;
4f774513
JS
13702 }
13703
895427bd
JS
13704 /* Save EQ associated with this CQ */
13705 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13706
f485c18d
DK
13707 if (!queue_work(phba->wq, &cq->irqwork))
13708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13709 "0363 Cannot schedule soft IRQ "
13710 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13711 cqid, cq->queue_id, smp_processor_id());
13712}
13713
13714/**
13715 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13716 * @phba: Pointer to HBA context object.
13717 * @eqe: Pointer to fast-path event queue entry.
13718 *
13719 * This routine process a event queue entry from the fast-path event queue.
13720 * It will check the MajorCode and MinorCode to determine this is for a
13721 * completion event on a completion queue, if not, an error shall be logged
13722 * and just return. Otherwise, it will get to the corresponding completion
13723 * queue and process all the entries on the completion queue, rearm the
13724 * completion queue, and then return.
13725 **/
13726static void
13727lpfc_sli4_hba_process_cq(struct work_struct *work)
13728{
13729 struct lpfc_queue *cq =
13730 container_of(work, struct lpfc_queue, irqwork);
13731 struct lpfc_hba *phba = cq->phba;
13732 struct lpfc_cqe *cqe;
13733 bool workposted = false;
13734 int ccount = 0;
13735
4f774513
JS
13736 /* Process all the entries to the CQ */
13737 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13738#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13739 if (phba->ktime_on)
13740 cq->isr_timestamp = ktime_get_ns();
13741 else
13742 cq->isr_timestamp = 0;
13743#endif
895427bd 13744 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13745 if (!(++ccount % cq->entry_repost))
7869da18 13746 break;
4f774513
JS
13747 }
13748
b84daac9 13749 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13750 if (ccount > cq->CQ_max_cqe)
13751 cq->CQ_max_cqe = ccount;
13752 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13753
4f774513 13754 /* Catch the no cq entry condition */
f485c18d 13755 if (unlikely(ccount == 0))
4f774513
JS
13756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13757 "0369 No entry from fast-path completion "
13758 "queue fcpcqid=%d\n", cq->queue_id);
13759
13760 /* In any case, flash and re-arm the CQ */
b71413dd 13761 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13762
13763 /* wake up worker thread if there are works to be done */
13764 if (workposted)
13765 lpfc_worker_wake_up(phba);
13766}
13767
13768static void
13769lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13770{
13771 struct lpfc_eqe *eqe;
13772
13773 /* walk all the EQ entries and drop on the floor */
13774 while ((eqe = lpfc_sli4_eq_get(eq)))
13775 ;
13776
13777 /* Clear and re-arm the EQ */
b71413dd 13778 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
4f774513
JS
13779}
13780
1ba981fd
JS
13781
13782/**
13783 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13784 * entry
13785 * @phba: Pointer to HBA context object.
13786 * @eqe: Pointer to fast-path event queue entry.
13787 *
13788 * This routine process a event queue entry from the Flash Optimized Fabric
13789 * event queue. It will check the MajorCode and MinorCode to determine this
13790 * is for a completion event on a completion queue, if not, an error shall be
13791 * logged and just return. Otherwise, it will get to the corresponding
13792 * completion queue and process all the entries on the completion queue, rearm
13793 * the completion queue, and then return.
13794 **/
13795static void
13796lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13797{
13798 struct lpfc_queue *cq;
1ba981fd 13799 uint16_t cqid;
1ba981fd
JS
13800
13801 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13803 "9147 Not a valid completion "
13804 "event: majorcode=x%x, minorcode=x%x\n",
13805 bf_get_le32(lpfc_eqe_major_code, eqe),
13806 bf_get_le32(lpfc_eqe_minor_code, eqe));
13807 return;
13808 }
13809
13810 /* Get the reference to the corresponding CQ */
13811 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13812
13813 /* Next check for OAS */
13814 cq = phba->sli4_hba.oas_cq;
13815 if (unlikely(!cq)) {
13816 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13818 "9148 OAS completion queue "
13819 "does not exist\n");
13820 return;
13821 }
13822
13823 if (unlikely(cqid != cq->queue_id)) {
13824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13825 "9149 Miss-matched fast-path compl "
13826 "queue id: eqcqid=%d, fcpcqid=%d\n",
13827 cqid, cq->queue_id);
13828 return;
13829 }
13830
d41b65bc
JS
13831 /* Save EQ associated with this CQ */
13832 cq->assoc_qp = phba->sli4_hba.fof_eq;
13833
f485c18d
DK
13834 /* CQ work will be processed on CPU affinitized to this IRQ */
13835 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13836 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13837 "0367 Cannot schedule soft IRQ "
13838 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13839 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13840}
13841
13842/**
13843 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13844 * @irq: Interrupt number.
13845 * @dev_id: The device context pointer.
13846 *
13847 * This function is directly called from the PCI layer as an interrupt
13848 * service routine when device with SLI-4 interface spec is enabled with
13849 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13850 * IOCB ring event in the HBA. However, when the device is enabled with either
13851 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13852 * device-level interrupt handler. When the PCI slot is in error recovery
13853 * or the HBA is undergoing initialization, the interrupt handler will not
13854 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13855 * the intrrupt context. This function is called without any lock held.
13856 * It gets the hbalock to access and update SLI data structures. Note that,
13857 * the EQ to CQ are one-to-one map such that the EQ index is
13858 * equal to that of CQ index.
13859 *
13860 * This function returns IRQ_HANDLED when interrupt is handled else it
13861 * returns IRQ_NONE.
13862 **/
13863irqreturn_t
13864lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13865{
13866 struct lpfc_hba *phba;
895427bd 13867 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13868 struct lpfc_queue *eq;
13869 struct lpfc_eqe *eqe;
13870 unsigned long iflag;
13871 int ecount = 0;
1ba981fd
JS
13872
13873 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13874 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13875 phba = hba_eq_hdl->phba;
1ba981fd
JS
13876
13877 if (unlikely(!phba))
13878 return IRQ_NONE;
13879
13880 /* Get to the EQ struct associated with this vector */
13881 eq = phba->sli4_hba.fof_eq;
13882 if (unlikely(!eq))
13883 return IRQ_NONE;
13884
13885 /* Check device state for handling interrupt */
13886 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13887 /* Check again for link_state with lock held */
13888 spin_lock_irqsave(&phba->hbalock, iflag);
13889 if (phba->link_state < LPFC_LINK_DOWN)
13890 /* Flush, clear interrupt, and rearm the EQ */
13891 lpfc_sli4_eq_flush(phba, eq);
13892 spin_unlock_irqrestore(&phba->hbalock, iflag);
13893 return IRQ_NONE;
13894 }
13895
13896 /*
13897 * Process all the event on FCP fast-path EQ
13898 */
13899 while ((eqe = lpfc_sli4_eq_get(eq))) {
13900 lpfc_sli4_fof_handle_eqe(phba, eqe);
13901 if (!(++ecount % eq->entry_repost))
7869da18 13902 break;
1ba981fd
JS
13903 eq->EQ_processed++;
13904 }
13905
13906 /* Track the max number of EQEs processed in 1 intr */
13907 if (ecount > eq->EQ_max_eqe)
13908 eq->EQ_max_eqe = ecount;
13909
13910
13911 if (unlikely(ecount == 0)) {
13912 eq->EQ_no_entry++;
13913
13914 if (phba->intr_type == MSIX)
13915 /* MSI-X treated interrupt served as no EQ share INT */
13916 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13917 "9145 MSI-X interrupt with no EQE\n");
13918 else {
13919 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13920 "9146 ISR interrupt with no EQE\n");
13921 /* Non MSI-X treated on interrupt as EQ share INT */
13922 return IRQ_NONE;
13923 }
13924 }
13925 /* Always clear and re-arm the fast-path EQ */
b71413dd 13926 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
1ba981fd
JS
13927 return IRQ_HANDLED;
13928}
13929
4f774513 13930/**
67d12733 13931 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13932 * @irq: Interrupt number.
13933 * @dev_id: The device context pointer.
13934 *
13935 * This function is directly called from the PCI layer as an interrupt
13936 * service routine when device with SLI-4 interface spec is enabled with
13937 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13938 * ring event in the HBA. However, when the device is enabled with either
13939 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13940 * device-level interrupt handler. When the PCI slot is in error recovery
13941 * or the HBA is undergoing initialization, the interrupt handler will not
13942 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13943 * the intrrupt context. This function is called without any lock held.
13944 * It gets the hbalock to access and update SLI data structures. Note that,
13945 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13946 * equal to that of FCP CQ index.
13947 *
67d12733
JS
13948 * The link attention and ELS ring attention events are handled
13949 * by the worker thread. The interrupt handler signals the worker thread
13950 * and returns for these events. This function is called without any lock
13951 * held. It gets the hbalock to access and update SLI data structures.
13952 *
4f774513
JS
13953 * This function returns IRQ_HANDLED when interrupt is handled else it
13954 * returns IRQ_NONE.
13955 **/
13956irqreturn_t
67d12733 13957lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13958{
13959 struct lpfc_hba *phba;
895427bd 13960 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13961 struct lpfc_queue *fpeq;
13962 struct lpfc_eqe *eqe;
13963 unsigned long iflag;
13964 int ecount = 0;
895427bd 13965 int hba_eqidx;
4f774513
JS
13966
13967 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13968 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13969 phba = hba_eq_hdl->phba;
13970 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13971
13972 if (unlikely(!phba))
13973 return IRQ_NONE;
67d12733 13974 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13975 return IRQ_NONE;
4f774513
JS
13976
13977 /* Get to the EQ struct associated with this vector */
895427bd 13978 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13979 if (unlikely(!fpeq))
13980 return IRQ_NONE;
4f774513 13981
ba20c853 13982 if (lpfc_fcp_look_ahead) {
895427bd 13983 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
b71413dd 13984 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 13985 else {
895427bd 13986 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13987 return IRQ_NONE;
13988 }
13989 }
13990
4f774513
JS
13991 /* Check device state for handling interrupt */
13992 if (unlikely(lpfc_intr_state_check(phba))) {
13993 /* Check again for link_state with lock held */
13994 spin_lock_irqsave(&phba->hbalock, iflag);
13995 if (phba->link_state < LPFC_LINK_DOWN)
13996 /* Flush, clear interrupt, and rearm the EQ */
13997 lpfc_sli4_eq_flush(phba, fpeq);
13998 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13999 if (lpfc_fcp_look_ahead)
895427bd 14000 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
14001 return IRQ_NONE;
14002 }
14003
14004 /*
14005 * Process all the event on FCP fast-path EQ
14006 */
14007 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
14008 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14009 if (!(++ecount % fpeq->entry_repost))
7869da18 14010 break;
b84daac9 14011 fpeq->EQ_processed++;
4f774513
JS
14012 }
14013
b84daac9
JS
14014 /* Track the max number of EQEs processed in 1 intr */
14015 if (ecount > fpeq->EQ_max_eqe)
14016 fpeq->EQ_max_eqe = ecount;
14017
4f774513 14018 /* Always clear and re-arm the fast-path EQ */
b71413dd 14019 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
4f774513
JS
14020
14021 if (unlikely(ecount == 0)) {
b84daac9 14022 fpeq->EQ_no_entry++;
ba20c853
JS
14023
14024 if (lpfc_fcp_look_ahead) {
895427bd 14025 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14026 return IRQ_NONE;
14027 }
14028
4f774513
JS
14029 if (phba->intr_type == MSIX)
14030 /* MSI-X treated interrupt served as no EQ share INT */
14031 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14032 "0358 MSI-X interrupt with no EQE\n");
14033 else
14034 /* Non MSI-X treated on interrupt as EQ share INT */
14035 return IRQ_NONE;
14036 }
14037
ba20c853 14038 if (lpfc_fcp_look_ahead)
895427bd
JS
14039 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14040
4f774513
JS
14041 return IRQ_HANDLED;
14042} /* lpfc_sli4_fp_intr_handler */
14043
14044/**
14045 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14046 * @irq: Interrupt number.
14047 * @dev_id: The device context pointer.
14048 *
14049 * This function is the device-level interrupt handler to device with SLI-4
14050 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14051 * interrupt mode is enabled and there is an event in the HBA which requires
14052 * driver attention. This function invokes the slow-path interrupt attention
14053 * handling function and fast-path interrupt attention handling function in
14054 * turn to process the relevant HBA attention events. This function is called
14055 * without any lock held. It gets the hbalock to access and update SLI data
14056 * structures.
14057 *
14058 * This function returns IRQ_HANDLED when interrupt is handled, else it
14059 * returns IRQ_NONE.
14060 **/
14061irqreturn_t
14062lpfc_sli4_intr_handler(int irq, void *dev_id)
14063{
14064 struct lpfc_hba *phba;
67d12733
JS
14065 irqreturn_t hba_irq_rc;
14066 bool hba_handled = false;
895427bd 14067 int qidx;
4f774513
JS
14068
14069 /* Get the driver's phba structure from the dev_id */
14070 phba = (struct lpfc_hba *)dev_id;
14071
14072 if (unlikely(!phba))
14073 return IRQ_NONE;
14074
4f774513
JS
14075 /*
14076 * Invoke fast-path host attention interrupt handling as appropriate.
14077 */
895427bd 14078 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 14079 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 14080 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
14081 if (hba_irq_rc == IRQ_HANDLED)
14082 hba_handled |= true;
4f774513
JS
14083 }
14084
1ba981fd
JS
14085 if (phba->cfg_fof) {
14086 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 14087 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
14088 if (hba_irq_rc == IRQ_HANDLED)
14089 hba_handled |= true;
14090 }
14091
67d12733 14092 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
14093} /* lpfc_sli4_intr_handler */
14094
14095/**
14096 * lpfc_sli4_queue_free - free a queue structure and associated memory
14097 * @queue: The queue structure to free.
14098 *
b595076a 14099 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
14100 * the host resident queue. This function must be called after destroying the
14101 * queue on the HBA.
14102 **/
14103void
14104lpfc_sli4_queue_free(struct lpfc_queue *queue)
14105{
14106 struct lpfc_dmabuf *dmabuf;
14107
14108 if (!queue)
14109 return;
14110
14111 while (!list_empty(&queue->page_list)) {
14112 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14113 list);
81b96eda 14114 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
14115 dmabuf->virt, dmabuf->phys);
14116 kfree(dmabuf);
14117 }
895427bd
JS
14118 if (queue->rqbp) {
14119 lpfc_free_rq_buffer(queue->phba, queue);
14120 kfree(queue->rqbp);
14121 }
d1f525aa
JS
14122
14123 if (!list_empty(&queue->wq_list))
14124 list_del(&queue->wq_list);
14125
4f774513
JS
14126 kfree(queue);
14127 return;
14128}
14129
14130/**
14131 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14132 * @phba: The HBA that this queue is being created on.
81b96eda 14133 * @page_size: The size of a queue page
4f774513
JS
14134 * @entry_size: The size of each queue entry for this queue.
14135 * @entry count: The number of entries that this queue will handle.
14136 *
14137 * This function allocates a queue structure and the DMAable memory used for
14138 * the host resident queue. This function must be called before creating the
14139 * queue on the HBA.
14140 **/
14141struct lpfc_queue *
81b96eda
JS
14142lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14143 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
14144{
14145 struct lpfc_queue *queue;
14146 struct lpfc_dmabuf *dmabuf;
14147 int x, total_qe_count;
14148 void *dma_pointer;
cb5172ea 14149 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 14150
cb5172ea 14151 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14152 hw_page_size = page_size;
cb5172ea 14153
4f774513
JS
14154 queue = kzalloc(sizeof(struct lpfc_queue) +
14155 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14156 if (!queue)
14157 return NULL;
cb5172ea
JS
14158 queue->page_count = (ALIGN(entry_size * entry_count,
14159 hw_page_size))/hw_page_size;
895427bd
JS
14160
14161 /* If needed, Adjust page count to match the max the adapter supports */
14162 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14163 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14164
4f774513 14165 INIT_LIST_HEAD(&queue->list);
895427bd 14166 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 14167 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
14168 INIT_LIST_HEAD(&queue->page_list);
14169 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
14170
14171 /* Set queue parameters now. If the system cannot provide memory
14172 * resources, the free routine needs to know what was allocated.
14173 */
14174 queue->entry_size = entry_size;
14175 queue->entry_count = entry_count;
14176 queue->page_size = hw_page_size;
14177 queue->phba = phba;
14178
4f774513
JS
14179 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14180 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14181 if (!dmabuf)
14182 goto out_fail;
1aee383d
JP
14183 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14184 hw_page_size, &dmabuf->phys,
14185 GFP_KERNEL);
4f774513
JS
14186 if (!dmabuf->virt) {
14187 kfree(dmabuf);
14188 goto out_fail;
14189 }
14190 dmabuf->buffer_tag = x;
14191 list_add_tail(&dmabuf->list, &queue->page_list);
14192 /* initialize queue's entry array */
14193 dma_pointer = dmabuf->virt;
14194 for (; total_qe_count < entry_count &&
cb5172ea 14195 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
14196 total_qe_count++, dma_pointer += entry_size) {
14197 queue->qe[total_qe_count].address = dma_pointer;
14198 }
14199 }
f485c18d
DK
14200 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14201 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 14202
64eb4dcb
JS
14203 /* entry_repost will be set during q creation */
14204
4f774513
JS
14205 return queue;
14206out_fail:
14207 lpfc_sli4_queue_free(queue);
14208 return NULL;
14209}
14210
962bc51b
JS
14211/**
14212 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14213 * @phba: HBA structure that indicates port to create a queue on.
14214 * @pci_barset: PCI BAR set flag.
14215 *
14216 * This function shall perform iomap of the specified PCI BAR address to host
14217 * memory address if not already done so and return it. The returned host
14218 * memory address can be NULL.
14219 */
14220static void __iomem *
14221lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14222{
962bc51b
JS
14223 if (!phba->pcidev)
14224 return NULL;
962bc51b
JS
14225
14226 switch (pci_barset) {
14227 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14228 return phba->pci_bar0_memmap_p;
14229 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14230 return phba->pci_bar2_memmap_p;
14231 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14232 return phba->pci_bar4_memmap_p;
14233 default:
14234 break;
14235 }
14236 return NULL;
14237}
14238
173edbb2 14239/**
895427bd 14240 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14241 * @phba: HBA structure that indicates port to create a queue on.
14242 * @startq: The starting FCP EQ to modify
14243 *
14244 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14245 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14246 * updated in one mailbox command.
173edbb2
JS
14247 *
14248 * The @phba struct is used to send mailbox command to HBA. The @startq
14249 * is used to get the starting FCP EQ to change.
14250 * This function is asynchronous and will wait for the mailbox
14251 * command to finish before continuing.
14252 *
14253 * On success this function will return a zero. If unable to allocate enough
14254 * memory this function will return -ENOMEM. If the queue create mailbox command
14255 * fails this function will return -ENXIO.
14256 **/
a2fc4aef 14257int
0cf07f84
JS
14258lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14259 uint32_t numq, uint32_t imax)
173edbb2
JS
14260{
14261 struct lpfc_mbx_modify_eq_delay *eq_delay;
14262 LPFC_MBOXQ_t *mbox;
14263 struct lpfc_queue *eq;
14264 int cnt, rc, length, status = 0;
14265 uint32_t shdr_status, shdr_add_status;
0cf07f84 14266 uint32_t result, val;
895427bd 14267 int qidx;
173edbb2
JS
14268 union lpfc_sli4_cfg_shdr *shdr;
14269 uint16_t dmult;
14270
895427bd 14271 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14272 return 0;
14273
14274 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14275 if (!mbox)
14276 return -ENOMEM;
14277 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14278 sizeof(struct lpfc_sli4_cfg_mhdr));
14279 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14280 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14281 length, LPFC_SLI4_MBX_EMBED);
14282 eq_delay = &mbox->u.mqe.un.eq_delay;
14283
14284 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14285 result = imax / phba->io_channel_irqs;
895427bd 14286 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14287 dmult = 0;
14288 else
14289 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14290 if (dmult > LPFC_DMULT_MAX)
14291 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14292
14293 cnt = 0;
895427bd
JS
14294 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14295 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14296 if (!eq)
14297 continue;
0cf07f84 14298 eq->q_mode = imax;
173edbb2
JS
14299 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14300 eq_delay->u.request.eq[cnt].phase = 0;
14301 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14302 cnt++;
0cf07f84
JS
14303
14304 /* q_mode is only used for auto_imax */
14305 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14306 /* Use EQ Delay Register method for q_mode */
14307
14308 /* Convert for EQ Delay register */
14309 val = phba->cfg_fcp_imax;
14310 if (val) {
14311 /* First, interrupts per sec per EQ */
14312 val = phba->cfg_fcp_imax /
14313 phba->io_channel_irqs;
14314
14315 /* us delay between each interrupt */
14316 val = LPFC_SEC_TO_USEC / val;
14317 }
14318 eq->q_mode = val;
14319 } else {
14320 eq->q_mode = imax;
14321 }
14322
14323 if (cnt >= numq)
173edbb2
JS
14324 break;
14325 }
14326 eq_delay->u.request.num_eq = cnt;
14327
14328 mbox->vport = phba->pport;
14329 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14330 mbox->context1 = NULL;
14331 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14332 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14333 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14334 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14335 if (shdr_status || shdr_add_status || rc) {
14336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14337 "2512 MODIFY_EQ_DELAY mailbox failed with "
14338 "status x%x add_status x%x, mbx status x%x\n",
14339 shdr_status, shdr_add_status, rc);
14340 status = -ENXIO;
14341 }
14342 mempool_free(mbox, phba->mbox_mem_pool);
14343 return status;
14344}
14345
4f774513
JS
14346/**
14347 * lpfc_eq_create - Create an Event Queue on the HBA
14348 * @phba: HBA structure that indicates port to create a queue on.
14349 * @eq: The queue structure to use to create the event queue.
14350 * @imax: The maximum interrupt per second limit.
14351 *
14352 * This function creates an event queue, as detailed in @eq, on a port,
14353 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14354 *
14355 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14356 * is used to get the entry count and entry size that are necessary to
14357 * determine the number of pages to allocate and use for this queue. This
14358 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14359 * event queue. This function is asynchronous and will wait for the mailbox
14360 * command to finish before continuing.
14361 *
14362 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14363 * memory this function will return -ENOMEM. If the queue create mailbox command
14364 * fails this function will return -ENXIO.
4f774513 14365 **/
a2fc4aef 14366int
ee02006b 14367lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14368{
14369 struct lpfc_mbx_eq_create *eq_create;
14370 LPFC_MBOXQ_t *mbox;
14371 int rc, length, status = 0;
14372 struct lpfc_dmabuf *dmabuf;
14373 uint32_t shdr_status, shdr_add_status;
14374 union lpfc_sli4_cfg_shdr *shdr;
14375 uint16_t dmult;
49198b37
JS
14376 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14377
2e90f4b5
JS
14378 /* sanity check on queue memory */
14379 if (!eq)
14380 return -ENODEV;
49198b37
JS
14381 if (!phba->sli4_hba.pc_sli4_params.supported)
14382 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14383
14384 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14385 if (!mbox)
14386 return -ENOMEM;
14387 length = (sizeof(struct lpfc_mbx_eq_create) -
14388 sizeof(struct lpfc_sli4_cfg_mhdr));
14389 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14390 LPFC_MBOX_OPCODE_EQ_CREATE,
14391 length, LPFC_SLI4_MBX_EMBED);
14392 eq_create = &mbox->u.mqe.un.eq_create;
7365f6fd 14393 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
4f774513
JS
14394 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14395 eq->page_count);
14396 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14397 LPFC_EQE_SIZE);
14398 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
7365f6fd
JS
14399
14400 /* Use version 2 of CREATE_EQ if eqav is set */
14401 if (phba->sli4_hba.pc_sli4_params.eqav) {
14402 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14403 LPFC_Q_CREATE_VERSION_2);
14404 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14405 phba->sli4_hba.pc_sli4_params.eqav);
14406 }
14407
2c9c5a00
JS
14408 /* don't setup delay multiplier using EQ_CREATE */
14409 dmult = 0;
4f774513
JS
14410 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14411 dmult);
14412 switch (eq->entry_count) {
14413 default:
14414 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14415 "0360 Unsupported EQ count. (%d)\n",
14416 eq->entry_count);
14417 if (eq->entry_count < 256)
14418 return -EINVAL;
14419 /* otherwise default to smallest count (drop through) */
14420 case 256:
14421 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14422 LPFC_EQ_CNT_256);
14423 break;
14424 case 512:
14425 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14426 LPFC_EQ_CNT_512);
14427 break;
14428 case 1024:
14429 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14430 LPFC_EQ_CNT_1024);
14431 break;
14432 case 2048:
14433 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14434 LPFC_EQ_CNT_2048);
14435 break;
14436 case 4096:
14437 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14438 LPFC_EQ_CNT_4096);
14439 break;
14440 }
14441 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14442 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14443 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14444 putPaddrLow(dmabuf->phys);
14445 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14446 putPaddrHigh(dmabuf->phys);
14447 }
14448 mbox->vport = phba->pport;
14449 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14450 mbox->context1 = NULL;
14451 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4f774513
JS
14452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14454 if (shdr_status || shdr_add_status || rc) {
14455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14456 "2500 EQ_CREATE mailbox failed with "
14457 "status x%x add_status x%x, mbx status x%x\n",
14458 shdr_status, shdr_add_status, rc);
14459 status = -ENXIO;
14460 }
14461 eq->type = LPFC_EQ;
14462 eq->subtype = LPFC_NONE;
14463 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14464 if (eq->queue_id == 0xFFFF)
14465 status = -ENXIO;
14466 eq->host_index = 0;
14467 eq->hba_index = 0;
64eb4dcb 14468 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14469
8fa38513 14470 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14471 return status;
14472}
14473
14474/**
14475 * lpfc_cq_create - Create a Completion Queue on the HBA
14476 * @phba: HBA structure that indicates port to create a queue on.
14477 * @cq: The queue structure to use to create the completion queue.
14478 * @eq: The event queue to bind this completion queue to.
14479 *
14480 * This function creates a completion queue, as detailed in @wq, on a port,
14481 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14482 *
14483 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14484 * is used to get the entry count and entry size that are necessary to
14485 * determine the number of pages to allocate and use for this queue. The @eq
14486 * is used to indicate which event queue to bind this completion queue to. This
14487 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14488 * completion queue. This function is asynchronous and will wait for the mailbox
14489 * command to finish before continuing.
14490 *
14491 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14492 * memory this function will return -ENOMEM. If the queue create mailbox command
14493 * fails this function will return -ENXIO.
4f774513 14494 **/
a2fc4aef 14495int
4f774513
JS
14496lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14497 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14498{
14499 struct lpfc_mbx_cq_create *cq_create;
14500 struct lpfc_dmabuf *dmabuf;
14501 LPFC_MBOXQ_t *mbox;
14502 int rc, length, status = 0;
14503 uint32_t shdr_status, shdr_add_status;
14504 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14505 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14506
2e90f4b5
JS
14507 /* sanity check on queue memory */
14508 if (!cq || !eq)
14509 return -ENODEV;
49198b37 14510 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14511 hw_page_size = cq->page_size;
49198b37 14512
4f774513
JS
14513 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14514 if (!mbox)
14515 return -ENOMEM;
14516 length = (sizeof(struct lpfc_mbx_cq_create) -
14517 sizeof(struct lpfc_sli4_cfg_mhdr));
14518 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14519 LPFC_MBOX_OPCODE_CQ_CREATE,
14520 length, LPFC_SLI4_MBX_EMBED);
14521 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14522 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14523 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14524 cq->page_count);
14525 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14526 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14527 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14528 phba->sli4_hba.pc_sli4_params.cqv);
14529 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14530 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14531 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14532 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14533 eq->queue_id);
7365f6fd
JS
14534 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14535 phba->sli4_hba.pc_sli4_params.cqav);
5a6f133e
JS
14536 } else {
14537 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14538 eq->queue_id);
14539 }
4f774513 14540 switch (cq->entry_count) {
81b96eda
JS
14541 case 2048:
14542 case 4096:
14543 if (phba->sli4_hba.pc_sli4_params.cqv ==
14544 LPFC_Q_CREATE_VERSION_2) {
14545 cq_create->u.request.context.lpfc_cq_context_count =
14546 cq->entry_count;
14547 bf_set(lpfc_cq_context_count,
14548 &cq_create->u.request.context,
14549 LPFC_CQ_CNT_WORD7);
14550 break;
14551 }
14552 /* Fall Thru */
4f774513
JS
14553 default:
14554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14555 "0361 Unsupported CQ count: "
64eb4dcb 14556 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14557 cq->entry_count, cq->entry_size,
64eb4dcb 14558 cq->page_count);
4f4c1863
JS
14559 if (cq->entry_count < 256) {
14560 status = -EINVAL;
14561 goto out;
14562 }
4f774513
JS
14563 /* otherwise default to smallest count (drop through) */
14564 case 256:
14565 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14566 LPFC_CQ_CNT_256);
14567 break;
14568 case 512:
14569 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14570 LPFC_CQ_CNT_512);
14571 break;
14572 case 1024:
14573 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14574 LPFC_CQ_CNT_1024);
14575 break;
14576 }
14577 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14578 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14579 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14580 putPaddrLow(dmabuf->phys);
14581 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14582 putPaddrHigh(dmabuf->phys);
14583 }
14584 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14585
14586 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14587 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14588 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14589 if (shdr_status || shdr_add_status || rc) {
14590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14591 "2501 CQ_CREATE mailbox failed with "
14592 "status x%x add_status x%x, mbx status x%x\n",
14593 shdr_status, shdr_add_status, rc);
14594 status = -ENXIO;
14595 goto out;
14596 }
14597 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14598 if (cq->queue_id == 0xFFFF) {
14599 status = -ENXIO;
14600 goto out;
14601 }
14602 /* link the cq onto the parent eq child list */
14603 list_add_tail(&cq->list, &eq->child_list);
14604 /* Set up completion queue's type and subtype */
14605 cq->type = type;
14606 cq->subtype = subtype;
14607 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14608 cq->assoc_qid = eq->queue_id;
4f774513
JS
14609 cq->host_index = 0;
14610 cq->hba_index = 0;
64eb4dcb 14611 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14612
8fa38513
JS
14613out:
14614 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14615 return status;
14616}
14617
2d7dbc4c
JS
14618/**
14619 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14620 * @phba: HBA structure that indicates port to create a queue on.
14621 * @cqp: The queue structure array to use to create the completion queues.
14622 * @eqp: The event queue array to bind these completion queues to.
14623 *
14624 * This function creates a set of completion queue, s to support MRQ
14625 * as detailed in @cqp, on a port,
14626 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14627 *
14628 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14629 * is used to get the entry count and entry size that are necessary to
14630 * determine the number of pages to allocate and use for this queue. The @eq
14631 * is used to indicate which event queue to bind this completion queue to. This
14632 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14633 * completion queue. This function is asynchronous and will wait for the mailbox
14634 * command to finish before continuing.
14635 *
14636 * On success this function will return a zero. If unable to allocate enough
14637 * memory this function will return -ENOMEM. If the queue create mailbox command
14638 * fails this function will return -ENXIO.
14639 **/
14640int
14641lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14642 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14643{
14644 struct lpfc_queue *cq;
14645 struct lpfc_queue *eq;
14646 struct lpfc_mbx_cq_create_set *cq_set;
14647 struct lpfc_dmabuf *dmabuf;
14648 LPFC_MBOXQ_t *mbox;
14649 int rc, length, alloclen, status = 0;
14650 int cnt, idx, numcq, page_idx = 0;
14651 uint32_t shdr_status, shdr_add_status;
14652 union lpfc_sli4_cfg_shdr *shdr;
14653 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14654
14655 /* sanity check on queue memory */
14656 numcq = phba->cfg_nvmet_mrq;
14657 if (!cqp || !eqp || !numcq)
14658 return -ENODEV;
2d7dbc4c
JS
14659
14660 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14661 if (!mbox)
14662 return -ENOMEM;
14663
14664 length = sizeof(struct lpfc_mbx_cq_create_set);
14665 length += ((numcq * cqp[0]->page_count) *
14666 sizeof(struct dma_address));
14667 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14668 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14669 LPFC_SLI4_MBX_NEMBED);
14670 if (alloclen < length) {
14671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14672 "3098 Allocated DMA memory size (%d) is "
14673 "less than the requested DMA memory size "
14674 "(%d)\n", alloclen, length);
14675 status = -ENOMEM;
14676 goto out;
14677 }
14678 cq_set = mbox->sge_array->addr[0];
14679 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14680 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14681
14682 for (idx = 0; idx < numcq; idx++) {
14683 cq = cqp[idx];
14684 eq = eqp[idx];
14685 if (!cq || !eq) {
14686 status = -ENOMEM;
14687 goto out;
14688 }
81b96eda
JS
14689 if (!phba->sli4_hba.pc_sli4_params.supported)
14690 hw_page_size = cq->page_size;
2d7dbc4c
JS
14691
14692 switch (idx) {
14693 case 0:
14694 bf_set(lpfc_mbx_cq_create_set_page_size,
14695 &cq_set->u.request,
14696 (hw_page_size / SLI4_PAGE_SIZE));
14697 bf_set(lpfc_mbx_cq_create_set_num_pages,
14698 &cq_set->u.request, cq->page_count);
14699 bf_set(lpfc_mbx_cq_create_set_evt,
14700 &cq_set->u.request, 1);
14701 bf_set(lpfc_mbx_cq_create_set_valid,
14702 &cq_set->u.request, 1);
14703 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14704 &cq_set->u.request, 0);
14705 bf_set(lpfc_mbx_cq_create_set_num_cq,
14706 &cq_set->u.request, numcq);
7365f6fd
JS
14707 bf_set(lpfc_mbx_cq_create_set_autovalid,
14708 &cq_set->u.request,
14709 phba->sli4_hba.pc_sli4_params.cqav);
2d7dbc4c 14710 switch (cq->entry_count) {
81b96eda
JS
14711 case 2048:
14712 case 4096:
14713 if (phba->sli4_hba.pc_sli4_params.cqv ==
14714 LPFC_Q_CREATE_VERSION_2) {
14715 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14716 &cq_set->u.request,
14717 cq->entry_count);
14718 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14719 &cq_set->u.request,
14720 LPFC_CQ_CNT_WORD7);
14721 break;
14722 }
14723 /* Fall Thru */
2d7dbc4c
JS
14724 default:
14725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14726 "3118 Bad CQ count. (%d)\n",
14727 cq->entry_count);
14728 if (cq->entry_count < 256) {
14729 status = -EINVAL;
14730 goto out;
14731 }
14732 /* otherwise default to smallest (drop thru) */
14733 case 256:
14734 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14735 &cq_set->u.request, LPFC_CQ_CNT_256);
14736 break;
14737 case 512:
14738 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14739 &cq_set->u.request, LPFC_CQ_CNT_512);
14740 break;
14741 case 1024:
14742 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14743 &cq_set->u.request, LPFC_CQ_CNT_1024);
14744 break;
14745 }
14746 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14747 &cq_set->u.request, eq->queue_id);
14748 break;
14749 case 1:
14750 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14751 &cq_set->u.request, eq->queue_id);
14752 break;
14753 case 2:
14754 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14755 &cq_set->u.request, eq->queue_id);
14756 break;
14757 case 3:
14758 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14759 &cq_set->u.request, eq->queue_id);
14760 break;
14761 case 4:
14762 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14763 &cq_set->u.request, eq->queue_id);
14764 break;
14765 case 5:
14766 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14767 &cq_set->u.request, eq->queue_id);
14768 break;
14769 case 6:
14770 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14771 &cq_set->u.request, eq->queue_id);
14772 break;
14773 case 7:
14774 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14775 &cq_set->u.request, eq->queue_id);
14776 break;
14777 case 8:
14778 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14779 &cq_set->u.request, eq->queue_id);
14780 break;
14781 case 9:
14782 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14783 &cq_set->u.request, eq->queue_id);
14784 break;
14785 case 10:
14786 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14787 &cq_set->u.request, eq->queue_id);
14788 break;
14789 case 11:
14790 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14791 &cq_set->u.request, eq->queue_id);
14792 break;
14793 case 12:
14794 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14795 &cq_set->u.request, eq->queue_id);
14796 break;
14797 case 13:
14798 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14799 &cq_set->u.request, eq->queue_id);
14800 break;
14801 case 14:
14802 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14803 &cq_set->u.request, eq->queue_id);
14804 break;
14805 case 15:
14806 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14807 &cq_set->u.request, eq->queue_id);
14808 break;
14809 }
14810
14811 /* link the cq onto the parent eq child list */
14812 list_add_tail(&cq->list, &eq->child_list);
14813 /* Set up completion queue's type and subtype */
14814 cq->type = type;
14815 cq->subtype = subtype;
14816 cq->assoc_qid = eq->queue_id;
14817 cq->host_index = 0;
14818 cq->hba_index = 0;
64eb4dcb 14819 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14820 cq->chann = idx;
2d7dbc4c
JS
14821
14822 rc = 0;
14823 list_for_each_entry(dmabuf, &cq->page_list, list) {
14824 memset(dmabuf->virt, 0, hw_page_size);
14825 cnt = page_idx + dmabuf->buffer_tag;
14826 cq_set->u.request.page[cnt].addr_lo =
14827 putPaddrLow(dmabuf->phys);
14828 cq_set->u.request.page[cnt].addr_hi =
14829 putPaddrHigh(dmabuf->phys);
14830 rc++;
14831 }
14832 page_idx += rc;
14833 }
14834
14835 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14836
14837 /* The IOCTL status is embedded in the mailbox subheader. */
14838 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14839 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14840 if (shdr_status || shdr_add_status || rc) {
14841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14842 "3119 CQ_CREATE_SET mailbox failed with "
14843 "status x%x add_status x%x, mbx status x%x\n",
14844 shdr_status, shdr_add_status, rc);
14845 status = -ENXIO;
14846 goto out;
14847 }
14848 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14849 if (rc == 0xFFFF) {
14850 status = -ENXIO;
14851 goto out;
14852 }
14853
14854 for (idx = 0; idx < numcq; idx++) {
14855 cq = cqp[idx];
14856 cq->queue_id = rc + idx;
14857 }
14858
14859out:
14860 lpfc_sli4_mbox_cmd_free(phba, mbox);
14861 return status;
14862}
14863
b19a061a
JS
14864/**
14865 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14866 * @phba: HBA structure that indicates port to create a queue on.
14867 * @mq: The queue structure to use to create the mailbox queue.
14868 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14869 * @cq: The completion queue to associate with this cq.
14870 *
14871 * This function provides failback (fb) functionality when the
14872 * mq_create_ext fails on older FW generations. It's purpose is identical
14873 * to mq_create_ext otherwise.
14874 *
14875 * This routine cannot fail as all attributes were previously accessed and
14876 * initialized in mq_create_ext.
14877 **/
14878static void
14879lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14880 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14881{
14882 struct lpfc_mbx_mq_create *mq_create;
14883 struct lpfc_dmabuf *dmabuf;
14884 int length;
14885
14886 length = (sizeof(struct lpfc_mbx_mq_create) -
14887 sizeof(struct lpfc_sli4_cfg_mhdr));
14888 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14889 LPFC_MBOX_OPCODE_MQ_CREATE,
14890 length, LPFC_SLI4_MBX_EMBED);
14891 mq_create = &mbox->u.mqe.un.mq_create;
14892 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14893 mq->page_count);
14894 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14895 cq->queue_id);
14896 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14897 switch (mq->entry_count) {
14898 case 16:
5a6f133e
JS
14899 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14900 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14901 break;
14902 case 32:
5a6f133e
JS
14903 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14904 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14905 break;
14906 case 64:
5a6f133e
JS
14907 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14908 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14909 break;
14910 case 128:
5a6f133e
JS
14911 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14912 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14913 break;
14914 }
14915 list_for_each_entry(dmabuf, &mq->page_list, list) {
14916 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14917 putPaddrLow(dmabuf->phys);
14918 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14919 putPaddrHigh(dmabuf->phys);
14920 }
14921}
14922
04c68496
JS
14923/**
14924 * lpfc_mq_create - Create a mailbox Queue on the HBA
14925 * @phba: HBA structure that indicates port to create a queue on.
14926 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14927 * @cq: The completion queue to associate with this cq.
14928 * @subtype: The queue's subtype.
04c68496
JS
14929 *
14930 * This function creates a mailbox queue, as detailed in @mq, on a port,
14931 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14932 *
14933 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14934 * is used to get the entry count and entry size that are necessary to
14935 * determine the number of pages to allocate and use for this queue. This
14936 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14937 * mailbox queue. This function is asynchronous and will wait for the mailbox
14938 * command to finish before continuing.
14939 *
14940 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14941 * memory this function will return -ENOMEM. If the queue create mailbox command
14942 * fails this function will return -ENXIO.
04c68496 14943 **/
b19a061a 14944int32_t
04c68496
JS
14945lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14946 struct lpfc_queue *cq, uint32_t subtype)
14947{
14948 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14949 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14950 struct lpfc_dmabuf *dmabuf;
14951 LPFC_MBOXQ_t *mbox;
14952 int rc, length, status = 0;
14953 uint32_t shdr_status, shdr_add_status;
14954 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14955 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14956
2e90f4b5
JS
14957 /* sanity check on queue memory */
14958 if (!mq || !cq)
14959 return -ENODEV;
49198b37
JS
14960 if (!phba->sli4_hba.pc_sli4_params.supported)
14961 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14962
04c68496
JS
14963 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14964 if (!mbox)
14965 return -ENOMEM;
b19a061a 14966 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14967 sizeof(struct lpfc_sli4_cfg_mhdr));
14968 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14969 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14970 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14971
14972 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14973 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14974 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14975 &mq_create_ext->u.request, mq->page_count);
14976 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14977 &mq_create_ext->u.request, 1);
14978 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14979 &mq_create_ext->u.request, 1);
14980 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14981 &mq_create_ext->u.request, 1);
70f3c073
JS
14982 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14983 &mq_create_ext->u.request, 1);
14984 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14985 &mq_create_ext->u.request, 1);
b19a061a 14986 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14987 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14988 phba->sli4_hba.pc_sli4_params.mqv);
14989 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14990 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14991 cq->queue_id);
14992 else
14993 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14994 cq->queue_id);
04c68496
JS
14995 switch (mq->entry_count) {
14996 default:
14997 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14998 "0362 Unsupported MQ count. (%d)\n",
14999 mq->entry_count);
4f4c1863
JS
15000 if (mq->entry_count < 16) {
15001 status = -EINVAL;
15002 goto out;
15003 }
04c68496
JS
15004 /* otherwise default to smallest count (drop through) */
15005 case 16:
5a6f133e
JS
15006 bf_set(lpfc_mq_context_ring_size,
15007 &mq_create_ext->u.request.context,
15008 LPFC_MQ_RING_SIZE_16);
04c68496
JS
15009 break;
15010 case 32:
5a6f133e
JS
15011 bf_set(lpfc_mq_context_ring_size,
15012 &mq_create_ext->u.request.context,
15013 LPFC_MQ_RING_SIZE_32);
04c68496
JS
15014 break;
15015 case 64:
5a6f133e
JS
15016 bf_set(lpfc_mq_context_ring_size,
15017 &mq_create_ext->u.request.context,
15018 LPFC_MQ_RING_SIZE_64);
04c68496
JS
15019 break;
15020 case 128:
5a6f133e
JS
15021 bf_set(lpfc_mq_context_ring_size,
15022 &mq_create_ext->u.request.context,
15023 LPFC_MQ_RING_SIZE_128);
04c68496
JS
15024 break;
15025 }
15026 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 15027 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 15028 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 15029 putPaddrLow(dmabuf->phys);
b19a061a 15030 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
15031 putPaddrHigh(dmabuf->phys);
15032 }
15033 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
15034 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15035 &mq_create_ext->u.response);
15036 if (rc != MBX_SUCCESS) {
15037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15038 "2795 MQ_CREATE_EXT failed with "
15039 "status x%x. Failback to MQ_CREATE.\n",
15040 rc);
15041 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15042 mq_create = &mbox->u.mqe.un.mq_create;
15043 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15044 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15045 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15046 &mq_create->u.response);
15047 }
15048
04c68496 15049 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
15050 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15051 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15052 if (shdr_status || shdr_add_status || rc) {
15053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15054 "2502 MQ_CREATE mailbox failed with "
15055 "status x%x add_status x%x, mbx status x%x\n",
15056 shdr_status, shdr_add_status, rc);
15057 status = -ENXIO;
15058 goto out;
15059 }
04c68496
JS
15060 if (mq->queue_id == 0xFFFF) {
15061 status = -ENXIO;
15062 goto out;
15063 }
15064 mq->type = LPFC_MQ;
2a622bfb 15065 mq->assoc_qid = cq->queue_id;
04c68496
JS
15066 mq->subtype = subtype;
15067 mq->host_index = 0;
15068 mq->hba_index = 0;
64eb4dcb 15069 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
15070
15071 /* link the mq onto the parent cq child list */
15072 list_add_tail(&mq->list, &cq->child_list);
15073out:
8fa38513 15074 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
15075 return status;
15076}
15077
4f774513
JS
15078/**
15079 * lpfc_wq_create - Create a Work Queue on the HBA
15080 * @phba: HBA structure that indicates port to create a queue on.
15081 * @wq: The queue structure to use to create the work queue.
15082 * @cq: The completion queue to bind this work queue to.
15083 * @subtype: The subtype of the work queue indicating its functionality.
15084 *
15085 * This function creates a work queue, as detailed in @wq, on a port, described
15086 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15087 *
15088 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15089 * is used to get the entry count and entry size that are necessary to
15090 * determine the number of pages to allocate and use for this queue. The @cq
15091 * is used to indicate which completion queue to bind this work queue to. This
15092 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15093 * work queue. This function is asynchronous and will wait for the mailbox
15094 * command to finish before continuing.
15095 *
15096 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15097 * memory this function will return -ENOMEM. If the queue create mailbox command
15098 * fails this function will return -ENXIO.
4f774513 15099 **/
a2fc4aef 15100int
4f774513
JS
15101lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15102 struct lpfc_queue *cq, uint32_t subtype)
15103{
15104 struct lpfc_mbx_wq_create *wq_create;
15105 struct lpfc_dmabuf *dmabuf;
15106 LPFC_MBOXQ_t *mbox;
15107 int rc, length, status = 0;
15108 uint32_t shdr_status, shdr_add_status;
15109 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15110 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 15111 struct dma_address *page;
962bc51b
JS
15112 void __iomem *bar_memmap_p;
15113 uint32_t db_offset;
15114 uint16_t pci_barset;
1351e69f
JS
15115 uint8_t dpp_barset;
15116 uint32_t dpp_offset;
15117 unsigned long pg_addr;
81b96eda 15118 uint8_t wq_create_version;
49198b37 15119
2e90f4b5
JS
15120 /* sanity check on queue memory */
15121 if (!wq || !cq)
15122 return -ENODEV;
49198b37 15123 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 15124 hw_page_size = wq->page_size;
4f774513
JS
15125
15126 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15127 if (!mbox)
15128 return -ENOMEM;
15129 length = (sizeof(struct lpfc_mbx_wq_create) -
15130 sizeof(struct lpfc_sli4_cfg_mhdr));
15131 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15132 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15133 length, LPFC_SLI4_MBX_EMBED);
15134 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 15135 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
15136 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15137 wq->page_count);
15138 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15139 cq->queue_id);
0c651878
JS
15140
15141 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
15142 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15143 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 15144
c176ffa0
JS
15145 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15146 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
15147 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15148 else
15149 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15150
0c651878 15151
1351e69f
JS
15152 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15153 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15154 else
15155 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15156
15157 switch (wq_create_version) {
0c651878 15158 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
15159 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15160 wq->entry_count);
3f247de7
JS
15161 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15162 LPFC_Q_CREATE_VERSION_1);
15163
5a6f133e
JS
15164 switch (wq->entry_size) {
15165 default:
15166 case 64:
15167 bf_set(lpfc_mbx_wq_create_wqe_size,
15168 &wq_create->u.request_1,
15169 LPFC_WQ_WQE_SIZE_64);
15170 break;
15171 case 128:
15172 bf_set(lpfc_mbx_wq_create_wqe_size,
15173 &wq_create->u.request_1,
15174 LPFC_WQ_WQE_SIZE_128);
15175 break;
15176 }
1351e69f
JS
15177 /* Request DPP by default */
15178 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
8ea73db4
JS
15179 bf_set(lpfc_mbx_wq_create_page_size,
15180 &wq_create->u.request_1,
81b96eda 15181 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 15182 page = wq_create->u.request_1.page;
0c651878
JS
15183 break;
15184 default:
1351e69f
JS
15185 page = wq_create->u.request.page;
15186 break;
5a6f133e 15187 }
0c651878 15188
4f774513 15189 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 15190 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
15191 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15192 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 15193 }
962bc51b
JS
15194
15195 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15196 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15197
4f774513
JS
15198 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15199 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15200 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15201 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15202 if (shdr_status || shdr_add_status || rc) {
15203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15204 "2503 WQ_CREATE mailbox failed with "
15205 "status x%x add_status x%x, mbx status x%x\n",
15206 shdr_status, shdr_add_status, rc);
15207 status = -ENXIO;
15208 goto out;
15209 }
1351e69f
JS
15210
15211 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15212 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15213 &wq_create->u.response);
15214 else
15215 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15216 &wq_create->u.response_1);
15217
4f774513
JS
15218 if (wq->queue_id == 0xFFFF) {
15219 status = -ENXIO;
15220 goto out;
15221 }
1351e69f
JS
15222
15223 wq->db_format = LPFC_DB_LIST_FORMAT;
15224 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15225 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15226 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15227 &wq_create->u.response);
15228 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15229 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15231 "3265 WQ[%d] doorbell format "
15232 "not supported: x%x\n",
15233 wq->queue_id, wq->db_format);
15234 status = -EINVAL;
15235 goto out;
15236 }
15237 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15238 &wq_create->u.response);
15239 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15240 pci_barset);
15241 if (!bar_memmap_p) {
15242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15243 "3263 WQ[%d] failed to memmap "
15244 "pci barset:x%x\n",
15245 wq->queue_id, pci_barset);
15246 status = -ENOMEM;
15247 goto out;
15248 }
15249 db_offset = wq_create->u.response.doorbell_offset;
15250 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15251 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15253 "3252 WQ[%d] doorbell offset "
15254 "not supported: x%x\n",
15255 wq->queue_id, db_offset);
15256 status = -EINVAL;
15257 goto out;
15258 }
15259 wq->db_regaddr = bar_memmap_p + db_offset;
15260 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15261 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15262 "format:x%x\n", wq->queue_id,
15263 pci_barset, db_offset, wq->db_format);
15264 } else
15265 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15266 } else {
1351e69f
JS
15267 /* Check if DPP was honored by the firmware */
15268 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15269 &wq_create->u.response_1);
15270 if (wq->dpp_enable) {
15271 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15272 &wq_create->u.response_1);
15273 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15274 pci_barset);
15275 if (!bar_memmap_p) {
15276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15277 "3267 WQ[%d] failed to memmap "
15278 "pci barset:x%x\n",
15279 wq->queue_id, pci_barset);
15280 status = -ENOMEM;
15281 goto out;
15282 }
15283 db_offset = wq_create->u.response_1.doorbell_offset;
15284 wq->db_regaddr = bar_memmap_p + db_offset;
15285 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15286 &wq_create->u.response_1);
15287 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15288 &wq_create->u.response_1);
15289 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15290 dpp_barset);
15291 if (!bar_memmap_p) {
15292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15293 "3268 WQ[%d] failed to memmap "
15294 "pci barset:x%x\n",
15295 wq->queue_id, dpp_barset);
15296 status = -ENOMEM;
15297 goto out;
15298 }
15299 dpp_offset = wq_create->u.response_1.dpp_offset;
15300 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15301 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15302 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15303 "dpp_id:x%x dpp_barset:x%x "
15304 "dpp_offset:x%x\n",
15305 wq->queue_id, pci_barset, db_offset,
15306 wq->dpp_id, dpp_barset, dpp_offset);
15307
15308 /* Enable combined writes for DPP aperture */
15309 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15310#ifdef CONFIG_X86
15311 rc = set_memory_wc(pg_addr, 1);
15312 if (rc) {
15313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15314 "3272 Cannot setup Combined "
15315 "Write on WQ[%d] - disable DPP\n",
15316 wq->queue_id);
15317 phba->cfg_enable_dpp = 0;
15318 }
15319#else
15320 phba->cfg_enable_dpp = 0;
15321#endif
15322 } else
15323 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15324 }
895427bd
JS
15325 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15326 if (wq->pring == NULL) {
15327 status = -ENOMEM;
15328 goto out;
15329 }
4f774513 15330 wq->type = LPFC_WQ;
2a622bfb 15331 wq->assoc_qid = cq->queue_id;
4f774513
JS
15332 wq->subtype = subtype;
15333 wq->host_index = 0;
15334 wq->hba_index = 0;
ff78d8f9 15335 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15336
15337 /* link the wq onto the parent cq child list */
15338 list_add_tail(&wq->list, &cq->child_list);
15339out:
8fa38513 15340 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15341 return status;
15342}
15343
15344/**
15345 * lpfc_rq_create - Create a Receive Queue on the HBA
15346 * @phba: HBA structure that indicates port to create a queue on.
15347 * @hrq: The queue structure to use to create the header receive queue.
15348 * @drq: The queue structure to use to create the data receive queue.
15349 * @cq: The completion queue to bind this work queue to.
15350 *
15351 * This function creates a receive buffer queue pair , as detailed in @hrq and
15352 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15353 * to the HBA.
15354 *
15355 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15356 * struct is used to get the entry count that is necessary to determine the
15357 * number of pages to use for this queue. The @cq is used to indicate which
15358 * completion queue to bind received buffers that are posted to these queues to.
15359 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15360 * receive queue pair. This function is asynchronous and will wait for the
15361 * mailbox command to finish before continuing.
15362 *
15363 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15364 * memory this function will return -ENOMEM. If the queue create mailbox command
15365 * fails this function will return -ENXIO.
4f774513 15366 **/
a2fc4aef 15367int
4f774513
JS
15368lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15369 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15370{
15371 struct lpfc_mbx_rq_create *rq_create;
15372 struct lpfc_dmabuf *dmabuf;
15373 LPFC_MBOXQ_t *mbox;
15374 int rc, length, status = 0;
15375 uint32_t shdr_status, shdr_add_status;
15376 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15377 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15378 void __iomem *bar_memmap_p;
15379 uint32_t db_offset;
15380 uint16_t pci_barset;
49198b37 15381
2e90f4b5
JS
15382 /* sanity check on queue memory */
15383 if (!hrq || !drq || !cq)
15384 return -ENODEV;
49198b37
JS
15385 if (!phba->sli4_hba.pc_sli4_params.supported)
15386 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15387
15388 if (hrq->entry_count != drq->entry_count)
15389 return -EINVAL;
15390 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15391 if (!mbox)
15392 return -ENOMEM;
15393 length = (sizeof(struct lpfc_mbx_rq_create) -
15394 sizeof(struct lpfc_sli4_cfg_mhdr));
15395 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15396 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15397 length, LPFC_SLI4_MBX_EMBED);
15398 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15399 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15400 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15401 phba->sli4_hba.pc_sli4_params.rqv);
15402 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15403 bf_set(lpfc_rq_context_rqe_count_1,
15404 &rq_create->u.request.context,
15405 hrq->entry_count);
15406 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15407 bf_set(lpfc_rq_context_rqe_size,
15408 &rq_create->u.request.context,
15409 LPFC_RQE_SIZE_8);
15410 bf_set(lpfc_rq_context_page_size,
15411 &rq_create->u.request.context,
8ea73db4 15412 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15413 } else {
15414 switch (hrq->entry_count) {
15415 default:
15416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15417 "2535 Unsupported RQ count. (%d)\n",
15418 hrq->entry_count);
4f4c1863
JS
15419 if (hrq->entry_count < 512) {
15420 status = -EINVAL;
15421 goto out;
15422 }
5a6f133e
JS
15423 /* otherwise default to smallest count (drop through) */
15424 case 512:
15425 bf_set(lpfc_rq_context_rqe_count,
15426 &rq_create->u.request.context,
15427 LPFC_RQ_RING_SIZE_512);
15428 break;
15429 case 1024:
15430 bf_set(lpfc_rq_context_rqe_count,
15431 &rq_create->u.request.context,
15432 LPFC_RQ_RING_SIZE_1024);
15433 break;
15434 case 2048:
15435 bf_set(lpfc_rq_context_rqe_count,
15436 &rq_create->u.request.context,
15437 LPFC_RQ_RING_SIZE_2048);
15438 break;
15439 case 4096:
15440 bf_set(lpfc_rq_context_rqe_count,
15441 &rq_create->u.request.context,
15442 LPFC_RQ_RING_SIZE_4096);
15443 break;
15444 }
15445 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15446 LPFC_HDR_BUF_SIZE);
4f774513
JS
15447 }
15448 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15449 cq->queue_id);
15450 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15451 hrq->page_count);
4f774513 15452 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15453 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15454 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15455 putPaddrLow(dmabuf->phys);
15456 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15457 putPaddrHigh(dmabuf->phys);
15458 }
962bc51b
JS
15459 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15460 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15461
4f774513
JS
15462 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15463 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15464 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15465 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15466 if (shdr_status || shdr_add_status || rc) {
15467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15468 "2504 RQ_CREATE mailbox failed with "
15469 "status x%x add_status x%x, mbx status x%x\n",
15470 shdr_status, shdr_add_status, rc);
15471 status = -ENXIO;
15472 goto out;
15473 }
15474 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15475 if (hrq->queue_id == 0xFFFF) {
15476 status = -ENXIO;
15477 goto out;
15478 }
962bc51b
JS
15479
15480 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15481 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15482 &rq_create->u.response);
15483 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15484 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15486 "3262 RQ [%d] doorbell format not "
15487 "supported: x%x\n", hrq->queue_id,
15488 hrq->db_format);
15489 status = -EINVAL;
15490 goto out;
15491 }
15492
15493 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15494 &rq_create->u.response);
15495 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15496 if (!bar_memmap_p) {
15497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15498 "3269 RQ[%d] failed to memmap pci "
15499 "barset:x%x\n", hrq->queue_id,
15500 pci_barset);
15501 status = -ENOMEM;
15502 goto out;
15503 }
15504
15505 db_offset = rq_create->u.response.doorbell_offset;
15506 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15507 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15509 "3270 RQ[%d] doorbell offset not "
15510 "supported: x%x\n", hrq->queue_id,
15511 db_offset);
15512 status = -EINVAL;
15513 goto out;
15514 }
15515 hrq->db_regaddr = bar_memmap_p + db_offset;
15516 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15517 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15518 "format:x%x\n", hrq->queue_id, pci_barset,
15519 db_offset, hrq->db_format);
962bc51b
JS
15520 } else {
15521 hrq->db_format = LPFC_DB_RING_FORMAT;
15522 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15523 }
4f774513 15524 hrq->type = LPFC_HRQ;
2a622bfb 15525 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15526 hrq->subtype = subtype;
15527 hrq->host_index = 0;
15528 hrq->hba_index = 0;
61f3d4bf 15529 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15530
15531 /* now create the data queue */
15532 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15533 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15534 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15535 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15536 phba->sli4_hba.pc_sli4_params.rqv);
15537 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15538 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15539 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15540 if (subtype == LPFC_NVMET)
15541 rq_create->u.request.context.buffer_size =
15542 LPFC_NVMET_DATA_BUF_SIZE;
15543 else
15544 rq_create->u.request.context.buffer_size =
15545 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15546 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15547 LPFC_RQE_SIZE_8);
15548 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15549 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15550 } else {
15551 switch (drq->entry_count) {
15552 default:
15553 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15554 "2536 Unsupported RQ count. (%d)\n",
15555 drq->entry_count);
4f4c1863
JS
15556 if (drq->entry_count < 512) {
15557 status = -EINVAL;
15558 goto out;
15559 }
5a6f133e
JS
15560 /* otherwise default to smallest count (drop through) */
15561 case 512:
15562 bf_set(lpfc_rq_context_rqe_count,
15563 &rq_create->u.request.context,
15564 LPFC_RQ_RING_SIZE_512);
15565 break;
15566 case 1024:
15567 bf_set(lpfc_rq_context_rqe_count,
15568 &rq_create->u.request.context,
15569 LPFC_RQ_RING_SIZE_1024);
15570 break;
15571 case 2048:
15572 bf_set(lpfc_rq_context_rqe_count,
15573 &rq_create->u.request.context,
15574 LPFC_RQ_RING_SIZE_2048);
15575 break;
15576 case 4096:
15577 bf_set(lpfc_rq_context_rqe_count,
15578 &rq_create->u.request.context,
15579 LPFC_RQ_RING_SIZE_4096);
15580 break;
15581 }
3c603be9
JS
15582 if (subtype == LPFC_NVMET)
15583 bf_set(lpfc_rq_context_buf_size,
15584 &rq_create->u.request.context,
15585 LPFC_NVMET_DATA_BUF_SIZE);
15586 else
15587 bf_set(lpfc_rq_context_buf_size,
15588 &rq_create->u.request.context,
15589 LPFC_DATA_BUF_SIZE);
4f774513
JS
15590 }
15591 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15592 cq->queue_id);
15593 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15594 drq->page_count);
4f774513
JS
15595 list_for_each_entry(dmabuf, &drq->page_list, list) {
15596 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15597 putPaddrLow(dmabuf->phys);
15598 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15599 putPaddrHigh(dmabuf->phys);
15600 }
962bc51b
JS
15601 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15602 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15603 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15604 /* The IOCTL status is embedded in the mailbox subheader. */
15605 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15606 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15607 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15608 if (shdr_status || shdr_add_status || rc) {
15609 status = -ENXIO;
15610 goto out;
15611 }
15612 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15613 if (drq->queue_id == 0xFFFF) {
15614 status = -ENXIO;
15615 goto out;
15616 }
15617 drq->type = LPFC_DRQ;
2a622bfb 15618 drq->assoc_qid = cq->queue_id;
4f774513
JS
15619 drq->subtype = subtype;
15620 drq->host_index = 0;
15621 drq->hba_index = 0;
61f3d4bf 15622 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15623
15624 /* link the header and data RQs onto the parent cq child list */
15625 list_add_tail(&hrq->list, &cq->child_list);
15626 list_add_tail(&drq->list, &cq->child_list);
15627
15628out:
8fa38513 15629 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15630 return status;
15631}
15632
2d7dbc4c
JS
15633/**
15634 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15635 * @phba: HBA structure that indicates port to create a queue on.
15636 * @hrqp: The queue structure array to use to create the header receive queues.
15637 * @drqp: The queue structure array to use to create the data receive queues.
15638 * @cqp: The completion queue array to bind these receive queues to.
15639 *
15640 * This function creates a receive buffer queue pair , as detailed in @hrq and
15641 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15642 * to the HBA.
15643 *
15644 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15645 * struct is used to get the entry count that is necessary to determine the
15646 * number of pages to use for this queue. The @cq is used to indicate which
15647 * completion queue to bind received buffers that are posted to these queues to.
15648 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15649 * receive queue pair. This function is asynchronous and will wait for the
15650 * mailbox command to finish before continuing.
15651 *
15652 * On success this function will return a zero. If unable to allocate enough
15653 * memory this function will return -ENOMEM. If the queue create mailbox command
15654 * fails this function will return -ENXIO.
15655 **/
15656int
15657lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15658 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15659 uint32_t subtype)
15660{
15661 struct lpfc_queue *hrq, *drq, *cq;
15662 struct lpfc_mbx_rq_create_v2 *rq_create;
15663 struct lpfc_dmabuf *dmabuf;
15664 LPFC_MBOXQ_t *mbox;
15665 int rc, length, alloclen, status = 0;
15666 int cnt, idx, numrq, page_idx = 0;
15667 uint32_t shdr_status, shdr_add_status;
15668 union lpfc_sli4_cfg_shdr *shdr;
15669 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15670
15671 numrq = phba->cfg_nvmet_mrq;
15672 /* sanity check on array memory */
15673 if (!hrqp || !drqp || !cqp || !numrq)
15674 return -ENODEV;
15675 if (!phba->sli4_hba.pc_sli4_params.supported)
15676 hw_page_size = SLI4_PAGE_SIZE;
15677
15678 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15679 if (!mbox)
15680 return -ENOMEM;
15681
15682 length = sizeof(struct lpfc_mbx_rq_create_v2);
15683 length += ((2 * numrq * hrqp[0]->page_count) *
15684 sizeof(struct dma_address));
15685
15686 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15687 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15688 LPFC_SLI4_MBX_NEMBED);
15689 if (alloclen < length) {
15690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15691 "3099 Allocated DMA memory size (%d) is "
15692 "less than the requested DMA memory size "
15693 "(%d)\n", alloclen, length);
15694 status = -ENOMEM;
15695 goto out;
15696 }
15697
15698
15699
15700 rq_create = mbox->sge_array->addr[0];
15701 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15702
15703 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15704 cnt = 0;
15705
15706 for (idx = 0; idx < numrq; idx++) {
15707 hrq = hrqp[idx];
15708 drq = drqp[idx];
15709 cq = cqp[idx];
15710
2d7dbc4c
JS
15711 /* sanity check on queue memory */
15712 if (!hrq || !drq || !cq) {
15713 status = -ENODEV;
15714 goto out;
15715 }
15716
7aabe84b
JS
15717 if (hrq->entry_count != drq->entry_count) {
15718 status = -EINVAL;
15719 goto out;
15720 }
15721
2d7dbc4c
JS
15722 if (idx == 0) {
15723 bf_set(lpfc_mbx_rq_create_num_pages,
15724 &rq_create->u.request,
15725 hrq->page_count);
15726 bf_set(lpfc_mbx_rq_create_rq_cnt,
15727 &rq_create->u.request, (numrq * 2));
15728 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15729 1);
15730 bf_set(lpfc_rq_context_base_cq,
15731 &rq_create->u.request.context,
15732 cq->queue_id);
15733 bf_set(lpfc_rq_context_data_size,
15734 &rq_create->u.request.context,
3c603be9 15735 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15736 bf_set(lpfc_rq_context_hdr_size,
15737 &rq_create->u.request.context,
15738 LPFC_HDR_BUF_SIZE);
15739 bf_set(lpfc_rq_context_rqe_count_1,
15740 &rq_create->u.request.context,
15741 hrq->entry_count);
15742 bf_set(lpfc_rq_context_rqe_size,
15743 &rq_create->u.request.context,
15744 LPFC_RQE_SIZE_8);
15745 bf_set(lpfc_rq_context_page_size,
15746 &rq_create->u.request.context,
15747 (PAGE_SIZE/SLI4_PAGE_SIZE));
15748 }
15749 rc = 0;
15750 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15751 memset(dmabuf->virt, 0, hw_page_size);
15752 cnt = page_idx + dmabuf->buffer_tag;
15753 rq_create->u.request.page[cnt].addr_lo =
15754 putPaddrLow(dmabuf->phys);
15755 rq_create->u.request.page[cnt].addr_hi =
15756 putPaddrHigh(dmabuf->phys);
15757 rc++;
15758 }
15759 page_idx += rc;
15760
15761 rc = 0;
15762 list_for_each_entry(dmabuf, &drq->page_list, list) {
15763 memset(dmabuf->virt, 0, hw_page_size);
15764 cnt = page_idx + dmabuf->buffer_tag;
15765 rq_create->u.request.page[cnt].addr_lo =
15766 putPaddrLow(dmabuf->phys);
15767 rq_create->u.request.page[cnt].addr_hi =
15768 putPaddrHigh(dmabuf->phys);
15769 rc++;
15770 }
15771 page_idx += rc;
15772
15773 hrq->db_format = LPFC_DB_RING_FORMAT;
15774 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15775 hrq->type = LPFC_HRQ;
15776 hrq->assoc_qid = cq->queue_id;
15777 hrq->subtype = subtype;
15778 hrq->host_index = 0;
15779 hrq->hba_index = 0;
61f3d4bf 15780 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15781
15782 drq->db_format = LPFC_DB_RING_FORMAT;
15783 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15784 drq->type = LPFC_DRQ;
15785 drq->assoc_qid = cq->queue_id;
15786 drq->subtype = subtype;
15787 drq->host_index = 0;
15788 drq->hba_index = 0;
61f3d4bf 15789 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15790
15791 list_add_tail(&hrq->list, &cq->child_list);
15792 list_add_tail(&drq->list, &cq->child_list);
15793 }
15794
15795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15796 /* The IOCTL status is embedded in the mailbox subheader. */
15797 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15798 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15799 if (shdr_status || shdr_add_status || rc) {
15800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15801 "3120 RQ_CREATE mailbox failed with "
15802 "status x%x add_status x%x, mbx status x%x\n",
15803 shdr_status, shdr_add_status, rc);
15804 status = -ENXIO;
15805 goto out;
15806 }
15807 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15808 if (rc == 0xFFFF) {
15809 status = -ENXIO;
15810 goto out;
15811 }
15812
15813 /* Initialize all RQs with associated queue id */
15814 for (idx = 0; idx < numrq; idx++) {
15815 hrq = hrqp[idx];
15816 hrq->queue_id = rc + (2 * idx);
15817 drq = drqp[idx];
15818 drq->queue_id = rc + (2 * idx) + 1;
15819 }
15820
15821out:
15822 lpfc_sli4_mbox_cmd_free(phba, mbox);
15823 return status;
15824}
15825
4f774513
JS
15826/**
15827 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15828 * @eq: The queue structure associated with the queue to destroy.
15829 *
15830 * This function destroys a queue, as detailed in @eq by sending an mailbox
15831 * command, specific to the type of queue, to the HBA.
15832 *
15833 * The @eq struct is used to get the queue ID of the queue to destroy.
15834 *
15835 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15836 * command fails this function will return -ENXIO.
4f774513 15837 **/
a2fc4aef 15838int
4f774513
JS
15839lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15840{
15841 LPFC_MBOXQ_t *mbox;
15842 int rc, length, status = 0;
15843 uint32_t shdr_status, shdr_add_status;
15844 union lpfc_sli4_cfg_shdr *shdr;
15845
2e90f4b5 15846 /* sanity check on queue memory */
4f774513
JS
15847 if (!eq)
15848 return -ENODEV;
15849 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15850 if (!mbox)
15851 return -ENOMEM;
15852 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15853 sizeof(struct lpfc_sli4_cfg_mhdr));
15854 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15855 LPFC_MBOX_OPCODE_EQ_DESTROY,
15856 length, LPFC_SLI4_MBX_EMBED);
15857 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15858 eq->queue_id);
15859 mbox->vport = eq->phba->pport;
15860 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15861
15862 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15863 /* The IOCTL status is embedded in the mailbox subheader. */
15864 shdr = (union lpfc_sli4_cfg_shdr *)
15865 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15868 if (shdr_status || shdr_add_status || rc) {
15869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15870 "2505 EQ_DESTROY mailbox failed with "
15871 "status x%x add_status x%x, mbx status x%x\n",
15872 shdr_status, shdr_add_status, rc);
15873 status = -ENXIO;
15874 }
15875
15876 /* Remove eq from any list */
15877 list_del_init(&eq->list);
8fa38513 15878 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15879 return status;
15880}
15881
15882/**
15883 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15884 * @cq: The queue structure associated with the queue to destroy.
15885 *
15886 * This function destroys a queue, as detailed in @cq by sending an mailbox
15887 * command, specific to the type of queue, to the HBA.
15888 *
15889 * The @cq struct is used to get the queue ID of the queue to destroy.
15890 *
15891 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15892 * command fails this function will return -ENXIO.
4f774513 15893 **/
a2fc4aef 15894int
4f774513
JS
15895lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15896{
15897 LPFC_MBOXQ_t *mbox;
15898 int rc, length, status = 0;
15899 uint32_t shdr_status, shdr_add_status;
15900 union lpfc_sli4_cfg_shdr *shdr;
15901
2e90f4b5 15902 /* sanity check on queue memory */
4f774513
JS
15903 if (!cq)
15904 return -ENODEV;
15905 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15906 if (!mbox)
15907 return -ENOMEM;
15908 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15909 sizeof(struct lpfc_sli4_cfg_mhdr));
15910 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15911 LPFC_MBOX_OPCODE_CQ_DESTROY,
15912 length, LPFC_SLI4_MBX_EMBED);
15913 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15914 cq->queue_id);
15915 mbox->vport = cq->phba->pport;
15916 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15917 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15918 /* The IOCTL status is embedded in the mailbox subheader. */
15919 shdr = (union lpfc_sli4_cfg_shdr *)
15920 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15921 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15922 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15923 if (shdr_status || shdr_add_status || rc) {
15924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15925 "2506 CQ_DESTROY mailbox failed with "
15926 "status x%x add_status x%x, mbx status x%x\n",
15927 shdr_status, shdr_add_status, rc);
15928 status = -ENXIO;
15929 }
15930 /* Remove cq from any list */
15931 list_del_init(&cq->list);
8fa38513 15932 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15933 return status;
15934}
15935
04c68496
JS
15936/**
15937 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15938 * @qm: The queue structure associated with the queue to destroy.
15939 *
15940 * This function destroys a queue, as detailed in @mq by sending an mailbox
15941 * command, specific to the type of queue, to the HBA.
15942 *
15943 * The @mq struct is used to get the queue ID of the queue to destroy.
15944 *
15945 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15946 * command fails this function will return -ENXIO.
04c68496 15947 **/
a2fc4aef 15948int
04c68496
JS
15949lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15950{
15951 LPFC_MBOXQ_t *mbox;
15952 int rc, length, status = 0;
15953 uint32_t shdr_status, shdr_add_status;
15954 union lpfc_sli4_cfg_shdr *shdr;
15955
2e90f4b5 15956 /* sanity check on queue memory */
04c68496
JS
15957 if (!mq)
15958 return -ENODEV;
15959 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15960 if (!mbox)
15961 return -ENOMEM;
15962 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15963 sizeof(struct lpfc_sli4_cfg_mhdr));
15964 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15965 LPFC_MBOX_OPCODE_MQ_DESTROY,
15966 length, LPFC_SLI4_MBX_EMBED);
15967 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15968 mq->queue_id);
15969 mbox->vport = mq->phba->pport;
15970 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15971 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15972 /* The IOCTL status is embedded in the mailbox subheader. */
15973 shdr = (union lpfc_sli4_cfg_shdr *)
15974 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15975 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15976 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15977 if (shdr_status || shdr_add_status || rc) {
15978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15979 "2507 MQ_DESTROY mailbox failed with "
15980 "status x%x add_status x%x, mbx status x%x\n",
15981 shdr_status, shdr_add_status, rc);
15982 status = -ENXIO;
15983 }
15984 /* Remove mq from any list */
15985 list_del_init(&mq->list);
8fa38513 15986 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15987 return status;
15988}
15989
4f774513
JS
15990/**
15991 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15992 * @wq: The queue structure associated with the queue to destroy.
15993 *
15994 * This function destroys a queue, as detailed in @wq by sending an mailbox
15995 * command, specific to the type of queue, to the HBA.
15996 *
15997 * The @wq struct is used to get the queue ID of the queue to destroy.
15998 *
15999 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16000 * command fails this function will return -ENXIO.
4f774513 16001 **/
a2fc4aef 16002int
4f774513
JS
16003lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16004{
16005 LPFC_MBOXQ_t *mbox;
16006 int rc, length, status = 0;
16007 uint32_t shdr_status, shdr_add_status;
16008 union lpfc_sli4_cfg_shdr *shdr;
16009
2e90f4b5 16010 /* sanity check on queue memory */
4f774513
JS
16011 if (!wq)
16012 return -ENODEV;
16013 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16014 if (!mbox)
16015 return -ENOMEM;
16016 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16017 sizeof(struct lpfc_sli4_cfg_mhdr));
16018 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16019 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16020 length, LPFC_SLI4_MBX_EMBED);
16021 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16022 wq->queue_id);
16023 mbox->vport = wq->phba->pport;
16024 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16025 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16026 shdr = (union lpfc_sli4_cfg_shdr *)
16027 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16028 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16029 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16030 if (shdr_status || shdr_add_status || rc) {
16031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16032 "2508 WQ_DESTROY mailbox failed with "
16033 "status x%x add_status x%x, mbx status x%x\n",
16034 shdr_status, shdr_add_status, rc);
16035 status = -ENXIO;
16036 }
16037 /* Remove wq from any list */
16038 list_del_init(&wq->list);
d1f525aa
JS
16039 kfree(wq->pring);
16040 wq->pring = NULL;
8fa38513 16041 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
16042 return status;
16043}
16044
16045/**
16046 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16047 * @rq: The queue structure associated with the queue to destroy.
16048 *
16049 * This function destroys a queue, as detailed in @rq by sending an mailbox
16050 * command, specific to the type of queue, to the HBA.
16051 *
16052 * The @rq struct is used to get the queue ID of the queue to destroy.
16053 *
16054 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16055 * command fails this function will return -ENXIO.
4f774513 16056 **/
a2fc4aef 16057int
4f774513
JS
16058lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16059 struct lpfc_queue *drq)
16060{
16061 LPFC_MBOXQ_t *mbox;
16062 int rc, length, status = 0;
16063 uint32_t shdr_status, shdr_add_status;
16064 union lpfc_sli4_cfg_shdr *shdr;
16065
2e90f4b5 16066 /* sanity check on queue memory */
4f774513
JS
16067 if (!hrq || !drq)
16068 return -ENODEV;
16069 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16070 if (!mbox)
16071 return -ENOMEM;
16072 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 16073 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
16074 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16075 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16076 length, LPFC_SLI4_MBX_EMBED);
16077 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16078 hrq->queue_id);
16079 mbox->vport = hrq->phba->pport;
16080 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16081 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16082 /* The IOCTL status is embedded in the mailbox subheader. */
16083 shdr = (union lpfc_sli4_cfg_shdr *)
16084 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16085 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16086 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16087 if (shdr_status || shdr_add_status || rc) {
16088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16089 "2509 RQ_DESTROY mailbox failed with "
16090 "status x%x add_status x%x, mbx status x%x\n",
16091 shdr_status, shdr_add_status, rc);
16092 if (rc != MBX_TIMEOUT)
16093 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16094 return -ENXIO;
16095 }
16096 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16097 drq->queue_id);
16098 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16099 shdr = (union lpfc_sli4_cfg_shdr *)
16100 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16101 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16102 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16103 if (shdr_status || shdr_add_status || rc) {
16104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16105 "2510 RQ_DESTROY mailbox failed with "
16106 "status x%x add_status x%x, mbx status x%x\n",
16107 shdr_status, shdr_add_status, rc);
16108 status = -ENXIO;
16109 }
16110 list_del_init(&hrq->list);
16111 list_del_init(&drq->list);
8fa38513 16112 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
16113 return status;
16114}
16115
16116/**
16117 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16118 * @phba: The virtual port for which this call being executed.
16119 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16120 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16121 * @xritag: the xritag that ties this io to the SGL pages.
16122 *
16123 * This routine will post the sgl pages for the IO that has the xritag
16124 * that is in the iocbq structure. The xritag is assigned during iocbq
16125 * creation and persists for as long as the driver is loaded.
16126 * if the caller has fewer than 256 scatter gather segments to map then
16127 * pdma_phys_addr1 should be 0.
16128 * If the caller needs to map more than 256 scatter gather segment then
16129 * pdma_phys_addr1 should be a valid physical address.
16130 * physical address for SGLs must be 64 byte aligned.
16131 * If you are going to map 2 SGL's then the first one must have 256 entries
16132 * the second sgl can have between 1 and 256 entries.
16133 *
16134 * Return codes:
16135 * 0 - Success
16136 * -ENXIO, -ENOMEM - Failure
16137 **/
16138int
16139lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16140 dma_addr_t pdma_phys_addr0,
16141 dma_addr_t pdma_phys_addr1,
16142 uint16_t xritag)
16143{
16144 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16145 LPFC_MBOXQ_t *mbox;
16146 int rc;
16147 uint32_t shdr_status, shdr_add_status;
6d368e53 16148 uint32_t mbox_tmo;
4f774513
JS
16149 union lpfc_sli4_cfg_shdr *shdr;
16150
16151 if (xritag == NO_XRI) {
16152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16153 "0364 Invalid param:\n");
16154 return -EINVAL;
16155 }
16156
16157 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16158 if (!mbox)
16159 return -ENOMEM;
16160
16161 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16162 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16163 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 16164 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
16165
16166 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16167 &mbox->u.mqe.un.post_sgl_pages;
16168 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16169 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16170
16171 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16172 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16173 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16174 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16175
16176 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16177 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16178 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16179 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16180 if (!phba->sli4_hba.intr_enable)
16181 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 16182 else {
a183a15f 16183 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
16184 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16185 }
4f774513
JS
16186 /* The IOCTL status is embedded in the mailbox subheader. */
16187 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16188 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16189 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16190 if (rc != MBX_TIMEOUT)
16191 mempool_free(mbox, phba->mbox_mem_pool);
16192 if (shdr_status || shdr_add_status || rc) {
16193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16194 "2511 POST_SGL mailbox failed with "
16195 "status x%x add_status x%x, mbx status x%x\n",
16196 shdr_status, shdr_add_status, rc);
4f774513
JS
16197 }
16198 return 0;
16199}
4f774513 16200
6d368e53 16201/**
88a2cfbb 16202 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
16203 * @phba: pointer to lpfc hba data structure.
16204 *
16205 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
16206 * HBA consistent with the SLI-4 interface spec. This routine
16207 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16208 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 16209 *
88a2cfbb
JS
16210 * Returns
16211 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16212 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16213 **/
5d8b8167 16214static uint16_t
6d368e53
JS
16215lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16216{
16217 unsigned long xri;
16218
16219 /*
16220 * Fetch the next logical xri. Because this index is logical,
16221 * the driver starts at 0 each time.
16222 */
16223 spin_lock_irq(&phba->hbalock);
16224 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16225 phba->sli4_hba.max_cfg_param.max_xri, 0);
16226 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16227 spin_unlock_irq(&phba->hbalock);
16228 return NO_XRI;
16229 } else {
16230 set_bit(xri, phba->sli4_hba.xri_bmask);
16231 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 16232 }
6d368e53
JS
16233 spin_unlock_irq(&phba->hbalock);
16234 return xri;
16235}
16236
16237/**
16238 * lpfc_sli4_free_xri - Release an xri for reuse.
16239 * @phba: pointer to lpfc hba data structure.
16240 *
16241 * This routine is invoked to release an xri to the pool of
16242 * available rpis maintained by the driver.
16243 **/
5d8b8167 16244static void
6d368e53
JS
16245__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16246{
16247 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
16248 phba->sli4_hba.max_cfg_param.xri_used--;
16249 }
16250}
16251
16252/**
16253 * lpfc_sli4_free_xri - Release an xri for reuse.
16254 * @phba: pointer to lpfc hba data structure.
16255 *
16256 * This routine is invoked to release an xri to the pool of
16257 * available rpis maintained by the driver.
16258 **/
16259void
16260lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16261{
16262 spin_lock_irq(&phba->hbalock);
16263 __lpfc_sli4_free_xri(phba, xri);
16264 spin_unlock_irq(&phba->hbalock);
16265}
16266
4f774513
JS
16267/**
16268 * lpfc_sli4_next_xritag - Get an xritag for the io
16269 * @phba: Pointer to HBA context object.
16270 *
16271 * This function gets an xritag for the iocb. If there is no unused xritag
16272 * it will return 0xffff.
16273 * The function returns the allocated xritag if successful, else returns zero.
16274 * Zero is not a valid xritag.
16275 * The caller is not required to hold any lock.
16276 **/
16277uint16_t
16278lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16279{
6d368e53 16280 uint16_t xri_index;
4f774513 16281
6d368e53 16282 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16283 if (xri_index == NO_XRI)
16284 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16285 "2004 Failed to allocate XRI.last XRITAG is %d"
16286 " Max XRI is %d, Used XRI is %d\n",
16287 xri_index,
16288 phba->sli4_hba.max_cfg_param.max_xri,
16289 phba->sli4_hba.max_cfg_param.xri_used);
16290 return xri_index;
4f774513
JS
16291}
16292
16293/**
895427bd 16294 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16295 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16296 * @post_sgl_list: pointer to els sgl entry list.
16297 * @count: number of els sgl entries on the list.
4f774513
JS
16298 *
16299 * This routine is invoked to post a block of driver's sgl pages to the
16300 * HBA using non-embedded mailbox command. No Lock is held. This routine
16301 * is only called when the driver is loading and after all IO has been
16302 * stopped.
16303 **/
8a9d2e80 16304static int
895427bd 16305lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16306 struct list_head *post_sgl_list,
16307 int post_cnt)
4f774513 16308{
8a9d2e80 16309 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16310 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16311 struct sgl_page_pairs *sgl_pg_pairs;
16312 void *viraddr;
16313 LPFC_MBOXQ_t *mbox;
16314 uint32_t reqlen, alloclen, pg_pairs;
16315 uint32_t mbox_tmo;
8a9d2e80
JS
16316 uint16_t xritag_start = 0;
16317 int rc = 0;
4f774513
JS
16318 uint32_t shdr_status, shdr_add_status;
16319 union lpfc_sli4_cfg_shdr *shdr;
16320
895427bd 16321 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16322 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16323 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16325 "2559 Block sgl registration required DMA "
16326 "size (%d) great than a page\n", reqlen);
16327 return -ENOMEM;
16328 }
895427bd 16329
4f774513 16330 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16331 if (!mbox)
4f774513 16332 return -ENOMEM;
4f774513
JS
16333
16334 /* Allocate DMA memory and set up the non-embedded mailbox command */
16335 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16336 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16337 LPFC_SLI4_MBX_NEMBED);
16338
16339 if (alloclen < reqlen) {
16340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16341 "0285 Allocated DMA memory size (%d) is "
16342 "less than the requested DMA memory "
16343 "size (%d)\n", alloclen, reqlen);
16344 lpfc_sli4_mbox_cmd_free(phba, mbox);
16345 return -ENOMEM;
16346 }
4f774513 16347 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16348 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16349 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16350 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16351
8a9d2e80
JS
16352 pg_pairs = 0;
16353 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16354 /* Set up the sge entry */
16355 sgl_pg_pairs->sgl_pg0_addr_lo =
16356 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16357 sgl_pg_pairs->sgl_pg0_addr_hi =
16358 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16359 sgl_pg_pairs->sgl_pg1_addr_lo =
16360 cpu_to_le32(putPaddrLow(0));
16361 sgl_pg_pairs->sgl_pg1_addr_hi =
16362 cpu_to_le32(putPaddrHigh(0));
6d368e53 16363
4f774513
JS
16364 /* Keep the first xritag on the list */
16365 if (pg_pairs == 0)
16366 xritag_start = sglq_entry->sli4_xritag;
16367 sgl_pg_pairs++;
8a9d2e80 16368 pg_pairs++;
4f774513 16369 }
6d368e53
JS
16370
16371 /* Complete initialization and perform endian conversion. */
4f774513 16372 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16373 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16374 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16375
4f774513
JS
16376 if (!phba->sli4_hba.intr_enable)
16377 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16378 else {
a183a15f 16379 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16380 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16381 }
16382 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16383 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16384 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16385 if (rc != MBX_TIMEOUT)
16386 lpfc_sli4_mbox_cmd_free(phba, mbox);
16387 if (shdr_status || shdr_add_status || rc) {
16388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16389 "2513 POST_SGL_BLOCK mailbox command failed "
16390 "status x%x add_status x%x mbx status x%x\n",
16391 shdr_status, shdr_add_status, rc);
16392 rc = -ENXIO;
16393 }
16394 return rc;
16395}
16396
16397/**
16398 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16399 * @phba: pointer to lpfc hba data structure.
16400 * @sblist: pointer to scsi buffer list.
16401 * @count: number of scsi buffers on the list.
16402 *
16403 * This routine is invoked to post a block of @count scsi sgl pages from a
16404 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16405 * No Lock is held.
16406 *
16407 **/
16408int
8a9d2e80
JS
16409lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16410 struct list_head *sblist,
16411 int count)
4f774513
JS
16412{
16413 struct lpfc_scsi_buf *psb;
16414 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16415 struct sgl_page_pairs *sgl_pg_pairs;
16416 void *viraddr;
16417 LPFC_MBOXQ_t *mbox;
16418 uint32_t reqlen, alloclen, pg_pairs;
16419 uint32_t mbox_tmo;
16420 uint16_t xritag_start = 0;
16421 int rc = 0;
16422 uint32_t shdr_status, shdr_add_status;
16423 dma_addr_t pdma_phys_bpl1;
16424 union lpfc_sli4_cfg_shdr *shdr;
16425
16426 /* Calculate the requested length of the dma memory */
8a9d2e80 16427 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16428 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16429 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16430 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16431 "0217 Block sgl registration required DMA "
16432 "size (%d) great than a page\n", reqlen);
16433 return -ENOMEM;
16434 }
16435 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16436 if (!mbox) {
16437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16438 "0283 Failed to allocate mbox cmd memory\n");
16439 return -ENOMEM;
16440 }
16441
16442 /* Allocate DMA memory and set up the non-embedded mailbox command */
16443 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16444 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16445 LPFC_SLI4_MBX_NEMBED);
16446
16447 if (alloclen < reqlen) {
16448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16449 "2561 Allocated DMA memory size (%d) is "
16450 "less than the requested DMA memory "
16451 "size (%d)\n", alloclen, reqlen);
16452 lpfc_sli4_mbox_cmd_free(phba, mbox);
16453 return -ENOMEM;
16454 }
6d368e53 16455
4f774513 16456 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16457 viraddr = mbox->sge_array->addr[0];
16458
16459 /* Set up the SGL pages in the non-embedded DMA pages */
16460 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16461 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16462
16463 pg_pairs = 0;
16464 list_for_each_entry(psb, sblist, list) {
16465 /* Set up the sge entry */
16466 sgl_pg_pairs->sgl_pg0_addr_lo =
16467 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16468 sgl_pg_pairs->sgl_pg0_addr_hi =
16469 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16470 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16471 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16472 else
16473 pdma_phys_bpl1 = 0;
16474 sgl_pg_pairs->sgl_pg1_addr_lo =
16475 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16476 sgl_pg_pairs->sgl_pg1_addr_hi =
16477 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16478 /* Keep the first xritag on the list */
16479 if (pg_pairs == 0)
16480 xritag_start = psb->cur_iocbq.sli4_xritag;
16481 sgl_pg_pairs++;
16482 pg_pairs++;
16483 }
16484 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16485 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16486 /* Perform endian conversion if necessary */
16487 sgl->word0 = cpu_to_le32(sgl->word0);
16488
16489 if (!phba->sli4_hba.intr_enable)
16490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16491 else {
a183a15f 16492 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16493 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16494 }
16495 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16496 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16497 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16498 if (rc != MBX_TIMEOUT)
16499 lpfc_sli4_mbox_cmd_free(phba, mbox);
16500 if (shdr_status || shdr_add_status || rc) {
16501 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16502 "2564 POST_SGL_BLOCK mailbox command failed "
16503 "status x%x add_status x%x mbx status x%x\n",
16504 shdr_status, shdr_add_status, rc);
16505 rc = -ENXIO;
16506 }
16507 return rc;
16508}
16509
16510/**
16511 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16512 * @phba: pointer to lpfc_hba struct that the frame was received on
16513 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16514 *
16515 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16516 * valid type of frame that the LPFC driver will handle. This function will
16517 * return a zero if the frame is a valid frame or a non zero value when the
16518 * frame does not pass the check.
16519 **/
16520static int
16521lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16522{
474ffb74 16523 /* make rctl_names static to save stack space */
4f774513 16524 struct fc_vft_header *fc_vft_hdr;
546fc854 16525 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16526
ae9e28f3
JS
16527#define FC_RCTL_MDS_DIAGS 0xF4
16528
4f774513
JS
16529 switch (fc_hdr->fh_r_ctl) {
16530 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16531 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16532 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16533 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16534 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16535 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16536 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16537 case FC_RCTL_DD_CMD_STATUS: /* command status */
16538 case FC_RCTL_ELS_REQ: /* extended link services request */
16539 case FC_RCTL_ELS_REP: /* extended link services reply */
16540 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16541 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16542 case FC_RCTL_BA_NOP: /* basic link service NOP */
16543 case FC_RCTL_BA_ABTS: /* basic link service abort */
16544 case FC_RCTL_BA_RMC: /* remove connection */
16545 case FC_RCTL_BA_ACC: /* basic accept */
16546 case FC_RCTL_BA_RJT: /* basic reject */
16547 case FC_RCTL_BA_PRMT:
16548 case FC_RCTL_ACK_1: /* acknowledge_1 */
16549 case FC_RCTL_ACK_0: /* acknowledge_0 */
16550 case FC_RCTL_P_RJT: /* port reject */
16551 case FC_RCTL_F_RJT: /* fabric reject */
16552 case FC_RCTL_P_BSY: /* port busy */
16553 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16554 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16555 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16556 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16557 case FC_RCTL_END: /* end */
16558 break;
16559 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16560 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16561 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16562 return lpfc_fc_frame_check(phba, fc_hdr);
16563 default:
16564 goto drop;
16565 }
ae9e28f3
JS
16566
16567#define FC_TYPE_VENDOR_UNIQUE 0xFF
16568
4f774513
JS
16569 switch (fc_hdr->fh_type) {
16570 case FC_TYPE_BLS:
16571 case FC_TYPE_ELS:
16572 case FC_TYPE_FCP:
16573 case FC_TYPE_CT:
895427bd 16574 case FC_TYPE_NVME:
ae9e28f3 16575 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16576 break;
16577 case FC_TYPE_IP:
16578 case FC_TYPE_ILS:
16579 default:
16580 goto drop;
16581 }
546fc854 16582
4f774513 16583 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16584 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16585 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16586 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16587 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16588 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16589 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16590 be32_to_cpu(header[6]));
4f774513
JS
16591 return 0;
16592drop:
16593 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16594 "2539 Dropped frame rctl:x%x type:x%x\n",
16595 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16596 return 1;
16597}
16598
16599/**
16600 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16601 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16602 *
16603 * This function processes the FC header to retrieve the VFI from the VF
16604 * header, if one exists. This function will return the VFI if one exists
16605 * or 0 if no VSAN Header exists.
16606 **/
16607static uint32_t
16608lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16609{
16610 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16611
16612 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16613 return 0;
16614 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16615}
16616
16617/**
16618 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16619 * @phba: Pointer to the HBA structure to search for the vport on
16620 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16621 * @fcfi: The FC Fabric ID that the frame came from
16622 *
16623 * This function searches the @phba for a vport that matches the content of the
16624 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16625 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16626 * returns the matching vport pointer or NULL if unable to match frame to a
16627 * vport.
16628 **/
16629static struct lpfc_vport *
16630lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16631 uint16_t fcfi, uint32_t did)
4f774513
JS
16632{
16633 struct lpfc_vport **vports;
16634 struct lpfc_vport *vport = NULL;
16635 int i;
939723a4 16636
bf08611b
JS
16637 if (did == Fabric_DID)
16638 return phba->pport;
939723a4
JS
16639 if ((phba->pport->fc_flag & FC_PT2PT) &&
16640 !(phba->link_state == LPFC_HBA_READY))
16641 return phba->pport;
16642
4f774513 16643 vports = lpfc_create_vport_work_array(phba);
895427bd 16644 if (vports != NULL) {
4f774513
JS
16645 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16646 if (phba->fcf.fcfi == fcfi &&
16647 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16648 vports[i]->fc_myDID == did) {
16649 vport = vports[i];
16650 break;
16651 }
16652 }
895427bd 16653 }
4f774513
JS
16654 lpfc_destroy_vport_work_array(phba, vports);
16655 return vport;
16656}
16657
45ed1190
JS
16658/**
16659 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16660 * @vport: The vport to work on.
16661 *
16662 * This function updates the receive sequence time stamp for this vport. The
16663 * receive sequence time stamp indicates the time that the last frame of the
16664 * the sequence that has been idle for the longest amount of time was received.
16665 * the driver uses this time stamp to indicate if any received sequences have
16666 * timed out.
16667 **/
5d8b8167 16668static void
45ed1190
JS
16669lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16670{
16671 struct lpfc_dmabuf *h_buf;
16672 struct hbq_dmabuf *dmabuf = NULL;
16673
16674 /* get the oldest sequence on the rcv list */
16675 h_buf = list_get_first(&vport->rcv_buffer_list,
16676 struct lpfc_dmabuf, list);
16677 if (!h_buf)
16678 return;
16679 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16680 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16681}
16682
16683/**
16684 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16685 * @vport: The vport that the received sequences were sent to.
16686 *
16687 * This function cleans up all outstanding received sequences. This is called
16688 * by the driver when a link event or user action invalidates all the received
16689 * sequences.
16690 **/
16691void
16692lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16693{
16694 struct lpfc_dmabuf *h_buf, *hnext;
16695 struct lpfc_dmabuf *d_buf, *dnext;
16696 struct hbq_dmabuf *dmabuf = NULL;
16697
16698 /* start with the oldest sequence on the rcv list */
16699 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16700 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16701 list_del_init(&dmabuf->hbuf.list);
16702 list_for_each_entry_safe(d_buf, dnext,
16703 &dmabuf->dbuf.list, list) {
16704 list_del_init(&d_buf->list);
16705 lpfc_in_buf_free(vport->phba, d_buf);
16706 }
16707 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16708 }
16709}
16710
16711/**
16712 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16713 * @vport: The vport that the received sequences were sent to.
16714 *
16715 * This function determines whether any received sequences have timed out by
16716 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16717 * indicates that there is at least one timed out sequence this routine will
16718 * go through the received sequences one at a time from most inactive to most
16719 * active to determine which ones need to be cleaned up. Once it has determined
16720 * that a sequence needs to be cleaned up it will simply free up the resources
16721 * without sending an abort.
16722 **/
16723void
16724lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16725{
16726 struct lpfc_dmabuf *h_buf, *hnext;
16727 struct lpfc_dmabuf *d_buf, *dnext;
16728 struct hbq_dmabuf *dmabuf = NULL;
16729 unsigned long timeout;
16730 int abort_count = 0;
16731
16732 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16733 vport->rcv_buffer_time_stamp);
16734 if (list_empty(&vport->rcv_buffer_list) ||
16735 time_before(jiffies, timeout))
16736 return;
16737 /* start with the oldest sequence on the rcv list */
16738 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16739 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16740 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16741 dmabuf->time_stamp);
16742 if (time_before(jiffies, timeout))
16743 break;
16744 abort_count++;
16745 list_del_init(&dmabuf->hbuf.list);
16746 list_for_each_entry_safe(d_buf, dnext,
16747 &dmabuf->dbuf.list, list) {
16748 list_del_init(&d_buf->list);
16749 lpfc_in_buf_free(vport->phba, d_buf);
16750 }
16751 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16752 }
16753 if (abort_count)
16754 lpfc_update_rcv_time_stamp(vport);
16755}
16756
4f774513
JS
16757/**
16758 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16759 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16760 *
16761 * This function searches through the existing incomplete sequences that have
16762 * been sent to this @vport. If the frame matches one of the incomplete
16763 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16764 * make up that sequence. If no sequence is found that matches this frame then
16765 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16766 * This function returns a pointer to the first dmabuf in the sequence list that
16767 * the frame was linked to.
16768 **/
16769static struct hbq_dmabuf *
16770lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16771{
16772 struct fc_frame_header *new_hdr;
16773 struct fc_frame_header *temp_hdr;
16774 struct lpfc_dmabuf *d_buf;
16775 struct lpfc_dmabuf *h_buf;
16776 struct hbq_dmabuf *seq_dmabuf = NULL;
16777 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16778 uint8_t found = 0;
4f774513 16779
4d9ab994 16780 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16781 dmabuf->time_stamp = jiffies;
4f774513 16782 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16783
4f774513
JS
16784 /* Use the hdr_buf to find the sequence that this frame belongs to */
16785 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16786 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16787 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16788 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16789 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16790 continue;
16791 /* found a pending sequence that matches this frame */
16792 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16793 break;
16794 }
16795 if (!seq_dmabuf) {
16796 /*
16797 * This indicates first frame received for this sequence.
16798 * Queue the buffer on the vport's rcv_buffer_list.
16799 */
16800 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16801 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16802 return dmabuf;
16803 }
16804 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16805 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16806 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16807 list_del_init(&seq_dmabuf->hbuf.list);
16808 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16809 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16810 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16811 return dmabuf;
16812 }
45ed1190
JS
16813 /* move this sequence to the tail to indicate a young sequence */
16814 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16815 seq_dmabuf->time_stamp = jiffies;
16816 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16817 if (list_empty(&seq_dmabuf->dbuf.list)) {
16818 temp_hdr = dmabuf->hbuf.virt;
16819 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16820 return seq_dmabuf;
16821 }
4f774513 16822 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16823 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16824 while (!found) {
4f774513
JS
16825 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16826 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16827 /*
16828 * If the frame's sequence count is greater than the frame on
16829 * the list then insert the frame right after this frame
16830 */
eeead811
JS
16831 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16832 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16833 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16834 found = 1;
16835 break;
4f774513 16836 }
4360ca9c
JS
16837
16838 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16839 break;
16840 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16841 }
4360ca9c
JS
16842
16843 if (found)
16844 return seq_dmabuf;
4f774513
JS
16845 return NULL;
16846}
16847
6669f9bb
JS
16848/**
16849 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16850 * @vport: pointer to a vitural port
16851 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16852 *
16853 * This function tries to abort from the partially assembed sequence, described
16854 * by the information from basic abbort @dmabuf. It checks to see whether such
16855 * partially assembled sequence held by the driver. If so, it shall free up all
16856 * the frames from the partially assembled sequence.
16857 *
16858 * Return
16859 * true -- if there is matching partially assembled sequence present and all
16860 * the frames freed with the sequence;
16861 * false -- if there is no matching partially assembled sequence present so
16862 * nothing got aborted in the lower layer driver
16863 **/
16864static bool
16865lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16866 struct hbq_dmabuf *dmabuf)
16867{
16868 struct fc_frame_header *new_hdr;
16869 struct fc_frame_header *temp_hdr;
16870 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16871 struct hbq_dmabuf *seq_dmabuf = NULL;
16872
16873 /* Use the hdr_buf to find the sequence that matches this frame */
16874 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16875 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16876 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16877 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16878 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16879 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16880 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16881 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16882 continue;
16883 /* found a pending sequence that matches this frame */
16884 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16885 break;
16886 }
16887
16888 /* Free up all the frames from the partially assembled sequence */
16889 if (seq_dmabuf) {
16890 list_for_each_entry_safe(d_buf, n_buf,
16891 &seq_dmabuf->dbuf.list, list) {
16892 list_del_init(&d_buf->list);
16893 lpfc_in_buf_free(vport->phba, d_buf);
16894 }
16895 return true;
16896 }
16897 return false;
16898}
16899
6dd9e31c
JS
16900/**
16901 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16902 * @vport: pointer to a vitural port
16903 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16904 *
16905 * This function tries to abort from the assembed sequence from upper level
16906 * protocol, described by the information from basic abbort @dmabuf. It
16907 * checks to see whether such pending context exists at upper level protocol.
16908 * If so, it shall clean up the pending context.
16909 *
16910 * Return
16911 * true -- if there is matching pending context of the sequence cleaned
16912 * at ulp;
16913 * false -- if there is no matching pending context of the sequence present
16914 * at ulp.
16915 **/
16916static bool
16917lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16918{
16919 struct lpfc_hba *phba = vport->phba;
16920 int handled;
16921
16922 /* Accepting abort at ulp with SLI4 only */
16923 if (phba->sli_rev < LPFC_SLI_REV4)
16924 return false;
16925
16926 /* Register all caring upper level protocols to attend abort */
16927 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16928 if (handled)
16929 return true;
16930
16931 return false;
16932}
16933
6669f9bb 16934/**
546fc854 16935 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16936 * @phba: Pointer to HBA context object.
16937 * @cmd_iocbq: pointer to the command iocbq structure.
16938 * @rsp_iocbq: pointer to the response iocbq structure.
16939 *
546fc854 16940 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16941 * event. It properly releases the memory allocated to the sequence abort
16942 * accept iocb.
16943 **/
16944static void
546fc854 16945lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16946 struct lpfc_iocbq *cmd_iocbq,
16947 struct lpfc_iocbq *rsp_iocbq)
16948{
6dd9e31c
JS
16949 struct lpfc_nodelist *ndlp;
16950
16951 if (cmd_iocbq) {
16952 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16953 lpfc_nlp_put(ndlp);
16954 lpfc_nlp_not_used(ndlp);
6669f9bb 16955 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16956 }
6b5151fd
JS
16957
16958 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16959 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16961 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16962 rsp_iocbq->iocb.ulpStatus,
16963 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16964}
16965
6d368e53
JS
16966/**
16967 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16968 * @phba: Pointer to HBA context object.
16969 * @xri: xri id in transaction.
16970 *
16971 * This function validates the xri maps to the known range of XRIs allocated an
16972 * used by the driver.
16973 **/
7851fe2c 16974uint16_t
6d368e53
JS
16975lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16976 uint16_t xri)
16977{
a2fc4aef 16978 uint16_t i;
6d368e53
JS
16979
16980 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16981 if (xri == phba->sli4_hba.xri_ids[i])
16982 return i;
16983 }
16984 return NO_XRI;
16985}
16986
6669f9bb 16987/**
546fc854 16988 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16989 * @phba: Pointer to HBA context object.
16990 * @fc_hdr: pointer to a FC frame header.
16991 *
546fc854 16992 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16993 * event after aborting the sequence handling.
16994 **/
86c67379 16995void
6dd9e31c
JS
16996lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16997 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16998{
6dd9e31c 16999 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
17000 struct lpfc_iocbq *ctiocb = NULL;
17001 struct lpfc_nodelist *ndlp;
ee0f4fe1 17002 uint16_t oxid, rxid, xri, lxri;
5ffc266e 17003 uint32_t sid, fctl;
6669f9bb 17004 IOCB_t *icmd;
546fc854 17005 int rc;
6669f9bb
JS
17006
17007 if (!lpfc_is_link_up(phba))
17008 return;
17009
17010 sid = sli4_sid_from_fc_hdr(fc_hdr);
17011 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 17012 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 17013
6dd9e31c 17014 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 17015 if (!ndlp) {
9d3d340d 17016 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
17017 if (!ndlp) {
17018 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17019 "1268 Failed to allocate ndlp for "
17020 "oxid:x%x SID:x%x\n", oxid, sid);
17021 return;
17022 }
6dd9e31c
JS
17023 /* Put ndlp onto pport node list */
17024 lpfc_enqueue_node(vport, ndlp);
17025 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17026 /* re-setup ndlp without removing from node list */
17027 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17028 if (!ndlp) {
17029 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17030 "3275 Failed to active ndlp found "
17031 "for oxid:x%x SID:x%x\n", oxid, sid);
17032 return;
17033 }
6669f9bb
JS
17034 }
17035
546fc854 17036 /* Allocate buffer for rsp iocb */
6669f9bb
JS
17037 ctiocb = lpfc_sli_get_iocbq(phba);
17038 if (!ctiocb)
17039 return;
17040
5ffc266e
JS
17041 /* Extract the F_CTL field from FC_HDR */
17042 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17043
6669f9bb 17044 icmd = &ctiocb->iocb;
6669f9bb 17045 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 17046 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
17047 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17048 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17049 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17050
17051 /* Fill in the rest of iocb fields */
17052 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17053 icmd->ulpBdeCount = 0;
17054 icmd->ulpLe = 1;
17055 icmd->ulpClass = CLASS3;
6d368e53 17056 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 17057 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 17058
6669f9bb
JS
17059 ctiocb->iocb_cmpl = NULL;
17060 ctiocb->vport = phba->pport;
546fc854 17061 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 17062 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
17063 ctiocb->sli4_xritag = NO_XRI;
17064
ee0f4fe1
JS
17065 if (fctl & FC_FC_EX_CTX)
17066 /* Exchange responder sent the abort so we
17067 * own the oxid.
17068 */
17069 xri = oxid;
17070 else
17071 xri = rxid;
17072 lxri = lpfc_sli4_xri_inrange(phba, xri);
17073 if (lxri != NO_XRI)
17074 lpfc_set_rrq_active(phba, ndlp, lxri,
17075 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
17076 /* For BA_ABTS from exchange responder, if the logical xri with
17077 * the oxid maps to the FCP XRI range, the port no longer has
17078 * that exchange context, send a BLS_RJT. Override the IOCB for
17079 * a BA_RJT.
17080 */
17081 if ((fctl & FC_FC_EX_CTX) &&
895427bd 17082 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
17083 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17084 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17085 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17086 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17087 }
17088
17089 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17090 * the driver no longer has that exchange, send a BLS_RJT. Override
17091 * the IOCB for a BA_RJT.
546fc854 17092 */
6dd9e31c 17093 if (aborted == false) {
546fc854
JS
17094 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17095 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17096 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17097 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17098 }
6669f9bb 17099
5ffc266e
JS
17100 if (fctl & FC_FC_EX_CTX) {
17101 /* ABTS sent by responder to CT exchange, construction
17102 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17103 * field and RX_ID from ABTS for RX_ID field.
17104 */
546fc854 17105 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
17106 } else {
17107 /* ABTS sent by initiator to CT exchange, construction
17108 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 17109 * XRI_TAG field.
5ffc266e 17110 */
546fc854 17111 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 17112 }
f09c3acc 17113 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 17114 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 17115
546fc854 17116 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
17117 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17118 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17119 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
17120
17121 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17122 if (rc == IOCB_ERROR) {
6dd9e31c
JS
17123 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17124 "2925 Failed to issue CT ABTS RSP x%x on "
17125 "xri x%x, Data x%x\n",
17126 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17127 phba->link_state);
17128 lpfc_nlp_put(ndlp);
17129 ctiocb->context1 = NULL;
546fc854
JS
17130 lpfc_sli_release_iocbq(phba, ctiocb);
17131 }
6669f9bb
JS
17132}
17133
17134/**
17135 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17136 * @vport: Pointer to the vport on which this sequence was received
17137 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17138 *
17139 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17140 * receive sequence is only partially assembed by the driver, it shall abort
17141 * the partially assembled frames for the sequence. Otherwise, if the
17142 * unsolicited receive sequence has been completely assembled and passed to
17143 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17144 * unsolicited sequence has been aborted. After that, it will issue a basic
17145 * accept to accept the abort.
17146 **/
5d8b8167 17147static void
6669f9bb
JS
17148lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17149 struct hbq_dmabuf *dmabuf)
17150{
17151 struct lpfc_hba *phba = vport->phba;
17152 struct fc_frame_header fc_hdr;
5ffc266e 17153 uint32_t fctl;
6dd9e31c 17154 bool aborted;
6669f9bb 17155
6669f9bb
JS
17156 /* Make a copy of fc_hdr before the dmabuf being released */
17157 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 17158 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 17159
5ffc266e 17160 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
17161 /* ABTS by responder to exchange, no cleanup needed */
17162 aborted = true;
5ffc266e 17163 } else {
6dd9e31c
JS
17164 /* ABTS by initiator to exchange, need to do cleanup */
17165 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17166 if (aborted == false)
17167 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 17168 }
6dd9e31c
JS
17169 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17170
86c67379
JS
17171 if (phba->nvmet_support) {
17172 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17173 return;
17174 }
17175
6dd9e31c
JS
17176 /* Respond with BA_ACC or BA_RJT accordingly */
17177 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
17178}
17179
4f774513
JS
17180/**
17181 * lpfc_seq_complete - Indicates if a sequence is complete
17182 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17183 *
17184 * This function checks the sequence, starting with the frame described by
17185 * @dmabuf, to see if all the frames associated with this sequence are present.
17186 * the frames associated with this sequence are linked to the @dmabuf using the
17187 * dbuf list. This function looks for two major things. 1) That the first frame
17188 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17189 * set. 3) That there are no holes in the sequence count. The function will
17190 * return 1 when the sequence is complete, otherwise it will return 0.
17191 **/
17192static int
17193lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17194{
17195 struct fc_frame_header *hdr;
17196 struct lpfc_dmabuf *d_buf;
17197 struct hbq_dmabuf *seq_dmabuf;
17198 uint32_t fctl;
17199 int seq_count = 0;
17200
17201 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17202 /* make sure first fame of sequence has a sequence count of zero */
17203 if (hdr->fh_seq_cnt != seq_count)
17204 return 0;
17205 fctl = (hdr->fh_f_ctl[0] << 16 |
17206 hdr->fh_f_ctl[1] << 8 |
17207 hdr->fh_f_ctl[2]);
17208 /* If last frame of sequence we can return success. */
17209 if (fctl & FC_FC_END_SEQ)
17210 return 1;
17211 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17212 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17213 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17214 /* If there is a hole in the sequence count then fail. */
eeead811 17215 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
17216 return 0;
17217 fctl = (hdr->fh_f_ctl[0] << 16 |
17218 hdr->fh_f_ctl[1] << 8 |
17219 hdr->fh_f_ctl[2]);
17220 /* If last frame of sequence we can return success. */
17221 if (fctl & FC_FC_END_SEQ)
17222 return 1;
17223 }
17224 return 0;
17225}
17226
17227/**
17228 * lpfc_prep_seq - Prep sequence for ULP processing
17229 * @vport: Pointer to the vport on which this sequence was received
17230 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17231 *
17232 * This function takes a sequence, described by a list of frames, and creates
17233 * a list of iocbq structures to describe the sequence. This iocbq list will be
17234 * used to issue to the generic unsolicited sequence handler. This routine
17235 * returns a pointer to the first iocbq in the list. If the function is unable
17236 * to allocate an iocbq then it throw out the received frames that were not
17237 * able to be described and return a pointer to the first iocbq. If unable to
17238 * allocate any iocbqs (including the first) this function will return NULL.
17239 **/
17240static struct lpfc_iocbq *
17241lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17242{
7851fe2c 17243 struct hbq_dmabuf *hbq_buf;
4f774513
JS
17244 struct lpfc_dmabuf *d_buf, *n_buf;
17245 struct lpfc_iocbq *first_iocbq, *iocbq;
17246 struct fc_frame_header *fc_hdr;
17247 uint32_t sid;
7851fe2c 17248 uint32_t len, tot_len;
eeead811 17249 struct ulp_bde64 *pbde;
4f774513
JS
17250
17251 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17252 /* remove from receive buffer list */
17253 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 17254 lpfc_update_rcv_time_stamp(vport);
4f774513 17255 /* get the Remote Port's SID */
6669f9bb 17256 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 17257 tot_len = 0;
4f774513
JS
17258 /* Get an iocbq struct to fill in. */
17259 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17260 if (first_iocbq) {
17261 /* Initialize the first IOCB. */
8fa38513 17262 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 17263 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 17264 first_iocbq->vport = vport;
939723a4
JS
17265
17266 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17267 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17268 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17269 first_iocbq->iocb.un.rcvels.parmRo =
17270 sli4_did_from_fc_hdr(fc_hdr);
17271 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17272 } else
17273 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17274 first_iocbq->iocb.ulpContext = NO_XRI;
17275 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17276 be16_to_cpu(fc_hdr->fh_ox_id);
17277 /* iocbq is prepped for internal consumption. Physical vpi. */
17278 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17279 vport->phba->vpi_ids[vport->vpi];
4f774513 17280 /* put the first buffer into the first IOCBq */
48a5a664
JS
17281 tot_len = bf_get(lpfc_rcqe_length,
17282 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17283
4f774513
JS
17284 first_iocbq->context2 = &seq_dmabuf->dbuf;
17285 first_iocbq->context3 = NULL;
17286 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17287 if (tot_len > LPFC_DATA_BUF_SIZE)
17288 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17289 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17290 else
17291 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17292
4f774513 17293 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17294
7851fe2c 17295 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17296 }
17297 iocbq = first_iocbq;
17298 /*
17299 * Each IOCBq can have two Buffers assigned, so go through the list
17300 * of buffers for this sequence and save two buffers in each IOCBq
17301 */
17302 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17303 if (!iocbq) {
17304 lpfc_in_buf_free(vport->phba, d_buf);
17305 continue;
17306 }
17307 if (!iocbq->context3) {
17308 iocbq->context3 = d_buf;
17309 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17310 /* We need to get the size out of the right CQE */
17311 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17312 len = bf_get(lpfc_rcqe_length,
17313 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17314 pbde = (struct ulp_bde64 *)
17315 &iocbq->iocb.unsli3.sli3Words[4];
17316 if (len > LPFC_DATA_BUF_SIZE)
17317 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17318 else
17319 pbde->tus.f.bdeSize = len;
17320
7851fe2c
JS
17321 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17322 tot_len += len;
4f774513
JS
17323 } else {
17324 iocbq = lpfc_sli_get_iocbq(vport->phba);
17325 if (!iocbq) {
17326 if (first_iocbq) {
17327 first_iocbq->iocb.ulpStatus =
17328 IOSTAT_FCP_RSP_ERROR;
17329 first_iocbq->iocb.un.ulpWord[4] =
17330 IOERR_NO_RESOURCES;
17331 }
17332 lpfc_in_buf_free(vport->phba, d_buf);
17333 continue;
17334 }
48a5a664
JS
17335 /* We need to get the size out of the right CQE */
17336 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17337 len = bf_get(lpfc_rcqe_length,
17338 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17339 iocbq->context2 = d_buf;
17340 iocbq->context3 = NULL;
17341 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17342 if (len > LPFC_DATA_BUF_SIZE)
17343 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17344 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17345 else
17346 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17347
7851fe2c
JS
17348 tot_len += len;
17349 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17350
4f774513
JS
17351 iocbq->iocb.un.rcvels.remoteID = sid;
17352 list_add_tail(&iocbq->list, &first_iocbq->list);
17353 }
17354 }
17355 return first_iocbq;
17356}
17357
6669f9bb
JS
17358static void
17359lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17360 struct hbq_dmabuf *seq_dmabuf)
17361{
17362 struct fc_frame_header *fc_hdr;
17363 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17364 struct lpfc_hba *phba = vport->phba;
17365
17366 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17367 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17368 if (!iocbq) {
17369 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17370 "2707 Ring %d handler: Failed to allocate "
17371 "iocb Rctl x%x Type x%x received\n",
17372 LPFC_ELS_RING,
17373 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17374 return;
17375 }
17376 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17377 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17378 iocbq, fc_hdr->fh_r_ctl,
17379 fc_hdr->fh_type))
6d368e53 17380 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17381 "2540 Ring %d handler: unexpected Rctl "
17382 "x%x Type x%x received\n",
17383 LPFC_ELS_RING,
17384 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17385
17386 /* Free iocb created in lpfc_prep_seq */
17387 list_for_each_entry_safe(curr_iocb, next_iocb,
17388 &iocbq->list, list) {
17389 list_del_init(&curr_iocb->list);
17390 lpfc_sli_release_iocbq(phba, curr_iocb);
17391 }
17392 lpfc_sli_release_iocbq(phba, iocbq);
17393}
17394
ae9e28f3
JS
17395static void
17396lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17397 struct lpfc_iocbq *rspiocb)
17398{
17399 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17400
17401 if (pcmd && pcmd->virt)
771db5c0 17402 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17403 kfree(pcmd);
17404 lpfc_sli_release_iocbq(phba, cmdiocb);
17405}
17406
17407static void
17408lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17409 struct hbq_dmabuf *dmabuf)
17410{
17411 struct fc_frame_header *fc_hdr;
17412 struct lpfc_hba *phba = vport->phba;
17413 struct lpfc_iocbq *iocbq = NULL;
17414 union lpfc_wqe *wqe;
17415 struct lpfc_dmabuf *pcmd = NULL;
17416 uint32_t frame_len;
17417 int rc;
17418
17419 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17420 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17421
17422 /* Send the received frame back */
17423 iocbq = lpfc_sli_get_iocbq(phba);
17424 if (!iocbq)
17425 goto exit;
17426
17427 /* Allocate buffer for command payload */
17428 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17429 if (pcmd)
771db5c0 17430 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17431 &pcmd->phys);
17432 if (!pcmd || !pcmd->virt)
17433 goto exit;
17434
17435 INIT_LIST_HEAD(&pcmd->list);
17436
17437 /* copyin the payload */
17438 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17439
17440 /* fill in BDE's for command */
17441 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17442 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17443 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17444 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17445
17446 iocbq->context2 = pcmd;
17447 iocbq->vport = vport;
17448 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17449 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17450
17451 /*
17452 * Setup rest of the iocb as though it were a WQE
17453 * Build the SEND_FRAME WQE
17454 */
17455 wqe = (union lpfc_wqe *)&iocbq->iocb;
17456
17457 wqe->send_frame.frame_len = frame_len;
17458 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17459 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17460 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17461 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17462 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17463 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17464
17465 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17466 iocbq->iocb.ulpLe = 1;
17467 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17468 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17469 if (rc == IOCB_ERROR)
17470 goto exit;
17471
17472 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17473 return;
17474
17475exit:
17476 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17477 "2023 Unable to process MDS loopback frame\n");
17478 if (pcmd && pcmd->virt)
771db5c0 17479 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17480 kfree(pcmd);
401bb416
DK
17481 if (iocbq)
17482 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17483 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17484}
17485
4f774513
JS
17486/**
17487 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17488 * @phba: Pointer to HBA context object.
17489 *
17490 * This function is called with no lock held. This function processes all
17491 * the received buffers and gives it to upper layers when a received buffer
17492 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17493 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17494 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17495 * appropriate receive function when the final frame in a sequence is received.
17496 **/
4d9ab994
JS
17497void
17498lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17499 struct hbq_dmabuf *dmabuf)
4f774513 17500{
4d9ab994 17501 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17502 struct fc_frame_header *fc_hdr;
17503 struct lpfc_vport *vport;
17504 uint32_t fcfi;
939723a4 17505 uint32_t did;
4f774513 17506
4f774513 17507 /* Process each received buffer */
4d9ab994 17508 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17509
4d9ab994
JS
17510 /* check to see if this a valid type of frame */
17511 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17512 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17513 return;
17514 }
2ea259ee 17515
7851fe2c
JS
17516 if ((bf_get(lpfc_cqe_code,
17517 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17518 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17519 &dmabuf->cq_event.cqe.rcqe_cmpl);
17520 else
17521 fcfi = bf_get(lpfc_rcqe_fcf_id,
17522 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17523
ae9e28f3
JS
17524 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17525 vport = phba->pport;
17526 /* Handle MDS Loopback frames */
17527 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17528 return;
17529 }
17530
895427bd
JS
17531 /* d_id this frame is directed to */
17532 did = sli4_did_from_fc_hdr(fc_hdr);
17533
17534 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17535 if (!vport) {
4d9ab994
JS
17536 /* throw out the frame */
17537 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17538 return;
17539 }
939723a4 17540
939723a4
JS
17541 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17542 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17543 (did != Fabric_DID)) {
17544 /*
17545 * Throw out the frame if we are not pt2pt.
17546 * The pt2pt protocol allows for discovery frames
17547 * to be received without a registered VPI.
17548 */
17549 if (!(vport->fc_flag & FC_PT2PT) ||
17550 (phba->link_state == LPFC_HBA_READY)) {
17551 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17552 return;
17553 }
17554 }
17555
6669f9bb
JS
17556 /* Handle the basic abort sequence (BA_ABTS) event */
17557 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17558 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17559 return;
17560 }
17561
4d9ab994
JS
17562 /* Link this frame */
17563 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17564 if (!seq_dmabuf) {
17565 /* unable to add frame to vport - throw it out */
17566 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17567 return;
17568 }
17569 /* If not last frame in sequence continue processing frames. */
def9c7a9 17570 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17571 return;
def9c7a9 17572
6669f9bb
JS
17573 /* Send the complete sequence to the upper layer protocol */
17574 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17575}
6fb120a7
JS
17576
17577/**
17578 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17579 * @phba: pointer to lpfc hba data structure.
17580 *
17581 * This routine is invoked to post rpi header templates to the
17582 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17583 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17584 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17585 *
17586 * This routine does not require any locks. It's usage is expected
17587 * to be driver load or reset recovery when the driver is
17588 * sequential.
17589 *
17590 * Return codes
af901ca1 17591 * 0 - successful
d439d286 17592 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17593 * When this error occurs, the driver is not guaranteed
17594 * to have any rpi regions posted to the device and
17595 * must either attempt to repost the regions or take a
17596 * fatal error.
17597 **/
17598int
17599lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17600{
17601 struct lpfc_rpi_hdr *rpi_page;
17602 uint32_t rc = 0;
6d368e53
JS
17603 uint16_t lrpi = 0;
17604
17605 /* SLI4 ports that support extents do not require RPI headers. */
17606 if (!phba->sli4_hba.rpi_hdrs_in_use)
17607 goto exit;
17608 if (phba->sli4_hba.extents_in_use)
17609 return -EIO;
6fb120a7 17610
6fb120a7 17611 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17612 /*
17613 * Assign the rpi headers a physical rpi only if the driver
17614 * has not initialized those resources. A port reset only
17615 * needs the headers posted.
17616 */
17617 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17618 LPFC_RPI_RSRC_RDY)
17619 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17620
6fb120a7
JS
17621 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17622 if (rc != MBX_SUCCESS) {
17623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17624 "2008 Error %d posting all rpi "
17625 "headers\n", rc);
17626 rc = -EIO;
17627 break;
17628 }
17629 }
17630
6d368e53
JS
17631 exit:
17632 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17633 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17634 return rc;
17635}
17636
17637/**
17638 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17639 * @phba: pointer to lpfc hba data structure.
17640 * @rpi_page: pointer to the rpi memory region.
17641 *
17642 * This routine is invoked to post a single rpi header to the
17643 * HBA consistent with the SLI-4 interface spec. This memory region
17644 * maps up to 64 rpi context regions.
17645 *
17646 * Return codes
af901ca1 17647 * 0 - successful
d439d286
JS
17648 * -ENOMEM - No available memory
17649 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17650 **/
17651int
17652lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17653{
17654 LPFC_MBOXQ_t *mboxq;
17655 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17656 uint32_t rc = 0;
6fb120a7
JS
17657 uint32_t shdr_status, shdr_add_status;
17658 union lpfc_sli4_cfg_shdr *shdr;
17659
6d368e53
JS
17660 /* SLI4 ports that support extents do not require RPI headers. */
17661 if (!phba->sli4_hba.rpi_hdrs_in_use)
17662 return rc;
17663 if (phba->sli4_hba.extents_in_use)
17664 return -EIO;
17665
6fb120a7
JS
17666 /* The port is notified of the header region via a mailbox command. */
17667 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17668 if (!mboxq) {
17669 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17670 "2001 Unable to allocate memory for issuing "
17671 "SLI_CONFIG_SPECIAL mailbox command\n");
17672 return -ENOMEM;
17673 }
17674
17675 /* Post all rpi memory regions to the port. */
17676 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17677 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17678 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17679 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17680 sizeof(struct lpfc_sli4_cfg_mhdr),
17681 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17682
17683
17684 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17685 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17686 rpi_page->start_rpi);
6d368e53
JS
17687 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17688 hdr_tmpl, rpi_page->page_count);
17689
6fb120a7
JS
17690 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17691 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17692 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17693 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17694 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17695 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17696 if (rc != MBX_TIMEOUT)
17697 mempool_free(mboxq, phba->mbox_mem_pool);
17698 if (shdr_status || shdr_add_status || rc) {
17699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17700 "2514 POST_RPI_HDR mailbox failed with "
17701 "status x%x add_status x%x, mbx status x%x\n",
17702 shdr_status, shdr_add_status, rc);
17703 rc = -ENXIO;
845d9e8d
JS
17704 } else {
17705 /*
17706 * The next_rpi stores the next logical module-64 rpi value used
17707 * to post physical rpis in subsequent rpi postings.
17708 */
17709 spin_lock_irq(&phba->hbalock);
17710 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17711 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17712 }
17713 return rc;
17714}
17715
17716/**
17717 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17718 * @phba: pointer to lpfc hba data structure.
17719 *
17720 * This routine is invoked to post rpi header templates to the
17721 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17722 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17723 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17724 *
17725 * Returns
af901ca1 17726 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17727 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17728 **/
17729int
17730lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17731{
6d368e53
JS
17732 unsigned long rpi;
17733 uint16_t max_rpi, rpi_limit;
17734 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17735 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17736 unsigned long iflag;
6fb120a7 17737
6fb120a7 17738 /*
6d368e53
JS
17739 * Fetch the next logical rpi. Because this index is logical,
17740 * the driver starts at 0 each time.
6fb120a7 17741 */
4902b381 17742 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17743 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17744 rpi_limit = phba->sli4_hba.next_rpi;
17745
6d368e53
JS
17746 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17747 if (rpi >= rpi_limit)
6fb120a7
JS
17748 rpi = LPFC_RPI_ALLOC_ERROR;
17749 else {
17750 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17751 phba->sli4_hba.max_cfg_param.rpi_used++;
17752 phba->sli4_hba.rpi_count++;
17753 }
be6bb941
JS
17754 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17755 "0001 rpi:%x max:%x lim:%x\n",
17756 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17757
17758 /*
17759 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17760 * has been exhausted.
6fb120a7
JS
17761 */
17762 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17763 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17764 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17765 return rpi;
17766 }
17767
6d368e53
JS
17768 /*
17769 * RPI header postings are not required for SLI4 ports capable of
17770 * extents.
17771 */
17772 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17773 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17774 return rpi;
17775 }
17776
6fb120a7
JS
17777 /*
17778 * If the driver is running low on rpi resources, allocate another
17779 * page now. Note that the next_rpi value is used because
17780 * it represents how many are actually in use whereas max_rpi notes
17781 * how many are supported max by the device.
17782 */
6d368e53 17783 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17784 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17785 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17786 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17787 if (!rpi_hdr) {
17788 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17789 "2002 Error Could not grow rpi "
17790 "count\n");
17791 } else {
6d368e53
JS
17792 lrpi = rpi_hdr->start_rpi;
17793 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17794 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17795 }
17796 }
17797
17798 return rpi;
17799}
17800
d7c47992
JS
17801/**
17802 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17803 * @phba: pointer to lpfc hba data structure.
17804 *
17805 * This routine is invoked to release an rpi to the pool of
17806 * available rpis maintained by the driver.
17807 **/
5d8b8167 17808static void
d7c47992
JS
17809__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17810{
17811 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17812 phba->sli4_hba.rpi_count--;
17813 phba->sli4_hba.max_cfg_param.rpi_used--;
17814 }
17815}
17816
6fb120a7
JS
17817/**
17818 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17819 * @phba: pointer to lpfc hba data structure.
17820 *
17821 * This routine is invoked to release an rpi to the pool of
17822 * available rpis maintained by the driver.
17823 **/
17824void
17825lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17826{
17827 spin_lock_irq(&phba->hbalock);
d7c47992 17828 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17829 spin_unlock_irq(&phba->hbalock);
17830}
17831
17832/**
17833 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17834 * @phba: pointer to lpfc hba data structure.
17835 *
17836 * This routine is invoked to remove the memory region that
17837 * provided rpi via a bitmask.
17838 **/
17839void
17840lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17841{
17842 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17843 kfree(phba->sli4_hba.rpi_ids);
17844 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17845}
17846
17847/**
17848 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17849 * @phba: pointer to lpfc hba data structure.
17850 *
17851 * This routine is invoked to remove the memory region that
17852 * provided rpi via a bitmask.
17853 **/
17854int
6b5151fd
JS
17855lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17856 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17857{
17858 LPFC_MBOXQ_t *mboxq;
17859 struct lpfc_hba *phba = ndlp->phba;
17860 int rc;
17861
17862 /* The port is notified of the header region via a mailbox command. */
17863 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17864 if (!mboxq)
17865 return -ENOMEM;
17866
17867 /* Post all rpi memory regions to the port. */
17868 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17869 if (cmpl) {
17870 mboxq->mbox_cmpl = cmpl;
17871 mboxq->context1 = arg;
17872 mboxq->context2 = ndlp;
72859909
JS
17873 } else
17874 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17875 mboxq->vport = ndlp->vport;
6fb120a7
JS
17876 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17877 if (rc == MBX_NOT_FINISHED) {
17878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17879 "2010 Resume RPI Mailbox failed "
17880 "status %d, mbxStatus x%x\n", rc,
17881 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17882 mempool_free(mboxq, phba->mbox_mem_pool);
17883 return -EIO;
17884 }
17885 return 0;
17886}
17887
17888/**
17889 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17890 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17891 *
76a95d75 17892 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17893 *
17894 * Returns:
17895 * 0 success
17896 * -Evalue otherwise
17897 **/
17898int
76a95d75 17899lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17900{
17901 LPFC_MBOXQ_t *mboxq;
17902 int rc = 0;
6a9c52cf 17903 int retval = MBX_SUCCESS;
6fb120a7 17904 uint32_t mbox_tmo;
76a95d75 17905 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17906 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17907 if (!mboxq)
17908 return -ENOMEM;
76a95d75 17909 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17910 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17911 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17912 if (rc != MBX_SUCCESS) {
76a95d75 17913 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17914 "2022 INIT VPI Mailbox failed "
17915 "status %d, mbxStatus x%x\n", rc,
17916 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17917 retval = -EIO;
6fb120a7 17918 }
6a9c52cf 17919 if (rc != MBX_TIMEOUT)
76a95d75 17920 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17921
17922 return retval;
6fb120a7
JS
17923}
17924
17925/**
17926 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17927 * @phba: pointer to lpfc hba data structure.
17928 * @mboxq: Pointer to mailbox object.
17929 *
17930 * This routine is invoked to manually add a single FCF record. The caller
17931 * must pass a completely initialized FCF_Record. This routine takes
17932 * care of the nonembedded mailbox operations.
17933 **/
17934static void
17935lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17936{
17937 void *virt_addr;
17938 union lpfc_sli4_cfg_shdr *shdr;
17939 uint32_t shdr_status, shdr_add_status;
17940
17941 virt_addr = mboxq->sge_array->addr[0];
17942 /* The IOCTL status is embedded in the mailbox subheader. */
17943 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17944 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17945 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17946
17947 if ((shdr_status || shdr_add_status) &&
17948 (shdr_status != STATUS_FCF_IN_USE))
17949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17950 "2558 ADD_FCF_RECORD mailbox failed with "
17951 "status x%x add_status x%x\n",
17952 shdr_status, shdr_add_status);
17953
17954 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17955}
17956
17957/**
17958 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17959 * @phba: pointer to lpfc hba data structure.
17960 * @fcf_record: pointer to the initialized fcf record to add.
17961 *
17962 * This routine is invoked to manually add a single FCF record. The caller
17963 * must pass a completely initialized FCF_Record. This routine takes
17964 * care of the nonembedded mailbox operations.
17965 **/
17966int
17967lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17968{
17969 int rc = 0;
17970 LPFC_MBOXQ_t *mboxq;
17971 uint8_t *bytep;
17972 void *virt_addr;
6fb120a7
JS
17973 struct lpfc_mbx_sge sge;
17974 uint32_t alloc_len, req_len;
17975 uint32_t fcfindex;
17976
17977 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17978 if (!mboxq) {
17979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17980 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17981 return -ENOMEM;
17982 }
17983
17984 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17985 sizeof(uint32_t);
17986
17987 /* Allocate DMA memory and set up the non-embedded mailbox command */
17988 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17989 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17990 req_len, LPFC_SLI4_MBX_NEMBED);
17991 if (alloc_len < req_len) {
17992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17993 "2523 Allocated DMA memory size (x%x) is "
17994 "less than the requested DMA memory "
17995 "size (x%x)\n", alloc_len, req_len);
17996 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17997 return -ENOMEM;
17998 }
17999
18000 /*
18001 * Get the first SGE entry from the non-embedded DMA memory. This
18002 * routine only uses a single SGE.
18003 */
18004 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
18005 virt_addr = mboxq->sge_array->addr[0];
18006 /*
18007 * Configure the FCF record for FCFI 0. This is the driver's
18008 * hardcoded default and gets used in nonFIP mode.
18009 */
18010 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18011 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18012 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18013
18014 /*
18015 * Copy the fcf_index and the FCF Record Data. The data starts after
18016 * the FCoE header plus word10. The data copy needs to be endian
18017 * correct.
18018 */
18019 bytep += sizeof(uint32_t);
18020 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18021 mboxq->vport = phba->pport;
18022 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18023 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18024 if (rc == MBX_NOT_FINISHED) {
18025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18026 "2515 ADD_FCF_RECORD mailbox failed with "
18027 "status 0x%x\n", rc);
18028 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18029 rc = -EIO;
18030 } else
18031 rc = 0;
18032
18033 return rc;
18034}
18035
18036/**
18037 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18038 * @phba: pointer to lpfc hba data structure.
18039 * @fcf_record: pointer to the fcf record to write the default data.
18040 * @fcf_index: FCF table entry index.
18041 *
18042 * This routine is invoked to build the driver's default FCF record. The
18043 * values used are hardcoded. This routine handles memory initialization.
18044 *
18045 **/
18046void
18047lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18048 struct fcf_record *fcf_record,
18049 uint16_t fcf_index)
18050{
18051 memset(fcf_record, 0, sizeof(struct fcf_record));
18052 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18053 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18054 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18055 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18056 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18057 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18058 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18059 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18060 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18061 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18062 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18063 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18064 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 18065 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
18066 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18067 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18068 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18069 /* Set the VLAN bit map */
18070 if (phba->valid_vlan) {
18071 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18072 = 1 << (phba->vlan_id % 8);
18073 }
18074}
18075
18076/**
0c9ab6f5 18077 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
18078 * @phba: pointer to lpfc hba data structure.
18079 * @fcf_index: FCF table entry offset.
18080 *
0c9ab6f5
JS
18081 * This routine is invoked to scan the entire FCF table by reading FCF
18082 * record and processing it one at a time starting from the @fcf_index
18083 * for initial FCF discovery or fast FCF failover rediscovery.
18084 *
25985edc 18085 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 18086 * otherwise.
6fb120a7
JS
18087 **/
18088int
0c9ab6f5 18089lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
18090{
18091 int rc = 0, error;
18092 LPFC_MBOXQ_t *mboxq;
6fb120a7 18093
32b9793f 18094 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 18095 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
18096 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18097 if (!mboxq) {
18098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18099 "2000 Failed to allocate mbox for "
18100 "READ_FCF cmd\n");
4d9ab994 18101 error = -ENOMEM;
0c9ab6f5 18102 goto fail_fcf_scan;
6fb120a7 18103 }
ecfd03c6 18104 /* Construct the read FCF record mailbox command */
0c9ab6f5 18105 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
18106 if (rc) {
18107 error = -EINVAL;
0c9ab6f5 18108 goto fail_fcf_scan;
6fb120a7 18109 }
ecfd03c6 18110 /* Issue the mailbox command asynchronously */
6fb120a7 18111 mboxq->vport = phba->pport;
0c9ab6f5 18112 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
18113
18114 spin_lock_irq(&phba->hbalock);
18115 phba->hba_flag |= FCF_TS_INPROG;
18116 spin_unlock_irq(&phba->hbalock);
18117
6fb120a7 18118 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 18119 if (rc == MBX_NOT_FINISHED)
6fb120a7 18120 error = -EIO;
ecfd03c6 18121 else {
38b92ef8
JS
18122 /* Reset eligible FCF count for new scan */
18123 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 18124 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 18125 error = 0;
32b9793f 18126 }
0c9ab6f5 18127fail_fcf_scan:
4d9ab994
JS
18128 if (error) {
18129 if (mboxq)
18130 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 18131 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 18132 spin_lock_irq(&phba->hbalock);
a93ff37a 18133 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
18134 spin_unlock_irq(&phba->hbalock);
18135 }
6fb120a7
JS
18136 return error;
18137}
a0c87cbd 18138
0c9ab6f5 18139/**
a93ff37a 18140 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
18141 * @phba: pointer to lpfc hba data structure.
18142 * @fcf_index: FCF table entry offset.
18143 *
18144 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 18145 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 18146 *
25985edc 18147 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18148 * otherwise.
18149 **/
18150int
18151lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18152{
18153 int rc = 0, error;
18154 LPFC_MBOXQ_t *mboxq;
18155
18156 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18157 if (!mboxq) {
18158 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18159 "2763 Failed to allocate mbox for "
18160 "READ_FCF cmd\n");
18161 error = -ENOMEM;
18162 goto fail_fcf_read;
18163 }
18164 /* Construct the read FCF record mailbox command */
18165 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18166 if (rc) {
18167 error = -EINVAL;
18168 goto fail_fcf_read;
18169 }
18170 /* Issue the mailbox command asynchronously */
18171 mboxq->vport = phba->pport;
18172 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18173 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18174 if (rc == MBX_NOT_FINISHED)
18175 error = -EIO;
18176 else
18177 error = 0;
18178
18179fail_fcf_read:
18180 if (error && mboxq)
18181 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18182 return error;
18183}
18184
18185/**
18186 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18187 * @phba: pointer to lpfc hba data structure.
18188 * @fcf_index: FCF table entry offset.
18189 *
18190 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 18191 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 18192 *
25985edc 18193 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18194 * otherwise.
18195 **/
18196int
18197lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18198{
18199 int rc = 0, error;
18200 LPFC_MBOXQ_t *mboxq;
18201
18202 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18203 if (!mboxq) {
18204 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18205 "2758 Failed to allocate mbox for "
18206 "READ_FCF cmd\n");
18207 error = -ENOMEM;
18208 goto fail_fcf_read;
18209 }
18210 /* Construct the read FCF record mailbox command */
18211 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18212 if (rc) {
18213 error = -EINVAL;
18214 goto fail_fcf_read;
18215 }
18216 /* Issue the mailbox command asynchronously */
18217 mboxq->vport = phba->pport;
18218 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18219 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18220 if (rc == MBX_NOT_FINISHED)
18221 error = -EIO;
18222 else
18223 error = 0;
18224
18225fail_fcf_read:
18226 if (error && mboxq)
18227 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18228 return error;
18229}
18230
7d791df7 18231/**
f5cb5304 18232 * lpfc_check_next_fcf_pri_level
7d791df7
JS
18233 * phba pointer to the lpfc_hba struct for this port.
18234 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18235 * routine when the rr_bmask is empty. The FCF indecies are put into the
18236 * rr_bmask based on their priority level. Starting from the highest priority
18237 * to the lowest. The most likely FCF candidate will be in the highest
18238 * priority group. When this routine is called it searches the fcf_pri list for
18239 * next lowest priority group and repopulates the rr_bmask with only those
18240 * fcf_indexes.
18241 * returns:
18242 * 1=success 0=failure
18243 **/
5d8b8167 18244static int
7d791df7
JS
18245lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18246{
18247 uint16_t next_fcf_pri;
18248 uint16_t last_index;
18249 struct lpfc_fcf_pri *fcf_pri;
18250 int rc;
18251 int ret = 0;
18252
18253 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18254 LPFC_SLI4_FCF_TBL_INDX_MAX);
18255 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18256 "3060 Last IDX %d\n", last_index);
2562669c
JS
18257
18258 /* Verify the priority list has 2 or more entries */
18259 spin_lock_irq(&phba->hbalock);
18260 if (list_empty(&phba->fcf.fcf_pri_list) ||
18261 list_is_singular(&phba->fcf.fcf_pri_list)) {
18262 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
18263 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18264 "3061 Last IDX %d\n", last_index);
18265 return 0; /* Empty rr list */
18266 }
2562669c
JS
18267 spin_unlock_irq(&phba->hbalock);
18268
7d791df7
JS
18269 next_fcf_pri = 0;
18270 /*
18271 * Clear the rr_bmask and set all of the bits that are at this
18272 * priority.
18273 */
18274 memset(phba->fcf.fcf_rr_bmask, 0,
18275 sizeof(*phba->fcf.fcf_rr_bmask));
18276 spin_lock_irq(&phba->hbalock);
18277 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18278 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18279 continue;
18280 /*
18281 * the 1st priority that has not FLOGI failed
18282 * will be the highest.
18283 */
18284 if (!next_fcf_pri)
18285 next_fcf_pri = fcf_pri->fcf_rec.priority;
18286 spin_unlock_irq(&phba->hbalock);
18287 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18288 rc = lpfc_sli4_fcf_rr_index_set(phba,
18289 fcf_pri->fcf_rec.fcf_index);
18290 if (rc)
18291 return 0;
18292 }
18293 spin_lock_irq(&phba->hbalock);
18294 }
18295 /*
18296 * if next_fcf_pri was not set above and the list is not empty then
18297 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18298 * and start at the beginning.
7d791df7
JS
18299 */
18300 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18301 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18302 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18303 /*
18304 * the 1st priority that has not FLOGI failed
18305 * will be the highest.
18306 */
18307 if (!next_fcf_pri)
18308 next_fcf_pri = fcf_pri->fcf_rec.priority;
18309 spin_unlock_irq(&phba->hbalock);
18310 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18311 rc = lpfc_sli4_fcf_rr_index_set(phba,
18312 fcf_pri->fcf_rec.fcf_index);
18313 if (rc)
18314 return 0;
18315 }
18316 spin_lock_irq(&phba->hbalock);
18317 }
18318 } else
18319 ret = 1;
18320 spin_unlock_irq(&phba->hbalock);
18321
18322 return ret;
18323}
0c9ab6f5
JS
18324/**
18325 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18326 * @phba: pointer to lpfc hba data structure.
18327 *
18328 * This routine is to get the next eligible FCF record index in a round
18329 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18330 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18331 * shall be returned, otherwise, the next eligible FCF record's index
18332 * shall be returned.
18333 **/
18334uint16_t
18335lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18336{
18337 uint16_t next_fcf_index;
18338
421c6622 18339initial_priority:
3804dc84 18340 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18341 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18342
7d791df7 18343next_priority:
421c6622
JS
18344 /* Determine the next fcf index to check */
18345 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18346 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18347 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18348 next_fcf_index);
18349
0c9ab6f5 18350 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18351 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18352 /*
18353 * If we have wrapped then we need to clear the bits that
18354 * have been tested so that we can detect when we should
18355 * change the priority level.
18356 */
0c9ab6f5
JS
18357 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18358 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18359 }
18360
3804dc84
JS
18361
18362 /* Check roundrobin failover list empty condition */
7d791df7
JS
18363 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18364 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18365 /*
18366 * If next fcf index is not found check if there are lower
18367 * Priority level fcf's in the fcf_priority list.
18368 * Set up the rr_bmask with all of the avaiable fcf bits
18369 * at that level and continue the selection process.
18370 */
18371 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18372 goto initial_priority;
3804dc84
JS
18373 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18374 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18375 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18376 return LPFC_FCOE_FCF_NEXT_NONE;
18377 else {
18378 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18379 "3063 Only FCF available idx %d, flag %x\n",
18380 next_fcf_index,
18381 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18382 return next_fcf_index;
18383 }
3804dc84
JS
18384 }
18385
7d791df7
JS
18386 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18387 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18388 LPFC_FCF_FLOGI_FAILED) {
18389 if (list_is_singular(&phba->fcf.fcf_pri_list))
18390 return LPFC_FCOE_FCF_NEXT_NONE;
18391
7d791df7 18392 goto next_priority;
f5cb5304 18393 }
7d791df7 18394
3804dc84 18395 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18396 "2845 Get next roundrobin failover FCF (x%x)\n",
18397 next_fcf_index);
18398
0c9ab6f5
JS
18399 return next_fcf_index;
18400}
18401
18402/**
18403 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18404 * @phba: pointer to lpfc hba data structure.
18405 *
18406 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18407 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18408 * does not go beyond the range of the driver allocated bmask dimension
18409 * before setting the bit.
18410 *
18411 * Returns 0 if the index bit successfully set, otherwise, it returns
18412 * -EINVAL.
18413 **/
18414int
18415lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18416{
18417 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18418 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18419 "2610 FCF (x%x) reached driver's book "
18420 "keeping dimension:x%x\n",
0c9ab6f5
JS
18421 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18422 return -EINVAL;
18423 }
18424 /* Set the eligible FCF record index bmask */
18425 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18426
3804dc84 18427 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18428 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18429 "bmask\n", fcf_index);
18430
0c9ab6f5
JS
18431 return 0;
18432}
18433
18434/**
3804dc84 18435 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18436 * @phba: pointer to lpfc hba data structure.
18437 *
18438 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18439 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18440 * does not go beyond the range of the driver allocated bmask dimension
18441 * before clearing the bit.
18442 **/
18443void
18444lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18445{
9a803a74 18446 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18447 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18448 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18449 "2762 FCF (x%x) reached driver's book "
18450 "keeping dimension:x%x\n",
0c9ab6f5
JS
18451 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18452 return;
18453 }
18454 /* Clear the eligible FCF record index bmask */
7d791df7 18455 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18456 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18457 list) {
7d791df7
JS
18458 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18459 list_del_init(&fcf_pri->list);
18460 break;
18461 }
18462 }
18463 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18464 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18465
18466 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18467 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18468 "bmask\n", fcf_index);
0c9ab6f5
JS
18469}
18470
ecfd03c6
JS
18471/**
18472 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18473 * @phba: pointer to lpfc hba data structure.
18474 *
18475 * This routine is the completion routine for the rediscover FCF table mailbox
18476 * command. If the mailbox command returned failure, it will try to stop the
18477 * FCF rediscover wait timer.
18478 **/
5d8b8167 18479static void
ecfd03c6
JS
18480lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18481{
18482 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18483 uint32_t shdr_status, shdr_add_status;
18484
18485 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18486
18487 shdr_status = bf_get(lpfc_mbox_hdr_status,
18488 &redisc_fcf->header.cfg_shdr.response);
18489 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18490 &redisc_fcf->header.cfg_shdr.response);
18491 if (shdr_status || shdr_add_status) {
0c9ab6f5 18492 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18493 "2746 Requesting for FCF rediscovery failed "
18494 "status x%x add_status x%x\n",
18495 shdr_status, shdr_add_status);
0c9ab6f5 18496 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18497 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18498 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18499 spin_unlock_irq(&phba->hbalock);
18500 /*
18501 * CVL event triggered FCF rediscover request failed,
18502 * last resort to re-try current registered FCF entry.
18503 */
18504 lpfc_retry_pport_discovery(phba);
18505 } else {
18506 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18507 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18508 spin_unlock_irq(&phba->hbalock);
18509 /*
18510 * DEAD FCF event triggered FCF rediscover request
18511 * failed, last resort to fail over as a link down
18512 * to FCF registration.
18513 */
18514 lpfc_sli4_fcf_dead_failthrough(phba);
18515 }
0c9ab6f5
JS
18516 } else {
18517 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18518 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18519 /*
18520 * Start FCF rediscovery wait timer for pending FCF
18521 * before rescan FCF record table.
18522 */
18523 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18524 }
ecfd03c6
JS
18525
18526 mempool_free(mbox, phba->mbox_mem_pool);
18527}
18528
18529/**
3804dc84 18530 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18531 * @phba: pointer to lpfc hba data structure.
18532 *
18533 * This routine is invoked to request for rediscovery of the entire FCF table
18534 * by the port.
18535 **/
18536int
18537lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18538{
18539 LPFC_MBOXQ_t *mbox;
18540 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18541 int rc, length;
18542
0c9ab6f5
JS
18543 /* Cancel retry delay timers to all vports before FCF rediscover */
18544 lpfc_cancel_all_vport_retry_delay_timer(phba);
18545
ecfd03c6
JS
18546 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18547 if (!mbox) {
18548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18549 "2745 Failed to allocate mbox for "
18550 "requesting FCF rediscover.\n");
18551 return -ENOMEM;
18552 }
18553
18554 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18555 sizeof(struct lpfc_sli4_cfg_mhdr));
18556 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18557 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18558 length, LPFC_SLI4_MBX_EMBED);
18559
18560 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18561 /* Set count to 0 for invalidating the entire FCF database */
18562 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18563
18564 /* Issue the mailbox command asynchronously */
18565 mbox->vport = phba->pport;
18566 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18567 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18568
18569 if (rc == MBX_NOT_FINISHED) {
18570 mempool_free(mbox, phba->mbox_mem_pool);
18571 return -EIO;
18572 }
18573 return 0;
18574}
18575
fc2b989b
JS
18576/**
18577 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18578 * @phba: pointer to lpfc hba data structure.
18579 *
18580 * This function is the failover routine as a last resort to the FCF DEAD
18581 * event when driver failed to perform fast FCF failover.
18582 **/
18583void
18584lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18585{
18586 uint32_t link_state;
18587
18588 /*
18589 * Last resort as FCF DEAD event failover will treat this as
18590 * a link down, but save the link state because we don't want
18591 * it to be changed to Link Down unless it is already down.
18592 */
18593 link_state = phba->link_state;
18594 lpfc_linkdown(phba);
18595 phba->link_state = link_state;
18596
18597 /* Unregister FCF if no devices connected to it */
18598 lpfc_unregister_unused_fcf(phba);
18599}
18600
a0c87cbd 18601/**
026abb87 18602 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18603 * @phba: pointer to lpfc hba data structure.
026abb87 18604 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18605 *
026abb87
JS
18606 * This function gets SLI3 port configure region 23 data through memory dump
18607 * mailbox command. When it successfully retrieves data, the size of the data
18608 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18609 **/
026abb87
JS
18610static uint32_t
18611lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18612{
18613 LPFC_MBOXQ_t *pmb = NULL;
18614 MAILBOX_t *mb;
026abb87 18615 uint32_t offset = 0;
a0c87cbd
JS
18616 int rc;
18617
026abb87
JS
18618 if (!rgn23_data)
18619 return 0;
18620
a0c87cbd
JS
18621 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18622 if (!pmb) {
18623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18624 "2600 failed to allocate mailbox memory\n");
18625 return 0;
a0c87cbd
JS
18626 }
18627 mb = &pmb->u.mb;
18628
a0c87cbd
JS
18629 do {
18630 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18631 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18632
18633 if (rc != MBX_SUCCESS) {
18634 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18635 "2601 failed to read config "
18636 "region 23, rc 0x%x Status 0x%x\n",
18637 rc, mb->mbxStatus);
a0c87cbd
JS
18638 mb->un.varDmp.word_cnt = 0;
18639 }
18640 /*
18641 * dump mem may return a zero when finished or we got a
18642 * mailbox error, either way we are done.
18643 */
18644 if (mb->un.varDmp.word_cnt == 0)
18645 break;
18646 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18647 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18648
18649 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18650 rgn23_data + offset,
18651 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18652 offset += mb->un.varDmp.word_cnt;
18653 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18654
026abb87
JS
18655 mempool_free(pmb, phba->mbox_mem_pool);
18656 return offset;
18657}
18658
18659/**
18660 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18661 * @phba: pointer to lpfc hba data structure.
18662 * @rgn23_data: pointer to configure region 23 data.
18663 *
18664 * This function gets SLI4 port configure region 23 data through memory dump
18665 * mailbox command. When it successfully retrieves data, the size of the data
18666 * will be returned, otherwise, 0 will be returned.
18667 **/
18668static uint32_t
18669lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18670{
18671 LPFC_MBOXQ_t *mboxq = NULL;
18672 struct lpfc_dmabuf *mp = NULL;
18673 struct lpfc_mqe *mqe;
18674 uint32_t data_length = 0;
18675 int rc;
18676
18677 if (!rgn23_data)
18678 return 0;
18679
18680 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18681 if (!mboxq) {
18682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18683 "3105 failed to allocate mailbox memory\n");
18684 return 0;
18685 }
18686
18687 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18688 goto out;
18689 mqe = &mboxq->u.mqe;
18690 mp = (struct lpfc_dmabuf *) mboxq->context1;
18691 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18692 if (rc)
18693 goto out;
18694 data_length = mqe->un.mb_words[5];
18695 if (data_length == 0)
18696 goto out;
18697 if (data_length > DMP_RGN23_SIZE) {
18698 data_length = 0;
18699 goto out;
18700 }
18701 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18702out:
18703 mempool_free(mboxq, phba->mbox_mem_pool);
18704 if (mp) {
18705 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18706 kfree(mp);
18707 }
18708 return data_length;
18709}
18710
18711/**
18712 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18713 * @phba: pointer to lpfc hba data structure.
18714 *
18715 * This function read region 23 and parse TLV for port status to
18716 * decide if the user disaled the port. If the TLV indicates the
18717 * port is disabled, the hba_flag is set accordingly.
18718 **/
18719void
18720lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18721{
18722 uint8_t *rgn23_data = NULL;
18723 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18724 uint32_t offset = 0;
18725
18726 /* Get adapter Region 23 data */
18727 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18728 if (!rgn23_data)
18729 goto out;
18730
18731 if (phba->sli_rev < LPFC_SLI_REV4)
18732 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18733 else {
18734 if_type = bf_get(lpfc_sli_intf_if_type,
18735 &phba->sli4_hba.sli_intf);
18736 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18737 goto out;
18738 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18739 }
a0c87cbd
JS
18740
18741 if (!data_size)
18742 goto out;
18743
18744 /* Check the region signature first */
18745 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18747 "2619 Config region 23 has bad signature\n");
18748 goto out;
18749 }
18750 offset += 4;
18751
18752 /* Check the data structure version */
18753 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18755 "2620 Config region 23 has bad version\n");
18756 goto out;
18757 }
18758 offset += 4;
18759
18760 /* Parse TLV entries in the region */
18761 while (offset < data_size) {
18762 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18763 break;
18764 /*
18765 * If the TLV is not driver specific TLV or driver id is
18766 * not linux driver id, skip the record.
18767 */
18768 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18769 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18770 (rgn23_data[offset + 3] != 0)) {
18771 offset += rgn23_data[offset + 1] * 4 + 4;
18772 continue;
18773 }
18774
18775 /* Driver found a driver specific TLV in the config region */
18776 sub_tlv_len = rgn23_data[offset + 1] * 4;
18777 offset += 4;
18778 tlv_offset = 0;
18779
18780 /*
18781 * Search for configured port state sub-TLV.
18782 */
18783 while ((offset < data_size) &&
18784 (tlv_offset < sub_tlv_len)) {
18785 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18786 offset += 4;
18787 tlv_offset += 4;
18788 break;
18789 }
18790 if (rgn23_data[offset] != PORT_STE_TYPE) {
18791 offset += rgn23_data[offset + 1] * 4 + 4;
18792 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18793 continue;
18794 }
18795
18796 /* This HBA contains PORT_STE configured */
18797 if (!rgn23_data[offset + 2])
18798 phba->hba_flag |= LINK_DISABLED;
18799
18800 goto out;
18801 }
18802 }
026abb87 18803
a0c87cbd 18804out:
a0c87cbd
JS
18805 kfree(rgn23_data);
18806 return;
18807}
695a814e 18808
52d52440
JS
18809/**
18810 * lpfc_wr_object - write an object to the firmware
18811 * @phba: HBA structure that indicates port to create a queue on.
18812 * @dmabuf_list: list of dmabufs to write to the port.
18813 * @size: the total byte value of the objects to write to the port.
18814 * @offset: the current offset to be used to start the transfer.
18815 *
18816 * This routine will create a wr_object mailbox command to send to the port.
18817 * the mailbox command will be constructed using the dma buffers described in
18818 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18819 * BDEs that the imbedded mailbox can support. The @offset variable will be
18820 * used to indicate the starting offset of the transfer and will also return
18821 * the offset after the write object mailbox has completed. @size is used to
18822 * determine the end of the object and whether the eof bit should be set.
18823 *
18824 * Return 0 is successful and offset will contain the the new offset to use
18825 * for the next write.
18826 * Return negative value for error cases.
18827 **/
18828int
18829lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18830 uint32_t size, uint32_t *offset)
18831{
18832 struct lpfc_mbx_wr_object *wr_object;
18833 LPFC_MBOXQ_t *mbox;
18834 int rc = 0, i = 0;
18835 uint32_t shdr_status, shdr_add_status;
18836 uint32_t mbox_tmo;
18837 union lpfc_sli4_cfg_shdr *shdr;
18838 struct lpfc_dmabuf *dmabuf;
18839 uint32_t written = 0;
18840
18841 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18842 if (!mbox)
18843 return -ENOMEM;
18844
18845 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18846 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18847 sizeof(struct lpfc_mbx_wr_object) -
18848 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18849
18850 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18851 wr_object->u.request.write_offset = *offset;
18852 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18853 wr_object->u.request.object_name[0] =
18854 cpu_to_le32(wr_object->u.request.object_name[0]);
18855 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18856 list_for_each_entry(dmabuf, dmabuf_list, list) {
18857 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18858 break;
18859 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18860 wr_object->u.request.bde[i].addrHigh =
18861 putPaddrHigh(dmabuf->phys);
18862 if (written + SLI4_PAGE_SIZE >= size) {
18863 wr_object->u.request.bde[i].tus.f.bdeSize =
18864 (size - written);
18865 written += (size - written);
18866 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18867 } else {
18868 wr_object->u.request.bde[i].tus.f.bdeSize =
18869 SLI4_PAGE_SIZE;
18870 written += SLI4_PAGE_SIZE;
18871 }
18872 i++;
18873 }
18874 wr_object->u.request.bde_count = i;
18875 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18876 if (!phba->sli4_hba.intr_enable)
18877 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18878 else {
a183a15f 18879 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18880 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18881 }
18882 /* The IOCTL status is embedded in the mailbox subheader. */
18883 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18884 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18885 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18886 if (rc != MBX_TIMEOUT)
18887 mempool_free(mbox, phba->mbox_mem_pool);
18888 if (shdr_status || shdr_add_status || rc) {
18889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18890 "3025 Write Object mailbox failed with "
18891 "status x%x add_status x%x, mbx status x%x\n",
18892 shdr_status, shdr_add_status, rc);
18893 rc = -ENXIO;
1feb8204 18894 *offset = shdr_add_status;
52d52440
JS
18895 } else
18896 *offset += wr_object->u.response.actual_write_length;
18897 return rc;
18898}
18899
695a814e
JS
18900/**
18901 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18902 * @vport: pointer to vport data structure.
18903 *
18904 * This function iterate through the mailboxq and clean up all REG_LOGIN
18905 * and REG_VPI mailbox commands associated with the vport. This function
18906 * is called when driver want to restart discovery of the vport due to
18907 * a Clear Virtual Link event.
18908 **/
18909void
18910lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18911{
18912 struct lpfc_hba *phba = vport->phba;
18913 LPFC_MBOXQ_t *mb, *nextmb;
18914 struct lpfc_dmabuf *mp;
78730cfe 18915 struct lpfc_nodelist *ndlp;
d439d286 18916 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18917 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18918 LIST_HEAD(mbox_cmd_list);
63e801ce 18919 uint8_t restart_loop;
695a814e 18920
d439d286 18921 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18922 spin_lock_irq(&phba->hbalock);
18923 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18924 if (mb->vport != vport)
18925 continue;
18926
18927 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18928 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18929 continue;
18930
d439d286
JS
18931 list_del(&mb->list);
18932 list_add_tail(&mb->list, &mbox_cmd_list);
18933 }
18934 /* Clean up active mailbox command with the vport */
18935 mb = phba->sli.mbox_active;
18936 if (mb && (mb->vport == vport)) {
18937 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18938 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18939 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18940 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18941 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18942 /* Put reference count for delayed processing */
18943 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18944 /* Unregister the RPI when mailbox complete */
18945 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18946 }
18947 }
63e801ce
JS
18948 /* Cleanup any mailbox completions which are not yet processed */
18949 do {
18950 restart_loop = 0;
18951 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18952 /*
18953 * If this mailox is already processed or it is
18954 * for another vport ignore it.
18955 */
18956 if ((mb->vport != vport) ||
18957 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18958 continue;
18959
18960 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18961 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18962 continue;
18963
18964 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18965 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18966 ndlp = (struct lpfc_nodelist *)mb->context2;
18967 /* Unregister the RPI when mailbox complete */
18968 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18969 restart_loop = 1;
18970 spin_unlock_irq(&phba->hbalock);
18971 spin_lock(shost->host_lock);
18972 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18973 spin_unlock(shost->host_lock);
18974 spin_lock_irq(&phba->hbalock);
18975 break;
18976 }
18977 }
18978 } while (restart_loop);
18979
d439d286
JS
18980 spin_unlock_irq(&phba->hbalock);
18981
18982 /* Release the cleaned-up mailbox commands */
18983 while (!list_empty(&mbox_cmd_list)) {
18984 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18985 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18986 mp = (struct lpfc_dmabuf *) (mb->context1);
18987 if (mp) {
18988 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18989 kfree(mp);
18990 }
78730cfe 18991 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18992 mb->context2 = NULL;
78730cfe 18993 if (ndlp) {
ec21b3b0 18994 spin_lock(shost->host_lock);
589a52d6 18995 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18996 spin_unlock(shost->host_lock);
78730cfe 18997 lpfc_nlp_put(ndlp);
78730cfe 18998 }
695a814e 18999 }
695a814e
JS
19000 mempool_free(mb, phba->mbox_mem_pool);
19001 }
d439d286
JS
19002
19003 /* Release the ndlp with the cleaned-up active mailbox command */
19004 if (act_mbx_ndlp) {
19005 spin_lock(shost->host_lock);
19006 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19007 spin_unlock(shost->host_lock);
19008 lpfc_nlp_put(act_mbx_ndlp);
695a814e 19009 }
695a814e
JS
19010}
19011
2a9bf3d0
JS
19012/**
19013 * lpfc_drain_txq - Drain the txq
19014 * @phba: Pointer to HBA context object.
19015 *
19016 * This function attempt to submit IOCBs on the txq
19017 * to the adapter. For SLI4 adapters, the txq contains
19018 * ELS IOCBs that have been deferred because the there
19019 * are no SGLs. This congestion can occur with large
19020 * vport counts during node discovery.
19021 **/
19022
19023uint32_t
19024lpfc_drain_txq(struct lpfc_hba *phba)
19025{
19026 LIST_HEAD(completions);
895427bd 19027 struct lpfc_sli_ring *pring;
2e706377 19028 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
19029 unsigned long iflags = 0;
19030 char *fail_msg = NULL;
19031 struct lpfc_sglq *sglq;
205e8240 19032 union lpfc_wqe128 wqe;
a2fc4aef 19033 uint32_t txq_cnt = 0;
2a9bf3d0 19034
895427bd 19035 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
19036 if (unlikely(!pring))
19037 return 0;
895427bd 19038
398d81c9 19039 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
19040 list_for_each_entry(piocbq, &pring->txq, list) {
19041 txq_cnt++;
19042 }
19043
19044 if (txq_cnt > pring->txq_max)
19045 pring->txq_max = txq_cnt;
2a9bf3d0 19046
398d81c9 19047 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19048
0e9bb8d7 19049 while (!list_empty(&pring->txq)) {
398d81c9 19050 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 19051
19ca7609 19052 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 19053 if (!piocbq) {
398d81c9 19054 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
19055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19056 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 19057 txq_cnt);
a629852a
JS
19058 break;
19059 }
895427bd 19060 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 19061 if (!sglq) {
19ca7609 19062 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 19063 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19064 break;
2a9bf3d0 19065 }
0e9bb8d7 19066 txq_cnt--;
2a9bf3d0
JS
19067
19068 /* The xri and iocb resources secured,
19069 * attempt to issue request
19070 */
6d368e53 19071 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
19072 piocbq->sli4_xritag = sglq->sli4_xritag;
19073 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19074 fail_msg = "to convert bpl to sgl";
205e8240 19075 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
2a9bf3d0 19076 fail_msg = "to convert iocb to wqe";
205e8240 19077 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
2a9bf3d0
JS
19078 fail_msg = " - Wq is full";
19079 else
19080 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19081
19082 if (fail_msg) {
19083 /* Failed means we can't issue and need to cancel */
19084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19085 "2822 IOCB failed %s iotag 0x%x "
19086 "xri 0x%x\n",
19087 fail_msg,
19088 piocbq->iotag, piocbq->sli4_xritag);
19089 list_add_tail(&piocbq->list, &completions);
19090 }
398d81c9 19091 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
19092 }
19093
2a9bf3d0
JS
19094 /* Cancel all the IOCBs that cannot be issued */
19095 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19096 IOERR_SLI_ABORTED);
19097
0e9bb8d7 19098 return txq_cnt;
2a9bf3d0 19099}
895427bd
JS
19100
19101/**
19102 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19103 * @phba: Pointer to HBA context object.
19104 * @pwqe: Pointer to command WQE.
19105 * @sglq: Pointer to the scatter gather queue object.
19106 *
19107 * This routine converts the bpl or bde that is in the WQE
19108 * to a sgl list for the sli4 hardware. The physical address
19109 * of the bpl/bde is converted back to a virtual address.
19110 * If the WQE contains a BPL then the list of BDE's is
19111 * converted to sli4_sge's. If the WQE contains a single
19112 * BDE then it is converted to a single sli_sge.
19113 * The WQE is still in cpu endianness so the contents of
19114 * the bpl can be used without byte swapping.
19115 *
19116 * Returns valid XRI = Success, NO_XRI = Failure.
19117 */
19118static uint16_t
19119lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19120 struct lpfc_sglq *sglq)
19121{
19122 uint16_t xritag = NO_XRI;
19123 struct ulp_bde64 *bpl = NULL;
19124 struct ulp_bde64 bde;
19125 struct sli4_sge *sgl = NULL;
19126 struct lpfc_dmabuf *dmabuf;
205e8240 19127 union lpfc_wqe128 *wqe;
895427bd
JS
19128 int numBdes = 0;
19129 int i = 0;
19130 uint32_t offset = 0; /* accumulated offset in the sg request list */
19131 int inbound = 0; /* number of sg reply entries inbound from firmware */
19132 uint32_t cmd;
19133
19134 if (!pwqeq || !sglq)
19135 return xritag;
19136
19137 sgl = (struct sli4_sge *)sglq->sgl;
19138 wqe = &pwqeq->wqe;
19139 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19140
19141 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19142 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19143 return sglq->sli4_xritag;
19144 numBdes = pwqeq->rsvd2;
19145 if (numBdes) {
19146 /* The addrHigh and addrLow fields within the WQE
19147 * have not been byteswapped yet so there is no
19148 * need to swap them back.
19149 */
19150 if (pwqeq->context3)
19151 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19152 else
19153 return xritag;
19154
19155 bpl = (struct ulp_bde64 *)dmabuf->virt;
19156 if (!bpl)
19157 return xritag;
19158
19159 for (i = 0; i < numBdes; i++) {
19160 /* Should already be byte swapped. */
19161 sgl->addr_hi = bpl->addrHigh;
19162 sgl->addr_lo = bpl->addrLow;
19163
19164 sgl->word2 = le32_to_cpu(sgl->word2);
19165 if ((i+1) == numBdes)
19166 bf_set(lpfc_sli4_sge_last, sgl, 1);
19167 else
19168 bf_set(lpfc_sli4_sge_last, sgl, 0);
19169 /* swap the size field back to the cpu so we
19170 * can assign it to the sgl.
19171 */
19172 bde.tus.w = le32_to_cpu(bpl->tus.w);
19173 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19174 /* The offsets in the sgl need to be accumulated
19175 * separately for the request and reply lists.
19176 * The request is always first, the reply follows.
19177 */
19178 switch (cmd) {
19179 case CMD_GEN_REQUEST64_WQE:
19180 /* add up the reply sg entries */
19181 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19182 inbound++;
19183 /* first inbound? reset the offset */
19184 if (inbound == 1)
19185 offset = 0;
19186 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19187 bf_set(lpfc_sli4_sge_type, sgl,
19188 LPFC_SGE_TYPE_DATA);
19189 offset += bde.tus.f.bdeSize;
19190 break;
19191 case CMD_FCP_TRSP64_WQE:
19192 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19193 bf_set(lpfc_sli4_sge_type, sgl,
19194 LPFC_SGE_TYPE_DATA);
19195 break;
19196 case CMD_FCP_TSEND64_WQE:
19197 case CMD_FCP_TRECEIVE64_WQE:
19198 bf_set(lpfc_sli4_sge_type, sgl,
19199 bpl->tus.f.bdeFlags);
19200 if (i < 3)
19201 offset = 0;
19202 else
19203 offset += bde.tus.f.bdeSize;
19204 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19205 break;
19206 }
19207 sgl->word2 = cpu_to_le32(sgl->word2);
19208 bpl++;
19209 sgl++;
19210 }
19211 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19212 /* The addrHigh and addrLow fields of the BDE have not
19213 * been byteswapped yet so they need to be swapped
19214 * before putting them in the sgl.
19215 */
19216 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19217 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19218 sgl->word2 = le32_to_cpu(sgl->word2);
19219 bf_set(lpfc_sli4_sge_last, sgl, 1);
19220 sgl->word2 = cpu_to_le32(sgl->word2);
19221 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19222 }
19223 return sglq->sli4_xritag;
19224}
19225
19226/**
19227 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19228 * @phba: Pointer to HBA context object.
19229 * @ring_number: Base sli ring number
19230 * @pwqe: Pointer to command WQE.
19231 **/
19232int
19233lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19234 struct lpfc_iocbq *pwqe)
19235{
205e8240 19236 union lpfc_wqe128 *wqe = &pwqe->wqe;
f358dd0c 19237 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
19238 struct lpfc_queue *wq;
19239 struct lpfc_sglq *sglq;
19240 struct lpfc_sli_ring *pring;
19241 unsigned long iflags;
cd22d605 19242 uint32_t ret = 0;
895427bd
JS
19243
19244 /* NVME_LS and NVME_LS ABTS requests. */
19245 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19246 pring = phba->sli4_hba.nvmels_wq->pring;
19247 spin_lock_irqsave(&pring->ring_lock, iflags);
19248 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19249 if (!sglq) {
19250 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19251 return WQE_BUSY;
19252 }
19253 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19254 pwqe->sli4_xritag = sglq->sli4_xritag;
19255 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19256 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19257 return WQE_ERROR;
19258 }
19259 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19260 pwqe->sli4_xritag);
cd22d605
DK
19261 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19262 if (ret) {
895427bd 19263 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19264 return ret;
895427bd 19265 }
cd22d605 19266
895427bd
JS
19267 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19268 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19269 return 0;
19270 }
19271
19272 /* NVME_FCREQ and NVME_ABTS requests */
19273 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19274 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19275 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19276
19277 spin_lock_irqsave(&pring->ring_lock, iflags);
19278 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19279 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19280 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19281 ret = lpfc_sli4_wq_put(wq, wqe);
19282 if (ret) {
895427bd 19283 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19284 return ret;
895427bd
JS
19285 }
19286 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19287 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19288 return 0;
19289 }
19290
f358dd0c
JS
19291 /* NVMET requests */
19292 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19293 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19294 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19295
19296 spin_lock_irqsave(&pring->ring_lock, iflags);
19297 ctxp = pwqe->context2;
6c621a22 19298 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19299 if (pwqe->sli4_xritag == NO_XRI) {
19300 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19301 pwqe->sli4_xritag = sglq->sli4_xritag;
19302 }
19303 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19304 pwqe->sli4_xritag);
19305 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19306 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19307 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19308 ret = lpfc_sli4_wq_put(wq, wqe);
19309 if (ret) {
f358dd0c 19310 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19311 return ret;
f358dd0c
JS
19312 }
19313 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19314 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19315 return 0;
19316 }
895427bd
JS
19317 return WQE_ERROR;
19318}