]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix a duplicate 0711 log message number.
[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 *
3e21d1cb 5 * “Broadcom” refers to Broadcom Inc. 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
48f8fdb4
JS
99#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
100/**
101 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
102 * @srcp: Source memory pointer.
103 * @destp: Destination memory pointer.
104 * @cnt: Number of words required to be copied.
105 * Must be a multiple of sizeof(uint64_t)
106 *
107 * This function is used for copying data between driver memory
108 * and the SLI WQ. This function also changes the endianness
109 * of each word if native endianness is different from SLI
110 * endianness. This function can be called with or without
111 * lock.
112 **/
113void
114lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
115{
116 uint64_t *src = srcp;
117 uint64_t *dest = destp;
118 int i;
119
120 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
121 *dest++ = *src++;
122}
123#else
124#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
125#endif
126
4f774513
JS
127/**
128 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
129 * @q: The Work Queue to operate on.
130 * @wqe: The work Queue Entry to put on the Work queue.
131 *
132 * This routine will copy the contents of @wqe to the next available entry on
133 * the @q. This function will then ring the Work Queue Doorbell to signal the
134 * HBA to start processing the Work Queue Entry. This function returns 0 if
135 * successful. If no entries are available on @q then this function will return
136 * -ENOMEM.
137 * The caller is expected to hold the hbalock when calling this routine.
138 **/
cd22d605 139static int
205e8240 140lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
4f774513 141{
2e90f4b5 142 union lpfc_wqe *temp_wqe;
4f774513
JS
143 struct lpfc_register doorbell;
144 uint32_t host_index;
027140ea 145 uint32_t idx;
1351e69f
JS
146 uint32_t i = 0;
147 uint8_t *tmp;
5cc167dd 148 u32 if_type;
4f774513 149
2e90f4b5
JS
150 /* sanity check on queue memory */
151 if (unlikely(!q))
152 return -ENOMEM;
153 temp_wqe = q->qe[q->host_index].wqe;
154
4f774513 155 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
156 idx = ((q->host_index + 1) % q->entry_count);
157 if (idx == q->hba_index) {
b84daac9 158 q->WQ_overflow++;
cd22d605 159 return -EBUSY;
b84daac9
JS
160 }
161 q->WQ_posted++;
4f774513 162 /* set consumption flag every once in a while */
ff78d8f9 163 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 164 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
04673e38
JS
165 else
166 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
fedd3b7b
JS
167 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
168 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
48f8fdb4 169 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
1351e69f
JS
170 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
171 /* write to DPP aperture taking advatage of Combined Writes */
4c06619f
JS
172 tmp = (uint8_t *)temp_wqe;
173#ifdef __raw_writeq
1351e69f 174 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
4c06619f
JS
175 __raw_writeq(*((uint64_t *)(tmp + i)),
176 q->dpp_regaddr + i);
177#else
178 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
179 __raw_writel(*((uint32_t *)(tmp + i)),
180 q->dpp_regaddr + i);
181#endif
1351e69f
JS
182 }
183 /* ensure WQE bcopy and DPP flushed before doorbell write */
6b3b3bdb 184 wmb();
4f774513
JS
185
186 /* Update the host index before invoking device */
187 host_index = q->host_index;
027140ea
JS
188
189 q->host_index = idx;
4f774513
JS
190
191 /* Ring Doorbell */
192 doorbell.word0 = 0;
962bc51b 193 if (q->db_format == LPFC_DB_LIST_FORMAT) {
1351e69f
JS
194 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
195 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
196 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
197 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
198 q->dpp_id);
199 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
200 q->queue_id);
201 } else {
202 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
1351e69f 203 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
5cc167dd
JS
204
205 /* Leave bits <23:16> clear for if_type 6 dpp */
206 if_type = bf_get(lpfc_sli_intf_if_type,
207 &q->phba->sli4_hba.sli_intf);
208 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
209 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
210 host_index);
1351e69f 211 }
962bc51b
JS
212 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
213 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
214 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
215 } else {
216 return -EINVAL;
217 }
218 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
219
220 return 0;
221}
222
223/**
224 * lpfc_sli4_wq_release - Updates internal hba index for WQ
225 * @q: The Work Queue to operate on.
226 * @index: The index to advance the hba index to.
227 *
228 * This routine will update the HBA index of a queue to reflect consumption of
229 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
230 * an entry the host calls this function to update the queue's internal
231 * pointers. This routine returns the number of entries that were consumed by
232 * the HBA.
233 **/
234static uint32_t
235lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
236{
237 uint32_t released = 0;
238
2e90f4b5
JS
239 /* sanity check on queue memory */
240 if (unlikely(!q))
241 return 0;
242
4f774513
JS
243 if (q->hba_index == index)
244 return 0;
245 do {
246 q->hba_index = ((q->hba_index + 1) % q->entry_count);
247 released++;
248 } while (q->hba_index != index);
249 return released;
250}
251
252/**
253 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
254 * @q: The Mailbox Queue to operate on.
255 * @wqe: The Mailbox Queue Entry to put on the Work queue.
256 *
257 * This routine will copy the contents of @mqe to the next available entry on
258 * the @q. This function will then ring the Work Queue Doorbell to signal the
259 * HBA to start processing the Work Queue Entry. This function returns 0 if
260 * successful. If no entries are available on @q then this function will return
261 * -ENOMEM.
262 * The caller is expected to hold the hbalock when calling this routine.
263 **/
264static uint32_t
265lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
266{
2e90f4b5 267 struct lpfc_mqe *temp_mqe;
4f774513 268 struct lpfc_register doorbell;
4f774513 269
2e90f4b5
JS
270 /* sanity check on queue memory */
271 if (unlikely(!q))
272 return -ENOMEM;
273 temp_mqe = q->qe[q->host_index].mqe;
274
4f774513
JS
275 /* If the host has not yet processed the next entry then we are done */
276 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
277 return -ENOMEM;
48f8fdb4 278 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
4f774513
JS
279 /* Save off the mailbox pointer for completion */
280 q->phba->mbox = (MAILBOX_t *)temp_mqe;
281
282 /* Update the host index before invoking device */
4f774513
JS
283 q->host_index = ((q->host_index + 1) % q->entry_count);
284
285 /* Ring Doorbell */
286 doorbell.word0 = 0;
287 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
288 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
289 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
290 return 0;
291}
292
293/**
294 * lpfc_sli4_mq_release - Updates internal hba index for MQ
295 * @q: The Mailbox Queue to operate on.
296 *
297 * This routine will update the HBA index of a queue to reflect consumption of
298 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
299 * an entry the host calls this function to update the queue's internal
300 * pointers. This routine returns the number of entries that were consumed by
301 * the HBA.
302 **/
303static uint32_t
304lpfc_sli4_mq_release(struct lpfc_queue *q)
305{
2e90f4b5
JS
306 /* sanity check on queue memory */
307 if (unlikely(!q))
308 return 0;
309
4f774513
JS
310 /* Clear the mailbox pointer for completion */
311 q->phba->mbox = NULL;
312 q->hba_index = ((q->hba_index + 1) % q->entry_count);
313 return 1;
314}
315
316/**
317 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
318 * @q: The Event Queue to get the first valid EQE from
319 *
320 * This routine will get the first valid Event Queue Entry from @q, update
321 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
322 * the Queue (no more work to do), or the Queue is full of EQEs that have been
323 * processed, but not popped back to the HBA then this routine will return NULL.
324 **/
325static struct lpfc_eqe *
326lpfc_sli4_eq_get(struct lpfc_queue *q)
327{
7365f6fd 328 struct lpfc_hba *phba;
2e90f4b5 329 struct lpfc_eqe *eqe;
027140ea 330 uint32_t idx;
2e90f4b5
JS
331
332 /* sanity check on queue memory */
333 if (unlikely(!q))
334 return NULL;
7365f6fd 335 phba = q->phba;
2e90f4b5 336 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
337
338 /* If the next EQE is not valid then we are done */
7365f6fd 339 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
4f774513
JS
340 return NULL;
341 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
342 idx = ((q->hba_index + 1) % q->entry_count);
343 if (idx == q->host_index)
4f774513
JS
344 return NULL;
345
027140ea 346 q->hba_index = idx;
7365f6fd
JS
347 /* if the index wrapped around, toggle the valid bit */
348 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
349 q->qe_valid = (q->qe_valid) ? 0 : 1;
350
27f344eb
JS
351
352 /*
353 * insert barrier for instruction interlock : data from the hardware
354 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
355 * upon. Speculative instructions were allowing a bcopy at the start
356 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
357 * after our return, to copy data before the valid bit check above
358 * was done. As such, some of the copied data was stale. The barrier
359 * ensures the check is before any data is copied.
27f344eb
JS
360 */
361 mb();
4f774513
JS
362 return eqe;
363}
364
ba20c853
JS
365/**
366 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
367 * @q: The Event Queue to disable interrupts
368 *
369 **/
b71413dd 370inline void
ba20c853
JS
371lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
372{
373 struct lpfc_register doorbell;
374
375 doorbell.word0 = 0;
376 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
377 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
378 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
379 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
380 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 381 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
ba20c853
JS
382}
383
27d6ac0a
JS
384/**
385 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
386 * @q: The Event Queue to disable interrupts
387 *
388 **/
389inline void
390lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
391{
392 struct lpfc_register doorbell;
393
394 doorbell.word0 = 0;
aad59d5d 395 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
27d6ac0a
JS
396 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
397}
398
4f774513
JS
399/**
400 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
401 * @q: The Event Queue that the host has completed processing for.
402 * @arm: Indicates whether the host wants to arms this CQ.
403 *
404 * This routine will mark all Event Queue Entries on @q, from the last
405 * known completed entry to the last entry that was processed, as completed
406 * by clearing the valid bit for each completion queue entry. Then it will
407 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
408 * The internal host index in the @q will be updated by this routine to indicate
409 * that the host has finished processing the entries. The @arm parameter
410 * indicates that the queue should be rearmed when ringing the doorbell.
411 *
412 * This function will return the number of EQEs that were popped.
413 **/
414uint32_t
415lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
416{
417 uint32_t released = 0;
7365f6fd 418 struct lpfc_hba *phba;
4f774513
JS
419 struct lpfc_eqe *temp_eqe;
420 struct lpfc_register doorbell;
421
2e90f4b5
JS
422 /* sanity check on queue memory */
423 if (unlikely(!q))
424 return 0;
7365f6fd 425 phba = q->phba;
2e90f4b5 426
4f774513
JS
427 /* while there are valid entries */
428 while (q->hba_index != q->host_index) {
7365f6fd
JS
429 if (!phba->sli4_hba.pc_sli4_params.eqav) {
430 temp_eqe = q->qe[q->host_index].eqe;
431 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
432 }
4f774513
JS
433 released++;
434 q->host_index = ((q->host_index + 1) % q->entry_count);
435 }
436 if (unlikely(released == 0 && !arm))
437 return 0;
438
439 /* ring doorbell for number popped */
440 doorbell.word0 = 0;
441 if (arm) {
442 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
443 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
444 }
445 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
446 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
447 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
448 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
449 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 450 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
a747c9ce
JS
451 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
452 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
9dd35425 453 readl(q->phba->sli4_hba.EQDBregaddr);
4f774513
JS
454 return released;
455}
456
27d6ac0a
JS
457/**
458 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
459 * @q: The Event Queue that the host has completed processing for.
460 * @arm: Indicates whether the host wants to arms this CQ.
461 *
462 * This routine will mark all Event Queue Entries on @q, from the last
463 * known completed entry to the last entry that was processed, as completed
464 * by clearing the valid bit for each completion queue entry. Then it will
465 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
466 * The internal host index in the @q will be updated by this routine to indicate
467 * that the host has finished processing the entries. The @arm parameter
468 * indicates that the queue should be rearmed when ringing the doorbell.
469 *
470 * This function will return the number of EQEs that were popped.
471 **/
472uint32_t
473lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
474{
475 uint32_t released = 0;
7365f6fd 476 struct lpfc_hba *phba;
27d6ac0a
JS
477 struct lpfc_eqe *temp_eqe;
478 struct lpfc_register doorbell;
479
480 /* sanity check on queue memory */
481 if (unlikely(!q))
482 return 0;
7365f6fd 483 phba = q->phba;
27d6ac0a
JS
484
485 /* while there are valid entries */
486 while (q->hba_index != q->host_index) {
7365f6fd
JS
487 if (!phba->sli4_hba.pc_sli4_params.eqav) {
488 temp_eqe = q->qe[q->host_index].eqe;
489 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
490 }
27d6ac0a
JS
491 released++;
492 q->host_index = ((q->host_index + 1) % q->entry_count);
493 }
494 if (unlikely(released == 0 && !arm))
495 return 0;
496
497 /* ring doorbell for number popped */
498 doorbell.word0 = 0;
499 if (arm)
500 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
501 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
502 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
503 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
504 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
505 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
506 readl(q->phba->sli4_hba.EQDBregaddr);
507 return released;
508}
509
4f774513
JS
510/**
511 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
512 * @q: The Completion Queue to get the first valid CQE from
513 *
514 * This routine will get the first valid Completion Queue Entry from @q, update
515 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
516 * the Queue (no more work to do), or the Queue is full of CQEs that have been
517 * processed, but not popped back to the HBA then this routine will return NULL.
518 **/
519static struct lpfc_cqe *
520lpfc_sli4_cq_get(struct lpfc_queue *q)
521{
7365f6fd 522 struct lpfc_hba *phba;
4f774513 523 struct lpfc_cqe *cqe;
027140ea 524 uint32_t idx;
4f774513 525
2e90f4b5
JS
526 /* sanity check on queue memory */
527 if (unlikely(!q))
528 return NULL;
7365f6fd
JS
529 phba = q->phba;
530 cqe = q->qe[q->hba_index].cqe;
2e90f4b5 531
4f774513 532 /* If the next CQE is not valid then we are done */
7365f6fd 533 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
4f774513
JS
534 return NULL;
535 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
536 idx = ((q->hba_index + 1) % q->entry_count);
537 if (idx == q->host_index)
4f774513
JS
538 return NULL;
539
027140ea 540 q->hba_index = idx;
7365f6fd
JS
541 /* if the index wrapped around, toggle the valid bit */
542 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
543 q->qe_valid = (q->qe_valid) ? 0 : 1;
27f344eb
JS
544
545 /*
546 * insert barrier for instruction interlock : data from the hardware
547 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
548 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
549 * instructions allowing action on content before valid bit checked,
550 * add barrier here as well. May not be needed as "content" is a
551 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
552 */
553 mb();
4f774513
JS
554 return cqe;
555}
556
557/**
558 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
559 * @q: The Completion Queue that the host has completed processing for.
560 * @arm: Indicates whether the host wants to arms this CQ.
561 *
562 * This routine will mark all Completion queue entries on @q, from the last
563 * known completed entry to the last entry that was processed, as completed
564 * by clearing the valid bit for each completion queue entry. Then it will
565 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
566 * The internal host index in the @q will be updated by this routine to indicate
567 * that the host has finished processing the entries. The @arm parameter
568 * indicates that the queue should be rearmed when ringing the doorbell.
569 *
570 * This function will return the number of CQEs that were released.
571 **/
572uint32_t
573lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
574{
575 uint32_t released = 0;
7365f6fd 576 struct lpfc_hba *phba;
4f774513
JS
577 struct lpfc_cqe *temp_qe;
578 struct lpfc_register doorbell;
579
2e90f4b5
JS
580 /* sanity check on queue memory */
581 if (unlikely(!q))
582 return 0;
7365f6fd
JS
583 phba = q->phba;
584
4f774513
JS
585 /* while there are valid entries */
586 while (q->hba_index != q->host_index) {
7365f6fd
JS
587 if (!phba->sli4_hba.pc_sli4_params.cqav) {
588 temp_qe = q->qe[q->host_index].cqe;
589 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
590 }
4f774513
JS
591 released++;
592 q->host_index = ((q->host_index + 1) % q->entry_count);
593 }
594 if (unlikely(released == 0 && !arm))
595 return 0;
596
597 /* ring doorbell for number popped */
598 doorbell.word0 = 0;
599 if (arm)
600 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
601 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
602 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
603 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
604 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
605 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
9dd35425 606 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
4f774513
JS
607 return released;
608}
609
27d6ac0a
JS
610/**
611 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
612 * @q: The Completion Queue that the host has completed processing for.
613 * @arm: Indicates whether the host wants to arms this CQ.
614 *
615 * This routine will mark all Completion queue entries on @q, from the last
616 * known completed entry to the last entry that was processed, as completed
617 * by clearing the valid bit for each completion queue entry. Then it will
618 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
619 * The internal host index in the @q will be updated by this routine to indicate
620 * that the host has finished processing the entries. The @arm parameter
621 * indicates that the queue should be rearmed when ringing the doorbell.
622 *
623 * This function will return the number of CQEs that were released.
624 **/
625uint32_t
626lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
627{
628 uint32_t released = 0;
7365f6fd 629 struct lpfc_hba *phba;
27d6ac0a
JS
630 struct lpfc_cqe *temp_qe;
631 struct lpfc_register doorbell;
632
633 /* sanity check on queue memory */
634 if (unlikely(!q))
635 return 0;
7365f6fd
JS
636 phba = q->phba;
637
27d6ac0a
JS
638 /* while there are valid entries */
639 while (q->hba_index != q->host_index) {
7365f6fd
JS
640 if (!phba->sli4_hba.pc_sli4_params.cqav) {
641 temp_qe = q->qe[q->host_index].cqe;
642 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
643 }
27d6ac0a
JS
644 released++;
645 q->host_index = ((q->host_index + 1) % q->entry_count);
646 }
647 if (unlikely(released == 0 && !arm))
648 return 0;
649
650 /* ring doorbell for number popped */
651 doorbell.word0 = 0;
652 if (arm)
653 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
654 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
655 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
656 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
657 return released;
658}
659
4f774513
JS
660/**
661 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
662 * @q: The Header Receive Queue to operate on.
663 * @wqe: The Receive Queue Entry to put on the Receive queue.
664 *
665 * This routine will copy the contents of @wqe to the next available entry on
666 * the @q. This function will then ring the Receive Queue Doorbell to signal the
667 * HBA to start processing the Receive Queue Entry. This function returns the
668 * index that the rqe was copied to if successful. If no entries are available
669 * on @q then this function will return -ENOMEM.
670 * The caller is expected to hold the hbalock when calling this routine.
671 **/
895427bd 672int
4f774513
JS
673lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
674 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
675{
2e90f4b5
JS
676 struct lpfc_rqe *temp_hrqe;
677 struct lpfc_rqe *temp_drqe;
4f774513 678 struct lpfc_register doorbell;
cbc5de1b
JS
679 int hq_put_index;
680 int dq_put_index;
4f774513 681
2e90f4b5
JS
682 /* sanity check on queue memory */
683 if (unlikely(!hq) || unlikely(!dq))
684 return -ENOMEM;
cbc5de1b
JS
685 hq_put_index = hq->host_index;
686 dq_put_index = dq->host_index;
687 temp_hrqe = hq->qe[hq_put_index].rqe;
688 temp_drqe = dq->qe[dq_put_index].rqe;
2e90f4b5 689
4f774513
JS
690 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
691 return -EINVAL;
cbc5de1b 692 if (hq_put_index != dq_put_index)
4f774513
JS
693 return -EINVAL;
694 /* If the host has not yet processed the next entry then we are done */
cbc5de1b 695 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513 696 return -EBUSY;
48f8fdb4
JS
697 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
698 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
4f774513
JS
699
700 /* Update the host index to point to the next slot */
cbc5de1b
JS
701 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
702 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
61f3d4bf 703 hq->RQ_buf_posted++;
4f774513
JS
704
705 /* Ring The Header Receive Queue Doorbell */
73d91e50 706 if (!(hq->host_index % hq->entry_repost)) {
4f774513 707 doorbell.word0 = 0;
962bc51b
JS
708 if (hq->db_format == LPFC_DB_RING_FORMAT) {
709 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
710 hq->entry_repost);
711 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
712 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
713 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
714 hq->entry_repost);
715 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
716 hq->host_index);
717 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
718 } else {
719 return -EINVAL;
720 }
721 writel(doorbell.word0, hq->db_regaddr);
4f774513 722 }
cbc5de1b 723 return hq_put_index;
4f774513
JS
724}
725
726/**
727 * lpfc_sli4_rq_release - Updates internal hba index for RQ
728 * @q: The Header Receive Queue to operate on.
729 *
730 * This routine will update the HBA index of a queue to reflect consumption of
731 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
732 * consumed an entry the host calls this function to update the queue's
733 * internal pointers. This routine returns the number of entries that were
734 * consumed by the HBA.
735 **/
736static uint32_t
737lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
738{
2e90f4b5
JS
739 /* sanity check on queue memory */
740 if (unlikely(!hq) || unlikely(!dq))
741 return 0;
742
4f774513
JS
743 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
744 return 0;
745 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
746 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
747 return 1;
748}
749
e59058c4 750/**
3621a710 751 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
752 * @phba: Pointer to HBA context object.
753 * @pring: Pointer to driver SLI ring object.
754 *
755 * This function returns pointer to next command iocb entry
756 * in the command ring. The caller must hold hbalock to prevent
757 * other threads consume the next command iocb.
758 * SLI-2/SLI-3 provide different sized iocbs.
759 **/
ed957684
JS
760static inline IOCB_t *
761lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
762{
7e56aa25
JS
763 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
764 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
765}
766
e59058c4 767/**
3621a710 768 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
769 * @phba: Pointer to HBA context object.
770 * @pring: Pointer to driver SLI ring object.
771 *
772 * This function returns pointer to next response iocb entry
773 * in the response ring. The caller must hold hbalock to make sure
774 * that no other thread consume the next response iocb.
775 * SLI-2/SLI-3 provide different sized iocbs.
776 **/
ed957684
JS
777static inline IOCB_t *
778lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
779{
7e56aa25
JS
780 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
781 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
782}
783
e59058c4 784/**
3621a710 785 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
786 * @phba: Pointer to HBA context object.
787 *
788 * This function is called with hbalock held. This function
789 * allocates a new driver iocb object from the iocb pool. If the
790 * allocation is successful, it returns pointer to the newly
791 * allocated iocb object else it returns NULL.
792 **/
4f2e66c6 793struct lpfc_iocbq *
2e0fef85 794__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
795{
796 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
797 struct lpfc_iocbq * iocbq = NULL;
798
1c2ba475
JT
799 lockdep_assert_held(&phba->hbalock);
800
0bd4ca25 801 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
802 if (iocbq)
803 phba->iocb_cnt++;
804 if (phba->iocb_cnt > phba->iocb_max)
805 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
806 return iocbq;
807}
808
da0436e9
JS
809/**
810 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
811 * @phba: Pointer to HBA context object.
812 * @xritag: XRI value.
813 *
814 * This function clears the sglq pointer from the array of acive
815 * sglq's. The xritag that is passed in is used to index into the
816 * array. Before the xritag can be used it needs to be adjusted
817 * by subtracting the xribase.
818 *
819 * Returns sglq ponter = success, NULL = Failure.
820 **/
895427bd 821struct lpfc_sglq *
da0436e9
JS
822__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
823{
da0436e9 824 struct lpfc_sglq *sglq;
6d368e53
JS
825
826 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
827 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
828 return sglq;
829}
830
831/**
832 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
833 * @phba: Pointer to HBA context object.
834 * @xritag: XRI value.
835 *
836 * This function returns the sglq pointer from the array of acive
837 * sglq's. The xritag that is passed in is used to index into the
838 * array. Before the xritag can be used it needs to be adjusted
839 * by subtracting the xribase.
840 *
841 * Returns sglq ponter = success, NULL = Failure.
842 **/
0f65ff68 843struct lpfc_sglq *
da0436e9
JS
844__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
845{
da0436e9 846 struct lpfc_sglq *sglq;
6d368e53
JS
847
848 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
849 return sglq;
850}
851
19ca7609 852/**
1151e3ec 853 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
854 * @phba: Pointer to HBA context object.
855 * @xritag: xri used in this exchange.
856 * @rrq: The RRQ to be cleared.
857 *
19ca7609 858 **/
1151e3ec
JS
859void
860lpfc_clr_rrq_active(struct lpfc_hba *phba,
861 uint16_t xritag,
862 struct lpfc_node_rrq *rrq)
19ca7609 863{
1151e3ec 864 struct lpfc_nodelist *ndlp = NULL;
19ca7609 865
1151e3ec
JS
866 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
867 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
868
869 /* The target DID could have been swapped (cable swap)
870 * we should use the ndlp from the findnode if it is
871 * available.
872 */
1151e3ec 873 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
874 ndlp = rrq->ndlp;
875
1151e3ec
JS
876 if (!ndlp)
877 goto out;
878
cff261f6 879 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
880 rrq->send_rrq = 0;
881 rrq->xritag = 0;
882 rrq->rrq_stop_time = 0;
883 }
1151e3ec 884out:
19ca7609
JS
885 mempool_free(rrq, phba->rrq_pool);
886}
887
888/**
889 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
890 * @phba: Pointer to HBA context object.
891 *
892 * This function is called with hbalock held. This function
893 * Checks if stop_time (ratov from setting rrq active) has
894 * been reached, if it has and the send_rrq flag is set then
895 * it will call lpfc_send_rrq. If the send_rrq flag is not set
896 * then it will just call the routine to clear the rrq and
897 * free the rrq resource.
898 * The timer is set to the next rrq that is going to expire before
899 * leaving the routine.
900 *
901 **/
902void
903lpfc_handle_rrq_active(struct lpfc_hba *phba)
904{
905 struct lpfc_node_rrq *rrq;
906 struct lpfc_node_rrq *nextrrq;
907 unsigned long next_time;
908 unsigned long iflags;
1151e3ec 909 LIST_HEAD(send_rrq);
19ca7609
JS
910
911 spin_lock_irqsave(&phba->hbalock, iflags);
912 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 913 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 914 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
915 &phba->active_rrq_list, list) {
916 if (time_after(jiffies, rrq->rrq_stop_time))
917 list_move(&rrq->list, &send_rrq);
918 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
919 next_time = rrq->rrq_stop_time;
920 }
921 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
922 if ((!list_empty(&phba->active_rrq_list)) &&
923 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 924 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
925 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
926 list_del(&rrq->list);
927 if (!rrq->send_rrq)
928 /* this call will free the rrq */
929 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
930 else if (lpfc_send_rrq(phba, rrq)) {
931 /* if we send the rrq then the completion handler
932 * will clear the bit in the xribitmap.
933 */
934 lpfc_clr_rrq_active(phba, rrq->xritag,
935 rrq);
936 }
937 }
19ca7609
JS
938}
939
940/**
941 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
942 * @vport: Pointer to vport context object.
943 * @xri: The xri used in the exchange.
944 * @did: The targets DID for this exchange.
945 *
946 * returns NULL = rrq not found in the phba->active_rrq_list.
947 * rrq = rrq for this xri and target.
948 **/
949struct lpfc_node_rrq *
950lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
951{
952 struct lpfc_hba *phba = vport->phba;
953 struct lpfc_node_rrq *rrq;
954 struct lpfc_node_rrq *nextrrq;
955 unsigned long iflags;
956
957 if (phba->sli_rev != LPFC_SLI_REV4)
958 return NULL;
959 spin_lock_irqsave(&phba->hbalock, iflags);
960 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
961 if (rrq->vport == vport && rrq->xritag == xri &&
962 rrq->nlp_DID == did){
963 list_del(&rrq->list);
964 spin_unlock_irqrestore(&phba->hbalock, iflags);
965 return rrq;
966 }
967 }
968 spin_unlock_irqrestore(&phba->hbalock, iflags);
969 return NULL;
970}
971
972/**
973 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
974 * @vport: Pointer to vport context object.
1151e3ec
JS
975 * @ndlp: Pointer to the lpfc_node_list structure.
976 * If ndlp is NULL Remove all active RRQs for this vport from the
977 * phba->active_rrq_list and clear the rrq.
978 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
979 **/
980void
1151e3ec 981lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
982
983{
984 struct lpfc_hba *phba = vport->phba;
985 struct lpfc_node_rrq *rrq;
986 struct lpfc_node_rrq *nextrrq;
987 unsigned long iflags;
1151e3ec 988 LIST_HEAD(rrq_list);
19ca7609
JS
989
990 if (phba->sli_rev != LPFC_SLI_REV4)
991 return;
1151e3ec
JS
992 if (!ndlp) {
993 lpfc_sli4_vport_delete_els_xri_aborted(vport);
994 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 995 }
1151e3ec
JS
996 spin_lock_irqsave(&phba->hbalock, iflags);
997 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
998 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
999 list_move(&rrq->list, &rrq_list);
19ca7609 1000 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
1001
1002 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1003 list_del(&rrq->list);
1004 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1005 }
19ca7609
JS
1006}
1007
19ca7609 1008/**
1151e3ec 1009 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
1010 * @phba: Pointer to HBA context object.
1011 * @ndlp: Targets nodelist pointer for this exchange.
1012 * @xritag the xri in the bitmap to test.
1013 *
1014 * This function is called with hbalock held. This function
1015 * returns 0 = rrq not active for this xri
1016 * 1 = rrq is valid for this xri.
1017 **/
1151e3ec
JS
1018int
1019lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
1020 uint16_t xritag)
1021{
1c2ba475 1022 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
1023 if (!ndlp)
1024 return 0;
cff261f6
JS
1025 if (!ndlp->active_rrqs_xri_bitmap)
1026 return 0;
1027 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
1028 return 1;
1029 else
1030 return 0;
1031}
1032
1033/**
1034 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1035 * @phba: Pointer to HBA context object.
1036 * @ndlp: nodelist pointer for this target.
1037 * @xritag: xri used in this exchange.
1038 * @rxid: Remote Exchange ID.
1039 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1040 *
1041 * This function takes the hbalock.
1042 * The active bit is always set in the active rrq xri_bitmap even
1043 * if there is no slot avaiable for the other rrq information.
1044 *
1045 * returns 0 rrq actived for this xri
1046 * < 0 No memory or invalid ndlp.
1047 **/
1048int
1049lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 1050 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 1051{
19ca7609 1052 unsigned long iflags;
b42c07c8
JS
1053 struct lpfc_node_rrq *rrq;
1054 int empty;
1055
1056 if (!ndlp)
1057 return -EINVAL;
1058
1059 if (!phba->cfg_enable_rrq)
1060 return -EINVAL;
19ca7609
JS
1061
1062 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
1063 if (phba->pport->load_flag & FC_UNLOADING) {
1064 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1065 goto out;
1066 }
1067
1068 /*
1069 * set the active bit even if there is no mem available.
1070 */
1071 if (NLP_CHK_FREE_REQ(ndlp))
1072 goto out;
1073
1074 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1075 goto out;
1076
cff261f6
JS
1077 if (!ndlp->active_rrqs_xri_bitmap)
1078 goto out;
1079
1080 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
1081 goto out;
1082
19ca7609 1083 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
1084 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1085 if (!rrq) {
1086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1087 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1088 " DID:0x%x Send:%d\n",
1089 xritag, rxid, ndlp->nlp_DID, send_rrq);
1090 return -EINVAL;
1091 }
e5771b4d
JS
1092 if (phba->cfg_enable_rrq == 1)
1093 rrq->send_rrq = send_rrq;
1094 else
1095 rrq->send_rrq = 0;
b42c07c8 1096 rrq->xritag = xritag;
256ec0d0
JS
1097 rrq->rrq_stop_time = jiffies +
1098 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
1099 rrq->ndlp = ndlp;
1100 rrq->nlp_DID = ndlp->nlp_DID;
1101 rrq->vport = ndlp->vport;
1102 rrq->rxid = rxid;
b42c07c8
JS
1103 spin_lock_irqsave(&phba->hbalock, iflags);
1104 empty = list_empty(&phba->active_rrq_list);
1105 list_add_tail(&rrq->list, &phba->active_rrq_list);
1106 phba->hba_flag |= HBA_RRQ_ACTIVE;
1107 if (empty)
1108 lpfc_worker_wake_up(phba);
1109 spin_unlock_irqrestore(&phba->hbalock, iflags);
1110 return 0;
1111out:
1112 spin_unlock_irqrestore(&phba->hbalock, iflags);
1113 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1114 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1115 " DID:0x%x Send:%d\n",
1116 xritag, rxid, ndlp->nlp_DID, send_rrq);
1117 return -EINVAL;
19ca7609
JS
1118}
1119
da0436e9 1120/**
895427bd 1121 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 1122 * @phba: Pointer to HBA context object.
19ca7609 1123 * @piocb: Pointer to the iocbq.
da0436e9 1124 *
dafe8cea 1125 * This function is called with the ring lock held. This function
6d368e53 1126 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
1127 * list is not empty then it is successful, it returns pointer to the newly
1128 * allocated sglq object else it returns NULL.
1129 **/
1130static struct lpfc_sglq *
895427bd 1131__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 1132{
895427bd 1133 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 1134 struct lpfc_sglq *sglq = NULL;
19ca7609 1135 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
1136 struct lpfc_scsi_buf *lpfc_cmd;
1137 struct lpfc_nodelist *ndlp;
1138 int found = 0;
1139
1c2ba475
JT
1140 lockdep_assert_held(&phba->hbalock);
1141
19ca7609
JS
1142 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1143 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1144 ndlp = lpfc_cmd->rdata->pnode;
be858b65 1145 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 1146 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 1147 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
1148 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1149 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1150 ndlp = NULL;
1151 else
1152 ndlp = piocbq->context_un.ndlp;
1153 } else {
19ca7609 1154 ndlp = piocbq->context1;
6c7cf486 1155 }
19ca7609 1156
895427bd
JS
1157 spin_lock(&phba->sli4_hba.sgl_list_lock);
1158 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
1159 start_sglq = sglq;
1160 while (!found) {
1161 if (!sglq)
d11f54b7 1162 break;
895427bd
JS
1163 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1164 test_bit(sglq->sli4_lxritag,
1165 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
1166 /* This xri has an rrq outstanding for this DID.
1167 * put it back in the list and get another xri.
1168 */
895427bd 1169 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 1170 sglq = NULL;
895427bd 1171 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
1172 struct lpfc_sglq, list);
1173 if (sglq == start_sglq) {
14041bd1 1174 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
1175 sglq = NULL;
1176 break;
1177 } else
1178 continue;
1179 }
1180 sglq->ndlp = ndlp;
1181 found = 1;
6d368e53 1182 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
1183 sglq->state = SGL_ALLOCATED;
1184 }
895427bd 1185 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
1186 return sglq;
1187}
1188
f358dd0c
JS
1189/**
1190 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1191 * @phba: Pointer to HBA context object.
1192 * @piocb: Pointer to the iocbq.
1193 *
1194 * This function is called with the sgl_list lock held. This function
1195 * gets a new driver sglq object from the sglq list. If the
1196 * list is not empty then it is successful, it returns pointer to the newly
1197 * allocated sglq object else it returns NULL.
1198 **/
1199struct lpfc_sglq *
1200__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1201{
1202 struct list_head *lpfc_nvmet_sgl_list;
1203 struct lpfc_sglq *sglq = NULL;
1204
1205 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1206
1207 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1208
1209 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1210 if (!sglq)
1211 return NULL;
1212 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1213 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1214 return sglq;
1215}
1216
e59058c4 1217/**
3621a710 1218 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1219 * @phba: Pointer to HBA context object.
1220 *
1221 * This function is called with no lock held. This function
1222 * allocates a new driver iocb object from the iocb pool. If the
1223 * allocation is successful, it returns pointer to the newly
1224 * allocated iocb object else it returns NULL.
1225 **/
2e0fef85
JS
1226struct lpfc_iocbq *
1227lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1228{
1229 struct lpfc_iocbq * iocbq = NULL;
1230 unsigned long iflags;
1231
1232 spin_lock_irqsave(&phba->hbalock, iflags);
1233 iocbq = __lpfc_sli_get_iocbq(phba);
1234 spin_unlock_irqrestore(&phba->hbalock, iflags);
1235 return iocbq;
1236}
1237
4f774513
JS
1238/**
1239 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1240 * @phba: Pointer to HBA context object.
1241 * @iocbq: Pointer to driver iocb object.
1242 *
1243 * This function is called with hbalock held to release driver
1244 * iocb object to the iocb pool. The iotag in the iocb object
1245 * does not change for each use of the iocb object. This function
1246 * clears all other fields of the iocb object when it is freed.
1247 * The sqlq structure that holds the xritag and phys and virtual
1248 * mappings for the scatter gather list is retrieved from the
1249 * active array of sglq. The get of the sglq pointer also clears
1250 * the entry in the array. If the status of the IO indiactes that
1251 * this IO was aborted then the sglq entry it put on the
1252 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1253 * IO has good status or fails for any other reason then the sglq
895427bd 1254 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1255 **/
1256static void
1257__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1258{
1259 struct lpfc_sglq *sglq;
1260 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1261 unsigned long iflag = 0;
895427bd 1262 struct lpfc_sli_ring *pring;
4f774513 1263
1c2ba475
JT
1264 lockdep_assert_held(&phba->hbalock);
1265
4f774513
JS
1266 if (iocbq->sli4_xritag == NO_XRI)
1267 sglq = NULL;
1268 else
6d368e53
JS
1269 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1270
0e9bb8d7 1271
4f774513 1272 if (sglq) {
f358dd0c
JS
1273 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1274 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1275 iflag);
1276 sglq->state = SGL_FREED;
1277 sglq->ndlp = NULL;
1278 list_add_tail(&sglq->list,
1279 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1280 spin_unlock_irqrestore(
1281 &phba->sli4_hba.sgl_list_lock, iflag);
1282 goto out;
1283 }
1284
895427bd 1285 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1286 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1287 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1288 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1289 iflag);
4f774513 1290 list_add(&sglq->list,
895427bd 1291 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1292 spin_unlock_irqrestore(
895427bd 1293 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1294 } else {
895427bd
JS
1295 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1296 iflag);
0f65ff68 1297 sglq->state = SGL_FREED;
19ca7609 1298 sglq->ndlp = NULL;
fedd3b7b 1299 list_add_tail(&sglq->list,
895427bd
JS
1300 &phba->sli4_hba.lpfc_els_sgl_list);
1301 spin_unlock_irqrestore(
1302 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1303
1304 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1305 if (!list_empty(&pring->txq))
2a9bf3d0 1306 lpfc_worker_wake_up(phba);
0f65ff68 1307 }
4f774513
JS
1308 }
1309
f358dd0c 1310out:
4f774513
JS
1311 /*
1312 * Clean all volatile data fields, preserve iotag and node struct.
1313 */
1314 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1315 iocbq->sli4_lxritag = NO_XRI;
4f774513 1316 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1317 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1318 LPFC_IO_NVME_LS);
4f774513
JS
1319 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1320}
1321
2a9bf3d0 1322
e59058c4 1323/**
3772a991 1324 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1325 * @phba: Pointer to HBA context object.
1326 * @iocbq: Pointer to driver iocb object.
1327 *
1328 * This function is called with hbalock held to release driver
1329 * iocb object to the iocb pool. The iotag in the iocb object
1330 * does not change for each use of the iocb object. This function
1331 * clears all other fields of the iocb object when it is freed.
1332 **/
a6ababd2 1333static void
3772a991 1334__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1335{
2e0fef85 1336 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1337
1c2ba475 1338 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1339
604a3e30
JB
1340 /*
1341 * Clean all volatile data fields, preserve iotag and node struct.
1342 */
1343 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1344 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1345 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1346}
1347
3772a991
JS
1348/**
1349 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1350 * @phba: Pointer to HBA context object.
1351 * @iocbq: Pointer to driver iocb object.
1352 *
1353 * This function is called with hbalock held to release driver
1354 * iocb object to the iocb pool. The iotag in the iocb object
1355 * does not change for each use of the iocb object. This function
1356 * clears all other fields of the iocb object when it is freed.
1357 **/
1358static void
1359__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1360{
1c2ba475
JT
1361 lockdep_assert_held(&phba->hbalock);
1362
3772a991 1363 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1364 phba->iocb_cnt--;
3772a991
JS
1365}
1366
e59058c4 1367/**
3621a710 1368 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1369 * @phba: Pointer to HBA context object.
1370 * @iocbq: Pointer to driver iocb object.
1371 *
1372 * This function is called with no lock held to release the iocb to
1373 * iocb pool.
1374 **/
2e0fef85
JS
1375void
1376lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1377{
1378 unsigned long iflags;
1379
1380 /*
1381 * Clean all volatile data fields, preserve iotag and node struct.
1382 */
1383 spin_lock_irqsave(&phba->hbalock, iflags);
1384 __lpfc_sli_release_iocbq(phba, iocbq);
1385 spin_unlock_irqrestore(&phba->hbalock, iflags);
1386}
1387
a257bf90
JS
1388/**
1389 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1390 * @phba: Pointer to HBA context object.
1391 * @iocblist: List of IOCBs.
1392 * @ulpstatus: ULP status in IOCB command field.
1393 * @ulpWord4: ULP word-4 in IOCB command field.
1394 *
1395 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1396 * on the list by invoking the complete callback function associated with the
1397 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1398 * fields.
1399 **/
1400void
1401lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1402 uint32_t ulpstatus, uint32_t ulpWord4)
1403{
1404 struct lpfc_iocbq *piocb;
1405
1406 while (!list_empty(iocblist)) {
1407 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1408 if (!piocb->iocb_cmpl)
1409 lpfc_sli_release_iocbq(phba, piocb);
1410 else {
1411 piocb->iocb.ulpStatus = ulpstatus;
1412 piocb->iocb.un.ulpWord[4] = ulpWord4;
1413 (piocb->iocb_cmpl) (phba, piocb, piocb);
1414 }
1415 }
1416 return;
1417}
1418
e59058c4 1419/**
3621a710
JS
1420 * lpfc_sli_iocb_cmd_type - Get the iocb type
1421 * @iocb_cmnd: iocb command code.
e59058c4
JS
1422 *
1423 * This function is called by ring event handler function to get the iocb type.
1424 * This function translates the iocb command to an iocb command type used to
1425 * decide the final disposition of each completed IOCB.
1426 * The function returns
1427 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1428 * LPFC_SOL_IOCB if it is a solicited iocb completion
1429 * LPFC_ABORT_IOCB if it is an abort iocb
1430 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1431 *
1432 * The caller is not required to hold any lock.
1433 **/
dea3101e
JB
1434static lpfc_iocb_type
1435lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1436{
1437 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1438
1439 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1440 return 0;
1441
1442 switch (iocb_cmnd) {
1443 case CMD_XMIT_SEQUENCE_CR:
1444 case CMD_XMIT_SEQUENCE_CX:
1445 case CMD_XMIT_BCAST_CN:
1446 case CMD_XMIT_BCAST_CX:
1447 case CMD_ELS_REQUEST_CR:
1448 case CMD_ELS_REQUEST_CX:
1449 case CMD_CREATE_XRI_CR:
1450 case CMD_CREATE_XRI_CX:
1451 case CMD_GET_RPI_CN:
1452 case CMD_XMIT_ELS_RSP_CX:
1453 case CMD_GET_RPI_CR:
1454 case CMD_FCP_IWRITE_CR:
1455 case CMD_FCP_IWRITE_CX:
1456 case CMD_FCP_IREAD_CR:
1457 case CMD_FCP_IREAD_CX:
1458 case CMD_FCP_ICMND_CR:
1459 case CMD_FCP_ICMND_CX:
f5603511
JS
1460 case CMD_FCP_TSEND_CX:
1461 case CMD_FCP_TRSP_CX:
1462 case CMD_FCP_TRECEIVE_CX:
1463 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1464 case CMD_ADAPTER_MSG:
1465 case CMD_ADAPTER_DUMP:
1466 case CMD_XMIT_SEQUENCE64_CR:
1467 case CMD_XMIT_SEQUENCE64_CX:
1468 case CMD_XMIT_BCAST64_CN:
1469 case CMD_XMIT_BCAST64_CX:
1470 case CMD_ELS_REQUEST64_CR:
1471 case CMD_ELS_REQUEST64_CX:
1472 case CMD_FCP_IWRITE64_CR:
1473 case CMD_FCP_IWRITE64_CX:
1474 case CMD_FCP_IREAD64_CR:
1475 case CMD_FCP_IREAD64_CX:
1476 case CMD_FCP_ICMND64_CR:
1477 case CMD_FCP_ICMND64_CX:
f5603511
JS
1478 case CMD_FCP_TSEND64_CX:
1479 case CMD_FCP_TRSP64_CX:
1480 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1481 case CMD_GEN_REQUEST64_CR:
1482 case CMD_GEN_REQUEST64_CX:
1483 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1484 case DSSCMD_IWRITE64_CR:
1485 case DSSCMD_IWRITE64_CX:
1486 case DSSCMD_IREAD64_CR:
1487 case DSSCMD_IREAD64_CX:
dea3101e
JB
1488 type = LPFC_SOL_IOCB;
1489 break;
1490 case CMD_ABORT_XRI_CN:
1491 case CMD_ABORT_XRI_CX:
1492 case CMD_CLOSE_XRI_CN:
1493 case CMD_CLOSE_XRI_CX:
1494 case CMD_XRI_ABORTED_CX:
1495 case CMD_ABORT_MXRI64_CN:
6669f9bb 1496 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1497 type = LPFC_ABORT_IOCB;
1498 break;
1499 case CMD_RCV_SEQUENCE_CX:
1500 case CMD_RCV_ELS_REQ_CX:
1501 case CMD_RCV_SEQUENCE64_CX:
1502 case CMD_RCV_ELS_REQ64_CX:
57127f15 1503 case CMD_ASYNC_STATUS:
ed957684
JS
1504 case CMD_IOCB_RCV_SEQ64_CX:
1505 case CMD_IOCB_RCV_ELS64_CX:
1506 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1507 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1508 type = LPFC_UNSOL_IOCB;
1509 break;
3163f725
JS
1510 case CMD_IOCB_XMIT_MSEQ64_CR:
1511 case CMD_IOCB_XMIT_MSEQ64_CX:
1512 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1513 case CMD_IOCB_RCV_ELS_LIST64_CX:
1514 case CMD_IOCB_CLOSE_EXTENDED_CN:
1515 case CMD_IOCB_ABORT_EXTENDED_CN:
1516 case CMD_IOCB_RET_HBQE64_CN:
1517 case CMD_IOCB_FCP_IBIDIR64_CR:
1518 case CMD_IOCB_FCP_IBIDIR64_CX:
1519 case CMD_IOCB_FCP_ITASKMGT64_CX:
1520 case CMD_IOCB_LOGENTRY_CN:
1521 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1522 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1523 __func__, iocb_cmnd);
3163f725
JS
1524 type = LPFC_UNKNOWN_IOCB;
1525 break;
dea3101e
JB
1526 default:
1527 type = LPFC_UNKNOWN_IOCB;
1528 break;
1529 }
1530
1531 return type;
1532}
1533
e59058c4 1534/**
3621a710 1535 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1536 * @phba: Pointer to HBA context object.
1537 *
1538 * This function is called from SLI initialization code
1539 * to configure every ring of the HBA's SLI interface. The
1540 * caller is not required to hold any lock. This function issues
1541 * a config_ring mailbox command for each ring.
1542 * This function returns zero if successful else returns a negative
1543 * error code.
1544 **/
dea3101e 1545static int
ed957684 1546lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1547{
1548 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1549 LPFC_MBOXQ_t *pmb;
1550 MAILBOX_t *pmbox;
1551 int i, rc, ret = 0;
dea3101e 1552
ed957684
JS
1553 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1554 if (!pmb)
1555 return -ENOMEM;
04c68496 1556 pmbox = &pmb->u.mb;
ed957684 1557 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1558 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1559 lpfc_config_ring(phba, i, pmb);
1560 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1561 if (rc != MBX_SUCCESS) {
92d7f7b0 1562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1563 "0446 Adapter failed to init (%d), "
dea3101e
JB
1564 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1565 "ring %d\n",
e8b62011
JS
1566 rc, pmbox->mbxCommand,
1567 pmbox->mbxStatus, i);
2e0fef85 1568 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1569 ret = -ENXIO;
1570 break;
dea3101e
JB
1571 }
1572 }
ed957684
JS
1573 mempool_free(pmb, phba->mbox_mem_pool);
1574 return ret;
dea3101e
JB
1575}
1576
e59058c4 1577/**
3621a710 1578 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1579 * @phba: Pointer to HBA context object.
1580 * @pring: Pointer to driver SLI ring object.
1581 * @piocb: Pointer to the driver iocb object.
1582 *
1583 * This function is called with hbalock held. The function adds the
1584 * new iocb to txcmplq of the given ring. This function always returns
1585 * 0. If this function is called for ELS ring, this function checks if
1586 * there is a vport associated with the ELS command. This function also
1587 * starts els_tmofunc timer if this is an ELS command.
1588 **/
dea3101e 1589static int
2e0fef85
JS
1590lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1591 struct lpfc_iocbq *piocb)
dea3101e 1592{
1c2ba475
JT
1593 lockdep_assert_held(&phba->hbalock);
1594
2319f847 1595 BUG_ON(!piocb);
22466da5 1596
dea3101e 1597 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1598 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1599
92d7f7b0
JS
1600 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1601 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1602 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1603 BUG_ON(!piocb->vport);
1604 if (!(piocb->vport->load_flag & FC_UNLOADING))
1605 mod_timer(&piocb->vport->els_tmofunc,
1606 jiffies +
1607 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1608 }
dea3101e 1609
2e0fef85 1610 return 0;
dea3101e
JB
1611}
1612
e59058c4 1613/**
3621a710 1614 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1615 * @phba: Pointer to HBA context object.
1616 * @pring: Pointer to driver SLI ring object.
1617 *
1618 * This function is called with hbalock held to get next
1619 * iocb in txq of the given ring. If there is any iocb in
1620 * the txq, the function returns first iocb in the list after
1621 * removing the iocb from the list, else it returns NULL.
1622 **/
2a9bf3d0 1623struct lpfc_iocbq *
2e0fef85 1624lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1625{
dea3101e
JB
1626 struct lpfc_iocbq *cmd_iocb;
1627
1c2ba475
JT
1628 lockdep_assert_held(&phba->hbalock);
1629
858c9f6c 1630 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1631 return cmd_iocb;
dea3101e
JB
1632}
1633
e59058c4 1634/**
3621a710 1635 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1636 * @phba: Pointer to HBA context object.
1637 * @pring: Pointer to driver SLI ring object.
1638 *
1639 * This function is called with hbalock held and the caller must post the
1640 * iocb without releasing the lock. If the caller releases the lock,
1641 * iocb slot returned by the function is not guaranteed to be available.
1642 * The function returns pointer to the next available iocb slot if there
1643 * is available slot in the ring, else it returns NULL.
1644 * If the get index of the ring is ahead of the put index, the function
1645 * will post an error attention event to the worker thread to take the
1646 * HBA to offline state.
1647 **/
dea3101e
JB
1648static IOCB_t *
1649lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1650{
34b02dcd 1651 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1652 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1653
1654 lockdep_assert_held(&phba->hbalock);
1655
7e56aa25
JS
1656 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1657 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1658 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1659
7e56aa25
JS
1660 if (unlikely(pring->sli.sli3.local_getidx ==
1661 pring->sli.sli3.next_cmdidx)) {
dea3101e 1662
7e56aa25 1663 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1664
7e56aa25 1665 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1667 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1668 "is bigger than cmd ring %d\n",
e8b62011 1669 pring->ringno,
7e56aa25
JS
1670 pring->sli.sli3.local_getidx,
1671 max_cmd_idx);
dea3101e 1672
2e0fef85 1673 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1674 /*
1675 * All error attention handlers are posted to
1676 * worker thread
1677 */
1678 phba->work_ha |= HA_ERATT;
1679 phba->work_hs = HS_FFER3;
92d7f7b0 1680
5e9d9b82 1681 lpfc_worker_wake_up(phba);
dea3101e
JB
1682
1683 return NULL;
1684 }
1685
7e56aa25 1686 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1687 return NULL;
1688 }
1689
ed957684 1690 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1691}
1692
e59058c4 1693/**
3621a710 1694 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1695 * @phba: Pointer to HBA context object.
1696 * @iocbq: Pointer to driver iocb object.
1697 *
1698 * This function gets an iotag for the iocb. If there is no unused iotag and
1699 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1700 * array and assigns a new iotag.
1701 * The function returns the allocated iotag if successful, else returns zero.
1702 * Zero is not a valid iotag.
1703 * The caller is not required to hold any lock.
1704 **/
604a3e30 1705uint16_t
2e0fef85 1706lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1707{
2e0fef85
JS
1708 struct lpfc_iocbq **new_arr;
1709 struct lpfc_iocbq **old_arr;
604a3e30
JB
1710 size_t new_len;
1711 struct lpfc_sli *psli = &phba->sli;
1712 uint16_t iotag;
dea3101e 1713
2e0fef85 1714 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1715 iotag = psli->last_iotag;
1716 if(++iotag < psli->iocbq_lookup_len) {
1717 psli->last_iotag = iotag;
1718 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1719 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1720 iocbq->iotag = iotag;
1721 return iotag;
2e0fef85 1722 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1723 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1724 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85 1725 spin_unlock_irq(&phba->hbalock);
6396bb22 1726 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
604a3e30
JB
1727 GFP_KERNEL);
1728 if (new_arr) {
2e0fef85 1729 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1730 old_arr = psli->iocbq_lookup;
1731 if (new_len <= psli->iocbq_lookup_len) {
1732 /* highly unprobable case */
1733 kfree(new_arr);
1734 iotag = psli->last_iotag;
1735 if(++iotag < psli->iocbq_lookup_len) {
1736 psli->last_iotag = iotag;
1737 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1738 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1739 iocbq->iotag = iotag;
1740 return iotag;
1741 }
2e0fef85 1742 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1743 return 0;
1744 }
1745 if (psli->iocbq_lookup)
1746 memcpy(new_arr, old_arr,
1747 ((psli->last_iotag + 1) *
311464ec 1748 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1749 psli->iocbq_lookup = new_arr;
1750 psli->iocbq_lookup_len = new_len;
1751 psli->last_iotag = iotag;
1752 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1753 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1754 iocbq->iotag = iotag;
1755 kfree(old_arr);
1756 return iotag;
1757 }
8f6d98d2 1758 } else
2e0fef85 1759 spin_unlock_irq(&phba->hbalock);
dea3101e 1760
bc73905a 1761 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1762 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1763 psli->last_iotag);
dea3101e 1764
604a3e30 1765 return 0;
dea3101e
JB
1766}
1767
e59058c4 1768/**
3621a710 1769 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1770 * @phba: Pointer to HBA context object.
1771 * @pring: Pointer to driver SLI ring object.
1772 * @iocb: Pointer to iocb slot in the ring.
1773 * @nextiocb: Pointer to driver iocb object which need to be
1774 * posted to firmware.
1775 *
1776 * This function is called with hbalock held to post a new iocb to
1777 * the firmware. This function copies the new iocb to ring iocb slot and
1778 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1779 * a completion call back for this iocb else the function will free the
1780 * iocb object.
1781 **/
dea3101e
JB
1782static void
1783lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1784 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1785{
1c2ba475 1786 lockdep_assert_held(&phba->hbalock);
dea3101e 1787 /*
604a3e30 1788 * Set up an iotag
dea3101e 1789 */
604a3e30 1790 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1791
e2a0a9d6 1792
a58cbd52
JS
1793 if (pring->ringno == LPFC_ELS_RING) {
1794 lpfc_debugfs_slow_ring_trc(phba,
1795 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1796 *(((uint32_t *) &nextiocb->iocb) + 4),
1797 *(((uint32_t *) &nextiocb->iocb) + 6),
1798 *(((uint32_t *) &nextiocb->iocb) + 7));
1799 }
1800
dea3101e
JB
1801 /*
1802 * Issue iocb command to adapter
1803 */
92d7f7b0 1804 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1805 wmb();
1806 pring->stats.iocb_cmd++;
1807
1808 /*
1809 * If there is no completion routine to call, we can release the
1810 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1811 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1812 */
1813 if (nextiocb->iocb_cmpl)
1814 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1815 else
2e0fef85 1816 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1817
1818 /*
1819 * Let the HBA know what IOCB slot will be the next one the
1820 * driver will put a command into.
1821 */
7e56aa25
JS
1822 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1823 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1824}
1825
e59058c4 1826/**
3621a710 1827 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1828 * @phba: Pointer to HBA context object.
1829 * @pring: Pointer to driver SLI ring object.
1830 *
1831 * The caller is not required to hold any lock for calling this function.
1832 * This function updates the chip attention bits for the ring to inform firmware
1833 * that there are pending work to be done for this ring and requests an
1834 * interrupt when there is space available in the ring. This function is
1835 * called when the driver is unable to post more iocbs to the ring due
1836 * to unavailability of space in the ring.
1837 **/
dea3101e 1838static void
2e0fef85 1839lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1840{
1841 int ringno = pring->ringno;
1842
1843 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1844
1845 wmb();
1846
1847 /*
1848 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1849 * The HBA will tell us when an IOCB entry is available.
1850 */
1851 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1852 readl(phba->CAregaddr); /* flush */
1853
1854 pring->stats.iocb_cmd_full++;
1855}
1856
e59058c4 1857/**
3621a710 1858 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1859 * @phba: Pointer to HBA context object.
1860 * @pring: Pointer to driver SLI ring object.
1861 *
1862 * This function updates the chip attention register bit for the
1863 * given ring to inform HBA that there is more work to be done
1864 * in this ring. The caller is not required to hold any lock.
1865 **/
dea3101e 1866static void
2e0fef85 1867lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1868{
1869 int ringno = pring->ringno;
1870
1871 /*
1872 * Tell the HBA that there is work to do in this ring.
1873 */
34b02dcd
JS
1874 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1875 wmb();
1876 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1877 readl(phba->CAregaddr); /* flush */
1878 }
dea3101e
JB
1879}
1880
e59058c4 1881/**
3621a710 1882 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1883 * @phba: Pointer to HBA context object.
1884 * @pring: Pointer to driver SLI ring object.
1885 *
1886 * This function is called with hbalock held to post pending iocbs
1887 * in the txq to the firmware. This function is called when driver
1888 * detects space available in the ring.
1889 **/
dea3101e 1890static void
2e0fef85 1891lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1892{
1893 IOCB_t *iocb;
1894 struct lpfc_iocbq *nextiocb;
1895
1c2ba475
JT
1896 lockdep_assert_held(&phba->hbalock);
1897
dea3101e
JB
1898 /*
1899 * Check to see if:
1900 * (a) there is anything on the txq to send
1901 * (b) link is up
1902 * (c) link attention events can be processed (fcp ring only)
1903 * (d) IOCB processing is not blocked by the outstanding mbox command.
1904 */
0e9bb8d7
JS
1905
1906 if (lpfc_is_link_up(phba) &&
1907 (!list_empty(&pring->txq)) &&
895427bd 1908 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1909 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1910
1911 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1912 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1913 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1914
1915 if (iocb)
1916 lpfc_sli_update_ring(phba, pring);
1917 else
1918 lpfc_sli_update_full_ring(phba, pring);
1919 }
1920
1921 return;
1922}
1923
e59058c4 1924/**
3621a710 1925 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1926 * @phba: Pointer to HBA context object.
1927 * @hbqno: HBQ number.
1928 *
1929 * This function is called with hbalock held to get the next
1930 * available slot for the given HBQ. If there is free slot
1931 * available for the HBQ it will return pointer to the next available
1932 * HBQ entry else it will return NULL.
1933 **/
a6ababd2 1934static struct lpfc_hbq_entry *
ed957684
JS
1935lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1936{
1937 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1938
1c2ba475
JT
1939 lockdep_assert_held(&phba->hbalock);
1940
ed957684
JS
1941 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1942 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1943 hbqp->next_hbqPutIdx = 0;
1944
1945 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1946 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1947 uint32_t getidx = le32_to_cpu(raw_index);
1948
1949 hbqp->local_hbqGetIdx = getidx;
1950
1951 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1952 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1953 LOG_SLI | LOG_VPORT,
e8b62011 1954 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1955 "%u is > than hbqp->entry_count %u\n",
e8b62011 1956 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1957 hbqp->entry_count);
1958
1959 phba->link_state = LPFC_HBA_ERROR;
1960 return NULL;
1961 }
1962
1963 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1964 return NULL;
1965 }
1966
51ef4c26
JS
1967 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1968 hbqp->hbqPutIdx;
ed957684
JS
1969}
1970
e59058c4 1971/**
3621a710 1972 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1973 * @phba: Pointer to HBA context object.
1974 *
1975 * This function is called with no lock held to free all the
1976 * hbq buffers while uninitializing the SLI interface. It also
1977 * frees the HBQ buffers returned by the firmware but not yet
1978 * processed by the upper layers.
1979 **/
ed957684
JS
1980void
1981lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1982{
92d7f7b0
JS
1983 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1984 struct hbq_dmabuf *hbq_buf;
3163f725 1985 unsigned long flags;
51ef4c26 1986 int i, hbq_count;
ed957684 1987
51ef4c26 1988 hbq_count = lpfc_sli_hbq_count();
ed957684 1989 /* Return all memory used by all HBQs */
3163f725 1990 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1991 for (i = 0; i < hbq_count; ++i) {
1992 list_for_each_entry_safe(dmabuf, next_dmabuf,
1993 &phba->hbqs[i].hbq_buffer_list, list) {
1994 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1995 list_del(&hbq_buf->dbuf.list);
1996 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1997 }
a8adb832 1998 phba->hbqs[i].buffer_count = 0;
ed957684 1999 }
3163f725
JS
2000
2001 /* Mark the HBQs not in use */
2002 phba->hbq_in_use = 0;
2003 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
2004}
2005
e59058c4 2006/**
3621a710 2007 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
2008 * @phba: Pointer to HBA context object.
2009 * @hbqno: HBQ number.
2010 * @hbq_buf: Pointer to HBQ buffer.
2011 *
2012 * This function is called with the hbalock held to post a
2013 * hbq buffer to the firmware. If the function finds an empty
2014 * slot in the HBQ, it will post the buffer. The function will return
2015 * pointer to the hbq entry if it successfully post the buffer
2016 * else it will return NULL.
2017 **/
3772a991 2018static int
ed957684 2019lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 2020 struct hbq_dmabuf *hbq_buf)
3772a991 2021{
1c2ba475 2022 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2023 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2024}
2025
2026/**
2027 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2028 * @phba: Pointer to HBA context object.
2029 * @hbqno: HBQ number.
2030 * @hbq_buf: Pointer to HBQ buffer.
2031 *
2032 * This function is called with the hbalock held to post a hbq buffer to the
2033 * firmware. If the function finds an empty slot in the HBQ, it will post the
2034 * buffer and place it on the hbq_buffer_list. The function will return zero if
2035 * it successfully post the buffer else it will return an error.
2036 **/
2037static int
2038lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2039 struct hbq_dmabuf *hbq_buf)
ed957684
JS
2040{
2041 struct lpfc_hbq_entry *hbqe;
92d7f7b0 2042 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 2043
1c2ba475 2044 lockdep_assert_held(&phba->hbalock);
ed957684
JS
2045 /* Get next HBQ entry slot to use */
2046 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2047 if (hbqe) {
2048 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2049
92d7f7b0
JS
2050 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2051 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 2052 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 2053 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
2054 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2055 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2056 /* Sync SLIM */
ed957684
JS
2057 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2058 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 2059 /* flush */
ed957684 2060 readl(phba->hbq_put + hbqno);
51ef4c26 2061 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
2062 return 0;
2063 } else
2064 return -ENOMEM;
ed957684
JS
2065}
2066
4f774513
JS
2067/**
2068 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2069 * @phba: Pointer to HBA context object.
2070 * @hbqno: HBQ number.
2071 * @hbq_buf: Pointer to HBQ buffer.
2072 *
2073 * This function is called with the hbalock held to post an RQE to the SLI4
2074 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2075 * the hbq_buffer_list and return zero, otherwise it will return an error.
2076 **/
2077static int
2078lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2079 struct hbq_dmabuf *hbq_buf)
2080{
2081 int rc;
2082 struct lpfc_rqe hrqe;
2083 struct lpfc_rqe drqe;
895427bd
JS
2084 struct lpfc_queue *hrq;
2085 struct lpfc_queue *drq;
2086
2087 if (hbqno != LPFC_ELS_HBQ)
2088 return 1;
2089 hrq = phba->sli4_hba.hdr_rq;
2090 drq = phba->sli4_hba.dat_rq;
4f774513 2091
1c2ba475 2092 lockdep_assert_held(&phba->hbalock);
4f774513
JS
2093 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2094 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2095 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2096 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 2097 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
2098 if (rc < 0)
2099 return rc;
895427bd 2100 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
2101 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2102 return 0;
2103}
2104
e59058c4 2105/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
2106static struct lpfc_hbq_init lpfc_els_hbq = {
2107 .rn = 1,
def9c7a9 2108 .entry_count = 256,
92d7f7b0
JS
2109 .mask_count = 0,
2110 .profile = 0,
51ef4c26 2111 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 2112 .buffer_count = 0,
a257bf90
JS
2113 .init_count = 40,
2114 .add_count = 40,
92d7f7b0 2115};
ed957684 2116
e59058c4 2117/* Array of HBQs */
78b2d852 2118struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
2119 &lpfc_els_hbq,
2120};
ed957684 2121
e59058c4 2122/**
3621a710 2123 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
2124 * @phba: Pointer to HBA context object.
2125 * @hbqno: HBQ number.
2126 * @count: Number of HBQ buffers to be posted.
2127 *
d7c255b2
JS
2128 * This function is called with no lock held to post more hbq buffers to the
2129 * given HBQ. The function returns the number of HBQ buffers successfully
2130 * posted.
e59058c4 2131 **/
311464ec 2132static int
92d7f7b0 2133lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 2134{
d7c255b2 2135 uint32_t i, posted = 0;
3163f725 2136 unsigned long flags;
92d7f7b0 2137 struct hbq_dmabuf *hbq_buffer;
d7c255b2 2138 LIST_HEAD(hbq_buf_list);
eafe1df9 2139 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 2140 return 0;
51ef4c26 2141
d7c255b2
JS
2142 if ((phba->hbqs[hbqno].buffer_count + count) >
2143 lpfc_hbq_defs[hbqno]->entry_count)
2144 count = lpfc_hbq_defs[hbqno]->entry_count -
2145 phba->hbqs[hbqno].buffer_count;
2146 if (!count)
2147 return 0;
2148 /* Allocate HBQ entries */
2149 for (i = 0; i < count; i++) {
2150 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2151 if (!hbq_buffer)
2152 break;
2153 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2154 }
3163f725
JS
2155 /* Check whether HBQ is still in use */
2156 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 2157 if (!phba->hbq_in_use)
d7c255b2
JS
2158 goto err;
2159 while (!list_empty(&hbq_buf_list)) {
2160 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2161 dbuf.list);
2162 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2163 (hbqno << 16));
3772a991 2164 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 2165 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
2166 posted++;
2167 } else
51ef4c26 2168 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 2169 }
3163f725 2170 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2171 return posted;
2172err:
eafe1df9 2173 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2174 while (!list_empty(&hbq_buf_list)) {
2175 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2176 dbuf.list);
2177 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2178 }
2179 return 0;
ed957684
JS
2180}
2181
e59058c4 2182/**
3621a710 2183 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
2184 * @phba: Pointer to HBA context object.
2185 * @qno: HBQ number.
2186 *
2187 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
2188 * is called with no lock held. The function returns the number of HBQ entries
2189 * successfully allocated.
e59058c4 2190 **/
92d7f7b0
JS
2191int
2192lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 2193{
def9c7a9
JS
2194 if (phba->sli_rev == LPFC_SLI_REV4)
2195 return 0;
2196 else
2197 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2198 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 2199}
ed957684 2200
e59058c4 2201/**
3621a710 2202 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2203 * @phba: Pointer to HBA context object.
2204 * @qno: HBQ queue number.
2205 *
2206 * This function is called from SLI initialization code path with
2207 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2208 * function returns the number of HBQ entries successfully allocated.
e59058c4 2209 **/
a6ababd2 2210static int
92d7f7b0
JS
2211lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2212{
def9c7a9
JS
2213 if (phba->sli_rev == LPFC_SLI_REV4)
2214 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2215 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2216 else
2217 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2218 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2219}
2220
3772a991
JS
2221/**
2222 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2223 * @phba: Pointer to HBA context object.
2224 * @hbqno: HBQ number.
2225 *
2226 * This function removes the first hbq buffer on an hbq list and returns a
2227 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2228 **/
2229static struct hbq_dmabuf *
2230lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2231{
2232 struct lpfc_dmabuf *d_buf;
2233
2234 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2235 if (!d_buf)
2236 return NULL;
2237 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2238}
2239
2d7dbc4c
JS
2240/**
2241 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2242 * @phba: Pointer to HBA context object.
2243 * @hbqno: HBQ number.
2244 *
2245 * This function removes the first RQ buffer on an RQ buffer list and returns a
2246 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2247 **/
2248static struct rqb_dmabuf *
2249lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2250{
2251 struct lpfc_dmabuf *h_buf;
2252 struct lpfc_rqb *rqbp;
2253
2254 rqbp = hrq->rqbp;
2255 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2256 struct lpfc_dmabuf, list);
2257 if (!h_buf)
2258 return NULL;
2259 rqbp->buffer_count--;
2260 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2261}
2262
e59058c4 2263/**
3621a710 2264 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2265 * @phba: Pointer to HBA context object.
2266 * @tag: Tag of the hbq buffer.
2267 *
71892418
SH
2268 * This function searches for the hbq buffer associated with the given tag in
2269 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2270 * otherwise it returns NULL.
e59058c4 2271 **/
a6ababd2 2272static struct hbq_dmabuf *
92d7f7b0 2273lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2274{
92d7f7b0
JS
2275 struct lpfc_dmabuf *d_buf;
2276 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2277 uint32_t hbqno;
2278
2279 hbqno = tag >> 16;
a0a74e45 2280 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2281 return NULL;
ed957684 2282
3772a991 2283 spin_lock_irq(&phba->hbalock);
51ef4c26 2284 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2285 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2286 if (hbq_buf->tag == tag) {
3772a991 2287 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2288 return hbq_buf;
ed957684
JS
2289 }
2290 }
3772a991 2291 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2293 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2294 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2295 return NULL;
ed957684
JS
2296}
2297
e59058c4 2298/**
3621a710 2299 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2300 * @phba: Pointer to HBA context object.
2301 * @hbq_buffer: Pointer to HBQ buffer.
2302 *
2303 * This function is called with hbalock. This function gives back
2304 * the hbq buffer to firmware. If the HBQ does not have space to
2305 * post the buffer, it will free the buffer.
2306 **/
ed957684 2307void
51ef4c26 2308lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2309{
2310 uint32_t hbqno;
2311
51ef4c26
JS
2312 if (hbq_buffer) {
2313 hbqno = hbq_buffer->tag >> 16;
3772a991 2314 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2315 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2316 }
2317}
2318
e59058c4 2319/**
3621a710 2320 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2321 * @mbxCommand: mailbox command code.
2322 *
2323 * This function is called by the mailbox event handler function to verify
2324 * that the completed mailbox command is a legitimate mailbox command. If the
2325 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2326 * and the mailbox event handler will take the HBA offline.
2327 **/
dea3101e
JB
2328static int
2329lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2330{
2331 uint8_t ret;
2332
2333 switch (mbxCommand) {
2334 case MBX_LOAD_SM:
2335 case MBX_READ_NV:
2336 case MBX_WRITE_NV:
a8adb832 2337 case MBX_WRITE_VPARMS:
dea3101e
JB
2338 case MBX_RUN_BIU_DIAG:
2339 case MBX_INIT_LINK:
2340 case MBX_DOWN_LINK:
2341 case MBX_CONFIG_LINK:
2342 case MBX_CONFIG_RING:
2343 case MBX_RESET_RING:
2344 case MBX_READ_CONFIG:
2345 case MBX_READ_RCONFIG:
2346 case MBX_READ_SPARM:
2347 case MBX_READ_STATUS:
2348 case MBX_READ_RPI:
2349 case MBX_READ_XRI:
2350 case MBX_READ_REV:
2351 case MBX_READ_LNK_STAT:
2352 case MBX_REG_LOGIN:
2353 case MBX_UNREG_LOGIN:
dea3101e
JB
2354 case MBX_CLEAR_LA:
2355 case MBX_DUMP_MEMORY:
2356 case MBX_DUMP_CONTEXT:
2357 case MBX_RUN_DIAGS:
2358 case MBX_RESTART:
2359 case MBX_UPDATE_CFG:
2360 case MBX_DOWN_LOAD:
2361 case MBX_DEL_LD_ENTRY:
2362 case MBX_RUN_PROGRAM:
2363 case MBX_SET_MASK:
09372820 2364 case MBX_SET_VARIABLE:
dea3101e 2365 case MBX_UNREG_D_ID:
41415862 2366 case MBX_KILL_BOARD:
dea3101e 2367 case MBX_CONFIG_FARP:
41415862 2368 case MBX_BEACON:
dea3101e
JB
2369 case MBX_LOAD_AREA:
2370 case MBX_RUN_BIU_DIAG64:
2371 case MBX_CONFIG_PORT:
2372 case MBX_READ_SPARM64:
2373 case MBX_READ_RPI64:
2374 case MBX_REG_LOGIN64:
76a95d75 2375 case MBX_READ_TOPOLOGY:
09372820 2376 case MBX_WRITE_WWN:
dea3101e
JB
2377 case MBX_SET_DEBUG:
2378 case MBX_LOAD_EXP_ROM:
57127f15 2379 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2380 case MBX_REG_VPI:
2381 case MBX_UNREG_VPI:
858c9f6c 2382 case MBX_HEARTBEAT:
84774a4d
JS
2383 case MBX_PORT_CAPABILITIES:
2384 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2385 case MBX_SLI4_CONFIG:
2386 case MBX_SLI4_REQ_FTRS:
2387 case MBX_REG_FCFI:
2388 case MBX_UNREG_FCFI:
2389 case MBX_REG_VFI:
2390 case MBX_UNREG_VFI:
2391 case MBX_INIT_VPI:
2392 case MBX_INIT_VFI:
2393 case MBX_RESUME_RPI:
c7495937
JS
2394 case MBX_READ_EVENT_LOG_STATUS:
2395 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2396 case MBX_SECURITY_MGMT:
2397 case MBX_AUTH_PORT:
940eb687 2398 case MBX_ACCESS_VDATA:
dea3101e
JB
2399 ret = mbxCommand;
2400 break;
2401 default:
2402 ret = MBX_SHUTDOWN;
2403 break;
2404 }
2e0fef85 2405 return ret;
dea3101e 2406}
e59058c4
JS
2407
2408/**
3621a710 2409 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2410 * @phba: Pointer to HBA context object.
2411 * @pmboxq: Pointer to mailbox command.
2412 *
2413 * This is completion handler function for mailbox commands issued from
2414 * lpfc_sli_issue_mbox_wait function. This function is called by the
2415 * mailbox event handler function with no lock held. This function
2416 * will wake up thread waiting on the wait queue pointed by context1
2417 * of the mailbox.
2418 **/
04c68496 2419void
2e0fef85 2420lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 2421{
858c9f6c 2422 unsigned long drvr_flag;
e29d74f8 2423 struct completion *pmbox_done;
dea3101e
JB
2424
2425 /*
e29d74f8 2426 * If pmbox_done is empty, the driver thread gave up waiting and
dea3101e
JB
2427 * continued running.
2428 */
7054a606 2429 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2430 spin_lock_irqsave(&phba->hbalock, drvr_flag);
e29d74f8
JS
2431 pmbox_done = (struct completion *)pmboxq->context3;
2432 if (pmbox_done)
2433 complete(pmbox_done);
858c9f6c 2434 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2435 return;
2436}
2437
e59058c4
JS
2438
2439/**
3621a710 2440 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2441 * @phba: Pointer to HBA context object.
2442 * @pmb: Pointer to mailbox object.
2443 *
2444 * This function is the default mailbox completion handler. It
2445 * frees the memory resources associated with the completed mailbox
2446 * command. If the completed command is a REG_LOGIN mailbox command,
2447 * this function will issue a UREG_LOGIN to re-claim the RPI.
2448 **/
dea3101e 2449void
2e0fef85 2450lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2451{
d439d286 2452 struct lpfc_vport *vport = pmb->vport;
dea3101e 2453 struct lpfc_dmabuf *mp;
d439d286 2454 struct lpfc_nodelist *ndlp;
5af5eee7 2455 struct Scsi_Host *shost;
04c68496 2456 uint16_t rpi, vpi;
7054a606
JS
2457 int rc;
2458
3e1f0718 2459 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
7054a606 2460
dea3101e
JB
2461 if (mp) {
2462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2463 kfree(mp);
2464 }
7054a606
JS
2465
2466 /*
2467 * If a REG_LOGIN succeeded after node is destroyed or node
2468 * is in re-discovery driver need to cleanup the RPI.
2469 */
2e0fef85 2470 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2471 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2472 !pmb->u.mb.mbxStatus) {
2473 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2474 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2475 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2476 pmb->vport = vport;
92d7f7b0 2477 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2478 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2479 if (rc != MBX_NOT_FINISHED)
2480 return;
2481 }
2482
695a814e
JS
2483 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2484 !(phba->pport->load_flag & FC_UNLOADING) &&
2485 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2486 shost = lpfc_shost_from_vport(vport);
2487 spin_lock_irq(shost->host_lock);
2488 vport->vpi_state |= LPFC_VPI_REGISTERED;
2489 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2490 spin_unlock_irq(shost->host_lock);
695a814e
JS
2491 }
2492
d439d286 2493 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
3e1f0718 2494 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
d439d286 2495 lpfc_nlp_put(ndlp);
dea16bda
JS
2496 pmb->ctx_buf = NULL;
2497 pmb->ctx_ndlp = NULL;
2498 }
2499
2500 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2502
2503 /* Check to see if there are any deferred events to process */
2504 if (ndlp) {
2505 lpfc_printf_vlog(
2506 vport,
2507 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2508 "1438 UNREG cmpl deferred mbox x%x "
2509 "on NPort x%x Data: x%x x%x %p\n",
2510 ndlp->nlp_rpi, ndlp->nlp_DID,
2511 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2512
2513 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2514 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
2515 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2516 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2517 }
2518 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2519 }
3e1f0718 2520 pmb->ctx_ndlp = NULL;
d439d286
JS
2521 }
2522
dcf2a4e0
JS
2523 /* Check security permission status on INIT_LINK mailbox command */
2524 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2525 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2527 "2860 SLI authentication is required "
2528 "for INIT_LINK but has not done yet\n");
2529
04c68496
JS
2530 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2531 lpfc_sli4_mbox_cmd_free(phba, pmb);
2532 else
2533 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2534}
be6bb941
JS
2535 /**
2536 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2537 * @phba: Pointer to HBA context object.
2538 * @pmb: Pointer to mailbox object.
2539 *
2540 * This function is the unreg rpi mailbox completion handler. It
2541 * frees the memory resources associated with the completed mailbox
2542 * command. An additional refrenece is put on the ndlp to prevent
2543 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2544 * the unreg mailbox command completes, this routine puts the
2545 * reference back.
2546 *
2547 **/
2548void
2549lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2550{
2551 struct lpfc_vport *vport = pmb->vport;
2552 struct lpfc_nodelist *ndlp;
2553
3e1f0718 2554 ndlp = pmb->ctx_ndlp;
be6bb941
JS
2555 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2556 if (phba->sli_rev == LPFC_SLI_REV4 &&
2557 (bf_get(lpfc_sli_intf_if_type,
27d6ac0a 2558 &phba->sli4_hba.sli_intf) >=
be6bb941
JS
2559 LPFC_SLI_INTF_IF_TYPE_2)) {
2560 if (ndlp) {
dea16bda
JS
2561 lpfc_printf_vlog(
2562 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2563 "0010 UNREG_LOGIN vpi:%x "
2564 "rpi:%x DID:%x defer x%x flg x%x "
2565 "map:%x %p\n",
2566 vport->vpi, ndlp->nlp_rpi,
2567 ndlp->nlp_DID, ndlp->nlp_defer_did,
2568 ndlp->nlp_flag,
2569 ndlp->nlp_usg_map, ndlp);
7c5e518c 2570 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941 2571 lpfc_nlp_put(ndlp);
dea16bda
JS
2572
2573 /* Check to see if there are any deferred
2574 * events to process
2575 */
2576 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2577 (ndlp->nlp_defer_did !=
2578 NLP_EVT_NOTHING_PENDING)) {
2579 lpfc_printf_vlog(
2580 vport, KERN_INFO, LOG_DISCOVERY,
2581 "4111 UNREG cmpl deferred "
2582 "clr x%x on "
2583 "NPort x%x Data: x%x %p\n",
2584 ndlp->nlp_rpi, ndlp->nlp_DID,
2585 ndlp->nlp_defer_did, ndlp);
2586 ndlp->nlp_defer_did =
2587 NLP_EVT_NOTHING_PENDING;
2588 lpfc_issue_els_plogi(
2589 vport, ndlp->nlp_DID, 0);
2590 }
2591 ndlp->nlp_flag &= ~NLP_UNREG_INP;
be6bb941
JS
2592 }
2593 }
2594 }
2595
2596 mempool_free(pmb, phba->mbox_mem_pool);
2597}
dea3101e 2598
e59058c4 2599/**
3621a710 2600 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2601 * @phba: Pointer to HBA context object.
2602 *
2603 * This function is called with no lock held. This function processes all
2604 * the completed mailbox commands and gives it to upper layers. The interrupt
2605 * service routine processes mailbox completion interrupt and adds completed
2606 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2607 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2608 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2609 * function returns the mailbox commands to the upper layer by calling the
2610 * completion handler function of each mailbox.
2611 **/
dea3101e 2612int
2e0fef85 2613lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2614{
92d7f7b0 2615 MAILBOX_t *pmbox;
dea3101e 2616 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2617 int rc;
2618 LIST_HEAD(cmplq);
dea3101e
JB
2619
2620 phba->sli.slistat.mbox_event++;
2621
92d7f7b0
JS
2622 /* Get all completed mailboxe buffers into the cmplq */
2623 spin_lock_irq(&phba->hbalock);
2624 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2625 spin_unlock_irq(&phba->hbalock);
dea3101e 2626
92d7f7b0
JS
2627 /* Get a Mailbox buffer to setup mailbox commands for callback */
2628 do {
2629 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2630 if (pmb == NULL)
2631 break;
2e0fef85 2632
04c68496 2633 pmbox = &pmb->u.mb;
dea3101e 2634
858c9f6c
JS
2635 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2636 if (pmb->vport) {
2637 lpfc_debugfs_disc_trc(pmb->vport,
2638 LPFC_DISC_TRC_MBOX_VPORT,
2639 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2640 (uint32_t)pmbox->mbxCommand,
2641 pmbox->un.varWords[0],
2642 pmbox->un.varWords[1]);
2643 }
2644 else {
2645 lpfc_debugfs_disc_trc(phba->pport,
2646 LPFC_DISC_TRC_MBOX,
2647 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2648 (uint32_t)pmbox->mbxCommand,
2649 pmbox->un.varWords[0],
2650 pmbox->un.varWords[1]);
2651 }
2652 }
2653
dea3101e
JB
2654 /*
2655 * It is a fatal error if unknown mbox command completion.
2656 */
2657 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2658 MBX_SHUTDOWN) {
af901ca1 2659 /* Unknown mailbox command compl */
92d7f7b0 2660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2661 "(%d):0323 Unknown Mailbox command "
a183a15f 2662 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2663 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2664 pmbox->mbxCommand,
a183a15f
JS
2665 lpfc_sli_config_mbox_subsys_get(phba,
2666 pmb),
2667 lpfc_sli_config_mbox_opcode_get(phba,
2668 pmb));
2e0fef85 2669 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2670 phba->work_hs = HS_FFER3;
2671 lpfc_handle_eratt(phba);
92d7f7b0 2672 continue;
dea3101e
JB
2673 }
2674
dea3101e
JB
2675 if (pmbox->mbxStatus) {
2676 phba->sli.slistat.mbox_stat_err++;
2677 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2678 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2679 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2680 LOG_MBOX | LOG_SLI,
2681 "(%d):0305 Mbox cmd cmpl "
2682 "error - RETRYing Data: x%x "
2683 "(x%x/x%x) x%x x%x x%x\n",
2684 pmb->vport ? pmb->vport->vpi : 0,
2685 pmbox->mbxCommand,
2686 lpfc_sli_config_mbox_subsys_get(phba,
2687 pmb),
2688 lpfc_sli_config_mbox_opcode_get(phba,
2689 pmb),
2690 pmbox->mbxStatus,
2691 pmbox->un.varWords[0],
2692 pmb->vport->port_state);
dea3101e
JB
2693 pmbox->mbxStatus = 0;
2694 pmbox->mbxOwner = OWN_HOST;
dea3101e 2695 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2696 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2697 continue;
dea3101e
JB
2698 }
2699 }
2700
2701 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2702 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2703 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2704 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2705 "x%x x%x x%x\n",
92d7f7b0 2706 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2707 pmbox->mbxCommand,
a183a15f
JS
2708 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2709 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2710 pmb->mbox_cmpl,
2711 *((uint32_t *) pmbox),
2712 pmbox->un.varWords[0],
2713 pmbox->un.varWords[1],
2714 pmbox->un.varWords[2],
2715 pmbox->un.varWords[3],
2716 pmbox->un.varWords[4],
2717 pmbox->un.varWords[5],
2718 pmbox->un.varWords[6],
e74c03c8
JS
2719 pmbox->un.varWords[7],
2720 pmbox->un.varWords[8],
2721 pmbox->un.varWords[9],
2722 pmbox->un.varWords[10]);
dea3101e 2723
92d7f7b0 2724 if (pmb->mbox_cmpl)
dea3101e 2725 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2726 } while (1);
2727 return 0;
2728}
dea3101e 2729
e59058c4 2730/**
3621a710 2731 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2732 * @phba: Pointer to HBA context object.
2733 * @pring: Pointer to driver SLI ring object.
2734 * @tag: buffer tag.
2735 *
2736 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2737 * is set in the tag the buffer is posted for a particular exchange,
2738 * the function will return the buffer without replacing the buffer.
2739 * If the buffer is for unsolicited ELS or CT traffic, this function
2740 * returns the buffer and also posts another buffer to the firmware.
2741 **/
76bb24ef
JS
2742static struct lpfc_dmabuf *
2743lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2744 struct lpfc_sli_ring *pring,
2745 uint32_t tag)
76bb24ef 2746{
9f1e1b50
JS
2747 struct hbq_dmabuf *hbq_entry;
2748
76bb24ef
JS
2749 if (tag & QUE_BUFTAG_BIT)
2750 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2751 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2752 if (!hbq_entry)
2753 return NULL;
2754 return &hbq_entry->dbuf;
76bb24ef 2755}
57127f15 2756
3772a991
JS
2757/**
2758 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2759 * @phba: Pointer to HBA context object.
2760 * @pring: Pointer to driver SLI ring object.
2761 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2762 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2763 * @fch_type: the type for the first frame of the sequence.
2764 *
2765 * This function is called with no lock held. This function uses the r_ctl and
2766 * type of the received sequence to find the correct callback function to call
2767 * to process the sequence.
2768 **/
2769static int
2770lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2771 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2772 uint32_t fch_type)
2773{
2774 int i;
2775
f358dd0c
JS
2776 switch (fch_type) {
2777 case FC_TYPE_NVME:
d613b6a7 2778 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2779 return 1;
2780 default:
2781 break;
2782 }
2783
3772a991
JS
2784 /* unSolicited Responses */
2785 if (pring->prt[0].profile) {
2786 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2787 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2788 saveq);
2789 return 1;
2790 }
2791 /* We must search, based on rctl / type
2792 for the right routine */
2793 for (i = 0; i < pring->num_mask; i++) {
2794 if ((pring->prt[i].rctl == fch_r_ctl) &&
2795 (pring->prt[i].type == fch_type)) {
2796 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2797 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2798 (phba, pring, saveq);
2799 return 1;
2800 }
2801 }
2802 return 0;
2803}
e59058c4
JS
2804
2805/**
3621a710 2806 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @saveq: Pointer to the unsolicited iocb.
2810 *
2811 * This function is called with no lock held by the ring event handler
2812 * when there is an unsolicited iocb posted to the response ring by the
2813 * firmware. This function gets the buffer associated with the iocbs
2814 * and calls the event handler for the ring. This function handles both
2815 * qring buffers and hbq buffers.
2816 * When the function returns 1 the caller can free the iocb object otherwise
2817 * upper layer functions will free the iocb objects.
2818 **/
dea3101e
JB
2819static int
2820lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2821 struct lpfc_iocbq *saveq)
2822{
2823 IOCB_t * irsp;
2824 WORD5 * w5p;
2825 uint32_t Rctl, Type;
76bb24ef 2826 struct lpfc_iocbq *iocbq;
3163f725 2827 struct lpfc_dmabuf *dmzbuf;
dea3101e 2828
dea3101e 2829 irsp = &(saveq->iocb);
57127f15
JS
2830
2831 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2832 if (pring->lpfc_sli_rcv_async_status)
2833 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2834 else
2835 lpfc_printf_log(phba,
2836 KERN_WARNING,
2837 LOG_SLI,
2838 "0316 Ring %d handler: unexpected "
2839 "ASYNC_STATUS iocb received evt_code "
2840 "0x%x\n",
2841 pring->ringno,
2842 irsp->un.asyncstat.evt_code);
2843 return 1;
2844 }
2845
3163f725
JS
2846 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2847 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2848 if (irsp->ulpBdeCount > 0) {
2849 dmzbuf = lpfc_sli_get_buff(phba, pring,
2850 irsp->un.ulpWord[3]);
2851 lpfc_in_buf_free(phba, dmzbuf);
2852 }
2853
2854 if (irsp->ulpBdeCount > 1) {
2855 dmzbuf = lpfc_sli_get_buff(phba, pring,
2856 irsp->unsli3.sli3Words[3]);
2857 lpfc_in_buf_free(phba, dmzbuf);
2858 }
2859
2860 if (irsp->ulpBdeCount > 2) {
2861 dmzbuf = lpfc_sli_get_buff(phba, pring,
2862 irsp->unsli3.sli3Words[7]);
2863 lpfc_in_buf_free(phba, dmzbuf);
2864 }
2865
2866 return 1;
2867 }
2868
92d7f7b0 2869 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2870 if (irsp->ulpBdeCount != 0) {
2871 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2872 irsp->un.ulpWord[3]);
2873 if (!saveq->context2)
2874 lpfc_printf_log(phba,
2875 KERN_ERR,
2876 LOG_SLI,
2877 "0341 Ring %d Cannot find buffer for "
2878 "an unsolicited iocb. tag 0x%x\n",
2879 pring->ringno,
2880 irsp->un.ulpWord[3]);
76bb24ef
JS
2881 }
2882 if (irsp->ulpBdeCount == 2) {
2883 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2884 irsp->unsli3.sli3Words[7]);
2885 if (!saveq->context3)
2886 lpfc_printf_log(phba,
2887 KERN_ERR,
2888 LOG_SLI,
2889 "0342 Ring %d Cannot find buffer for an"
2890 " unsolicited iocb. tag 0x%x\n",
2891 pring->ringno,
2892 irsp->unsli3.sli3Words[7]);
2893 }
2894 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2895 irsp = &(iocbq->iocb);
76bb24ef
JS
2896 if (irsp->ulpBdeCount != 0) {
2897 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2898 irsp->un.ulpWord[3]);
9c2face6 2899 if (!iocbq->context2)
76bb24ef
JS
2900 lpfc_printf_log(phba,
2901 KERN_ERR,
2902 LOG_SLI,
2903 "0343 Ring %d Cannot find "
2904 "buffer for an unsolicited iocb"
2905 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2906 irsp->un.ulpWord[3]);
76bb24ef
JS
2907 }
2908 if (irsp->ulpBdeCount == 2) {
2909 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2910 irsp->unsli3.sli3Words[7]);
9c2face6 2911 if (!iocbq->context3)
76bb24ef
JS
2912 lpfc_printf_log(phba,
2913 KERN_ERR,
2914 LOG_SLI,
2915 "0344 Ring %d Cannot find "
2916 "buffer for an unsolicited "
2917 "iocb. tag 0x%x\n",
2918 pring->ringno,
2919 irsp->unsli3.sli3Words[7]);
2920 }
2921 }
92d7f7b0 2922 }
9c2face6
JS
2923 if (irsp->ulpBdeCount != 0 &&
2924 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2925 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2926 int found = 0;
2927
2928 /* search continue save q for same XRI */
2929 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2930 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2931 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2932 list_add_tail(&saveq->list, &iocbq->list);
2933 found = 1;
2934 break;
2935 }
2936 }
2937 if (!found)
2938 list_add_tail(&saveq->clist,
2939 &pring->iocb_continue_saveq);
2940 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2941 list_del_init(&iocbq->clist);
2942 saveq = iocbq;
2943 irsp = &(saveq->iocb);
2944 } else
2945 return 0;
2946 }
2947 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2948 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2949 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2950 Rctl = FC_RCTL_ELS_REQ;
2951 Type = FC_TYPE_ELS;
9c2face6
JS
2952 } else {
2953 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2954 Rctl = w5p->hcsw.Rctl;
2955 Type = w5p->hcsw.Type;
2956
2957 /* Firmware Workaround */
2958 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2959 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2960 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2961 Rctl = FC_RCTL_ELS_REQ;
2962 Type = FC_TYPE_ELS;
9c2face6
JS
2963 w5p->hcsw.Rctl = Rctl;
2964 w5p->hcsw.Type = Type;
2965 }
2966 }
92d7f7b0 2967
3772a991 2968 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2969 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2970 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2971 "Type x%x received\n",
e8b62011 2972 pring->ringno, Rctl, Type);
3772a991 2973
92d7f7b0 2974 return 1;
dea3101e
JB
2975}
2976
e59058c4 2977/**
3621a710 2978 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2979 * @phba: Pointer to HBA context object.
2980 * @pring: Pointer to driver SLI ring object.
2981 * @prspiocb: Pointer to response iocb object.
2982 *
2983 * This function looks up the iocb_lookup table to get the command iocb
2984 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2985 * response iocb. This function is called with the hbalock held
2986 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2987 * This function returns the command iocb object if it finds the command
2988 * iocb else returns NULL.
2989 **/
dea3101e 2990static struct lpfc_iocbq *
2e0fef85
JS
2991lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2992 struct lpfc_sli_ring *pring,
2993 struct lpfc_iocbq *prspiocb)
dea3101e 2994{
dea3101e
JB
2995 struct lpfc_iocbq *cmd_iocb = NULL;
2996 uint16_t iotag;
1c2ba475 2997 lockdep_assert_held(&phba->hbalock);
dea3101e 2998
604a3e30
JB
2999 iotag = prspiocb->iocb.ulpIoTag;
3000
3001 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3002 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 3003 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
3004 /* remove from txcmpl queue list */
3005 list_del_init(&cmd_iocb->list);
4f2e66c6 3006 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 3007 return cmd_iocb;
2a9bf3d0 3008 }
dea3101e
JB
3009 }
3010
dea3101e 3011 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 3012 "0317 iotag x%x is out of "
604a3e30 3013 "range: max iotag x%x wd0 x%x\n",
e8b62011 3014 iotag, phba->sli.last_iotag,
604a3e30 3015 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
3016 return NULL;
3017}
3018
3772a991
JS
3019/**
3020 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3021 * @phba: Pointer to HBA context object.
3022 * @pring: Pointer to driver SLI ring object.
3023 * @iotag: IOCB tag.
3024 *
3025 * This function looks up the iocb_lookup table to get the command iocb
3026 * corresponding to the given iotag. This function is called with the
3027 * hbalock held.
3028 * This function returns the command iocb object if it finds the command
3029 * iocb else returns NULL.
3030 **/
3031static struct lpfc_iocbq *
3032lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3033 struct lpfc_sli_ring *pring, uint16_t iotag)
3034{
895427bd 3035 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 3036
1c2ba475 3037 lockdep_assert_held(&phba->hbalock);
3772a991
JS
3038 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3039 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
3040 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3041 /* remove from txcmpl queue list */
3042 list_del_init(&cmd_iocb->list);
3043 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 3044 return cmd_iocb;
2a9bf3d0 3045 }
3772a991 3046 }
89533e9b 3047
3772a991 3048 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
3049 "0372 iotag x%x lookup error: max iotag (x%x) "
3050 "iocb_flag x%x\n",
3051 iotag, phba->sli.last_iotag,
3052 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
3053 return NULL;
3054}
3055
e59058c4 3056/**
3621a710 3057 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
3058 * @phba: Pointer to HBA context object.
3059 * @pring: Pointer to driver SLI ring object.
3060 * @saveq: Pointer to the response iocb to be processed.
3061 *
3062 * This function is called by the ring event handler for non-fcp
3063 * rings when there is a new response iocb in the response ring.
3064 * The caller is not required to hold any locks. This function
3065 * gets the command iocb associated with the response iocb and
3066 * calls the completion handler for the command iocb. If there
3067 * is no completion handler, the function will free the resources
3068 * associated with command iocb. If the response iocb is for
3069 * an already aborted command iocb, the status of the completion
3070 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3071 * This function always returns 1.
3072 **/
dea3101e 3073static int
2e0fef85 3074lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
3075 struct lpfc_iocbq *saveq)
3076{
2e0fef85 3077 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
3078 int rc = 1;
3079 unsigned long iflag;
3080
3081 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
3082 if (phba->sli_rev == LPFC_SLI_REV4)
3083 spin_lock_irqsave(&pring->ring_lock, iflag);
3084 else
3085 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 3086 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
3087 if (phba->sli_rev == LPFC_SLI_REV4)
3088 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3089 else
3090 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 3091
dea3101e
JB
3092 if (cmdiocbp) {
3093 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
3094 /*
3095 * If an ELS command failed send an event to mgmt
3096 * application.
3097 */
3098 if (saveq->iocb.ulpStatus &&
3099 (pring->ringno == LPFC_ELS_RING) &&
3100 (cmdiocbp->iocb.ulpCommand ==
3101 CMD_ELS_REQUEST64_CR))
3102 lpfc_send_els_failure_event(phba,
3103 cmdiocbp, saveq);
3104
dea3101e
JB
3105 /*
3106 * Post all ELS completions to the worker thread.
3107 * All other are passed to the completion callback.
3108 */
3109 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
3110 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3111 (cmdiocbp->iocb_flag &
3112 LPFC_DRIVER_ABORTED)) {
3113 spin_lock_irqsave(&phba->hbalock,
3114 iflag);
07951076
JS
3115 cmdiocbp->iocb_flag &=
3116 ~LPFC_DRIVER_ABORTED;
341af102
JS
3117 spin_unlock_irqrestore(&phba->hbalock,
3118 iflag);
07951076
JS
3119 saveq->iocb.ulpStatus =
3120 IOSTAT_LOCAL_REJECT;
3121 saveq->iocb.un.ulpWord[4] =
3122 IOERR_SLI_ABORTED;
0ff10d46
JS
3123
3124 /* Firmware could still be in progress
3125 * of DMAing payload, so don't free data
3126 * buffer till after a hbeat.
3127 */
341af102
JS
3128 spin_lock_irqsave(&phba->hbalock,
3129 iflag);
0ff10d46 3130 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
3131 spin_unlock_irqrestore(&phba->hbalock,
3132 iflag);
3133 }
0f65ff68
JS
3134 if (phba->sli_rev == LPFC_SLI_REV4) {
3135 if (saveq->iocb_flag &
3136 LPFC_EXCHANGE_BUSY) {
3137 /* Set cmdiocb flag for the
3138 * exchange busy so sgl (xri)
3139 * will not be released until
3140 * the abort xri is received
3141 * from hba.
3142 */
3143 spin_lock_irqsave(
3144 &phba->hbalock, iflag);
3145 cmdiocbp->iocb_flag |=
3146 LPFC_EXCHANGE_BUSY;
3147 spin_unlock_irqrestore(
3148 &phba->hbalock, iflag);
3149 }
3150 if (cmdiocbp->iocb_flag &
3151 LPFC_DRIVER_ABORTED) {
3152 /*
3153 * Clear LPFC_DRIVER_ABORTED
3154 * bit in case it was driver
3155 * initiated abort.
3156 */
3157 spin_lock_irqsave(
3158 &phba->hbalock, iflag);
3159 cmdiocbp->iocb_flag &=
3160 ~LPFC_DRIVER_ABORTED;
3161 spin_unlock_irqrestore(
3162 &phba->hbalock, iflag);
3163 cmdiocbp->iocb.ulpStatus =
3164 IOSTAT_LOCAL_REJECT;
3165 cmdiocbp->iocb.un.ulpWord[4] =
3166 IOERR_ABORT_REQUESTED;
3167 /*
3168 * For SLI4, irsiocb contains
3169 * NO_XRI in sli_xritag, it
3170 * shall not affect releasing
3171 * sgl (xri) process.
3172 */
3173 saveq->iocb.ulpStatus =
3174 IOSTAT_LOCAL_REJECT;
3175 saveq->iocb.un.ulpWord[4] =
3176 IOERR_SLI_ABORTED;
3177 spin_lock_irqsave(
3178 &phba->hbalock, iflag);
3179 saveq->iocb_flag |=
3180 LPFC_DELAY_MEM_FREE;
3181 spin_unlock_irqrestore(
3182 &phba->hbalock, iflag);
3183 }
07951076 3184 }
dea3101e 3185 }
2e0fef85 3186 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
3187 } else
3188 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
3189 } else {
3190 /*
3191 * Unknown initiating command based on the response iotag.
3192 * This could be the case on the ELS ring because of
3193 * lpfc_els_abort().
3194 */
3195 if (pring->ringno != LPFC_ELS_RING) {
3196 /*
3197 * Ring <ringno> handler: unexpected completion IoTag
3198 * <IoTag>
3199 */
a257bf90 3200 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
3201 "0322 Ring %d handler: "
3202 "unexpected completion IoTag x%x "
3203 "Data: x%x x%x x%x x%x\n",
3204 pring->ringno,
3205 saveq->iocb.ulpIoTag,
3206 saveq->iocb.ulpStatus,
3207 saveq->iocb.un.ulpWord[4],
3208 saveq->iocb.ulpCommand,
3209 saveq->iocb.ulpContext);
dea3101e
JB
3210 }
3211 }
68876920 3212
dea3101e
JB
3213 return rc;
3214}
3215
e59058c4 3216/**
3621a710 3217 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
3218 * @phba: Pointer to HBA context object.
3219 * @pring: Pointer to driver SLI ring object.
3220 *
3221 * This function is called from the iocb ring event handlers when
3222 * put pointer is ahead of the get pointer for a ring. This function signal
3223 * an error attention condition to the worker thread and the worker
3224 * thread will transition the HBA to offline state.
3225 **/
2e0fef85
JS
3226static void
3227lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 3228{
34b02dcd 3229 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 3230 /*
025dfdaf 3231 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
3232 * rsp ring <portRspMax>
3233 */
3234 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3235 "0312 Ring %d handler: portRspPut %d "
025dfdaf 3236 "is bigger than rsp ring %d\n",
e8b62011 3237 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 3238 pring->sli.sli3.numRiocb);
875fbdfe 3239
2e0fef85 3240 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
3241
3242 /*
3243 * All error attention handlers are posted to
3244 * worker thread
3245 */
3246 phba->work_ha |= HA_ERATT;
3247 phba->work_hs = HS_FFER3;
92d7f7b0 3248
5e9d9b82 3249 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3250
3251 return;
3252}
3253
9399627f 3254/**
3621a710 3255 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3256 * @ptr: Pointer to address of HBA context object.
3257 *
3258 * This function is invoked by the Error Attention polling timer when the
3259 * timer times out. It will check the SLI Error Attention register for
3260 * possible attention events. If so, it will post an Error Attention event
3261 * and wake up worker thread to process it. Otherwise, it will set up the
3262 * Error Attention polling timer for the next poll.
3263 **/
f22eb4d3 3264void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3265{
3266 struct lpfc_hba *phba;
eb016566 3267 uint32_t eratt = 0;
aa6fbb75 3268 uint64_t sli_intr, cnt;
9399627f 3269
f22eb4d3 3270 phba = from_timer(phba, t, eratt_poll);
9399627f 3271
aa6fbb75
JS
3272 /* Here we will also keep track of interrupts per sec of the hba */
3273 sli_intr = phba->sli.slistat.sli_intr;
3274
3275 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3276 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3277 sli_intr);
3278 else
3279 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3280
65791f1f
JS
3281 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3282 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3283 phba->sli.slistat.sli_ips = cnt;
3284
3285 phba->sli.slistat.sli_prev_intr = sli_intr;
3286
9399627f
JS
3287 /* Check chip HA register for error event */
3288 eratt = lpfc_sli_check_eratt(phba);
3289
3290 if (eratt)
3291 /* Tell the worker thread there is work to do */
3292 lpfc_worker_wake_up(phba);
3293 else
3294 /* Restart the timer for next eratt poll */
256ec0d0
JS
3295 mod_timer(&phba->eratt_poll,
3296 jiffies +
65791f1f 3297 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3298 return;
3299}
3300
875fbdfe 3301
e59058c4 3302/**
3621a710 3303 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3304 * @phba: Pointer to HBA context object.
3305 * @pring: Pointer to driver SLI ring object.
3306 * @mask: Host attention register mask for this ring.
3307 *
3308 * This function is called from the interrupt context when there is a ring
3309 * event for the fcp ring. The caller does not hold any lock.
3310 * The function processes each response iocb in the response ring until it
25985edc 3311 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3312 * LE bit set. The function will call the completion handler of the command iocb
3313 * if the response iocb indicates a completion for a command iocb or it is
3314 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3315 * function if this is an unsolicited iocb.
dea3101e 3316 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3317 * to check it explicitly.
3318 */
3319int
2e0fef85
JS
3320lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3321 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3322{
34b02dcd 3323 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3324 IOCB_t *irsp = NULL;
87f6eaff 3325 IOCB_t *entry = NULL;
dea3101e
JB
3326 struct lpfc_iocbq *cmdiocbq = NULL;
3327 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3328 uint32_t status;
3329 uint32_t portRspPut, portRspMax;
3330 int rc = 1;
3331 lpfc_iocb_type type;
3332 unsigned long iflag;
3333 uint32_t rsp_cmpl = 0;
dea3101e 3334
2e0fef85 3335 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3336 pring->stats.iocb_event++;
3337
dea3101e
JB
3338 /*
3339 * The next available response entry should never exceed the maximum
3340 * entries. If it does, treat it as an adapter hardware error.
3341 */
7e56aa25 3342 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3343 portRspPut = le32_to_cpu(pgp->rspPutInx);
3344 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3345 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3346 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3347 return 1;
3348 }
45ed1190
JS
3349 if (phba->fcp_ring_in_use) {
3350 spin_unlock_irqrestore(&phba->hbalock, iflag);
3351 return 1;
3352 } else
3353 phba->fcp_ring_in_use = 1;
dea3101e
JB
3354
3355 rmb();
7e56aa25 3356 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3357 /*
3358 * Fetch an entry off the ring and copy it into a local data
3359 * structure. The copy involves a byte-swap since the
3360 * network byte order and pci byte orders are different.
3361 */
ed957684 3362 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3363 phba->last_completion_time = jiffies;
875fbdfe 3364
7e56aa25
JS
3365 if (++pring->sli.sli3.rspidx >= portRspMax)
3366 pring->sli.sli3.rspidx = 0;
875fbdfe 3367
87f6eaff
JSEC
3368 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3369 (uint32_t *) &rspiocbq.iocb,
ed957684 3370 phba->iocb_rsp_size);
a4bc3379 3371 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3372 irsp = &rspiocbq.iocb;
3373
dea3101e
JB
3374 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3375 pring->stats.iocb_rsp++;
3376 rsp_cmpl++;
3377
3378 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3379 /*
3380 * If resource errors reported from HBA, reduce
3381 * queuedepths of the SCSI device.
3382 */
3383 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3384 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3385 IOERR_NO_RESOURCES)) {
92d7f7b0 3386 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3387 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3388 spin_lock_irqsave(&phba->hbalock, iflag);
3389 }
3390
dea3101e
JB
3391 /* Rsp ring <ringno> error: IOCB */
3392 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3393 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3394 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3395 pring->ringno,
92d7f7b0
JS
3396 irsp->un.ulpWord[0],
3397 irsp->un.ulpWord[1],
3398 irsp->un.ulpWord[2],
3399 irsp->un.ulpWord[3],
3400 irsp->un.ulpWord[4],
3401 irsp->un.ulpWord[5],
d7c255b2
JS
3402 *(uint32_t *)&irsp->un1,
3403 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3404 }
3405
3406 switch (type) {
3407 case LPFC_ABORT_IOCB:
3408 case LPFC_SOL_IOCB:
3409 /*
3410 * Idle exchange closed via ABTS from port. No iocb
3411 * resources need to be recovered.
3412 */
3413 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3415 "0333 IOCB cmd 0x%x"
dca9479b 3416 " processed. Skipping"
92d7f7b0 3417 " completion\n",
dca9479b 3418 irsp->ulpCommand);
dea3101e
JB
3419 break;
3420 }
3421
604a3e30
JB
3422 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3423 &rspiocbq);
0f65ff68
JS
3424 if (unlikely(!cmdiocbq))
3425 break;
3426 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3427 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3428 if (cmdiocbq->iocb_cmpl) {
3429 spin_unlock_irqrestore(&phba->hbalock, iflag);
3430 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3431 &rspiocbq);
3432 spin_lock_irqsave(&phba->hbalock, iflag);
3433 }
dea3101e 3434 break;
a4bc3379 3435 case LPFC_UNSOL_IOCB:
2e0fef85 3436 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3437 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3438 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3439 break;
dea3101e
JB
3440 default:
3441 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3442 char adaptermsg[LPFC_MAX_ADPTMSG];
3443 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3444 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3445 MAX_MSG_DATA);
898eb71c
JP
3446 dev_warn(&((phba->pcidev)->dev),
3447 "lpfc%d: %s\n",
dea3101e
JB
3448 phba->brd_no, adaptermsg);
3449 } else {
3450 /* Unknown IOCB command */
3451 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3452 "0334 Unknown IOCB command "
92d7f7b0 3453 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3454 type, irsp->ulpCommand,
92d7f7b0
JS
3455 irsp->ulpStatus,
3456 irsp->ulpIoTag,
3457 irsp->ulpContext);
dea3101e
JB
3458 }
3459 break;
3460 }
3461
3462 /*
3463 * The response IOCB has been processed. Update the ring
3464 * pointer in SLIM. If the port response put pointer has not
3465 * been updated, sync the pgp->rspPutInx and fetch the new port
3466 * response put pointer.
3467 */
7e56aa25
JS
3468 writel(pring->sli.sli3.rspidx,
3469 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3470
7e56aa25 3471 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3472 portRspPut = le32_to_cpu(pgp->rspPutInx);
3473 }
3474
3475 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3476 pring->stats.iocb_rsp_full++;
3477 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3478 writel(status, phba->CAregaddr);
3479 readl(phba->CAregaddr);
3480 }
3481 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3482 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3483 pring->stats.iocb_cmd_empty++;
3484
3485 /* Force update of the local copy of cmdGetInx */
7e56aa25 3486 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3487 lpfc_sli_resume_iocb(phba, pring);
3488
3489 if ((pring->lpfc_sli_cmd_available))
3490 (pring->lpfc_sli_cmd_available) (phba, pring);
3491
3492 }
3493
45ed1190 3494 phba->fcp_ring_in_use = 0;
2e0fef85 3495 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3496 return rc;
3497}
3498
e59058c4 3499/**
3772a991
JS
3500 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3501 * @phba: Pointer to HBA context object.
3502 * @pring: Pointer to driver SLI ring object.
3503 * @rspiocbp: Pointer to driver response IOCB object.
3504 *
3505 * This function is called from the worker thread when there is a slow-path
3506 * response IOCB to process. This function chains all the response iocbs until
3507 * seeing the iocb with the LE bit set. The function will call
3508 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3509 * completion of a command iocb. The function will call the
3510 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3511 * The function frees the resources or calls the completion handler if this
3512 * iocb is an abort completion. The function returns NULL when the response
3513 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3514 * this function shall chain the iocb on to the iocb_continueq and return the
3515 * response iocb passed in.
3516 **/
3517static struct lpfc_iocbq *
3518lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3519 struct lpfc_iocbq *rspiocbp)
3520{
3521 struct lpfc_iocbq *saveq;
3522 struct lpfc_iocbq *cmdiocbp;
3523 struct lpfc_iocbq *next_iocb;
3524 IOCB_t *irsp = NULL;
3525 uint32_t free_saveq;
3526 uint8_t iocb_cmd_type;
3527 lpfc_iocb_type type;
3528 unsigned long iflag;
3529 int rc;
3530
3531 spin_lock_irqsave(&phba->hbalock, iflag);
3532 /* First add the response iocb to the countinueq list */
3533 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3534 pring->iocb_continueq_cnt++;
3535
70f23fd6 3536 /* Now, determine whether the list is completed for processing */
3772a991
JS
3537 irsp = &rspiocbp->iocb;
3538 if (irsp->ulpLe) {
3539 /*
3540 * By default, the driver expects to free all resources
3541 * associated with this iocb completion.
3542 */
3543 free_saveq = 1;
3544 saveq = list_get_first(&pring->iocb_continueq,
3545 struct lpfc_iocbq, list);
3546 irsp = &(saveq->iocb);
3547 list_del_init(&pring->iocb_continueq);
3548 pring->iocb_continueq_cnt = 0;
3549
3550 pring->stats.iocb_rsp++;
3551
3552 /*
3553 * If resource errors reported from HBA, reduce
3554 * queuedepths of the SCSI device.
3555 */
3556 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3557 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3558 IOERR_NO_RESOURCES)) {
3772a991
JS
3559 spin_unlock_irqrestore(&phba->hbalock, iflag);
3560 phba->lpfc_rampdown_queue_depth(phba);
3561 spin_lock_irqsave(&phba->hbalock, iflag);
3562 }
3563
3564 if (irsp->ulpStatus) {
3565 /* Rsp ring <ringno> error: IOCB */
3566 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3567 "0328 Rsp Ring %d error: "
3568 "IOCB Data: "
3569 "x%x x%x x%x x%x "
3570 "x%x x%x x%x x%x "
3571 "x%x x%x x%x x%x "
3572 "x%x x%x x%x x%x\n",
3573 pring->ringno,
3574 irsp->un.ulpWord[0],
3575 irsp->un.ulpWord[1],
3576 irsp->un.ulpWord[2],
3577 irsp->un.ulpWord[3],
3578 irsp->un.ulpWord[4],
3579 irsp->un.ulpWord[5],
3580 *(((uint32_t *) irsp) + 6),
3581 *(((uint32_t *) irsp) + 7),
3582 *(((uint32_t *) irsp) + 8),
3583 *(((uint32_t *) irsp) + 9),
3584 *(((uint32_t *) irsp) + 10),
3585 *(((uint32_t *) irsp) + 11),
3586 *(((uint32_t *) irsp) + 12),
3587 *(((uint32_t *) irsp) + 13),
3588 *(((uint32_t *) irsp) + 14),
3589 *(((uint32_t *) irsp) + 15));
3590 }
3591
3592 /*
3593 * Fetch the IOCB command type and call the correct completion
3594 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3595 * get freed back to the lpfc_iocb_list by the discovery
3596 * kernel thread.
3597 */
3598 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3599 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3600 switch (type) {
3601 case LPFC_SOL_IOCB:
3602 spin_unlock_irqrestore(&phba->hbalock, iflag);
3603 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3604 spin_lock_irqsave(&phba->hbalock, iflag);
3605 break;
3606
3607 case LPFC_UNSOL_IOCB:
3608 spin_unlock_irqrestore(&phba->hbalock, iflag);
3609 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3610 spin_lock_irqsave(&phba->hbalock, iflag);
3611 if (!rc)
3612 free_saveq = 0;
3613 break;
3614
3615 case LPFC_ABORT_IOCB:
3616 cmdiocbp = NULL;
3617 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3618 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3619 saveq);
3620 if (cmdiocbp) {
3621 /* Call the specified completion routine */
3622 if (cmdiocbp->iocb_cmpl) {
3623 spin_unlock_irqrestore(&phba->hbalock,
3624 iflag);
3625 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3626 saveq);
3627 spin_lock_irqsave(&phba->hbalock,
3628 iflag);
3629 } else
3630 __lpfc_sli_release_iocbq(phba,
3631 cmdiocbp);
3632 }
3633 break;
3634
3635 case LPFC_UNKNOWN_IOCB:
3636 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3637 char adaptermsg[LPFC_MAX_ADPTMSG];
3638 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3639 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3640 MAX_MSG_DATA);
3641 dev_warn(&((phba->pcidev)->dev),
3642 "lpfc%d: %s\n",
3643 phba->brd_no, adaptermsg);
3644 } else {
3645 /* Unknown IOCB command */
3646 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3647 "0335 Unknown IOCB "
3648 "command Data: x%x "
3649 "x%x x%x x%x\n",
3650 irsp->ulpCommand,
3651 irsp->ulpStatus,
3652 irsp->ulpIoTag,
3653 irsp->ulpContext);
3654 }
3655 break;
3656 }
3657
3658 if (free_saveq) {
3659 list_for_each_entry_safe(rspiocbp, next_iocb,
3660 &saveq->list, list) {
61f35bff 3661 list_del_init(&rspiocbp->list);
3772a991
JS
3662 __lpfc_sli_release_iocbq(phba, rspiocbp);
3663 }
3664 __lpfc_sli_release_iocbq(phba, saveq);
3665 }
3666 rspiocbp = NULL;
3667 }
3668 spin_unlock_irqrestore(&phba->hbalock, iflag);
3669 return rspiocbp;
3670}
3671
3672/**
3673 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3674 * @phba: Pointer to HBA context object.
3675 * @pring: Pointer to driver SLI ring object.
3676 * @mask: Host attention register mask for this ring.
3677 *
3772a991
JS
3678 * This routine wraps the actual slow_ring event process routine from the
3679 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3680 **/
3772a991 3681void
2e0fef85
JS
3682lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3683 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3684{
3685 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3686}
3687
3688/**
3689 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3690 * @phba: Pointer to HBA context object.
3691 * @pring: Pointer to driver SLI ring object.
3692 * @mask: Host attention register mask for this ring.
3693 *
3694 * This function is called from the worker thread when there is a ring event
3695 * for non-fcp rings. The caller does not hold any lock. The function will
3696 * remove each response iocb in the response ring and calls the handle
3697 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3698 **/
3699static void
3700lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3701 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3702{
34b02dcd 3703 struct lpfc_pgp *pgp;
dea3101e
JB
3704 IOCB_t *entry;
3705 IOCB_t *irsp = NULL;
3706 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3707 uint32_t portRspPut, portRspMax;
dea3101e 3708 unsigned long iflag;
3772a991 3709 uint32_t status;
dea3101e 3710
34b02dcd 3711 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3712 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3713 pring->stats.iocb_event++;
3714
dea3101e
JB
3715 /*
3716 * The next available response entry should never exceed the maximum
3717 * entries. If it does, treat it as an adapter hardware error.
3718 */
7e56aa25 3719 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3720 portRspPut = le32_to_cpu(pgp->rspPutInx);
3721 if (portRspPut >= portRspMax) {
3722 /*
025dfdaf 3723 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3724 * rsp ring <portRspMax>
3725 */
ed957684 3726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3727 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3728 "is bigger than rsp ring %d\n",
e8b62011 3729 pring->ringno, portRspPut, portRspMax);
dea3101e 3730
2e0fef85
JS
3731 phba->link_state = LPFC_HBA_ERROR;
3732 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3733
3734 phba->work_hs = HS_FFER3;
3735 lpfc_handle_eratt(phba);
3736
3772a991 3737 return;
dea3101e
JB
3738 }
3739
3740 rmb();
7e56aa25 3741 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3742 /*
3743 * Build a completion list and call the appropriate handler.
3744 * The process is to get the next available response iocb, get
3745 * a free iocb from the list, copy the response data into the
3746 * free iocb, insert to the continuation list, and update the
3747 * next response index to slim. This process makes response
3748 * iocb's in the ring available to DMA as fast as possible but
3749 * pays a penalty for a copy operation. Since the iocb is
3750 * only 32 bytes, this penalty is considered small relative to
3751 * the PCI reads for register values and a slim write. When
3752 * the ulpLe field is set, the entire Command has been
3753 * received.
3754 */
ed957684
JS
3755 entry = lpfc_resp_iocb(phba, pring);
3756
858c9f6c 3757 phba->last_completion_time = jiffies;
2e0fef85 3758 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3759 if (rspiocbp == NULL) {
3760 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3761 "completion.\n", __func__);
dea3101e
JB
3762 break;
3763 }
3764
ed957684
JS
3765 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3766 phba->iocb_rsp_size);
dea3101e
JB
3767 irsp = &rspiocbp->iocb;
3768
7e56aa25
JS
3769 if (++pring->sli.sli3.rspidx >= portRspMax)
3770 pring->sli.sli3.rspidx = 0;
dea3101e 3771
a58cbd52
JS
3772 if (pring->ringno == LPFC_ELS_RING) {
3773 lpfc_debugfs_slow_ring_trc(phba,
3774 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3775 *(((uint32_t *) irsp) + 4),
3776 *(((uint32_t *) irsp) + 6),
3777 *(((uint32_t *) irsp) + 7));
3778 }
3779
7e56aa25
JS
3780 writel(pring->sli.sli3.rspidx,
3781 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3782
3772a991
JS
3783 spin_unlock_irqrestore(&phba->hbalock, iflag);
3784 /* Handle the response IOCB */
3785 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3786 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3787
3788 /*
3789 * If the port response put pointer has not been updated, sync
3790 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3791 * response put pointer.
3792 */
7e56aa25 3793 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3794 portRspPut = le32_to_cpu(pgp->rspPutInx);
3795 }
7e56aa25 3796 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3797
92d7f7b0 3798 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3799 /* At least one response entry has been freed */
3800 pring->stats.iocb_rsp_full++;
3801 /* SET RxRE_RSP in Chip Att register */
3802 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3803 writel(status, phba->CAregaddr);
3804 readl(phba->CAregaddr); /* flush */
3805 }
3806 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3807 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3808 pring->stats.iocb_cmd_empty++;
3809
3810 /* Force update of the local copy of cmdGetInx */
7e56aa25 3811 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3812 lpfc_sli_resume_iocb(phba, pring);
3813
3814 if ((pring->lpfc_sli_cmd_available))
3815 (pring->lpfc_sli_cmd_available) (phba, pring);
3816
3817 }
3818
2e0fef85 3819 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3820 return;
dea3101e
JB
3821}
3822
4f774513
JS
3823/**
3824 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3825 * @phba: Pointer to HBA context object.
3826 * @pring: Pointer to driver SLI ring object.
3827 * @mask: Host attention register mask for this ring.
3828 *
3829 * This function is called from the worker thread when there is a pending
3830 * ELS response iocb on the driver internal slow-path response iocb worker
3831 * queue. The caller does not hold any lock. The function will remove each
3832 * response iocb from the response worker queue and calls the handle
3833 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3834 **/
3835static void
3836lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3837 struct lpfc_sli_ring *pring, uint32_t mask)
3838{
3839 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3840 struct hbq_dmabuf *dmabuf;
3841 struct lpfc_cq_event *cq_event;
4f774513 3842 unsigned long iflag;
0ef01a2d 3843 int count = 0;
4f774513 3844
45ed1190
JS
3845 spin_lock_irqsave(&phba->hbalock, iflag);
3846 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3847 spin_unlock_irqrestore(&phba->hbalock, iflag);
3848 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3849 /* Get the response iocb from the head of work queue */
3850 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3851 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3852 cq_event, struct lpfc_cq_event, list);
4f774513 3853 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3854
3855 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3856 case CQE_CODE_COMPL_WQE:
3857 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3858 cq_event);
45ed1190
JS
3859 /* Translate ELS WCQE to response IOCBQ */
3860 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3861 irspiocbq);
3862 if (irspiocbq)
3863 lpfc_sli_sp_handle_rspiocb(phba, pring,
3864 irspiocbq);
0ef01a2d 3865 count++;
4d9ab994
JS
3866 break;
3867 case CQE_CODE_RECEIVE:
7851fe2c 3868 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3869 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3870 cq_event);
3871 lpfc_sli4_handle_received_buffer(phba, dmabuf);
0ef01a2d 3872 count++;
4d9ab994
JS
3873 break;
3874 default:
3875 break;
3876 }
0ef01a2d
JS
3877
3878 /* Limit the number of events to 64 to avoid soft lockups */
3879 if (count == 64)
3880 break;
4f774513
JS
3881 }
3882}
3883
e59058c4 3884/**
3621a710 3885 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3886 * @phba: Pointer to HBA context object.
3887 * @pring: Pointer to driver SLI ring object.
3888 *
3889 * This function aborts all iocbs in the given ring and frees all the iocb
3890 * objects in txq. This function issues an abort iocb for all the iocb commands
3891 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3892 * the return of this function. The caller is not required to hold any locks.
3893 **/
2e0fef85 3894void
dea3101e
JB
3895lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3896{
2534ba75 3897 LIST_HEAD(completions);
dea3101e 3898 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3899
92d7f7b0
JS
3900 if (pring->ringno == LPFC_ELS_RING) {
3901 lpfc_fabric_abort_hba(phba);
3902 }
3903
dea3101e
JB
3904 /* Error everything on txq and txcmplq
3905 * First do the txq.
3906 */
db55fba8
JS
3907 if (phba->sli_rev >= LPFC_SLI_REV4) {
3908 spin_lock_irq(&pring->ring_lock);
3909 list_splice_init(&pring->txq, &completions);
3910 pring->txq_cnt = 0;
3911 spin_unlock_irq(&pring->ring_lock);
dea3101e 3912
db55fba8
JS
3913 spin_lock_irq(&phba->hbalock);
3914 /* Next issue ABTS for everything on the txcmplq */
3915 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3916 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3917 spin_unlock_irq(&phba->hbalock);
3918 } else {
3919 spin_lock_irq(&phba->hbalock);
3920 list_splice_init(&pring->txq, &completions);
3921 pring->txq_cnt = 0;
dea3101e 3922
db55fba8
JS
3923 /* Next issue ABTS for everything on the txcmplq */
3924 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3925 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3926 spin_unlock_irq(&phba->hbalock);
3927 }
dea3101e 3928
a257bf90
JS
3929 /* Cancel all the IOCBs from the completions list */
3930 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3931 IOERR_SLI_ABORTED);
dea3101e
JB
3932}
3933
895427bd
JS
3934/**
3935 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3936 * @phba: Pointer to HBA context object.
3937 * @pring: Pointer to driver SLI ring object.
3938 *
3939 * This function aborts all iocbs in the given ring and frees all the iocb
3940 * objects in txq. This function issues an abort iocb for all the iocb commands
3941 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3942 * the return of this function. The caller is not required to hold any locks.
3943 **/
3944void
3945lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3946{
3947 LIST_HEAD(completions);
3948 struct lpfc_iocbq *iocb, *next_iocb;
3949
3950 if (pring->ringno == LPFC_ELS_RING)
3951 lpfc_fabric_abort_hba(phba);
3952
3953 spin_lock_irq(&phba->hbalock);
3954 /* Next issue ABTS for everything on the txcmplq */
3955 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3956 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3957 spin_unlock_irq(&phba->hbalock);
3958}
3959
3960
db55fba8
JS
3961/**
3962 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3963 * @phba: Pointer to HBA context object.
3964 * @pring: Pointer to driver SLI ring object.
3965 *
3966 * This function aborts all iocbs in FCP rings and frees all the iocb
3967 * objects in txq. This function issues an abort iocb for all the iocb commands
3968 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3969 * the return of this function. The caller is not required to hold any locks.
3970 **/
3971void
3972lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3973{
3974 struct lpfc_sli *psli = &phba->sli;
3975 struct lpfc_sli_ring *pring;
3976 uint32_t i;
3977
3978 /* Look on all the FCP Rings for the iotag */
3979 if (phba->sli_rev >= LPFC_SLI_REV4) {
3980 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3981 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3982 lpfc_sli_abort_iocb_ring(phba, pring);
3983 }
3984 } else {
895427bd 3985 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3986 lpfc_sli_abort_iocb_ring(phba, pring);
3987 }
3988}
3989
895427bd
JS
3990/**
3991 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3992 * @phba: Pointer to HBA context object.
3993 *
3994 * This function aborts all wqes in NVME rings. This function issues an
3995 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3996 * the txcmplq is not guaranteed to complete before the return of this
3997 * function. The caller is not required to hold any locks.
3998 **/
3999void
4000lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
4001{
4002 struct lpfc_sli_ring *pring;
4003 uint32_t i;
4004
4005 if (phba->sli_rev < LPFC_SLI_REV4)
4006 return;
4007
4008 /* Abort all IO on each NVME ring. */
4009 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4010 pring = phba->sli4_hba.nvme_wq[i]->pring;
4011 lpfc_sli_abort_wqe_ring(phba, pring);
4012 }
4013}
4014
db55fba8 4015
a8e497d5 4016/**
3621a710 4017 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
4018 * @phba: Pointer to HBA context object.
4019 *
4020 * This function flushes all iocbs in the fcp ring and frees all the iocb
4021 * objects in txq and txcmplq. This function will not issue abort iocbs
4022 * for all the iocb commands in txcmplq, they will just be returned with
4023 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4024 * slot has been permanently disabled.
4025 **/
4026void
4027lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
4028{
4029 LIST_HEAD(txq);
4030 LIST_HEAD(txcmplq);
a8e497d5
JS
4031 struct lpfc_sli *psli = &phba->sli;
4032 struct lpfc_sli_ring *pring;
db55fba8 4033 uint32_t i;
c1dd9111 4034 struct lpfc_iocbq *piocb, *next_iocb;
a8e497d5
JS
4035
4036 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
4037 /* Indicate the I/O queues are flushed */
4038 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
4039 spin_unlock_irq(&phba->hbalock);
4040
db55fba8
JS
4041 /* Look on all the FCP Rings for the iotag */
4042 if (phba->sli_rev >= LPFC_SLI_REV4) {
4043 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 4044 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
4045
4046 spin_lock_irq(&pring->ring_lock);
4047 /* Retrieve everything on txq */
4048 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
4049 list_for_each_entry_safe(piocb, next_iocb,
4050 &pring->txcmplq, list)
4051 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
4052 /* Retrieve everything on the txcmplq */
4053 list_splice_init(&pring->txcmplq, &txcmplq);
4054 pring->txq_cnt = 0;
4055 pring->txcmplq_cnt = 0;
4056 spin_unlock_irq(&pring->ring_lock);
4057
4058 /* Flush the txq */
4059 lpfc_sli_cancel_iocbs(phba, &txq,
4060 IOSTAT_LOCAL_REJECT,
4061 IOERR_SLI_DOWN);
4062 /* Flush the txcmpq */
4063 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4064 IOSTAT_LOCAL_REJECT,
4065 IOERR_SLI_DOWN);
4066 }
4067 } else {
895427bd 4068 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 4069
db55fba8
JS
4070 spin_lock_irq(&phba->hbalock);
4071 /* Retrieve everything on txq */
4072 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
4073 list_for_each_entry_safe(piocb, next_iocb,
4074 &pring->txcmplq, list)
4075 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
4076 /* Retrieve everything on the txcmplq */
4077 list_splice_init(&pring->txcmplq, &txcmplq);
4078 pring->txq_cnt = 0;
4079 pring->txcmplq_cnt = 0;
4080 spin_unlock_irq(&phba->hbalock);
4081
4082 /* Flush the txq */
4083 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4084 IOERR_SLI_DOWN);
4085 /* Flush the txcmpq */
4086 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4087 IOERR_SLI_DOWN);
4088 }
a8e497d5
JS
4089}
4090
895427bd
JS
4091/**
4092 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4093 * @phba: Pointer to HBA context object.
4094 *
4095 * This function flushes all wqes in the nvme rings and frees all resources
4096 * in the txcmplq. This function does not issue abort wqes for the IO
4097 * commands in txcmplq, they will just be returned with
4098 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4099 * slot has been permanently disabled.
4100 **/
4101void
4102lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4103{
4104 LIST_HEAD(txcmplq);
4105 struct lpfc_sli_ring *pring;
4106 uint32_t i;
c1dd9111 4107 struct lpfc_iocbq *piocb, *next_iocb;
895427bd
JS
4108
4109 if (phba->sli_rev < LPFC_SLI_REV4)
4110 return;
4111
4112 /* Hint to other driver operations that a flush is in progress. */
4113 spin_lock_irq(&phba->hbalock);
4114 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4115 spin_unlock_irq(&phba->hbalock);
4116
4117 /* Cycle through all NVME rings and complete each IO with
4118 * a local driver reason code. This is a flush so no
4119 * abort exchange to FW.
4120 */
4121 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4122 pring = phba->sli4_hba.nvme_wq[i]->pring;
4123
895427bd 4124 spin_lock_irq(&pring->ring_lock);
c1dd9111
JS
4125 list_for_each_entry_safe(piocb, next_iocb,
4126 &pring->txcmplq, list)
4127 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4128 /* Retrieve everything on the txcmplq */
895427bd
JS
4129 list_splice_init(&pring->txcmplq, &txcmplq);
4130 pring->txcmplq_cnt = 0;
4131 spin_unlock_irq(&pring->ring_lock);
4132
4133 /* Flush the txcmpq &&&PAE */
4134 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4135 IOSTAT_LOCAL_REJECT,
4136 IOERR_SLI_DOWN);
4137 }
4138}
4139
e59058c4 4140/**
3772a991 4141 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
4142 * @phba: Pointer to HBA context object.
4143 * @mask: Bit mask to be checked.
4144 *
4145 * This function reads the host status register and compares
4146 * with the provided bit mask to check if HBA completed
4147 * the restart. This function will wait in a loop for the
4148 * HBA to complete restart. If the HBA does not restart within
4149 * 15 iterations, the function will reset the HBA again. The
4150 * function returns 1 when HBA fail to restart otherwise returns
4151 * zero.
4152 **/
3772a991
JS
4153static int
4154lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 4155{
41415862
JW
4156 uint32_t status;
4157 int i = 0;
4158 int retval = 0;
dea3101e 4159
41415862 4160 /* Read the HBA Host Status Register */
9940b97b
JS
4161 if (lpfc_readl(phba->HSregaddr, &status))
4162 return 1;
dea3101e 4163
41415862
JW
4164 /*
4165 * Check status register every 100ms for 5 retries, then every
4166 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4167 * every 2.5 sec for 4.
4168 * Break our of the loop if errors occurred during init.
4169 */
4170 while (((status & mask) != mask) &&
4171 !(status & HS_FFERM) &&
4172 i++ < 20) {
dea3101e 4173
41415862
JW
4174 if (i <= 5)
4175 msleep(10);
4176 else if (i <= 10)
4177 msleep(500);
4178 else
4179 msleep(2500);
dea3101e 4180
41415862 4181 if (i == 15) {
2e0fef85 4182 /* Do post */
92d7f7b0 4183 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
4184 lpfc_sli_brdrestart(phba);
4185 }
4186 /* Read the HBA Host Status Register */
9940b97b
JS
4187 if (lpfc_readl(phba->HSregaddr, &status)) {
4188 retval = 1;
4189 break;
4190 }
41415862 4191 }
dea3101e 4192
41415862
JW
4193 /* Check to see if any errors occurred during init */
4194 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
4195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4196 "2751 Adapter failed to restart, "
4197 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4198 status,
4199 readl(phba->MBslimaddr + 0xa8),
4200 readl(phba->MBslimaddr + 0xac));
2e0fef85 4201 phba->link_state = LPFC_HBA_ERROR;
41415862 4202 retval = 1;
dea3101e 4203 }
dea3101e 4204
41415862
JW
4205 return retval;
4206}
dea3101e 4207
da0436e9
JS
4208/**
4209 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4210 * @phba: Pointer to HBA context object.
4211 * @mask: Bit mask to be checked.
4212 *
4213 * This function checks the host status register to check if HBA is
4214 * ready. This function will wait in a loop for the HBA to be ready
4215 * If the HBA is not ready , the function will will reset the HBA PCI
4216 * function again. The function returns 1 when HBA fail to be ready
4217 * otherwise returns zero.
4218 **/
4219static int
4220lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4221{
4222 uint32_t status;
4223 int retval = 0;
4224
4225 /* Read the HBA Host Status Register */
4226 status = lpfc_sli4_post_status_check(phba);
4227
4228 if (status) {
4229 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4230 lpfc_sli_brdrestart(phba);
4231 status = lpfc_sli4_post_status_check(phba);
4232 }
4233
4234 /* Check to see if any errors occurred during init */
4235 if (status) {
4236 phba->link_state = LPFC_HBA_ERROR;
4237 retval = 1;
4238 } else
4239 phba->sli4_hba.intr_enable = 0;
4240
4241 return retval;
4242}
4243
4244/**
4245 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4246 * @phba: Pointer to HBA context object.
4247 * @mask: Bit mask to be checked.
4248 *
4249 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4250 * from the API jump table function pointer from the lpfc_hba struct.
4251 **/
4252int
4253lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4254{
4255 return phba->lpfc_sli_brdready(phba, mask);
4256}
4257
9290831f
JS
4258#define BARRIER_TEST_PATTERN (0xdeadbeef)
4259
e59058c4 4260/**
3621a710 4261 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
4262 * @phba: Pointer to HBA context object.
4263 *
1b51197d
JS
4264 * This function is called before resetting an HBA. This function is called
4265 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 4266 **/
2e0fef85 4267void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4268{
65a29c16
JS
4269 uint32_t __iomem *resp_buf;
4270 uint32_t __iomem *mbox_buf;
9290831f 4271 volatile uint32_t mbox;
9940b97b 4272 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4273 int i;
4274 uint8_t hdrtype;
4275
1c2ba475
JT
4276 lockdep_assert_held(&phba->hbalock);
4277
9290831f
JS
4278 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4279 if (hdrtype != 0x80 ||
4280 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4281 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4282 return;
4283
4284 /*
4285 * Tell the other part of the chip to suspend temporarily all
4286 * its DMA activity.
4287 */
65a29c16 4288 resp_buf = phba->MBslimaddr;
9290831f
JS
4289
4290 /* Disable the error attention */
9940b97b
JS
4291 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4292 return;
9290831f
JS
4293 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4294 readl(phba->HCregaddr); /* flush */
2e0fef85 4295 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4296
9940b97b
JS
4297 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4298 return;
4299 if (ha_copy & HA_ERATT) {
9290831f
JS
4300 /* Clear Chip error bit */
4301 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4302 phba->pport->stopped = 1;
9290831f
JS
4303 }
4304
4305 mbox = 0;
4306 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4307 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4308
4309 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4310 mbox_buf = phba->MBslimaddr;
9290831f
JS
4311 writel(mbox, mbox_buf);
4312
9940b97b
JS
4313 for (i = 0; i < 50; i++) {
4314 if (lpfc_readl((resp_buf + 1), &resp_data))
4315 return;
4316 if (resp_data != ~(BARRIER_TEST_PATTERN))
4317 mdelay(1);
4318 else
4319 break;
4320 }
4321 resp_data = 0;
4322 if (lpfc_readl((resp_buf + 1), &resp_data))
4323 return;
4324 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4325 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4326 phba->pport->stopped)
9290831f
JS
4327 goto restore_hc;
4328 else
4329 goto clear_errat;
4330 }
4331
4332 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4333 resp_data = 0;
4334 for (i = 0; i < 500; i++) {
4335 if (lpfc_readl(resp_buf, &resp_data))
4336 return;
4337 if (resp_data != mbox)
4338 mdelay(1);
4339 else
4340 break;
4341 }
9290831f
JS
4342
4343clear_errat:
4344
9940b97b
JS
4345 while (++i < 500) {
4346 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4347 return;
4348 if (!(ha_copy & HA_ERATT))
4349 mdelay(1);
4350 else
4351 break;
4352 }
9290831f
JS
4353
4354 if (readl(phba->HAregaddr) & HA_ERATT) {
4355 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4356 phba->pport->stopped = 1;
9290831f
JS
4357 }
4358
4359restore_hc:
2e0fef85 4360 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4361 writel(hc_copy, phba->HCregaddr);
4362 readl(phba->HCregaddr); /* flush */
4363}
4364
e59058c4 4365/**
3621a710 4366 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4367 * @phba: Pointer to HBA context object.
4368 *
4369 * This function issues a kill_board mailbox command and waits for
4370 * the error attention interrupt. This function is called for stopping
4371 * the firmware processing. The caller is not required to hold any
4372 * locks. This function calls lpfc_hba_down_post function to free
4373 * any pending commands after the kill. The function will return 1 when it
4374 * fails to kill the board else will return 0.
4375 **/
41415862 4376int
2e0fef85 4377lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4378{
4379 struct lpfc_sli *psli;
4380 LPFC_MBOXQ_t *pmb;
4381 uint32_t status;
4382 uint32_t ha_copy;
4383 int retval;
4384 int i = 0;
dea3101e 4385
41415862 4386 psli = &phba->sli;
dea3101e 4387
41415862 4388 /* Kill HBA */
ed957684 4389 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4390 "0329 Kill HBA Data: x%x x%x\n",
4391 phba->pport->port_state, psli->sli_flag);
41415862 4392
98c9ea5c
JS
4393 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4394 if (!pmb)
41415862 4395 return 1;
41415862
JW
4396
4397 /* Disable the error attention */
2e0fef85 4398 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4399 if (lpfc_readl(phba->HCregaddr, &status)) {
4400 spin_unlock_irq(&phba->hbalock);
4401 mempool_free(pmb, phba->mbox_mem_pool);
4402 return 1;
4403 }
41415862
JW
4404 status &= ~HC_ERINT_ENA;
4405 writel(status, phba->HCregaddr);
4406 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4407 phba->link_flag |= LS_IGNORE_ERATT;
4408 spin_unlock_irq(&phba->hbalock);
41415862
JW
4409
4410 lpfc_kill_board(phba, pmb);
4411 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4412 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4413
4414 if (retval != MBX_SUCCESS) {
4415 if (retval != MBX_BUSY)
4416 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4417 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4418 "2752 KILL_BOARD command failed retval %d\n",
4419 retval);
2e0fef85
JS
4420 spin_lock_irq(&phba->hbalock);
4421 phba->link_flag &= ~LS_IGNORE_ERATT;
4422 spin_unlock_irq(&phba->hbalock);
41415862
JW
4423 return 1;
4424 }
4425
f4b4c68f
JS
4426 spin_lock_irq(&phba->hbalock);
4427 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4428 spin_unlock_irq(&phba->hbalock);
9290831f 4429
41415862
JW
4430 mempool_free(pmb, phba->mbox_mem_pool);
4431
4432 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4433 * attention every 100ms for 3 seconds. If we don't get ERATT after
4434 * 3 seconds we still set HBA_ERROR state because the status of the
4435 * board is now undefined.
4436 */
9940b97b
JS
4437 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4438 return 1;
41415862
JW
4439 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4440 mdelay(100);
9940b97b
JS
4441 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4442 return 1;
41415862
JW
4443 }
4444
4445 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4446 if (ha_copy & HA_ERATT) {
4447 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4448 phba->pport->stopped = 1;
9290831f 4449 }
2e0fef85 4450 spin_lock_irq(&phba->hbalock);
41415862 4451 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4452 psli->mbox_active = NULL;
2e0fef85
JS
4453 phba->link_flag &= ~LS_IGNORE_ERATT;
4454 spin_unlock_irq(&phba->hbalock);
41415862 4455
41415862 4456 lpfc_hba_down_post(phba);
2e0fef85 4457 phba->link_state = LPFC_HBA_ERROR;
41415862 4458
2e0fef85 4459 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4460}
4461
e59058c4 4462/**
3772a991 4463 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4464 * @phba: Pointer to HBA context object.
4465 *
4466 * This function resets the HBA by writing HC_INITFF to the control
4467 * register. After the HBA resets, this function resets all the iocb ring
4468 * indices. This function disables PCI layer parity checking during
4469 * the reset.
4470 * This function returns 0 always.
4471 * The caller is not required to hold any locks.
4472 **/
41415862 4473int
2e0fef85 4474lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4475{
41415862 4476 struct lpfc_sli *psli;
dea3101e 4477 struct lpfc_sli_ring *pring;
41415862 4478 uint16_t cfg_value;
dea3101e 4479 int i;
dea3101e 4480
41415862 4481 psli = &phba->sli;
dea3101e 4482
41415862
JW
4483 /* Reset HBA */
4484 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4485 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4486 (phba->pport) ? phba->pport->port_state : 0,
4487 psli->sli_flag);
dea3101e
JB
4488
4489 /* perform board reset */
4490 phba->fc_eventTag = 0;
4d9ab994 4491 phba->link_events = 0;
4492b739
JS
4492 if (phba->pport) {
4493 phba->pport->fc_myDID = 0;
4494 phba->pport->fc_prevDID = 0;
4495 }
dea3101e 4496
41415862
JW
4497 /* Turn off parity checking and serr during the physical reset */
4498 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4499 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4500 (cfg_value &
4501 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4502
3772a991
JS
4503 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4504
41415862
JW
4505 /* Now toggle INITFF bit in the Host Control Register */
4506 writel(HC_INITFF, phba->HCregaddr);
4507 mdelay(1);
4508 readl(phba->HCregaddr); /* flush */
4509 writel(0, phba->HCregaddr);
4510 readl(phba->HCregaddr); /* flush */
4511
4512 /* Restore PCI cmd register */
4513 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4514
4515 /* Initialize relevant SLI info */
41415862 4516 for (i = 0; i < psli->num_rings; i++) {
895427bd 4517 pring = &psli->sli3_ring[i];
dea3101e 4518 pring->flag = 0;
7e56aa25
JS
4519 pring->sli.sli3.rspidx = 0;
4520 pring->sli.sli3.next_cmdidx = 0;
4521 pring->sli.sli3.local_getidx = 0;
4522 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4523 pring->missbufcnt = 0;
4524 }
dea3101e 4525
2e0fef85 4526 phba->link_state = LPFC_WARM_START;
41415862
JW
4527 return 0;
4528}
4529
e59058c4 4530/**
da0436e9
JS
4531 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4532 * @phba: Pointer to HBA context object.
4533 *
4534 * This function resets a SLI4 HBA. This function disables PCI layer parity
4535 * checking during resets the device. The caller is not required to hold
4536 * any locks.
4537 *
4538 * This function returns 0 always.
4539 **/
4540int
4541lpfc_sli4_brdreset(struct lpfc_hba *phba)
4542{
4543 struct lpfc_sli *psli = &phba->sli;
4544 uint16_t cfg_value;
0293635e 4545 int rc = 0;
da0436e9
JS
4546
4547 /* Reset HBA */
4548 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4549 "0295 Reset HBA Data: x%x x%x x%x\n",
4550 phba->pport->port_state, psli->sli_flag,
4551 phba->hba_flag);
da0436e9
JS
4552
4553 /* perform board reset */
4554 phba->fc_eventTag = 0;
4d9ab994 4555 phba->link_events = 0;
da0436e9
JS
4556 phba->pport->fc_myDID = 0;
4557 phba->pport->fc_prevDID = 0;
4558
da0436e9
JS
4559 spin_lock_irq(&phba->hbalock);
4560 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4561 phba->fcf.fcf_flag = 0;
da0436e9
JS
4562 spin_unlock_irq(&phba->hbalock);
4563
0293635e
JS
4564 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4565 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4566 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4567 return rc;
4568 }
4569
da0436e9
JS
4570 /* Now physically reset the device */
4571 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4572 "0389 Performing PCI function reset!\n");
be858b65
JS
4573
4574 /* Turn off parity checking and serr during the physical reset */
4575 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4576 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4577 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4578
88318816 4579 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4580 rc = lpfc_pci_function_reset(phba);
da0436e9 4581
be858b65
JS
4582 /* Restore PCI cmd register */
4583 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4584
27b01b82 4585 return rc;
da0436e9
JS
4586}
4587
4588/**
4589 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4590 * @phba: Pointer to HBA context object.
4591 *
4592 * This function is called in the SLI initialization code path to
4593 * restart the HBA. The caller is not required to hold any lock.
4594 * This function writes MBX_RESTART mailbox command to the SLIM and
4595 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4596 * function to free any pending commands. The function enables
4597 * POST only during the first initialization. The function returns zero.
4598 * The function does not guarantee completion of MBX_RESTART mailbox
4599 * command before the return of this function.
4600 **/
da0436e9
JS
4601static int
4602lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4603{
4604 MAILBOX_t *mb;
4605 struct lpfc_sli *psli;
41415862
JW
4606 volatile uint32_t word0;
4607 void __iomem *to_slim;
0d878419 4608 uint32_t hba_aer_enabled;
41415862 4609
2e0fef85 4610 spin_lock_irq(&phba->hbalock);
41415862 4611
0d878419
JS
4612 /* Take PCIe device Advanced Error Reporting (AER) state */
4613 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4614
41415862
JW
4615 psli = &phba->sli;
4616
4617 /* Restart HBA */
4618 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4619 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4620 (phba->pport) ? phba->pport->port_state : 0,
4621 psli->sli_flag);
41415862
JW
4622
4623 word0 = 0;
4624 mb = (MAILBOX_t *) &word0;
4625 mb->mbxCommand = MBX_RESTART;
4626 mb->mbxHc = 1;
4627
9290831f
JS
4628 lpfc_reset_barrier(phba);
4629
41415862
JW
4630 to_slim = phba->MBslimaddr;
4631 writel(*(uint32_t *) mb, to_slim);
4632 readl(to_slim); /* flush */
4633
4634 /* Only skip post after fc_ffinit is completed */
4492b739 4635 if (phba->pport && phba->pport->port_state)
41415862 4636 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4637 else
41415862 4638 word0 = 0; /* This is really setting up word1 */
65a29c16 4639 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4640 writel(*(uint32_t *) mb, to_slim);
4641 readl(to_slim); /* flush */
dea3101e 4642
41415862 4643 lpfc_sli_brdreset(phba);
4492b739
JS
4644 if (phba->pport)
4645 phba->pport->stopped = 0;
2e0fef85 4646 phba->link_state = LPFC_INIT_START;
da0436e9 4647 phba->hba_flag = 0;
2e0fef85 4648 spin_unlock_irq(&phba->hbalock);
41415862 4649
64ba8818 4650 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
c4d6204d 4651 psli->stats_start = ktime_get_seconds();
64ba8818 4652
eaf15d5b
JS
4653 /* Give the INITFF and Post time to settle. */
4654 mdelay(100);
41415862 4655
0d878419
JS
4656 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4657 if (hba_aer_enabled)
4658 pci_disable_pcie_error_reporting(phba->pcidev);
4659
41415862 4660 lpfc_hba_down_post(phba);
dea3101e
JB
4661
4662 return 0;
4663}
4664
da0436e9
JS
4665/**
4666 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4667 * @phba: Pointer to HBA context object.
4668 *
4669 * This function is called in the SLI initialization code path to restart
4670 * a SLI4 HBA. The caller is not required to hold any lock.
4671 * At the end of the function, it calls lpfc_hba_down_post function to
4672 * free any pending commands.
4673 **/
4674static int
4675lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4676{
4677 struct lpfc_sli *psli = &phba->sli;
75baf696 4678 uint32_t hba_aer_enabled;
27b01b82 4679 int rc;
da0436e9
JS
4680
4681 /* Restart HBA */
4682 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4683 "0296 Restart HBA Data: x%x x%x\n",
4684 phba->pport->port_state, psli->sli_flag);
4685
75baf696
JS
4686 /* Take PCIe device Advanced Error Reporting (AER) state */
4687 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4688
27b01b82 4689 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4690
4691 spin_lock_irq(&phba->hbalock);
4692 phba->pport->stopped = 0;
4693 phba->link_state = LPFC_INIT_START;
4694 phba->hba_flag = 0;
4695 spin_unlock_irq(&phba->hbalock);
4696
4697 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
c4d6204d 4698 psli->stats_start = ktime_get_seconds();
da0436e9 4699
75baf696
JS
4700 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4701 if (hba_aer_enabled)
4702 pci_disable_pcie_error_reporting(phba->pcidev);
4703
da0436e9 4704 lpfc_hba_down_post(phba);
569dbe84 4705 lpfc_sli4_queue_destroy(phba);
da0436e9 4706
27b01b82 4707 return rc;
da0436e9
JS
4708}
4709
4710/**
4711 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4712 * @phba: Pointer to HBA context object.
4713 *
4714 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4715 * API jump table function pointer from the lpfc_hba struct.
4716**/
4717int
4718lpfc_sli_brdrestart(struct lpfc_hba *phba)
4719{
4720 return phba->lpfc_sli_brdrestart(phba);
4721}
4722
e59058c4 4723/**
3621a710 4724 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4725 * @phba: Pointer to HBA context object.
4726 *
4727 * This function is called after a HBA restart to wait for successful
4728 * restart of the HBA. Successful restart of the HBA is indicated by
4729 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4730 * iteration, the function will restart the HBA again. The function returns
4731 * zero if HBA successfully restarted else returns negative error code.
4732 **/
4492b739 4733int
dea3101e
JB
4734lpfc_sli_chipset_init(struct lpfc_hba *phba)
4735{
4736 uint32_t status, i = 0;
4737
4738 /* Read the HBA Host Status Register */
9940b97b
JS
4739 if (lpfc_readl(phba->HSregaddr, &status))
4740 return -EIO;
dea3101e
JB
4741
4742 /* Check status register to see what current state is */
4743 i = 0;
4744 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4745
dcf2a4e0
JS
4746 /* Check every 10ms for 10 retries, then every 100ms for 90
4747 * retries, then every 1 sec for 50 retires for a total of
4748 * ~60 seconds before reset the board again and check every
4749 * 1 sec for 50 retries. The up to 60 seconds before the
4750 * board ready is required by the Falcon FIPS zeroization
4751 * complete, and any reset the board in between shall cause
4752 * restart of zeroization, further delay the board ready.
dea3101e 4753 */
dcf2a4e0 4754 if (i++ >= 200) {
dea3101e
JB
4755 /* Adapter failed to init, timeout, status reg
4756 <status> */
ed957684 4757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4758 "0436 Adapter failed to init, "
09372820
JS
4759 "timeout, status reg x%x, "
4760 "FW Data: A8 x%x AC x%x\n", status,
4761 readl(phba->MBslimaddr + 0xa8),
4762 readl(phba->MBslimaddr + 0xac));
2e0fef85 4763 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4764 return -ETIMEDOUT;
4765 }
4766
4767 /* Check to see if any errors occurred during init */
4768 if (status & HS_FFERM) {
4769 /* ERROR: During chipset initialization */
4770 /* Adapter failed to init, chipset, status reg
4771 <status> */
ed957684 4772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4773 "0437 Adapter failed to init, "
09372820
JS
4774 "chipset, status reg x%x, "
4775 "FW Data: A8 x%x AC x%x\n", status,
4776 readl(phba->MBslimaddr + 0xa8),
4777 readl(phba->MBslimaddr + 0xac));
2e0fef85 4778 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4779 return -EIO;
4780 }
4781
dcf2a4e0 4782 if (i <= 10)
dea3101e 4783 msleep(10);
dcf2a4e0
JS
4784 else if (i <= 100)
4785 msleep(100);
4786 else
4787 msleep(1000);
dea3101e 4788
dcf2a4e0
JS
4789 if (i == 150) {
4790 /* Do post */
92d7f7b0 4791 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4792 lpfc_sli_brdrestart(phba);
dea3101e
JB
4793 }
4794 /* Read the HBA Host Status Register */
9940b97b
JS
4795 if (lpfc_readl(phba->HSregaddr, &status))
4796 return -EIO;
dea3101e
JB
4797 }
4798
4799 /* Check to see if any errors occurred during init */
4800 if (status & HS_FFERM) {
4801 /* ERROR: During chipset initialization */
4802 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4804 "0438 Adapter failed to init, chipset, "
09372820
JS
4805 "status reg x%x, "
4806 "FW Data: A8 x%x AC x%x\n", status,
4807 readl(phba->MBslimaddr + 0xa8),
4808 readl(phba->MBslimaddr + 0xac));
2e0fef85 4809 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4810 return -EIO;
4811 }
4812
4813 /* Clear all interrupt enable conditions */
4814 writel(0, phba->HCregaddr);
4815 readl(phba->HCregaddr); /* flush */
4816
4817 /* setup host attn register */
4818 writel(0xffffffff, phba->HAregaddr);
4819 readl(phba->HAregaddr); /* flush */
4820 return 0;
4821}
4822
e59058c4 4823/**
3621a710 4824 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4825 *
4826 * This function calculates and returns the number of HBQs required to be
4827 * configured.
4828 **/
78b2d852 4829int
ed957684
JS
4830lpfc_sli_hbq_count(void)
4831{
92d7f7b0 4832 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4833}
4834
e59058c4 4835/**
3621a710 4836 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4837 *
4838 * This function adds the number of hbq entries in every HBQ to get
4839 * the total number of hbq entries required for the HBA and returns
4840 * the total count.
4841 **/
ed957684
JS
4842static int
4843lpfc_sli_hbq_entry_count(void)
4844{
4845 int hbq_count = lpfc_sli_hbq_count();
4846 int count = 0;
4847 int i;
4848
4849 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4850 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4851 return count;
4852}
4853
e59058c4 4854/**
3621a710 4855 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4856 *
4857 * This function calculates amount of memory required for all hbq entries
4858 * to be configured and returns the total memory required.
4859 **/
dea3101e 4860int
ed957684
JS
4861lpfc_sli_hbq_size(void)
4862{
4863 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4864}
4865
e59058c4 4866/**
3621a710 4867 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4868 * @phba: Pointer to HBA context object.
4869 *
4870 * This function is called during the SLI initialization to configure
4871 * all the HBQs and post buffers to the HBQ. The caller is not
4872 * required to hold any locks. This function will return zero if successful
4873 * else it will return negative error code.
4874 **/
ed957684
JS
4875static int
4876lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4877{
4878 int hbq_count = lpfc_sli_hbq_count();
4879 LPFC_MBOXQ_t *pmb;
4880 MAILBOX_t *pmbox;
4881 uint32_t hbqno;
4882 uint32_t hbq_entry_index;
ed957684 4883
92d7f7b0
JS
4884 /* Get a Mailbox buffer to setup mailbox
4885 * commands for HBA initialization
4886 */
ed957684
JS
4887 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4888
4889 if (!pmb)
4890 return -ENOMEM;
4891
04c68496 4892 pmbox = &pmb->u.mb;
ed957684
JS
4893
4894 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4895 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4896 phba->hbq_in_use = 1;
ed957684
JS
4897
4898 hbq_entry_index = 0;
4899 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4900 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4901 phba->hbqs[hbqno].hbqPutIdx = 0;
4902 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4903 phba->hbqs[hbqno].entry_count =
92d7f7b0 4904 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4905 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4906 hbq_entry_index, pmb);
ed957684
JS
4907 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4908
4909 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4910 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4911 mbxStatus <status>, ring <num> */
4912
4913 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4914 LOG_SLI | LOG_VPORT,
e8b62011 4915 "1805 Adapter failed to init. "
ed957684 4916 "Data: x%x x%x x%x\n",
e8b62011 4917 pmbox->mbxCommand,
ed957684
JS
4918 pmbox->mbxStatus, hbqno);
4919
4920 phba->link_state = LPFC_HBA_ERROR;
4921 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4922 return -ENXIO;
ed957684
JS
4923 }
4924 }
4925 phba->hbq_count = hbq_count;
4926
ed957684
JS
4927 mempool_free(pmb, phba->mbox_mem_pool);
4928
92d7f7b0 4929 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4930 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4931 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4932 return 0;
4933}
4934
4f774513
JS
4935/**
4936 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4937 * @phba: Pointer to HBA context object.
4938 *
4939 * This function is called during the SLI initialization to configure
4940 * all the HBQs and post buffers to the HBQ. The caller is not
4941 * required to hold any locks. This function will return zero if successful
4942 * else it will return negative error code.
4943 **/
4944static int
4945lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4946{
4947 phba->hbq_in_use = 1;
895427bd
JS
4948 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4949 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4950 phba->hbq_count = 1;
895427bd 4951 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4952 /* Initially populate or replenish the HBQs */
4f774513
JS
4953 return 0;
4954}
4955
e59058c4 4956/**
3621a710 4957 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4958 * @phba: Pointer to HBA context object.
4959 * @sli_mode: sli mode - 2/3
4960 *
183b8021 4961 * This function is called by the sli initialization code path
e59058c4
JS
4962 * to issue config_port mailbox command. This function restarts the
4963 * HBA firmware and issues a config_port mailbox command to configure
4964 * the SLI interface in the sli mode specified by sli_mode
4965 * variable. The caller is not required to hold any locks.
4966 * The function returns 0 if successful, else returns negative error
4967 * code.
4968 **/
9399627f
JS
4969int
4970lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4971{
4972 LPFC_MBOXQ_t *pmb;
4973 uint32_t resetcount = 0, rc = 0, done = 0;
4974
4975 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4976 if (!pmb) {
2e0fef85 4977 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4978 return -ENOMEM;
4979 }
4980
ed957684 4981 phba->sli_rev = sli_mode;
dea3101e 4982 while (resetcount < 2 && !done) {
2e0fef85 4983 spin_lock_irq(&phba->hbalock);
1c067a42 4984 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4985 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4986 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4987 lpfc_sli_brdrestart(phba);
dea3101e
JB
4988 rc = lpfc_sli_chipset_init(phba);
4989 if (rc)
4990 break;
4991
2e0fef85 4992 spin_lock_irq(&phba->hbalock);
1c067a42 4993 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4994 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4995 resetcount++;
4996
ed957684
JS
4997 /* Call pre CONFIG_PORT mailbox command initialization. A
4998 * value of 0 means the call was successful. Any other
4999 * nonzero value is a failure, but if ERESTART is returned,
5000 * the driver may reset the HBA and try again.
5001 */
dea3101e
JB
5002 rc = lpfc_config_port_prep(phba);
5003 if (rc == -ERESTART) {
ed957684 5004 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 5005 continue;
34b02dcd 5006 } else if (rc)
dea3101e 5007 break;
6d368e53 5008
2e0fef85 5009 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
5010 lpfc_config_port(phba, pmb);
5011 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
5012 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5013 LPFC_SLI3_HBQ_ENABLED |
5014 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
5015 LPFC_SLI3_BG_ENABLED |
5016 LPFC_SLI3_DSS_ENABLED);
ed957684 5017 if (rc != MBX_SUCCESS) {
dea3101e 5018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 5019 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 5020 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 5021 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 5022 spin_lock_irq(&phba->hbalock);
04c68496 5023 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
5024 spin_unlock_irq(&phba->hbalock);
5025 rc = -ENXIO;
04c68496
JS
5026 } else {
5027 /* Allow asynchronous mailbox command to go through */
5028 spin_lock_irq(&phba->hbalock);
5029 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5030 spin_unlock_irq(&phba->hbalock);
ed957684 5031 done = 1;
cb69f7de
JS
5032
5033 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5034 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5035 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5036 "3110 Port did not grant ASABT\n");
04c68496 5037 }
dea3101e 5038 }
ed957684
JS
5039 if (!done) {
5040 rc = -EINVAL;
5041 goto do_prep_failed;
5042 }
04c68496
JS
5043 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5044 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
5045 rc = -ENXIO;
5046 goto do_prep_failed;
5047 }
04c68496 5048 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 5049 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
5050 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5051 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5052 phba->max_vpi : phba->max_vports;
5053
34b02dcd
JS
5054 } else
5055 phba->max_vpi = 0;
bc73905a
JS
5056 phba->fips_level = 0;
5057 phba->fips_spec_rev = 0;
5058 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 5059 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
5060 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5061 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5062 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5063 "2850 Security Crypto Active. FIPS x%d "
5064 "(Spec Rev: x%d)",
5065 phba->fips_level, phba->fips_spec_rev);
5066 }
5067 if (pmb->u.mb.un.varCfgPort.sec_err) {
5068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5069 "2856 Config Port Security Crypto "
5070 "Error: x%x ",
5071 pmb->u.mb.un.varCfgPort.sec_err);
5072 }
04c68496 5073 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 5074 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 5075 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 5076 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
5077
5078 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5079 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6 5080
f44ac12f
JS
5081 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5082 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5083 phba->cfg_enable_bg = 0;
5084 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
e2a0a9d6
JS
5085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5086 "0443 Adapter did not grant "
5087 "BlockGuard\n");
f44ac12f 5088 }
e2a0a9d6 5089 }
34b02dcd 5090 } else {
8f34f4ce 5091 phba->hbq_get = NULL;
34b02dcd 5092 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 5093 phba->max_vpi = 0;
ed957684 5094 }
92d7f7b0 5095do_prep_failed:
ed957684
JS
5096 mempool_free(pmb, phba->mbox_mem_pool);
5097 return rc;
5098}
5099
e59058c4
JS
5100
5101/**
183b8021 5102 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
5103 * @phba: Pointer to HBA context object.
5104 *
183b8021
MY
5105 * This function is the main SLI initialization function. This function
5106 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
5107 * error attention handler code. Caller is not required to hold any
5108 * locks. This function issues config_port mailbox command to configure
5109 * the SLI, setup iocb rings and HBQ rings. In the end the function
5110 * calls the config_port_post function to issue init_link mailbox
5111 * command and to start the discovery. The function will return zero
5112 * if successful, else it will return negative error code.
5113 **/
ed957684
JS
5114int
5115lpfc_sli_hba_setup(struct lpfc_hba *phba)
5116{
5117 uint32_t rc;
6d368e53
JS
5118 int mode = 3, i;
5119 int longs;
ed957684 5120
12247e81 5121 switch (phba->cfg_sli_mode) {
ed957684 5122 case 2:
78b2d852 5123 if (phba->cfg_enable_npiv) {
92d7f7b0 5124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 5125 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 5126 "parameter (%d) to auto (0).\n",
12247e81 5127 phba->cfg_sli_mode);
92d7f7b0
JS
5128 break;
5129 }
ed957684
JS
5130 mode = 2;
5131 break;
5132 case 0:
5133 case 3:
5134 break;
5135 default:
92d7f7b0 5136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
5137 "1819 Unrecognized sli_mode parameter: %d.\n",
5138 phba->cfg_sli_mode);
ed957684
JS
5139
5140 break;
5141 }
b5c53958 5142 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 5143
9399627f
JS
5144 rc = lpfc_sli_config_port(phba, mode);
5145
12247e81 5146 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 5147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
5148 "1820 Unable to select SLI-3. "
5149 "Not supported by adapter.\n");
ed957684 5150 if (rc && mode != 2)
9399627f 5151 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
5152 else if (rc && mode == 2)
5153 rc = lpfc_sli_config_port(phba, 3);
ed957684 5154 if (rc)
dea3101e
JB
5155 goto lpfc_sli_hba_setup_error;
5156
0d878419
JS
5157 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5158 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5159 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5160 if (!rc) {
5161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5162 "2709 This device supports "
5163 "Advanced Error Reporting (AER)\n");
5164 spin_lock_irq(&phba->hbalock);
5165 phba->hba_flag |= HBA_AER_ENABLED;
5166 spin_unlock_irq(&phba->hbalock);
5167 } else {
5168 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5169 "2708 This device does not support "
b069d7eb
JS
5170 "Advanced Error Reporting (AER): %d\n",
5171 rc);
0d878419
JS
5172 phba->cfg_aer_support = 0;
5173 }
5174 }
5175
ed957684
JS
5176 if (phba->sli_rev == 3) {
5177 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5178 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
5179 } else {
5180 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5181 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 5182 phba->sli3_options = 0;
ed957684
JS
5183 }
5184
5185 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
5186 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5187 phba->sli_rev, phba->max_vpi);
ed957684 5188 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
5189
5190 if (rc)
5191 goto lpfc_sli_hba_setup_error;
5192
6d368e53
JS
5193 /* Initialize VPIs. */
5194 if (phba->sli_rev == LPFC_SLI_REV3) {
5195 /*
5196 * The VPI bitmask and physical ID array are allocated
5197 * and initialized once only - at driver load. A port
5198 * reset doesn't need to reinitialize this memory.
5199 */
5200 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5201 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
6396bb22
KC
5202 phba->vpi_bmask = kcalloc(longs,
5203 sizeof(unsigned long),
6d368e53
JS
5204 GFP_KERNEL);
5205 if (!phba->vpi_bmask) {
5206 rc = -ENOMEM;
5207 goto lpfc_sli_hba_setup_error;
5208 }
5209
6396bb22
KC
5210 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5211 sizeof(uint16_t),
5212 GFP_KERNEL);
6d368e53
JS
5213 if (!phba->vpi_ids) {
5214 kfree(phba->vpi_bmask);
5215 rc = -ENOMEM;
5216 goto lpfc_sli_hba_setup_error;
5217 }
5218 for (i = 0; i < phba->max_vpi; i++)
5219 phba->vpi_ids[i] = i;
5220 }
5221 }
5222
9399627f 5223 /* Init HBQs */
ed957684
JS
5224 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5225 rc = lpfc_sli_hbq_setup(phba);
5226 if (rc)
5227 goto lpfc_sli_hba_setup_error;
5228 }
04c68496 5229 spin_lock_irq(&phba->hbalock);
dea3101e 5230 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 5231 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
5232
5233 rc = lpfc_config_port_post(phba);
5234 if (rc)
5235 goto lpfc_sli_hba_setup_error;
5236
ed957684
JS
5237 return rc;
5238
92d7f7b0 5239lpfc_sli_hba_setup_error:
2e0fef85 5240 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 5241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 5242 "0445 Firmware initialization failed\n");
dea3101e
JB
5243 return rc;
5244}
5245
e59058c4 5246/**
da0436e9
JS
5247 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5248 * @phba: Pointer to HBA context object.
5249 * @mboxq: mailbox pointer.
5250 * This function issue a dump mailbox command to read config region
5251 * 23 and parse the records in the region and populate driver
5252 * data structure.
e59058c4 5253 **/
da0436e9 5254static int
ff78d8f9 5255lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 5256{
ff78d8f9 5257 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
5258 struct lpfc_dmabuf *mp;
5259 struct lpfc_mqe *mqe;
5260 uint32_t data_length;
5261 int rc;
dea3101e 5262
da0436e9
JS
5263 /* Program the default value of vlan_id and fc_map */
5264 phba->valid_vlan = 0;
5265 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5266 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5267 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 5268
ff78d8f9
JS
5269 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5270 if (!mboxq)
da0436e9
JS
5271 return -ENOMEM;
5272
ff78d8f9
JS
5273 mqe = &mboxq->u.mqe;
5274 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5275 rc = -ENOMEM;
5276 goto out_free_mboxq;
5277 }
5278
3e1f0718 5279 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
da0436e9
JS
5280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5281
5282 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5283 "(%d):2571 Mailbox cmd x%x Status x%x "
5284 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5285 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5286 "CQ: x%x x%x x%x x%x\n",
5287 mboxq->vport ? mboxq->vport->vpi : 0,
5288 bf_get(lpfc_mqe_command, mqe),
5289 bf_get(lpfc_mqe_status, mqe),
5290 mqe->un.mb_words[0], mqe->un.mb_words[1],
5291 mqe->un.mb_words[2], mqe->un.mb_words[3],
5292 mqe->un.mb_words[4], mqe->un.mb_words[5],
5293 mqe->un.mb_words[6], mqe->un.mb_words[7],
5294 mqe->un.mb_words[8], mqe->un.mb_words[9],
5295 mqe->un.mb_words[10], mqe->un.mb_words[11],
5296 mqe->un.mb_words[12], mqe->un.mb_words[13],
5297 mqe->un.mb_words[14], mqe->un.mb_words[15],
5298 mqe->un.mb_words[16], mqe->un.mb_words[50],
5299 mboxq->mcqe.word0,
5300 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5301 mboxq->mcqe.trailer);
5302
5303 if (rc) {
5304 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5305 kfree(mp);
ff78d8f9
JS
5306 rc = -EIO;
5307 goto out_free_mboxq;
da0436e9
JS
5308 }
5309 data_length = mqe->un.mb_words[5];
a0c87cbd 5310 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5311 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5312 kfree(mp);
ff78d8f9
JS
5313 rc = -EIO;
5314 goto out_free_mboxq;
d11e31dd 5315 }
dea3101e 5316
da0436e9
JS
5317 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5318 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5319 kfree(mp);
ff78d8f9
JS
5320 rc = 0;
5321
5322out_free_mboxq:
5323 mempool_free(mboxq, phba->mbox_mem_pool);
5324 return rc;
da0436e9 5325}
e59058c4
JS
5326
5327/**
da0436e9
JS
5328 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5329 * @phba: pointer to lpfc hba data structure.
5330 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5331 * @vpd: pointer to the memory to hold resulting port vpd data.
5332 * @vpd_size: On input, the number of bytes allocated to @vpd.
5333 * On output, the number of data bytes in @vpd.
e59058c4 5334 *
da0436e9
JS
5335 * This routine executes a READ_REV SLI4 mailbox command. In
5336 * addition, this routine gets the port vpd data.
5337 *
5338 * Return codes
af901ca1 5339 * 0 - successful
d439d286 5340 * -ENOMEM - could not allocated memory.
e59058c4 5341 **/
da0436e9
JS
5342static int
5343lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5344 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5345{
da0436e9
JS
5346 int rc = 0;
5347 uint32_t dma_size;
5348 struct lpfc_dmabuf *dmabuf;
5349 struct lpfc_mqe *mqe;
dea3101e 5350
da0436e9
JS
5351 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5352 if (!dmabuf)
5353 return -ENOMEM;
5354
5355 /*
5356 * Get a DMA buffer for the vpd data resulting from the READ_REV
5357 * mailbox command.
a257bf90 5358 */
da0436e9 5359 dma_size = *vpd_size;
1aee383d
JP
5360 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5361 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5362 if (!dmabuf->virt) {
5363 kfree(dmabuf);
5364 return -ENOMEM;
a257bf90
JS
5365 }
5366
da0436e9
JS
5367 /*
5368 * The SLI4 implementation of READ_REV conflicts at word1,
5369 * bits 31:16 and SLI4 adds vpd functionality not present
5370 * in SLI3. This code corrects the conflicts.
1dcb58e5 5371 */
da0436e9
JS
5372 lpfc_read_rev(phba, mboxq);
5373 mqe = &mboxq->u.mqe;
5374 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5375 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5376 mqe->un.read_rev.word1 &= 0x0000FFFF;
5377 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5378 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5379
5380 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5381 if (rc) {
5382 dma_free_coherent(&phba->pcidev->dev, dma_size,
5383 dmabuf->virt, dmabuf->phys);
def9c7a9 5384 kfree(dmabuf);
da0436e9
JS
5385 return -EIO;
5386 }
1dcb58e5 5387
da0436e9
JS
5388 /*
5389 * The available vpd length cannot be bigger than the
5390 * DMA buffer passed to the port. Catch the less than
5391 * case and update the caller's size.
5392 */
5393 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5394 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5395
d7c47992
JS
5396 memcpy(vpd, dmabuf->virt, *vpd_size);
5397
da0436e9
JS
5398 dma_free_coherent(&phba->pcidev->dev, dma_size,
5399 dmabuf->virt, dmabuf->phys);
5400 kfree(dmabuf);
5401 return 0;
dea3101e
JB
5402}
5403
cd1c8301
JS
5404/**
5405 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5406 * @phba: pointer to lpfc hba data structure.
5407 *
5408 * This routine retrieves SLI4 device physical port name this PCI function
5409 * is attached to.
5410 *
5411 * Return codes
4907cb7b 5412 * 0 - successful
cd1c8301
JS
5413 * otherwise - failed to retrieve physical port name
5414 **/
5415static int
5416lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5417{
5418 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5419 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5420 struct lpfc_controller_attribute *cntl_attr;
5421 struct lpfc_mbx_get_port_name *get_port_name;
5422 void *virtaddr = NULL;
5423 uint32_t alloclen, reqlen;
5424 uint32_t shdr_status, shdr_add_status;
5425 union lpfc_sli4_cfg_shdr *shdr;
5426 char cport_name = 0;
5427 int rc;
5428
5429 /* We assume nothing at this point */
5430 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5431 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5432
5433 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5434 if (!mboxq)
5435 return -ENOMEM;
cd1c8301 5436 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5437 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5438 lpfc_sli4_read_config(phba);
5439 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5440 goto retrieve_ppname;
cd1c8301
JS
5441
5442 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5443 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5444 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5445 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5446 LPFC_SLI4_MBX_NEMBED);
5447 if (alloclen < reqlen) {
5448 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5449 "3084 Allocated DMA memory size (%d) is "
5450 "less than the requested DMA memory size "
5451 "(%d)\n", alloclen, reqlen);
5452 rc = -ENOMEM;
5453 goto out_free_mboxq;
5454 }
5455 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5456 virtaddr = mboxq->sge_array->addr[0];
5457 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5458 shdr = &mbx_cntl_attr->cfg_shdr;
5459 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5460 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5461 if (shdr_status || shdr_add_status || rc) {
5462 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5463 "3085 Mailbox x%x (x%x/x%x) failed, "
5464 "rc:x%x, status:x%x, add_status:x%x\n",
5465 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5466 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5467 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5468 rc, shdr_status, shdr_add_status);
5469 rc = -ENXIO;
5470 goto out_free_mboxq;
5471 }
5472 cntl_attr = &mbx_cntl_attr->cntl_attr;
5473 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5474 phba->sli4_hba.lnk_info.lnk_tp =
5475 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5476 phba->sli4_hba.lnk_info.lnk_no =
5477 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5478 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5479 "3086 lnk_type:%d, lnk_numb:%d\n",
5480 phba->sli4_hba.lnk_info.lnk_tp,
5481 phba->sli4_hba.lnk_info.lnk_no);
5482
5483retrieve_ppname:
5484 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5485 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5486 sizeof(struct lpfc_mbx_get_port_name) -
5487 sizeof(struct lpfc_sli4_cfg_mhdr),
5488 LPFC_SLI4_MBX_EMBED);
5489 get_port_name = &mboxq->u.mqe.un.get_port_name;
5490 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5491 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5492 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5493 phba->sli4_hba.lnk_info.lnk_tp);
5494 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5495 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5496 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5497 if (shdr_status || shdr_add_status || rc) {
5498 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5499 "3087 Mailbox x%x (x%x/x%x) failed: "
5500 "rc:x%x, status:x%x, add_status:x%x\n",
5501 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5502 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5503 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5504 rc, shdr_status, shdr_add_status);
5505 rc = -ENXIO;
5506 goto out_free_mboxq;
5507 }
5508 switch (phba->sli4_hba.lnk_info.lnk_no) {
5509 case LPFC_LINK_NUMBER_0:
5510 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5511 &get_port_name->u.response);
5512 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5513 break;
5514 case LPFC_LINK_NUMBER_1:
5515 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5516 &get_port_name->u.response);
5517 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5518 break;
5519 case LPFC_LINK_NUMBER_2:
5520 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5521 &get_port_name->u.response);
5522 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5523 break;
5524 case LPFC_LINK_NUMBER_3:
5525 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5526 &get_port_name->u.response);
5527 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5528 break;
5529 default:
5530 break;
5531 }
5532
5533 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5534 phba->Port[0] = cport_name;
5535 phba->Port[1] = '\0';
5536 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5537 "3091 SLI get port name: %s\n", phba->Port);
5538 }
5539
5540out_free_mboxq:
5541 if (rc != MBX_TIMEOUT) {
5542 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5543 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5544 else
5545 mempool_free(mboxq, phba->mbox_mem_pool);
5546 }
5547 return rc;
5548}
5549
e59058c4 5550/**
da0436e9
JS
5551 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5552 * @phba: pointer to lpfc hba data structure.
e59058c4 5553 *
da0436e9
JS
5554 * This routine is called to explicitly arm the SLI4 device's completion and
5555 * event queues
5556 **/
5557static void
5558lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5559{
895427bd 5560 int qidx;
b71413dd 5561 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
da0436e9 5562
b71413dd
JS
5563 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5564 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5565 if (sli4_hba->nvmels_cq)
5566 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
895427bd
JS
5567 LPFC_QUEUE_REARM);
5568
b71413dd 5569 if (sli4_hba->fcp_cq)
895427bd 5570 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
b71413dd 5571 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
895427bd
JS
5572 LPFC_QUEUE_REARM);
5573
b71413dd 5574 if (sli4_hba->nvme_cq)
895427bd 5575 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
b71413dd 5576 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
895427bd 5577 LPFC_QUEUE_REARM);
1ba981fd 5578
f38fa0bb 5579 if (phba->cfg_fof)
b71413dd 5580 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
1ba981fd 5581
b71413dd 5582 if (sli4_hba->hba_eq)
895427bd 5583 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
b71413dd
JS
5584 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5585 LPFC_QUEUE_REARM);
1ba981fd 5586
2d7dbc4c
JS
5587 if (phba->nvmet_support) {
5588 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
b71413dd
JS
5589 sli4_hba->sli4_cq_release(
5590 sli4_hba->nvmet_cqset[qidx],
2d7dbc4c
JS
5591 LPFC_QUEUE_REARM);
5592 }
2e90f4b5 5593 }
1ba981fd
JS
5594
5595 if (phba->cfg_fof)
b71413dd 5596 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5597}
5598
6d368e53
JS
5599/**
5600 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5601 * @phba: Pointer to HBA context object.
5602 * @type: The resource extent type.
b76f2dc9
JS
5603 * @extnt_count: buffer to hold port available extent count.
5604 * @extnt_size: buffer to hold element count per extent.
6d368e53 5605 *
b76f2dc9
JS
5606 * This function calls the port and retrievs the number of available
5607 * extents and their size for a particular extent type.
5608 *
5609 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5610 **/
b76f2dc9 5611int
6d368e53
JS
5612lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5613 uint16_t *extnt_count, uint16_t *extnt_size)
5614{
5615 int rc = 0;
5616 uint32_t length;
5617 uint32_t mbox_tmo;
5618 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5619 LPFC_MBOXQ_t *mbox;
5620
5621 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5622 if (!mbox)
5623 return -ENOMEM;
5624
5625 /* Find out how many extents are available for this resource type */
5626 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5627 sizeof(struct lpfc_sli4_cfg_mhdr));
5628 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5629 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5630 length, LPFC_SLI4_MBX_EMBED);
5631
5632 /* Send an extents count of 0 - the GET doesn't use it. */
5633 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5634 LPFC_SLI4_MBX_EMBED);
5635 if (unlikely(rc)) {
5636 rc = -EIO;
5637 goto err_exit;
5638 }
5639
5640 if (!phba->sli4_hba.intr_enable)
5641 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5642 else {
a183a15f 5643 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5644 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5645 }
5646 if (unlikely(rc)) {
5647 rc = -EIO;
5648 goto err_exit;
5649 }
5650
5651 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5652 if (bf_get(lpfc_mbox_hdr_status,
5653 &rsrc_info->header.cfg_shdr.response)) {
5654 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5655 "2930 Failed to get resource extents "
5656 "Status 0x%x Add'l Status 0x%x\n",
5657 bf_get(lpfc_mbox_hdr_status,
5658 &rsrc_info->header.cfg_shdr.response),
5659 bf_get(lpfc_mbox_hdr_add_status,
5660 &rsrc_info->header.cfg_shdr.response));
5661 rc = -EIO;
5662 goto err_exit;
5663 }
5664
5665 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5666 &rsrc_info->u.rsp);
5667 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5668 &rsrc_info->u.rsp);
8a9d2e80
JS
5669
5670 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5671 "3162 Retrieved extents type-%d from port: count:%d, "
5672 "size:%d\n", type, *extnt_count, *extnt_size);
5673
5674err_exit:
6d368e53
JS
5675 mempool_free(mbox, phba->mbox_mem_pool);
5676 return rc;
5677}
5678
5679/**
5680 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5681 * @phba: Pointer to HBA context object.
5682 * @type: The extent type to check.
5683 *
5684 * This function reads the current available extents from the port and checks
5685 * if the extent count or extent size has changed since the last access.
5686 * Callers use this routine post port reset to understand if there is a
5687 * extent reprovisioning requirement.
5688 *
5689 * Returns:
5690 * -Error: error indicates problem.
5691 * 1: Extent count or size has changed.
5692 * 0: No changes.
5693 **/
5694static int
5695lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5696{
5697 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5698 uint16_t size_diff, rsrc_ext_size;
5699 int rc = 0;
5700 struct lpfc_rsrc_blks *rsrc_entry;
5701 struct list_head *rsrc_blk_list = NULL;
5702
5703 size_diff = 0;
5704 curr_ext_cnt = 0;
5705 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5706 &rsrc_ext_cnt,
5707 &rsrc_ext_size);
5708 if (unlikely(rc))
5709 return -EIO;
5710
5711 switch (type) {
5712 case LPFC_RSC_TYPE_FCOE_RPI:
5713 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5714 break;
5715 case LPFC_RSC_TYPE_FCOE_VPI:
5716 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5717 break;
5718 case LPFC_RSC_TYPE_FCOE_XRI:
5719 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5720 break;
5721 case LPFC_RSC_TYPE_FCOE_VFI:
5722 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5723 break;
5724 default:
5725 break;
5726 }
5727
5728 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5729 curr_ext_cnt++;
5730 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5731 size_diff++;
5732 }
5733
5734 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5735 rc = 1;
5736
5737 return rc;
5738}
5739
5740/**
5741 * lpfc_sli4_cfg_post_extnts -
5742 * @phba: Pointer to HBA context object.
5743 * @extnt_cnt - number of available extents.
5744 * @type - the extent type (rpi, xri, vfi, vpi).
5745 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5746 * @mbox - pointer to the caller's allocated mailbox structure.
5747 *
5748 * This function executes the extents allocation request. It also
5749 * takes care of the amount of memory needed to allocate or get the
5750 * allocated extents. It is the caller's responsibility to evaluate
5751 * the response.
5752 *
5753 * Returns:
5754 * -Error: Error value describes the condition found.
5755 * 0: if successful
5756 **/
5757static int
8a9d2e80 5758lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5759 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5760{
5761 int rc = 0;
5762 uint32_t req_len;
5763 uint32_t emb_len;
5764 uint32_t alloc_len, mbox_tmo;
5765
5766 /* Calculate the total requested length of the dma memory */
8a9d2e80 5767 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5768
5769 /*
5770 * Calculate the size of an embedded mailbox. The uint32_t
5771 * accounts for extents-specific word.
5772 */
5773 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5774 sizeof(uint32_t);
5775
5776 /*
5777 * Presume the allocation and response will fit into an embedded
5778 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5779 */
5780 *emb = LPFC_SLI4_MBX_EMBED;
5781 if (req_len > emb_len) {
8a9d2e80 5782 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5783 sizeof(union lpfc_sli4_cfg_shdr) +
5784 sizeof(uint32_t);
5785 *emb = LPFC_SLI4_MBX_NEMBED;
5786 }
5787
5788 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5789 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5790 req_len, *emb);
5791 if (alloc_len < req_len) {
5792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5793 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5794 "less than the requested DMA memory "
5795 "size (x%x)\n", alloc_len, req_len);
5796 return -ENOMEM;
5797 }
8a9d2e80 5798 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5799 if (unlikely(rc))
5800 return -EIO;
5801
5802 if (!phba->sli4_hba.intr_enable)
5803 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5804 else {
a183a15f 5805 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5806 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5807 }
5808
5809 if (unlikely(rc))
5810 rc = -EIO;
5811 return rc;
5812}
5813
5814/**
5815 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5816 * @phba: Pointer to HBA context object.
5817 * @type: The resource extent type to allocate.
5818 *
5819 * This function allocates the number of elements for the specified
5820 * resource type.
5821 **/
5822static int
5823lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5824{
5825 bool emb = false;
5826 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5827 uint16_t rsrc_id, rsrc_start, j, k;
5828 uint16_t *ids;
5829 int i, rc;
5830 unsigned long longs;
5831 unsigned long *bmask;
5832 struct lpfc_rsrc_blks *rsrc_blks;
5833 LPFC_MBOXQ_t *mbox;
5834 uint32_t length;
5835 struct lpfc_id_range *id_array = NULL;
5836 void *virtaddr = NULL;
5837 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5838 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5839 struct list_head *ext_blk_list;
5840
5841 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5842 &rsrc_cnt,
5843 &rsrc_size);
5844 if (unlikely(rc))
5845 return -EIO;
5846
5847 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5849 "3009 No available Resource Extents "
5850 "for resource type 0x%x: Count: 0x%x, "
5851 "Size 0x%x\n", type, rsrc_cnt,
5852 rsrc_size);
5853 return -ENOMEM;
5854 }
5855
8a9d2e80
JS
5856 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5857 "2903 Post resource extents type-0x%x: "
5858 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5859
5860 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5861 if (!mbox)
5862 return -ENOMEM;
5863
8a9d2e80 5864 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5865 if (unlikely(rc)) {
5866 rc = -EIO;
5867 goto err_exit;
5868 }
5869
5870 /*
5871 * Figure out where the response is located. Then get local pointers
5872 * to the response data. The port does not guarantee to respond to
5873 * all extents counts request so update the local variable with the
5874 * allocated count from the port.
5875 */
5876 if (emb == LPFC_SLI4_MBX_EMBED) {
5877 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5878 id_array = &rsrc_ext->u.rsp.id[0];
5879 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5880 } else {
5881 virtaddr = mbox->sge_array->addr[0];
5882 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5883 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5884 id_array = &n_rsrc->id;
5885 }
5886
5887 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5888 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5889
5890 /*
5891 * Based on the resource size and count, correct the base and max
5892 * resource values.
5893 */
5894 length = sizeof(struct lpfc_rsrc_blks);
5895 switch (type) {
5896 case LPFC_RSC_TYPE_FCOE_RPI:
6396bb22 5897 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6d368e53
JS
5898 sizeof(unsigned long),
5899 GFP_KERNEL);
5900 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5901 rc = -ENOMEM;
5902 goto err_exit;
5903 }
6396bb22 5904 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
6d368e53
JS
5905 sizeof(uint16_t),
5906 GFP_KERNEL);
5907 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5908 kfree(phba->sli4_hba.rpi_bmask);
5909 rc = -ENOMEM;
5910 goto err_exit;
5911 }
5912
5913 /*
5914 * The next_rpi was initialized with the maximum available
5915 * count but the port may allocate a smaller number. Catch
5916 * that case and update the next_rpi.
5917 */
5918 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5919
5920 /* Initialize local ptrs for common extent processing later. */
5921 bmask = phba->sli4_hba.rpi_bmask;
5922 ids = phba->sli4_hba.rpi_ids;
5923 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5924 break;
5925 case LPFC_RSC_TYPE_FCOE_VPI:
6396bb22 5926 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6d368e53
JS
5927 GFP_KERNEL);
5928 if (unlikely(!phba->vpi_bmask)) {
5929 rc = -ENOMEM;
5930 goto err_exit;
5931 }
6396bb22 5932 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
6d368e53
JS
5933 GFP_KERNEL);
5934 if (unlikely(!phba->vpi_ids)) {
5935 kfree(phba->vpi_bmask);
5936 rc = -ENOMEM;
5937 goto err_exit;
5938 }
5939
5940 /* Initialize local ptrs for common extent processing later. */
5941 bmask = phba->vpi_bmask;
5942 ids = phba->vpi_ids;
5943 ext_blk_list = &phba->lpfc_vpi_blk_list;
5944 break;
5945 case LPFC_RSC_TYPE_FCOE_XRI:
6396bb22 5946 phba->sli4_hba.xri_bmask = kcalloc(longs,
6d368e53
JS
5947 sizeof(unsigned long),
5948 GFP_KERNEL);
5949 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5950 rc = -ENOMEM;
5951 goto err_exit;
5952 }
8a9d2e80 5953 phba->sli4_hba.max_cfg_param.xri_used = 0;
6396bb22 5954 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
6d368e53
JS
5955 sizeof(uint16_t),
5956 GFP_KERNEL);
5957 if (unlikely(!phba->sli4_hba.xri_ids)) {
5958 kfree(phba->sli4_hba.xri_bmask);
5959 rc = -ENOMEM;
5960 goto err_exit;
5961 }
5962
5963 /* Initialize local ptrs for common extent processing later. */
5964 bmask = phba->sli4_hba.xri_bmask;
5965 ids = phba->sli4_hba.xri_ids;
5966 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5967 break;
5968 case LPFC_RSC_TYPE_FCOE_VFI:
6396bb22 5969 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6d368e53
JS
5970 sizeof(unsigned long),
5971 GFP_KERNEL);
5972 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5973 rc = -ENOMEM;
5974 goto err_exit;
5975 }
6396bb22 5976 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
6d368e53
JS
5977 sizeof(uint16_t),
5978 GFP_KERNEL);
5979 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5980 kfree(phba->sli4_hba.vfi_bmask);
5981 rc = -ENOMEM;
5982 goto err_exit;
5983 }
5984
5985 /* Initialize local ptrs for common extent processing later. */
5986 bmask = phba->sli4_hba.vfi_bmask;
5987 ids = phba->sli4_hba.vfi_ids;
5988 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5989 break;
5990 default:
5991 /* Unsupported Opcode. Fail call. */
5992 id_array = NULL;
5993 bmask = NULL;
5994 ids = NULL;
5995 ext_blk_list = NULL;
5996 goto err_exit;
5997 }
5998
5999 /*
6000 * Complete initializing the extent configuration with the
6001 * allocated ids assigned to this function. The bitmask serves
6002 * as an index into the array and manages the available ids. The
6003 * array just stores the ids communicated to the port via the wqes.
6004 */
6005 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6006 if ((i % 2) == 0)
6007 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6008 &id_array[k]);
6009 else
6010 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6011 &id_array[k]);
6012
6013 rsrc_blks = kzalloc(length, GFP_KERNEL);
6014 if (unlikely(!rsrc_blks)) {
6015 rc = -ENOMEM;
6016 kfree(bmask);
6017 kfree(ids);
6018 goto err_exit;
6019 }
6020 rsrc_blks->rsrc_start = rsrc_id;
6021 rsrc_blks->rsrc_size = rsrc_size;
6022 list_add_tail(&rsrc_blks->list, ext_blk_list);
6023 rsrc_start = rsrc_id;
895427bd 6024 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 6025 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
6026 lpfc_sli4_get_iocb_cnt(phba);
6027 phba->sli4_hba.nvme_xri_start =
6028 phba->sli4_hba.scsi_xri_start +
6029 phba->sli4_hba.scsi_xri_max;
6030 }
6d368e53
JS
6031
6032 while (rsrc_id < (rsrc_start + rsrc_size)) {
6033 ids[j] = rsrc_id;
6034 rsrc_id++;
6035 j++;
6036 }
6037 /* Entire word processed. Get next word.*/
6038 if ((i % 2) == 1)
6039 k++;
6040 }
6041 err_exit:
6042 lpfc_sli4_mbox_cmd_free(phba, mbox);
6043 return rc;
6044}
6045
895427bd
JS
6046
6047
6d368e53
JS
6048/**
6049 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6050 * @phba: Pointer to HBA context object.
6051 * @type: the extent's type.
6052 *
6053 * This function deallocates all extents of a particular resource type.
6054 * SLI4 does not allow for deallocating a particular extent range. It
6055 * is the caller's responsibility to release all kernel memory resources.
6056 **/
6057static int
6058lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6059{
6060 int rc;
6061 uint32_t length, mbox_tmo = 0;
6062 LPFC_MBOXQ_t *mbox;
6063 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6064 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6065
6066 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6067 if (!mbox)
6068 return -ENOMEM;
6069
6070 /*
6071 * This function sends an embedded mailbox because it only sends the
6072 * the resource type. All extents of this type are released by the
6073 * port.
6074 */
6075 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6076 sizeof(struct lpfc_sli4_cfg_mhdr));
6077 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6078 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6079 length, LPFC_SLI4_MBX_EMBED);
6080
6081 /* Send an extents count of 0 - the dealloc doesn't use it. */
6082 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6083 LPFC_SLI4_MBX_EMBED);
6084 if (unlikely(rc)) {
6085 rc = -EIO;
6086 goto out_free_mbox;
6087 }
6088 if (!phba->sli4_hba.intr_enable)
6089 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6090 else {
a183a15f 6091 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
6092 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6093 }
6094 if (unlikely(rc)) {
6095 rc = -EIO;
6096 goto out_free_mbox;
6097 }
6098
6099 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6100 if (bf_get(lpfc_mbox_hdr_status,
6101 &dealloc_rsrc->header.cfg_shdr.response)) {
6102 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6103 "2919 Failed to release resource extents "
6104 "for type %d - Status 0x%x Add'l Status 0x%x. "
6105 "Resource memory not released.\n",
6106 type,
6107 bf_get(lpfc_mbox_hdr_status,
6108 &dealloc_rsrc->header.cfg_shdr.response),
6109 bf_get(lpfc_mbox_hdr_add_status,
6110 &dealloc_rsrc->header.cfg_shdr.response));
6111 rc = -EIO;
6112 goto out_free_mbox;
6113 }
6114
6115 /* Release kernel memory resources for the specific type. */
6116 switch (type) {
6117 case LPFC_RSC_TYPE_FCOE_VPI:
6118 kfree(phba->vpi_bmask);
6119 kfree(phba->vpi_ids);
6120 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6121 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6122 &phba->lpfc_vpi_blk_list, list) {
6123 list_del_init(&rsrc_blk->list);
6124 kfree(rsrc_blk);
6125 }
16a3a208 6126 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6127 break;
6128 case LPFC_RSC_TYPE_FCOE_XRI:
6129 kfree(phba->sli4_hba.xri_bmask);
6130 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6131 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6132 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6133 list_del_init(&rsrc_blk->list);
6134 kfree(rsrc_blk);
6135 }
6136 break;
6137 case LPFC_RSC_TYPE_FCOE_VFI:
6138 kfree(phba->sli4_hba.vfi_bmask);
6139 kfree(phba->sli4_hba.vfi_ids);
6140 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6141 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6142 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6143 list_del_init(&rsrc_blk->list);
6144 kfree(rsrc_blk);
6145 }
6146 break;
6147 case LPFC_RSC_TYPE_FCOE_RPI:
6148 /* RPI bitmask and physical id array are cleaned up earlier. */
6149 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6150 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6151 list_del_init(&rsrc_blk->list);
6152 kfree(rsrc_blk);
6153 }
6154 break;
6155 default:
6156 break;
6157 }
6158
6159 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6160
6161 out_free_mbox:
6162 mempool_free(mbox, phba->mbox_mem_pool);
6163 return rc;
6164}
6165
bd4b3e5c 6166static void
7bdedb34
JS
6167lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6168 uint32_t feature)
65791f1f 6169{
65791f1f 6170 uint32_t len;
65791f1f 6171
65791f1f
JS
6172 len = sizeof(struct lpfc_mbx_set_feature) -
6173 sizeof(struct lpfc_sli4_cfg_mhdr);
6174 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6175 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6176 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
6177
6178 switch (feature) {
6179 case LPFC_SET_UE_RECOVERY:
6180 bf_set(lpfc_mbx_set_feature_UER,
6181 &mbox->u.mqe.un.set_feature, 1);
6182 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6183 mbox->u.mqe.un.set_feature.param_len = 8;
6184 break;
6185 case LPFC_SET_MDS_DIAGS:
6186 bf_set(lpfc_mbx_set_feature_mds,
6187 &mbox->u.mqe.un.set_feature, 1);
6188 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 6189 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
6190 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6191 mbox->u.mqe.un.set_feature.param_len = 8;
6192 break;
65791f1f 6193 }
7bdedb34
JS
6194
6195 return;
65791f1f
JS
6196}
6197
d2cc9bcd
JS
6198/**
6199 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6200 * @phba: Pointer to HBA context object.
6201 *
6202 * This function is called to free memory allocated for RAS FW logging
6203 * support in the driver.
6204 **/
6205void
6206lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6207{
6208 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6209 struct lpfc_dmabuf *dmabuf, *next;
6210
6211 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6212 list_for_each_entry_safe(dmabuf, next,
6213 &ras_fwlog->fwlog_buff_list,
6214 list) {
6215 list_del(&dmabuf->list);
6216 dma_free_coherent(&phba->pcidev->dev,
6217 LPFC_RAS_MAX_ENTRY_SIZE,
6218 dmabuf->virt, dmabuf->phys);
6219 kfree(dmabuf);
6220 }
6221 }
6222
6223 if (ras_fwlog->lwpd.virt) {
6224 dma_free_coherent(&phba->pcidev->dev,
6225 sizeof(uint32_t) * 2,
6226 ras_fwlog->lwpd.virt,
6227 ras_fwlog->lwpd.phys);
6228 ras_fwlog->lwpd.virt = NULL;
6229 }
6230
6231 ras_fwlog->ras_active = false;
6232}
6233
6234/**
6235 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6236 * @phba: Pointer to HBA context object.
6237 * @fwlog_buff_count: Count of buffers to be created.
6238 *
6239 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6240 * to update FW log is posted to the adapter.
6241 * Buffer count is calculated based on module param ras_fwlog_buffsize
6242 * Size of each buffer posted to FW is 64K.
6243 **/
6244
6245static int
6246lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6247 uint32_t fwlog_buff_count)
6248{
6249 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6250 struct lpfc_dmabuf *dmabuf;
6251 int rc = 0, i = 0;
6252
6253 /* Initialize List */
6254 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6255
6256 /* Allocate memory for the LWPD */
6257 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6258 sizeof(uint32_t) * 2,
6259 &ras_fwlog->lwpd.phys,
6260 GFP_KERNEL);
6261 if (!ras_fwlog->lwpd.virt) {
cb34990b 6262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d2cc9bcd
JS
6263 "6185 LWPD Memory Alloc Failed\n");
6264
6265 return -ENOMEM;
6266 }
6267
6268 ras_fwlog->fw_buffcount = fwlog_buff_count;
6269 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6270 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6271 GFP_KERNEL);
6272 if (!dmabuf) {
6273 rc = -ENOMEM;
6274 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6275 "6186 Memory Alloc failed FW logging");
6276 goto free_mem;
6277 }
6278
359d0ac1 6279 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
d2cc9bcd
JS
6280 LPFC_RAS_MAX_ENTRY_SIZE,
6281 &dmabuf->phys,
6282 GFP_KERNEL);
6283 if (!dmabuf->virt) {
6284 kfree(dmabuf);
6285 rc = -ENOMEM;
6286 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6287 "6187 DMA Alloc Failed FW logging");
6288 goto free_mem;
6289 }
d2cc9bcd
JS
6290 dmabuf->buffer_tag = i;
6291 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6292 }
6293
6294free_mem:
6295 if (rc)
6296 lpfc_sli4_ras_dma_free(phba);
6297
6298 return rc;
6299}
6300
6301/**
6302 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6303 * @phba: pointer to lpfc hba data structure.
6304 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6305 *
6306 * Completion handler for driver's RAS MBX command to the device.
6307 **/
6308static void
6309lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6310{
6311 MAILBOX_t *mb;
6312 union lpfc_sli4_cfg_shdr *shdr;
6313 uint32_t shdr_status, shdr_add_status;
6314 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6315
6316 mb = &pmb->u.mb;
6317
6318 shdr = (union lpfc_sli4_cfg_shdr *)
6319 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6320 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6321 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6322
6323 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
cb34990b 6324 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
d2cc9bcd
JS
6325 "6188 FW LOG mailbox "
6326 "completed with status x%x add_status x%x,"
6327 " mbx status x%x\n",
6328 shdr_status, shdr_add_status, mb->mbxStatus);
cb34990b
JS
6329
6330 ras_fwlog->ras_hwsupport = false;
d2cc9bcd
JS
6331 goto disable_ras;
6332 }
6333
6334 ras_fwlog->ras_active = true;
6335 mempool_free(pmb, phba->mbox_mem_pool);
6336
6337 return;
6338
6339disable_ras:
6340 /* Free RAS DMA memory */
6341 lpfc_sli4_ras_dma_free(phba);
6342 mempool_free(pmb, phba->mbox_mem_pool);
6343}
6344
6345/**
6346 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6347 * @phba: pointer to lpfc hba data structure.
6348 * @fwlog_level: Logging verbosity level.
6349 * @fwlog_enable: Enable/Disable logging.
6350 *
6351 * Initialize memory and post mailbox command to enable FW logging in host
6352 * memory.
6353 **/
6354int
6355lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6356 uint32_t fwlog_level,
6357 uint32_t fwlog_enable)
6358{
6359 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6360 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6361 struct lpfc_dmabuf *dmabuf;
6362 LPFC_MBOXQ_t *mbox;
6363 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6364 int rc = 0;
6365
6366 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6367 phba->cfg_ras_fwlog_buffsize);
6368 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6369
6370 /*
6371 * If re-enabling FW logging support use earlier allocated
6372 * DMA buffers while posting MBX command.
6373 **/
6374 if (!ras_fwlog->lwpd.virt) {
6375 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6376 if (rc) {
6377 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
cb34990b 6378 "6189 FW Log Memory Allocation Failed");
d2cc9bcd
JS
6379 return rc;
6380 }
6381 }
6382
6383 /* Setup Mailbox command */
6384 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6385 if (!mbox) {
cb34990b 6386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d2cc9bcd
JS
6387 "6190 RAS MBX Alloc Failed");
6388 rc = -ENOMEM;
6389 goto mem_free;
6390 }
6391
6392 ras_fwlog->fw_loglevel = fwlog_level;
6393 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6394 sizeof(struct lpfc_sli4_cfg_mhdr));
6395
6396 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6397 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6398 len, LPFC_SLI4_MBX_EMBED);
6399
6400 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6401 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6402 fwlog_enable);
6403 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6404 ras_fwlog->fw_loglevel);
6405 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6406 ras_fwlog->fw_buffcount);
6407 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6408 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6409
6410 /* Update DMA buffer address */
6411 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6412 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6413
6414 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6415 putPaddrLow(dmabuf->phys);
6416
6417 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6418 putPaddrHigh(dmabuf->phys);
6419 }
6420
6421 /* Update LPWD address */
6422 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6423 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6424
6425 mbox->vport = phba->pport;
6426 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6427
6428 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6429
6430 if (rc == MBX_NOT_FINISHED) {
cb34990b
JS
6431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6432 "6191 FW-Log Mailbox failed. "
d2cc9bcd
JS
6433 "status %d mbxStatus : x%x", rc,
6434 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6435 mempool_free(mbox, phba->mbox_mem_pool);
6436 rc = -EIO;
6437 goto mem_free;
6438 } else
6439 rc = 0;
6440mem_free:
6441 if (rc)
6442 lpfc_sli4_ras_dma_free(phba);
6443
6444 return rc;
6445}
6446
6447/**
6448 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6449 * @phba: Pointer to HBA context object.
6450 *
6451 * Check if RAS is supported on the adapter and initialize it.
6452 **/
6453void
6454lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6455{
6456 /* Check RAS FW Log needs to be enabled or not */
6457 if (lpfc_check_fwlog_support(phba))
6458 return;
6459
6460 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6461 LPFC_RAS_ENABLE_LOGGING);
6462}
6463
6d368e53
JS
6464/**
6465 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6466 * @phba: Pointer to HBA context object.
6467 *
6468 * This function allocates all SLI4 resource identifiers.
6469 **/
6470int
6471lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6472{
6473 int i, rc, error = 0;
6474 uint16_t count, base;
6475 unsigned long longs;
6476
ff78d8f9
JS
6477 if (!phba->sli4_hba.rpi_hdrs_in_use)
6478 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
6479 if (phba->sli4_hba.extents_in_use) {
6480 /*
6481 * The port supports resource extents. The XRI, VPI, VFI, RPI
6482 * resource extent count must be read and allocated before
6483 * provisioning the resource id arrays.
6484 */
6485 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6486 LPFC_IDX_RSRC_RDY) {
6487 /*
6488 * Extent-based resources are set - the driver could
6489 * be in a port reset. Figure out if any corrective
6490 * actions need to be taken.
6491 */
6492 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6493 LPFC_RSC_TYPE_FCOE_VFI);
6494 if (rc != 0)
6495 error++;
6496 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6497 LPFC_RSC_TYPE_FCOE_VPI);
6498 if (rc != 0)
6499 error++;
6500 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6501 LPFC_RSC_TYPE_FCOE_XRI);
6502 if (rc != 0)
6503 error++;
6504 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6505 LPFC_RSC_TYPE_FCOE_RPI);
6506 if (rc != 0)
6507 error++;
6508
6509 /*
6510 * It's possible that the number of resources
6511 * provided to this port instance changed between
6512 * resets. Detect this condition and reallocate
6513 * resources. Otherwise, there is no action.
6514 */
6515 if (error) {
6516 lpfc_printf_log(phba, KERN_INFO,
6517 LOG_MBOX | LOG_INIT,
6518 "2931 Detected extent resource "
6519 "change. Reallocating all "
6520 "extents.\n");
6521 rc = lpfc_sli4_dealloc_extent(phba,
6522 LPFC_RSC_TYPE_FCOE_VFI);
6523 rc = lpfc_sli4_dealloc_extent(phba,
6524 LPFC_RSC_TYPE_FCOE_VPI);
6525 rc = lpfc_sli4_dealloc_extent(phba,
6526 LPFC_RSC_TYPE_FCOE_XRI);
6527 rc = lpfc_sli4_dealloc_extent(phba,
6528 LPFC_RSC_TYPE_FCOE_RPI);
6529 } else
6530 return 0;
6531 }
6532
6533 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6534 if (unlikely(rc))
6535 goto err_exit;
6536
6537 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6538 if (unlikely(rc))
6539 goto err_exit;
6540
6541 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6542 if (unlikely(rc))
6543 goto err_exit;
6544
6545 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6546 if (unlikely(rc))
6547 goto err_exit;
6548 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6549 LPFC_IDX_RSRC_RDY);
6550 return rc;
6551 } else {
6552 /*
6553 * The port does not support resource extents. The XRI, VPI,
6554 * VFI, RPI resource ids were determined from READ_CONFIG.
6555 * Just allocate the bitmasks and provision the resource id
6556 * arrays. If a port reset is active, the resources don't
6557 * need any action - just exit.
6558 */
6559 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6560 LPFC_IDX_RSRC_RDY) {
6561 lpfc_sli4_dealloc_resource_identifiers(phba);
6562 lpfc_sli4_remove_rpis(phba);
6563 }
6d368e53
JS
6564 /* RPIs. */
6565 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6566 if (count <= 0) {
6567 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6568 "3279 Invalid provisioning of "
6569 "rpi:%d\n", count);
6570 rc = -EINVAL;
6571 goto err_exit;
6572 }
6d368e53
JS
6573 base = phba->sli4_hba.max_cfg_param.rpi_base;
6574 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6396bb22 6575 phba->sli4_hba.rpi_bmask = kcalloc(longs,
6d368e53
JS
6576 sizeof(unsigned long),
6577 GFP_KERNEL);
6578 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6579 rc = -ENOMEM;
6580 goto err_exit;
6581 }
6396bb22 6582 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
6d368e53
JS
6583 GFP_KERNEL);
6584 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6585 rc = -ENOMEM;
6586 goto free_rpi_bmask;
6587 }
6588
6589 for (i = 0; i < count; i++)
6590 phba->sli4_hba.rpi_ids[i] = base + i;
6591
6592 /* VPIs. */
6593 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6594 if (count <= 0) {
6595 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6596 "3280 Invalid provisioning of "
6597 "vpi:%d\n", count);
6598 rc = -EINVAL;
6599 goto free_rpi_ids;
6600 }
6d368e53
JS
6601 base = phba->sli4_hba.max_cfg_param.vpi_base;
6602 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6396bb22 6603 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
6d368e53
JS
6604 GFP_KERNEL);
6605 if (unlikely(!phba->vpi_bmask)) {
6606 rc = -ENOMEM;
6607 goto free_rpi_ids;
6608 }
6396bb22 6609 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
6d368e53
JS
6610 GFP_KERNEL);
6611 if (unlikely(!phba->vpi_ids)) {
6612 rc = -ENOMEM;
6613 goto free_vpi_bmask;
6614 }
6615
6616 for (i = 0; i < count; i++)
6617 phba->vpi_ids[i] = base + i;
6618
6619 /* XRIs. */
6620 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6621 if (count <= 0) {
6622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6623 "3281 Invalid provisioning of "
6624 "xri:%d\n", count);
6625 rc = -EINVAL;
6626 goto free_vpi_ids;
6627 }
6d368e53
JS
6628 base = phba->sli4_hba.max_cfg_param.xri_base;
6629 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6396bb22 6630 phba->sli4_hba.xri_bmask = kcalloc(longs,
6d368e53
JS
6631 sizeof(unsigned long),
6632 GFP_KERNEL);
6633 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6634 rc = -ENOMEM;
6635 goto free_vpi_ids;
6636 }
41899be7 6637 phba->sli4_hba.max_cfg_param.xri_used = 0;
6396bb22 6638 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
6d368e53
JS
6639 GFP_KERNEL);
6640 if (unlikely(!phba->sli4_hba.xri_ids)) {
6641 rc = -ENOMEM;
6642 goto free_xri_bmask;
6643 }
6644
6645 for (i = 0; i < count; i++)
6646 phba->sli4_hba.xri_ids[i] = base + i;
6647
6648 /* VFIs. */
6649 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6650 if (count <= 0) {
6651 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6652 "3282 Invalid provisioning of "
6653 "vfi:%d\n", count);
6654 rc = -EINVAL;
6655 goto free_xri_ids;
6656 }
6d368e53
JS
6657 base = phba->sli4_hba.max_cfg_param.vfi_base;
6658 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6396bb22 6659 phba->sli4_hba.vfi_bmask = kcalloc(longs,
6d368e53
JS
6660 sizeof(unsigned long),
6661 GFP_KERNEL);
6662 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6663 rc = -ENOMEM;
6664 goto free_xri_ids;
6665 }
6396bb22 6666 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
6d368e53
JS
6667 GFP_KERNEL);
6668 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6669 rc = -ENOMEM;
6670 goto free_vfi_bmask;
6671 }
6672
6673 for (i = 0; i < count; i++)
6674 phba->sli4_hba.vfi_ids[i] = base + i;
6675
6676 /*
6677 * Mark all resources ready. An HBA reset doesn't need
6678 * to reset the initialization.
6679 */
6680 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6681 LPFC_IDX_RSRC_RDY);
6682 return 0;
6683 }
6684
6685 free_vfi_bmask:
6686 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6687 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6688 free_xri_ids:
6689 kfree(phba->sli4_hba.xri_ids);
cd60be49 6690 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6691 free_xri_bmask:
6692 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6693 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6694 free_vpi_ids:
6695 kfree(phba->vpi_ids);
cd60be49 6696 phba->vpi_ids = NULL;
6d368e53
JS
6697 free_vpi_bmask:
6698 kfree(phba->vpi_bmask);
cd60be49 6699 phba->vpi_bmask = NULL;
6d368e53
JS
6700 free_rpi_ids:
6701 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6702 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6703 free_rpi_bmask:
6704 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6705 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6706 err_exit:
6707 return rc;
6708}
6709
6710/**
6711 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6712 * @phba: Pointer to HBA context object.
6713 *
6714 * This function allocates the number of elements for the specified
6715 * resource type.
6716 **/
6717int
6718lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6719{
6720 if (phba->sli4_hba.extents_in_use) {
6721 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6722 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6723 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6724 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6725 } else {
6726 kfree(phba->vpi_bmask);
16a3a208 6727 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6728 kfree(phba->vpi_ids);
6729 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6730 kfree(phba->sli4_hba.xri_bmask);
6731 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6732 kfree(phba->sli4_hba.vfi_bmask);
6733 kfree(phba->sli4_hba.vfi_ids);
6734 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6735 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6736 }
6737
6738 return 0;
6739}
6740
b76f2dc9
JS
6741/**
6742 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6743 * @phba: Pointer to HBA context object.
6744 * @type: The resource extent type.
6745 * @extnt_count: buffer to hold port extent count response
6746 * @extnt_size: buffer to hold port extent size response.
6747 *
6748 * This function calls the port to read the host allocated extents
6749 * for a particular type.
6750 **/
6751int
6752lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6753 uint16_t *extnt_cnt, uint16_t *extnt_size)
6754{
6755 bool emb;
6756 int rc = 0;
6757 uint16_t curr_blks = 0;
6758 uint32_t req_len, emb_len;
6759 uint32_t alloc_len, mbox_tmo;
6760 struct list_head *blk_list_head;
6761 struct lpfc_rsrc_blks *rsrc_blk;
6762 LPFC_MBOXQ_t *mbox;
6763 void *virtaddr = NULL;
6764 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6765 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6766 union lpfc_sli4_cfg_shdr *shdr;
6767
6768 switch (type) {
6769 case LPFC_RSC_TYPE_FCOE_VPI:
6770 blk_list_head = &phba->lpfc_vpi_blk_list;
6771 break;
6772 case LPFC_RSC_TYPE_FCOE_XRI:
6773 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6774 break;
6775 case LPFC_RSC_TYPE_FCOE_VFI:
6776 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6777 break;
6778 case LPFC_RSC_TYPE_FCOE_RPI:
6779 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6780 break;
6781 default:
6782 return -EIO;
6783 }
6784
6785 /* Count the number of extents currently allocatd for this type. */
6786 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6787 if (curr_blks == 0) {
6788 /*
6789 * The GET_ALLOCATED mailbox does not return the size,
6790 * just the count. The size should be just the size
6791 * stored in the current allocated block and all sizes
6792 * for an extent type are the same so set the return
6793 * value now.
6794 */
6795 *extnt_size = rsrc_blk->rsrc_size;
6796 }
6797 curr_blks++;
6798 }
6799
b76f2dc9
JS
6800 /*
6801 * Calculate the size of an embedded mailbox. The uint32_t
6802 * accounts for extents-specific word.
6803 */
6804 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6805 sizeof(uint32_t);
6806
6807 /*
6808 * Presume the allocation and response will fit into an embedded
6809 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6810 */
6811 emb = LPFC_SLI4_MBX_EMBED;
6812 req_len = emb_len;
6813 if (req_len > emb_len) {
6814 req_len = curr_blks * sizeof(uint16_t) +
6815 sizeof(union lpfc_sli4_cfg_shdr) +
6816 sizeof(uint32_t);
6817 emb = LPFC_SLI4_MBX_NEMBED;
6818 }
6819
6820 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6821 if (!mbox)
6822 return -ENOMEM;
6823 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6824
6825 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6826 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6827 req_len, emb);
6828 if (alloc_len < req_len) {
6829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6830 "2983 Allocated DMA memory size (x%x) is "
6831 "less than the requested DMA memory "
6832 "size (x%x)\n", alloc_len, req_len);
6833 rc = -ENOMEM;
6834 goto err_exit;
6835 }
6836 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6837 if (unlikely(rc)) {
6838 rc = -EIO;
6839 goto err_exit;
6840 }
6841
6842 if (!phba->sli4_hba.intr_enable)
6843 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6844 else {
a183a15f 6845 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6846 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6847 }
6848
6849 if (unlikely(rc)) {
6850 rc = -EIO;
6851 goto err_exit;
6852 }
6853
6854 /*
6855 * Figure out where the response is located. Then get local pointers
6856 * to the response data. The port does not guarantee to respond to
6857 * all extents counts request so update the local variable with the
6858 * allocated count from the port.
6859 */
6860 if (emb == LPFC_SLI4_MBX_EMBED) {
6861 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6862 shdr = &rsrc_ext->header.cfg_shdr;
6863 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6864 } else {
6865 virtaddr = mbox->sge_array->addr[0];
6866 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6867 shdr = &n_rsrc->cfg_shdr;
6868 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6869 }
6870
6871 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6873 "2984 Failed to read allocated resources "
6874 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6875 type,
6876 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6877 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6878 rc = -EIO;
6879 goto err_exit;
6880 }
6881 err_exit:
6882 lpfc_sli4_mbox_cmd_free(phba, mbox);
6883 return rc;
6884}
6885
8a9d2e80 6886/**
0ef69968 6887 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6888 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6889 * @pring: Pointer to driver SLI ring object.
6890 * @sgl_list: linked link of sgl buffers to post
6891 * @cnt: number of linked list buffers
8a9d2e80 6892 *
895427bd 6893 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6894 * repost them to the port by using SGL block post. This is needed after a
6895 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6896 * of buffer sgls which contains contiguous xris and uses the non-embedded
6897 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6898 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6899 * mailbox command for posting.
6900 *
6901 * Returns: 0 = success, non-zero failure.
6902 **/
6903static int
895427bd
JS
6904lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6905 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6906{
6907 struct lpfc_sglq *sglq_entry = NULL;
6908 struct lpfc_sglq *sglq_entry_next = NULL;
6909 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6910 int status, total_cnt;
6911 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6912 int last_xritag = NO_XRI;
6913 LIST_HEAD(prep_sgl_list);
6914 LIST_HEAD(blck_sgl_list);
6915 LIST_HEAD(allc_sgl_list);
6916 LIST_HEAD(post_sgl_list);
6917 LIST_HEAD(free_sgl_list);
6918
38c20673 6919 spin_lock_irq(&phba->hbalock);
895427bd
JS
6920 spin_lock(&phba->sli4_hba.sgl_list_lock);
6921 list_splice_init(sgl_list, &allc_sgl_list);
6922 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6923 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6924
895427bd 6925 total_cnt = cnt;
8a9d2e80
JS
6926 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6927 &allc_sgl_list, list) {
6928 list_del_init(&sglq_entry->list);
6929 block_cnt++;
6930 if ((last_xritag != NO_XRI) &&
6931 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6932 /* a hole in xri block, form a sgl posting block */
6933 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6934 post_cnt = block_cnt - 1;
6935 /* prepare list for next posting block */
6936 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6937 block_cnt = 1;
6938 } else {
6939 /* prepare list for next posting block */
6940 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6941 /* enough sgls for non-embed sgl mbox command */
6942 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6943 list_splice_init(&prep_sgl_list,
6944 &blck_sgl_list);
6945 post_cnt = block_cnt;
6946 block_cnt = 0;
6947 }
6948 }
6949 num_posted++;
6950
6951 /* keep track of last sgl's xritag */
6952 last_xritag = sglq_entry->sli4_xritag;
6953
895427bd
JS
6954 /* end of repost sgl list condition for buffers */
6955 if (num_posted == total_cnt) {
8a9d2e80
JS
6956 if (post_cnt == 0) {
6957 list_splice_init(&prep_sgl_list,
6958 &blck_sgl_list);
6959 post_cnt = block_cnt;
6960 } else if (block_cnt == 1) {
6961 status = lpfc_sli4_post_sgl(phba,
6962 sglq_entry->phys, 0,
6963 sglq_entry->sli4_xritag);
6964 if (!status) {
6965 /* successful, put sgl to posted list */
6966 list_add_tail(&sglq_entry->list,
6967 &post_sgl_list);
6968 } else {
6969 /* Failure, put sgl to free list */
6970 lpfc_printf_log(phba, KERN_WARNING,
6971 LOG_SLI,
895427bd 6972 "3159 Failed to post "
8a9d2e80
JS
6973 "sgl, xritag:x%x\n",
6974 sglq_entry->sli4_xritag);
6975 list_add_tail(&sglq_entry->list,
6976 &free_sgl_list);
711ea882 6977 total_cnt--;
8a9d2e80
JS
6978 }
6979 }
6980 }
6981
6982 /* continue until a nembed page worth of sgls */
6983 if (post_cnt == 0)
6984 continue;
6985
895427bd
JS
6986 /* post the buffer list sgls as a block */
6987 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6988 post_cnt);
8a9d2e80
JS
6989
6990 if (!status) {
6991 /* success, put sgl list to posted sgl list */
6992 list_splice_init(&blck_sgl_list, &post_sgl_list);
6993 } else {
6994 /* Failure, put sgl list to free sgl list */
6995 sglq_entry_first = list_first_entry(&blck_sgl_list,
6996 struct lpfc_sglq,
6997 list);
6998 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6999 "3160 Failed to post sgl-list, "
8a9d2e80
JS
7000 "xritag:x%x-x%x\n",
7001 sglq_entry_first->sli4_xritag,
7002 (sglq_entry_first->sli4_xritag +
7003 post_cnt - 1));
7004 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 7005 total_cnt -= post_cnt;
8a9d2e80
JS
7006 }
7007
7008 /* don't reset xirtag due to hole in xri block */
7009 if (block_cnt == 0)
7010 last_xritag = NO_XRI;
7011
895427bd 7012 /* reset sgl post count for next round of posting */
8a9d2e80
JS
7013 post_cnt = 0;
7014 }
7015
895427bd 7016 /* free the sgls failed to post */
8a9d2e80
JS
7017 lpfc_free_sgl_list(phba, &free_sgl_list);
7018
895427bd 7019 /* push sgls posted to the available list */
8a9d2e80 7020 if (!list_empty(&post_sgl_list)) {
38c20673 7021 spin_lock_irq(&phba->hbalock);
895427bd
JS
7022 spin_lock(&phba->sli4_hba.sgl_list_lock);
7023 list_splice_init(&post_sgl_list, sgl_list);
7024 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 7025 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
7026 } else {
7027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 7028 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
7029 return -EIO;
7030 }
895427bd
JS
7031
7032 /* return the number of XRIs actually posted */
7033 return total_cnt;
8a9d2e80
JS
7034}
7035
61bda8f7
JS
7036void
7037lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7038{
7039 uint32_t len;
7040
7041 len = sizeof(struct lpfc_mbx_set_host_data) -
7042 sizeof(struct lpfc_sli4_cfg_mhdr);
7043 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7044 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7045 LPFC_SLI4_MBX_EMBED);
7046
7047 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
7048 mbox->u.mqe.un.set_host_data.param_len =
7049 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
7050 snprintf(mbox->u.mqe.un.set_host_data.data,
7051 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7052 "Linux %s v"LPFC_DRIVER_VERSION,
7053 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7054}
7055
a8cf5dfe 7056int
6c621a22 7057lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 7058 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
7059{
7060 int rc, i;
7061 struct lpfc_rqe hrqe;
7062 struct lpfc_rqe drqe;
7063 struct lpfc_rqb *rqbp;
411de511 7064 unsigned long flags;
6c621a22
JS
7065 struct rqb_dmabuf *rqb_buffer;
7066 LIST_HEAD(rqb_buf_list);
7067
411de511 7068 spin_lock_irqsave(&phba->hbalock, flags);
6c621a22
JS
7069 rqbp = hrq->rqbp;
7070 for (i = 0; i < count; i++) {
7071 /* IF RQ is already full, don't bother */
7072 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7073 break;
7074 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7075 if (!rqb_buffer)
7076 break;
7077 rqb_buffer->hrq = hrq;
7078 rqb_buffer->drq = drq;
a8cf5dfe 7079 rqb_buffer->idx = idx;
6c621a22
JS
7080 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7081 }
7082 while (!list_empty(&rqb_buf_list)) {
7083 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7084 hbuf.list);
7085
7086 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7087 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7088 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7089 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7090 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7091 if (rc < 0) {
411de511
JS
7092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7093 "6421 Cannot post to HRQ %d: %x %x %x "
7094 "DRQ %x %x\n",
7095 hrq->queue_id,
7096 hrq->host_index,
7097 hrq->hba_index,
7098 hrq->entry_count,
7099 drq->host_index,
7100 drq->hba_index);
6c621a22
JS
7101 rqbp->rqb_free_buffer(phba, rqb_buffer);
7102 } else {
7103 list_add_tail(&rqb_buffer->hbuf.list,
7104 &rqbp->rqb_buffer_list);
7105 rqbp->buffer_count++;
7106 }
7107 }
411de511 7108 spin_unlock_irqrestore(&phba->hbalock, flags);
6c621a22
JS
7109 return 1;
7110}
7111
da0436e9 7112/**
183b8021 7113 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
7114 * @phba: Pointer to HBA context object.
7115 *
183b8021
MY
7116 * This function is the main SLI4 device initialization PCI function. This
7117 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
7118 * HBA error attention handler code. Caller is not required to hold any
7119 * locks.
7120 **/
7121int
7122lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7123{
6c621a22 7124 int rc, i, cnt;
da0436e9
JS
7125 LPFC_MBOXQ_t *mboxq;
7126 struct lpfc_mqe *mqe;
7127 uint8_t *vpd;
7128 uint32_t vpd_size;
7129 uint32_t ftr_rsp = 0;
7130 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7131 struct lpfc_vport *vport = phba->pport;
7132 struct lpfc_dmabuf *mp;
2d7dbc4c 7133 struct lpfc_rqb *rqbp;
da0436e9
JS
7134
7135 /* Perform a PCI function reset to start from clean */
7136 rc = lpfc_pci_function_reset(phba);
7137 if (unlikely(rc))
7138 return -ENODEV;
7139
7140 /* Check the HBA Host Status Register for readyness */
7141 rc = lpfc_sli4_post_status_check(phba);
7142 if (unlikely(rc))
7143 return -ENODEV;
7144 else {
7145 spin_lock_irq(&phba->hbalock);
7146 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7147 spin_unlock_irq(&phba->hbalock);
7148 }
7149
7150 /*
7151 * Allocate a single mailbox container for initializing the
7152 * port.
7153 */
7154 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7155 if (!mboxq)
7156 return -ENOMEM;
7157
da0436e9 7158 /* Issue READ_REV to collect vpd and FW information. */
49198b37 7159 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
7160 vpd = kzalloc(vpd_size, GFP_KERNEL);
7161 if (!vpd) {
7162 rc = -ENOMEM;
7163 goto out_free_mbox;
7164 }
7165
7166 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
7167 if (unlikely(rc)) {
7168 kfree(vpd);
7169 goto out_free_mbox;
7170 }
572709e2 7171
da0436e9 7172 mqe = &mboxq->u.mqe;
f1126688 7173 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 7174 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 7175 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
7176 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7177 } else {
76a95d75 7178 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 7179 }
45ed1190
JS
7180
7181 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7182 LPFC_DCBX_CEE_MODE)
7183 phba->hba_flag |= HBA_FIP_SUPPORT;
7184 else
7185 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7186
4f2e66c6
JS
7187 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
7188
c31098ce 7189 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
7190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7191 "0376 READ_REV Error. SLI Level %d "
7192 "FCoE enabled %d\n",
76a95d75 7193 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 7194 rc = -EIO;
76a95d75
JS
7195 kfree(vpd);
7196 goto out_free_mbox;
da0436e9 7197 }
cd1c8301 7198
ff78d8f9
JS
7199 /*
7200 * Continue initialization with default values even if driver failed
7201 * to read FCoE param config regions, only read parameters if the
7202 * board is FCoE
7203 */
7204 if (phba->hba_flag & HBA_FCOE_MODE &&
7205 lpfc_sli4_read_fcoe_params(phba))
7206 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7207 "2570 Failed to read FCoE parameters\n");
7208
cd1c8301
JS
7209 /*
7210 * Retrieve sli4 device physical port name, failure of doing it
7211 * is considered as non-fatal.
7212 */
7213 rc = lpfc_sli4_retrieve_pport_name(phba);
7214 if (!rc)
7215 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7216 "3080 Successful retrieving SLI4 device "
7217 "physical port name: %s.\n", phba->Port);
7218
da0436e9
JS
7219 /*
7220 * Evaluate the read rev and vpd data. Populate the driver
7221 * state with the results. If this routine fails, the failure
7222 * is not fatal as the driver will use generic values.
7223 */
7224 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7225 if (unlikely(!rc)) {
7226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7227 "0377 Error %d parsing vpd. "
7228 "Using defaults.\n", rc);
7229 rc = 0;
7230 }
76a95d75 7231 kfree(vpd);
da0436e9 7232
f1126688
JS
7233 /* Save information as VPD data */
7234 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7235 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4e565cf0
JS
7236
7237 /*
7238 * This is because first G7 ASIC doesn't support the standard
7239 * 0x5a NVME cmd descriptor type/subtype
7240 */
7241 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7242 LPFC_SLI_INTF_IF_TYPE_6) &&
7243 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7244 (phba->vpd.rev.smRev == 0) &&
7245 (phba->cfg_nvme_embed_cmd == 1))
7246 phba->cfg_nvme_embed_cmd = 0;
7247
f1126688
JS
7248 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7249 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7250 &mqe->un.read_rev);
7251 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7252 &mqe->un.read_rev);
7253 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7254 &mqe->un.read_rev);
7255 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7256 &mqe->un.read_rev);
7257 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7258 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7259 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7260 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7261 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7262 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7263 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7264 "(%d):0380 READ_REV Status x%x "
7265 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7266 mboxq->vport ? mboxq->vport->vpi : 0,
7267 bf_get(lpfc_mqe_status, mqe),
7268 phba->vpd.rev.opFwName,
7269 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7270 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 7271
572709e2
JS
7272 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
7273 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
7274 if (phba->pport->cfg_lun_queue_depth > rc) {
7275 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7276 "3362 LUN queue depth changed from %d to %d\n",
7277 phba->pport->cfg_lun_queue_depth, rc);
7278 phba->pport->cfg_lun_queue_depth = rc;
7279 }
7280
65791f1f 7281 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
7282 LPFC_SLI_INTF_IF_TYPE_0) {
7283 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7284 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7285 if (rc == MBX_SUCCESS) {
7286 phba->hba_flag |= HBA_RECOVERABLE_UE;
7287 /* Set 1Sec interval to detect UE */
7288 phba->eratt_poll_interval = 1;
7289 phba->sli4_hba.ue_to_sr = bf_get(
7290 lpfc_mbx_set_feature_UESR,
7291 &mboxq->u.mqe.un.set_feature);
7292 phba->sli4_hba.ue_to_rp = bf_get(
7293 lpfc_mbx_set_feature_UERP,
7294 &mboxq->u.mqe.un.set_feature);
7295 }
7296 }
7297
7298 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7299 /* Enable MDS Diagnostics only if the SLI Port supports it */
7300 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7301 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7302 if (rc != MBX_SUCCESS)
7303 phba->mds_diags_support = 0;
7304 }
572709e2 7305
da0436e9
JS
7306 /*
7307 * Discover the port's supported feature set and match it against the
7308 * hosts requests.
7309 */
7310 lpfc_request_features(phba, mboxq);
7311 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7312 if (unlikely(rc)) {
7313 rc = -EIO;
76a95d75 7314 goto out_free_mbox;
da0436e9
JS
7315 }
7316
7317 /*
7318 * The port must support FCP initiator mode as this is the
7319 * only mode running in the host.
7320 */
7321 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7322 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7323 "0378 No support for fcpi mode.\n");
7324 ftr_rsp++;
7325 }
0bc2b7c5
JS
7326
7327 /* Performance Hints are ONLY for FCoE */
7328 if (phba->hba_flag & HBA_FCOE_MODE) {
7329 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7330 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7331 else
7332 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7333 }
7334
da0436e9
JS
7335 /*
7336 * If the port cannot support the host's requested features
7337 * then turn off the global config parameters to disable the
7338 * feature in the driver. This is not a fatal error.
7339 */
f44ac12f
JS
7340 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7341 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7342 phba->cfg_enable_bg = 0;
7343 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
bf08611b 7344 ftr_rsp++;
f44ac12f 7345 }
bf08611b 7346 }
da0436e9
JS
7347
7348 if (phba->max_vpi && phba->cfg_enable_npiv &&
7349 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7350 ftr_rsp++;
7351
7352 if (ftr_rsp) {
7353 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7354 "0379 Feature Mismatch Data: x%08x %08x "
7355 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7356 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7357 phba->cfg_enable_npiv, phba->max_vpi);
7358 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7359 phba->cfg_enable_bg = 0;
7360 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7361 phba->cfg_enable_npiv = 0;
7362 }
7363
7364 /* These SLI3 features are assumed in SLI4 */
7365 spin_lock_irq(&phba->hbalock);
7366 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7367 spin_unlock_irq(&phba->hbalock);
7368
6d368e53
JS
7369 /*
7370 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7371 * calls depends on these resources to complete port setup.
7372 */
7373 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7374 if (rc) {
7375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7376 "2920 Failed to alloc Resource IDs "
7377 "rc = x%x\n", rc);
7378 goto out_free_mbox;
7379 }
7380
61bda8f7
JS
7381 lpfc_set_host_data(phba, mboxq);
7382
7383 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7384 if (rc) {
7385 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7386 "2134 Failed to set host os driver version %x",
7387 rc);
7388 }
7389
da0436e9 7390 /* Read the port's service parameters. */
9f1177a3
JS
7391 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7392 if (rc) {
7393 phba->link_state = LPFC_HBA_ERROR;
7394 rc = -ENOMEM;
76a95d75 7395 goto out_free_mbox;
9f1177a3
JS
7396 }
7397
da0436e9
JS
7398 mboxq->vport = vport;
7399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3e1f0718 7400 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
da0436e9
JS
7401 if (rc == MBX_SUCCESS) {
7402 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7403 rc = 0;
7404 }
7405
7406 /*
7407 * This memory was allocated by the lpfc_read_sparam routine. Release
7408 * it to the mbuf pool.
7409 */
7410 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7411 kfree(mp);
3e1f0718 7412 mboxq->ctx_buf = NULL;
da0436e9
JS
7413 if (unlikely(rc)) {
7414 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7415 "0382 READ_SPARAM command failed "
7416 "status %d, mbxStatus x%x\n",
7417 rc, bf_get(lpfc_mqe_status, mqe));
7418 phba->link_state = LPFC_HBA_ERROR;
7419 rc = -EIO;
76a95d75 7420 goto out_free_mbox;
da0436e9
JS
7421 }
7422
0558056c 7423 lpfc_update_vport_wwn(vport);
da0436e9
JS
7424
7425 /* Update the fc_host data structures with new wwn. */
7426 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7427 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7428
895427bd
JS
7429 /* Create all the SLI4 queues */
7430 rc = lpfc_sli4_queue_create(phba);
7431 if (rc) {
7432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7433 "3089 Failed to allocate queues\n");
7434 rc = -ENODEV;
7435 goto out_free_mbox;
7436 }
7437 /* Set up all the queues to the device */
7438 rc = lpfc_sli4_queue_setup(phba);
7439 if (unlikely(rc)) {
7440 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7441 "0381 Error %d during queue setup.\n ", rc);
7442 goto out_stop_timers;
7443 }
7444 /* Initialize the driver internal SLI layer lists. */
7445 lpfc_sli4_setup(phba);
7446 lpfc_sli4_queue_init(phba);
7447
7448 /* update host els xri-sgl sizes and mappings */
7449 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
7450 if (unlikely(rc)) {
7451 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7452 "1400 Failed to update xri-sgl size and "
7453 "mapping: %d\n", rc);
895427bd 7454 goto out_destroy_queue;
da0436e9
JS
7455 }
7456
8a9d2e80 7457 /* register the els sgl pool to the port */
895427bd
JS
7458 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7459 phba->sli4_hba.els_xri_cnt);
7460 if (unlikely(rc < 0)) {
8a9d2e80
JS
7461 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7462 "0582 Error %d during els sgl post "
7463 "operation\n", rc);
7464 rc = -ENODEV;
895427bd 7465 goto out_destroy_queue;
8a9d2e80 7466 }
895427bd 7467 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 7468
f358dd0c
JS
7469 if (phba->nvmet_support) {
7470 /* update host nvmet xri-sgl sizes and mappings */
7471 rc = lpfc_sli4_nvmet_sgl_update(phba);
7472 if (unlikely(rc)) {
7473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7474 "6308 Failed to update nvmet-sgl size "
7475 "and mapping: %d\n", rc);
7476 goto out_destroy_queue;
7477 }
7478
7479 /* register the nvmet sgl pool to the port */
7480 rc = lpfc_sli4_repost_sgl_list(
7481 phba,
7482 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7483 phba->sli4_hba.nvmet_xri_cnt);
7484 if (unlikely(rc < 0)) {
7485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7486 "3117 Error %d during nvmet "
7487 "sgl post\n", rc);
7488 rc = -ENODEV;
7489 goto out_destroy_queue;
7490 }
7491 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
7492
7493 cnt = phba->cfg_iocb_cnt * 1024;
7494 /* We need 1 iocbq for every SGL, for IO processing */
7495 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 7496 } else {
895427bd
JS
7497 /* update host scsi xri-sgl sizes and mappings */
7498 rc = lpfc_sli4_scsi_sgl_update(phba);
7499 if (unlikely(rc)) {
7500 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7501 "6309 Failed to update scsi-sgl size "
7502 "and mapping: %d\n", rc);
7503 goto out_destroy_queue;
7504 }
7505
7506 /* update host nvme xri-sgl sizes and mappings */
7507 rc = lpfc_sli4_nvme_sgl_update(phba);
7508 if (unlikely(rc)) {
7509 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7510 "6082 Failed to update nvme-sgl size "
7511 "and mapping: %d\n", rc);
7512 goto out_destroy_queue;
7513 }
6c621a22
JS
7514
7515 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
7516 }
7517
7518 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
7519 /* Initialize and populate the iocb list per host */
7520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 7521 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
7522 phba->cfg_iocb_cnt, cnt);
7523 rc = lpfc_init_iocb_list(phba, cnt);
7524 if (rc) {
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 7526 "1413 Failed to init iocb list.\n");
6c621a22
JS
7527 goto out_destroy_queue;
7528 }
895427bd
JS
7529 }
7530
11e644e2
JS
7531 if (phba->nvmet_support)
7532 lpfc_nvmet_create_targetport(phba);
7533
2d7dbc4c 7534 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
7535 /* Post initial buffers to all RQs created */
7536 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7537 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7538 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7539 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7540 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 7541 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
7542 rqbp->buffer_count = 0;
7543
2d7dbc4c
JS
7544 lpfc_post_rq_buffer(
7545 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7546 phba->sli4_hba.nvmet_mrq_data[i],
2448e484 7547 phba->cfg_nvmet_mrq_post, i);
2d7dbc4c
JS
7548 }
7549 }
7550
895427bd
JS
7551 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7552 /* register the allocated scsi sgl pool to the port */
7553 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7554 if (unlikely(rc)) {
7555 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7556 "0383 Error %d during scsi sgl post "
7557 "operation\n", rc);
7558 /* Some Scsi buffers were moved to abort scsi list */
7559 /* A pci function reset will repost them */
7560 rc = -ENODEV;
7561 goto out_destroy_queue;
7562 }
da0436e9
JS
7563 }
7564
01649561
JS
7565 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7566 (phba->nvmet_support == 0)) {
7567
7568 /* register the allocated nvme sgl pool to the port */
7569 rc = lpfc_repost_nvme_sgl_list(phba);
7570 if (unlikely(rc)) {
7571 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7572 "6116 Error %d during nvme sgl post "
7573 "operation\n", rc);
7574 /* Some NVME buffers were moved to abort nvme list */
7575 /* A pci function reset will repost them */
7576 rc = -ENODEV;
7577 goto out_destroy_queue;
7578 }
da0436e9
JS
7579 }
7580
7581 /* Post the rpi header region to the device. */
7582 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7583 if (unlikely(rc)) {
7584 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7585 "0393 Error %d during rpi post operation\n",
7586 rc);
7587 rc = -ENODEV;
895427bd 7588 goto out_destroy_queue;
da0436e9 7589 }
97f2ecf1 7590 lpfc_sli4_node_prep(phba);
da0436e9 7591
895427bd 7592 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7593 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7594 /*
7595 * The FC Port needs to register FCFI (index 0)
7596 */
7597 lpfc_reg_fcfi(phba, mboxq);
7598 mboxq->vport = phba->pport;
7599 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7600 if (rc != MBX_SUCCESS)
7601 goto out_unset_queue;
7602 rc = 0;
7603 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7604 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7605 } else {
7606 /* We are a NVME Target mode with MRQ > 1 */
7607
7608 /* First register the FCFI */
7609 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7610 mboxq->vport = phba->pport;
7611 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7612 if (rc != MBX_SUCCESS)
7613 goto out_unset_queue;
7614 rc = 0;
7615 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7616 &mboxq->u.mqe.un.reg_fcfi_mrq);
7617
7618 /* Next register the MRQs */
7619 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7620 mboxq->vport = phba->pport;
7621 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7622 if (rc != MBX_SUCCESS)
7623 goto out_unset_queue;
7624 rc = 0;
895427bd
JS
7625 }
7626 /* Check if the port is configured to be disabled */
7627 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7628 }
7629
7630 /* Arm the CQs and then EQs on device */
7631 lpfc_sli4_arm_cqeq_intr(phba);
7632
7633 /* Indicate device interrupt mode */
7634 phba->sli4_hba.intr_enable = 1;
7635
7636 /* Allow asynchronous mailbox command to go through */
7637 spin_lock_irq(&phba->hbalock);
7638 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7639 spin_unlock_irq(&phba->hbalock);
7640
7641 /* Post receive buffers to the device */
7642 lpfc_sli4_rb_setup(phba);
7643
fc2b989b
JS
7644 /* Reset HBA FCF states after HBA reset */
7645 phba->fcf.fcf_flag = 0;
7646 phba->fcf.current_rec.flag = 0;
7647
da0436e9 7648 /* Start the ELS watchdog timer */
8fa38513 7649 mod_timer(&vport->els_tmofunc,
256ec0d0 7650 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7651
7652 /* Start heart beat timer */
7653 mod_timer(&phba->hb_tmofunc,
256ec0d0 7654 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7655 phba->hb_outstanding = 0;
7656 phba->last_completion_time = jiffies;
7657
7658 /* Start error attention (ERATT) polling timer */
256ec0d0 7659 mod_timer(&phba->eratt_poll,
65791f1f 7660 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7661
75baf696
JS
7662 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7663 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7664 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7665 if (!rc) {
7666 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7667 "2829 This device supports "
7668 "Advanced Error Reporting (AER)\n");
7669 spin_lock_irq(&phba->hbalock);
7670 phba->hba_flag |= HBA_AER_ENABLED;
7671 spin_unlock_irq(&phba->hbalock);
7672 } else {
7673 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7674 "2830 This device does not support "
7675 "Advanced Error Reporting (AER)\n");
7676 phba->cfg_aer_support = 0;
7677 }
0a96e975 7678 rc = 0;
75baf696
JS
7679 }
7680
da0436e9
JS
7681 /*
7682 * The port is ready, set the host's link state to LINK_DOWN
7683 * in preparation for link interrupts.
7684 */
da0436e9
JS
7685 spin_lock_irq(&phba->hbalock);
7686 phba->link_state = LPFC_LINK_DOWN;
1dc5ec24
JS
7687
7688 /* Check if physical ports are trunked */
7689 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7690 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7691 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7692 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7693 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7694 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7695 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7696 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
da0436e9 7697 spin_unlock_irq(&phba->hbalock);
1dc5ec24 7698
026abb87
JS
7699 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7700 (phba->hba_flag & LINK_DISABLED)) {
7701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7702 "3103 Adapter Link is disabled.\n");
7703 lpfc_down_link(phba, mboxq);
7704 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7705 if (rc != MBX_SUCCESS) {
7706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7707 "3104 Adapter failed to issue "
7708 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7709 goto out_unset_queue;
7710 }
7711 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7712 /* don't perform init_link on SLI4 FC port loopback test */
7713 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7714 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7715 if (rc)
7716 goto out_unset_queue;
7717 }
5350d872
JS
7718 }
7719 mempool_free(mboxq, phba->mbox_mem_pool);
7720 return rc;
76a95d75 7721out_unset_queue:
da0436e9 7722 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7723 lpfc_sli4_queue_unset(phba);
7724out_destroy_queue:
6c621a22 7725 lpfc_free_iocb_list(phba);
5350d872 7726 lpfc_sli4_queue_destroy(phba);
da0436e9 7727out_stop_timers:
5350d872 7728 lpfc_stop_hba_timers(phba);
da0436e9
JS
7729out_free_mbox:
7730 mempool_free(mboxq, phba->mbox_mem_pool);
7731 return rc;
7732}
7733
7734/**
7735 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7736 * @ptr: context object - pointer to hba structure.
7737 *
7738 * This is the callback function for mailbox timer. The mailbox
7739 * timer is armed when a new mailbox command is issued and the timer
7740 * is deleted when the mailbox complete. The function is called by
7741 * the kernel timer code when a mailbox does not complete within
7742 * expected time. This function wakes up the worker thread to
7743 * process the mailbox timeout and returns. All the processing is
7744 * done by the worker thread function lpfc_mbox_timeout_handler.
7745 **/
7746void
f22eb4d3 7747lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7748{
f22eb4d3 7749 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7750 unsigned long iflag;
7751 uint32_t tmo_posted;
7752
7753 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7754 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7755 if (!tmo_posted)
7756 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7757 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7758
7759 if (!tmo_posted)
7760 lpfc_worker_wake_up(phba);
7761 return;
7762}
7763
e8d3c3b1
JS
7764/**
7765 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7766 * are pending
7767 * @phba: Pointer to HBA context object.
7768 *
7769 * This function checks if any mailbox completions are present on the mailbox
7770 * completion queue.
7771 **/
3bb11fc5 7772static bool
e8d3c3b1
JS
7773lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7774{
7775
7776 uint32_t idx;
7777 struct lpfc_queue *mcq;
7778 struct lpfc_mcqe *mcqe;
7779 bool pending_completions = false;
7365f6fd 7780 uint8_t qe_valid;
e8d3c3b1
JS
7781
7782 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7783 return false;
7784
7785 /* Check for completions on mailbox completion queue */
7786
7787 mcq = phba->sli4_hba.mbx_cq;
7788 idx = mcq->hba_index;
7365f6fd
JS
7789 qe_valid = mcq->qe_valid;
7790 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
e8d3c3b1
JS
7791 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7792 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7793 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7794 pending_completions = true;
7795 break;
7796 }
7797 idx = (idx + 1) % mcq->entry_count;
7798 if (mcq->hba_index == idx)
7799 break;
7365f6fd
JS
7800
7801 /* if the index wrapped around, toggle the valid bit */
7802 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7803 qe_valid = (qe_valid) ? 0 : 1;
e8d3c3b1
JS
7804 }
7805 return pending_completions;
7806
7807}
7808
7809/**
7810 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7811 * that were missed.
7812 * @phba: Pointer to HBA context object.
7813 *
7814 * For sli4, it is possible to miss an interrupt. As such mbox completions
7815 * maybe missed causing erroneous mailbox timeouts to occur. This function
7816 * checks to see if mbox completions are on the mailbox completion queue
7817 * and will process all the completions associated with the eq for the
7818 * mailbox completion queue.
7819 **/
7820bool
7821lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7822{
b71413dd 7823 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
e8d3c3b1
JS
7824 uint32_t eqidx;
7825 struct lpfc_queue *fpeq = NULL;
7826 struct lpfc_eqe *eqe;
7827 bool mbox_pending;
7828
7829 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7830 return false;
7831
7832 /* Find the eq associated with the mcq */
7833
b71413dd 7834 if (sli4_hba->hba_eq)
895427bd 7835 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
b71413dd
JS
7836 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7837 sli4_hba->mbx_cq->assoc_qid) {
7838 fpeq = sli4_hba->hba_eq[eqidx];
e8d3c3b1
JS
7839 break;
7840 }
7841 if (!fpeq)
7842 return false;
7843
7844 /* Turn off interrupts from this EQ */
7845
b71413dd 7846 sli4_hba->sli4_eq_clr_intr(fpeq);
e8d3c3b1
JS
7847
7848 /* Check to see if a mbox completion is pending */
7849
7850 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7851
7852 /*
7853 * If a mbox completion is pending, process all the events on EQ
7854 * associated with the mbox completion queue (this could include
7855 * mailbox commands, async events, els commands, receive queue data
7856 * and fcp commands)
7857 */
7858
7859 if (mbox_pending)
7860 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7861 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7862 fpeq->EQ_processed++;
7863 }
7864
7865 /* Always clear and re-arm the EQ */
7866
b71413dd 7867 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
e8d3c3b1
JS
7868
7869 return mbox_pending;
7870
7871}
da0436e9
JS
7872
7873/**
7874 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7875 * @phba: Pointer to HBA context object.
7876 *
7877 * This function is called from worker thread when a mailbox command times out.
7878 * The caller is not required to hold any locks. This function will reset the
7879 * HBA and recover all the pending commands.
7880 **/
7881void
7882lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7883{
7884 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7885 MAILBOX_t *mb = NULL;
7886
da0436e9 7887 struct lpfc_sli *psli = &phba->sli;
da0436e9 7888
e8d3c3b1
JS
7889 /* If the mailbox completed, process the completion and return */
7890 if (lpfc_sli4_process_missed_mbox_completions(phba))
7891 return;
7892
eb016566
JS
7893 if (pmbox != NULL)
7894 mb = &pmbox->u.mb;
da0436e9
JS
7895 /* Check the pmbox pointer first. There is a race condition
7896 * between the mbox timeout handler getting executed in the
7897 * worklist and the mailbox actually completing. When this
7898 * race condition occurs, the mbox_active will be NULL.
7899 */
7900 spin_lock_irq(&phba->hbalock);
7901 if (pmbox == NULL) {
7902 lpfc_printf_log(phba, KERN_WARNING,
7903 LOG_MBOX | LOG_SLI,
7904 "0353 Active Mailbox cleared - mailbox timeout "
7905 "exiting\n");
7906 spin_unlock_irq(&phba->hbalock);
7907 return;
7908 }
7909
7910 /* Mbox cmd <mbxCommand> timeout */
7911 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7912 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7913 mb->mbxCommand,
7914 phba->pport->port_state,
7915 phba->sli.sli_flag,
7916 phba->sli.mbox_active);
7917 spin_unlock_irq(&phba->hbalock);
7918
7919 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7920 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7921 * it to fail all outstanding SCSI IO.
da0436e9
JS
7922 */
7923 spin_lock_irq(&phba->pport->work_port_lock);
7924 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7925 spin_unlock_irq(&phba->pport->work_port_lock);
7926 spin_lock_irq(&phba->hbalock);
7927 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7928 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7929 spin_unlock_irq(&phba->hbalock);
7930
db55fba8 7931 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7932
7933 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7934 "0345 Resetting board due to mailbox timeout\n");
7935
7936 /* Reset the HBA device */
7937 lpfc_reset_hba(phba);
7938}
7939
7940/**
7941 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7942 * @phba: Pointer to HBA context object.
7943 * @pmbox: Pointer to mailbox object.
7944 * @flag: Flag indicating how the mailbox need to be processed.
7945 *
7946 * This function is called by discovery code and HBA management code
7947 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7948 * function gets the hbalock to protect the data structures.
7949 * The mailbox command can be submitted in polling mode, in which case
7950 * this function will wait in a polling loop for the completion of the
7951 * mailbox.
7952 * If the mailbox is submitted in no_wait mode (not polling) the
7953 * function will submit the command and returns immediately without waiting
7954 * for the mailbox completion. The no_wait is supported only when HBA
7955 * is in SLI2/SLI3 mode - interrupts are enabled.
7956 * The SLI interface allows only one mailbox pending at a time. If the
7957 * mailbox is issued in polling mode and there is already a mailbox
7958 * pending, then the function will return an error. If the mailbox is issued
7959 * in NO_WAIT mode and there is a mailbox pending already, the function
7960 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7961 * The sli layer owns the mailbox object until the completion of mailbox
7962 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7963 * return codes the caller owns the mailbox command after the return of
7964 * the function.
e59058c4 7965 **/
3772a991
JS
7966static int
7967lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7968 uint32_t flag)
dea3101e 7969{
bf07bdea 7970 MAILBOX_t *mbx;
2e0fef85 7971 struct lpfc_sli *psli = &phba->sli;
dea3101e 7972 uint32_t status, evtctr;
9940b97b 7973 uint32_t ha_copy, hc_copy;
dea3101e 7974 int i;
09372820 7975 unsigned long timeout;
dea3101e 7976 unsigned long drvr_flag = 0;
34b02dcd 7977 uint32_t word0, ldata;
dea3101e 7978 void __iomem *to_slim;
58da1ffb
JS
7979 int processing_queue = 0;
7980
7981 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7982 if (!pmbox) {
8568a4d2 7983 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7984 /* processing mbox queue from intr_handler */
3772a991
JS
7985 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7986 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7987 return MBX_SUCCESS;
7988 }
58da1ffb 7989 processing_queue = 1;
58da1ffb
JS
7990 pmbox = lpfc_mbox_get(phba);
7991 if (!pmbox) {
7992 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7993 return MBX_SUCCESS;
7994 }
7995 }
dea3101e 7996
ed957684 7997 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7998 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7999 if(!pmbox->vport) {
58da1ffb 8000 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 8001 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 8002 LOG_MBOX | LOG_VPORT,
e8b62011 8003 "1806 Mbox x%x failed. No vport\n",
3772a991 8004 pmbox->u.mb.mbxCommand);
ed957684 8005 dump_stack();
58da1ffb 8006 goto out_not_finished;
ed957684
JS
8007 }
8008 }
8009
8d63f375 8010 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
8011 if (unlikely(pci_channel_offline(phba->pcidev))) {
8012 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8013 goto out_not_finished;
8014 }
8d63f375 8015
a257bf90
JS
8016 /* If HBA has a deferred error attention, fail the iocb. */
8017 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8018 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8019 goto out_not_finished;
8020 }
8021
dea3101e 8022 psli = &phba->sli;
92d7f7b0 8023
bf07bdea 8024 mbx = &pmbox->u.mb;
dea3101e
JB
8025 status = MBX_SUCCESS;
8026
2e0fef85
JS
8027 if (phba->link_state == LPFC_HBA_ERROR) {
8028 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
8029
8030 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
8031 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8032 "(%d):0311 Mailbox command x%x cannot "
8033 "issue Data: x%x x%x\n",
8034 pmbox->vport ? pmbox->vport->vpi : 0,
8035 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 8036 goto out_not_finished;
41415862
JW
8037 }
8038
bf07bdea 8039 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
8040 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8041 !(hc_copy & HC_MBINT_ENA)) {
8042 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8043 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
8044 "(%d):2528 Mailbox command x%x cannot "
8045 "issue Data: x%x x%x\n",
8046 pmbox->vport ? pmbox->vport->vpi : 0,
8047 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
8048 goto out_not_finished;
8049 }
9290831f
JS
8050 }
8051
dea3101e
JB
8052 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8053 /* Polling for a mbox command when another one is already active
8054 * is not allowed in SLI. Also, the driver must have established
8055 * SLI2 mode to queue and process multiple mbox commands.
8056 */
8057
8058 if (flag & MBX_POLL) {
2e0fef85 8059 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
8060
8061 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
8062 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8063 "(%d):2529 Mailbox command x%x "
8064 "cannot issue Data: x%x x%x\n",
8065 pmbox->vport ? pmbox->vport->vpi : 0,
8066 pmbox->u.mb.mbxCommand,
8067 psli->sli_flag, flag);
58da1ffb 8068 goto out_not_finished;
dea3101e
JB
8069 }
8070
3772a991 8071 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 8072 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 8073 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
8074 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8075 "(%d):2530 Mailbox command x%x "
8076 "cannot issue Data: x%x x%x\n",
8077 pmbox->vport ? pmbox->vport->vpi : 0,
8078 pmbox->u.mb.mbxCommand,
8079 psli->sli_flag, flag);
58da1ffb 8080 goto out_not_finished;
dea3101e
JB
8081 }
8082
dea3101e
JB
8083 /* Another mailbox command is still being processed, queue this
8084 * command to be processed later.
8085 */
8086 lpfc_mbox_put(phba, pmbox);
8087
8088 /* Mbox cmd issue - BUSY */
ed957684 8089 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 8090 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 8091 "x%x x%x x%x x%x\n",
92d7f7b0 8092 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
8093 mbx->mbxCommand,
8094 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 8095 psli->sli_flag, flag);
dea3101e
JB
8096
8097 psli->slistat.mbox_busy++;
2e0fef85 8098 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 8099
858c9f6c
JS
8100 if (pmbox->vport) {
8101 lpfc_debugfs_disc_trc(pmbox->vport,
8102 LPFC_DISC_TRC_MBOX_VPORT,
8103 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
8104 (uint32_t)mbx->mbxCommand,
8105 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
8106 }
8107 else {
8108 lpfc_debugfs_disc_trc(phba->pport,
8109 LPFC_DISC_TRC_MBOX,
8110 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
8111 (uint32_t)mbx->mbxCommand,
8112 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
8113 }
8114
2e0fef85 8115 return MBX_BUSY;
dea3101e
JB
8116 }
8117
dea3101e
JB
8118 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8119
8120 /* If we are not polling, we MUST be in SLI2 mode */
8121 if (flag != MBX_POLL) {
3772a991 8122 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 8123 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 8124 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 8125 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 8126 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
8127 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8128 "(%d):2531 Mailbox command x%x "
8129 "cannot issue Data: x%x x%x\n",
8130 pmbox->vport ? pmbox->vport->vpi : 0,
8131 pmbox->u.mb.mbxCommand,
8132 psli->sli_flag, flag);
58da1ffb 8133 goto out_not_finished;
dea3101e
JB
8134 }
8135 /* timeout active mbox command */
256ec0d0
JS
8136 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8137 1000);
8138 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
8139 }
8140
8141 /* Mailbox cmd <cmd> issue */
ed957684 8142 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 8143 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 8144 "x%x\n",
e8b62011 8145 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
8146 mbx->mbxCommand,
8147 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 8148 psli->sli_flag, flag);
dea3101e 8149
bf07bdea 8150 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
8151 if (pmbox->vport) {
8152 lpfc_debugfs_disc_trc(pmbox->vport,
8153 LPFC_DISC_TRC_MBOX_VPORT,
8154 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
8155 (uint32_t)mbx->mbxCommand,
8156 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
8157 }
8158 else {
8159 lpfc_debugfs_disc_trc(phba->pport,
8160 LPFC_DISC_TRC_MBOX,
8161 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
8162 (uint32_t)mbx->mbxCommand,
8163 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
8164 }
8165 }
8166
dea3101e
JB
8167 psli->slistat.mbox_cmd++;
8168 evtctr = psli->slistat.mbox_event;
8169
8170 /* next set own bit for the adapter and copy over command word */
bf07bdea 8171 mbx->mbxOwner = OWN_CHIP;
dea3101e 8172
3772a991 8173 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
8174 /* Populate mbox extension offset word. */
8175 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 8176 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
8177 = (uint8_t *)phba->mbox_ext
8178 - (uint8_t *)phba->mbox;
8179 }
8180
8181 /* Copy the mailbox extension data */
3e1f0718
JS
8182 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8183 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8184 (uint8_t *)phba->mbox_ext,
8185 pmbox->in_ext_byte_len);
7a470277
JS
8186 }
8187 /* Copy command data to host SLIM area */
bf07bdea 8188 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 8189 } else {
7a470277
JS
8190 /* Populate mbox extension offset word. */
8191 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 8192 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
8193 = MAILBOX_HBA_EXT_OFFSET;
8194
8195 /* Copy the mailbox extension data */
3e1f0718 8196 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
7a470277
JS
8197 lpfc_memcpy_to_slim(phba->MBslimaddr +
8198 MAILBOX_HBA_EXT_OFFSET,
3e1f0718 8199 pmbox->ctx_buf, pmbox->in_ext_byte_len);
7a470277 8200
895427bd 8201 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 8202 /* copy command data into host mbox for cmpl */
895427bd
JS
8203 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8204 MAILBOX_CMD_SIZE);
dea3101e
JB
8205
8206 /* First copy mbox command data to HBA SLIM, skip past first
8207 word */
8208 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 8209 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
8210 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8211
8212 /* Next copy over first word, with mbxOwner set */
bf07bdea 8213 ldata = *((uint32_t *)mbx);
dea3101e
JB
8214 to_slim = phba->MBslimaddr;
8215 writel(ldata, to_slim);
8216 readl(to_slim); /* flush */
8217
895427bd 8218 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 8219 /* switch over to host mailbox */
3772a991 8220 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
8221 }
8222
8223 wmb();
dea3101e
JB
8224
8225 switch (flag) {
8226 case MBX_NOWAIT:
09372820 8227 /* Set up reference to mailbox command */
dea3101e 8228 psli->mbox_active = pmbox;
09372820
JS
8229 /* Interrupt board to do it */
8230 writel(CA_MBATT, phba->CAregaddr);
8231 readl(phba->CAregaddr); /* flush */
8232 /* Don't wait for it to finish, just return */
dea3101e
JB
8233 break;
8234
8235 case MBX_POLL:
09372820 8236 /* Set up null reference to mailbox command */
dea3101e 8237 psli->mbox_active = NULL;
09372820
JS
8238 /* Interrupt board to do it */
8239 writel(CA_MBATT, phba->CAregaddr);
8240 readl(phba->CAregaddr); /* flush */
8241
3772a991 8242 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 8243 /* First read mbox status word */
34b02dcd 8244 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
8245 word0 = le32_to_cpu(word0);
8246 } else {
8247 /* First read mbox status word */
9940b97b
JS
8248 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8249 spin_unlock_irqrestore(&phba->hbalock,
8250 drvr_flag);
8251 goto out_not_finished;
8252 }
dea3101e
JB
8253 }
8254
8255 /* Read the HBA Host Attention Register */
9940b97b
JS
8256 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8257 spin_unlock_irqrestore(&phba->hbalock,
8258 drvr_flag);
8259 goto out_not_finished;
8260 }
a183a15f
JS
8261 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8262 1000) + jiffies;
09372820 8263 i = 0;
dea3101e 8264 /* Wait for command to complete */
41415862
JW
8265 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8266 (!(ha_copy & HA_MBATT) &&
2e0fef85 8267 (phba->link_state > LPFC_WARM_START))) {
09372820 8268 if (time_after(jiffies, timeout)) {
dea3101e 8269 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 8270 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 8271 drvr_flag);
58da1ffb 8272 goto out_not_finished;
dea3101e
JB
8273 }
8274
8275 /* Check if we took a mbox interrupt while we were
8276 polling */
8277 if (((word0 & OWN_CHIP) != OWN_CHIP)
8278 && (evtctr != psli->slistat.mbox_event))
8279 break;
8280
09372820
JS
8281 if (i++ > 10) {
8282 spin_unlock_irqrestore(&phba->hbalock,
8283 drvr_flag);
8284 msleep(1);
8285 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8286 }
dea3101e 8287
3772a991 8288 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 8289 /* First copy command data */
34b02dcd 8290 word0 = *((uint32_t *)phba->mbox);
dea3101e 8291 word0 = le32_to_cpu(word0);
bf07bdea 8292 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 8293 MAILBOX_t *slimmb;
34b02dcd 8294 uint32_t slimword0;
dea3101e
JB
8295 /* Check real SLIM for any errors */
8296 slimword0 = readl(phba->MBslimaddr);
8297 slimmb = (MAILBOX_t *) & slimword0;
8298 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8299 && slimmb->mbxStatus) {
8300 psli->sli_flag &=
3772a991 8301 ~LPFC_SLI_ACTIVE;
dea3101e
JB
8302 word0 = slimword0;
8303 }
8304 }
8305 } else {
8306 /* First copy command data */
8307 word0 = readl(phba->MBslimaddr);
8308 }
8309 /* Read the HBA Host Attention Register */
9940b97b
JS
8310 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8311 spin_unlock_irqrestore(&phba->hbalock,
8312 drvr_flag);
8313 goto out_not_finished;
8314 }
dea3101e
JB
8315 }
8316
3772a991 8317 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 8318 /* copy results back to user */
2ea259ee
JS
8319 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8320 MAILBOX_CMD_SIZE);
7a470277 8321 /* Copy the mailbox extension data */
3e1f0718 8322 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
7a470277 8323 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
3e1f0718 8324 pmbox->ctx_buf,
7a470277
JS
8325 pmbox->out_ext_byte_len);
8326 }
dea3101e
JB
8327 } else {
8328 /* First copy command data */
bf07bdea 8329 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 8330 MAILBOX_CMD_SIZE);
7a470277 8331 /* Copy the mailbox extension data */
3e1f0718
JS
8332 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8333 lpfc_memcpy_from_slim(
8334 pmbox->ctx_buf,
7a470277
JS
8335 phba->MBslimaddr +
8336 MAILBOX_HBA_EXT_OFFSET,
8337 pmbox->out_ext_byte_len);
dea3101e
JB
8338 }
8339 }
8340
8341 writel(HA_MBATT, phba->HAregaddr);
8342 readl(phba->HAregaddr); /* flush */
8343
8344 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 8345 status = mbx->mbxStatus;
dea3101e
JB
8346 }
8347
2e0fef85
JS
8348 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8349 return status;
58da1ffb
JS
8350
8351out_not_finished:
8352 if (processing_queue) {
da0436e9 8353 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
8354 lpfc_mbox_cmpl_put(phba, pmbox);
8355 }
8356 return MBX_NOT_FINISHED;
dea3101e
JB
8357}
8358
f1126688
JS
8359/**
8360 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8361 * @phba: Pointer to HBA context object.
8362 *
8363 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8364 * the driver internal pending mailbox queue. It will then try to wait out the
8365 * possible outstanding mailbox command before return.
8366 *
8367 * Returns:
8368 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8369 * the outstanding mailbox command timed out.
8370 **/
8371static int
8372lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8373{
8374 struct lpfc_sli *psli = &phba->sli;
f1126688 8375 int rc = 0;
a183a15f 8376 unsigned long timeout = 0;
f1126688
JS
8377
8378 /* Mark the asynchronous mailbox command posting as blocked */
8379 spin_lock_irq(&phba->hbalock);
8380 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
8381 /* Determine how long we might wait for the active mailbox
8382 * command to be gracefully completed by firmware.
8383 */
a183a15f
JS
8384 if (phba->sli.mbox_active)
8385 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8386 phba->sli.mbox_active) *
8387 1000) + jiffies;
8388 spin_unlock_irq(&phba->hbalock);
8389
e8d3c3b1
JS
8390 /* Make sure the mailbox is really active */
8391 if (timeout)
8392 lpfc_sli4_process_missed_mbox_completions(phba);
8393
f1126688
JS
8394 /* Wait for the outstnading mailbox command to complete */
8395 while (phba->sli.mbox_active) {
8396 /* Check active mailbox complete status every 2ms */
8397 msleep(2);
8398 if (time_after(jiffies, timeout)) {
8399 /* Timeout, marked the outstanding cmd not complete */
8400 rc = 1;
8401 break;
8402 }
8403 }
8404
8405 /* Can not cleanly block async mailbox command, fails it */
8406 if (rc) {
8407 spin_lock_irq(&phba->hbalock);
8408 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8409 spin_unlock_irq(&phba->hbalock);
8410 }
8411 return rc;
8412}
8413
8414/**
8415 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8416 * @phba: Pointer to HBA context object.
8417 *
8418 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8419 * commands from the driver internal pending mailbox queue. It makes sure
8420 * that there is no outstanding mailbox command before resuming posting
8421 * asynchronous mailbox commands. If, for any reason, there is outstanding
8422 * mailbox command, it will try to wait it out before resuming asynchronous
8423 * mailbox command posting.
8424 **/
8425static void
8426lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8427{
8428 struct lpfc_sli *psli = &phba->sli;
8429
8430 spin_lock_irq(&phba->hbalock);
8431 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8432 /* Asynchronous mailbox posting is not blocked, do nothing */
8433 spin_unlock_irq(&phba->hbalock);
8434 return;
8435 }
8436
8437 /* Outstanding synchronous mailbox command is guaranteed to be done,
8438 * successful or timeout, after timing-out the outstanding mailbox
8439 * command shall always be removed, so just unblock posting async
8440 * mailbox command and resume
8441 */
8442 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8443 spin_unlock_irq(&phba->hbalock);
8444
8445 /* wake up worker thread to post asynchronlous mailbox command */
8446 lpfc_worker_wake_up(phba);
8447}
8448
2d843edc
JS
8449/**
8450 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8451 * @phba: Pointer to HBA context object.
8452 * @mboxq: Pointer to mailbox object.
8453 *
8454 * The function waits for the bootstrap mailbox register ready bit from
8455 * port for twice the regular mailbox command timeout value.
8456 *
8457 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8458 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8459 **/
8460static int
8461lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8462{
8463 uint32_t db_ready;
8464 unsigned long timeout;
8465 struct lpfc_register bmbx_reg;
8466
8467 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8468 * 1000) + jiffies;
8469
8470 do {
8471 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8472 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8473 if (!db_ready)
8474 msleep(2);
8475
8476 if (time_after(jiffies, timeout))
8477 return MBXERR_ERROR;
8478 } while (!db_ready);
8479
8480 return 0;
8481}
8482
da0436e9
JS
8483/**
8484 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8485 * @phba: Pointer to HBA context object.
8486 * @mboxq: Pointer to mailbox object.
8487 *
8488 * The function posts a mailbox to the port. The mailbox is expected
8489 * to be comletely filled in and ready for the port to operate on it.
8490 * This routine executes a synchronous completion operation on the
8491 * mailbox by polling for its completion.
8492 *
8493 * The caller must not be holding any locks when calling this routine.
8494 *
8495 * Returns:
8496 * MBX_SUCCESS - mailbox posted successfully
8497 * Any of the MBX error values.
8498 **/
8499static int
8500lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8501{
8502 int rc = MBX_SUCCESS;
8503 unsigned long iflag;
da0436e9
JS
8504 uint32_t mcqe_status;
8505 uint32_t mbx_cmnd;
da0436e9
JS
8506 struct lpfc_sli *psli = &phba->sli;
8507 struct lpfc_mqe *mb = &mboxq->u.mqe;
8508 struct lpfc_bmbx_create *mbox_rgn;
8509 struct dma_address *dma_address;
da0436e9
JS
8510
8511 /*
8512 * Only one mailbox can be active to the bootstrap mailbox region
8513 * at a time and there is no queueing provided.
8514 */
8515 spin_lock_irqsave(&phba->hbalock, iflag);
8516 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8517 spin_unlock_irqrestore(&phba->hbalock, iflag);
8518 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8519 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8520 "cannot issue Data: x%x x%x\n",
8521 mboxq->vport ? mboxq->vport->vpi : 0,
8522 mboxq->u.mb.mbxCommand,
a183a15f
JS
8523 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8524 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8525 psli->sli_flag, MBX_POLL);
8526 return MBXERR_ERROR;
8527 }
8528 /* The server grabs the token and owns it until release */
8529 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8530 phba->sli.mbox_active = mboxq;
8531 spin_unlock_irqrestore(&phba->hbalock, iflag);
8532
2d843edc
JS
8533 /* wait for bootstrap mbox register for readyness */
8534 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8535 if (rc)
8536 goto exit;
8537
da0436e9
JS
8538 /*
8539 * Initialize the bootstrap memory region to avoid stale data areas
8540 * in the mailbox post. Then copy the caller's mailbox contents to
8541 * the bmbx mailbox region.
8542 */
8543 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8544 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
48f8fdb4
JS
8545 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8546 sizeof(struct lpfc_mqe));
da0436e9
JS
8547
8548 /* Post the high mailbox dma address to the port and wait for ready. */
8549 dma_address = &phba->sli4_hba.bmbx.dma_address;
8550 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8551
2d843edc
JS
8552 /* wait for bootstrap mbox register for hi-address write done */
8553 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8554 if (rc)
8555 goto exit;
da0436e9
JS
8556
8557 /* Post the low mailbox dma address to the port. */
8558 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 8559
2d843edc
JS
8560 /* wait for bootstrap mbox register for low address write done */
8561 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8562 if (rc)
8563 goto exit;
da0436e9
JS
8564
8565 /*
8566 * Read the CQ to ensure the mailbox has completed.
8567 * If so, update the mailbox status so that the upper layers
8568 * can complete the request normally.
8569 */
48f8fdb4
JS
8570 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8571 sizeof(struct lpfc_mqe));
da0436e9 8572 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
48f8fdb4
JS
8573 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8574 sizeof(struct lpfc_mcqe));
da0436e9 8575 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
8576 /*
8577 * When the CQE status indicates a failure and the mailbox status
8578 * indicates success then copy the CQE status into the mailbox status
8579 * (and prefix it with x4000).
8580 */
da0436e9 8581 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8582 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8583 bf_set(lpfc_mqe_status, mb,
8584 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8585 rc = MBXERR_ERROR;
d7c47992
JS
8586 } else
8587 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8588
8589 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8590 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8591 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8592 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8593 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8594 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8595 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8596 bf_get(lpfc_mqe_status, mb),
8597 mb->un.mb_words[0], mb->un.mb_words[1],
8598 mb->un.mb_words[2], mb->un.mb_words[3],
8599 mb->un.mb_words[4], mb->un.mb_words[5],
8600 mb->un.mb_words[6], mb->un.mb_words[7],
8601 mb->un.mb_words[8], mb->un.mb_words[9],
8602 mb->un.mb_words[10], mb->un.mb_words[11],
8603 mb->un.mb_words[12], mboxq->mcqe.word0,
8604 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8605 mboxq->mcqe.trailer);
8606exit:
8607 /* We are holding the token, no needed for lock when release */
8608 spin_lock_irqsave(&phba->hbalock, iflag);
8609 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8610 phba->sli.mbox_active = NULL;
8611 spin_unlock_irqrestore(&phba->hbalock, iflag);
8612 return rc;
8613}
8614
8615/**
8616 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8617 * @phba: Pointer to HBA context object.
8618 * @pmbox: Pointer to mailbox object.
8619 * @flag: Flag indicating how the mailbox need to be processed.
8620 *
8621 * This function is called by discovery code and HBA management code to submit
8622 * a mailbox command to firmware with SLI-4 interface spec.
8623 *
8624 * Return codes the caller owns the mailbox command after the return of the
8625 * function.
8626 **/
8627static int
8628lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8629 uint32_t flag)
8630{
8631 struct lpfc_sli *psli = &phba->sli;
8632 unsigned long iflags;
8633 int rc;
8634
b76f2dc9
JS
8635 /* dump from issue mailbox command if setup */
8636 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8637
8fa38513
JS
8638 rc = lpfc_mbox_dev_check(phba);
8639 if (unlikely(rc)) {
8640 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8641 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8642 "cannot issue Data: x%x x%x\n",
8643 mboxq->vport ? mboxq->vport->vpi : 0,
8644 mboxq->u.mb.mbxCommand,
a183a15f
JS
8645 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8646 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8647 psli->sli_flag, flag);
8648 goto out_not_finished;
8649 }
8650
da0436e9
JS
8651 /* Detect polling mode and jump to a handler */
8652 if (!phba->sli4_hba.intr_enable) {
8653 if (flag == MBX_POLL)
8654 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8655 else
8656 rc = -EIO;
8657 if (rc != MBX_SUCCESS)
0558056c 8658 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8659 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8660 "(x%x/x%x) failure: "
8661 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8662 "Data: x%x x%x\n,",
da0436e9
JS
8663 mboxq->vport ? mboxq->vport->vpi : 0,
8664 mboxq->u.mb.mbxCommand,
a183a15f
JS
8665 lpfc_sli_config_mbox_subsys_get(phba,
8666 mboxq),
8667 lpfc_sli_config_mbox_opcode_get(phba,
8668 mboxq),
cc459f19
JS
8669 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8670 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8671 bf_get(lpfc_mcqe_ext_status,
8672 &mboxq->mcqe),
da0436e9
JS
8673 psli->sli_flag, flag);
8674 return rc;
8675 } else if (flag == MBX_POLL) {
f1126688
JS
8676 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8677 "(%d):2542 Try to issue mailbox command "
7365f6fd 8678 "x%x (x%x/x%x) synchronously ahead of async "
f1126688 8679 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8680 mboxq->vport ? mboxq->vport->vpi : 0,
8681 mboxq->u.mb.mbxCommand,
a183a15f
JS
8682 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8683 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8684 psli->sli_flag, flag);
f1126688
JS
8685 /* Try to block the asynchronous mailbox posting */
8686 rc = lpfc_sli4_async_mbox_block(phba);
8687 if (!rc) {
8688 /* Successfully blocked, now issue sync mbox cmd */
8689 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8690 if (rc != MBX_SUCCESS)
cc459f19 8691 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8692 LOG_MBOX | LOG_SLI,
cc459f19
JS
8693 "(%d):2597 Sync Mailbox command "
8694 "x%x (x%x/x%x) failure: "
8695 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8696 "Data: x%x x%x\n,",
8697 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8698 mboxq->u.mb.mbxCommand,
8699 lpfc_sli_config_mbox_subsys_get(phba,
8700 mboxq),
8701 lpfc_sli_config_mbox_opcode_get(phba,
8702 mboxq),
cc459f19
JS
8703 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8704 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8705 bf_get(lpfc_mcqe_ext_status,
8706 &mboxq->mcqe),
a183a15f 8707 psli->sli_flag, flag);
f1126688
JS
8708 /* Unblock the async mailbox posting afterward */
8709 lpfc_sli4_async_mbox_unblock(phba);
8710 }
8711 return rc;
da0436e9
JS
8712 }
8713
8714 /* Now, interrupt mode asynchrous mailbox command */
8715 rc = lpfc_mbox_cmd_check(phba, mboxq);
8716 if (rc) {
8717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8718 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8719 "cannot issue Data: x%x x%x\n",
8720 mboxq->vport ? mboxq->vport->vpi : 0,
8721 mboxq->u.mb.mbxCommand,
a183a15f
JS
8722 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8723 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8724 psli->sli_flag, flag);
8725 goto out_not_finished;
8726 }
da0436e9
JS
8727
8728 /* Put the mailbox command to the driver internal FIFO */
8729 psli->slistat.mbox_busy++;
8730 spin_lock_irqsave(&phba->hbalock, iflags);
8731 lpfc_mbox_put(phba, mboxq);
8732 spin_unlock_irqrestore(&phba->hbalock, iflags);
8733 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8734 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8735 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8736 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8737 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8738 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8739 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8740 phba->pport->port_state,
8741 psli->sli_flag, MBX_NOWAIT);
8742 /* Wake up worker thread to transport mailbox command from head */
8743 lpfc_worker_wake_up(phba);
8744
8745 return MBX_BUSY;
8746
8747out_not_finished:
8748 return MBX_NOT_FINISHED;
8749}
8750
8751/**
8752 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8753 * @phba: Pointer to HBA context object.
8754 *
8755 * This function is called by worker thread to send a mailbox command to
8756 * SLI4 HBA firmware.
8757 *
8758 **/
8759int
8760lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8761{
8762 struct lpfc_sli *psli = &phba->sli;
8763 LPFC_MBOXQ_t *mboxq;
8764 int rc = MBX_SUCCESS;
8765 unsigned long iflags;
8766 struct lpfc_mqe *mqe;
8767 uint32_t mbx_cmnd;
8768
8769 /* Check interrupt mode before post async mailbox command */
8770 if (unlikely(!phba->sli4_hba.intr_enable))
8771 return MBX_NOT_FINISHED;
8772
8773 /* Check for mailbox command service token */
8774 spin_lock_irqsave(&phba->hbalock, iflags);
8775 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8776 spin_unlock_irqrestore(&phba->hbalock, iflags);
8777 return MBX_NOT_FINISHED;
8778 }
8779 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8780 spin_unlock_irqrestore(&phba->hbalock, iflags);
8781 return MBX_NOT_FINISHED;
8782 }
8783 if (unlikely(phba->sli.mbox_active)) {
8784 spin_unlock_irqrestore(&phba->hbalock, iflags);
8785 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8786 "0384 There is pending active mailbox cmd\n");
8787 return MBX_NOT_FINISHED;
8788 }
8789 /* Take the mailbox command service token */
8790 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8791
8792 /* Get the next mailbox command from head of queue */
8793 mboxq = lpfc_mbox_get(phba);
8794
8795 /* If no more mailbox command waiting for post, we're done */
8796 if (!mboxq) {
8797 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8798 spin_unlock_irqrestore(&phba->hbalock, iflags);
8799 return MBX_SUCCESS;
8800 }
8801 phba->sli.mbox_active = mboxq;
8802 spin_unlock_irqrestore(&phba->hbalock, iflags);
8803
8804 /* Check device readiness for posting mailbox command */
8805 rc = lpfc_mbox_dev_check(phba);
8806 if (unlikely(rc))
8807 /* Driver clean routine will clean up pending mailbox */
8808 goto out_not_finished;
8809
8810 /* Prepare the mbox command to be posted */
8811 mqe = &mboxq->u.mqe;
8812 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8813
8814 /* Start timer for the mbox_tmo and log some mailbox post messages */
8815 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8816 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8817
8818 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8819 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8820 "x%x x%x\n",
8821 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8822 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8823 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8824 phba->pport->port_state, psli->sli_flag);
8825
8826 if (mbx_cmnd != MBX_HEARTBEAT) {
8827 if (mboxq->vport) {
8828 lpfc_debugfs_disc_trc(mboxq->vport,
8829 LPFC_DISC_TRC_MBOX_VPORT,
8830 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8831 mbx_cmnd, mqe->un.mb_words[0],
8832 mqe->un.mb_words[1]);
8833 } else {
8834 lpfc_debugfs_disc_trc(phba->pport,
8835 LPFC_DISC_TRC_MBOX,
8836 "MBOX Send: cmd:x%x mb:x%x x%x",
8837 mbx_cmnd, mqe->un.mb_words[0],
8838 mqe->un.mb_words[1]);
8839 }
8840 }
8841 psli->slistat.mbox_cmd++;
8842
8843 /* Post the mailbox command to the port */
8844 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8845 if (rc != MBX_SUCCESS) {
8846 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8847 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8848 "cannot issue Data: x%x x%x\n",
8849 mboxq->vport ? mboxq->vport->vpi : 0,
8850 mboxq->u.mb.mbxCommand,
a183a15f
JS
8851 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8852 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8853 psli->sli_flag, MBX_NOWAIT);
8854 goto out_not_finished;
8855 }
8856
8857 return rc;
8858
8859out_not_finished:
8860 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8861 if (phba->sli.mbox_active) {
8862 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8863 __lpfc_mbox_cmpl_put(phba, mboxq);
8864 /* Release the token */
8865 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8866 phba->sli.mbox_active = NULL;
8867 }
da0436e9
JS
8868 spin_unlock_irqrestore(&phba->hbalock, iflags);
8869
8870 return MBX_NOT_FINISHED;
8871}
8872
8873/**
8874 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8875 * @phba: Pointer to HBA context object.
8876 * @pmbox: Pointer to mailbox object.
8877 * @flag: Flag indicating how the mailbox need to be processed.
8878 *
8879 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8880 * the API jump table function pointer from the lpfc_hba struct.
8881 *
8882 * Return codes the caller owns the mailbox command after the return of the
8883 * function.
8884 **/
8885int
8886lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8887{
8888 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8889}
8890
8891/**
25985edc 8892 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8893 * @phba: The hba struct for which this call is being executed.
8894 * @dev_grp: The HBA PCI-Device group number.
8895 *
8896 * This routine sets up the mbox interface API function jump table in @phba
8897 * struct.
8898 * Returns: 0 - success, -ENODEV - failure.
8899 **/
8900int
8901lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8902{
8903
8904 switch (dev_grp) {
8905 case LPFC_PCI_DEV_LP:
8906 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8907 phba->lpfc_sli_handle_slow_ring_event =
8908 lpfc_sli_handle_slow_ring_event_s3;
8909 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8910 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8911 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8912 break;
8913 case LPFC_PCI_DEV_OC:
8914 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8915 phba->lpfc_sli_handle_slow_ring_event =
8916 lpfc_sli_handle_slow_ring_event_s4;
8917 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8918 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8919 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8920 break;
8921 default:
8922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8923 "1420 Invalid HBA PCI-device group: 0x%x\n",
8924 dev_grp);
8925 return -ENODEV;
8926 break;
8927 }
8928 return 0;
8929}
8930
e59058c4 8931/**
3621a710 8932 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8933 * @phba: Pointer to HBA context object.
8934 * @pring: Pointer to driver SLI ring object.
8935 * @piocb: Pointer to address of newly added command iocb.
8936 *
8937 * This function is called with hbalock held to add a command
8938 * iocb to the txq when SLI layer cannot submit the command iocb
8939 * to the ring.
8940 **/
2a9bf3d0 8941void
92d7f7b0 8942__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8943 struct lpfc_iocbq *piocb)
dea3101e 8944{
1c2ba475 8945 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8946 /* Insert the caller's iocb in the txq tail for later processing. */
8947 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8948}
8949
e59058c4 8950/**
3621a710 8951 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8952 * @phba: Pointer to HBA context object.
8953 * @pring: Pointer to driver SLI ring object.
8954 * @piocb: Pointer to address of newly added command iocb.
8955 *
8956 * This function is called with hbalock held before a new
8957 * iocb is submitted to the firmware. This function checks
8958 * txq to flush the iocbs in txq to Firmware before
8959 * submitting new iocbs to the Firmware.
8960 * If there are iocbs in the txq which need to be submitted
8961 * to firmware, lpfc_sli_next_iocb returns the first element
8962 * of the txq after dequeuing it from txq.
8963 * If there is no iocb in the txq then the function will return
8964 * *piocb and *piocb is set to NULL. Caller needs to check
8965 * *piocb to find if there are more commands in the txq.
8966 **/
dea3101e
JB
8967static struct lpfc_iocbq *
8968lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8969 struct lpfc_iocbq **piocb)
dea3101e
JB
8970{
8971 struct lpfc_iocbq * nextiocb;
8972
1c2ba475
JT
8973 lockdep_assert_held(&phba->hbalock);
8974
dea3101e
JB
8975 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8976 if (!nextiocb) {
8977 nextiocb = *piocb;
8978 *piocb = NULL;
8979 }
8980
8981 return nextiocb;
8982}
8983
e59058c4 8984/**
3772a991 8985 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8986 * @phba: Pointer to HBA context object.
3772a991 8987 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8988 * @piocb: Pointer to command iocb.
8989 * @flag: Flag indicating if this command can be put into txq.
8990 *
3772a991
JS
8991 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8992 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8993 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8994 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8995 * this function allows only iocbs for posting buffers. This function finds
8996 * next available slot in the command ring and posts the command to the
8997 * available slot and writes the port attention register to request HBA start
8998 * processing new iocb. If there is no slot available in the ring and
8999 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9000 * the function returns IOCB_BUSY.
e59058c4 9001 *
3772a991
JS
9002 * This function is called with hbalock held. The function will return success
9003 * after it successfully submit the iocb to firmware or after adding to the
9004 * txq.
e59058c4 9005 **/
98c9ea5c 9006static int
3772a991 9007__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
9008 struct lpfc_iocbq *piocb, uint32_t flag)
9009{
9010 struct lpfc_iocbq *nextiocb;
9011 IOCB_t *iocb;
895427bd 9012 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 9013
1c2ba475
JT
9014 lockdep_assert_held(&phba->hbalock);
9015
92d7f7b0
JS
9016 if (piocb->iocb_cmpl && (!piocb->vport) &&
9017 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9018 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9019 lpfc_printf_log(phba, KERN_ERR,
9020 LOG_SLI | LOG_VPORT,
e8b62011 9021 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
9022 piocb->iocb.ulpCommand);
9023 dump_stack();
9024 return IOCB_ERROR;
9025 }
9026
9027
8d63f375
LV
9028 /* If the PCI channel is in offline state, do not post iocbs. */
9029 if (unlikely(pci_channel_offline(phba->pcidev)))
9030 return IOCB_ERROR;
9031
a257bf90
JS
9032 /* If HBA has a deferred error attention, fail the iocb. */
9033 if (unlikely(phba->hba_flag & DEFER_ERATT))
9034 return IOCB_ERROR;
9035
dea3101e
JB
9036 /*
9037 * We should never get an IOCB if we are in a < LINK_DOWN state
9038 */
2e0fef85 9039 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
9040 return IOCB_ERROR;
9041
9042 /*
9043 * Check to see if we are blocking IOCB processing because of a
0b727fea 9044 * outstanding event.
dea3101e 9045 */
0b727fea 9046 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
9047 goto iocb_busy;
9048
2e0fef85 9049 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 9050 /*
2680eeaa 9051 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
9052 * can be issued if the link is not up.
9053 */
9054 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
9055 case CMD_GEN_REQUEST64_CR:
9056 case CMD_GEN_REQUEST64_CX:
9057 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9058 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 9059 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
9060 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9061 MENLO_TRANSPORT_TYPE))
9062
9063 goto iocb_busy;
9064 break;
dea3101e
JB
9065 case CMD_QUE_RING_BUF_CN:
9066 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
9067 /*
9068 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9069 * completion, iocb_cmpl MUST be 0.
9070 */
9071 if (piocb->iocb_cmpl)
9072 piocb->iocb_cmpl = NULL;
9073 /*FALLTHROUGH*/
9074 case CMD_CREATE_XRI_CR:
2680eeaa
JS
9075 case CMD_CLOSE_XRI_CN:
9076 case CMD_CLOSE_XRI_CX:
dea3101e
JB
9077 break;
9078 default:
9079 goto iocb_busy;
9080 }
9081
9082 /*
9083 * For FCP commands, we must be in a state where we can process link
9084 * attention events.
9085 */
895427bd 9086 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 9087 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 9088 goto iocb_busy;
92d7f7b0 9089 }
dea3101e 9090
dea3101e
JB
9091 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9092 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9093 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9094
9095 if (iocb)
9096 lpfc_sli_update_ring(phba, pring);
9097 else
9098 lpfc_sli_update_full_ring(phba, pring);
9099
9100 if (!piocb)
9101 return IOCB_SUCCESS;
9102
9103 goto out_busy;
9104
9105 iocb_busy:
9106 pring->stats.iocb_cmd_delay++;
9107
9108 out_busy:
9109
9110 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 9111 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
9112 return IOCB_SUCCESS;
9113 }
9114
9115 return IOCB_BUSY;
9116}
9117
3772a991 9118/**
4f774513
JS
9119 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9120 * @phba: Pointer to HBA context object.
9121 * @piocb: Pointer to command iocb.
9122 * @sglq: Pointer to the scatter gather queue object.
9123 *
9124 * This routine converts the bpl or bde that is in the IOCB
9125 * to a sgl list for the sli4 hardware. The physical address
9126 * of the bpl/bde is converted back to a virtual address.
9127 * If the IOCB contains a BPL then the list of BDE's is
9128 * converted to sli4_sge's. If the IOCB contains a single
9129 * BDE then it is converted to a single sli_sge.
9130 * The IOCB is still in cpu endianess so the contents of
9131 * the bpl can be used without byte swapping.
9132 *
9133 * Returns valid XRI = Success, NO_XRI = Failure.
9134**/
9135static uint16_t
9136lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9137 struct lpfc_sglq *sglq)
3772a991 9138{
4f774513
JS
9139 uint16_t xritag = NO_XRI;
9140 struct ulp_bde64 *bpl = NULL;
9141 struct ulp_bde64 bde;
9142 struct sli4_sge *sgl = NULL;
1b51197d 9143 struct lpfc_dmabuf *dmabuf;
4f774513
JS
9144 IOCB_t *icmd;
9145 int numBdes = 0;
9146 int i = 0;
63e801ce
JS
9147 uint32_t offset = 0; /* accumulated offset in the sg request list */
9148 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 9149
4f774513
JS
9150 if (!piocbq || !sglq)
9151 return xritag;
9152
9153 sgl = (struct sli4_sge *)sglq->sgl;
9154 icmd = &piocbq->iocb;
6b5151fd
JS
9155 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9156 return sglq->sli4_xritag;
4f774513
JS
9157 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9158 numBdes = icmd->un.genreq64.bdl.bdeSize /
9159 sizeof(struct ulp_bde64);
9160 /* The addrHigh and addrLow fields within the IOCB
9161 * have not been byteswapped yet so there is no
9162 * need to swap them back.
9163 */
1b51197d
JS
9164 if (piocbq->context3)
9165 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9166 else
9167 return xritag;
4f774513 9168
1b51197d 9169 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
9170 if (!bpl)
9171 return xritag;
9172
9173 for (i = 0; i < numBdes; i++) {
9174 /* Should already be byte swapped. */
28baac74
JS
9175 sgl->addr_hi = bpl->addrHigh;
9176 sgl->addr_lo = bpl->addrLow;
9177
0558056c 9178 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
9179 if ((i+1) == numBdes)
9180 bf_set(lpfc_sli4_sge_last, sgl, 1);
9181 else
9182 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
9183 /* swap the size field back to the cpu so we
9184 * can assign it to the sgl.
9185 */
9186 bde.tus.w = le32_to_cpu(bpl->tus.w);
9187 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
9188 /* The offsets in the sgl need to be accumulated
9189 * separately for the request and reply lists.
9190 * The request is always first, the reply follows.
9191 */
9192 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9193 /* add up the reply sg entries */
9194 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9195 inbound++;
9196 /* first inbound? reset the offset */
9197 if (inbound == 1)
9198 offset = 0;
9199 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
9200 bf_set(lpfc_sli4_sge_type, sgl,
9201 LPFC_SGE_TYPE_DATA);
63e801ce
JS
9202 offset += bde.tus.f.bdeSize;
9203 }
546fc854 9204 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
9205 bpl++;
9206 sgl++;
9207 }
9208 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9209 /* The addrHigh and addrLow fields of the BDE have not
9210 * been byteswapped yet so they need to be swapped
9211 * before putting them in the sgl.
9212 */
9213 sgl->addr_hi =
9214 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9215 sgl->addr_lo =
9216 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 9217 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
9218 bf_set(lpfc_sli4_sge_last, sgl, 1);
9219 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
9220 sgl->sge_len =
9221 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
9222 }
9223 return sglq->sli4_xritag;
3772a991 9224}
92d7f7b0 9225
e59058c4 9226/**
4f774513 9227 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 9228 * @phba: Pointer to HBA context object.
4f774513
JS
9229 * @piocb: Pointer to command iocb.
9230 * @wqe: Pointer to the work queue entry.
e59058c4 9231 *
4f774513
JS
9232 * This routine converts the iocb command to its Work Queue Entry
9233 * equivalent. The wqe pointer should not have any fields set when
9234 * this routine is called because it will memcpy over them.
9235 * This routine does not set the CQ_ID or the WQEC bits in the
9236 * wqe.
e59058c4 9237 *
4f774513 9238 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 9239 **/
cf5bf97e 9240static int
4f774513 9241lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
205e8240 9242 union lpfc_wqe128 *wqe)
cf5bf97e 9243{
5ffc266e 9244 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
9245 uint8_t ct = 0;
9246 uint32_t fip;
9247 uint32_t abort_tag;
9248 uint8_t command_type = ELS_COMMAND_NON_FIP;
9249 uint8_t cmnd;
9250 uint16_t xritag;
dcf2a4e0
JS
9251 uint16_t abrt_iotag;
9252 struct lpfc_iocbq *abrtiocbq;
4f774513 9253 struct ulp_bde64 *bpl = NULL;
f0d9bccc 9254 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
9255 int numBdes, i;
9256 struct ulp_bde64 bde;
c31098ce 9257 struct lpfc_nodelist *ndlp;
ff78d8f9 9258 uint32_t *pcmd;
1b51197d 9259 uint32_t if_type;
4f774513 9260
45ed1190 9261 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 9262 /* The fcp commands will set command type */
0c287589 9263 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 9264 command_type = FCP_COMMAND;
c868595d 9265 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
9266 command_type = ELS_COMMAND_FIP;
9267 else
9268 command_type = ELS_COMMAND_NON_FIP;
9269
b5c53958
JS
9270 if (phba->fcp_embed_io)
9271 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
9272 /* Some of the fields are in the right position already */
9273 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
9274 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
9275 /* The ct field has moved so reset */
9276 wqe->generic.wqe_com.word7 = 0;
9277 wqe->generic.wqe_com.word10 = 0;
9278 }
b5c53958
JS
9279
9280 abort_tag = (uint32_t) iocbq->iotag;
9281 xritag = iocbq->sli4_xritag;
4f774513
JS
9282 /* words0-2 bpl convert bde */
9283 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
9284 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9285 sizeof(struct ulp_bde64);
4f774513
JS
9286 bpl = (struct ulp_bde64 *)
9287 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9288 if (!bpl)
9289 return IOCB_ERROR;
cf5bf97e 9290
4f774513
JS
9291 /* Should already be byte swapped. */
9292 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9293 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9294 /* swap the size field back to the cpu so we
9295 * can assign it to the sgl.
9296 */
9297 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
9298 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9299 total_len = 0;
9300 for (i = 0; i < numBdes; i++) {
9301 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9302 total_len += bde.tus.f.bdeSize;
9303 }
4f774513 9304 } else
5ffc266e 9305 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 9306
4f774513
JS
9307 iocbq->iocb.ulpIoTag = iocbq->iotag;
9308 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 9309
4f774513
JS
9310 switch (iocbq->iocb.ulpCommand) {
9311 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
9312 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9313 ndlp = iocbq->context_un.ndlp;
9314 else
9315 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
9316 if (!iocbq->iocb.ulpLe) {
9317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9318 "2007 Only Limited Edition cmd Format"
9319 " supported 0x%x\n",
9320 iocbq->iocb.ulpCommand);
9321 return IOCB_ERROR;
9322 }
ff78d8f9 9323
5ffc266e 9324 wqe->els_req.payload_len = xmit_len;
4f774513
JS
9325 /* Els_reguest64 has a TMO */
9326 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9327 iocbq->iocb.ulpTimeout);
9328 /* Need a VF for word 4 set the vf bit*/
9329 bf_set(els_req64_vf, &wqe->els_req, 0);
9330 /* And a VFID for word 12 */
9331 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 9332 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
9333 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9334 iocbq->iocb.ulpContext);
9335 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9336 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 9337 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 9338 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
9339 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9340 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
9341 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9342 iocbq->context2)->virt);
1b51197d
JS
9343 if_type = bf_get(lpfc_sli_intf_if_type,
9344 &phba->sli4_hba.sli_intf);
27d6ac0a 9345 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 9346 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 9347 *pcmd == ELS_CMD_SCR ||
6b5151fd 9348 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 9349 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
9350 *pcmd == ELS_CMD_PLOGI)) {
9351 bf_set(els_req64_sp, &wqe->els_req, 1);
9352 bf_set(els_req64_sid, &wqe->els_req,
9353 iocbq->vport->fc_myDID);
939723a4
JS
9354 if ((*pcmd == ELS_CMD_FLOGI) &&
9355 !(phba->fc_topology ==
9356 LPFC_TOPOLOGY_LOOP))
9357 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
9358 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9359 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 9360 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 9361 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
9362 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9363 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9364 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9365 }
c868595d 9366 }
6d368e53
JS
9367 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9368 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
9369 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9370 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9371 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9372 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9373 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9374 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 9375 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 9376 break;
5ffc266e 9377 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
9378 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9379 iocbq->iocb.un.ulpWord[3]);
9380 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 9381 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
9382 /* The entire sequence is transmitted for this IOCB */
9383 xmit_len = total_len;
9384 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
9385 if (phba->link_flag & LS_LOOPBACK_MODE)
9386 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 9387 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
9388 /* word3 iocb=io_tag32 wqe=reserved */
9389 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
9390 /* word4 relative_offset memcpy */
9391 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
9392 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9393 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9394 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9395 LPFC_WQE_IOD_WRITE);
9396 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9397 LPFC_WQE_LENLOC_WORD12);
9398 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
9399 wqe->xmit_sequence.xmit_len = xmit_len;
9400 command_type = OTHER_COMMAND;
7851fe2c 9401 break;
4f774513 9402 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
9403 /* word3 iocb=iotag32 wqe=seq_payload_len */
9404 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
9405 /* word4 iocb=rsvd wqe=rsvd */
9406 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9407 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 9408 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 9409 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
9410 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9411 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9412 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9413 LPFC_WQE_LENLOC_WORD3);
9414 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 9415 break;
4f774513
JS
9416 case CMD_FCP_IWRITE64_CR:
9417 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
9418 /* word3 iocb=iotag wqe=payload_offset_len */
9419 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9420 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9421 xmit_len + sizeof(struct fcp_rsp));
9422 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9423 0);
f0d9bccc
JS
9424 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9425 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9426 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9427 iocbq->iocb.ulpFCP2Rcvy);
9428 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9429 /* Always open the exchange */
f0d9bccc
JS
9430 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9431 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9432 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9433 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9434 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
9435 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9436 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
9437 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9438 if (iocbq->priority) {
9439 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9440 (iocbq->priority << 1));
9441 } else {
1ba981fd
JS
9442 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9443 (phba->cfg_XLanePriority << 1));
9444 }
9445 }
b5c53958
JS
9446 /* Note, word 10 is already initialized to 0 */
9447
414abe0a
JS
9448 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9449 if (phba->cfg_enable_pbde)
0bc2b7c5
JS
9450 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9451 else
9452 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9453
b5c53958
JS
9454 if (phba->fcp_embed_io) {
9455 struct lpfc_scsi_buf *lpfc_cmd;
9456 struct sli4_sge *sgl;
b5c53958
JS
9457 struct fcp_cmnd *fcp_cmnd;
9458 uint32_t *ptr;
9459
9460 /* 128 byte wqe support here */
b5c53958
JS
9461
9462 lpfc_cmd = iocbq->context1;
9463 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9464 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9465
9466 /* Word 0-2 - FCP_CMND */
205e8240 9467 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9468 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9469 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9470 wqe->generic.bde.addrHigh = 0;
9471 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9472
205e8240
JS
9473 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9474 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
b5c53958
JS
9475
9476 /* Word 22-29 FCP CMND Payload */
205e8240 9477 ptr = &wqe->words[22];
b5c53958
JS
9478 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9479 }
7851fe2c 9480 break;
4f774513 9481 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
9482 /* word3 iocb=iotag wqe=payload_offset_len */
9483 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9484 bf_set(payload_offset_len, &wqe->fcp_iread,
9485 xmit_len + sizeof(struct fcp_rsp));
9486 bf_set(cmd_buff_len, &wqe->fcp_iread,
9487 0);
f0d9bccc
JS
9488 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9489 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9490 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9491 iocbq->iocb.ulpFCP2Rcvy);
9492 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 9493 /* Always open the exchange */
f0d9bccc
JS
9494 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9495 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9496 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9497 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9498 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
9499 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9500 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
9501 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9502 if (iocbq->priority) {
9503 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9504 (iocbq->priority << 1));
9505 } else {
1ba981fd
JS
9506 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9507 (phba->cfg_XLanePriority << 1));
9508 }
9509 }
b5c53958
JS
9510 /* Note, word 10 is already initialized to 0 */
9511
414abe0a
JS
9512 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9513 if (phba->cfg_enable_pbde)
0bc2b7c5
JS
9514 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9515 else
9516 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9517
b5c53958
JS
9518 if (phba->fcp_embed_io) {
9519 struct lpfc_scsi_buf *lpfc_cmd;
9520 struct sli4_sge *sgl;
b5c53958
JS
9521 struct fcp_cmnd *fcp_cmnd;
9522 uint32_t *ptr;
9523
9524 /* 128 byte wqe support here */
b5c53958
JS
9525
9526 lpfc_cmd = iocbq->context1;
9527 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9528 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9529
9530 /* Word 0-2 - FCP_CMND */
205e8240 9531 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9532 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9533 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9534 wqe->generic.bde.addrHigh = 0;
9535 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9536
205e8240
JS
9537 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9538 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
b5c53958
JS
9539
9540 /* Word 22-29 FCP CMND Payload */
205e8240 9541 ptr = &wqe->words[22];
b5c53958
JS
9542 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9543 }
7851fe2c 9544 break;
4f774513 9545 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
9546 /* word3 iocb=iotag wqe=payload_offset_len */
9547 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9548 bf_set(payload_offset_len, &wqe->fcp_icmd,
9549 xmit_len + sizeof(struct fcp_rsp));
9550 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9551 0);
f0d9bccc 9552 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 9553 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 9554 /* Always open the exchange */
f0d9bccc
JS
9555 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9556 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9557 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9558 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9559 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
9560 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9561 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
9562 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9563 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
9564 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9565 if (iocbq->priority) {
9566 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9567 (iocbq->priority << 1));
9568 } else {
1ba981fd
JS
9569 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9570 (phba->cfg_XLanePriority << 1));
9571 }
9572 }
b5c53958
JS
9573 /* Note, word 10 is already initialized to 0 */
9574
9575 if (phba->fcp_embed_io) {
9576 struct lpfc_scsi_buf *lpfc_cmd;
9577 struct sli4_sge *sgl;
b5c53958
JS
9578 struct fcp_cmnd *fcp_cmnd;
9579 uint32_t *ptr;
9580
9581 /* 128 byte wqe support here */
b5c53958
JS
9582
9583 lpfc_cmd = iocbq->context1;
9584 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9585 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9586
9587 /* Word 0-2 - FCP_CMND */
205e8240 9588 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9589 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9590 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9591 wqe->generic.bde.addrHigh = 0;
9592 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9593
205e8240
JS
9594 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9595 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
b5c53958
JS
9596
9597 /* Word 22-29 FCP CMND Payload */
205e8240 9598 ptr = &wqe->words[22];
b5c53958
JS
9599 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9600 }
7851fe2c 9601 break;
4f774513 9602 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9603 /* For this command calculate the xmit length of the
9604 * request bde.
9605 */
9606 xmit_len = 0;
9607 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9608 sizeof(struct ulp_bde64);
9609 for (i = 0; i < numBdes; i++) {
63e801ce 9610 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9611 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9612 break;
63e801ce
JS
9613 xmit_len += bde.tus.f.bdeSize;
9614 }
f0d9bccc
JS
9615 /* word3 iocb=IO_TAG wqe=request_payload_len */
9616 wqe->gen_req.request_payload_len = xmit_len;
9617 /* word4 iocb=parameter wqe=relative_offset memcpy */
9618 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9619 /* word6 context tag copied in memcpy */
9620 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9621 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9623 "2015 Invalid CT %x command 0x%x\n",
9624 ct, iocbq->iocb.ulpCommand);
9625 return IOCB_ERROR;
9626 }
f0d9bccc
JS
9627 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9628 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9629 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9630 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9631 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9632 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9633 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9634 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9635 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9636 command_type = OTHER_COMMAND;
7851fe2c 9637 break;
4f774513 9638 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9639 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9640 /* words0-2 BDE memcpy */
f0d9bccc
JS
9641 /* word3 iocb=iotag32 wqe=response_payload_len */
9642 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9643 /* word4 */
9644 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9645 /* word5 iocb=rsvd wge=did */
9646 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9647 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9648
9649 if_type = bf_get(lpfc_sli_intf_if_type,
9650 &phba->sli4_hba.sli_intf);
27d6ac0a 9651 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
939723a4
JS
9652 if (iocbq->vport->fc_flag & FC_PT2PT) {
9653 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9654 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9655 iocbq->vport->fc_myDID);
9656 if (iocbq->vport->fc_myDID == Fabric_DID) {
9657 bf_set(wqe_els_did,
9658 &wqe->xmit_els_rsp.wqe_dest, 0);
9659 }
9660 }
9661 }
f0d9bccc
JS
9662 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9663 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9664 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9665 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9666 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9667 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9668 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9669 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9670 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9671 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9672 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9673 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9674 LPFC_WQE_LENLOC_WORD3);
9675 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9676 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9677 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9678 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9679 iocbq->context2)->virt);
9680 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9681 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9682 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9683 iocbq->vport->fc_myDID);
939723a4
JS
9684 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9685 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9686 phba->vpi_ids[phba->pport->vpi]);
9687 }
4f774513 9688 command_type = OTHER_COMMAND;
7851fe2c 9689 break;
4f774513
JS
9690 case CMD_CLOSE_XRI_CN:
9691 case CMD_ABORT_XRI_CN:
9692 case CMD_ABORT_XRI_CX:
9693 /* words 0-2 memcpy should be 0 rserved */
9694 /* port will send abts */
dcf2a4e0
JS
9695 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9696 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9697 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9698 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9699 } else
9700 fip = 0;
9701
9702 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9703 /*
dcf2a4e0
JS
9704 * The link is down, or the command was ELS_FIP
9705 * so the fw does not need to send abts
4f774513
JS
9706 * on the wire.
9707 */
9708 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9709 else
9710 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9711 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9712 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9713 wqe->abort_cmd.rsrvd5 = 0;
9714 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9715 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9716 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9717 /*
9718 * The abort handler will send us CMD_ABORT_XRI_CN or
9719 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9720 */
f0d9bccc
JS
9721 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9722 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9723 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9724 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9725 cmnd = CMD_ABORT_XRI_CX;
9726 command_type = OTHER_COMMAND;
9727 xritag = 0;
7851fe2c 9728 break;
6669f9bb 9729 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9730 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9731 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9732 * we re-construct this WQE here based on information in
9733 * iocbq from scratch.
9734 */
9735 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9736 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9737 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9738 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9739 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9740 LPFC_ABTS_UNSOL_INT) {
9741 /* ABTS sent by initiator to CT exchange, the
9742 * RX_ID field will be filled with the newly
9743 * allocated responder XRI.
9744 */
9745 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9746 iocbq->sli4_xritag);
9747 } else {
9748 /* ABTS sent by responder to CT exchange, the
9749 * RX_ID field will be filled with the responder
9750 * RX_ID from ABTS.
9751 */
9752 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9753 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9754 }
6669f9bb
JS
9755 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9756 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9757
9758 /* Use CT=VPI */
9759 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9760 ndlp->nlp_DID);
9761 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9762 iocbq->iocb.ulpContext);
9763 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9764 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9765 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9766 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9767 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9768 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9769 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9770 command_type = OTHER_COMMAND;
546fc854
JS
9771 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9772 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9773 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9774 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9775 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9776 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9777 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9778 }
9779
7851fe2c 9780 break;
ae9e28f3
JS
9781 case CMD_SEND_FRAME:
9782 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9783 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9784 return 0;
4f774513
JS
9785 case CMD_XRI_ABORTED_CX:
9786 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9787 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9788 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9789 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9790 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9791 default:
9792 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9793 "2014 Invalid command 0x%x\n",
9794 iocbq->iocb.ulpCommand);
9795 return IOCB_ERROR;
7851fe2c 9796 break;
4f774513 9797 }
6d368e53 9798
8012cc38
JS
9799 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9800 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9801 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9802 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9803 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9804 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9805 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9806 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9807 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9808 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9809 wqe->generic.wqe_com.abort_tag = abort_tag;
9810 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9811 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9812 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9813 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9814 return 0;
9815}
9816
9817/**
9818 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9819 * @phba: Pointer to HBA context object.
9820 * @ring_number: SLI ring number to issue iocb on.
9821 * @piocb: Pointer to command iocb.
9822 * @flag: Flag indicating if this command can be put into txq.
9823 *
9824 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9825 * an iocb command to an HBA with SLI-4 interface spec.
9826 *
9827 * This function is called with hbalock held. The function will return success
9828 * after it successfully submit the iocb to firmware or after adding to the
9829 * txq.
9830 **/
9831static int
9832__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9833 struct lpfc_iocbq *piocb, uint32_t flag)
9834{
9835 struct lpfc_sglq *sglq;
205e8240 9836 union lpfc_wqe128 wqe;
1ba981fd 9837 struct lpfc_queue *wq;
895427bd 9838 struct lpfc_sli_ring *pring;
4f774513 9839
895427bd
JS
9840 /* Get the WQ */
9841 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9842 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9843 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9844 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9845 else
9846 wq = phba->sli4_hba.oas_wq;
9847 } else {
9848 wq = phba->sli4_hba.els_wq;
9849 }
9850
9851 /* Get corresponding ring */
9852 pring = wq->pring;
1c2ba475 9853
b5c53958
JS
9854 /*
9855 * The WQE can be either 64 or 128 bytes,
b5c53958 9856 */
b5c53958 9857
895427bd
JS
9858 lockdep_assert_held(&phba->hbalock);
9859
4f774513
JS
9860 if (piocb->sli4_xritag == NO_XRI) {
9861 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9862 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9863 sglq = NULL;
9864 else {
0e9bb8d7 9865 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9866 if (!(flag & SLI_IOCB_RET_IOCB)) {
9867 __lpfc_sli_ringtx_put(phba,
9868 pring, piocb);
9869 return IOCB_SUCCESS;
9870 } else {
9871 return IOCB_BUSY;
9872 }
9873 } else {
895427bd 9874 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9875 if (!sglq) {
9876 if (!(flag & SLI_IOCB_RET_IOCB)) {
9877 __lpfc_sli_ringtx_put(phba,
9878 pring,
9879 piocb);
9880 return IOCB_SUCCESS;
9881 } else
9882 return IOCB_BUSY;
9883 }
9884 }
4f774513 9885 }
2ea259ee 9886 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9887 /* These IO's already have an XRI and a mapped sgl. */
9888 sglq = NULL;
2ea259ee 9889 else {
6d368e53
JS
9890 /*
9891 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9892 * sglq is on the active list
9893 */
edccdc17 9894 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9895 if (!sglq)
9896 return IOCB_ERROR;
9897 }
9898
9899 if (sglq) {
6d368e53 9900 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9901 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9902 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9903 return IOCB_ERROR;
9904 }
9905
205e8240 9906 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
4f774513
JS
9907 return IOCB_ERROR;
9908
205e8240 9909 if (lpfc_sli4_wq_put(wq, &wqe))
895427bd 9910 return IOCB_ERROR;
4f774513
JS
9911 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9912
9913 return 0;
9914}
9915
9916/**
9917 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9918 *
9919 * This routine wraps the actual lockless version for issusing IOCB function
9920 * pointer from the lpfc_hba struct.
9921 *
9922 * Return codes:
b5c53958
JS
9923 * IOCB_ERROR - Error
9924 * IOCB_SUCCESS - Success
9925 * IOCB_BUSY - Busy
4f774513 9926 **/
2a9bf3d0 9927int
4f774513
JS
9928__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9929 struct lpfc_iocbq *piocb, uint32_t flag)
9930{
9931 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9932}
9933
9934/**
25985edc 9935 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9936 * @phba: The hba struct for which this call is being executed.
9937 * @dev_grp: The HBA PCI-Device group number.
9938 *
9939 * This routine sets up the SLI interface API function jump table in @phba
9940 * struct.
9941 * Returns: 0 - success, -ENODEV - failure.
9942 **/
9943int
9944lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9945{
9946
9947 switch (dev_grp) {
9948 case LPFC_PCI_DEV_LP:
9949 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9950 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9951 break;
9952 case LPFC_PCI_DEV_OC:
9953 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9954 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9955 break;
9956 default:
9957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9958 "1419 Invalid HBA PCI-device group: 0x%x\n",
9959 dev_grp);
9960 return -ENODEV;
9961 break;
9962 }
9963 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9964 return 0;
9965}
9966
a1efe163 9967/**
895427bd 9968 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9969 * @phba: Pointer to HBA context object.
a1efe163
JS
9970 * @piocb: Pointer to command iocb.
9971 *
895427bd
JS
9972 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9973 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9974 * Since ABORTS must go on the same WQ of the command they are
895427bd 9975 * aborting, we use command's hba_wqidx.
a1efe163 9976 */
895427bd
JS
9977struct lpfc_sli_ring *
9978lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9979{
895427bd 9980 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9981 if (!(phba->cfg_fof) ||
895427bd 9982 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9983 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9984 return NULL;
8b0dff14 9985 /*
895427bd 9986 * for abort iocb hba_wqidx should already
8b0dff14
JS
9987 * be setup based on what work queue we used.
9988 */
8e036a94 9989 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9990 piocb->hba_wqidx =
8b0dff14
JS
9991 lpfc_sli4_scmd_to_wqidx_distr(phba,
9992 piocb->context1);
8e036a94
DK
9993 piocb->hba_wqidx = piocb->hba_wqidx %
9994 phba->cfg_fcp_io_channel;
9995 }
895427bd 9996 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9997 } else {
9998 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9999 return NULL;
10000 piocb->hba_wqidx = 0;
10001 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 10002 }
895427bd
JS
10003 } else {
10004 if (unlikely(!phba->sli4_hba.els_wq))
10005 return NULL;
10006 piocb->hba_wqidx = 0;
10007 return phba->sli4_hba.els_wq->pring;
9bd2bff5 10008 }
9bd2bff5
JS
10009}
10010
4f774513
JS
10011/**
10012 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
10013 * @phba: Pointer to HBA context object.
10014 * @pring: Pointer to driver SLI ring object.
10015 * @piocb: Pointer to command iocb.
10016 * @flag: Flag indicating if this command can be put into txq.
10017 *
10018 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10019 * function. This function gets the hbalock and calls
10020 * __lpfc_sli_issue_iocb function and will return the error returned
10021 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10022 * functions which do not hold hbalock.
10023 **/
10024int
10025lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10026 struct lpfc_iocbq *piocb, uint32_t flag)
10027{
895427bd 10028 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 10029 struct lpfc_sli_ring *pring;
ba20c853
JS
10030 struct lpfc_queue *fpeq;
10031 struct lpfc_eqe *eqe;
4f774513 10032 unsigned long iflags;
2a76a283 10033 int rc, idx;
4f774513 10034
7e56aa25 10035 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10036 pring = lpfc_sli4_calc_ring(phba, piocb);
10037 if (unlikely(pring == NULL))
9bd2bff5 10038 return IOCB_ERROR;
ba20c853 10039
9bd2bff5
JS
10040 spin_lock_irqsave(&pring->ring_lock, iflags);
10041 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10042 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 10043
9bd2bff5 10044 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
10045 idx = piocb->hba_wqidx;
10046 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 10047
895427bd 10048 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 10049
9bd2bff5
JS
10050 /* Get associated EQ with this index */
10051 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 10052
9bd2bff5 10053 /* Turn off interrupts from this EQ */
b71413dd 10054 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 10055
9bd2bff5
JS
10056 /*
10057 * Process all the events on FCP EQ
10058 */
10059 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
10060 lpfc_sli4_hba_handle_eqe(phba,
10061 eqe, idx);
10062 fpeq->EQ_processed++;
ba20c853 10063 }
ba20c853 10064
9bd2bff5 10065 /* Always clear and re-arm the EQ */
b71413dd 10066 phba->sli4_hba.sli4_eq_release(fpeq,
9bd2bff5
JS
10067 LPFC_QUEUE_REARM);
10068 }
895427bd 10069 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 10070 }
7e56aa25
JS
10071 } else {
10072 /* For now, SLI2/3 will still use hbalock */
10073 spin_lock_irqsave(&phba->hbalock, iflags);
10074 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10075 spin_unlock_irqrestore(&phba->hbalock, iflags);
10076 }
4f774513
JS
10077 return rc;
10078}
10079
10080/**
10081 * lpfc_extra_ring_setup - Extra ring setup function
10082 * @phba: Pointer to HBA context object.
10083 *
10084 * This function is called while driver attaches with the
10085 * HBA to setup the extra ring. The extra ring is used
10086 * only when driver needs to support target mode functionality
10087 * or IP over FC functionalities.
10088 *
895427bd 10089 * This function is called with no lock held. SLI3 only.
4f774513
JS
10090 **/
10091static int
10092lpfc_extra_ring_setup( struct lpfc_hba *phba)
10093{
10094 struct lpfc_sli *psli;
10095 struct lpfc_sli_ring *pring;
10096
10097 psli = &phba->sli;
10098
10099 /* Adjust cmd/rsp ring iocb entries more evenly */
10100
10101 /* Take some away from the FCP ring */
895427bd 10102 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
10103 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10104 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10105 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10106 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 10107
a4bc3379 10108 /* and give them to the extra ring */
895427bd 10109 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 10110
7e56aa25
JS
10111 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10112 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10113 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10114 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
10115
10116 /* Setup default profile for this ring */
10117 pring->iotag_max = 4096;
10118 pring->num_mask = 1;
10119 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
10120 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10121 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
10122 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10123 return 0;
10124}
10125
cb69f7de
JS
10126/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10127 * @phba: Pointer to HBA context object.
10128 * @iocbq: Pointer to iocb object.
10129 *
10130 * The async_event handler calls this routine when it receives
10131 * an ASYNC_STATUS_CN event from the port. The port generates
10132 * this event when an Abort Sequence request to an rport fails
10133 * twice in succession. The abort could be originated by the
10134 * driver or by the port. The ABTS could have been for an ELS
10135 * or FCP IO. The port only generates this event when an ABTS
10136 * fails to complete after one retry.
10137 */
10138static void
10139lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10140 struct lpfc_iocbq *iocbq)
10141{
10142 struct lpfc_nodelist *ndlp = NULL;
10143 uint16_t rpi = 0, vpi = 0;
10144 struct lpfc_vport *vport = NULL;
10145
10146 /* The rpi in the ulpContext is vport-sensitive. */
10147 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10148 rpi = iocbq->iocb.ulpContext;
10149
10150 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10151 "3092 Port generated ABTS async event "
10152 "on vpi %d rpi %d status 0x%x\n",
10153 vpi, rpi, iocbq->iocb.ulpStatus);
10154
10155 vport = lpfc_find_vport_by_vpid(phba, vpi);
10156 if (!vport)
10157 goto err_exit;
10158 ndlp = lpfc_findnode_rpi(vport, rpi);
10159 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10160 goto err_exit;
10161
10162 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10163 lpfc_sli_abts_recover_port(vport, ndlp);
10164 return;
10165
10166 err_exit:
10167 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10168 "3095 Event Context not found, no "
10169 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10170 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10171 vpi, rpi);
10172}
10173
10174/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10175 * @phba: pointer to HBA context object.
10176 * @ndlp: nodelist pointer for the impacted rport.
10177 * @axri: pointer to the wcqe containing the failed exchange.
10178 *
10179 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10180 * port. The port generates this event when an abort exchange request to an
10181 * rport fails twice in succession with no reply. The abort could be originated
10182 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10183 */
10184void
10185lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10186 struct lpfc_nodelist *ndlp,
10187 struct sli4_wcqe_xri_aborted *axri)
10188{
10189 struct lpfc_vport *vport;
5c1db2ac 10190 uint32_t ext_status = 0;
cb69f7de 10191
6b5151fd 10192 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
10193 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10194 "3115 Node Context not found, driver "
10195 "ignoring abts err event\n");
6b5151fd
JS
10196 return;
10197 }
10198
cb69f7de
JS
10199 vport = ndlp->vport;
10200 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10201 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 10202 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 10203 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 10204 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
10205 bf_get(lpfc_wcqe_xa_status, axri),
10206 axri->parameter);
cb69f7de 10207
5c1db2ac
JS
10208 /*
10209 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10210 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10211 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10212 */
e3d2b802 10213 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
10214 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10215 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
10216 lpfc_sli_abts_recover_port(vport, ndlp);
10217}
10218
e59058c4 10219/**
3621a710 10220 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
10221 * @phba: Pointer to HBA context object.
10222 * @pring: Pointer to driver SLI ring object.
10223 * @iocbq: Pointer to iocb object.
10224 *
10225 * This function is called by the slow ring event handler
10226 * function when there is an ASYNC event iocb in the ring.
10227 * This function is called with no lock held.
10228 * Currently this function handles only temperature related
10229 * ASYNC events. The function decodes the temperature sensor
10230 * event message and posts events for the management applications.
10231 **/
98c9ea5c 10232static void
57127f15
JS
10233lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10234 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10235{
10236 IOCB_t *icmd;
10237 uint16_t evt_code;
57127f15
JS
10238 struct temp_event temp_event_data;
10239 struct Scsi_Host *shost;
a257bf90 10240 uint32_t *iocb_w;
57127f15
JS
10241
10242 icmd = &iocbq->iocb;
10243 evt_code = icmd->un.asyncstat.evt_code;
57127f15 10244
cb69f7de
JS
10245 switch (evt_code) {
10246 case ASYNC_TEMP_WARN:
10247 case ASYNC_TEMP_SAFE:
10248 temp_event_data.data = (uint32_t) icmd->ulpContext;
10249 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10250 if (evt_code == ASYNC_TEMP_WARN) {
10251 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10252 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10253 "0347 Adapter is very hot, please take "
10254 "corrective action. temperature : %d Celsius\n",
10255 (uint32_t) icmd->ulpContext);
10256 } else {
10257 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10258 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10259 "0340 Adapter temperature is OK now. "
10260 "temperature : %d Celsius\n",
10261 (uint32_t) icmd->ulpContext);
10262 }
10263
10264 /* Send temperature change event to applications */
10265 shost = lpfc_shost_from_vport(phba->pport);
10266 fc_host_post_vendor_event(shost, fc_get_event_number(),
10267 sizeof(temp_event_data), (char *) &temp_event_data,
10268 LPFC_NL_VENDOR_ID);
10269 break;
10270 case ASYNC_STATUS_CN:
10271 lpfc_sli_abts_err_handler(phba, iocbq);
10272 break;
10273 default:
a257bf90 10274 iocb_w = (uint32_t *) icmd;
cb69f7de 10275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 10276 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 10277 " evt_code 0x%x\n"
a257bf90
JS
10278 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10279 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10280 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10281 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 10282 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
10283 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10284 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10285 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10286 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10287
cb69f7de 10288 break;
57127f15 10289 }
57127f15
JS
10290}
10291
10292
e59058c4 10293/**
895427bd 10294 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
10295 * @phba: Pointer to HBA context object.
10296 *
10297 * lpfc_sli_setup sets up rings of the SLI interface with
10298 * number of iocbs per ring and iotags. This function is
10299 * called while driver attach to the HBA and before the
10300 * interrupts are enabled. So there is no need for locking.
10301 *
10302 * This function always returns 0.
10303 **/
dea3101e 10304int
895427bd
JS
10305lpfc_sli4_setup(struct lpfc_hba *phba)
10306{
10307 struct lpfc_sli_ring *pring;
10308
10309 pring = phba->sli4_hba.els_wq->pring;
10310 pring->num_mask = LPFC_MAX_RING_MASK;
10311 pring->prt[0].profile = 0; /* Mask 0 */
10312 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10313 pring->prt[0].type = FC_TYPE_ELS;
10314 pring->prt[0].lpfc_sli_rcv_unsol_event =
10315 lpfc_els_unsol_event;
10316 pring->prt[1].profile = 0; /* Mask 1 */
10317 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10318 pring->prt[1].type = FC_TYPE_ELS;
10319 pring->prt[1].lpfc_sli_rcv_unsol_event =
10320 lpfc_els_unsol_event;
10321 pring->prt[2].profile = 0; /* Mask 2 */
10322 /* NameServer Inquiry */
10323 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10324 /* NameServer */
10325 pring->prt[2].type = FC_TYPE_CT;
10326 pring->prt[2].lpfc_sli_rcv_unsol_event =
10327 lpfc_ct_unsol_event;
10328 pring->prt[3].profile = 0; /* Mask 3 */
10329 /* NameServer response */
10330 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10331 /* NameServer */
10332 pring->prt[3].type = FC_TYPE_CT;
10333 pring->prt[3].lpfc_sli_rcv_unsol_event =
10334 lpfc_ct_unsol_event;
10335 return 0;
10336}
10337
10338/**
10339 * lpfc_sli_setup - SLI ring setup function
10340 * @phba: Pointer to HBA context object.
10341 *
10342 * lpfc_sli_setup sets up rings of the SLI interface with
10343 * number of iocbs per ring and iotags. This function is
10344 * called while driver attach to the HBA and before the
10345 * interrupts are enabled. So there is no need for locking.
10346 *
10347 * This function always returns 0. SLI3 only.
10348 **/
10349int
dea3101e
JB
10350lpfc_sli_setup(struct lpfc_hba *phba)
10351{
ed957684 10352 int i, totiocbsize = 0;
dea3101e
JB
10353 struct lpfc_sli *psli = &phba->sli;
10354 struct lpfc_sli_ring *pring;
10355
2a76a283 10356 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 10357 psli->sli_flag = 0;
dea3101e 10358
604a3e30
JB
10359 psli->iocbq_lookup = NULL;
10360 psli->iocbq_lookup_len = 0;
10361 psli->last_iotag = 0;
10362
dea3101e 10363 for (i = 0; i < psli->num_rings; i++) {
895427bd 10364 pring = &psli->sli3_ring[i];
dea3101e
JB
10365 switch (i) {
10366 case LPFC_FCP_RING: /* ring 0 - FCP */
10367 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10368 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10369 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10370 pring->sli.sli3.numCiocb +=
10371 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10372 pring->sli.sli3.numRiocb +=
10373 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10374 pring->sli.sli3.numCiocb +=
10375 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10376 pring->sli.sli3.numRiocb +=
10377 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10378 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10379 SLI3_IOCB_CMD_SIZE :
10380 SLI2_IOCB_CMD_SIZE;
7e56aa25 10381 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10382 SLI3_IOCB_RSP_SIZE :
10383 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
10384 pring->iotag_ctr = 0;
10385 pring->iotag_max =
92d7f7b0 10386 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
10387 pring->fast_iotag = pring->iotag_max;
10388 pring->num_mask = 0;
10389 break;
a4bc3379 10390 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 10391 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10392 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10393 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10394 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10395 SLI3_IOCB_CMD_SIZE :
10396 SLI2_IOCB_CMD_SIZE;
7e56aa25 10397 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10398 SLI3_IOCB_RSP_SIZE :
10399 SLI2_IOCB_RSP_SIZE;
2e0fef85 10400 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
10401 pring->num_mask = 0;
10402 break;
10403 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10404 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10405 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10406 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10407 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10408 SLI3_IOCB_CMD_SIZE :
10409 SLI2_IOCB_CMD_SIZE;
7e56aa25 10410 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10411 SLI3_IOCB_RSP_SIZE :
10412 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
10413 pring->fast_iotag = 0;
10414 pring->iotag_ctr = 0;
10415 pring->iotag_max = 4096;
57127f15
JS
10416 pring->lpfc_sli_rcv_async_status =
10417 lpfc_sli_async_event_handler;
6669f9bb 10418 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 10419 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
10420 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10421 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 10422 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 10423 lpfc_els_unsol_event;
dea3101e 10424 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
10425 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10426 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 10427 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 10428 lpfc_els_unsol_event;
dea3101e
JB
10429 pring->prt[2].profile = 0; /* Mask 2 */
10430 /* NameServer Inquiry */
6a9c52cf 10431 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 10432 /* NameServer */
6a9c52cf 10433 pring->prt[2].type = FC_TYPE_CT;
dea3101e 10434 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 10435 lpfc_ct_unsol_event;
dea3101e
JB
10436 pring->prt[3].profile = 0; /* Mask 3 */
10437 /* NameServer response */
6a9c52cf 10438 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 10439 /* NameServer */
6a9c52cf 10440 pring->prt[3].type = FC_TYPE_CT;
dea3101e 10441 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 10442 lpfc_ct_unsol_event;
dea3101e
JB
10443 break;
10444 }
7e56aa25
JS
10445 totiocbsize += (pring->sli.sli3.numCiocb *
10446 pring->sli.sli3.sizeCiocb) +
10447 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 10448 }
ed957684 10449 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 10450 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
10451 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10452 "SLI2 SLIM Data: x%x x%lx\n",
10453 phba->brd_no, totiocbsize,
10454 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 10455 }
cf5bf97e
JW
10456 if (phba->cfg_multi_ring_support == 2)
10457 lpfc_extra_ring_setup(phba);
dea3101e
JB
10458
10459 return 0;
10460}
10461
e59058c4 10462/**
895427bd 10463 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
10464 * @phba: Pointer to HBA context object.
10465 *
895427bd 10466 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
10467 * ring. This function also initializes ring indices of each ring.
10468 * This function is called during the initialization of the SLI
10469 * interface of an HBA.
10470 * This function is called with no lock held and always returns
10471 * 1.
10472 **/
895427bd
JS
10473void
10474lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10475{
10476 struct lpfc_sli *psli;
10477 struct lpfc_sli_ring *pring;
604a3e30 10478 int i;
dea3101e
JB
10479
10480 psli = &phba->sli;
2e0fef85 10481 spin_lock_irq(&phba->hbalock);
dea3101e 10482 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10483 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 10484 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
10485 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10486 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 10487 pring->flag = 0;
895427bd 10488 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
10489 INIT_LIST_HEAD(&pring->txq);
10490 INIT_LIST_HEAD(&pring->txcmplq);
10491 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 10492 spin_lock_init(&pring->ring_lock);
dea3101e 10493 }
895427bd
JS
10494 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10495 pring = phba->sli4_hba.nvme_wq[i]->pring;
10496 pring->flag = 0;
10497 pring->ringno = LPFC_FCP_RING;
10498 INIT_LIST_HEAD(&pring->txq);
10499 INIT_LIST_HEAD(&pring->txcmplq);
10500 INIT_LIST_HEAD(&pring->iocb_continueq);
10501 spin_lock_init(&pring->ring_lock);
10502 }
10503 pring = phba->sli4_hba.els_wq->pring;
10504 pring->flag = 0;
10505 pring->ringno = LPFC_ELS_RING;
10506 INIT_LIST_HEAD(&pring->txq);
10507 INIT_LIST_HEAD(&pring->txcmplq);
10508 INIT_LIST_HEAD(&pring->iocb_continueq);
10509 spin_lock_init(&pring->ring_lock);
dea3101e 10510
895427bd
JS
10511 if (phba->cfg_nvme_io_channel) {
10512 pring = phba->sli4_hba.nvmels_wq->pring;
10513 pring->flag = 0;
10514 pring->ringno = LPFC_ELS_RING;
10515 INIT_LIST_HEAD(&pring->txq);
10516 INIT_LIST_HEAD(&pring->txcmplq);
10517 INIT_LIST_HEAD(&pring->iocb_continueq);
10518 spin_lock_init(&pring->ring_lock);
10519 }
10520
10521 if (phba->cfg_fof) {
10522 pring = phba->sli4_hba.oas_wq->pring;
10523 pring->flag = 0;
10524 pring->ringno = LPFC_FCP_RING;
10525 INIT_LIST_HEAD(&pring->txq);
10526 INIT_LIST_HEAD(&pring->txcmplq);
10527 INIT_LIST_HEAD(&pring->iocb_continueq);
10528 spin_lock_init(&pring->ring_lock);
10529 }
10530
10531 spin_unlock_irq(&phba->hbalock);
10532}
10533
10534/**
10535 * lpfc_sli_queue_init - Queue initialization function
10536 * @phba: Pointer to HBA context object.
10537 *
10538 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10539 * ring. This function also initializes ring indices of each ring.
10540 * This function is called during the initialization of the SLI
10541 * interface of an HBA.
10542 * This function is called with no lock held and always returns
10543 * 1.
10544 **/
10545void
10546lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10547{
10548 struct lpfc_sli *psli;
10549 struct lpfc_sli_ring *pring;
604a3e30 10550 int i;
dea3101e
JB
10551
10552 psli = &phba->sli;
2e0fef85 10553 spin_lock_irq(&phba->hbalock);
dea3101e 10554 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10555 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
10556 /* Initialize list headers for txq and txcmplq as double linked lists */
10557 for (i = 0; i < psli->num_rings; i++) {
895427bd 10558 pring = &psli->sli3_ring[i];
dea3101e 10559 pring->ringno = i;
7e56aa25
JS
10560 pring->sli.sli3.next_cmdidx = 0;
10561 pring->sli.sli3.local_getidx = 0;
10562 pring->sli.sli3.cmdidx = 0;
dea3101e 10563 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 10564 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 10565 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
10566 pring->flag = 0;
10567 INIT_LIST_HEAD(&pring->txq);
10568 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 10569 spin_lock_init(&pring->ring_lock);
dea3101e 10570 }
2e0fef85 10571 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10572}
10573
04c68496
JS
10574/**
10575 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10576 * @phba: Pointer to HBA context object.
10577 *
10578 * This routine flushes the mailbox command subsystem. It will unconditionally
10579 * flush all the mailbox commands in the three possible stages in the mailbox
10580 * command sub-system: pending mailbox command queue; the outstanding mailbox
10581 * command; and completed mailbox command queue. It is caller's responsibility
10582 * to make sure that the driver is in the proper state to flush the mailbox
10583 * command sub-system. Namely, the posting of mailbox commands into the
10584 * pending mailbox command queue from the various clients must be stopped;
10585 * either the HBA is in a state that it will never works on the outstanding
10586 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10587 * mailbox command has been completed.
10588 **/
10589static void
10590lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10591{
10592 LIST_HEAD(completions);
10593 struct lpfc_sli *psli = &phba->sli;
10594 LPFC_MBOXQ_t *pmb;
10595 unsigned long iflag;
10596
523128e5
JS
10597 /* Disable softirqs, including timers from obtaining phba->hbalock */
10598 local_bh_disable();
10599
04c68496
JS
10600 /* Flush all the mailbox commands in the mbox system */
10601 spin_lock_irqsave(&phba->hbalock, iflag);
523128e5 10602
04c68496
JS
10603 /* The pending mailbox command queue */
10604 list_splice_init(&phba->sli.mboxq, &completions);
10605 /* The outstanding active mailbox command */
10606 if (psli->mbox_active) {
10607 list_add_tail(&psli->mbox_active->list, &completions);
10608 psli->mbox_active = NULL;
10609 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10610 }
10611 /* The completed mailbox command queue */
10612 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10613 spin_unlock_irqrestore(&phba->hbalock, iflag);
10614
523128e5
JS
10615 /* Enable softirqs again, done with phba->hbalock */
10616 local_bh_enable();
10617
04c68496
JS
10618 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10619 while (!list_empty(&completions)) {
10620 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10621 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10622 if (pmb->mbox_cmpl)
10623 pmb->mbox_cmpl(phba, pmb);
10624 }
10625}
10626
e59058c4 10627/**
3621a710 10628 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10629 * @vport: Pointer to virtual port object.
10630 *
10631 * lpfc_sli_host_down is called to clean up the resources
10632 * associated with a vport before destroying virtual
10633 * port data structures.
10634 * This function does following operations:
10635 * - Free discovery resources associated with this virtual
10636 * port.
10637 * - Free iocbs associated with this virtual port in
10638 * the txq.
10639 * - Send abort for all iocb commands associated with this
10640 * vport in txcmplq.
10641 *
10642 * This function is called with no lock held and always returns 1.
10643 **/
92d7f7b0
JS
10644int
10645lpfc_sli_host_down(struct lpfc_vport *vport)
10646{
858c9f6c 10647 LIST_HEAD(completions);
92d7f7b0
JS
10648 struct lpfc_hba *phba = vport->phba;
10649 struct lpfc_sli *psli = &phba->sli;
895427bd 10650 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10651 struct lpfc_sli_ring *pring;
10652 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10653 int i;
10654 unsigned long flags = 0;
10655 uint16_t prev_pring_flag;
10656
10657 lpfc_cleanup_discovery_resources(vport);
10658
10659 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10660
895427bd
JS
10661 /*
10662 * Error everything on the txq since these iocbs
10663 * have not been given to the FW yet.
10664 * Also issue ABTS for everything on the txcmplq
10665 */
10666 if (phba->sli_rev != LPFC_SLI_REV4) {
10667 for (i = 0; i < psli->num_rings; i++) {
10668 pring = &psli->sli3_ring[i];
10669 prev_pring_flag = pring->flag;
10670 /* Only slow rings */
10671 if (pring->ringno == LPFC_ELS_RING) {
10672 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10673 /* Set the lpfc data pending flag */
10674 set_bit(LPFC_DATA_READY, &phba->data_flags);
10675 }
10676 list_for_each_entry_safe(iocb, next_iocb,
10677 &pring->txq, list) {
10678 if (iocb->vport != vport)
10679 continue;
10680 list_move_tail(&iocb->list, &completions);
10681 }
10682 list_for_each_entry_safe(iocb, next_iocb,
10683 &pring->txcmplq, list) {
10684 if (iocb->vport != vport)
10685 continue;
10686 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10687 }
10688 pring->flag = prev_pring_flag;
10689 }
10690 } else {
10691 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10692 pring = qp->pring;
10693 if (!pring)
92d7f7b0 10694 continue;
895427bd
JS
10695 if (pring == phba->sli4_hba.els_wq->pring) {
10696 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10697 /* Set the lpfc data pending flag */
10698 set_bit(LPFC_DATA_READY, &phba->data_flags);
10699 }
10700 prev_pring_flag = pring->flag;
10701 spin_lock_irq(&pring->ring_lock);
10702 list_for_each_entry_safe(iocb, next_iocb,
10703 &pring->txq, list) {
10704 if (iocb->vport != vport)
10705 continue;
10706 list_move_tail(&iocb->list, &completions);
10707 }
10708 spin_unlock_irq(&pring->ring_lock);
10709 list_for_each_entry_safe(iocb, next_iocb,
10710 &pring->txcmplq, list) {
10711 if (iocb->vport != vport)
10712 continue;
10713 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10714 }
10715 pring->flag = prev_pring_flag;
92d7f7b0 10716 }
92d7f7b0 10717 }
92d7f7b0
JS
10718 spin_unlock_irqrestore(&phba->hbalock, flags);
10719
a257bf90
JS
10720 /* Cancel all the IOCBs from the completions list */
10721 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10722 IOERR_SLI_DOWN);
92d7f7b0
JS
10723 return 1;
10724}
10725
e59058c4 10726/**
3621a710 10727 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10728 * @phba: Pointer to HBA context object.
10729 *
10730 * This function cleans up all iocb, buffers, mailbox commands
10731 * while shutting down the HBA. This function is called with no
10732 * lock held and always returns 1.
10733 * This function does the following to cleanup driver resources:
10734 * - Free discovery resources for each virtual port
10735 * - Cleanup any pending fabric iocbs
10736 * - Iterate through the iocb txq and free each entry
10737 * in the list.
10738 * - Free up any buffer posted to the HBA
10739 * - Free mailbox commands in the mailbox queue.
10740 **/
dea3101e 10741int
2e0fef85 10742lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10743{
2534ba75 10744 LIST_HEAD(completions);
2e0fef85 10745 struct lpfc_sli *psli = &phba->sli;
895427bd 10746 struct lpfc_queue *qp = NULL;
dea3101e 10747 struct lpfc_sli_ring *pring;
0ff10d46 10748 struct lpfc_dmabuf *buf_ptr;
dea3101e 10749 unsigned long flags = 0;
04c68496
JS
10750 int i;
10751
10752 /* Shutdown the mailbox command sub-system */
618a5230 10753 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10754
dea3101e
JB
10755 lpfc_hba_down_prep(phba);
10756
523128e5
JS
10757 /* Disable softirqs, including timers from obtaining phba->hbalock */
10758 local_bh_disable();
10759
92d7f7b0
JS
10760 lpfc_fabric_abort_hba(phba);
10761
2e0fef85 10762 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10763
895427bd
JS
10764 /*
10765 * Error everything on the txq since these iocbs
10766 * have not been given to the FW yet.
10767 */
10768 if (phba->sli_rev != LPFC_SLI_REV4) {
10769 for (i = 0; i < psli->num_rings; i++) {
10770 pring = &psli->sli3_ring[i];
10771 /* Only slow rings */
10772 if (pring->ringno == LPFC_ELS_RING) {
10773 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10774 /* Set the lpfc data pending flag */
10775 set_bit(LPFC_DATA_READY, &phba->data_flags);
10776 }
10777 list_splice_init(&pring->txq, &completions);
10778 }
10779 } else {
10780 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10781 pring = qp->pring;
10782 if (!pring)
10783 continue;
10784 spin_lock_irq(&pring->ring_lock);
10785 list_splice_init(&pring->txq, &completions);
10786 spin_unlock_irq(&pring->ring_lock);
10787 if (pring == phba->sli4_hba.els_wq->pring) {
10788 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10789 /* Set the lpfc data pending flag */
10790 set_bit(LPFC_DATA_READY, &phba->data_flags);
10791 }
10792 }
2534ba75 10793 }
2e0fef85 10794 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10795
a257bf90
JS
10796 /* Cancel all the IOCBs from the completions list */
10797 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10798 IOERR_SLI_DOWN);
dea3101e 10799
0ff10d46
JS
10800 spin_lock_irqsave(&phba->hbalock, flags);
10801 list_splice_init(&phba->elsbuf, &completions);
10802 phba->elsbuf_cnt = 0;
10803 phba->elsbuf_prev_cnt = 0;
10804 spin_unlock_irqrestore(&phba->hbalock, flags);
10805
10806 while (!list_empty(&completions)) {
10807 list_remove_head(&completions, buf_ptr,
10808 struct lpfc_dmabuf, list);
10809 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10810 kfree(buf_ptr);
10811 }
10812
523128e5
JS
10813 /* Enable softirqs again, done with phba->hbalock */
10814 local_bh_enable();
10815
dea3101e
JB
10816 /* Return any active mbox cmds */
10817 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10818
da0436e9 10819 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10820 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10821 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10822
da0436e9
JS
10823 return 1;
10824}
10825
e59058c4 10826/**
3621a710 10827 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10828 * @srcp: Source memory pointer.
10829 * @destp: Destination memory pointer.
10830 * @cnt: Number of words required to be copied.
10831 *
10832 * This function is used for copying data between driver memory
10833 * and the SLI memory. This function also changes the endianness
10834 * of each word if native endianness is different from SLI
10835 * endianness. This function can be called with or without
10836 * lock.
10837 **/
dea3101e
JB
10838void
10839lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10840{
10841 uint32_t *src = srcp;
10842 uint32_t *dest = destp;
10843 uint32_t ldata;
10844 int i;
10845
10846 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10847 ldata = *src;
10848 ldata = le32_to_cpu(ldata);
10849 *dest = ldata;
10850 src++;
10851 dest++;
10852 }
10853}
10854
e59058c4 10855
a0c87cbd
JS
10856/**
10857 * lpfc_sli_bemem_bcopy - SLI memory copy function
10858 * @srcp: Source memory pointer.
10859 * @destp: Destination memory pointer.
10860 * @cnt: Number of words required to be copied.
10861 *
10862 * This function is used for copying data between a data structure
10863 * with big endian representation to local endianness.
10864 * This function can be called with or without lock.
10865 **/
10866void
10867lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10868{
10869 uint32_t *src = srcp;
10870 uint32_t *dest = destp;
10871 uint32_t ldata;
10872 int i;
10873
10874 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10875 ldata = *src;
10876 ldata = be32_to_cpu(ldata);
10877 *dest = ldata;
10878 src++;
10879 dest++;
10880 }
10881}
10882
e59058c4 10883/**
3621a710 10884 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10885 * @phba: Pointer to HBA context object.
10886 * @pring: Pointer to driver SLI ring object.
10887 * @mp: Pointer to driver buffer object.
10888 *
10889 * This function is called with no lock held.
10890 * It always return zero after adding the buffer to the postbufq
10891 * buffer list.
10892 **/
dea3101e 10893int
2e0fef85
JS
10894lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10895 struct lpfc_dmabuf *mp)
dea3101e
JB
10896{
10897 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10898 later */
2e0fef85 10899 spin_lock_irq(&phba->hbalock);
dea3101e 10900 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10901 pring->postbufq_cnt++;
2e0fef85 10902 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10903 return 0;
10904}
10905
e59058c4 10906/**
3621a710 10907 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10908 * @phba: Pointer to HBA context object.
10909 *
10910 * When HBQ is enabled, buffers are searched based on tags. This function
10911 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10912 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10913 * does not conflict with tags of buffer posted for unsolicited events.
10914 * The function returns the allocated tag. The function is called with
10915 * no locks held.
10916 **/
76bb24ef
JS
10917uint32_t
10918lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10919{
10920 spin_lock_irq(&phba->hbalock);
10921 phba->buffer_tag_count++;
10922 /*
10923 * Always set the QUE_BUFTAG_BIT to distiguish between
10924 * a tag assigned by HBQ.
10925 */
10926 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10927 spin_unlock_irq(&phba->hbalock);
10928 return phba->buffer_tag_count;
10929}
10930
e59058c4 10931/**
3621a710 10932 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10933 * @phba: Pointer to HBA context object.
10934 * @pring: Pointer to driver SLI ring object.
10935 * @tag: Buffer tag.
10936 *
10937 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10938 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10939 * iocb is posted to the response ring with the tag of the buffer.
10940 * This function searches the pring->postbufq list using the tag
10941 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10942 * iocb. If the buffer is found then lpfc_dmabuf object of the
10943 * buffer is returned to the caller else NULL is returned.
10944 * This function is called with no lock held.
10945 **/
76bb24ef
JS
10946struct lpfc_dmabuf *
10947lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10948 uint32_t tag)
10949{
10950 struct lpfc_dmabuf *mp, *next_mp;
10951 struct list_head *slp = &pring->postbufq;
10952
25985edc 10953 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10954 spin_lock_irq(&phba->hbalock);
10955 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10956 if (mp->buffer_tag == tag) {
10957 list_del_init(&mp->list);
10958 pring->postbufq_cnt--;
10959 spin_unlock_irq(&phba->hbalock);
10960 return mp;
10961 }
10962 }
10963
10964 spin_unlock_irq(&phba->hbalock);
10965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10966 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10967 "ring %d Data x%lx x%p x%p x%x\n",
10968 pring->ringno, (unsigned long) tag,
10969 slp->next, slp->prev, pring->postbufq_cnt);
10970
10971 return NULL;
10972}
dea3101e 10973
e59058c4 10974/**
3621a710 10975 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10976 * @phba: Pointer to HBA context object.
10977 * @pring: Pointer to driver SLI ring object.
10978 * @phys: DMA address of the buffer.
10979 *
10980 * This function searches the buffer list using the dma_address
10981 * of unsolicited event to find the driver's lpfc_dmabuf object
10982 * corresponding to the dma_address. The function returns the
10983 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10984 * This function is called by the ct and els unsolicited event
10985 * handlers to get the buffer associated with the unsolicited
10986 * event.
10987 *
10988 * This function is called with no lock held.
10989 **/
dea3101e
JB
10990struct lpfc_dmabuf *
10991lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10992 dma_addr_t phys)
10993{
10994 struct lpfc_dmabuf *mp, *next_mp;
10995 struct list_head *slp = &pring->postbufq;
10996
25985edc 10997 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10998 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10999 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11000 if (mp->phys == phys) {
11001 list_del_init(&mp->list);
11002 pring->postbufq_cnt--;
2e0fef85 11003 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
11004 return mp;
11005 }
11006 }
11007
2e0fef85 11008 spin_unlock_irq(&phba->hbalock);
dea3101e 11009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 11010 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 11011 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 11012 pring->ringno, (unsigned long long)phys,
dea3101e
JB
11013 slp->next, slp->prev, pring->postbufq_cnt);
11014 return NULL;
11015}
11016
e59058c4 11017/**
3621a710 11018 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
11019 * @phba: Pointer to HBA context object.
11020 * @cmdiocb: Pointer to driver command iocb object.
11021 * @rspiocb: Pointer to driver response iocb object.
11022 *
11023 * This function is the completion handler for the abort iocbs for
11024 * ELS commands. This function is called from the ELS ring event
11025 * handler with no lock held. This function frees memory resources
11026 * associated with the abort iocb.
11027 **/
dea3101e 11028static void
2e0fef85
JS
11029lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11030 struct lpfc_iocbq *rspiocb)
dea3101e 11031{
2e0fef85 11032 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 11033 uint16_t abort_iotag, abort_context;
ff78d8f9 11034 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
11035
11036 if (irsp->ulpStatus) {
ff78d8f9
JS
11037
11038 /*
11039 * Assume that the port already completed and returned, or
11040 * will return the iocb. Just Log the message.
11041 */
2680eeaa
JS
11042 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11043 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11044
2e0fef85 11045 spin_lock_irq(&phba->hbalock);
45ed1190 11046 if (phba->sli_rev < LPFC_SLI_REV4) {
faa832e9
JS
11047 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11048 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11049 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11050 spin_unlock_irq(&phba->hbalock);
11051 goto release_iocb;
11052 }
45ed1190
JS
11053 if (abort_iotag != 0 &&
11054 abort_iotag <= phba->sli.last_iotag)
11055 abort_iocb =
11056 phba->sli.iocbq_lookup[abort_iotag];
11057 } else
11058 /* For sli4 the abort_tag is the XRI,
11059 * so the abort routine puts the iotag of the iocb
11060 * being aborted in the context field of the abort
11061 * IOCB.
11062 */
11063 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 11064
2a9bf3d0
JS
11065 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
11066 "0327 Cannot abort els iocb %p "
11067 "with tag %x context %x, abort status %x, "
11068 "abort code %x\n",
11069 abort_iocb, abort_iotag, abort_context,
11070 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 11071
ff78d8f9 11072 spin_unlock_irq(&phba->hbalock);
2680eeaa 11073 }
faa832e9 11074release_iocb:
604a3e30 11075 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
11076 return;
11077}
11078
e59058c4 11079/**
3621a710 11080 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
11081 * @phba: Pointer to HBA context object.
11082 * @cmdiocb: Pointer to driver command iocb object.
11083 * @rspiocb: Pointer to driver response iocb object.
11084 *
11085 * The function is called from SLI ring event handler with no
11086 * lock held. This function is the completion handler for ELS commands
11087 * which are aborted. The function frees memory resources used for
11088 * the aborted ELS commands.
11089 **/
92d7f7b0
JS
11090static void
11091lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11092 struct lpfc_iocbq *rspiocb)
11093{
11094 IOCB_t *irsp = &rspiocb->iocb;
11095
11096 /* ELS cmd tag <ulpIoTag> completes */
11097 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 11098 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 11099 "x%x x%x x%x\n",
e8b62011 11100 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 11101 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
11102 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11103 lpfc_ct_free_iocb(phba, cmdiocb);
11104 else
11105 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
11106 return;
11107}
11108
e59058c4 11109/**
5af5eee7 11110 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
11111 * @phba: Pointer to HBA context object.
11112 * @pring: Pointer to driver SLI ring object.
11113 * @cmdiocb: Pointer to driver command iocb object.
11114 *
5af5eee7
JS
11115 * This function issues an abort iocb for the provided command iocb down to
11116 * the port. Other than the case the outstanding command iocb is an abort
11117 * request, this function issues abort out unconditionally. This function is
11118 * called with hbalock held. The function returns 0 when it fails due to
11119 * memory allocation failure or when the command iocb is an abort request.
e59058c4 11120 **/
5af5eee7
JS
11121static int
11122lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 11123 struct lpfc_iocbq *cmdiocb)
dea3101e 11124{
2e0fef85 11125 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 11126 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
11127 IOCB_t *icmd = NULL;
11128 IOCB_t *iabt = NULL;
5af5eee7 11129 int retval;
7e56aa25 11130 unsigned long iflags;
faa832e9 11131 struct lpfc_nodelist *ndlp;
07951076 11132
1c2ba475
JT
11133 lockdep_assert_held(&phba->hbalock);
11134
92d7f7b0
JS
11135 /*
11136 * There are certain command types we don't want to abort. And we
11137 * don't want to abort commands that are already in the process of
11138 * being aborted.
07951076
JS
11139 */
11140 icmd = &cmdiocb->iocb;
2e0fef85 11141 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
11142 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11143 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
11144 return 0;
11145
dea3101e 11146 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 11147 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
11148 if (abtsiocbp == NULL)
11149 return 0;
dea3101e 11150
07951076 11151 /* This signals the response to set the correct status
341af102 11152 * before calling the completion handler
07951076
JS
11153 */
11154 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11155
dea3101e 11156 iabt = &abtsiocbp->iocb;
07951076
JS
11157 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11158 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 11159 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 11160 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190 11161 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
faa832e9 11162 } else {
da0436e9 11163 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
faa832e9
JS
11164 if (pring->ringno == LPFC_ELS_RING) {
11165 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11166 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11167 }
11168 }
07951076
JS
11169 iabt->ulpLe = 1;
11170 iabt->ulpClass = icmd->ulpClass;
dea3101e 11171
5ffc266e 11172 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11173 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
11174 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11175 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11176 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11177 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11178
2e0fef85 11179 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
11180 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11181 else
11182 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 11183
07951076 11184 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 11185 abtsiocbp->vport = vport;
5b8bd0c9 11186
e8b62011
JS
11187 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11188 "0339 Abort xri x%x, original iotag x%x, "
11189 "abort cmd iotag x%x\n",
2a9bf3d0 11190 iabt->un.acxri.abortIoTag,
e8b62011 11191 iabt->un.acxri.abortContextTag,
2a9bf3d0 11192 abtsiocbp->iotag);
7e56aa25
JS
11193
11194 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11195 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11196 if (unlikely(pring == NULL))
9bd2bff5 11197 return 0;
7e56aa25
JS
11198 /* Note: both hbalock and ring_lock need to be set here */
11199 spin_lock_irqsave(&pring->ring_lock, iflags);
11200 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11201 abtsiocbp, 0);
11202 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11203 } else {
11204 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11205 abtsiocbp, 0);
11206 }
dea3101e 11207
d7c255b2
JS
11208 if (retval)
11209 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
11210
11211 /*
11212 * Caller to this routine should check for IOCB_ERROR
11213 * and handle it properly. This routine no longer removes
11214 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11215 */
11216 return retval;
11217}
11218
11219/**
11220 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11221 * @phba: Pointer to HBA context object.
11222 * @pring: Pointer to driver SLI ring object.
11223 * @cmdiocb: Pointer to driver command iocb object.
11224 *
11225 * This function issues an abort iocb for the provided command iocb. In case
11226 * of unloading, the abort iocb will not be issued to commands on the ELS
11227 * ring. Instead, the callback function shall be changed to those commands
11228 * so that nothing happens when them finishes. This function is called with
11229 * hbalock held. The function returns 0 when the command iocb is an abort
11230 * request.
11231 **/
11232int
11233lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11234 struct lpfc_iocbq *cmdiocb)
11235{
11236 struct lpfc_vport *vport = cmdiocb->vport;
11237 int retval = IOCB_ERROR;
11238 IOCB_t *icmd = NULL;
11239
1c2ba475
JT
11240 lockdep_assert_held(&phba->hbalock);
11241
5af5eee7
JS
11242 /*
11243 * There are certain command types we don't want to abort. And we
11244 * don't want to abort commands that are already in the process of
11245 * being aborted.
11246 */
11247 icmd = &cmdiocb->iocb;
11248 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11249 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11250 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11251 return 0;
11252
1234a6d5
DK
11253 if (!pring) {
11254 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11255 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11256 else
11257 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11258 goto abort_iotag_exit;
11259 }
11260
5af5eee7
JS
11261 /*
11262 * If we're unloading, don't abort iocb on the ELS ring, but change
11263 * the callback so that nothing happens when it finishes.
11264 */
11265 if ((vport->load_flag & FC_UNLOADING) &&
11266 (pring->ringno == LPFC_ELS_RING)) {
11267 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11268 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11269 else
11270 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11271 goto abort_iotag_exit;
11272 }
11273
11274 /* Now, we try to issue the abort to the cmdiocb out */
11275 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11276
07951076 11277abort_iotag_exit:
2e0fef85
JS
11278 /*
11279 * Caller to this routine should check for IOCB_ERROR
11280 * and handle it properly. This routine no longer removes
11281 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 11282 */
2e0fef85 11283 return retval;
dea3101e
JB
11284}
11285
895427bd
JS
11286/**
11287 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
11288 * @phba: Pointer to HBA context object.
11289 * @pring: Pointer to driver SLI ring object.
11290 * @cmdiocb: Pointer to driver command iocb object.
11291 *
11292 * This function issues an abort iocb for the provided command iocb down to
11293 * the port. Other than the case the outstanding command iocb is an abort
11294 * request, this function issues abort out unconditionally. This function is
11295 * called with hbalock held. The function returns 0 when it fails due to
11296 * memory allocation failure or when the command iocb is an abort request.
11297 **/
11298static int
11299lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11300 struct lpfc_iocbq *cmdiocb)
11301{
11302 struct lpfc_vport *vport = cmdiocb->vport;
11303 struct lpfc_iocbq *abtsiocbp;
205e8240 11304 union lpfc_wqe128 *abts_wqe;
895427bd
JS
11305 int retval;
11306
11307 /*
11308 * There are certain command types we don't want to abort. And we
11309 * don't want to abort commands that are already in the process of
11310 * being aborted.
11311 */
11312 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
11313 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
11314 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11315 return 0;
11316
11317 /* issue ABTS for this io based on iotag */
11318 abtsiocbp = __lpfc_sli_get_iocbq(phba);
11319 if (abtsiocbp == NULL)
11320 return 0;
11321
11322 /* This signals the response to set the correct status
11323 * before calling the completion handler
11324 */
11325 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11326
11327 /* Complete prepping the abort wqe and issue to the FW. */
11328 abts_wqe = &abtsiocbp->wqe;
11329 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
11330 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
11331
11332 /* Explicitly set reserved fields to zero.*/
11333 abts_wqe->abort_cmd.rsrvd4 = 0;
11334 abts_wqe->abort_cmd.rsrvd5 = 0;
11335
11336 /* WQE Common - word 6. Context is XRI tag. Set 0. */
11337 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11338 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
11339
11340 /* word 7 */
11341 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
11342 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
11343 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
11344 cmdiocb->iocb.ulpClass);
11345
11346 /* word 8 - tell the FW to abort the IO associated with this
11347 * outstanding exchange ID.
11348 */
11349 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
11350
11351 /* word 9 - this is the iotag for the abts_wqe completion. */
11352 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
11353 abtsiocbp->iotag);
11354
11355 /* word 10 */
11356 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11357 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11358 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11359
11360 /* word 11 */
11361 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11362 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11363 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11364
11365 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11366 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11367 abtsiocbp->vport = vport;
01649561 11368 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 11369 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 11370 if (retval) {
895427bd
JS
11371 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11372 "6147 Failed abts issue_wqe with status x%x "
11373 "for oxid x%x\n",
11374 retval, cmdiocb->sli4_xritag);
11375 lpfc_sli_release_iocbq(phba, abtsiocbp);
11376 return retval;
11377 }
11378
11379 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11380 "6148 Drv Abort NVME Request Issued for "
11381 "ox_id x%x on reqtag x%x\n",
11382 cmdiocb->sli4_xritag,
11383 abtsiocbp->iotag);
11384
11385 return retval;
11386}
11387
5af5eee7
JS
11388/**
11389 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11390 * @phba: pointer to lpfc HBA data structure.
11391 *
11392 * This routine will abort all pending and outstanding iocbs to an HBA.
11393 **/
11394void
11395lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11396{
11397 struct lpfc_sli *psli = &phba->sli;
11398 struct lpfc_sli_ring *pring;
895427bd 11399 struct lpfc_queue *qp = NULL;
5af5eee7
JS
11400 int i;
11401
895427bd
JS
11402 if (phba->sli_rev != LPFC_SLI_REV4) {
11403 for (i = 0; i < psli->num_rings; i++) {
11404 pring = &psli->sli3_ring[i];
11405 lpfc_sli_abort_iocb_ring(phba, pring);
11406 }
11407 return;
11408 }
11409 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11410 pring = qp->pring;
11411 if (!pring)
11412 continue;
db55fba8 11413 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
11414 }
11415}
11416
e59058c4 11417/**
3621a710 11418 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
11419 * @iocbq: Pointer to driver iocb object.
11420 * @vport: Pointer to driver virtual port object.
11421 * @tgt_id: SCSI ID of the target.
11422 * @lun_id: LUN ID of the scsi device.
11423 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11424 *
3621a710 11425 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
11426 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11427 * 0 if the filtering criteria is met for the given iocb and will return
11428 * 1 if the filtering criteria is not met.
11429 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11430 * given iocb is for the SCSI device specified by vport, tgt_id and
11431 * lun_id parameter.
11432 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11433 * given iocb is for the SCSI target specified by vport and tgt_id
11434 * parameters.
11435 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11436 * given iocb is for the SCSI host associated with the given vport.
11437 * This function is called with no locks held.
11438 **/
dea3101e 11439static int
51ef4c26
JS
11440lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11441 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 11442 lpfc_ctx_cmd ctx_cmd)
dea3101e 11443{
0bd4ca25 11444 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
11445 int rc = 1;
11446
b0e83012 11447 if (iocbq->vport != vport)
0bd4ca25
JSEC
11448 return rc;
11449
b0e83012
JS
11450 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11451 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
51ef4c26
JS
11452 return rc;
11453
0bd4ca25 11454 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 11455
495a714c 11456 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
11457 return rc;
11458
11459 switch (ctx_cmd) {
11460 case LPFC_CTX_LUN:
b0e83012 11461 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
495a714c
JS
11462 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11463 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
11464 rc = 0;
11465 break;
11466 case LPFC_CTX_TGT:
b0e83012 11467 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
495a714c 11468 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
11469 rc = 0;
11470 break;
dea3101e
JB
11471 case LPFC_CTX_HOST:
11472 rc = 0;
11473 break;
11474 default:
11475 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 11476 __func__, ctx_cmd);
dea3101e
JB
11477 break;
11478 }
11479
11480 return rc;
11481}
11482
e59058c4 11483/**
3621a710 11484 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
11485 * @vport: Pointer to virtual port.
11486 * @tgt_id: SCSI ID of the target.
11487 * @lun_id: LUN ID of the scsi device.
11488 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11489 *
11490 * This function returns number of FCP commands pending for the vport.
11491 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11492 * commands pending on the vport associated with SCSI device specified
11493 * by tgt_id and lun_id parameters.
11494 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11495 * commands pending on the vport associated with SCSI target specified
11496 * by tgt_id parameter.
11497 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11498 * commands pending on the vport.
11499 * This function returns the number of iocbs which satisfy the filter.
11500 * This function is called without any lock held.
11501 **/
dea3101e 11502int
51ef4c26
JS
11503lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11504 lpfc_ctx_cmd ctx_cmd)
dea3101e 11505{
51ef4c26 11506 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11507 struct lpfc_iocbq *iocbq;
11508 int sum, i;
dea3101e 11509
31979008 11510 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
11511 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11512 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11513
51ef4c26
JS
11514 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11515 ctx_cmd) == 0)
0bd4ca25 11516 sum++;
dea3101e 11517 }
31979008 11518 spin_unlock_irq(&phba->hbalock);
0bd4ca25 11519
dea3101e
JB
11520 return sum;
11521}
11522
e59058c4 11523/**
3621a710 11524 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
11525 * @phba: Pointer to HBA context object
11526 * @cmdiocb: Pointer to command iocb object.
11527 * @rspiocb: Pointer to response iocb object.
11528 *
11529 * This function is called when an aborted FCP iocb completes. This
11530 * function is called by the ring event handler with no lock held.
11531 * This function frees the iocb.
11532 **/
5eb95af0 11533void
2e0fef85
JS
11534lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11535 struct lpfc_iocbq *rspiocb)
5eb95af0 11536{
cb69f7de 11537 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 11538 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
11539 "original iotag x%x, abort cmd iotag x%x "
11540 "status 0x%x, reason 0x%x\n",
11541 cmdiocb->iocb.un.acxri.abortContextTag,
11542 cmdiocb->iocb.un.acxri.abortIoTag,
11543 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11544 rspiocb->iocb.un.ulpWord[4]);
604a3e30 11545 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
11546 return;
11547}
11548
e59058c4 11549/**
3621a710 11550 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
11551 * @vport: Pointer to virtual port.
11552 * @pring: Pointer to driver SLI ring object.
11553 * @tgt_id: SCSI ID of the target.
11554 * @lun_id: LUN ID of the scsi device.
11555 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11556 *
11557 * This function sends an abort command for every SCSI command
11558 * associated with the given virtual port pending on the ring
11559 * filtered by lpfc_sli_validate_fcp_iocb function.
11560 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11561 * FCP iocbs associated with lun specified by tgt_id and lun_id
11562 * parameters
11563 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11564 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11565 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11566 * FCP iocbs associated with virtual port.
11567 * This function returns number of iocbs it failed to abort.
11568 * This function is called with no locks held.
11569 **/
dea3101e 11570int
51ef4c26
JS
11571lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11572 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 11573{
51ef4c26 11574 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11575 struct lpfc_iocbq *iocbq;
11576 struct lpfc_iocbq *abtsiocb;
ecbb227e 11577 struct lpfc_sli_ring *pring_s4;
dea3101e 11578 IOCB_t *cmd = NULL;
dea3101e 11579 int errcnt = 0, ret_val = 0;
0bd4ca25 11580 int i;
dea3101e 11581
b0e83012
JS
11582 /* all I/Os are in process of being flushed */
11583 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
11584 return errcnt;
11585
0bd4ca25
JSEC
11586 for (i = 1; i <= phba->sli.last_iotag; i++) {
11587 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11588
51ef4c26 11589 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 11590 abort_cmd) != 0)
dea3101e
JB
11591 continue;
11592
afbd8d88
JS
11593 /*
11594 * If the iocbq is already being aborted, don't take a second
11595 * action, but do count it.
11596 */
11597 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11598 continue;
11599
dea3101e 11600 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 11601 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
11602 if (abtsiocb == NULL) {
11603 errcnt++;
11604 continue;
11605 }
dea3101e 11606
afbd8d88
JS
11607 /* indicate the IO is being aborted by the driver. */
11608 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11609
0bd4ca25 11610 cmd = &iocbq->iocb;
dea3101e
JB
11611 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11612 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11613 if (phba->sli_rev == LPFC_SLI_REV4)
11614 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11615 else
11616 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
11617 abtsiocb->iocb.ulpLe = 1;
11618 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11619 abtsiocb->vport = vport;
dea3101e 11620
5ffc266e 11621 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11622 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11623 if (iocbq->iocb_flag & LPFC_IO_FCP)
11624 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11625 if (iocbq->iocb_flag & LPFC_IO_FOF)
11626 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11627
2e0fef85 11628 if (lpfc_is_link_up(phba))
dea3101e
JB
11629 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11630 else
11631 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11632
5eb95af0
JSEC
11633 /* Setup callback routine and issue the command. */
11634 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11635 if (phba->sli_rev == LPFC_SLI_REV4) {
11636 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11637 if (!pring_s4)
11638 continue;
11639 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11640 abtsiocb, 0);
11641 } else
11642 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11643 abtsiocb, 0);
dea3101e 11644 if (ret_val == IOCB_ERROR) {
604a3e30 11645 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
11646 errcnt++;
11647 continue;
11648 }
11649 }
11650
11651 return errcnt;
11652}
11653
98912dda
JS
11654/**
11655 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11656 * @vport: Pointer to virtual port.
11657 * @pring: Pointer to driver SLI ring object.
11658 * @tgt_id: SCSI ID of the target.
11659 * @lun_id: LUN ID of the scsi device.
11660 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11661 *
11662 * This function sends an abort command for every SCSI command
11663 * associated with the given virtual port pending on the ring
11664 * filtered by lpfc_sli_validate_fcp_iocb function.
11665 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11666 * FCP iocbs associated with lun specified by tgt_id and lun_id
11667 * parameters
11668 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11669 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11670 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11671 * FCP iocbs associated with virtual port.
11672 * This function returns number of iocbs it aborted .
11673 * This function is called with no locks held right after a taskmgmt
11674 * command is sent.
11675 **/
11676int
11677lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11678 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11679{
11680 struct lpfc_hba *phba = vport->phba;
8c50d25c 11681 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11682 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11683 struct lpfc_nodelist *ndlp;
98912dda
JS
11684 struct lpfc_iocbq *iocbq;
11685 IOCB_t *icmd;
11686 int sum, i, ret_val;
11687 unsigned long iflags;
11688 struct lpfc_sli_ring *pring_s4;
98912dda 11689
59c68eaa 11690 spin_lock_irqsave(&phba->hbalock, iflags);
98912dda
JS
11691
11692 /* all I/Os are in process of being flushed */
11693 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
59c68eaa 11694 spin_unlock_irqrestore(&phba->hbalock, iflags);
98912dda
JS
11695 return 0;
11696 }
11697 sum = 0;
11698
11699 for (i = 1; i <= phba->sli.last_iotag; i++) {
11700 iocbq = phba->sli.iocbq_lookup[i];
11701
11702 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11703 cmd) != 0)
11704 continue;
11705
11706 /*
11707 * If the iocbq is already being aborted, don't take a second
11708 * action, but do count it.
11709 */
11710 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11711 continue;
11712
11713 /* issue ABTS for this IOCB based on iotag */
11714 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11715 if (abtsiocbq == NULL)
11716 continue;
11717
11718 icmd = &iocbq->iocb;
11719 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11720 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11721 if (phba->sli_rev == LPFC_SLI_REV4)
11722 abtsiocbq->iocb.un.acxri.abortIoTag =
11723 iocbq->sli4_xritag;
11724 else
11725 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11726 abtsiocbq->iocb.ulpLe = 1;
11727 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11728 abtsiocbq->vport = vport;
11729
11730 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11731 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11732 if (iocbq->iocb_flag & LPFC_IO_FCP)
11733 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11734 if (iocbq->iocb_flag & LPFC_IO_FOF)
11735 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11736
8c50d25c
JS
11737 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11738 ndlp = lpfc_cmd->rdata->pnode;
11739
11740 if (lpfc_is_link_up(phba) &&
11741 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11742 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11743 else
11744 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11745
11746 /* Setup callback routine and issue the command. */
11747 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11748
11749 /*
11750 * Indicate the IO is being aborted by the driver and set
11751 * the caller's flag into the aborted IO.
11752 */
11753 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11754
11755 if (phba->sli_rev == LPFC_SLI_REV4) {
59c68eaa
JS
11756 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11757 if (!pring_s4)
895427bd 11758 continue;
98912dda 11759 /* Note: both hbalock and ring_lock must be set here */
59c68eaa 11760 spin_lock(&pring_s4->ring_lock);
98912dda
JS
11761 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11762 abtsiocbq, 0);
59c68eaa 11763 spin_unlock(&pring_s4->ring_lock);
98912dda
JS
11764 } else {
11765 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11766 abtsiocbq, 0);
11767 }
11768
11769
11770 if (ret_val == IOCB_ERROR)
11771 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11772 else
11773 sum++;
11774 }
59c68eaa 11775 spin_unlock_irqrestore(&phba->hbalock, iflags);
98912dda
JS
11776 return sum;
11777}
11778
e59058c4 11779/**
3621a710 11780 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11781 * @phba: Pointer to HBA context object.
11782 * @cmdiocbq: Pointer to command iocb.
11783 * @rspiocbq: Pointer to response iocb.
11784 *
11785 * This function is the completion handler for iocbs issued using
11786 * lpfc_sli_issue_iocb_wait function. This function is called by the
11787 * ring event handler function without any lock held. This function
11788 * can be called from both worker thread context and interrupt
11789 * context. This function also can be called from other thread which
11790 * cleans up the SLI layer objects.
11791 * This function copy the contents of the response iocb to the
11792 * response iocb memory object provided by the caller of
11793 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11794 * sleeps for the iocb completion.
11795 **/
68876920
JSEC
11796static void
11797lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11798 struct lpfc_iocbq *cmdiocbq,
11799 struct lpfc_iocbq *rspiocbq)
dea3101e 11800{
68876920
JSEC
11801 wait_queue_head_t *pdone_q;
11802 unsigned long iflags;
0f65ff68 11803 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11804
2e0fef85 11805 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11806 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11807
11808 /*
11809 * A time out has occurred for the iocb. If a time out
11810 * completion handler has been supplied, call it. Otherwise,
11811 * just free the iocbq.
11812 */
11813
11814 spin_unlock_irqrestore(&phba->hbalock, iflags);
11815 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11816 cmdiocbq->wait_iocb_cmpl = NULL;
11817 if (cmdiocbq->iocb_cmpl)
11818 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11819 else
11820 lpfc_sli_release_iocbq(phba, cmdiocbq);
11821 return;
11822 }
11823
68876920
JSEC
11824 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11825 if (cmdiocbq->context2 && rspiocbq)
11826 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11827 &rspiocbq->iocb, sizeof(IOCB_t));
11828
0f65ff68
JS
11829 /* Set the exchange busy flag for task management commands */
11830 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11831 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11832 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11833 cur_iocbq);
11834 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11835 }
11836
68876920 11837 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11838 if (pdone_q)
11839 wake_up(pdone_q);
858c9f6c 11840 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11841 return;
11842}
11843
d11e31dd
JS
11844/**
11845 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11846 * @phba: Pointer to HBA context object..
11847 * @piocbq: Pointer to command iocb.
11848 * @flag: Flag to test.
11849 *
11850 * This routine grabs the hbalock and then test the iocb_flag to
11851 * see if the passed in flag is set.
11852 * Returns:
11853 * 1 if flag is set.
11854 * 0 if flag is not set.
11855 **/
11856static int
11857lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11858 struct lpfc_iocbq *piocbq, uint32_t flag)
11859{
11860 unsigned long iflags;
11861 int ret;
11862
11863 spin_lock_irqsave(&phba->hbalock, iflags);
11864 ret = piocbq->iocb_flag & flag;
11865 spin_unlock_irqrestore(&phba->hbalock, iflags);
11866 return ret;
11867
11868}
11869
e59058c4 11870/**
3621a710 11871 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11872 * @phba: Pointer to HBA context object..
11873 * @pring: Pointer to sli ring.
11874 * @piocb: Pointer to command iocb.
11875 * @prspiocbq: Pointer to response iocb.
11876 * @timeout: Timeout in number of seconds.
11877 *
11878 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11879 * iocb to complete. The iocb_cmpl field of the shall be used
11880 * to handle iocbs which time out. If the field is NULL, the
11881 * function shall free the iocbq structure. If more clean up is
11882 * needed, the caller is expected to provide a completion function
11883 * that will provide the needed clean up. If the iocb command is
11884 * not completed within timeout seconds, the function will either
11885 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11886 * completion function set in the iocb_cmpl field and then return
11887 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11888 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11889 * The function waits for the iocb completion using an
11890 * non-interruptible wait.
11891 * This function will sleep while waiting for iocb completion.
11892 * So, this function should not be called from any context which
11893 * does not allow sleeping. Due to the same reason, this function
11894 * cannot be called with interrupt disabled.
11895 * This function assumes that the iocb completions occur while
11896 * this function sleep. So, this function cannot be called from
11897 * the thread which process iocb completion for this ring.
11898 * This function clears the iocb_flag of the iocb object before
11899 * issuing the iocb and the iocb completion handler sets this
11900 * flag and wakes this thread when the iocb completes.
11901 * The contents of the response iocb will be copied to prspiocbq
11902 * by the completion handler when the command completes.
11903 * This function returns IOCB_SUCCESS when success.
11904 * This function is called with no lock held.
11905 **/
dea3101e 11906int
2e0fef85 11907lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11908 uint32_t ring_number,
2e0fef85
JS
11909 struct lpfc_iocbq *piocb,
11910 struct lpfc_iocbq *prspiocbq,
68876920 11911 uint32_t timeout)
dea3101e 11912{
7259f0d0 11913 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11914 long timeleft, timeout_req = 0;
11915 int retval = IOCB_SUCCESS;
875fbdfe 11916 uint32_t creg_val;
0e9bb8d7
JS
11917 struct lpfc_iocbq *iocb;
11918 int txq_cnt = 0;
11919 int txcmplq_cnt = 0;
895427bd 11920 struct lpfc_sli_ring *pring;
5a0916b4
JS
11921 unsigned long iflags;
11922 bool iocb_completed = true;
11923
895427bd
JS
11924 if (phba->sli_rev >= LPFC_SLI_REV4)
11925 pring = lpfc_sli4_calc_ring(phba, piocb);
11926 else
11927 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11928 /*
68876920
JSEC
11929 * If the caller has provided a response iocbq buffer, then context2
11930 * is NULL or its an error.
dea3101e 11931 */
68876920
JSEC
11932 if (prspiocbq) {
11933 if (piocb->context2)
11934 return IOCB_ERROR;
11935 piocb->context2 = prspiocbq;
dea3101e
JB
11936 }
11937
5a0916b4 11938 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11939 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11940 piocb->context_un.wait_queue = &done_q;
5a0916b4 11941 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11942
875fbdfe 11943 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11944 if (lpfc_readl(phba->HCregaddr, &creg_val))
11945 return IOCB_ERROR;
875fbdfe
JSEC
11946 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11947 writel(creg_val, phba->HCregaddr);
11948 readl(phba->HCregaddr); /* flush */
11949 }
11950
2a9bf3d0
JS
11951 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11952 SLI_IOCB_RET_IOCB);
68876920 11953 if (retval == IOCB_SUCCESS) {
256ec0d0 11954 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11955 timeleft = wait_event_timeout(done_q,
d11e31dd 11956 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11957 timeout_req);
5a0916b4
JS
11958 spin_lock_irqsave(&phba->hbalock, iflags);
11959 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11960
11961 /*
11962 * IOCB timed out. Inform the wake iocb wait
11963 * completion function and set local status
11964 */
dea3101e 11965
5a0916b4
JS
11966 iocb_completed = false;
11967 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11968 }
11969 spin_unlock_irqrestore(&phba->hbalock, iflags);
11970 if (iocb_completed) {
7054a606 11971 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11972 "0331 IOCB wake signaled\n");
53151bbb
JS
11973 /* Note: we are not indicating if the IOCB has a success
11974 * status or not - that's for the caller to check.
11975 * IOCB_SUCCESS means just that the command was sent and
11976 * completed. Not that it completed successfully.
11977 * */
7054a606 11978 } else if (timeleft == 0) {
68876920 11979 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11980 "0338 IOCB wait timeout error - no "
11981 "wake response Data x%x\n", timeout);
68876920 11982 retval = IOCB_TIMEDOUT;
7054a606 11983 } else {
68876920 11984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11985 "0330 IOCB wake NOT set, "
11986 "Data x%x x%lx\n",
68876920
JSEC
11987 timeout, (timeleft / jiffies));
11988 retval = IOCB_TIMEDOUT;
dea3101e 11989 }
2a9bf3d0 11990 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11991 if (phba->cfg_log_verbose & LOG_SLI) {
11992 list_for_each_entry(iocb, &pring->txq, list) {
11993 txq_cnt++;
11994 }
11995 list_for_each_entry(iocb, &pring->txcmplq, list) {
11996 txcmplq_cnt++;
11997 }
11998 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11999 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12000 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12001 }
2a9bf3d0 12002 return retval;
68876920
JSEC
12003 } else {
12004 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 12005 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 12006 retval);
68876920 12007 retval = IOCB_ERROR;
dea3101e
JB
12008 }
12009
875fbdfe 12010 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
12011 if (lpfc_readl(phba->HCregaddr, &creg_val))
12012 return IOCB_ERROR;
875fbdfe
JSEC
12013 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12014 writel(creg_val, phba->HCregaddr);
12015 readl(phba->HCregaddr); /* flush */
12016 }
12017
68876920
JSEC
12018 if (prspiocbq)
12019 piocb->context2 = NULL;
12020
12021 piocb->context_un.wait_queue = NULL;
12022 piocb->iocb_cmpl = NULL;
dea3101e
JB
12023 return retval;
12024}
68876920 12025
e59058c4 12026/**
3621a710 12027 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
12028 * @phba: Pointer to HBA context object.
12029 * @pmboxq: Pointer to driver mailbox object.
12030 * @timeout: Timeout in number of seconds.
12031 *
12032 * This function issues the mailbox to firmware and waits for the
12033 * mailbox command to complete. If the mailbox command is not
12034 * completed within timeout seconds, it returns MBX_TIMEOUT.
12035 * The function waits for the mailbox completion using an
12036 * interruptible wait. If the thread is woken up due to a
12037 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12038 * should not free the mailbox resources, if this function returns
12039 * MBX_TIMEOUT.
12040 * This function will sleep while waiting for mailbox completion.
12041 * So, this function should not be called from any context which
12042 * does not allow sleeping. Due to the same reason, this function
12043 * cannot be called with interrupt disabled.
12044 * This function assumes that the mailbox completion occurs while
12045 * this function sleep. So, this function cannot be called from
12046 * the worker thread which processes mailbox completion.
12047 * This function is called in the context of HBA management
12048 * applications.
12049 * This function returns MBX_SUCCESS when successful.
12050 * This function is called with no lock held.
12051 **/
dea3101e 12052int
2e0fef85 12053lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
12054 uint32_t timeout)
12055{
e29d74f8 12056 struct completion mbox_done;
dea3101e 12057 int retval;
858c9f6c 12058 unsigned long flag;
dea3101e 12059
495a714c 12060 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
12061 /* setup wake call as IOCB callback */
12062 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea3101e 12063
e29d74f8
JS
12064 /* setup context3 field to pass wait_queue pointer to wake function */
12065 init_completion(&mbox_done);
12066 pmboxq->context3 = &mbox_done;
dea3101e
JB
12067 /* now issue the command */
12068 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 12069 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
e29d74f8
JS
12070 wait_for_completion_timeout(&mbox_done,
12071 msecs_to_jiffies(timeout * 1000));
7054a606 12072
858c9f6c 12073 spin_lock_irqsave(&phba->hbalock, flag);
e29d74f8 12074 pmboxq->context3 = NULL;
7054a606
JS
12075 /*
12076 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12077 * else do not free the resources.
12078 */
d7c47992 12079 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 12080 retval = MBX_SUCCESS;
d7c47992 12081 } else {
7054a606 12082 retval = MBX_TIMEOUT;
858c9f6c
JS
12083 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12084 }
12085 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 12086 }
dea3101e
JB
12087 return retval;
12088}
12089
e59058c4 12090/**
3772a991 12091 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
12092 * @phba: Pointer to HBA context.
12093 *
3772a991
JS
12094 * This function is called to shutdown the driver's mailbox sub-system.
12095 * It first marks the mailbox sub-system is in a block state to prevent
12096 * the asynchronous mailbox command from issued off the pending mailbox
12097 * command queue. If the mailbox command sub-system shutdown is due to
12098 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12099 * the mailbox sub-system flush routine to forcefully bring down the
12100 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12101 * as with offline or HBA function reset), this routine will wait for the
12102 * outstanding mailbox command to complete before invoking the mailbox
12103 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 12104 **/
3772a991 12105void
618a5230 12106lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 12107{
3772a991 12108 struct lpfc_sli *psli = &phba->sli;
3772a991 12109 unsigned long timeout;
b4c02652 12110
618a5230
JS
12111 if (mbx_action == LPFC_MBX_NO_WAIT) {
12112 /* delay 100ms for port state */
12113 msleep(100);
12114 lpfc_sli_mbox_sys_flush(phba);
12115 return;
12116 }
a183a15f 12117 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 12118
523128e5
JS
12119 /* Disable softirqs, including timers from obtaining phba->hbalock */
12120 local_bh_disable();
12121
3772a991
JS
12122 spin_lock_irq(&phba->hbalock);
12123 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 12124
3772a991 12125 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
12126 /* Determine how long we might wait for the active mailbox
12127 * command to be gracefully completed by firmware.
12128 */
a183a15f
JS
12129 if (phba->sli.mbox_active)
12130 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12131 phba->sli.mbox_active) *
12132 1000) + jiffies;
12133 spin_unlock_irq(&phba->hbalock);
12134
523128e5
JS
12135 /* Enable softirqs again, done with phba->hbalock */
12136 local_bh_enable();
12137
3772a991
JS
12138 while (phba->sli.mbox_active) {
12139 /* Check active mailbox complete status every 2ms */
12140 msleep(2);
12141 if (time_after(jiffies, timeout))
12142 /* Timeout, let the mailbox flush routine to
12143 * forcefully release active mailbox command
12144 */
12145 break;
12146 }
523128e5 12147 } else {
d7069f09
JS
12148 spin_unlock_irq(&phba->hbalock);
12149
523128e5
JS
12150 /* Enable softirqs again, done with phba->hbalock */
12151 local_bh_enable();
12152 }
12153
3772a991
JS
12154 lpfc_sli_mbox_sys_flush(phba);
12155}
ed957684 12156
3772a991
JS
12157/**
12158 * lpfc_sli_eratt_read - read sli-3 error attention events
12159 * @phba: Pointer to HBA context.
12160 *
12161 * This function is called to read the SLI3 device error attention registers
12162 * for possible error attention events. The caller must hold the hostlock
12163 * with spin_lock_irq().
12164 *
25985edc 12165 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
12166 * Register and returns 0 otherwise.
12167 **/
12168static int
12169lpfc_sli_eratt_read(struct lpfc_hba *phba)
12170{
12171 uint32_t ha_copy;
b4c02652 12172
3772a991 12173 /* Read chip Host Attention (HA) register */
9940b97b
JS
12174 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12175 goto unplug_err;
12176
3772a991
JS
12177 if (ha_copy & HA_ERATT) {
12178 /* Read host status register to retrieve error event */
9940b97b
JS
12179 if (lpfc_sli_read_hs(phba))
12180 goto unplug_err;
b4c02652 12181
3772a991
JS
12182 /* Check if there is a deferred error condition is active */
12183 if ((HS_FFER1 & phba->work_hs) &&
12184 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 12185 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 12186 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
12187 /* Clear all interrupt enable conditions */
12188 writel(0, phba->HCregaddr);
12189 readl(phba->HCregaddr);
12190 }
12191
12192 /* Set the driver HA work bitmap */
3772a991
JS
12193 phba->work_ha |= HA_ERATT;
12194 /* Indicate polling handles this ERATT */
12195 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
12196 return 1;
12197 }
12198 return 0;
9940b97b
JS
12199
12200unplug_err:
12201 /* Set the driver HS work bitmap */
12202 phba->work_hs |= UNPLUG_ERR;
12203 /* Set the driver HA work bitmap */
12204 phba->work_ha |= HA_ERATT;
12205 /* Indicate polling handles this ERATT */
12206 phba->hba_flag |= HBA_ERATT_HANDLED;
12207 return 1;
b4c02652
JS
12208}
12209
da0436e9
JS
12210/**
12211 * lpfc_sli4_eratt_read - read sli-4 error attention events
12212 * @phba: Pointer to HBA context.
12213 *
12214 * This function is called to read the SLI4 device error attention registers
12215 * for possible error attention events. The caller must hold the hostlock
12216 * with spin_lock_irq().
12217 *
25985edc 12218 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
12219 * Register and returns 0 otherwise.
12220 **/
12221static int
12222lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12223{
12224 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
12225 uint32_t if_type, portsmphr;
12226 struct lpfc_register portstat_reg;
da0436e9 12227
2fcee4bf
JS
12228 /*
12229 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
12230 * registers for error attention. This can be changed later.
12231 */
2fcee4bf
JS
12232 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12233 switch (if_type) {
12234 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
12235 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12236 &uerr_sta_lo) ||
12237 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12238 &uerr_sta_hi)) {
12239 phba->work_hs |= UNPLUG_ERR;
12240 phba->work_ha |= HA_ERATT;
12241 phba->hba_flag |= HBA_ERATT_HANDLED;
12242 return 1;
12243 }
2fcee4bf
JS
12244 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12245 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12247 "1423 HBA Unrecoverable error: "
12248 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12249 "ue_mask_lo_reg=0x%x, "
12250 "ue_mask_hi_reg=0x%x\n",
12251 uerr_sta_lo, uerr_sta_hi,
12252 phba->sli4_hba.ue_mask_lo,
12253 phba->sli4_hba.ue_mask_hi);
12254 phba->work_status[0] = uerr_sta_lo;
12255 phba->work_status[1] = uerr_sta_hi;
12256 phba->work_ha |= HA_ERATT;
12257 phba->hba_flag |= HBA_ERATT_HANDLED;
12258 return 1;
12259 }
12260 break;
12261 case LPFC_SLI_INTF_IF_TYPE_2:
27d6ac0a 12262 case LPFC_SLI_INTF_IF_TYPE_6:
9940b97b
JS
12263 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12264 &portstat_reg.word0) ||
12265 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12266 &portsmphr)){
12267 phba->work_hs |= UNPLUG_ERR;
12268 phba->work_ha |= HA_ERATT;
12269 phba->hba_flag |= HBA_ERATT_HANDLED;
12270 return 1;
12271 }
2fcee4bf
JS
12272 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12273 phba->work_status[0] =
12274 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12275 phba->work_status[1] =
12276 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 12278 "2885 Port Status Event: "
2fcee4bf
JS
12279 "port status reg 0x%x, "
12280 "port smphr reg 0x%x, "
12281 "error 1=0x%x, error 2=0x%x\n",
12282 portstat_reg.word0,
12283 portsmphr,
12284 phba->work_status[0],
12285 phba->work_status[1]);
12286 phba->work_ha |= HA_ERATT;
12287 phba->hba_flag |= HBA_ERATT_HANDLED;
12288 return 1;
12289 }
12290 break;
12291 case LPFC_SLI_INTF_IF_TYPE_1:
12292 default:
a747c9ce 12293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
12294 "2886 HBA Error Attention on unsupported "
12295 "if type %d.", if_type);
a747c9ce 12296 return 1;
da0436e9 12297 }
2fcee4bf 12298
da0436e9
JS
12299 return 0;
12300}
12301
e59058c4 12302/**
3621a710 12303 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
12304 * @phba: Pointer to HBA context.
12305 *
3772a991 12306 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
12307 * error attention register bit for error attention events.
12308 *
25985edc 12309 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
12310 * Register and returns 0 otherwise.
12311 **/
12312int
12313lpfc_sli_check_eratt(struct lpfc_hba *phba)
12314{
12315 uint32_t ha_copy;
12316
12317 /* If somebody is waiting to handle an eratt, don't process it
12318 * here. The brdkill function will do this.
12319 */
12320 if (phba->link_flag & LS_IGNORE_ERATT)
12321 return 0;
12322
12323 /* Check if interrupt handler handles this ERATT */
12324 spin_lock_irq(&phba->hbalock);
12325 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12326 /* Interrupt handler has handled ERATT */
12327 spin_unlock_irq(&phba->hbalock);
12328 return 0;
12329 }
12330
a257bf90
JS
12331 /*
12332 * If there is deferred error attention, do not check for error
12333 * attention
12334 */
12335 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12336 spin_unlock_irq(&phba->hbalock);
12337 return 0;
12338 }
12339
3772a991
JS
12340 /* If PCI channel is offline, don't process it */
12341 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 12342 spin_unlock_irq(&phba->hbalock);
3772a991
JS
12343 return 0;
12344 }
12345
12346 switch (phba->sli_rev) {
12347 case LPFC_SLI_REV2:
12348 case LPFC_SLI_REV3:
12349 /* Read chip Host Attention (HA) register */
12350 ha_copy = lpfc_sli_eratt_read(phba);
12351 break;
da0436e9 12352 case LPFC_SLI_REV4:
2fcee4bf 12353 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
12354 ha_copy = lpfc_sli4_eratt_read(phba);
12355 break;
3772a991
JS
12356 default:
12357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12358 "0299 Invalid SLI revision (%d)\n",
12359 phba->sli_rev);
12360 ha_copy = 0;
12361 break;
9399627f
JS
12362 }
12363 spin_unlock_irq(&phba->hbalock);
3772a991
JS
12364
12365 return ha_copy;
12366}
12367
12368/**
12369 * lpfc_intr_state_check - Check device state for interrupt handling
12370 * @phba: Pointer to HBA context.
12371 *
12372 * This inline routine checks whether a device or its PCI slot is in a state
12373 * that the interrupt should be handled.
12374 *
12375 * This function returns 0 if the device or the PCI slot is in a state that
12376 * interrupt should be handled, otherwise -EIO.
12377 */
12378static inline int
12379lpfc_intr_state_check(struct lpfc_hba *phba)
12380{
12381 /* If the pci channel is offline, ignore all the interrupts */
12382 if (unlikely(pci_channel_offline(phba->pcidev)))
12383 return -EIO;
12384
12385 /* Update device level interrupt statistics */
12386 phba->sli.slistat.sli_intr++;
12387
12388 /* Ignore all interrupts during initialization. */
12389 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12390 return -EIO;
12391
9399627f
JS
12392 return 0;
12393}
12394
12395/**
3772a991 12396 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
12397 * @irq: Interrupt number.
12398 * @dev_id: The device context pointer.
12399 *
9399627f 12400 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12401 * service routine when device with SLI-3 interface spec is enabled with
12402 * MSI-X multi-message interrupt mode and there are slow-path events in
12403 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12404 * interrupt mode, this function is called as part of the device-level
12405 * interrupt handler. When the PCI slot is in error recovery or the HBA
12406 * is undergoing initialization, the interrupt handler will not process
12407 * the interrupt. The link attention and ELS ring attention events are
12408 * handled by the worker thread. The interrupt handler signals the worker
12409 * thread and returns for these events. This function is called without
12410 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
12411 * structures.
12412 *
12413 * This function returns IRQ_HANDLED when interrupt is handled else it
12414 * returns IRQ_NONE.
e59058c4 12415 **/
dea3101e 12416irqreturn_t
3772a991 12417lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 12418{
2e0fef85 12419 struct lpfc_hba *phba;
a747c9ce 12420 uint32_t ha_copy, hc_copy;
dea3101e
JB
12421 uint32_t work_ha_copy;
12422 unsigned long status;
5b75da2f 12423 unsigned long iflag;
dea3101e
JB
12424 uint32_t control;
12425
92d7f7b0 12426 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
12427 struct lpfc_vport *vport;
12428 struct lpfc_nodelist *ndlp;
12429 struct lpfc_dmabuf *mp;
92d7f7b0
JS
12430 LPFC_MBOXQ_t *pmb;
12431 int rc;
12432
dea3101e
JB
12433 /*
12434 * Get the driver's phba structure from the dev_id and
12435 * assume the HBA is not interrupting.
12436 */
9399627f 12437 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
12438
12439 if (unlikely(!phba))
12440 return IRQ_NONE;
12441
dea3101e 12442 /*
9399627f
JS
12443 * Stuff needs to be attented to when this function is invoked as an
12444 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 12445 */
9399627f 12446 if (phba->intr_type == MSIX) {
3772a991
JS
12447 /* Check device state for handling interrupt */
12448 if (lpfc_intr_state_check(phba))
9399627f
JS
12449 return IRQ_NONE;
12450 /* Need to read HA REG for slow-path events */
5b75da2f 12451 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12452 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12453 goto unplug_error;
9399627f
JS
12454 /* If somebody is waiting to handle an eratt don't process it
12455 * here. The brdkill function will do this.
12456 */
12457 if (phba->link_flag & LS_IGNORE_ERATT)
12458 ha_copy &= ~HA_ERATT;
12459 /* Check the need for handling ERATT in interrupt handler */
12460 if (ha_copy & HA_ERATT) {
12461 if (phba->hba_flag & HBA_ERATT_HANDLED)
12462 /* ERATT polling has handled ERATT */
12463 ha_copy &= ~HA_ERATT;
12464 else
12465 /* Indicate interrupt handler handles ERATT */
12466 phba->hba_flag |= HBA_ERATT_HANDLED;
12467 }
a257bf90
JS
12468
12469 /*
12470 * If there is deferred error attention, do not check for any
12471 * interrupt.
12472 */
12473 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12474 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12475 return IRQ_NONE;
12476 }
12477
9399627f 12478 /* Clear up only attention source related to slow-path */
9940b97b
JS
12479 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12480 goto unplug_error;
12481
a747c9ce
JS
12482 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12483 HC_LAINT_ENA | HC_ERINT_ENA),
12484 phba->HCregaddr);
9399627f
JS
12485 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12486 phba->HAregaddr);
a747c9ce 12487 writel(hc_copy, phba->HCregaddr);
9399627f 12488 readl(phba->HAregaddr); /* flush */
5b75da2f 12489 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12490 } else
12491 ha_copy = phba->ha_copy;
dea3101e 12492
dea3101e
JB
12493 work_ha_copy = ha_copy & phba->work_ha_mask;
12494
9399627f 12495 if (work_ha_copy) {
dea3101e
JB
12496 if (work_ha_copy & HA_LATT) {
12497 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12498 /*
12499 * Turn off Link Attention interrupts
12500 * until CLEAR_LA done
12501 */
5b75da2f 12502 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12503 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
12504 if (lpfc_readl(phba->HCregaddr, &control))
12505 goto unplug_error;
dea3101e
JB
12506 control &= ~HC_LAINT_ENA;
12507 writel(control, phba->HCregaddr);
12508 readl(phba->HCregaddr); /* flush */
5b75da2f 12509 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12510 }
12511 else
12512 work_ha_copy &= ~HA_LATT;
12513 }
12514
9399627f 12515 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
12516 /*
12517 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12518 * the only slow ring.
12519 */
12520 status = (work_ha_copy &
12521 (HA_RXMASK << (4*LPFC_ELS_RING)));
12522 status >>= (4*LPFC_ELS_RING);
12523 if (status & HA_RXMASK) {
5b75da2f 12524 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12525 if (lpfc_readl(phba->HCregaddr, &control))
12526 goto unplug_error;
a58cbd52
JS
12527
12528 lpfc_debugfs_slow_ring_trc(phba,
12529 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12530 control, status,
12531 (uint32_t)phba->sli.slistat.sli_intr);
12532
858c9f6c 12533 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
12534 lpfc_debugfs_slow_ring_trc(phba,
12535 "ISR Disable ring:"
12536 "pwork:x%x hawork:x%x wait:x%x",
12537 phba->work_ha, work_ha_copy,
12538 (uint32_t)((unsigned long)
5e9d9b82 12539 &phba->work_waitq));
a58cbd52 12540
858c9f6c
JS
12541 control &=
12542 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
12543 writel(control, phba->HCregaddr);
12544 readl(phba->HCregaddr); /* flush */
dea3101e 12545 }
a58cbd52
JS
12546 else {
12547 lpfc_debugfs_slow_ring_trc(phba,
12548 "ISR slow ring: pwork:"
12549 "x%x hawork:x%x wait:x%x",
12550 phba->work_ha, work_ha_copy,
12551 (uint32_t)((unsigned long)
5e9d9b82 12552 &phba->work_waitq));
a58cbd52 12553 }
5b75da2f 12554 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12555 }
12556 }
5b75da2f 12557 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 12558 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
12559 if (lpfc_sli_read_hs(phba))
12560 goto unplug_error;
a257bf90
JS
12561 /*
12562 * Check if there is a deferred error condition
12563 * is active
12564 */
12565 if ((HS_FFER1 & phba->work_hs) &&
12566 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
12567 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12568 phba->work_hs)) {
a257bf90
JS
12569 phba->hba_flag |= DEFER_ERATT;
12570 /* Clear all interrupt enable conditions */
12571 writel(0, phba->HCregaddr);
12572 readl(phba->HCregaddr);
12573 }
12574 }
12575
9399627f 12576 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 12577 pmb = phba->sli.mbox_active;
04c68496 12578 pmbox = &pmb->u.mb;
34b02dcd 12579 mbox = phba->mbox;
858c9f6c 12580 vport = pmb->vport;
92d7f7b0
JS
12581
12582 /* First check out the status word */
12583 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12584 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 12585 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
12586 /*
12587 * Stray Mailbox Interrupt, mbxCommand <cmd>
12588 * mbxStatus <status>
12589 */
09372820 12590 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 12591 LOG_SLI,
e8b62011 12592 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
12593 "Interrupt mbxCommand x%x "
12594 "mbxStatus x%x\n",
e8b62011 12595 (vport ? vport->vpi : 0),
92d7f7b0
JS
12596 pmbox->mbxCommand,
12597 pmbox->mbxStatus);
09372820
JS
12598 /* clear mailbox attention bit */
12599 work_ha_copy &= ~HA_MBATT;
12600 } else {
97eab634 12601 phba->sli.mbox_active = NULL;
5b75da2f 12602 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
12603 phba->last_completion_time = jiffies;
12604 del_timer(&phba->sli.mbox_tmo);
09372820
JS
12605 if (pmb->mbox_cmpl) {
12606 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12607 MAILBOX_CMD_SIZE);
7a470277 12608 if (pmb->out_ext_byte_len &&
3e1f0718 12609 pmb->ctx_buf)
7a470277
JS
12610 lpfc_sli_pcimem_bcopy(
12611 phba->mbox_ext,
3e1f0718 12612 pmb->ctx_buf,
7a470277 12613 pmb->out_ext_byte_len);
09372820
JS
12614 }
12615 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12616 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12617
12618 lpfc_debugfs_disc_trc(vport,
12619 LPFC_DISC_TRC_MBOX_VPORT,
12620 "MBOX dflt rpi: : "
12621 "status:x%x rpi:x%x",
12622 (uint32_t)pmbox->mbxStatus,
12623 pmbox->un.varWords[0], 0);
12624
12625 if (!pmbox->mbxStatus) {
12626 mp = (struct lpfc_dmabuf *)
3e1f0718 12627 (pmb->ctx_buf);
09372820 12628 ndlp = (struct lpfc_nodelist *)
3e1f0718 12629 pmb->ctx_ndlp;
09372820
JS
12630
12631 /* Reg_LOGIN of dflt RPI was
12632 * successful. new lets get
12633 * rid of the RPI using the
12634 * same mbox buffer.
12635 */
12636 lpfc_unreg_login(phba,
12637 vport->vpi,
12638 pmbox->un.varWords[0],
12639 pmb);
12640 pmb->mbox_cmpl =
12641 lpfc_mbx_cmpl_dflt_rpi;
3e1f0718
JS
12642 pmb->ctx_buf = mp;
12643 pmb->ctx_ndlp = ndlp;
09372820 12644 pmb->vport = vport;
58da1ffb
JS
12645 rc = lpfc_sli_issue_mbox(phba,
12646 pmb,
12647 MBX_NOWAIT);
12648 if (rc != MBX_BUSY)
12649 lpfc_printf_log(phba,
12650 KERN_ERR,
12651 LOG_MBOX | LOG_SLI,
d7c255b2 12652 "0350 rc should have"
6a9c52cf 12653 "been MBX_BUSY\n");
3772a991
JS
12654 if (rc != MBX_NOT_FINISHED)
12655 goto send_current_mbox;
09372820 12656 }
858c9f6c 12657 }
5b75da2f
JS
12658 spin_lock_irqsave(
12659 &phba->pport->work_port_lock,
12660 iflag);
09372820
JS
12661 phba->pport->work_port_events &=
12662 ~WORKER_MBOX_TMO;
5b75da2f
JS
12663 spin_unlock_irqrestore(
12664 &phba->pport->work_port_lock,
12665 iflag);
09372820 12666 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12667 }
97eab634 12668 } else
5b75da2f 12669 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12670
92d7f7b0
JS
12671 if ((work_ha_copy & HA_MBATT) &&
12672 (phba->sli.mbox_active == NULL)) {
858c9f6c 12673send_current_mbox:
92d7f7b0 12674 /* Process next mailbox command if there is one */
58da1ffb
JS
12675 do {
12676 rc = lpfc_sli_issue_mbox(phba, NULL,
12677 MBX_NOWAIT);
12678 } while (rc == MBX_NOT_FINISHED);
12679 if (rc != MBX_SUCCESS)
12680 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12681 LOG_SLI, "0349 rc should be "
6a9c52cf 12682 "MBX_SUCCESS\n");
92d7f7b0
JS
12683 }
12684
5b75da2f 12685 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12686 phba->work_ha |= work_ha_copy;
5b75da2f 12687 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12688 lpfc_worker_wake_up(phba);
dea3101e 12689 }
9399627f 12690 return IRQ_HANDLED;
9940b97b
JS
12691unplug_error:
12692 spin_unlock_irqrestore(&phba->hbalock, iflag);
12693 return IRQ_HANDLED;
dea3101e 12694
3772a991 12695} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12696
12697/**
3772a991 12698 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12699 * @irq: Interrupt number.
12700 * @dev_id: The device context pointer.
12701 *
12702 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12703 * service routine when device with SLI-3 interface spec is enabled with
12704 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12705 * ring event in the HBA. However, when the device is enabled with either
12706 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12707 * device-level interrupt handler. When the PCI slot is in error recovery
12708 * or the HBA is undergoing initialization, the interrupt handler will not
12709 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12710 * the intrrupt context. This function is called without any lock held.
12711 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12712 *
12713 * This function returns IRQ_HANDLED when interrupt is handled else it
12714 * returns IRQ_NONE.
12715 **/
12716irqreturn_t
3772a991 12717lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12718{
12719 struct lpfc_hba *phba;
12720 uint32_t ha_copy;
12721 unsigned long status;
5b75da2f 12722 unsigned long iflag;
895427bd 12723 struct lpfc_sli_ring *pring;
9399627f
JS
12724
12725 /* Get the driver's phba structure from the dev_id and
12726 * assume the HBA is not interrupting.
12727 */
12728 phba = (struct lpfc_hba *) dev_id;
12729
12730 if (unlikely(!phba))
12731 return IRQ_NONE;
12732
12733 /*
12734 * Stuff needs to be attented to when this function is invoked as an
12735 * individual interrupt handler in MSI-X multi-message interrupt mode
12736 */
12737 if (phba->intr_type == MSIX) {
3772a991
JS
12738 /* Check device state for handling interrupt */
12739 if (lpfc_intr_state_check(phba))
9399627f
JS
12740 return IRQ_NONE;
12741 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12742 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12743 return IRQ_HANDLED;
9399627f 12744 /* Clear up only attention source related to fast-path */
5b75da2f 12745 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12746 /*
12747 * If there is deferred error attention, do not check for
12748 * any interrupt.
12749 */
12750 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12751 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12752 return IRQ_NONE;
12753 }
9399627f
JS
12754 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12755 phba->HAregaddr);
12756 readl(phba->HAregaddr); /* flush */
5b75da2f 12757 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12758 } else
12759 ha_copy = phba->ha_copy;
dea3101e
JB
12760
12761 /*
9399627f 12762 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12763 */
9399627f
JS
12764 ha_copy &= ~(phba->work_ha_mask);
12765
12766 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12767 status >>= (4*LPFC_FCP_RING);
895427bd 12768 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12769 if (status & HA_RXMASK)
895427bd 12770 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12771
12772 if (phba->cfg_multi_ring_support == 2) {
12773 /*
9399627f
JS
12774 * Process all events on extra ring. Take the optimized path
12775 * for extra ring IO.
a4bc3379 12776 */
9399627f 12777 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12778 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12779 if (status & HA_RXMASK) {
a4bc3379 12780 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12781 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12782 status);
12783 }
12784 }
dea3101e 12785 return IRQ_HANDLED;
3772a991 12786} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12787
12788/**
3772a991 12789 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12790 * @irq: Interrupt number.
12791 * @dev_id: The device context pointer.
12792 *
3772a991
JS
12793 * This function is the HBA device-level interrupt handler to device with
12794 * SLI-3 interface spec, called from the PCI layer when either MSI or
12795 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12796 * requires driver attention. This function invokes the slow-path interrupt
12797 * attention handling function and fast-path interrupt attention handling
12798 * function in turn to process the relevant HBA attention events. This
12799 * function is called without any lock held. It gets the hbalock to access
12800 * and update SLI data structures.
9399627f
JS
12801 *
12802 * This function returns IRQ_HANDLED when interrupt is handled, else it
12803 * returns IRQ_NONE.
12804 **/
12805irqreturn_t
3772a991 12806lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12807{
12808 struct lpfc_hba *phba;
12809 irqreturn_t sp_irq_rc, fp_irq_rc;
12810 unsigned long status1, status2;
a747c9ce 12811 uint32_t hc_copy;
9399627f
JS
12812
12813 /*
12814 * Get the driver's phba structure from the dev_id and
12815 * assume the HBA is not interrupting.
12816 */
12817 phba = (struct lpfc_hba *) dev_id;
12818
12819 if (unlikely(!phba))
12820 return IRQ_NONE;
12821
3772a991
JS
12822 /* Check device state for handling interrupt */
12823 if (lpfc_intr_state_check(phba))
9399627f
JS
12824 return IRQ_NONE;
12825
12826 spin_lock(&phba->hbalock);
9940b97b
JS
12827 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12828 spin_unlock(&phba->hbalock);
12829 return IRQ_HANDLED;
12830 }
12831
9399627f
JS
12832 if (unlikely(!phba->ha_copy)) {
12833 spin_unlock(&phba->hbalock);
12834 return IRQ_NONE;
12835 } else if (phba->ha_copy & HA_ERATT) {
12836 if (phba->hba_flag & HBA_ERATT_HANDLED)
12837 /* ERATT polling has handled ERATT */
12838 phba->ha_copy &= ~HA_ERATT;
12839 else
12840 /* Indicate interrupt handler handles ERATT */
12841 phba->hba_flag |= HBA_ERATT_HANDLED;
12842 }
12843
a257bf90
JS
12844 /*
12845 * If there is deferred error attention, do not check for any interrupt.
12846 */
12847 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12848 spin_unlock(&phba->hbalock);
a257bf90
JS
12849 return IRQ_NONE;
12850 }
12851
9399627f 12852 /* Clear attention sources except link and error attentions */
9940b97b
JS
12853 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12854 spin_unlock(&phba->hbalock);
12855 return IRQ_HANDLED;
12856 }
a747c9ce
JS
12857 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12858 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12859 phba->HCregaddr);
9399627f 12860 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12861 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12862 readl(phba->HAregaddr); /* flush */
12863 spin_unlock(&phba->hbalock);
12864
12865 /*
12866 * Invokes slow-path host attention interrupt handling as appropriate.
12867 */
12868
12869 /* status of events with mailbox and link attention */
12870 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12871
12872 /* status of events with ELS ring */
12873 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12874 status2 >>= (4*LPFC_ELS_RING);
12875
12876 if (status1 || (status2 & HA_RXMASK))
3772a991 12877 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12878 else
12879 sp_irq_rc = IRQ_NONE;
12880
12881 /*
12882 * Invoke fast-path host attention interrupt handling as appropriate.
12883 */
12884
12885 /* status of events with FCP ring */
12886 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12887 status1 >>= (4*LPFC_FCP_RING);
12888
12889 /* status of events with extra ring */
12890 if (phba->cfg_multi_ring_support == 2) {
12891 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12892 status2 >>= (4*LPFC_EXTRA_RING);
12893 } else
12894 status2 = 0;
12895
12896 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12897 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12898 else
12899 fp_irq_rc = IRQ_NONE;
dea3101e 12900
9399627f
JS
12901 /* Return device-level interrupt handling status */
12902 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12903} /* lpfc_sli_intr_handler */
4f774513
JS
12904
12905/**
12906 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12907 * @phba: pointer to lpfc hba data structure.
12908 *
12909 * This routine is invoked by the worker thread to process all the pending
12910 * SLI4 FCP abort XRI events.
12911 **/
12912void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12913{
12914 struct lpfc_cq_event *cq_event;
12915
12916 /* First, declare the fcp xri abort event has been handled */
12917 spin_lock_irq(&phba->hbalock);
12918 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12919 spin_unlock_irq(&phba->hbalock);
12920 /* Now, handle all the fcp xri abort events */
12921 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12922 /* Get the first event from the head of the event queue */
12923 spin_lock_irq(&phba->hbalock);
12924 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12925 cq_event, struct lpfc_cq_event, list);
12926 spin_unlock_irq(&phba->hbalock);
12927 /* Notify aborted XRI for FCP work queue */
12928 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12929 /* Free the event processed back to the free pool */
12930 lpfc_sli4_cq_event_release(phba, cq_event);
12931 }
12932}
12933
12934/**
12935 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12936 * @phba: pointer to lpfc hba data structure.
12937 *
12938 * This routine is invoked by the worker thread to process all the pending
12939 * SLI4 els abort xri events.
12940 **/
12941void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12942{
12943 struct lpfc_cq_event *cq_event;
12944
12945 /* First, declare the els xri abort event has been handled */
12946 spin_lock_irq(&phba->hbalock);
12947 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12948 spin_unlock_irq(&phba->hbalock);
12949 /* Now, handle all the els xri abort events */
12950 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12951 /* Get the first event from the head of the event queue */
12952 spin_lock_irq(&phba->hbalock);
12953 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12954 cq_event, struct lpfc_cq_event, list);
12955 spin_unlock_irq(&phba->hbalock);
12956 /* Notify aborted XRI for ELS work queue */
12957 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12958 /* Free the event processed back to the free pool */
12959 lpfc_sli4_cq_event_release(phba, cq_event);
12960 }
12961}
12962
341af102
JS
12963/**
12964 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12965 * @phba: pointer to lpfc hba data structure
12966 * @pIocbIn: pointer to the rspiocbq
12967 * @pIocbOut: pointer to the cmdiocbq
12968 * @wcqe: pointer to the complete wcqe
12969 *
12970 * This routine transfers the fields of a command iocbq to a response iocbq
12971 * by copying all the IOCB fields from command iocbq and transferring the
12972 * completion status information from the complete wcqe.
12973 **/
4f774513 12974static void
341af102
JS
12975lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12976 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12977 struct lpfc_iocbq *pIocbOut,
12978 struct lpfc_wcqe_complete *wcqe)
12979{
af22741c 12980 int numBdes, i;
341af102 12981 unsigned long iflags;
af22741c
JS
12982 uint32_t status, max_response;
12983 struct lpfc_dmabuf *dmabuf;
12984 struct ulp_bde64 *bpl, bde;
4f774513
JS
12985 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12986
12987 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12988 sizeof(struct lpfc_iocbq) - offset);
4f774513 12989 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12990 status = bf_get(lpfc_wcqe_c_status, wcqe);
12991 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12992 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12993 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12994 pIocbIn->iocb.un.fcpi.fcpi_parm =
12995 pIocbOut->iocb.un.fcpi.fcpi_parm -
12996 wcqe->total_data_placed;
12997 else
12998 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12999 else {
4f774513 13000 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
13001 switch (pIocbOut->iocb.ulpCommand) {
13002 case CMD_ELS_REQUEST64_CR:
13003 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13004 bpl = (struct ulp_bde64 *)dmabuf->virt;
13005 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13006 max_response = bde.tus.f.bdeSize;
13007 break;
13008 case CMD_GEN_REQUEST64_CR:
13009 max_response = 0;
13010 if (!pIocbOut->context3)
13011 break;
13012 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13013 sizeof(struct ulp_bde64);
13014 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13015 bpl = (struct ulp_bde64 *)dmabuf->virt;
13016 for (i = 0; i < numBdes; i++) {
13017 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13018 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13019 max_response += bde.tus.f.bdeSize;
13020 }
13021 break;
13022 default:
13023 max_response = wcqe->total_data_placed;
13024 break;
13025 }
13026 if (max_response < wcqe->total_data_placed)
13027 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13028 else
13029 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13030 wcqe->total_data_placed;
695a814e 13031 }
341af102 13032
acd6859b
JS
13033 /* Convert BG errors for completion status */
13034 if (status == CQE_STATUS_DI_ERROR) {
13035 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13036
13037 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13038 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13039 else
13040 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13041
13042 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13043 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13044 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13045 BGS_GUARD_ERR_MASK;
13046 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13047 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13048 BGS_APPTAG_ERR_MASK;
13049 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13050 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13051 BGS_REFTAG_ERR_MASK;
13052
13053 /* Check to see if there was any good data before the error */
13054 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13055 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13056 BGS_HI_WATER_MARK_PRESENT_MASK;
13057 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13058 wcqe->total_data_placed;
13059 }
13060
13061 /*
13062 * Set ALL the error bits to indicate we don't know what
13063 * type of error it is.
13064 */
13065 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13066 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13067 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13068 BGS_GUARD_ERR_MASK);
13069 }
13070
341af102
JS
13071 /* Pick up HBA exchange busy condition */
13072 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13073 spin_lock_irqsave(&phba->hbalock, iflags);
13074 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13075 spin_unlock_irqrestore(&phba->hbalock, iflags);
13076 }
4f774513
JS
13077}
13078
45ed1190
JS
13079/**
13080 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13081 * @phba: Pointer to HBA context object.
13082 * @wcqe: Pointer to work-queue completion queue entry.
13083 *
13084 * This routine handles an ELS work-queue completion event and construct
13085 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13086 * discovery engine to handle.
13087 *
13088 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13089 **/
13090static struct lpfc_iocbq *
13091lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13092 struct lpfc_iocbq *irspiocbq)
13093{
895427bd 13094 struct lpfc_sli_ring *pring;
45ed1190
JS
13095 struct lpfc_iocbq *cmdiocbq;
13096 struct lpfc_wcqe_complete *wcqe;
13097 unsigned long iflags;
13098
895427bd 13099 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
13100 if (unlikely(!pring))
13101 return NULL;
895427bd 13102
45ed1190 13103 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 13104 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
13105 pring->stats.iocb_event++;
13106 /* Look up the ELS command IOCB and create pseudo response IOCB */
13107 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13108 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 13109 if (unlikely(!cmdiocbq)) {
401bb416 13110 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
13111 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13112 "0386 ELS complete with no corresponding "
401bb416
DK
13113 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13114 wcqe->word0, wcqe->total_data_placed,
13115 wcqe->parameter, wcqe->word3);
45ed1190
JS
13116 lpfc_sli_release_iocbq(phba, irspiocbq);
13117 return NULL;
13118 }
13119
401bb416
DK
13120 /* Put the iocb back on the txcmplq */
13121 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13122 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13123
45ed1190 13124 /* Fake the irspiocbq and copy necessary response information */
341af102 13125 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
13126
13127 return irspiocbq;
13128}
13129
8a5ca109
JS
13130inline struct lpfc_cq_event *
13131lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13132{
13133 struct lpfc_cq_event *cq_event;
13134
13135 /* Allocate a new internal CQ_EVENT entry */
13136 cq_event = lpfc_sli4_cq_event_alloc(phba);
13137 if (!cq_event) {
13138 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13139 "0602 Failed to alloc CQ_EVENT entry\n");
13140 return NULL;
13141 }
13142
13143 /* Move the CQE into the event */
13144 memcpy(&cq_event->cqe, entry, size);
13145 return cq_event;
13146}
13147
04c68496
JS
13148/**
13149 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
13150 * @phba: Pointer to HBA context object.
13151 * @cqe: Pointer to mailbox completion queue entry.
13152 *
13153 * This routine process a mailbox completion queue entry with asynchrous
13154 * event.
13155 *
13156 * Return: true if work posted to worker thread, otherwise false.
13157 **/
13158static bool
13159lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13160{
13161 struct lpfc_cq_event *cq_event;
13162 unsigned long iflags;
13163
13164 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13165 "0392 Async Event: word0:x%x, word1:x%x, "
13166 "word2:x%x, word3:x%x\n", mcqe->word0,
13167 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13168
8a5ca109
JS
13169 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13170 if (!cq_event)
04c68496 13171 return false;
04c68496
JS
13172 spin_lock_irqsave(&phba->hbalock, iflags);
13173 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13174 /* Set the async event flag */
13175 phba->hba_flag |= ASYNC_EVENT;
13176 spin_unlock_irqrestore(&phba->hbalock, iflags);
13177
13178 return true;
13179}
13180
13181/**
13182 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13183 * @phba: Pointer to HBA context object.
13184 * @cqe: Pointer to mailbox completion queue entry.
13185 *
13186 * This routine process a mailbox completion queue entry with mailbox
13187 * completion event.
13188 *
13189 * Return: true if work posted to worker thread, otherwise false.
13190 **/
13191static bool
13192lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13193{
13194 uint32_t mcqe_status;
13195 MAILBOX_t *mbox, *pmbox;
13196 struct lpfc_mqe *mqe;
13197 struct lpfc_vport *vport;
13198 struct lpfc_nodelist *ndlp;
13199 struct lpfc_dmabuf *mp;
13200 unsigned long iflags;
13201 LPFC_MBOXQ_t *pmb;
13202 bool workposted = false;
13203 int rc;
13204
13205 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13206 if (!bf_get(lpfc_trailer_completed, mcqe))
13207 goto out_no_mqe_complete;
13208
13209 /* Get the reference to the active mbox command */
13210 spin_lock_irqsave(&phba->hbalock, iflags);
13211 pmb = phba->sli.mbox_active;
13212 if (unlikely(!pmb)) {
13213 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13214 "1832 No pending MBOX command to handle\n");
13215 spin_unlock_irqrestore(&phba->hbalock, iflags);
13216 goto out_no_mqe_complete;
13217 }
13218 spin_unlock_irqrestore(&phba->hbalock, iflags);
13219 mqe = &pmb->u.mqe;
13220 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13221 mbox = phba->mbox;
13222 vport = pmb->vport;
13223
13224 /* Reset heartbeat timer */
13225 phba->last_completion_time = jiffies;
13226 del_timer(&phba->sli.mbox_tmo);
13227
13228 /* Move mbox data to caller's mailbox region, do endian swapping */
13229 if (pmb->mbox_cmpl && mbox)
48f8fdb4 13230 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 13231
73d91e50
JS
13232 /*
13233 * For mcqe errors, conditionally move a modified error code to
13234 * the mbox so that the error will not be missed.
13235 */
13236 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13237 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13238 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13239 bf_set(lpfc_mqe_status, mqe,
13240 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13241 }
04c68496
JS
13242 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13243 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13244 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13245 "MBOX dflt rpi: status:x%x rpi:x%x",
13246 mcqe_status,
13247 pmbox->un.varWords[0], 0);
13248 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
3e1f0718
JS
13249 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13250 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
04c68496
JS
13251 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13252 * RID of the PPI using the same mbox buffer.
13253 */
13254 lpfc_unreg_login(phba, vport->vpi,
13255 pmbox->un.varWords[0], pmb);
13256 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3e1f0718
JS
13257 pmb->ctx_buf = mp;
13258 pmb->ctx_ndlp = ndlp;
04c68496
JS
13259 pmb->vport = vport;
13260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13261 if (rc != MBX_BUSY)
13262 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13263 LOG_SLI, "0385 rc should "
13264 "have been MBX_BUSY\n");
13265 if (rc != MBX_NOT_FINISHED)
13266 goto send_current_mbox;
13267 }
13268 }
13269 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13270 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13271 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13272
13273 /* There is mailbox completion work to do */
13274 spin_lock_irqsave(&phba->hbalock, iflags);
13275 __lpfc_mbox_cmpl_put(phba, pmb);
13276 phba->work_ha |= HA_MBATT;
13277 spin_unlock_irqrestore(&phba->hbalock, iflags);
13278 workposted = true;
13279
13280send_current_mbox:
13281 spin_lock_irqsave(&phba->hbalock, iflags);
13282 /* Release the mailbox command posting token */
13283 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13284 /* Setting active mailbox pointer need to be in sync to flag clear */
13285 phba->sli.mbox_active = NULL;
13286 spin_unlock_irqrestore(&phba->hbalock, iflags);
13287 /* Wake up worker thread to post the next pending mailbox command */
13288 lpfc_worker_wake_up(phba);
13289out_no_mqe_complete:
13290 if (bf_get(lpfc_trailer_consumed, mcqe))
13291 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13292 return workposted;
13293}
13294
13295/**
13296 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13297 * @phba: Pointer to HBA context object.
13298 * @cqe: Pointer to mailbox completion queue entry.
13299 *
13300 * This routine process a mailbox completion queue entry, it invokes the
13301 * proper mailbox complete handling or asynchrous event handling routine
13302 * according to the MCQE's async bit.
13303 *
13304 * Return: true if work posted to worker thread, otherwise false.
13305 **/
13306static bool
13307lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
13308{
13309 struct lpfc_mcqe mcqe;
13310 bool workposted;
13311
13312 /* Copy the mailbox MCQE and convert endian order as needed */
48f8fdb4 13313 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
04c68496
JS
13314
13315 /* Invoke the proper event handling routine */
13316 if (!bf_get(lpfc_trailer_async, &mcqe))
13317 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13318 else
13319 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13320 return workposted;
13321}
13322
4f774513
JS
13323/**
13324 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13325 * @phba: Pointer to HBA context object.
2a76a283 13326 * @cq: Pointer to associated CQ
4f774513
JS
13327 * @wcqe: Pointer to work-queue completion queue entry.
13328 *
13329 * This routine handles an ELS work-queue completion event.
13330 *
13331 * Return: true if work posted to worker thread, otherwise false.
13332 **/
13333static bool
2a76a283 13334lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13335 struct lpfc_wcqe_complete *wcqe)
13336{
4f774513
JS
13337 struct lpfc_iocbq *irspiocbq;
13338 unsigned long iflags;
2a76a283 13339 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
13340 int txq_cnt = 0;
13341 int txcmplq_cnt = 0;
13342 int fcp_txcmplq_cnt = 0;
4f774513 13343
11f0e34f
JS
13344 /* Check for response status */
13345 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13346 /* Log the error status */
13347 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13348 "0357 ELS CQE error: status=x%x: "
13349 "CQE: %08x %08x %08x %08x\n",
13350 bf_get(lpfc_wcqe_c_status, wcqe),
13351 wcqe->word0, wcqe->total_data_placed,
13352 wcqe->parameter, wcqe->word3);
13353 }
13354
45ed1190 13355 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
13356 irspiocbq = lpfc_sli_get_iocbq(phba);
13357 if (!irspiocbq) {
0e9bb8d7
JS
13358 if (!list_empty(&pring->txq))
13359 txq_cnt++;
13360 if (!list_empty(&pring->txcmplq))
13361 txcmplq_cnt++;
4f774513 13362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
13363 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
13364 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
13365 txq_cnt, phba->iocb_cnt,
13366 fcp_txcmplq_cnt,
13367 txcmplq_cnt);
45ed1190 13368 return false;
4f774513 13369 }
4f774513 13370
45ed1190
JS
13371 /* Save off the slow-path queue event for work thread to process */
13372 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 13373 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 13374 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
13375 &phba->sli4_hba.sp_queue_event);
13376 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 13377 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 13378
45ed1190 13379 return true;
4f774513
JS
13380}
13381
13382/**
13383 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13384 * @phba: Pointer to HBA context object.
13385 * @wcqe: Pointer to work-queue completion queue entry.
13386 *
3f8b6fb7 13387 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
13388 * proper WQ release routine to the slow-path WQ.
13389 **/
13390static void
13391lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13392 struct lpfc_wcqe_release *wcqe)
13393{
2e90f4b5
JS
13394 /* sanity check on queue memory */
13395 if (unlikely(!phba->sli4_hba.els_wq))
13396 return;
4f774513
JS
13397 /* Check for the slow-path ELS work queue */
13398 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13399 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13400 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13401 else
13402 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13403 "2579 Slow-path wqe consume event carries "
13404 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13405 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13406 phba->sli4_hba.els_wq->queue_id);
13407}
13408
13409/**
13410 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13411 * @phba: Pointer to HBA context object.
13412 * @cq: Pointer to a WQ completion queue.
13413 * @wcqe: Pointer to work-queue completion queue entry.
13414 *
13415 * This routine handles an XRI abort event.
13416 *
13417 * Return: true if work posted to worker thread, otherwise false.
13418 **/
13419static bool
13420lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13421 struct lpfc_queue *cq,
13422 struct sli4_wcqe_xri_aborted *wcqe)
13423{
13424 bool workposted = false;
13425 struct lpfc_cq_event *cq_event;
13426 unsigned long iflags;
13427
4f774513
JS
13428 switch (cq->subtype) {
13429 case LPFC_FCP:
8a5ca109
JS
13430 cq_event = lpfc_cq_event_setup(
13431 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13432 if (!cq_event)
13433 return false;
4f774513
JS
13434 spin_lock_irqsave(&phba->hbalock, iflags);
13435 list_add_tail(&cq_event->list,
13436 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13437 /* Set the fcp xri abort event flag */
13438 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13439 spin_unlock_irqrestore(&phba->hbalock, iflags);
13440 workposted = true;
13441 break;
422c4cb7 13442 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 13443 case LPFC_ELS:
8a5ca109
JS
13444 cq_event = lpfc_cq_event_setup(
13445 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13446 if (!cq_event)
13447 return false;
4f774513
JS
13448 spin_lock_irqsave(&phba->hbalock, iflags);
13449 list_add_tail(&cq_event->list,
13450 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13451 /* Set the els xri abort event flag */
13452 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13453 spin_unlock_irqrestore(&phba->hbalock, iflags);
13454 workposted = true;
13455 break;
318083ad 13456 case LPFC_NVME:
8a5ca109
JS
13457 /* Notify aborted XRI for NVME work queue */
13458 if (phba->nvmet_support)
13459 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13460 else
13461 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13462
13463 workposted = false;
318083ad 13464 break;
4f774513
JS
13465 default:
13466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
13467 "0603 Invalid CQ subtype %d: "
13468 "%08x %08x %08x %08x\n",
13469 cq->subtype, wcqe->word0, wcqe->parameter,
13470 wcqe->word2, wcqe->word3);
4f774513
JS
13471 workposted = false;
13472 break;
13473 }
13474 return workposted;
13475}
13476
4f774513
JS
13477/**
13478 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13479 * @phba: Pointer to HBA context object.
13480 * @rcqe: Pointer to receive-queue completion queue entry.
13481 *
13482 * This routine process a receive-queue completion queue entry.
13483 *
13484 * Return: true if work posted to worker thread, otherwise false.
13485 **/
13486static bool
4d9ab994 13487lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 13488{
4f774513
JS
13489 bool workposted = false;
13490 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13491 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 13492 struct lpfc_nvmet_tgtport *tgtp;
4f774513 13493 struct hbq_dmabuf *dma_buf;
7851fe2c 13494 uint32_t status, rq_id;
4f774513
JS
13495 unsigned long iflags;
13496
2e90f4b5
JS
13497 /* sanity check on queue memory */
13498 if (unlikely(!hrq) || unlikely(!drq))
13499 return workposted;
13500
7851fe2c
JS
13501 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13502 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13503 else
13504 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13505 if (rq_id != hrq->queue_id)
4f774513
JS
13506 goto out;
13507
4d9ab994 13508 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
13509 switch (status) {
13510 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13512 "2537 Receive Frame Truncated!!\n");
13513 case FC_STATUS_RQ_SUCCESS:
13514 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13515 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
13516 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13517 if (!dma_buf) {
b84daac9 13518 hrq->RQ_no_buf_found++;
4f774513
JS
13519 spin_unlock_irqrestore(&phba->hbalock, iflags);
13520 goto out;
13521 }
b84daac9 13522 hrq->RQ_rcv_buf++;
547077a4 13523 hrq->RQ_buf_posted--;
4d9ab994 13524 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd 13525
4f774513 13526 /* save off the frame for the word thread to process */
4d9ab994 13527 list_add_tail(&dma_buf->cq_event.list,
45ed1190 13528 &phba->sli4_hba.sp_queue_event);
4f774513 13529 /* Frame received */
45ed1190 13530 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
13531 spin_unlock_irqrestore(&phba->hbalock, iflags);
13532 workposted = true;
13533 break;
4f774513 13534 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13535 if (phba->nvmet_support) {
13536 tgtp = phba->targetport->private;
13537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13538 "6402 RQE Error x%x, posted %d err_cnt "
13539 "%d: %x %x %x\n",
13540 status, hrq->RQ_buf_posted,
13541 hrq->RQ_no_posted_buf,
13542 atomic_read(&tgtp->rcv_fcp_cmd_in),
13543 atomic_read(&tgtp->rcv_fcp_cmd_out),
13544 atomic_read(&tgtp->xmt_fcp_release));
13545 }
13546 /* fallthrough */
13547
13548 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 13549 hrq->RQ_no_posted_buf++;
4f774513
JS
13550 /* Post more buffers if possible */
13551 spin_lock_irqsave(&phba->hbalock, iflags);
13552 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13553 spin_unlock_irqrestore(&phba->hbalock, iflags);
13554 workposted = true;
13555 break;
13556 }
13557out:
13558 return workposted;
4f774513
JS
13559}
13560
4d9ab994
JS
13561/**
13562 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13563 * @phba: Pointer to HBA context object.
13564 * @cq: Pointer to the completion queue.
13565 * @wcqe: Pointer to a completion queue entry.
13566 *
25985edc 13567 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
13568 * entry.
13569 *
13570 * Return: true if work posted to worker thread, otherwise false.
13571 **/
13572static bool
13573lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13574 struct lpfc_cqe *cqe)
13575{
45ed1190 13576 struct lpfc_cqe cqevt;
4d9ab994
JS
13577 bool workposted = false;
13578
13579 /* Copy the work queue CQE and convert endian order if needed */
48f8fdb4 13580 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
13581
13582 /* Check and process for different type of WCQE and dispatch */
45ed1190 13583 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 13584 case CQE_CODE_COMPL_WQE:
45ed1190 13585 /* Process the WQ/RQ complete event */
bc73905a 13586 phba->last_completion_time = jiffies;
2a76a283 13587 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 13588 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
13589 break;
13590 case CQE_CODE_RELEASE_WQE:
13591 /* Process the WQ release event */
13592 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 13593 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
13594 break;
13595 case CQE_CODE_XRI_ABORTED:
13596 /* Process the WQ XRI abort event */
bc73905a 13597 phba->last_completion_time = jiffies;
4d9ab994 13598 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 13599 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
13600 break;
13601 case CQE_CODE_RECEIVE:
7851fe2c 13602 case CQE_CODE_RECEIVE_V1:
4d9ab994 13603 /* Process the RQ event */
bc73905a 13604 phba->last_completion_time = jiffies;
4d9ab994 13605 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 13606 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
13607 break;
13608 default:
13609 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13610 "0388 Not a valid WCQE code: x%x\n",
45ed1190 13611 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
13612 break;
13613 }
13614 return workposted;
13615}
13616
4f774513
JS
13617/**
13618 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13619 * @phba: Pointer to HBA context object.
13620 * @eqe: Pointer to fast-path event queue entry.
13621 *
13622 * This routine process a event queue entry from the slow-path event queue.
13623 * It will check the MajorCode and MinorCode to determine this is for a
13624 * completion event on a completion queue, if not, an error shall be logged
13625 * and just return. Otherwise, it will get to the corresponding completion
13626 * queue and process all the entries on that completion queue, rearm the
13627 * completion queue, and then return.
13628 *
13629 **/
f485c18d 13630static void
67d12733
JS
13631lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13632 struct lpfc_queue *speq)
4f774513 13633{
67d12733 13634 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13635 uint16_t cqid;
13636
4f774513 13637 /* Get the reference to the corresponding CQ */
cb5172ea 13638 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13639
4f774513
JS
13640 list_for_each_entry(childq, &speq->child_list, list) {
13641 if (childq->queue_id == cqid) {
13642 cq = childq;
13643 break;
13644 }
13645 }
13646 if (unlikely(!cq)) {
75baf696
JS
13647 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13649 "0365 Slow-path CQ identifier "
13650 "(%d) does not exist\n", cqid);
f485c18d 13651 return;
4f774513
JS
13652 }
13653
895427bd
JS
13654 /* Save EQ associated with this CQ */
13655 cq->assoc_qp = speq;
13656
f485c18d
DK
13657 if (!queue_work(phba->wq, &cq->spwork))
13658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13659 "0390 Cannot schedule soft IRQ "
13660 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13661 cqid, cq->queue_id, smp_processor_id());
13662}
13663
13664/**
13665 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13666 * @phba: Pointer to HBA context object.
13667 *
13668 * This routine process a event queue entry from the slow-path event queue.
13669 * It will check the MajorCode and MinorCode to determine this is for a
13670 * completion event on a completion queue, if not, an error shall be logged
13671 * and just return. Otherwise, it will get to the corresponding completion
13672 * queue and process all the entries on that completion queue, rearm the
13673 * completion queue, and then return.
13674 *
13675 **/
13676static void
13677lpfc_sli4_sp_process_cq(struct work_struct *work)
13678{
13679 struct lpfc_queue *cq =
13680 container_of(work, struct lpfc_queue, spwork);
13681 struct lpfc_hba *phba = cq->phba;
13682 struct lpfc_cqe *cqe;
13683 bool workposted = false;
13684 int ccount = 0;
13685
4f774513
JS
13686 /* Process all the entries to the CQ */
13687 switch (cq->type) {
13688 case LPFC_MCQ:
13689 while ((cqe = lpfc_sli4_cq_get(cq))) {
13690 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13691 if (!(++ccount % cq->entry_repost))
7869da18 13692 break;
b84daac9 13693 cq->CQ_mbox++;
4f774513
JS
13694 }
13695 break;
13696 case LPFC_WCQ:
13697 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13698 if (cq->subtype == LPFC_FCP ||
13699 cq->subtype == LPFC_NVME) {
13700#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13701 if (phba->ktime_on)
13702 cq->isr_timestamp = ktime_get_ns();
13703 else
13704 cq->isr_timestamp = 0;
13705#endif
895427bd 13706 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13707 cqe);
c8a4ce0b 13708 } else {
0558056c
JS
13709 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13710 cqe);
c8a4ce0b 13711 }
f485c18d 13712 if (!(++ccount % cq->entry_repost))
7869da18 13713 break;
4f774513 13714 }
b84daac9
JS
13715
13716 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13717 if (ccount > cq->CQ_max_cqe)
13718 cq->CQ_max_cqe = ccount;
4f774513
JS
13719 break;
13720 default:
13721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13722 "0370 Invalid completion queue type (%d)\n",
13723 cq->type);
f485c18d 13724 return;
4f774513
JS
13725 }
13726
13727 /* Catch the no cq entry condition, log an error */
f485c18d 13728 if (unlikely(ccount == 0))
4f774513
JS
13729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13730 "0371 No entry from the CQ: identifier "
13731 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13732
13733 /* In any case, flash and re-arm the RCQ */
b71413dd 13734 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13735
13736 /* wake up worker thread if there are works to be done */
13737 if (workposted)
13738 lpfc_worker_wake_up(phba);
13739}
13740
13741/**
13742 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13743 * @phba: Pointer to HBA context object.
13744 * @cq: Pointer to associated CQ
13745 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13746 *
13747 * This routine process a fast-path work queue completion entry from fast-path
13748 * event queue for FCP command response completion.
13749 **/
13750static void
2a76a283 13751lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13752 struct lpfc_wcqe_complete *wcqe)
13753{
2a76a283 13754 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13755 struct lpfc_iocbq *cmdiocbq;
13756 struct lpfc_iocbq irspiocbq;
13757 unsigned long iflags;
13758
4f774513
JS
13759 /* Check for response status */
13760 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13761 /* If resource errors reported from HBA, reduce queue
13762 * depth of the SCSI device.
13763 */
e3d2b802
JS
13764 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13765 IOSTAT_LOCAL_REJECT)) &&
13766 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13767 IOERR_NO_RESOURCES))
4f774513 13768 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13769
4f774513 13770 /* Log the error status */
11f0e34f
JS
13771 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13772 "0373 FCP CQE error: status=x%x: "
13773 "CQE: %08x %08x %08x %08x\n",
4f774513 13774 bf_get(lpfc_wcqe_c_status, wcqe),
11f0e34f
JS
13775 wcqe->word0, wcqe->total_data_placed,
13776 wcqe->parameter, wcqe->word3);
4f774513
JS
13777 }
13778
13779 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13780 spin_lock_irqsave(&pring->ring_lock, iflags);
13781 pring->stats.iocb_event++;
4f774513
JS
13782 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13783 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13784 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13785 if (unlikely(!cmdiocbq)) {
13786 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13787 "0374 FCP complete with no corresponding "
13788 "cmdiocb: iotag (%d)\n",
13789 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13790 return;
13791 }
c8a4ce0b
DK
13792#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13793 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13794#endif
895427bd
JS
13795 if (cmdiocbq->iocb_cmpl == NULL) {
13796 if (cmdiocbq->wqe_cmpl) {
13797 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13798 spin_lock_irqsave(&phba->hbalock, iflags);
13799 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13800 spin_unlock_irqrestore(&phba->hbalock, iflags);
13801 }
13802
13803 /* Pass the cmd_iocb and the wcqe to the upper layer */
13804 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13805 return;
13806 }
4f774513
JS
13807 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13808 "0375 FCP cmdiocb not callback function "
13809 "iotag: (%d)\n",
13810 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13811 return;
13812 }
13813
13814 /* Fake the irspiocb and copy necessary response information */
341af102 13815 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13816
0f65ff68
JS
13817 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13818 spin_lock_irqsave(&phba->hbalock, iflags);
13819 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13820 spin_unlock_irqrestore(&phba->hbalock, iflags);
13821 }
13822
4f774513
JS
13823 /* Pass the cmd_iocb and the rsp state to the upper layer */
13824 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13825}
13826
13827/**
13828 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13829 * @phba: Pointer to HBA context object.
13830 * @cq: Pointer to completion queue.
13831 * @wcqe: Pointer to work-queue completion queue entry.
13832 *
3f8b6fb7 13833 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13834 * proper WQ release routine to the slow-path WQ.
13835 **/
13836static void
13837lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13838 struct lpfc_wcqe_release *wcqe)
13839{
13840 struct lpfc_queue *childwq;
13841 bool wqid_matched = false;
895427bd 13842 uint16_t hba_wqid;
4f774513
JS
13843
13844 /* Check for fast-path FCP work queue release */
895427bd 13845 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13846 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13847 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13848 lpfc_sli4_wq_release(childwq,
13849 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
6e8e1c14
JS
13850 if (childwq->q_flag & HBA_NVMET_WQFULL)
13851 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13852 wqid_matched = true;
13853 break;
13854 }
13855 }
13856 /* Report warning log message if no match found */
13857 if (wqid_matched != true)
13858 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13859 "2580 Fast-path wqe consume event carries "
895427bd 13860 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13861}
13862
13863/**
2d7dbc4c
JS
13864 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13865 * @phba: Pointer to HBA context object.
13866 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13867 *
2d7dbc4c
JS
13868 * This routine process a receive-queue completion queue entry.
13869 *
13870 * Return: true if work posted to worker thread, otherwise false.
13871 **/
13872static bool
13873lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13874 struct lpfc_rcqe *rcqe)
13875{
13876 bool workposted = false;
13877 struct lpfc_queue *hrq;
13878 struct lpfc_queue *drq;
13879 struct rqb_dmabuf *dma_buf;
13880 struct fc_frame_header *fc_hdr;
547077a4 13881 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13882 uint32_t status, rq_id;
13883 unsigned long iflags;
13884 uint32_t fctl, idx;
13885
13886 if ((phba->nvmet_support == 0) ||
13887 (phba->sli4_hba.nvmet_cqset == NULL))
13888 return workposted;
13889
13890 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13891 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13892 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13893
13894 /* sanity check on queue memory */
13895 if (unlikely(!hrq) || unlikely(!drq))
13896 return workposted;
13897
13898 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13899 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13900 else
13901 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13902
13903 if ((phba->nvmet_support == 0) ||
13904 (rq_id != hrq->queue_id))
13905 return workposted;
13906
13907 status = bf_get(lpfc_rcqe_status, rcqe);
13908 switch (status) {
13909 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13911 "6126 Receive Frame Truncated!!\n");
78e1d200 13912 /* Drop thru */
2d7dbc4c 13913 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13914 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13915 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13916 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13917 if (!dma_buf) {
13918 hrq->RQ_no_buf_found++;
13919 spin_unlock_irqrestore(&phba->hbalock, iflags);
13920 goto out;
13921 }
13922 spin_unlock_irqrestore(&phba->hbalock, iflags);
13923 hrq->RQ_rcv_buf++;
547077a4 13924 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13925 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13926
13927 /* Just some basic sanity checks on FCP Command frame */
13928 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13929 fc_hdr->fh_f_ctl[1] << 8 |
13930 fc_hdr->fh_f_ctl[2]);
13931 if (((fctl &
13932 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13933 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13934 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13935 goto drop;
13936
13937 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13938 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13939 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13940 phba, idx, dma_buf,
c8a4ce0b 13941 cq->isr_timestamp);
2d7dbc4c
JS
13942 return false;
13943 }
13944drop:
13945 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13946 break;
2d7dbc4c 13947 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13948 if (phba->nvmet_support) {
13949 tgtp = phba->targetport->private;
13950 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13951 "6401 RQE Error x%x, posted %d err_cnt "
13952 "%d: %x %x %x\n",
13953 status, hrq->RQ_buf_posted,
13954 hrq->RQ_no_posted_buf,
13955 atomic_read(&tgtp->rcv_fcp_cmd_in),
13956 atomic_read(&tgtp->rcv_fcp_cmd_out),
13957 atomic_read(&tgtp->xmt_fcp_release));
13958 }
13959 /* fallthrough */
13960
13961 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13962 hrq->RQ_no_posted_buf++;
13963 /* Post more buffers if possible */
2d7dbc4c
JS
13964 break;
13965 }
13966out:
13967 return workposted;
13968}
13969
4f774513 13970/**
895427bd 13971 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13972 * @cq: Pointer to the completion queue.
13973 * @eqe: Pointer to fast-path completion queue entry.
13974 *
13975 * This routine process a fast-path work queue completion entry from fast-path
13976 * event queue for FCP command response completion.
13977 **/
13978static int
895427bd 13979lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13980 struct lpfc_cqe *cqe)
13981{
13982 struct lpfc_wcqe_release wcqe;
13983 bool workposted = false;
13984
13985 /* Copy the work queue CQE and convert endian order if needed */
48f8fdb4 13986 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
4f774513
JS
13987
13988 /* Check and process for different type of WCQE and dispatch */
13989 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13990 case CQE_CODE_COMPL_WQE:
895427bd 13991 case CQE_CODE_NVME_ERSP:
b84daac9 13992 cq->CQ_wq++;
4f774513 13993 /* Process the WQ complete event */
98fc5dd9 13994 phba->last_completion_time = jiffies;
895427bd
JS
13995 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13996 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13997 (struct lpfc_wcqe_complete *)&wcqe);
13998 if (cq->subtype == LPFC_NVME_LS)
13999 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
14000 (struct lpfc_wcqe_complete *)&wcqe);
14001 break;
14002 case CQE_CODE_RELEASE_WQE:
b84daac9 14003 cq->CQ_release_wqe++;
4f774513
JS
14004 /* Process the WQ release event */
14005 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14006 (struct lpfc_wcqe_release *)&wcqe);
14007 break;
14008 case CQE_CODE_XRI_ABORTED:
b84daac9 14009 cq->CQ_xri_aborted++;
4f774513 14010 /* Process the WQ XRI abort event */
bc73905a 14011 phba->last_completion_time = jiffies;
4f774513
JS
14012 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14013 (struct sli4_wcqe_xri_aborted *)&wcqe);
14014 break;
895427bd
JS
14015 case CQE_CODE_RECEIVE_V1:
14016 case CQE_CODE_RECEIVE:
14017 phba->last_completion_time = jiffies;
2d7dbc4c
JS
14018 if (cq->subtype == LPFC_NVMET) {
14019 workposted = lpfc_sli4_nvmet_handle_rcqe(
14020 phba, cq, (struct lpfc_rcqe *)&wcqe);
14021 }
895427bd 14022 break;
4f774513
JS
14023 default:
14024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 14025 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
14026 bf_get(lpfc_wcqe_c_code, &wcqe));
14027 break;
14028 }
14029 return workposted;
14030}
14031
14032/**
67d12733 14033 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
14034 * @phba: Pointer to HBA context object.
14035 * @eqe: Pointer to fast-path event queue entry.
14036 *
14037 * This routine process a event queue entry from the fast-path event queue.
14038 * It will check the MajorCode and MinorCode to determine this is for a
14039 * completion event on a completion queue, if not, an error shall be logged
14040 * and just return. Otherwise, it will get to the corresponding completion
14041 * queue and process all the entries on the completion queue, rearm the
14042 * completion queue, and then return.
14043 **/
f485c18d 14044static void
67d12733
JS
14045lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14046 uint32_t qidx)
4f774513 14047{
895427bd 14048 struct lpfc_queue *cq = NULL;
2d7dbc4c 14049 uint16_t cqid, id;
4f774513 14050
cb5172ea 14051 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 14052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 14053 "0366 Not a valid completion "
4f774513 14054 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
14055 bf_get_le32(lpfc_eqe_major_code, eqe),
14056 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 14057 return;
4f774513
JS
14058 }
14059
67d12733
JS
14060 /* Get the reference to the corresponding CQ */
14061 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14062
2d7dbc4c
JS
14063 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14064 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14065 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14066 /* Process NVMET unsol rcv */
14067 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14068 goto process_cq;
14069 }
67d12733
JS
14070 }
14071
895427bd
JS
14072 if (phba->sli4_hba.nvme_cq_map &&
14073 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 14074 /* Process NVME / NVMET command completion */
895427bd
JS
14075 cq = phba->sli4_hba.nvme_cq[qidx];
14076 goto process_cq;
2e90f4b5 14077 }
67d12733 14078
895427bd
JS
14079 if (phba->sli4_hba.fcp_cq_map &&
14080 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
14081 /* Process FCP command completion */
14082 cq = phba->sli4_hba.fcp_cq[qidx];
14083 goto process_cq;
2e90f4b5 14084 }
895427bd
JS
14085
14086 if (phba->sli4_hba.nvmels_cq &&
14087 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14088 /* Process NVME unsol rcv */
14089 cq = phba->sli4_hba.nvmels_cq;
14090 }
14091
14092 /* Otherwise this is a Slow path event */
14093 if (cq == NULL) {
f485c18d
DK
14094 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
14095 return;
4f774513
JS
14096 }
14097
895427bd 14098process_cq:
4f774513
JS
14099 if (unlikely(cqid != cq->queue_id)) {
14100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14101 "0368 Miss-matched fast-path completion "
14102 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14103 cqid, cq->queue_id);
f485c18d 14104 return;
4f774513
JS
14105 }
14106
895427bd
JS
14107 /* Save EQ associated with this CQ */
14108 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
14109
f485c18d
DK
14110 if (!queue_work(phba->wq, &cq->irqwork))
14111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14112 "0363 Cannot schedule soft IRQ "
14113 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14114 cqid, cq->queue_id, smp_processor_id());
14115}
14116
14117/**
14118 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14119 * @phba: Pointer to HBA context object.
14120 * @eqe: Pointer to fast-path event queue entry.
14121 *
14122 * This routine process a event queue entry from the fast-path event queue.
14123 * It will check the MajorCode and MinorCode to determine this is for a
14124 * completion event on a completion queue, if not, an error shall be logged
14125 * and just return. Otherwise, it will get to the corresponding completion
14126 * queue and process all the entries on the completion queue, rearm the
14127 * completion queue, and then return.
14128 **/
14129static void
14130lpfc_sli4_hba_process_cq(struct work_struct *work)
14131{
14132 struct lpfc_queue *cq =
14133 container_of(work, struct lpfc_queue, irqwork);
14134 struct lpfc_hba *phba = cq->phba;
14135 struct lpfc_cqe *cqe;
14136 bool workposted = false;
14137 int ccount = 0;
14138
4f774513
JS
14139 /* Process all the entries to the CQ */
14140 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
14141#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14142 if (phba->ktime_on)
14143 cq->isr_timestamp = ktime_get_ns();
14144 else
14145 cq->isr_timestamp = 0;
14146#endif
895427bd 14147 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 14148 if (!(++ccount % cq->entry_repost))
7869da18 14149 break;
4f774513
JS
14150 }
14151
b84daac9 14152 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
14153 if (ccount > cq->CQ_max_cqe)
14154 cq->CQ_max_cqe = ccount;
14155 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 14156
4f774513 14157 /* Catch the no cq entry condition */
f485c18d 14158 if (unlikely(ccount == 0))
4f774513
JS
14159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14160 "0369 No entry from fast-path completion "
14161 "queue fcpcqid=%d\n", cq->queue_id);
14162
14163 /* In any case, flash and re-arm the CQ */
b71413dd 14164 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
14165
14166 /* wake up worker thread if there are works to be done */
14167 if (workposted)
14168 lpfc_worker_wake_up(phba);
14169}
14170
14171static void
14172lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
14173{
14174 struct lpfc_eqe *eqe;
14175
14176 /* walk all the EQ entries and drop on the floor */
14177 while ((eqe = lpfc_sli4_eq_get(eq)))
14178 ;
14179
14180 /* Clear and re-arm the EQ */
b71413dd 14181 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
4f774513
JS
14182}
14183
1ba981fd
JS
14184
14185/**
14186 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
14187 * entry
14188 * @phba: Pointer to HBA context object.
14189 * @eqe: Pointer to fast-path event queue entry.
14190 *
14191 * This routine process a event queue entry from the Flash Optimized Fabric
14192 * event queue. It will check the MajorCode and MinorCode to determine this
14193 * is for a completion event on a completion queue, if not, an error shall be
14194 * logged and just return. Otherwise, it will get to the corresponding
14195 * completion queue and process all the entries on the completion queue, rearm
14196 * the completion queue, and then return.
14197 **/
14198static void
14199lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
14200{
14201 struct lpfc_queue *cq;
1ba981fd 14202 uint16_t cqid;
1ba981fd
JS
14203
14204 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
14205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14206 "9147 Not a valid completion "
14207 "event: majorcode=x%x, minorcode=x%x\n",
14208 bf_get_le32(lpfc_eqe_major_code, eqe),
14209 bf_get_le32(lpfc_eqe_minor_code, eqe));
14210 return;
14211 }
14212
14213 /* Get the reference to the corresponding CQ */
14214 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14215
14216 /* Next check for OAS */
14217 cq = phba->sli4_hba.oas_cq;
14218 if (unlikely(!cq)) {
14219 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14221 "9148 OAS completion queue "
14222 "does not exist\n");
14223 return;
14224 }
14225
14226 if (unlikely(cqid != cq->queue_id)) {
14227 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14228 "9149 Miss-matched fast-path compl "
14229 "queue id: eqcqid=%d, fcpcqid=%d\n",
14230 cqid, cq->queue_id);
14231 return;
14232 }
14233
d41b65bc
JS
14234 /* Save EQ associated with this CQ */
14235 cq->assoc_qp = phba->sli4_hba.fof_eq;
14236
f485c18d
DK
14237 /* CQ work will be processed on CPU affinitized to this IRQ */
14238 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 14239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
14240 "0367 Cannot schedule soft IRQ "
14241 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14242 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
14243}
14244
14245/**
14246 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
14247 * @irq: Interrupt number.
14248 * @dev_id: The device context pointer.
14249 *
14250 * This function is directly called from the PCI layer as an interrupt
14251 * service routine when device with SLI-4 interface spec is enabled with
14252 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
14253 * IOCB ring event in the HBA. However, when the device is enabled with either
14254 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14255 * device-level interrupt handler. When the PCI slot is in error recovery
14256 * or the HBA is undergoing initialization, the interrupt handler will not
14257 * process the interrupt. The Flash Optimized Fabric ring event are handled in
14258 * the intrrupt context. This function is called without any lock held.
14259 * It gets the hbalock to access and update SLI data structures. Note that,
14260 * the EQ to CQ are one-to-one map such that the EQ index is
14261 * equal to that of CQ index.
14262 *
14263 * This function returns IRQ_HANDLED when interrupt is handled else it
14264 * returns IRQ_NONE.
14265 **/
14266irqreturn_t
14267lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
14268{
14269 struct lpfc_hba *phba;
895427bd 14270 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
14271 struct lpfc_queue *eq;
14272 struct lpfc_eqe *eqe;
14273 unsigned long iflag;
14274 int ecount = 0;
1ba981fd
JS
14275
14276 /* Get the driver's phba structure from the dev_id */
895427bd
JS
14277 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14278 phba = hba_eq_hdl->phba;
1ba981fd
JS
14279
14280 if (unlikely(!phba))
14281 return IRQ_NONE;
14282
14283 /* Get to the EQ struct associated with this vector */
14284 eq = phba->sli4_hba.fof_eq;
14285 if (unlikely(!eq))
14286 return IRQ_NONE;
14287
14288 /* Check device state for handling interrupt */
14289 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
14290 /* Check again for link_state with lock held */
14291 spin_lock_irqsave(&phba->hbalock, iflag);
14292 if (phba->link_state < LPFC_LINK_DOWN)
14293 /* Flush, clear interrupt, and rearm the EQ */
14294 lpfc_sli4_eq_flush(phba, eq);
14295 spin_unlock_irqrestore(&phba->hbalock, iflag);
14296 return IRQ_NONE;
14297 }
14298
14299 /*
14300 * Process all the event on FCP fast-path EQ
14301 */
14302 while ((eqe = lpfc_sli4_eq_get(eq))) {
14303 lpfc_sli4_fof_handle_eqe(phba, eqe);
14304 if (!(++ecount % eq->entry_repost))
7869da18 14305 break;
1ba981fd
JS
14306 eq->EQ_processed++;
14307 }
14308
14309 /* Track the max number of EQEs processed in 1 intr */
14310 if (ecount > eq->EQ_max_eqe)
14311 eq->EQ_max_eqe = ecount;
14312
14313
14314 if (unlikely(ecount == 0)) {
14315 eq->EQ_no_entry++;
14316
14317 if (phba->intr_type == MSIX)
14318 /* MSI-X treated interrupt served as no EQ share INT */
14319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14320 "9145 MSI-X interrupt with no EQE\n");
14321 else {
14322 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14323 "9146 ISR interrupt with no EQE\n");
14324 /* Non MSI-X treated on interrupt as EQ share INT */
14325 return IRQ_NONE;
14326 }
14327 }
14328 /* Always clear and re-arm the fast-path EQ */
b71413dd 14329 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
1ba981fd
JS
14330 return IRQ_HANDLED;
14331}
14332
4f774513 14333/**
67d12733 14334 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
14335 * @irq: Interrupt number.
14336 * @dev_id: The device context pointer.
14337 *
14338 * This function is directly called from the PCI layer as an interrupt
14339 * service routine when device with SLI-4 interface spec is enabled with
14340 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14341 * ring event in the HBA. However, when the device is enabled with either
14342 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14343 * device-level interrupt handler. When the PCI slot is in error recovery
14344 * or the HBA is undergoing initialization, the interrupt handler will not
14345 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14346 * the intrrupt context. This function is called without any lock held.
14347 * It gets the hbalock to access and update SLI data structures. Note that,
14348 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14349 * equal to that of FCP CQ index.
14350 *
67d12733
JS
14351 * The link attention and ELS ring attention events are handled
14352 * by the worker thread. The interrupt handler signals the worker thread
14353 * and returns for these events. This function is called without any lock
14354 * held. It gets the hbalock to access and update SLI data structures.
14355 *
4f774513
JS
14356 * This function returns IRQ_HANDLED when interrupt is handled else it
14357 * returns IRQ_NONE.
14358 **/
14359irqreturn_t
67d12733 14360lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
14361{
14362 struct lpfc_hba *phba;
895427bd 14363 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
14364 struct lpfc_queue *fpeq;
14365 struct lpfc_eqe *eqe;
14366 unsigned long iflag;
14367 int ecount = 0;
895427bd 14368 int hba_eqidx;
4f774513
JS
14369
14370 /* Get the driver's phba structure from the dev_id */
895427bd
JS
14371 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14372 phba = hba_eq_hdl->phba;
14373 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
14374
14375 if (unlikely(!phba))
14376 return IRQ_NONE;
67d12733 14377 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 14378 return IRQ_NONE;
4f774513
JS
14379
14380 /* Get to the EQ struct associated with this vector */
895427bd 14381 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
14382 if (unlikely(!fpeq))
14383 return IRQ_NONE;
4f774513 14384
ba20c853 14385 if (lpfc_fcp_look_ahead) {
895427bd 14386 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
b71413dd 14387 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 14388 else {
895427bd 14389 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14390 return IRQ_NONE;
14391 }
14392 }
14393
4f774513
JS
14394 /* Check device state for handling interrupt */
14395 if (unlikely(lpfc_intr_state_check(phba))) {
14396 /* Check again for link_state with lock held */
14397 spin_lock_irqsave(&phba->hbalock, iflag);
14398 if (phba->link_state < LPFC_LINK_DOWN)
14399 /* Flush, clear interrupt, and rearm the EQ */
14400 lpfc_sli4_eq_flush(phba, fpeq);
14401 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 14402 if (lpfc_fcp_look_ahead)
895427bd 14403 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
14404 return IRQ_NONE;
14405 }
14406
14407 /*
14408 * Process all the event on FCP fast-path EQ
14409 */
14410 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
14411 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14412 if (!(++ecount % fpeq->entry_repost))
7869da18 14413 break;
b84daac9 14414 fpeq->EQ_processed++;
4f774513
JS
14415 }
14416
b84daac9
JS
14417 /* Track the max number of EQEs processed in 1 intr */
14418 if (ecount > fpeq->EQ_max_eqe)
14419 fpeq->EQ_max_eqe = ecount;
14420
4f774513 14421 /* Always clear and re-arm the fast-path EQ */
b71413dd 14422 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
4f774513
JS
14423
14424 if (unlikely(ecount == 0)) {
b84daac9 14425 fpeq->EQ_no_entry++;
ba20c853
JS
14426
14427 if (lpfc_fcp_look_ahead) {
895427bd 14428 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14429 return IRQ_NONE;
14430 }
14431
4f774513
JS
14432 if (phba->intr_type == MSIX)
14433 /* MSI-X treated interrupt served as no EQ share INT */
14434 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14435 "0358 MSI-X interrupt with no EQE\n");
14436 else
14437 /* Non MSI-X treated on interrupt as EQ share INT */
14438 return IRQ_NONE;
14439 }
14440
ba20c853 14441 if (lpfc_fcp_look_ahead)
895427bd
JS
14442 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14443
4f774513
JS
14444 return IRQ_HANDLED;
14445} /* lpfc_sli4_fp_intr_handler */
14446
14447/**
14448 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14449 * @irq: Interrupt number.
14450 * @dev_id: The device context pointer.
14451 *
14452 * This function is the device-level interrupt handler to device with SLI-4
14453 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14454 * interrupt mode is enabled and there is an event in the HBA which requires
14455 * driver attention. This function invokes the slow-path interrupt attention
14456 * handling function and fast-path interrupt attention handling function in
14457 * turn to process the relevant HBA attention events. This function is called
14458 * without any lock held. It gets the hbalock to access and update SLI data
14459 * structures.
14460 *
14461 * This function returns IRQ_HANDLED when interrupt is handled, else it
14462 * returns IRQ_NONE.
14463 **/
14464irqreturn_t
14465lpfc_sli4_intr_handler(int irq, void *dev_id)
14466{
14467 struct lpfc_hba *phba;
67d12733
JS
14468 irqreturn_t hba_irq_rc;
14469 bool hba_handled = false;
895427bd 14470 int qidx;
4f774513
JS
14471
14472 /* Get the driver's phba structure from the dev_id */
14473 phba = (struct lpfc_hba *)dev_id;
14474
14475 if (unlikely(!phba))
14476 return IRQ_NONE;
14477
4f774513
JS
14478 /*
14479 * Invoke fast-path host attention interrupt handling as appropriate.
14480 */
895427bd 14481 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 14482 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 14483 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
14484 if (hba_irq_rc == IRQ_HANDLED)
14485 hba_handled |= true;
4f774513
JS
14486 }
14487
1ba981fd
JS
14488 if (phba->cfg_fof) {
14489 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 14490 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
14491 if (hba_irq_rc == IRQ_HANDLED)
14492 hba_handled |= true;
14493 }
14494
67d12733 14495 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
14496} /* lpfc_sli4_intr_handler */
14497
14498/**
14499 * lpfc_sli4_queue_free - free a queue structure and associated memory
14500 * @queue: The queue structure to free.
14501 *
b595076a 14502 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
14503 * the host resident queue. This function must be called after destroying the
14504 * queue on the HBA.
14505 **/
14506void
14507lpfc_sli4_queue_free(struct lpfc_queue *queue)
14508{
14509 struct lpfc_dmabuf *dmabuf;
14510
14511 if (!queue)
14512 return;
14513
14514 while (!list_empty(&queue->page_list)) {
14515 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14516 list);
81b96eda 14517 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
14518 dmabuf->virt, dmabuf->phys);
14519 kfree(dmabuf);
14520 }
895427bd
JS
14521 if (queue->rqbp) {
14522 lpfc_free_rq_buffer(queue->phba, queue);
14523 kfree(queue->rqbp);
14524 }
d1f525aa
JS
14525
14526 if (!list_empty(&queue->wq_list))
14527 list_del(&queue->wq_list);
14528
4f774513
JS
14529 kfree(queue);
14530 return;
14531}
14532
14533/**
14534 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14535 * @phba: The HBA that this queue is being created on.
81b96eda 14536 * @page_size: The size of a queue page
4f774513
JS
14537 * @entry_size: The size of each queue entry for this queue.
14538 * @entry count: The number of entries that this queue will handle.
14539 *
14540 * This function allocates a queue structure and the DMAable memory used for
14541 * the host resident queue. This function must be called before creating the
14542 * queue on the HBA.
14543 **/
14544struct lpfc_queue *
81b96eda
JS
14545lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14546 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
14547{
14548 struct lpfc_queue *queue;
14549 struct lpfc_dmabuf *dmabuf;
14550 int x, total_qe_count;
14551 void *dma_pointer;
cb5172ea 14552 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 14553
cb5172ea 14554 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14555 hw_page_size = page_size;
cb5172ea 14556
4f774513
JS
14557 queue = kzalloc(sizeof(struct lpfc_queue) +
14558 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14559 if (!queue)
14560 return NULL;
cb5172ea
JS
14561 queue->page_count = (ALIGN(entry_size * entry_count,
14562 hw_page_size))/hw_page_size;
895427bd
JS
14563
14564 /* If needed, Adjust page count to match the max the adapter supports */
14565 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14566 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14567
4f774513 14568 INIT_LIST_HEAD(&queue->list);
895427bd 14569 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 14570 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
14571 INIT_LIST_HEAD(&queue->page_list);
14572 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
14573
14574 /* Set queue parameters now. If the system cannot provide memory
14575 * resources, the free routine needs to know what was allocated.
14576 */
14577 queue->entry_size = entry_size;
14578 queue->entry_count = entry_count;
14579 queue->page_size = hw_page_size;
14580 queue->phba = phba;
14581
4f774513
JS
14582 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14583 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14584 if (!dmabuf)
14585 goto out_fail;
1aee383d
JP
14586 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14587 hw_page_size, &dmabuf->phys,
14588 GFP_KERNEL);
4f774513
JS
14589 if (!dmabuf->virt) {
14590 kfree(dmabuf);
14591 goto out_fail;
14592 }
14593 dmabuf->buffer_tag = x;
14594 list_add_tail(&dmabuf->list, &queue->page_list);
14595 /* initialize queue's entry array */
14596 dma_pointer = dmabuf->virt;
14597 for (; total_qe_count < entry_count &&
cb5172ea 14598 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
14599 total_qe_count++, dma_pointer += entry_size) {
14600 queue->qe[total_qe_count].address = dma_pointer;
14601 }
14602 }
f485c18d
DK
14603 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14604 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 14605
64eb4dcb
JS
14606 /* entry_repost will be set during q creation */
14607
4f774513
JS
14608 return queue;
14609out_fail:
14610 lpfc_sli4_queue_free(queue);
14611 return NULL;
14612}
14613
962bc51b
JS
14614/**
14615 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14616 * @phba: HBA structure that indicates port to create a queue on.
14617 * @pci_barset: PCI BAR set flag.
14618 *
14619 * This function shall perform iomap of the specified PCI BAR address to host
14620 * memory address if not already done so and return it. The returned host
14621 * memory address can be NULL.
14622 */
14623static void __iomem *
14624lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14625{
962bc51b
JS
14626 if (!phba->pcidev)
14627 return NULL;
962bc51b
JS
14628
14629 switch (pci_barset) {
14630 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14631 return phba->pci_bar0_memmap_p;
14632 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14633 return phba->pci_bar2_memmap_p;
14634 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14635 return phba->pci_bar4_memmap_p;
14636 default:
14637 break;
14638 }
14639 return NULL;
14640}
14641
173edbb2 14642/**
895427bd 14643 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14644 * @phba: HBA structure that indicates port to create a queue on.
14645 * @startq: The starting FCP EQ to modify
14646 *
14647 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14648 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14649 * updated in one mailbox command.
173edbb2
JS
14650 *
14651 * The @phba struct is used to send mailbox command to HBA. The @startq
14652 * is used to get the starting FCP EQ to change.
14653 * This function is asynchronous and will wait for the mailbox
14654 * command to finish before continuing.
14655 *
14656 * On success this function will return a zero. If unable to allocate enough
14657 * memory this function will return -ENOMEM. If the queue create mailbox command
14658 * fails this function will return -ENXIO.
14659 **/
a2fc4aef 14660int
0cf07f84
JS
14661lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14662 uint32_t numq, uint32_t imax)
173edbb2
JS
14663{
14664 struct lpfc_mbx_modify_eq_delay *eq_delay;
14665 LPFC_MBOXQ_t *mbox;
14666 struct lpfc_queue *eq;
14667 int cnt, rc, length, status = 0;
14668 uint32_t shdr_status, shdr_add_status;
0cf07f84 14669 uint32_t result, val;
895427bd 14670 int qidx;
173edbb2
JS
14671 union lpfc_sli4_cfg_shdr *shdr;
14672 uint16_t dmult;
14673
895427bd 14674 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14675 return 0;
14676
14677 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14678 if (!mbox)
14679 return -ENOMEM;
14680 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14681 sizeof(struct lpfc_sli4_cfg_mhdr));
14682 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14683 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14684 length, LPFC_SLI4_MBX_EMBED);
14685 eq_delay = &mbox->u.mqe.un.eq_delay;
14686
14687 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14688 result = imax / phba->io_channel_irqs;
895427bd 14689 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14690 dmult = 0;
14691 else
14692 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14693 if (dmult > LPFC_DMULT_MAX)
14694 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14695
14696 cnt = 0;
895427bd
JS
14697 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14698 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14699 if (!eq)
14700 continue;
0cf07f84 14701 eq->q_mode = imax;
173edbb2
JS
14702 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14703 eq_delay->u.request.eq[cnt].phase = 0;
14704 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14705 cnt++;
0cf07f84
JS
14706
14707 /* q_mode is only used for auto_imax */
14708 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14709 /* Use EQ Delay Register method for q_mode */
14710
14711 /* Convert for EQ Delay register */
14712 val = phba->cfg_fcp_imax;
14713 if (val) {
14714 /* First, interrupts per sec per EQ */
14715 val = phba->cfg_fcp_imax /
14716 phba->io_channel_irqs;
14717
14718 /* us delay between each interrupt */
14719 val = LPFC_SEC_TO_USEC / val;
14720 }
14721 eq->q_mode = val;
14722 } else {
14723 eq->q_mode = imax;
14724 }
14725
14726 if (cnt >= numq)
173edbb2
JS
14727 break;
14728 }
14729 eq_delay->u.request.num_eq = cnt;
14730
14731 mbox->vport = phba->pport;
14732 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3e1f0718
JS
14733 mbox->ctx_buf = NULL;
14734 mbox->ctx_ndlp = NULL;
173edbb2
JS
14735 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14736 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14737 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14739 if (shdr_status || shdr_add_status || rc) {
14740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14741 "2512 MODIFY_EQ_DELAY mailbox failed with "
14742 "status x%x add_status x%x, mbx status x%x\n",
14743 shdr_status, shdr_add_status, rc);
14744 status = -ENXIO;
14745 }
14746 mempool_free(mbox, phba->mbox_mem_pool);
14747 return status;
14748}
14749
4f774513
JS
14750/**
14751 * lpfc_eq_create - Create an Event Queue on the HBA
14752 * @phba: HBA structure that indicates port to create a queue on.
14753 * @eq: The queue structure to use to create the event queue.
14754 * @imax: The maximum interrupt per second limit.
14755 *
14756 * This function creates an event queue, as detailed in @eq, on a port,
14757 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14758 *
14759 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14760 * is used to get the entry count and entry size that are necessary to
14761 * determine the number of pages to allocate and use for this queue. This
14762 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14763 * event queue. This function is asynchronous and will wait for the mailbox
14764 * command to finish before continuing.
14765 *
14766 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14767 * memory this function will return -ENOMEM. If the queue create mailbox command
14768 * fails this function will return -ENXIO.
4f774513 14769 **/
a2fc4aef 14770int
ee02006b 14771lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14772{
14773 struct lpfc_mbx_eq_create *eq_create;
14774 LPFC_MBOXQ_t *mbox;
14775 int rc, length, status = 0;
14776 struct lpfc_dmabuf *dmabuf;
14777 uint32_t shdr_status, shdr_add_status;
14778 union lpfc_sli4_cfg_shdr *shdr;
14779 uint16_t dmult;
49198b37
JS
14780 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14781
2e90f4b5
JS
14782 /* sanity check on queue memory */
14783 if (!eq)
14784 return -ENODEV;
49198b37
JS
14785 if (!phba->sli4_hba.pc_sli4_params.supported)
14786 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14787
14788 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14789 if (!mbox)
14790 return -ENOMEM;
14791 length = (sizeof(struct lpfc_mbx_eq_create) -
14792 sizeof(struct lpfc_sli4_cfg_mhdr));
14793 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14794 LPFC_MBOX_OPCODE_EQ_CREATE,
14795 length, LPFC_SLI4_MBX_EMBED);
14796 eq_create = &mbox->u.mqe.un.eq_create;
7365f6fd 14797 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
4f774513
JS
14798 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14799 eq->page_count);
14800 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14801 LPFC_EQE_SIZE);
14802 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
7365f6fd
JS
14803
14804 /* Use version 2 of CREATE_EQ if eqav is set */
14805 if (phba->sli4_hba.pc_sli4_params.eqav) {
14806 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14807 LPFC_Q_CREATE_VERSION_2);
14808 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14809 phba->sli4_hba.pc_sli4_params.eqav);
14810 }
14811
2c9c5a00
JS
14812 /* don't setup delay multiplier using EQ_CREATE */
14813 dmult = 0;
4f774513
JS
14814 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14815 dmult);
14816 switch (eq->entry_count) {
14817 default:
14818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14819 "0360 Unsupported EQ count. (%d)\n",
14820 eq->entry_count);
14821 if (eq->entry_count < 256)
14822 return -EINVAL;
14823 /* otherwise default to smallest count (drop through) */
14824 case 256:
14825 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14826 LPFC_EQ_CNT_256);
14827 break;
14828 case 512:
14829 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14830 LPFC_EQ_CNT_512);
14831 break;
14832 case 1024:
14833 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14834 LPFC_EQ_CNT_1024);
14835 break;
14836 case 2048:
14837 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14838 LPFC_EQ_CNT_2048);
14839 break;
14840 case 4096:
14841 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14842 LPFC_EQ_CNT_4096);
14843 break;
14844 }
14845 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14846 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14847 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14848 putPaddrLow(dmabuf->phys);
14849 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14850 putPaddrHigh(dmabuf->phys);
14851 }
14852 mbox->vport = phba->pport;
14853 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3e1f0718
JS
14854 mbox->ctx_buf = NULL;
14855 mbox->ctx_ndlp = NULL;
4f774513 14856 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4f774513
JS
14857 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14858 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14859 if (shdr_status || shdr_add_status || rc) {
14860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14861 "2500 EQ_CREATE mailbox failed with "
14862 "status x%x add_status x%x, mbx status x%x\n",
14863 shdr_status, shdr_add_status, rc);
14864 status = -ENXIO;
14865 }
14866 eq->type = LPFC_EQ;
14867 eq->subtype = LPFC_NONE;
14868 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14869 if (eq->queue_id == 0xFFFF)
14870 status = -ENXIO;
14871 eq->host_index = 0;
14872 eq->hba_index = 0;
64eb4dcb 14873 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14874
8fa38513 14875 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14876 return status;
14877}
14878
14879/**
14880 * lpfc_cq_create - Create a Completion Queue on the HBA
14881 * @phba: HBA structure that indicates port to create a queue on.
14882 * @cq: The queue structure to use to create the completion queue.
14883 * @eq: The event queue to bind this completion queue to.
14884 *
14885 * This function creates a completion queue, as detailed in @wq, on a port,
14886 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14887 *
14888 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14889 * is used to get the entry count and entry size that are necessary to
14890 * determine the number of pages to allocate and use for this queue. The @eq
14891 * is used to indicate which event queue to bind this completion queue to. This
14892 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14893 * completion queue. This function is asynchronous and will wait for the mailbox
14894 * command to finish before continuing.
14895 *
14896 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14897 * memory this function will return -ENOMEM. If the queue create mailbox command
14898 * fails this function will return -ENXIO.
4f774513 14899 **/
a2fc4aef 14900int
4f774513
JS
14901lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14902 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14903{
14904 struct lpfc_mbx_cq_create *cq_create;
14905 struct lpfc_dmabuf *dmabuf;
14906 LPFC_MBOXQ_t *mbox;
14907 int rc, length, status = 0;
14908 uint32_t shdr_status, shdr_add_status;
14909 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14910
2e90f4b5
JS
14911 /* sanity check on queue memory */
14912 if (!cq || !eq)
14913 return -ENODEV;
49198b37 14914
4f774513
JS
14915 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14916 if (!mbox)
14917 return -ENOMEM;
14918 length = (sizeof(struct lpfc_mbx_cq_create) -
14919 sizeof(struct lpfc_sli4_cfg_mhdr));
14920 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14921 LPFC_MBOX_OPCODE_CQ_CREATE,
14922 length, LPFC_SLI4_MBX_EMBED);
14923 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14924 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14925 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14926 cq->page_count);
14927 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14928 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14929 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14930 phba->sli4_hba.pc_sli4_params.cqv);
14931 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14932 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14933 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14934 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14935 eq->queue_id);
7365f6fd
JS
14936 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14937 phba->sli4_hba.pc_sli4_params.cqav);
5a6f133e
JS
14938 } else {
14939 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14940 eq->queue_id);
14941 }
4f774513 14942 switch (cq->entry_count) {
81b96eda
JS
14943 case 2048:
14944 case 4096:
14945 if (phba->sli4_hba.pc_sli4_params.cqv ==
14946 LPFC_Q_CREATE_VERSION_2) {
14947 cq_create->u.request.context.lpfc_cq_context_count =
14948 cq->entry_count;
14949 bf_set(lpfc_cq_context_count,
14950 &cq_create->u.request.context,
14951 LPFC_CQ_CNT_WORD7);
14952 break;
14953 }
14954 /* Fall Thru */
4f774513
JS
14955 default:
14956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14957 "0361 Unsupported CQ count: "
64eb4dcb 14958 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14959 cq->entry_count, cq->entry_size,
64eb4dcb 14960 cq->page_count);
4f4c1863
JS
14961 if (cq->entry_count < 256) {
14962 status = -EINVAL;
14963 goto out;
14964 }
4f774513
JS
14965 /* otherwise default to smallest count (drop through) */
14966 case 256:
14967 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14968 LPFC_CQ_CNT_256);
14969 break;
14970 case 512:
14971 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14972 LPFC_CQ_CNT_512);
14973 break;
14974 case 1024:
14975 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14976 LPFC_CQ_CNT_1024);
14977 break;
14978 }
14979 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14980 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14981 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14982 putPaddrLow(dmabuf->phys);
14983 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14984 putPaddrHigh(dmabuf->phys);
14985 }
14986 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14987
14988 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14989 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14990 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14991 if (shdr_status || shdr_add_status || rc) {
14992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14993 "2501 CQ_CREATE mailbox failed with "
14994 "status x%x add_status x%x, mbx status x%x\n",
14995 shdr_status, shdr_add_status, rc);
14996 status = -ENXIO;
14997 goto out;
14998 }
14999 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15000 if (cq->queue_id == 0xFFFF) {
15001 status = -ENXIO;
15002 goto out;
15003 }
15004 /* link the cq onto the parent eq child list */
15005 list_add_tail(&cq->list, &eq->child_list);
15006 /* Set up completion queue's type and subtype */
15007 cq->type = type;
15008 cq->subtype = subtype;
15009 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 15010 cq->assoc_qid = eq->queue_id;
4f774513
JS
15011 cq->host_index = 0;
15012 cq->hba_index = 0;
64eb4dcb 15013 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 15014
8fa38513
JS
15015out:
15016 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15017 return status;
15018}
15019
2d7dbc4c
JS
15020/**
15021 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15022 * @phba: HBA structure that indicates port to create a queue on.
15023 * @cqp: The queue structure array to use to create the completion queues.
15024 * @eqp: The event queue array to bind these completion queues to.
15025 *
15026 * This function creates a set of completion queue, s to support MRQ
15027 * as detailed in @cqp, on a port,
15028 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15029 *
15030 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15031 * is used to get the entry count and entry size that are necessary to
15032 * determine the number of pages to allocate and use for this queue. The @eq
15033 * is used to indicate which event queue to bind this completion queue to. This
15034 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15035 * completion queue. This function is asynchronous and will wait for the mailbox
15036 * command to finish before continuing.
15037 *
15038 * On success this function will return a zero. If unable to allocate enough
15039 * memory this function will return -ENOMEM. If the queue create mailbox command
15040 * fails this function will return -ENXIO.
15041 **/
15042int
15043lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
15044 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
15045{
15046 struct lpfc_queue *cq;
15047 struct lpfc_queue *eq;
15048 struct lpfc_mbx_cq_create_set *cq_set;
15049 struct lpfc_dmabuf *dmabuf;
15050 LPFC_MBOXQ_t *mbox;
15051 int rc, length, alloclen, status = 0;
15052 int cnt, idx, numcq, page_idx = 0;
15053 uint32_t shdr_status, shdr_add_status;
15054 union lpfc_sli4_cfg_shdr *shdr;
15055 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15056
15057 /* sanity check on queue memory */
15058 numcq = phba->cfg_nvmet_mrq;
15059 if (!cqp || !eqp || !numcq)
15060 return -ENODEV;
2d7dbc4c
JS
15061
15062 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15063 if (!mbox)
15064 return -ENOMEM;
15065
15066 length = sizeof(struct lpfc_mbx_cq_create_set);
15067 length += ((numcq * cqp[0]->page_count) *
15068 sizeof(struct dma_address));
15069 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15070 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15071 LPFC_SLI4_MBX_NEMBED);
15072 if (alloclen < length) {
15073 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15074 "3098 Allocated DMA memory size (%d) is "
15075 "less than the requested DMA memory size "
15076 "(%d)\n", alloclen, length);
15077 status = -ENOMEM;
15078 goto out;
15079 }
15080 cq_set = mbox->sge_array->addr[0];
15081 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15082 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15083
15084 for (idx = 0; idx < numcq; idx++) {
15085 cq = cqp[idx];
15086 eq = eqp[idx];
15087 if (!cq || !eq) {
15088 status = -ENOMEM;
15089 goto out;
15090 }
81b96eda
JS
15091 if (!phba->sli4_hba.pc_sli4_params.supported)
15092 hw_page_size = cq->page_size;
2d7dbc4c
JS
15093
15094 switch (idx) {
15095 case 0:
15096 bf_set(lpfc_mbx_cq_create_set_page_size,
15097 &cq_set->u.request,
15098 (hw_page_size / SLI4_PAGE_SIZE));
15099 bf_set(lpfc_mbx_cq_create_set_num_pages,
15100 &cq_set->u.request, cq->page_count);
15101 bf_set(lpfc_mbx_cq_create_set_evt,
15102 &cq_set->u.request, 1);
15103 bf_set(lpfc_mbx_cq_create_set_valid,
15104 &cq_set->u.request, 1);
15105 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15106 &cq_set->u.request, 0);
15107 bf_set(lpfc_mbx_cq_create_set_num_cq,
15108 &cq_set->u.request, numcq);
7365f6fd
JS
15109 bf_set(lpfc_mbx_cq_create_set_autovalid,
15110 &cq_set->u.request,
15111 phba->sli4_hba.pc_sli4_params.cqav);
2d7dbc4c 15112 switch (cq->entry_count) {
81b96eda
JS
15113 case 2048:
15114 case 4096:
15115 if (phba->sli4_hba.pc_sli4_params.cqv ==
15116 LPFC_Q_CREATE_VERSION_2) {
15117 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15118 &cq_set->u.request,
15119 cq->entry_count);
15120 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15121 &cq_set->u.request,
15122 LPFC_CQ_CNT_WORD7);
15123 break;
15124 }
15125 /* Fall Thru */
2d7dbc4c
JS
15126 default:
15127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15128 "3118 Bad CQ count. (%d)\n",
15129 cq->entry_count);
15130 if (cq->entry_count < 256) {
15131 status = -EINVAL;
15132 goto out;
15133 }
15134 /* otherwise default to smallest (drop thru) */
15135 case 256:
15136 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15137 &cq_set->u.request, LPFC_CQ_CNT_256);
15138 break;
15139 case 512:
15140 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15141 &cq_set->u.request, LPFC_CQ_CNT_512);
15142 break;
15143 case 1024:
15144 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15145 &cq_set->u.request, LPFC_CQ_CNT_1024);
15146 break;
15147 }
15148 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15149 &cq_set->u.request, eq->queue_id);
15150 break;
15151 case 1:
15152 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15153 &cq_set->u.request, eq->queue_id);
15154 break;
15155 case 2:
15156 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15157 &cq_set->u.request, eq->queue_id);
15158 break;
15159 case 3:
15160 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15161 &cq_set->u.request, eq->queue_id);
15162 break;
15163 case 4:
15164 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15165 &cq_set->u.request, eq->queue_id);
15166 break;
15167 case 5:
15168 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15169 &cq_set->u.request, eq->queue_id);
15170 break;
15171 case 6:
15172 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15173 &cq_set->u.request, eq->queue_id);
15174 break;
15175 case 7:
15176 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15177 &cq_set->u.request, eq->queue_id);
15178 break;
15179 case 8:
15180 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15181 &cq_set->u.request, eq->queue_id);
15182 break;
15183 case 9:
15184 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15185 &cq_set->u.request, eq->queue_id);
15186 break;
15187 case 10:
15188 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15189 &cq_set->u.request, eq->queue_id);
15190 break;
15191 case 11:
15192 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15193 &cq_set->u.request, eq->queue_id);
15194 break;
15195 case 12:
15196 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15197 &cq_set->u.request, eq->queue_id);
15198 break;
15199 case 13:
15200 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15201 &cq_set->u.request, eq->queue_id);
15202 break;
15203 case 14:
15204 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15205 &cq_set->u.request, eq->queue_id);
15206 break;
15207 case 15:
15208 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15209 &cq_set->u.request, eq->queue_id);
15210 break;
15211 }
15212
15213 /* link the cq onto the parent eq child list */
15214 list_add_tail(&cq->list, &eq->child_list);
15215 /* Set up completion queue's type and subtype */
15216 cq->type = type;
15217 cq->subtype = subtype;
15218 cq->assoc_qid = eq->queue_id;
15219 cq->host_index = 0;
15220 cq->hba_index = 0;
64eb4dcb 15221 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 15222 cq->chann = idx;
2d7dbc4c
JS
15223
15224 rc = 0;
15225 list_for_each_entry(dmabuf, &cq->page_list, list) {
15226 memset(dmabuf->virt, 0, hw_page_size);
15227 cnt = page_idx + dmabuf->buffer_tag;
15228 cq_set->u.request.page[cnt].addr_lo =
15229 putPaddrLow(dmabuf->phys);
15230 cq_set->u.request.page[cnt].addr_hi =
15231 putPaddrHigh(dmabuf->phys);
15232 rc++;
15233 }
15234 page_idx += rc;
15235 }
15236
15237 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15238
15239 /* The IOCTL status is embedded in the mailbox subheader. */
15240 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15241 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15242 if (shdr_status || shdr_add_status || rc) {
15243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15244 "3119 CQ_CREATE_SET mailbox failed with "
15245 "status x%x add_status x%x, mbx status x%x\n",
15246 shdr_status, shdr_add_status, rc);
15247 status = -ENXIO;
15248 goto out;
15249 }
15250 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15251 if (rc == 0xFFFF) {
15252 status = -ENXIO;
15253 goto out;
15254 }
15255
15256 for (idx = 0; idx < numcq; idx++) {
15257 cq = cqp[idx];
15258 cq->queue_id = rc + idx;
15259 }
15260
15261out:
15262 lpfc_sli4_mbox_cmd_free(phba, mbox);
15263 return status;
15264}
15265
b19a061a
JS
15266/**
15267 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
15268 * @phba: HBA structure that indicates port to create a queue on.
15269 * @mq: The queue structure to use to create the mailbox queue.
15270 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15271 * @cq: The completion queue to associate with this cq.
15272 *
15273 * This function provides failback (fb) functionality when the
15274 * mq_create_ext fails on older FW generations. It's purpose is identical
15275 * to mq_create_ext otherwise.
15276 *
15277 * This routine cannot fail as all attributes were previously accessed and
15278 * initialized in mq_create_ext.
15279 **/
15280static void
15281lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15282 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
15283{
15284 struct lpfc_mbx_mq_create *mq_create;
15285 struct lpfc_dmabuf *dmabuf;
15286 int length;
15287
15288 length = (sizeof(struct lpfc_mbx_mq_create) -
15289 sizeof(struct lpfc_sli4_cfg_mhdr));
15290 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15291 LPFC_MBOX_OPCODE_MQ_CREATE,
15292 length, LPFC_SLI4_MBX_EMBED);
15293 mq_create = &mbox->u.mqe.un.mq_create;
15294 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
15295 mq->page_count);
15296 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
15297 cq->queue_id);
15298 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15299 switch (mq->entry_count) {
15300 case 16:
5a6f133e
JS
15301 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15302 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
15303 break;
15304 case 32:
5a6f133e
JS
15305 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15306 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
15307 break;
15308 case 64:
5a6f133e
JS
15309 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15310 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
15311 break;
15312 case 128:
5a6f133e
JS
15313 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15314 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
15315 break;
15316 }
15317 list_for_each_entry(dmabuf, &mq->page_list, list) {
15318 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15319 putPaddrLow(dmabuf->phys);
15320 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15321 putPaddrHigh(dmabuf->phys);
15322 }
15323}
15324
04c68496
JS
15325/**
15326 * lpfc_mq_create - Create a mailbox Queue on the HBA
15327 * @phba: HBA structure that indicates port to create a queue on.
15328 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
15329 * @cq: The completion queue to associate with this cq.
15330 * @subtype: The queue's subtype.
04c68496
JS
15331 *
15332 * This function creates a mailbox queue, as detailed in @mq, on a port,
15333 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15334 *
15335 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15336 * is used to get the entry count and entry size that are necessary to
15337 * determine the number of pages to allocate and use for this queue. This
15338 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15339 * mailbox queue. This function is asynchronous and will wait for the mailbox
15340 * command to finish before continuing.
15341 *
15342 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15343 * memory this function will return -ENOMEM. If the queue create mailbox command
15344 * fails this function will return -ENXIO.
04c68496 15345 **/
b19a061a 15346int32_t
04c68496
JS
15347lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15348 struct lpfc_queue *cq, uint32_t subtype)
15349{
15350 struct lpfc_mbx_mq_create *mq_create;
b19a061a 15351 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
15352 struct lpfc_dmabuf *dmabuf;
15353 LPFC_MBOXQ_t *mbox;
15354 int rc, length, status = 0;
15355 uint32_t shdr_status, shdr_add_status;
15356 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15357 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 15358
2e90f4b5
JS
15359 /* sanity check on queue memory */
15360 if (!mq || !cq)
15361 return -ENODEV;
49198b37
JS
15362 if (!phba->sli4_hba.pc_sli4_params.supported)
15363 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 15364
04c68496
JS
15365 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15366 if (!mbox)
15367 return -ENOMEM;
b19a061a 15368 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
15369 sizeof(struct lpfc_sli4_cfg_mhdr));
15370 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 15371 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 15372 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
15373
15374 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 15375 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
15376 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15377 &mq_create_ext->u.request, mq->page_count);
15378 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15379 &mq_create_ext->u.request, 1);
15380 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
15381 &mq_create_ext->u.request, 1);
15382 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15383 &mq_create_ext->u.request, 1);
70f3c073
JS
15384 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15385 &mq_create_ext->u.request, 1);
15386 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15387 &mq_create_ext->u.request, 1);
b19a061a 15388 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
15389 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15390 phba->sli4_hba.pc_sli4_params.mqv);
15391 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15392 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15393 cq->queue_id);
15394 else
15395 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15396 cq->queue_id);
04c68496
JS
15397 switch (mq->entry_count) {
15398 default:
15399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15400 "0362 Unsupported MQ count. (%d)\n",
15401 mq->entry_count);
4f4c1863
JS
15402 if (mq->entry_count < 16) {
15403 status = -EINVAL;
15404 goto out;
15405 }
04c68496
JS
15406 /* otherwise default to smallest count (drop through) */
15407 case 16:
5a6f133e
JS
15408 bf_set(lpfc_mq_context_ring_size,
15409 &mq_create_ext->u.request.context,
15410 LPFC_MQ_RING_SIZE_16);
04c68496
JS
15411 break;
15412 case 32:
5a6f133e
JS
15413 bf_set(lpfc_mq_context_ring_size,
15414 &mq_create_ext->u.request.context,
15415 LPFC_MQ_RING_SIZE_32);
04c68496
JS
15416 break;
15417 case 64:
5a6f133e
JS
15418 bf_set(lpfc_mq_context_ring_size,
15419 &mq_create_ext->u.request.context,
15420 LPFC_MQ_RING_SIZE_64);
04c68496
JS
15421 break;
15422 case 128:
5a6f133e
JS
15423 bf_set(lpfc_mq_context_ring_size,
15424 &mq_create_ext->u.request.context,
15425 LPFC_MQ_RING_SIZE_128);
04c68496
JS
15426 break;
15427 }
15428 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 15429 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 15430 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 15431 putPaddrLow(dmabuf->phys);
b19a061a 15432 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
15433 putPaddrHigh(dmabuf->phys);
15434 }
15435 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
15436 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15437 &mq_create_ext->u.response);
15438 if (rc != MBX_SUCCESS) {
15439 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15440 "2795 MQ_CREATE_EXT failed with "
15441 "status x%x. Failback to MQ_CREATE.\n",
15442 rc);
15443 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15444 mq_create = &mbox->u.mqe.un.mq_create;
15445 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15446 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15447 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15448 &mq_create->u.response);
15449 }
15450
04c68496 15451 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
15452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15454 if (shdr_status || shdr_add_status || rc) {
15455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15456 "2502 MQ_CREATE mailbox failed with "
15457 "status x%x add_status x%x, mbx status x%x\n",
15458 shdr_status, shdr_add_status, rc);
15459 status = -ENXIO;
15460 goto out;
15461 }
04c68496
JS
15462 if (mq->queue_id == 0xFFFF) {
15463 status = -ENXIO;
15464 goto out;
15465 }
15466 mq->type = LPFC_MQ;
2a622bfb 15467 mq->assoc_qid = cq->queue_id;
04c68496
JS
15468 mq->subtype = subtype;
15469 mq->host_index = 0;
15470 mq->hba_index = 0;
64eb4dcb 15471 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
15472
15473 /* link the mq onto the parent cq child list */
15474 list_add_tail(&mq->list, &cq->child_list);
15475out:
8fa38513 15476 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
15477 return status;
15478}
15479
4f774513
JS
15480/**
15481 * lpfc_wq_create - Create a Work Queue on the HBA
15482 * @phba: HBA structure that indicates port to create a queue on.
15483 * @wq: The queue structure to use to create the work queue.
15484 * @cq: The completion queue to bind this work queue to.
15485 * @subtype: The subtype of the work queue indicating its functionality.
15486 *
15487 * This function creates a work queue, as detailed in @wq, on a port, described
15488 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15489 *
15490 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15491 * is used to get the entry count and entry size that are necessary to
15492 * determine the number of pages to allocate and use for this queue. The @cq
15493 * is used to indicate which completion queue to bind this work queue to. This
15494 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15495 * work queue. This function is asynchronous and will wait for the mailbox
15496 * command to finish before continuing.
15497 *
15498 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15499 * memory this function will return -ENOMEM. If the queue create mailbox command
15500 * fails this function will return -ENXIO.
4f774513 15501 **/
a2fc4aef 15502int
4f774513
JS
15503lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15504 struct lpfc_queue *cq, uint32_t subtype)
15505{
15506 struct lpfc_mbx_wq_create *wq_create;
15507 struct lpfc_dmabuf *dmabuf;
15508 LPFC_MBOXQ_t *mbox;
15509 int rc, length, status = 0;
15510 uint32_t shdr_status, shdr_add_status;
15511 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15512 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 15513 struct dma_address *page;
962bc51b
JS
15514 void __iomem *bar_memmap_p;
15515 uint32_t db_offset;
15516 uint16_t pci_barset;
1351e69f
JS
15517 uint8_t dpp_barset;
15518 uint32_t dpp_offset;
15519 unsigned long pg_addr;
81b96eda 15520 uint8_t wq_create_version;
49198b37 15521
2e90f4b5
JS
15522 /* sanity check on queue memory */
15523 if (!wq || !cq)
15524 return -ENODEV;
49198b37 15525 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 15526 hw_page_size = wq->page_size;
4f774513
JS
15527
15528 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15529 if (!mbox)
15530 return -ENOMEM;
15531 length = (sizeof(struct lpfc_mbx_wq_create) -
15532 sizeof(struct lpfc_sli4_cfg_mhdr));
15533 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15534 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15535 length, LPFC_SLI4_MBX_EMBED);
15536 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 15537 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
15538 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15539 wq->page_count);
15540 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15541 cq->queue_id);
0c651878
JS
15542
15543 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
15544 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15545 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 15546
c176ffa0
JS
15547 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15548 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
15549 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15550 else
15551 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15552
0c651878 15553
1351e69f
JS
15554 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15555 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15556 else
15557 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15558
15559 switch (wq_create_version) {
0c651878 15560 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
15561 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15562 wq->entry_count);
3f247de7
JS
15563 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15564 LPFC_Q_CREATE_VERSION_1);
15565
5a6f133e
JS
15566 switch (wq->entry_size) {
15567 default:
15568 case 64:
15569 bf_set(lpfc_mbx_wq_create_wqe_size,
15570 &wq_create->u.request_1,
15571 LPFC_WQ_WQE_SIZE_64);
15572 break;
15573 case 128:
15574 bf_set(lpfc_mbx_wq_create_wqe_size,
15575 &wq_create->u.request_1,
15576 LPFC_WQ_WQE_SIZE_128);
15577 break;
15578 }
1351e69f
JS
15579 /* Request DPP by default */
15580 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
8ea73db4
JS
15581 bf_set(lpfc_mbx_wq_create_page_size,
15582 &wq_create->u.request_1,
81b96eda 15583 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 15584 page = wq_create->u.request_1.page;
0c651878
JS
15585 break;
15586 default:
1351e69f
JS
15587 page = wq_create->u.request.page;
15588 break;
5a6f133e 15589 }
0c651878 15590
4f774513 15591 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 15592 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
15593 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15594 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 15595 }
962bc51b
JS
15596
15597 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15598 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15599
4f774513
JS
15600 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15601 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15602 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15603 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15604 if (shdr_status || shdr_add_status || rc) {
15605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15606 "2503 WQ_CREATE mailbox failed with "
15607 "status x%x add_status x%x, mbx status x%x\n",
15608 shdr_status, shdr_add_status, rc);
15609 status = -ENXIO;
15610 goto out;
15611 }
1351e69f
JS
15612
15613 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15614 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15615 &wq_create->u.response);
15616 else
15617 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15618 &wq_create->u.response_1);
15619
4f774513
JS
15620 if (wq->queue_id == 0xFFFF) {
15621 status = -ENXIO;
15622 goto out;
15623 }
1351e69f
JS
15624
15625 wq->db_format = LPFC_DB_LIST_FORMAT;
15626 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15627 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15628 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15629 &wq_create->u.response);
15630 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15631 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15633 "3265 WQ[%d] doorbell format "
15634 "not supported: x%x\n",
15635 wq->queue_id, wq->db_format);
15636 status = -EINVAL;
15637 goto out;
15638 }
15639 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15640 &wq_create->u.response);
15641 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15642 pci_barset);
15643 if (!bar_memmap_p) {
15644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15645 "3263 WQ[%d] failed to memmap "
15646 "pci barset:x%x\n",
15647 wq->queue_id, pci_barset);
15648 status = -ENOMEM;
15649 goto out;
15650 }
15651 db_offset = wq_create->u.response.doorbell_offset;
15652 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15653 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15655 "3252 WQ[%d] doorbell offset "
15656 "not supported: x%x\n",
15657 wq->queue_id, db_offset);
15658 status = -EINVAL;
15659 goto out;
15660 }
15661 wq->db_regaddr = bar_memmap_p + db_offset;
15662 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15663 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15664 "format:x%x\n", wq->queue_id,
15665 pci_barset, db_offset, wq->db_format);
15666 } else
15667 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15668 } else {
1351e69f
JS
15669 /* Check if DPP was honored by the firmware */
15670 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15671 &wq_create->u.response_1);
15672 if (wq->dpp_enable) {
15673 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15674 &wq_create->u.response_1);
15675 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15676 pci_barset);
15677 if (!bar_memmap_p) {
15678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15679 "3267 WQ[%d] failed to memmap "
15680 "pci barset:x%x\n",
15681 wq->queue_id, pci_barset);
15682 status = -ENOMEM;
15683 goto out;
15684 }
15685 db_offset = wq_create->u.response_1.doorbell_offset;
15686 wq->db_regaddr = bar_memmap_p + db_offset;
15687 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15688 &wq_create->u.response_1);
15689 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15690 &wq_create->u.response_1);
15691 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15692 dpp_barset);
15693 if (!bar_memmap_p) {
15694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15695 "3268 WQ[%d] failed to memmap "
15696 "pci barset:x%x\n",
15697 wq->queue_id, dpp_barset);
15698 status = -ENOMEM;
15699 goto out;
15700 }
15701 dpp_offset = wq_create->u.response_1.dpp_offset;
15702 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15703 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15704 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15705 "dpp_id:x%x dpp_barset:x%x "
15706 "dpp_offset:x%x\n",
15707 wq->queue_id, pci_barset, db_offset,
15708 wq->dpp_id, dpp_barset, dpp_offset);
15709
15710 /* Enable combined writes for DPP aperture */
15711 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15712#ifdef CONFIG_X86
15713 rc = set_memory_wc(pg_addr, 1);
15714 if (rc) {
15715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15716 "3272 Cannot setup Combined "
15717 "Write on WQ[%d] - disable DPP\n",
15718 wq->queue_id);
15719 phba->cfg_enable_dpp = 0;
15720 }
15721#else
15722 phba->cfg_enable_dpp = 0;
15723#endif
15724 } else
15725 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15726 }
895427bd
JS
15727 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15728 if (wq->pring == NULL) {
15729 status = -ENOMEM;
15730 goto out;
15731 }
4f774513 15732 wq->type = LPFC_WQ;
2a622bfb 15733 wq->assoc_qid = cq->queue_id;
4f774513
JS
15734 wq->subtype = subtype;
15735 wq->host_index = 0;
15736 wq->hba_index = 0;
ff78d8f9 15737 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15738
15739 /* link the wq onto the parent cq child list */
15740 list_add_tail(&wq->list, &cq->child_list);
15741out:
8fa38513 15742 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15743 return status;
15744}
15745
15746/**
15747 * lpfc_rq_create - Create a Receive Queue on the HBA
15748 * @phba: HBA structure that indicates port to create a queue on.
15749 * @hrq: The queue structure to use to create the header receive queue.
15750 * @drq: The queue structure to use to create the data receive queue.
15751 * @cq: The completion queue to bind this work queue to.
15752 *
15753 * This function creates a receive buffer queue pair , as detailed in @hrq and
15754 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15755 * to the HBA.
15756 *
15757 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15758 * struct is used to get the entry count that is necessary to determine the
15759 * number of pages to use for this queue. The @cq is used to indicate which
15760 * completion queue to bind received buffers that are posted to these queues to.
15761 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15762 * receive queue pair. This function is asynchronous and will wait for the
15763 * mailbox command to finish before continuing.
15764 *
15765 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15766 * memory this function will return -ENOMEM. If the queue create mailbox command
15767 * fails this function will return -ENXIO.
4f774513 15768 **/
a2fc4aef 15769int
4f774513
JS
15770lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15771 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15772{
15773 struct lpfc_mbx_rq_create *rq_create;
15774 struct lpfc_dmabuf *dmabuf;
15775 LPFC_MBOXQ_t *mbox;
15776 int rc, length, status = 0;
15777 uint32_t shdr_status, shdr_add_status;
15778 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15779 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15780 void __iomem *bar_memmap_p;
15781 uint32_t db_offset;
15782 uint16_t pci_barset;
49198b37 15783
2e90f4b5
JS
15784 /* sanity check on queue memory */
15785 if (!hrq || !drq || !cq)
15786 return -ENODEV;
49198b37
JS
15787 if (!phba->sli4_hba.pc_sli4_params.supported)
15788 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15789
15790 if (hrq->entry_count != drq->entry_count)
15791 return -EINVAL;
15792 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15793 if (!mbox)
15794 return -ENOMEM;
15795 length = (sizeof(struct lpfc_mbx_rq_create) -
15796 sizeof(struct lpfc_sli4_cfg_mhdr));
15797 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15798 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15799 length, LPFC_SLI4_MBX_EMBED);
15800 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15801 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15802 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15803 phba->sli4_hba.pc_sli4_params.rqv);
15804 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15805 bf_set(lpfc_rq_context_rqe_count_1,
15806 &rq_create->u.request.context,
15807 hrq->entry_count);
15808 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15809 bf_set(lpfc_rq_context_rqe_size,
15810 &rq_create->u.request.context,
15811 LPFC_RQE_SIZE_8);
15812 bf_set(lpfc_rq_context_page_size,
15813 &rq_create->u.request.context,
8ea73db4 15814 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15815 } else {
15816 switch (hrq->entry_count) {
15817 default:
15818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15819 "2535 Unsupported RQ count. (%d)\n",
15820 hrq->entry_count);
4f4c1863
JS
15821 if (hrq->entry_count < 512) {
15822 status = -EINVAL;
15823 goto out;
15824 }
5a6f133e
JS
15825 /* otherwise default to smallest count (drop through) */
15826 case 512:
15827 bf_set(lpfc_rq_context_rqe_count,
15828 &rq_create->u.request.context,
15829 LPFC_RQ_RING_SIZE_512);
15830 break;
15831 case 1024:
15832 bf_set(lpfc_rq_context_rqe_count,
15833 &rq_create->u.request.context,
15834 LPFC_RQ_RING_SIZE_1024);
15835 break;
15836 case 2048:
15837 bf_set(lpfc_rq_context_rqe_count,
15838 &rq_create->u.request.context,
15839 LPFC_RQ_RING_SIZE_2048);
15840 break;
15841 case 4096:
15842 bf_set(lpfc_rq_context_rqe_count,
15843 &rq_create->u.request.context,
15844 LPFC_RQ_RING_SIZE_4096);
15845 break;
15846 }
15847 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15848 LPFC_HDR_BUF_SIZE);
4f774513
JS
15849 }
15850 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15851 cq->queue_id);
15852 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15853 hrq->page_count);
4f774513 15854 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15855 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15856 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15857 putPaddrLow(dmabuf->phys);
15858 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15859 putPaddrHigh(dmabuf->phys);
15860 }
962bc51b
JS
15861 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15862 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15863
4f774513
JS
15864 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15865 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
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 "2504 RQ_CREATE 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 goto out;
15875 }
15876 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15877 if (hrq->queue_id == 0xFFFF) {
15878 status = -ENXIO;
15879 goto out;
15880 }
962bc51b
JS
15881
15882 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15883 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15884 &rq_create->u.response);
15885 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15886 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15888 "3262 RQ [%d] doorbell format not "
15889 "supported: x%x\n", hrq->queue_id,
15890 hrq->db_format);
15891 status = -EINVAL;
15892 goto out;
15893 }
15894
15895 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15896 &rq_create->u.response);
15897 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15898 if (!bar_memmap_p) {
15899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15900 "3269 RQ[%d] failed to memmap pci "
15901 "barset:x%x\n", hrq->queue_id,
15902 pci_barset);
15903 status = -ENOMEM;
15904 goto out;
15905 }
15906
15907 db_offset = rq_create->u.response.doorbell_offset;
15908 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15909 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15911 "3270 RQ[%d] doorbell offset not "
15912 "supported: x%x\n", hrq->queue_id,
15913 db_offset);
15914 status = -EINVAL;
15915 goto out;
15916 }
15917 hrq->db_regaddr = bar_memmap_p + db_offset;
15918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15919 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15920 "format:x%x\n", hrq->queue_id, pci_barset,
15921 db_offset, hrq->db_format);
962bc51b
JS
15922 } else {
15923 hrq->db_format = LPFC_DB_RING_FORMAT;
15924 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15925 }
4f774513 15926 hrq->type = LPFC_HRQ;
2a622bfb 15927 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15928 hrq->subtype = subtype;
15929 hrq->host_index = 0;
15930 hrq->hba_index = 0;
61f3d4bf 15931 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15932
15933 /* now create the data queue */
15934 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15935 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15936 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15937 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15938 phba->sli4_hba.pc_sli4_params.rqv);
15939 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15940 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15941 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15942 if (subtype == LPFC_NVMET)
15943 rq_create->u.request.context.buffer_size =
15944 LPFC_NVMET_DATA_BUF_SIZE;
15945 else
15946 rq_create->u.request.context.buffer_size =
15947 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15948 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15949 LPFC_RQE_SIZE_8);
15950 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15951 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15952 } else {
15953 switch (drq->entry_count) {
15954 default:
15955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15956 "2536 Unsupported RQ count. (%d)\n",
15957 drq->entry_count);
4f4c1863
JS
15958 if (drq->entry_count < 512) {
15959 status = -EINVAL;
15960 goto out;
15961 }
5a6f133e
JS
15962 /* otherwise default to smallest count (drop through) */
15963 case 512:
15964 bf_set(lpfc_rq_context_rqe_count,
15965 &rq_create->u.request.context,
15966 LPFC_RQ_RING_SIZE_512);
15967 break;
15968 case 1024:
15969 bf_set(lpfc_rq_context_rqe_count,
15970 &rq_create->u.request.context,
15971 LPFC_RQ_RING_SIZE_1024);
15972 break;
15973 case 2048:
15974 bf_set(lpfc_rq_context_rqe_count,
15975 &rq_create->u.request.context,
15976 LPFC_RQ_RING_SIZE_2048);
15977 break;
15978 case 4096:
15979 bf_set(lpfc_rq_context_rqe_count,
15980 &rq_create->u.request.context,
15981 LPFC_RQ_RING_SIZE_4096);
15982 break;
15983 }
3c603be9
JS
15984 if (subtype == LPFC_NVMET)
15985 bf_set(lpfc_rq_context_buf_size,
15986 &rq_create->u.request.context,
15987 LPFC_NVMET_DATA_BUF_SIZE);
15988 else
15989 bf_set(lpfc_rq_context_buf_size,
15990 &rq_create->u.request.context,
15991 LPFC_DATA_BUF_SIZE);
4f774513
JS
15992 }
15993 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15994 cq->queue_id);
15995 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15996 drq->page_count);
4f774513
JS
15997 list_for_each_entry(dmabuf, &drq->page_list, list) {
15998 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15999 putPaddrLow(dmabuf->phys);
16000 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16001 putPaddrHigh(dmabuf->phys);
16002 }
962bc51b
JS
16003 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16004 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
16005 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16006 /* The IOCTL status is embedded in the mailbox subheader. */
16007 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16008 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16009 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16010 if (shdr_status || shdr_add_status || rc) {
16011 status = -ENXIO;
16012 goto out;
16013 }
16014 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16015 if (drq->queue_id == 0xFFFF) {
16016 status = -ENXIO;
16017 goto out;
16018 }
16019 drq->type = LPFC_DRQ;
2a622bfb 16020 drq->assoc_qid = cq->queue_id;
4f774513
JS
16021 drq->subtype = subtype;
16022 drq->host_index = 0;
16023 drq->hba_index = 0;
61f3d4bf 16024 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
16025
16026 /* link the header and data RQs onto the parent cq child list */
16027 list_add_tail(&hrq->list, &cq->child_list);
16028 list_add_tail(&drq->list, &cq->child_list);
16029
16030out:
8fa38513 16031 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
16032 return status;
16033}
16034
2d7dbc4c
JS
16035/**
16036 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16037 * @phba: HBA structure that indicates port to create a queue on.
16038 * @hrqp: The queue structure array to use to create the header receive queues.
16039 * @drqp: The queue structure array to use to create the data receive queues.
16040 * @cqp: The completion queue array to bind these receive queues to.
16041 *
16042 * This function creates a receive buffer queue pair , as detailed in @hrq and
16043 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16044 * to the HBA.
16045 *
16046 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16047 * struct is used to get the entry count that is necessary to determine the
16048 * number of pages to use for this queue. The @cq is used to indicate which
16049 * completion queue to bind received buffers that are posted to these queues to.
16050 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16051 * receive queue pair. This function is asynchronous and will wait for the
16052 * mailbox command to finish before continuing.
16053 *
16054 * On success this function will return a zero. If unable to allocate enough
16055 * memory this function will return -ENOMEM. If the queue create mailbox command
16056 * fails this function will return -ENXIO.
16057 **/
16058int
16059lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16060 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16061 uint32_t subtype)
16062{
16063 struct lpfc_queue *hrq, *drq, *cq;
16064 struct lpfc_mbx_rq_create_v2 *rq_create;
16065 struct lpfc_dmabuf *dmabuf;
16066 LPFC_MBOXQ_t *mbox;
16067 int rc, length, alloclen, status = 0;
16068 int cnt, idx, numrq, page_idx = 0;
16069 uint32_t shdr_status, shdr_add_status;
16070 union lpfc_sli4_cfg_shdr *shdr;
16071 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16072
16073 numrq = phba->cfg_nvmet_mrq;
16074 /* sanity check on array memory */
16075 if (!hrqp || !drqp || !cqp || !numrq)
16076 return -ENODEV;
16077 if (!phba->sli4_hba.pc_sli4_params.supported)
16078 hw_page_size = SLI4_PAGE_SIZE;
16079
16080 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16081 if (!mbox)
16082 return -ENOMEM;
16083
16084 length = sizeof(struct lpfc_mbx_rq_create_v2);
16085 length += ((2 * numrq * hrqp[0]->page_count) *
16086 sizeof(struct dma_address));
16087
16088 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16089 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16090 LPFC_SLI4_MBX_NEMBED);
16091 if (alloclen < length) {
16092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16093 "3099 Allocated DMA memory size (%d) is "
16094 "less than the requested DMA memory size "
16095 "(%d)\n", alloclen, length);
16096 status = -ENOMEM;
16097 goto out;
16098 }
16099
16100
16101
16102 rq_create = mbox->sge_array->addr[0];
16103 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16104
16105 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16106 cnt = 0;
16107
16108 for (idx = 0; idx < numrq; idx++) {
16109 hrq = hrqp[idx];
16110 drq = drqp[idx];
16111 cq = cqp[idx];
16112
2d7dbc4c
JS
16113 /* sanity check on queue memory */
16114 if (!hrq || !drq || !cq) {
16115 status = -ENODEV;
16116 goto out;
16117 }
16118
7aabe84b
JS
16119 if (hrq->entry_count != drq->entry_count) {
16120 status = -EINVAL;
16121 goto out;
16122 }
16123
2d7dbc4c
JS
16124 if (idx == 0) {
16125 bf_set(lpfc_mbx_rq_create_num_pages,
16126 &rq_create->u.request,
16127 hrq->page_count);
16128 bf_set(lpfc_mbx_rq_create_rq_cnt,
16129 &rq_create->u.request, (numrq * 2));
16130 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16131 1);
16132 bf_set(lpfc_rq_context_base_cq,
16133 &rq_create->u.request.context,
16134 cq->queue_id);
16135 bf_set(lpfc_rq_context_data_size,
16136 &rq_create->u.request.context,
3c603be9 16137 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
16138 bf_set(lpfc_rq_context_hdr_size,
16139 &rq_create->u.request.context,
16140 LPFC_HDR_BUF_SIZE);
16141 bf_set(lpfc_rq_context_rqe_count_1,
16142 &rq_create->u.request.context,
16143 hrq->entry_count);
16144 bf_set(lpfc_rq_context_rqe_size,
16145 &rq_create->u.request.context,
16146 LPFC_RQE_SIZE_8);
16147 bf_set(lpfc_rq_context_page_size,
16148 &rq_create->u.request.context,
16149 (PAGE_SIZE/SLI4_PAGE_SIZE));
16150 }
16151 rc = 0;
16152 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16153 memset(dmabuf->virt, 0, hw_page_size);
16154 cnt = page_idx + dmabuf->buffer_tag;
16155 rq_create->u.request.page[cnt].addr_lo =
16156 putPaddrLow(dmabuf->phys);
16157 rq_create->u.request.page[cnt].addr_hi =
16158 putPaddrHigh(dmabuf->phys);
16159 rc++;
16160 }
16161 page_idx += rc;
16162
16163 rc = 0;
16164 list_for_each_entry(dmabuf, &drq->page_list, list) {
16165 memset(dmabuf->virt, 0, hw_page_size);
16166 cnt = page_idx + dmabuf->buffer_tag;
16167 rq_create->u.request.page[cnt].addr_lo =
16168 putPaddrLow(dmabuf->phys);
16169 rq_create->u.request.page[cnt].addr_hi =
16170 putPaddrHigh(dmabuf->phys);
16171 rc++;
16172 }
16173 page_idx += rc;
16174
16175 hrq->db_format = LPFC_DB_RING_FORMAT;
16176 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16177 hrq->type = LPFC_HRQ;
16178 hrq->assoc_qid = cq->queue_id;
16179 hrq->subtype = subtype;
16180 hrq->host_index = 0;
16181 hrq->hba_index = 0;
61f3d4bf 16182 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
16183
16184 drq->db_format = LPFC_DB_RING_FORMAT;
16185 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16186 drq->type = LPFC_DRQ;
16187 drq->assoc_qid = cq->queue_id;
16188 drq->subtype = subtype;
16189 drq->host_index = 0;
16190 drq->hba_index = 0;
61f3d4bf 16191 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
16192
16193 list_add_tail(&hrq->list, &cq->child_list);
16194 list_add_tail(&drq->list, &cq->child_list);
16195 }
16196
16197 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16198 /* The IOCTL status is embedded in the mailbox subheader. */
16199 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16200 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16201 if (shdr_status || shdr_add_status || rc) {
16202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16203 "3120 RQ_CREATE mailbox failed with "
16204 "status x%x add_status x%x, mbx status x%x\n",
16205 shdr_status, shdr_add_status, rc);
16206 status = -ENXIO;
16207 goto out;
16208 }
16209 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16210 if (rc == 0xFFFF) {
16211 status = -ENXIO;
16212 goto out;
16213 }
16214
16215 /* Initialize all RQs with associated queue id */
16216 for (idx = 0; idx < numrq; idx++) {
16217 hrq = hrqp[idx];
16218 hrq->queue_id = rc + (2 * idx);
16219 drq = drqp[idx];
16220 drq->queue_id = rc + (2 * idx) + 1;
16221 }
16222
16223out:
16224 lpfc_sli4_mbox_cmd_free(phba, mbox);
16225 return status;
16226}
16227
4f774513
JS
16228/**
16229 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16230 * @eq: The queue structure associated with the queue to destroy.
16231 *
16232 * This function destroys a queue, as detailed in @eq by sending an mailbox
16233 * command, specific to the type of queue, to the HBA.
16234 *
16235 * The @eq struct is used to get the queue ID of the queue to destroy.
16236 *
16237 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16238 * command fails this function will return -ENXIO.
4f774513 16239 **/
a2fc4aef 16240int
4f774513
JS
16241lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16242{
16243 LPFC_MBOXQ_t *mbox;
16244 int rc, length, status = 0;
16245 uint32_t shdr_status, shdr_add_status;
16246 union lpfc_sli4_cfg_shdr *shdr;
16247
2e90f4b5 16248 /* sanity check on queue memory */
4f774513
JS
16249 if (!eq)
16250 return -ENODEV;
16251 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16252 if (!mbox)
16253 return -ENOMEM;
16254 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16255 sizeof(struct lpfc_sli4_cfg_mhdr));
16256 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16257 LPFC_MBOX_OPCODE_EQ_DESTROY,
16258 length, LPFC_SLI4_MBX_EMBED);
16259 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16260 eq->queue_id);
16261 mbox->vport = eq->phba->pport;
16262 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16263
16264 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16265 /* The IOCTL status is embedded in the mailbox subheader. */
16266 shdr = (union lpfc_sli4_cfg_shdr *)
16267 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16268 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16269 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16270 if (shdr_status || shdr_add_status || rc) {
16271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16272 "2505 EQ_DESTROY mailbox failed with "
16273 "status x%x add_status x%x, mbx status x%x\n",
16274 shdr_status, shdr_add_status, rc);
16275 status = -ENXIO;
16276 }
16277
16278 /* Remove eq from any list */
16279 list_del_init(&eq->list);
8fa38513 16280 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
16281 return status;
16282}
16283
16284/**
16285 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16286 * @cq: The queue structure associated with the queue to destroy.
16287 *
16288 * This function destroys a queue, as detailed in @cq by sending an mailbox
16289 * command, specific to the type of queue, to the HBA.
16290 *
16291 * The @cq struct is used to get the queue ID of the queue to destroy.
16292 *
16293 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16294 * command fails this function will return -ENXIO.
4f774513 16295 **/
a2fc4aef 16296int
4f774513
JS
16297lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16298{
16299 LPFC_MBOXQ_t *mbox;
16300 int rc, length, status = 0;
16301 uint32_t shdr_status, shdr_add_status;
16302 union lpfc_sli4_cfg_shdr *shdr;
16303
2e90f4b5 16304 /* sanity check on queue memory */
4f774513
JS
16305 if (!cq)
16306 return -ENODEV;
16307 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16308 if (!mbox)
16309 return -ENOMEM;
16310 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16311 sizeof(struct lpfc_sli4_cfg_mhdr));
16312 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16313 LPFC_MBOX_OPCODE_CQ_DESTROY,
16314 length, LPFC_SLI4_MBX_EMBED);
16315 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16316 cq->queue_id);
16317 mbox->vport = cq->phba->pport;
16318 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16319 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16320 /* The IOCTL status is embedded in the mailbox subheader. */
16321 shdr = (union lpfc_sli4_cfg_shdr *)
16322 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16323 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16324 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16325 if (shdr_status || shdr_add_status || rc) {
16326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16327 "2506 CQ_DESTROY mailbox failed with "
16328 "status x%x add_status x%x, mbx status x%x\n",
16329 shdr_status, shdr_add_status, rc);
16330 status = -ENXIO;
16331 }
16332 /* Remove cq from any list */
16333 list_del_init(&cq->list);
8fa38513 16334 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
16335 return status;
16336}
16337
04c68496
JS
16338/**
16339 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16340 * @qm: The queue structure associated with the queue to destroy.
16341 *
16342 * This function destroys a queue, as detailed in @mq by sending an mailbox
16343 * command, specific to the type of queue, to the HBA.
16344 *
16345 * The @mq struct is used to get the queue ID of the queue to destroy.
16346 *
16347 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16348 * command fails this function will return -ENXIO.
04c68496 16349 **/
a2fc4aef 16350int
04c68496
JS
16351lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16352{
16353 LPFC_MBOXQ_t *mbox;
16354 int rc, length, status = 0;
16355 uint32_t shdr_status, shdr_add_status;
16356 union lpfc_sli4_cfg_shdr *shdr;
16357
2e90f4b5 16358 /* sanity check on queue memory */
04c68496
JS
16359 if (!mq)
16360 return -ENODEV;
16361 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16362 if (!mbox)
16363 return -ENOMEM;
16364 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16365 sizeof(struct lpfc_sli4_cfg_mhdr));
16366 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16367 LPFC_MBOX_OPCODE_MQ_DESTROY,
16368 length, LPFC_SLI4_MBX_EMBED);
16369 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16370 mq->queue_id);
16371 mbox->vport = mq->phba->pport;
16372 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16373 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16374 /* The IOCTL status is embedded in the mailbox subheader. */
16375 shdr = (union lpfc_sli4_cfg_shdr *)
16376 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16377 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16378 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16379 if (shdr_status || shdr_add_status || rc) {
16380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16381 "2507 MQ_DESTROY mailbox failed with "
16382 "status x%x add_status x%x, mbx status x%x\n",
16383 shdr_status, shdr_add_status, rc);
16384 status = -ENXIO;
16385 }
16386 /* Remove mq from any list */
16387 list_del_init(&mq->list);
8fa38513 16388 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
16389 return status;
16390}
16391
4f774513
JS
16392/**
16393 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16394 * @wq: The queue structure associated with the queue to destroy.
16395 *
16396 * This function destroys a queue, as detailed in @wq by sending an mailbox
16397 * command, specific to the type of queue, to the HBA.
16398 *
16399 * The @wq struct is used to get the queue ID of the queue to destroy.
16400 *
16401 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16402 * command fails this function will return -ENXIO.
4f774513 16403 **/
a2fc4aef 16404int
4f774513
JS
16405lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16406{
16407 LPFC_MBOXQ_t *mbox;
16408 int rc, length, status = 0;
16409 uint32_t shdr_status, shdr_add_status;
16410 union lpfc_sli4_cfg_shdr *shdr;
16411
2e90f4b5 16412 /* sanity check on queue memory */
4f774513
JS
16413 if (!wq)
16414 return -ENODEV;
16415 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16416 if (!mbox)
16417 return -ENOMEM;
16418 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16419 sizeof(struct lpfc_sli4_cfg_mhdr));
16420 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16421 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16422 length, LPFC_SLI4_MBX_EMBED);
16423 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16424 wq->queue_id);
16425 mbox->vport = wq->phba->pport;
16426 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16427 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16428 shdr = (union lpfc_sli4_cfg_shdr *)
16429 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16430 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16431 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16432 if (shdr_status || shdr_add_status || rc) {
16433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16434 "2508 WQ_DESTROY mailbox failed with "
16435 "status x%x add_status x%x, mbx status x%x\n",
16436 shdr_status, shdr_add_status, rc);
16437 status = -ENXIO;
16438 }
16439 /* Remove wq from any list */
16440 list_del_init(&wq->list);
d1f525aa
JS
16441 kfree(wq->pring);
16442 wq->pring = NULL;
8fa38513 16443 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
16444 return status;
16445}
16446
16447/**
16448 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16449 * @rq: The queue structure associated with the queue to destroy.
16450 *
16451 * This function destroys a queue, as detailed in @rq by sending an mailbox
16452 * command, specific to the type of queue, to the HBA.
16453 *
16454 * The @rq struct is used to get the queue ID of the queue to destroy.
16455 *
16456 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16457 * command fails this function will return -ENXIO.
4f774513 16458 **/
a2fc4aef 16459int
4f774513
JS
16460lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16461 struct lpfc_queue *drq)
16462{
16463 LPFC_MBOXQ_t *mbox;
16464 int rc, length, status = 0;
16465 uint32_t shdr_status, shdr_add_status;
16466 union lpfc_sli4_cfg_shdr *shdr;
16467
2e90f4b5 16468 /* sanity check on queue memory */
4f774513
JS
16469 if (!hrq || !drq)
16470 return -ENODEV;
16471 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16472 if (!mbox)
16473 return -ENOMEM;
16474 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 16475 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
16476 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16477 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16478 length, LPFC_SLI4_MBX_EMBED);
16479 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16480 hrq->queue_id);
16481 mbox->vport = hrq->phba->pport;
16482 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16483 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16484 /* The IOCTL status is embedded in the mailbox subheader. */
16485 shdr = (union lpfc_sli4_cfg_shdr *)
16486 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16487 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16488 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16489 if (shdr_status || shdr_add_status || rc) {
16490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16491 "2509 RQ_DESTROY mailbox failed with "
16492 "status x%x add_status x%x, mbx status x%x\n",
16493 shdr_status, shdr_add_status, rc);
16494 if (rc != MBX_TIMEOUT)
16495 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16496 return -ENXIO;
16497 }
16498 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16499 drq->queue_id);
16500 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16501 shdr = (union lpfc_sli4_cfg_shdr *)
16502 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16503 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16504 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16505 if (shdr_status || shdr_add_status || rc) {
16506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16507 "2510 RQ_DESTROY mailbox failed with "
16508 "status x%x add_status x%x, mbx status x%x\n",
16509 shdr_status, shdr_add_status, rc);
16510 status = -ENXIO;
16511 }
16512 list_del_init(&hrq->list);
16513 list_del_init(&drq->list);
8fa38513 16514 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
16515 return status;
16516}
16517
16518/**
16519 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16520 * @phba: The virtual port for which this call being executed.
16521 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16522 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16523 * @xritag: the xritag that ties this io to the SGL pages.
16524 *
16525 * This routine will post the sgl pages for the IO that has the xritag
16526 * that is in the iocbq structure. The xritag is assigned during iocbq
16527 * creation and persists for as long as the driver is loaded.
16528 * if the caller has fewer than 256 scatter gather segments to map then
16529 * pdma_phys_addr1 should be 0.
16530 * If the caller needs to map more than 256 scatter gather segment then
16531 * pdma_phys_addr1 should be a valid physical address.
16532 * physical address for SGLs must be 64 byte aligned.
16533 * If you are going to map 2 SGL's then the first one must have 256 entries
16534 * the second sgl can have between 1 and 256 entries.
16535 *
16536 * Return codes:
16537 * 0 - Success
16538 * -ENXIO, -ENOMEM - Failure
16539 **/
16540int
16541lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16542 dma_addr_t pdma_phys_addr0,
16543 dma_addr_t pdma_phys_addr1,
16544 uint16_t xritag)
16545{
16546 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16547 LPFC_MBOXQ_t *mbox;
16548 int rc;
16549 uint32_t shdr_status, shdr_add_status;
6d368e53 16550 uint32_t mbox_tmo;
4f774513
JS
16551 union lpfc_sli4_cfg_shdr *shdr;
16552
16553 if (xritag == NO_XRI) {
16554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16555 "0364 Invalid param:\n");
16556 return -EINVAL;
16557 }
16558
16559 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16560 if (!mbox)
16561 return -ENOMEM;
16562
16563 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16564 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16565 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 16566 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
16567
16568 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16569 &mbox->u.mqe.un.post_sgl_pages;
16570 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16571 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16572
16573 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16574 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16575 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16576 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16577
16578 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16579 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16580 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16581 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16582 if (!phba->sli4_hba.intr_enable)
16583 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 16584 else {
a183a15f 16585 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
16586 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16587 }
4f774513
JS
16588 /* The IOCTL status is embedded in the mailbox subheader. */
16589 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16590 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16591 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16592 if (rc != MBX_TIMEOUT)
16593 mempool_free(mbox, phba->mbox_mem_pool);
16594 if (shdr_status || shdr_add_status || rc) {
16595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16596 "2511 POST_SGL mailbox failed with "
16597 "status x%x add_status x%x, mbx status x%x\n",
16598 shdr_status, shdr_add_status, rc);
4f774513
JS
16599 }
16600 return 0;
16601}
4f774513 16602
6d368e53 16603/**
88a2cfbb 16604 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
16605 * @phba: pointer to lpfc hba data structure.
16606 *
16607 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
16608 * HBA consistent with the SLI-4 interface spec. This routine
16609 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16610 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 16611 *
88a2cfbb
JS
16612 * Returns
16613 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16614 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16615 **/
5d8b8167 16616static uint16_t
6d368e53
JS
16617lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16618{
16619 unsigned long xri;
16620
16621 /*
16622 * Fetch the next logical xri. Because this index is logical,
16623 * the driver starts at 0 each time.
16624 */
16625 spin_lock_irq(&phba->hbalock);
16626 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16627 phba->sli4_hba.max_cfg_param.max_xri, 0);
16628 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16629 spin_unlock_irq(&phba->hbalock);
16630 return NO_XRI;
16631 } else {
16632 set_bit(xri, phba->sli4_hba.xri_bmask);
16633 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 16634 }
6d368e53
JS
16635 spin_unlock_irq(&phba->hbalock);
16636 return xri;
16637}
16638
16639/**
16640 * lpfc_sli4_free_xri - Release an xri for reuse.
16641 * @phba: pointer to lpfc hba data structure.
16642 *
16643 * This routine is invoked to release an xri to the pool of
16644 * available rpis maintained by the driver.
16645 **/
5d8b8167 16646static void
6d368e53
JS
16647__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16648{
16649 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
16650 phba->sli4_hba.max_cfg_param.xri_used--;
16651 }
16652}
16653
16654/**
16655 * lpfc_sli4_free_xri - Release an xri for reuse.
16656 * @phba: pointer to lpfc hba data structure.
16657 *
16658 * This routine is invoked to release an xri to the pool of
16659 * available rpis maintained by the driver.
16660 **/
16661void
16662lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16663{
16664 spin_lock_irq(&phba->hbalock);
16665 __lpfc_sli4_free_xri(phba, xri);
16666 spin_unlock_irq(&phba->hbalock);
16667}
16668
4f774513
JS
16669/**
16670 * lpfc_sli4_next_xritag - Get an xritag for the io
16671 * @phba: Pointer to HBA context object.
16672 *
16673 * This function gets an xritag for the iocb. If there is no unused xritag
16674 * it will return 0xffff.
16675 * The function returns the allocated xritag if successful, else returns zero.
16676 * Zero is not a valid xritag.
16677 * The caller is not required to hold any lock.
16678 **/
16679uint16_t
16680lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16681{
6d368e53 16682 uint16_t xri_index;
4f774513 16683
6d368e53 16684 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16685 if (xri_index == NO_XRI)
16686 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16687 "2004 Failed to allocate XRI.last XRITAG is %d"
16688 " Max XRI is %d, Used XRI is %d\n",
16689 xri_index,
16690 phba->sli4_hba.max_cfg_param.max_xri,
16691 phba->sli4_hba.max_cfg_param.xri_used);
16692 return xri_index;
4f774513
JS
16693}
16694
16695/**
895427bd 16696 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16697 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16698 * @post_sgl_list: pointer to els sgl entry list.
16699 * @count: number of els sgl entries on the list.
4f774513
JS
16700 *
16701 * This routine is invoked to post a block of driver's sgl pages to the
16702 * HBA using non-embedded mailbox command. No Lock is held. This routine
16703 * is only called when the driver is loading and after all IO has been
16704 * stopped.
16705 **/
8a9d2e80 16706static int
895427bd 16707lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16708 struct list_head *post_sgl_list,
16709 int post_cnt)
4f774513 16710{
8a9d2e80 16711 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16712 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16713 struct sgl_page_pairs *sgl_pg_pairs;
16714 void *viraddr;
16715 LPFC_MBOXQ_t *mbox;
16716 uint32_t reqlen, alloclen, pg_pairs;
16717 uint32_t mbox_tmo;
8a9d2e80
JS
16718 uint16_t xritag_start = 0;
16719 int rc = 0;
4f774513
JS
16720 uint32_t shdr_status, shdr_add_status;
16721 union lpfc_sli4_cfg_shdr *shdr;
16722
895427bd 16723 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16724 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16725 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16727 "2559 Block sgl registration required DMA "
16728 "size (%d) great than a page\n", reqlen);
16729 return -ENOMEM;
16730 }
895427bd 16731
4f774513 16732 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16733 if (!mbox)
4f774513 16734 return -ENOMEM;
4f774513
JS
16735
16736 /* Allocate DMA memory and set up the non-embedded mailbox command */
16737 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16738 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16739 LPFC_SLI4_MBX_NEMBED);
16740
16741 if (alloclen < reqlen) {
16742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16743 "0285 Allocated DMA memory size (%d) is "
16744 "less than the requested DMA memory "
16745 "size (%d)\n", alloclen, reqlen);
16746 lpfc_sli4_mbox_cmd_free(phba, mbox);
16747 return -ENOMEM;
16748 }
4f774513 16749 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16750 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16751 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16752 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16753
8a9d2e80
JS
16754 pg_pairs = 0;
16755 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16756 /* Set up the sge entry */
16757 sgl_pg_pairs->sgl_pg0_addr_lo =
16758 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16759 sgl_pg_pairs->sgl_pg0_addr_hi =
16760 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16761 sgl_pg_pairs->sgl_pg1_addr_lo =
16762 cpu_to_le32(putPaddrLow(0));
16763 sgl_pg_pairs->sgl_pg1_addr_hi =
16764 cpu_to_le32(putPaddrHigh(0));
6d368e53 16765
4f774513
JS
16766 /* Keep the first xritag on the list */
16767 if (pg_pairs == 0)
16768 xritag_start = sglq_entry->sli4_xritag;
16769 sgl_pg_pairs++;
8a9d2e80 16770 pg_pairs++;
4f774513 16771 }
6d368e53
JS
16772
16773 /* Complete initialization and perform endian conversion. */
4f774513 16774 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16775 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16776 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16777
4f774513
JS
16778 if (!phba->sli4_hba.intr_enable)
16779 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16780 else {
a183a15f 16781 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16782 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16783 }
16784 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16785 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16786 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16787 if (rc != MBX_TIMEOUT)
16788 lpfc_sli4_mbox_cmd_free(phba, mbox);
16789 if (shdr_status || shdr_add_status || rc) {
16790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16791 "2513 POST_SGL_BLOCK mailbox command failed "
16792 "status x%x add_status x%x mbx status x%x\n",
16793 shdr_status, shdr_add_status, rc);
16794 rc = -ENXIO;
16795 }
16796 return rc;
16797}
16798
16799/**
16800 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16801 * @phba: pointer to lpfc hba data structure.
16802 * @sblist: pointer to scsi buffer list.
16803 * @count: number of scsi buffers on the list.
16804 *
16805 * This routine is invoked to post a block of @count scsi sgl pages from a
16806 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16807 * No Lock is held.
16808 *
16809 **/
16810int
8a9d2e80
JS
16811lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16812 struct list_head *sblist,
16813 int count)
4f774513
JS
16814{
16815 struct lpfc_scsi_buf *psb;
16816 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16817 struct sgl_page_pairs *sgl_pg_pairs;
16818 void *viraddr;
16819 LPFC_MBOXQ_t *mbox;
16820 uint32_t reqlen, alloclen, pg_pairs;
16821 uint32_t mbox_tmo;
16822 uint16_t xritag_start = 0;
16823 int rc = 0;
16824 uint32_t shdr_status, shdr_add_status;
16825 dma_addr_t pdma_phys_bpl1;
16826 union lpfc_sli4_cfg_shdr *shdr;
16827
16828 /* Calculate the requested length of the dma memory */
8a9d2e80 16829 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16830 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16831 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16832 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16833 "0217 Block sgl registration required DMA "
16834 "size (%d) great than a page\n", reqlen);
16835 return -ENOMEM;
16836 }
16837 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16838 if (!mbox) {
16839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16840 "0283 Failed to allocate mbox cmd memory\n");
16841 return -ENOMEM;
16842 }
16843
16844 /* Allocate DMA memory and set up the non-embedded mailbox command */
16845 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16846 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16847 LPFC_SLI4_MBX_NEMBED);
16848
16849 if (alloclen < reqlen) {
16850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16851 "2561 Allocated DMA memory size (%d) is "
16852 "less than the requested DMA memory "
16853 "size (%d)\n", alloclen, reqlen);
16854 lpfc_sli4_mbox_cmd_free(phba, mbox);
16855 return -ENOMEM;
16856 }
6d368e53 16857
4f774513 16858 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16859 viraddr = mbox->sge_array->addr[0];
16860
16861 /* Set up the SGL pages in the non-embedded DMA pages */
16862 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16863 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16864
16865 pg_pairs = 0;
16866 list_for_each_entry(psb, sblist, list) {
16867 /* Set up the sge entry */
16868 sgl_pg_pairs->sgl_pg0_addr_lo =
16869 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16870 sgl_pg_pairs->sgl_pg0_addr_hi =
16871 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16872 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16873 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16874 else
16875 pdma_phys_bpl1 = 0;
16876 sgl_pg_pairs->sgl_pg1_addr_lo =
16877 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16878 sgl_pg_pairs->sgl_pg1_addr_hi =
16879 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16880 /* Keep the first xritag on the list */
16881 if (pg_pairs == 0)
16882 xritag_start = psb->cur_iocbq.sli4_xritag;
16883 sgl_pg_pairs++;
16884 pg_pairs++;
16885 }
16886 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16887 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16888 /* Perform endian conversion if necessary */
16889 sgl->word0 = cpu_to_le32(sgl->word0);
16890
16891 if (!phba->sli4_hba.intr_enable)
16892 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16893 else {
a183a15f 16894 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16895 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16896 }
16897 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16898 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16899 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16900 if (rc != MBX_TIMEOUT)
16901 lpfc_sli4_mbox_cmd_free(phba, mbox);
16902 if (shdr_status || shdr_add_status || rc) {
16903 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16904 "2564 POST_SGL_BLOCK mailbox command failed "
16905 "status x%x add_status x%x mbx status x%x\n",
16906 shdr_status, shdr_add_status, rc);
16907 rc = -ENXIO;
16908 }
16909 return rc;
16910}
16911
16912/**
16913 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16914 * @phba: pointer to lpfc_hba struct that the frame was received on
16915 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16916 *
16917 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16918 * valid type of frame that the LPFC driver will handle. This function will
16919 * return a zero if the frame is a valid frame or a non zero value when the
16920 * frame does not pass the check.
16921 **/
16922static int
16923lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16924{
474ffb74 16925 /* make rctl_names static to save stack space */
4f774513 16926 struct fc_vft_header *fc_vft_hdr;
546fc854 16927 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16928
ae9e28f3
JS
16929#define FC_RCTL_MDS_DIAGS 0xF4
16930
4f774513
JS
16931 switch (fc_hdr->fh_r_ctl) {
16932 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16933 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16934 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16935 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16936 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16937 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16938 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16939 case FC_RCTL_DD_CMD_STATUS: /* command status */
16940 case FC_RCTL_ELS_REQ: /* extended link services request */
16941 case FC_RCTL_ELS_REP: /* extended link services reply */
16942 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16943 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16944 case FC_RCTL_BA_NOP: /* basic link service NOP */
16945 case FC_RCTL_BA_ABTS: /* basic link service abort */
16946 case FC_RCTL_BA_RMC: /* remove connection */
16947 case FC_RCTL_BA_ACC: /* basic accept */
16948 case FC_RCTL_BA_RJT: /* basic reject */
16949 case FC_RCTL_BA_PRMT:
16950 case FC_RCTL_ACK_1: /* acknowledge_1 */
16951 case FC_RCTL_ACK_0: /* acknowledge_0 */
16952 case FC_RCTL_P_RJT: /* port reject */
16953 case FC_RCTL_F_RJT: /* fabric reject */
16954 case FC_RCTL_P_BSY: /* port busy */
16955 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16956 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16957 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16958 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16959 case FC_RCTL_END: /* end */
16960 break;
16961 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16962 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16963 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16964 return lpfc_fc_frame_check(phba, fc_hdr);
16965 default:
16966 goto drop;
16967 }
ae9e28f3
JS
16968
16969#define FC_TYPE_VENDOR_UNIQUE 0xFF
16970
4f774513
JS
16971 switch (fc_hdr->fh_type) {
16972 case FC_TYPE_BLS:
16973 case FC_TYPE_ELS:
16974 case FC_TYPE_FCP:
16975 case FC_TYPE_CT:
895427bd 16976 case FC_TYPE_NVME:
ae9e28f3 16977 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16978 break;
16979 case FC_TYPE_IP:
16980 case FC_TYPE_ILS:
16981 default:
16982 goto drop;
16983 }
546fc854 16984
4f774513 16985 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16986 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16987 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16988 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16989 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16990 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16991 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16992 be32_to_cpu(header[6]));
4f774513
JS
16993 return 0;
16994drop:
16995 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16996 "2539 Dropped frame rctl:x%x type:x%x\n",
16997 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16998 return 1;
16999}
17000
17001/**
17002 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17003 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17004 *
17005 * This function processes the FC header to retrieve the VFI from the VF
17006 * header, if one exists. This function will return the VFI if one exists
17007 * or 0 if no VSAN Header exists.
17008 **/
17009static uint32_t
17010lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17011{
17012 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17013
17014 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17015 return 0;
17016 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17017}
17018
17019/**
17020 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17021 * @phba: Pointer to the HBA structure to search for the vport on
17022 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17023 * @fcfi: The FC Fabric ID that the frame came from
17024 *
17025 * This function searches the @phba for a vport that matches the content of the
17026 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17027 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17028 * returns the matching vport pointer or NULL if unable to match frame to a
17029 * vport.
17030 **/
17031static struct lpfc_vport *
17032lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 17033 uint16_t fcfi, uint32_t did)
4f774513
JS
17034{
17035 struct lpfc_vport **vports;
17036 struct lpfc_vport *vport = NULL;
17037 int i;
939723a4 17038
bf08611b
JS
17039 if (did == Fabric_DID)
17040 return phba->pport;
939723a4
JS
17041 if ((phba->pport->fc_flag & FC_PT2PT) &&
17042 !(phba->link_state == LPFC_HBA_READY))
17043 return phba->pport;
17044
4f774513 17045 vports = lpfc_create_vport_work_array(phba);
895427bd 17046 if (vports != NULL) {
4f774513
JS
17047 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17048 if (phba->fcf.fcfi == fcfi &&
17049 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17050 vports[i]->fc_myDID == did) {
17051 vport = vports[i];
17052 break;
17053 }
17054 }
895427bd 17055 }
4f774513
JS
17056 lpfc_destroy_vport_work_array(phba, vports);
17057 return vport;
17058}
17059
45ed1190
JS
17060/**
17061 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17062 * @vport: The vport to work on.
17063 *
17064 * This function updates the receive sequence time stamp for this vport. The
17065 * receive sequence time stamp indicates the time that the last frame of the
17066 * the sequence that has been idle for the longest amount of time was received.
17067 * the driver uses this time stamp to indicate if any received sequences have
17068 * timed out.
17069 **/
5d8b8167 17070static void
45ed1190
JS
17071lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17072{
17073 struct lpfc_dmabuf *h_buf;
17074 struct hbq_dmabuf *dmabuf = NULL;
17075
17076 /* get the oldest sequence on the rcv list */
17077 h_buf = list_get_first(&vport->rcv_buffer_list,
17078 struct lpfc_dmabuf, list);
17079 if (!h_buf)
17080 return;
17081 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17082 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17083}
17084
17085/**
17086 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17087 * @vport: The vport that the received sequences were sent to.
17088 *
17089 * This function cleans up all outstanding received sequences. This is called
17090 * by the driver when a link event or user action invalidates all the received
17091 * sequences.
17092 **/
17093void
17094lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17095{
17096 struct lpfc_dmabuf *h_buf, *hnext;
17097 struct lpfc_dmabuf *d_buf, *dnext;
17098 struct hbq_dmabuf *dmabuf = NULL;
17099
17100 /* start with the oldest sequence on the rcv list */
17101 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17102 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17103 list_del_init(&dmabuf->hbuf.list);
17104 list_for_each_entry_safe(d_buf, dnext,
17105 &dmabuf->dbuf.list, list) {
17106 list_del_init(&d_buf->list);
17107 lpfc_in_buf_free(vport->phba, d_buf);
17108 }
17109 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17110 }
17111}
17112
17113/**
17114 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17115 * @vport: The vport that the received sequences were sent to.
17116 *
17117 * This function determines whether any received sequences have timed out by
17118 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17119 * indicates that there is at least one timed out sequence this routine will
17120 * go through the received sequences one at a time from most inactive to most
17121 * active to determine which ones need to be cleaned up. Once it has determined
17122 * that a sequence needs to be cleaned up it will simply free up the resources
17123 * without sending an abort.
17124 **/
17125void
17126lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17127{
17128 struct lpfc_dmabuf *h_buf, *hnext;
17129 struct lpfc_dmabuf *d_buf, *dnext;
17130 struct hbq_dmabuf *dmabuf = NULL;
17131 unsigned long timeout;
17132 int abort_count = 0;
17133
17134 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17135 vport->rcv_buffer_time_stamp);
17136 if (list_empty(&vport->rcv_buffer_list) ||
17137 time_before(jiffies, timeout))
17138 return;
17139 /* start with the oldest sequence on the rcv list */
17140 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17141 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17142 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17143 dmabuf->time_stamp);
17144 if (time_before(jiffies, timeout))
17145 break;
17146 abort_count++;
17147 list_del_init(&dmabuf->hbuf.list);
17148 list_for_each_entry_safe(d_buf, dnext,
17149 &dmabuf->dbuf.list, list) {
17150 list_del_init(&d_buf->list);
17151 lpfc_in_buf_free(vport->phba, d_buf);
17152 }
17153 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17154 }
17155 if (abort_count)
17156 lpfc_update_rcv_time_stamp(vport);
17157}
17158
4f774513
JS
17159/**
17160 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17161 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17162 *
17163 * This function searches through the existing incomplete sequences that have
17164 * been sent to this @vport. If the frame matches one of the incomplete
17165 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17166 * make up that sequence. If no sequence is found that matches this frame then
17167 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17168 * This function returns a pointer to the first dmabuf in the sequence list that
17169 * the frame was linked to.
17170 **/
17171static struct hbq_dmabuf *
17172lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17173{
17174 struct fc_frame_header *new_hdr;
17175 struct fc_frame_header *temp_hdr;
17176 struct lpfc_dmabuf *d_buf;
17177 struct lpfc_dmabuf *h_buf;
17178 struct hbq_dmabuf *seq_dmabuf = NULL;
17179 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 17180 uint8_t found = 0;
4f774513 17181
4d9ab994 17182 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 17183 dmabuf->time_stamp = jiffies;
4f774513 17184 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 17185
4f774513
JS
17186 /* Use the hdr_buf to find the sequence that this frame belongs to */
17187 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17188 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17189 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17190 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17191 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17192 continue;
17193 /* found a pending sequence that matches this frame */
17194 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17195 break;
17196 }
17197 if (!seq_dmabuf) {
17198 /*
17199 * This indicates first frame received for this sequence.
17200 * Queue the buffer on the vport's rcv_buffer_list.
17201 */
17202 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 17203 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
17204 return dmabuf;
17205 }
17206 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
17207 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17208 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
17209 list_del_init(&seq_dmabuf->hbuf.list);
17210 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17211 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 17212 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
17213 return dmabuf;
17214 }
45ed1190
JS
17215 /* move this sequence to the tail to indicate a young sequence */
17216 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17217 seq_dmabuf->time_stamp = jiffies;
17218 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
17219 if (list_empty(&seq_dmabuf->dbuf.list)) {
17220 temp_hdr = dmabuf->hbuf.virt;
17221 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17222 return seq_dmabuf;
17223 }
4f774513 17224 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
17225 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17226 while (!found) {
4f774513
JS
17227 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17228 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17229 /*
17230 * If the frame's sequence count is greater than the frame on
17231 * the list then insert the frame right after this frame
17232 */
eeead811
JS
17233 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17234 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 17235 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
17236 found = 1;
17237 break;
4f774513 17238 }
4360ca9c
JS
17239
17240 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17241 break;
17242 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 17243 }
4360ca9c
JS
17244
17245 if (found)
17246 return seq_dmabuf;
4f774513
JS
17247 return NULL;
17248}
17249
6669f9bb
JS
17250/**
17251 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17252 * @vport: pointer to a vitural port
17253 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17254 *
17255 * This function tries to abort from the partially assembed sequence, described
17256 * by the information from basic abbort @dmabuf. It checks to see whether such
17257 * partially assembled sequence held by the driver. If so, it shall free up all
17258 * the frames from the partially assembled sequence.
17259 *
17260 * Return
17261 * true -- if there is matching partially assembled sequence present and all
17262 * the frames freed with the sequence;
17263 * false -- if there is no matching partially assembled sequence present so
17264 * nothing got aborted in the lower layer driver
17265 **/
17266static bool
17267lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17268 struct hbq_dmabuf *dmabuf)
17269{
17270 struct fc_frame_header *new_hdr;
17271 struct fc_frame_header *temp_hdr;
17272 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17273 struct hbq_dmabuf *seq_dmabuf = NULL;
17274
17275 /* Use the hdr_buf to find the sequence that matches this frame */
17276 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17277 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17278 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17279 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17280 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17281 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17282 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17283 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17284 continue;
17285 /* found a pending sequence that matches this frame */
17286 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17287 break;
17288 }
17289
17290 /* Free up all the frames from the partially assembled sequence */
17291 if (seq_dmabuf) {
17292 list_for_each_entry_safe(d_buf, n_buf,
17293 &seq_dmabuf->dbuf.list, list) {
17294 list_del_init(&d_buf->list);
17295 lpfc_in_buf_free(vport->phba, d_buf);
17296 }
17297 return true;
17298 }
17299 return false;
17300}
17301
6dd9e31c
JS
17302/**
17303 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17304 * @vport: pointer to a vitural port
17305 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17306 *
17307 * This function tries to abort from the assembed sequence from upper level
17308 * protocol, described by the information from basic abbort @dmabuf. It
17309 * checks to see whether such pending context exists at upper level protocol.
17310 * If so, it shall clean up the pending context.
17311 *
17312 * Return
17313 * true -- if there is matching pending context of the sequence cleaned
17314 * at ulp;
17315 * false -- if there is no matching pending context of the sequence present
17316 * at ulp.
17317 **/
17318static bool
17319lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17320{
17321 struct lpfc_hba *phba = vport->phba;
17322 int handled;
17323
17324 /* Accepting abort at ulp with SLI4 only */
17325 if (phba->sli_rev < LPFC_SLI_REV4)
17326 return false;
17327
17328 /* Register all caring upper level protocols to attend abort */
17329 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17330 if (handled)
17331 return true;
17332
17333 return false;
17334}
17335
6669f9bb 17336/**
546fc854 17337 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
17338 * @phba: Pointer to HBA context object.
17339 * @cmd_iocbq: pointer to the command iocbq structure.
17340 * @rsp_iocbq: pointer to the response iocbq structure.
17341 *
546fc854 17342 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
17343 * event. It properly releases the memory allocated to the sequence abort
17344 * accept iocb.
17345 **/
17346static void
546fc854 17347lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
17348 struct lpfc_iocbq *cmd_iocbq,
17349 struct lpfc_iocbq *rsp_iocbq)
17350{
6dd9e31c
JS
17351 struct lpfc_nodelist *ndlp;
17352
17353 if (cmd_iocbq) {
17354 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17355 lpfc_nlp_put(ndlp);
17356 lpfc_nlp_not_used(ndlp);
6669f9bb 17357 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 17358 }
6b5151fd
JS
17359
17360 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17361 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17363 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17364 rsp_iocbq->iocb.ulpStatus,
17365 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
17366}
17367
6d368e53
JS
17368/**
17369 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17370 * @phba: Pointer to HBA context object.
17371 * @xri: xri id in transaction.
17372 *
17373 * This function validates the xri maps to the known range of XRIs allocated an
17374 * used by the driver.
17375 **/
7851fe2c 17376uint16_t
6d368e53
JS
17377lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17378 uint16_t xri)
17379{
a2fc4aef 17380 uint16_t i;
6d368e53
JS
17381
17382 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17383 if (xri == phba->sli4_hba.xri_ids[i])
17384 return i;
17385 }
17386 return NO_XRI;
17387}
17388
6669f9bb 17389/**
546fc854 17390 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
17391 * @phba: Pointer to HBA context object.
17392 * @fc_hdr: pointer to a FC frame header.
17393 *
546fc854 17394 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
17395 * event after aborting the sequence handling.
17396 **/
86c67379 17397void
6dd9e31c
JS
17398lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17399 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 17400{
6dd9e31c 17401 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
17402 struct lpfc_iocbq *ctiocb = NULL;
17403 struct lpfc_nodelist *ndlp;
ee0f4fe1 17404 uint16_t oxid, rxid, xri, lxri;
5ffc266e 17405 uint32_t sid, fctl;
6669f9bb 17406 IOCB_t *icmd;
546fc854 17407 int rc;
6669f9bb
JS
17408
17409 if (!lpfc_is_link_up(phba))
17410 return;
17411
17412 sid = sli4_sid_from_fc_hdr(fc_hdr);
17413 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 17414 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 17415
6dd9e31c 17416 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 17417 if (!ndlp) {
9d3d340d 17418 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
17419 if (!ndlp) {
17420 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17421 "1268 Failed to allocate ndlp for "
17422 "oxid:x%x SID:x%x\n", oxid, sid);
17423 return;
17424 }
6dd9e31c
JS
17425 /* Put ndlp onto pport node list */
17426 lpfc_enqueue_node(vport, ndlp);
17427 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17428 /* re-setup ndlp without removing from node list */
17429 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17430 if (!ndlp) {
17431 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17432 "3275 Failed to active ndlp found "
17433 "for oxid:x%x SID:x%x\n", oxid, sid);
17434 return;
17435 }
6669f9bb
JS
17436 }
17437
546fc854 17438 /* Allocate buffer for rsp iocb */
6669f9bb
JS
17439 ctiocb = lpfc_sli_get_iocbq(phba);
17440 if (!ctiocb)
17441 return;
17442
5ffc266e
JS
17443 /* Extract the F_CTL field from FC_HDR */
17444 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17445
6669f9bb 17446 icmd = &ctiocb->iocb;
6669f9bb 17447 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 17448 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
17449 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17450 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17451 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17452
17453 /* Fill in the rest of iocb fields */
17454 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17455 icmd->ulpBdeCount = 0;
17456 icmd->ulpLe = 1;
17457 icmd->ulpClass = CLASS3;
6d368e53 17458 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 17459 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 17460
6669f9bb
JS
17461 ctiocb->iocb_cmpl = NULL;
17462 ctiocb->vport = phba->pport;
546fc854 17463 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 17464 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
17465 ctiocb->sli4_xritag = NO_XRI;
17466
ee0f4fe1
JS
17467 if (fctl & FC_FC_EX_CTX)
17468 /* Exchange responder sent the abort so we
17469 * own the oxid.
17470 */
17471 xri = oxid;
17472 else
17473 xri = rxid;
17474 lxri = lpfc_sli4_xri_inrange(phba, xri);
17475 if (lxri != NO_XRI)
17476 lpfc_set_rrq_active(phba, ndlp, lxri,
17477 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
17478 /* For BA_ABTS from exchange responder, if the logical xri with
17479 * the oxid maps to the FCP XRI range, the port no longer has
17480 * that exchange context, send a BLS_RJT. Override the IOCB for
17481 * a BA_RJT.
17482 */
17483 if ((fctl & FC_FC_EX_CTX) &&
895427bd 17484 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
17485 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17486 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17487 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17488 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17489 }
17490
17491 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17492 * the driver no longer has that exchange, send a BLS_RJT. Override
17493 * the IOCB for a BA_RJT.
546fc854 17494 */
6dd9e31c 17495 if (aborted == false) {
546fc854
JS
17496 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17497 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17498 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17499 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17500 }
6669f9bb 17501
5ffc266e
JS
17502 if (fctl & FC_FC_EX_CTX) {
17503 /* ABTS sent by responder to CT exchange, construction
17504 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17505 * field and RX_ID from ABTS for RX_ID field.
17506 */
546fc854 17507 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
17508 } else {
17509 /* ABTS sent by initiator to CT exchange, construction
17510 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 17511 * XRI_TAG field.
5ffc266e 17512 */
546fc854 17513 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 17514 }
f09c3acc 17515 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 17516 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 17517
546fc854 17518 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
17519 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17520 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17521 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
17522
17523 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17524 if (rc == IOCB_ERROR) {
6dd9e31c
JS
17525 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17526 "2925 Failed to issue CT ABTS RSP x%x on "
17527 "xri x%x, Data x%x\n",
17528 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17529 phba->link_state);
17530 lpfc_nlp_put(ndlp);
17531 ctiocb->context1 = NULL;
546fc854
JS
17532 lpfc_sli_release_iocbq(phba, ctiocb);
17533 }
6669f9bb
JS
17534}
17535
17536/**
17537 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17538 * @vport: Pointer to the vport on which this sequence was received
17539 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17540 *
17541 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17542 * receive sequence is only partially assembed by the driver, it shall abort
17543 * the partially assembled frames for the sequence. Otherwise, if the
17544 * unsolicited receive sequence has been completely assembled and passed to
17545 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17546 * unsolicited sequence has been aborted. After that, it will issue a basic
17547 * accept to accept the abort.
17548 **/
5d8b8167 17549static void
6669f9bb
JS
17550lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17551 struct hbq_dmabuf *dmabuf)
17552{
17553 struct lpfc_hba *phba = vport->phba;
17554 struct fc_frame_header fc_hdr;
5ffc266e 17555 uint32_t fctl;
6dd9e31c 17556 bool aborted;
6669f9bb 17557
6669f9bb
JS
17558 /* Make a copy of fc_hdr before the dmabuf being released */
17559 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 17560 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 17561
5ffc266e 17562 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
17563 /* ABTS by responder to exchange, no cleanup needed */
17564 aborted = true;
5ffc266e 17565 } else {
6dd9e31c
JS
17566 /* ABTS by initiator to exchange, need to do cleanup */
17567 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17568 if (aborted == false)
17569 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 17570 }
6dd9e31c
JS
17571 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17572
86c67379
JS
17573 if (phba->nvmet_support) {
17574 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17575 return;
17576 }
17577
6dd9e31c
JS
17578 /* Respond with BA_ACC or BA_RJT accordingly */
17579 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
17580}
17581
4f774513
JS
17582/**
17583 * lpfc_seq_complete - Indicates if a sequence is complete
17584 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17585 *
17586 * This function checks the sequence, starting with the frame described by
17587 * @dmabuf, to see if all the frames associated with this sequence are present.
17588 * the frames associated with this sequence are linked to the @dmabuf using the
17589 * dbuf list. This function looks for two major things. 1) That the first frame
17590 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17591 * set. 3) That there are no holes in the sequence count. The function will
17592 * return 1 when the sequence is complete, otherwise it will return 0.
17593 **/
17594static int
17595lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17596{
17597 struct fc_frame_header *hdr;
17598 struct lpfc_dmabuf *d_buf;
17599 struct hbq_dmabuf *seq_dmabuf;
17600 uint32_t fctl;
17601 int seq_count = 0;
17602
17603 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17604 /* make sure first fame of sequence has a sequence count of zero */
17605 if (hdr->fh_seq_cnt != seq_count)
17606 return 0;
17607 fctl = (hdr->fh_f_ctl[0] << 16 |
17608 hdr->fh_f_ctl[1] << 8 |
17609 hdr->fh_f_ctl[2]);
17610 /* If last frame of sequence we can return success. */
17611 if (fctl & FC_FC_END_SEQ)
17612 return 1;
17613 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17614 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17615 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17616 /* If there is a hole in the sequence count then fail. */
eeead811 17617 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
17618 return 0;
17619 fctl = (hdr->fh_f_ctl[0] << 16 |
17620 hdr->fh_f_ctl[1] << 8 |
17621 hdr->fh_f_ctl[2]);
17622 /* If last frame of sequence we can return success. */
17623 if (fctl & FC_FC_END_SEQ)
17624 return 1;
17625 }
17626 return 0;
17627}
17628
17629/**
17630 * lpfc_prep_seq - Prep sequence for ULP processing
17631 * @vport: Pointer to the vport on which this sequence was received
17632 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17633 *
17634 * This function takes a sequence, described by a list of frames, and creates
17635 * a list of iocbq structures to describe the sequence. This iocbq list will be
17636 * used to issue to the generic unsolicited sequence handler. This routine
17637 * returns a pointer to the first iocbq in the list. If the function is unable
17638 * to allocate an iocbq then it throw out the received frames that were not
17639 * able to be described and return a pointer to the first iocbq. If unable to
17640 * allocate any iocbqs (including the first) this function will return NULL.
17641 **/
17642static struct lpfc_iocbq *
17643lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17644{
7851fe2c 17645 struct hbq_dmabuf *hbq_buf;
4f774513
JS
17646 struct lpfc_dmabuf *d_buf, *n_buf;
17647 struct lpfc_iocbq *first_iocbq, *iocbq;
17648 struct fc_frame_header *fc_hdr;
17649 uint32_t sid;
7851fe2c 17650 uint32_t len, tot_len;
eeead811 17651 struct ulp_bde64 *pbde;
4f774513
JS
17652
17653 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17654 /* remove from receive buffer list */
17655 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 17656 lpfc_update_rcv_time_stamp(vport);
4f774513 17657 /* get the Remote Port's SID */
6669f9bb 17658 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 17659 tot_len = 0;
4f774513
JS
17660 /* Get an iocbq struct to fill in. */
17661 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17662 if (first_iocbq) {
17663 /* Initialize the first IOCB. */
8fa38513 17664 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 17665 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 17666 first_iocbq->vport = vport;
939723a4
JS
17667
17668 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17669 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17670 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17671 first_iocbq->iocb.un.rcvels.parmRo =
17672 sli4_did_from_fc_hdr(fc_hdr);
17673 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17674 } else
17675 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17676 first_iocbq->iocb.ulpContext = NO_XRI;
17677 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17678 be16_to_cpu(fc_hdr->fh_ox_id);
17679 /* iocbq is prepped for internal consumption. Physical vpi. */
17680 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17681 vport->phba->vpi_ids[vport->vpi];
4f774513 17682 /* put the first buffer into the first IOCBq */
48a5a664
JS
17683 tot_len = bf_get(lpfc_rcqe_length,
17684 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17685
4f774513
JS
17686 first_iocbq->context2 = &seq_dmabuf->dbuf;
17687 first_iocbq->context3 = NULL;
17688 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17689 if (tot_len > LPFC_DATA_BUF_SIZE)
17690 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17691 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17692 else
17693 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17694
4f774513 17695 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17696
7851fe2c 17697 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17698 }
17699 iocbq = first_iocbq;
17700 /*
17701 * Each IOCBq can have two Buffers assigned, so go through the list
17702 * of buffers for this sequence and save two buffers in each IOCBq
17703 */
17704 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17705 if (!iocbq) {
17706 lpfc_in_buf_free(vport->phba, d_buf);
17707 continue;
17708 }
17709 if (!iocbq->context3) {
17710 iocbq->context3 = d_buf;
17711 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17712 /* We need to get the size out of the right CQE */
17713 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17714 len = bf_get(lpfc_rcqe_length,
17715 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17716 pbde = (struct ulp_bde64 *)
17717 &iocbq->iocb.unsli3.sli3Words[4];
17718 if (len > LPFC_DATA_BUF_SIZE)
17719 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17720 else
17721 pbde->tus.f.bdeSize = len;
17722
7851fe2c
JS
17723 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17724 tot_len += len;
4f774513
JS
17725 } else {
17726 iocbq = lpfc_sli_get_iocbq(vport->phba);
17727 if (!iocbq) {
17728 if (first_iocbq) {
17729 first_iocbq->iocb.ulpStatus =
17730 IOSTAT_FCP_RSP_ERROR;
17731 first_iocbq->iocb.un.ulpWord[4] =
17732 IOERR_NO_RESOURCES;
17733 }
17734 lpfc_in_buf_free(vport->phba, d_buf);
17735 continue;
17736 }
48a5a664
JS
17737 /* We need to get the size out of the right CQE */
17738 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17739 len = bf_get(lpfc_rcqe_length,
17740 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17741 iocbq->context2 = d_buf;
17742 iocbq->context3 = NULL;
17743 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17744 if (len > LPFC_DATA_BUF_SIZE)
17745 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17746 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17747 else
17748 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17749
7851fe2c
JS
17750 tot_len += len;
17751 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17752
4f774513
JS
17753 iocbq->iocb.un.rcvels.remoteID = sid;
17754 list_add_tail(&iocbq->list, &first_iocbq->list);
17755 }
17756 }
17757 return first_iocbq;
17758}
17759
6669f9bb
JS
17760static void
17761lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17762 struct hbq_dmabuf *seq_dmabuf)
17763{
17764 struct fc_frame_header *fc_hdr;
17765 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17766 struct lpfc_hba *phba = vport->phba;
17767
17768 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17769 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17770 if (!iocbq) {
17771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17772 "2707 Ring %d handler: Failed to allocate "
17773 "iocb Rctl x%x Type x%x received\n",
17774 LPFC_ELS_RING,
17775 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17776 return;
17777 }
17778 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17779 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17780 iocbq, fc_hdr->fh_r_ctl,
17781 fc_hdr->fh_type))
6d368e53 17782 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17783 "2540 Ring %d handler: unexpected Rctl "
17784 "x%x Type x%x received\n",
17785 LPFC_ELS_RING,
17786 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17787
17788 /* Free iocb created in lpfc_prep_seq */
17789 list_for_each_entry_safe(curr_iocb, next_iocb,
17790 &iocbq->list, list) {
17791 list_del_init(&curr_iocb->list);
17792 lpfc_sli_release_iocbq(phba, curr_iocb);
17793 }
17794 lpfc_sli_release_iocbq(phba, iocbq);
17795}
17796
ae9e28f3
JS
17797static void
17798lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17799 struct lpfc_iocbq *rspiocb)
17800{
17801 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17802
17803 if (pcmd && pcmd->virt)
771db5c0 17804 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17805 kfree(pcmd);
17806 lpfc_sli_release_iocbq(phba, cmdiocb);
17807}
17808
17809static void
17810lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17811 struct hbq_dmabuf *dmabuf)
17812{
17813 struct fc_frame_header *fc_hdr;
17814 struct lpfc_hba *phba = vport->phba;
17815 struct lpfc_iocbq *iocbq = NULL;
17816 union lpfc_wqe *wqe;
17817 struct lpfc_dmabuf *pcmd = NULL;
17818 uint32_t frame_len;
17819 int rc;
17820
17821 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17822 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17823
17824 /* Send the received frame back */
17825 iocbq = lpfc_sli_get_iocbq(phba);
17826 if (!iocbq)
17827 goto exit;
17828
17829 /* Allocate buffer for command payload */
17830 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17831 if (pcmd)
771db5c0 17832 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17833 &pcmd->phys);
17834 if (!pcmd || !pcmd->virt)
17835 goto exit;
17836
17837 INIT_LIST_HEAD(&pcmd->list);
17838
17839 /* copyin the payload */
17840 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17841
17842 /* fill in BDE's for command */
17843 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17844 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17845 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17846 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17847
17848 iocbq->context2 = pcmd;
17849 iocbq->vport = vport;
17850 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17851 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17852
17853 /*
17854 * Setup rest of the iocb as though it were a WQE
17855 * Build the SEND_FRAME WQE
17856 */
17857 wqe = (union lpfc_wqe *)&iocbq->iocb;
17858
17859 wqe->send_frame.frame_len = frame_len;
17860 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17861 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17862 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17863 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17864 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17865 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17866
17867 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17868 iocbq->iocb.ulpLe = 1;
17869 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17870 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17871 if (rc == IOCB_ERROR)
17872 goto exit;
17873
17874 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17875 return;
17876
17877exit:
17878 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17879 "2023 Unable to process MDS loopback frame\n");
17880 if (pcmd && pcmd->virt)
771db5c0 17881 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17882 kfree(pcmd);
401bb416
DK
17883 if (iocbq)
17884 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17885 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17886}
17887
4f774513
JS
17888/**
17889 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17890 * @phba: Pointer to HBA context object.
17891 *
17892 * This function is called with no lock held. This function processes all
17893 * the received buffers and gives it to upper layers when a received buffer
17894 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17895 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17896 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17897 * appropriate receive function when the final frame in a sequence is received.
17898 **/
4d9ab994
JS
17899void
17900lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17901 struct hbq_dmabuf *dmabuf)
4f774513 17902{
4d9ab994 17903 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17904 struct fc_frame_header *fc_hdr;
17905 struct lpfc_vport *vport;
17906 uint32_t fcfi;
939723a4 17907 uint32_t did;
4f774513 17908
4f774513 17909 /* Process each received buffer */
4d9ab994 17910 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17911
4d9ab994
JS
17912 /* check to see if this a valid type of frame */
17913 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17914 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17915 return;
17916 }
2ea259ee 17917
7851fe2c
JS
17918 if ((bf_get(lpfc_cqe_code,
17919 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17920 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17921 &dmabuf->cq_event.cqe.rcqe_cmpl);
17922 else
17923 fcfi = bf_get(lpfc_rcqe_fcf_id,
17924 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17925
ae9e28f3
JS
17926 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17927 vport = phba->pport;
17928 /* Handle MDS Loopback frames */
17929 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17930 return;
17931 }
17932
895427bd
JS
17933 /* d_id this frame is directed to */
17934 did = sli4_did_from_fc_hdr(fc_hdr);
17935
17936 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17937 if (!vport) {
4d9ab994
JS
17938 /* throw out the frame */
17939 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17940 return;
17941 }
939723a4 17942
939723a4
JS
17943 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17944 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17945 (did != Fabric_DID)) {
17946 /*
17947 * Throw out the frame if we are not pt2pt.
17948 * The pt2pt protocol allows for discovery frames
17949 * to be received without a registered VPI.
17950 */
17951 if (!(vport->fc_flag & FC_PT2PT) ||
17952 (phba->link_state == LPFC_HBA_READY)) {
17953 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17954 return;
17955 }
17956 }
17957
6669f9bb
JS
17958 /* Handle the basic abort sequence (BA_ABTS) event */
17959 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17960 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17961 return;
17962 }
17963
4d9ab994
JS
17964 /* Link this frame */
17965 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17966 if (!seq_dmabuf) {
17967 /* unable to add frame to vport - throw it out */
17968 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17969 return;
17970 }
17971 /* If not last frame in sequence continue processing frames. */
def9c7a9 17972 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17973 return;
def9c7a9 17974
6669f9bb
JS
17975 /* Send the complete sequence to the upper layer protocol */
17976 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17977}
6fb120a7
JS
17978
17979/**
17980 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17981 * @phba: pointer to lpfc hba data structure.
17982 *
17983 * This routine is invoked to post rpi header templates to the
17984 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17985 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17986 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17987 *
17988 * This routine does not require any locks. It's usage is expected
17989 * to be driver load or reset recovery when the driver is
17990 * sequential.
17991 *
17992 * Return codes
af901ca1 17993 * 0 - successful
d439d286 17994 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17995 * When this error occurs, the driver is not guaranteed
17996 * to have any rpi regions posted to the device and
17997 * must either attempt to repost the regions or take a
17998 * fatal error.
17999 **/
18000int
18001lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18002{
18003 struct lpfc_rpi_hdr *rpi_page;
18004 uint32_t rc = 0;
6d368e53
JS
18005 uint16_t lrpi = 0;
18006
18007 /* SLI4 ports that support extents do not require RPI headers. */
18008 if (!phba->sli4_hba.rpi_hdrs_in_use)
18009 goto exit;
18010 if (phba->sli4_hba.extents_in_use)
18011 return -EIO;
6fb120a7 18012
6fb120a7 18013 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
18014 /*
18015 * Assign the rpi headers a physical rpi only if the driver
18016 * has not initialized those resources. A port reset only
18017 * needs the headers posted.
18018 */
18019 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18020 LPFC_RPI_RSRC_RDY)
18021 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18022
6fb120a7
JS
18023 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18024 if (rc != MBX_SUCCESS) {
18025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18026 "2008 Error %d posting all rpi "
18027 "headers\n", rc);
18028 rc = -EIO;
18029 break;
18030 }
18031 }
18032
6d368e53
JS
18033 exit:
18034 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18035 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
18036 return rc;
18037}
18038
18039/**
18040 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18041 * @phba: pointer to lpfc hba data structure.
18042 * @rpi_page: pointer to the rpi memory region.
18043 *
18044 * This routine is invoked to post a single rpi header to the
18045 * HBA consistent with the SLI-4 interface spec. This memory region
18046 * maps up to 64 rpi context regions.
18047 *
18048 * Return codes
af901ca1 18049 * 0 - successful
d439d286
JS
18050 * -ENOMEM - No available memory
18051 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
18052 **/
18053int
18054lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18055{
18056 LPFC_MBOXQ_t *mboxq;
18057 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18058 uint32_t rc = 0;
6fb120a7
JS
18059 uint32_t shdr_status, shdr_add_status;
18060 union lpfc_sli4_cfg_shdr *shdr;
18061
6d368e53
JS
18062 /* SLI4 ports that support extents do not require RPI headers. */
18063 if (!phba->sli4_hba.rpi_hdrs_in_use)
18064 return rc;
18065 if (phba->sli4_hba.extents_in_use)
18066 return -EIO;
18067
6fb120a7
JS
18068 /* The port is notified of the header region via a mailbox command. */
18069 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18070 if (!mboxq) {
18071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18072 "2001 Unable to allocate memory for issuing "
18073 "SLI_CONFIG_SPECIAL mailbox command\n");
18074 return -ENOMEM;
18075 }
18076
18077 /* Post all rpi memory regions to the port. */
18078 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
18079 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18080 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18081 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
18082 sizeof(struct lpfc_sli4_cfg_mhdr),
18083 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
18084
18085
18086 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
18087 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18088 rpi_page->start_rpi);
6d368e53
JS
18089 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18090 hdr_tmpl, rpi_page->page_count);
18091
6fb120a7
JS
18092 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18093 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 18094 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
18095 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18096 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18097 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18098 if (rc != MBX_TIMEOUT)
18099 mempool_free(mboxq, phba->mbox_mem_pool);
18100 if (shdr_status || shdr_add_status || rc) {
18101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18102 "2514 POST_RPI_HDR mailbox failed with "
18103 "status x%x add_status x%x, mbx status x%x\n",
18104 shdr_status, shdr_add_status, rc);
18105 rc = -ENXIO;
845d9e8d
JS
18106 } else {
18107 /*
18108 * The next_rpi stores the next logical module-64 rpi value used
18109 * to post physical rpis in subsequent rpi postings.
18110 */
18111 spin_lock_irq(&phba->hbalock);
18112 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18113 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
18114 }
18115 return rc;
18116}
18117
18118/**
18119 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18120 * @phba: pointer to lpfc hba data structure.
18121 *
18122 * This routine is invoked to post rpi header templates to the
18123 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
18124 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18125 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
18126 *
18127 * Returns
af901ca1 18128 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
18129 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18130 **/
18131int
18132lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18133{
6d368e53
JS
18134 unsigned long rpi;
18135 uint16_t max_rpi, rpi_limit;
18136 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 18137 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 18138 unsigned long iflag;
6fb120a7 18139
6fb120a7 18140 /*
6d368e53
JS
18141 * Fetch the next logical rpi. Because this index is logical,
18142 * the driver starts at 0 each time.
6fb120a7 18143 */
4902b381 18144 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
18145 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18146 rpi_limit = phba->sli4_hba.next_rpi;
18147
6d368e53
JS
18148 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18149 if (rpi >= rpi_limit)
6fb120a7
JS
18150 rpi = LPFC_RPI_ALLOC_ERROR;
18151 else {
18152 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18153 phba->sli4_hba.max_cfg_param.rpi_used++;
18154 phba->sli4_hba.rpi_count++;
18155 }
be6bb941
JS
18156 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18157 "0001 rpi:%x max:%x lim:%x\n",
18158 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
18159
18160 /*
18161 * Don't try to allocate more rpi header regions if the device limit
6d368e53 18162 * has been exhausted.
6fb120a7
JS
18163 */
18164 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18165 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 18166 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
18167 return rpi;
18168 }
18169
6d368e53
JS
18170 /*
18171 * RPI header postings are not required for SLI4 ports capable of
18172 * extents.
18173 */
18174 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 18175 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
18176 return rpi;
18177 }
18178
6fb120a7
JS
18179 /*
18180 * If the driver is running low on rpi resources, allocate another
18181 * page now. Note that the next_rpi value is used because
18182 * it represents how many are actually in use whereas max_rpi notes
18183 * how many are supported max by the device.
18184 */
6d368e53 18185 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 18186 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
18187 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18188 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18189 if (!rpi_hdr) {
18190 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18191 "2002 Error Could not grow rpi "
18192 "count\n");
18193 } else {
6d368e53
JS
18194 lrpi = rpi_hdr->start_rpi;
18195 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
18196 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18197 }
18198 }
18199
18200 return rpi;
18201}
18202
d7c47992
JS
18203/**
18204 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18205 * @phba: pointer to lpfc hba data structure.
18206 *
18207 * This routine is invoked to release an rpi to the pool of
18208 * available rpis maintained by the driver.
18209 **/
5d8b8167 18210static void
d7c47992
JS
18211__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18212{
18213 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18214 phba->sli4_hba.rpi_count--;
18215 phba->sli4_hba.max_cfg_param.rpi_used--;
18216 }
18217}
18218
6fb120a7
JS
18219/**
18220 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18221 * @phba: pointer to lpfc hba data structure.
18222 *
18223 * This routine is invoked to release an rpi to the pool of
18224 * available rpis maintained by the driver.
18225 **/
18226void
18227lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18228{
18229 spin_lock_irq(&phba->hbalock);
d7c47992 18230 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
18231 spin_unlock_irq(&phba->hbalock);
18232}
18233
18234/**
18235 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18236 * @phba: pointer to lpfc hba data structure.
18237 *
18238 * This routine is invoked to remove the memory region that
18239 * provided rpi via a bitmask.
18240 **/
18241void
18242lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18243{
18244 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
18245 kfree(phba->sli4_hba.rpi_ids);
18246 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
18247}
18248
18249/**
18250 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18251 * @phba: pointer to lpfc hba data structure.
18252 *
18253 * This routine is invoked to remove the memory region that
18254 * provided rpi via a bitmask.
18255 **/
18256int
6b5151fd
JS
18257lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18258 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
18259{
18260 LPFC_MBOXQ_t *mboxq;
18261 struct lpfc_hba *phba = ndlp->phba;
18262 int rc;
18263
18264 /* The port is notified of the header region via a mailbox command. */
18265 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18266 if (!mboxq)
18267 return -ENOMEM;
18268
18269 /* Post all rpi memory regions to the port. */
18270 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
18271 if (cmpl) {
18272 mboxq->mbox_cmpl = cmpl;
3e1f0718
JS
18273 mboxq->ctx_buf = arg;
18274 mboxq->ctx_ndlp = ndlp;
72859909
JS
18275 } else
18276 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 18277 mboxq->vport = ndlp->vport;
6fb120a7
JS
18278 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18279 if (rc == MBX_NOT_FINISHED) {
18280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18281 "2010 Resume RPI Mailbox failed "
18282 "status %d, mbxStatus x%x\n", rc,
18283 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18284 mempool_free(mboxq, phba->mbox_mem_pool);
18285 return -EIO;
18286 }
18287 return 0;
18288}
18289
18290/**
18291 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 18292 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 18293 *
76a95d75 18294 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
18295 *
18296 * Returns:
18297 * 0 success
18298 * -Evalue otherwise
18299 **/
18300int
76a95d75 18301lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
18302{
18303 LPFC_MBOXQ_t *mboxq;
18304 int rc = 0;
6a9c52cf 18305 int retval = MBX_SUCCESS;
6fb120a7 18306 uint32_t mbox_tmo;
76a95d75 18307 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
18308 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18309 if (!mboxq)
18310 return -ENOMEM;
76a95d75 18311 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 18312 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 18313 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 18314 if (rc != MBX_SUCCESS) {
76a95d75 18315 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
18316 "2022 INIT VPI Mailbox failed "
18317 "status %d, mbxStatus x%x\n", rc,
18318 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 18319 retval = -EIO;
6fb120a7 18320 }
6a9c52cf 18321 if (rc != MBX_TIMEOUT)
76a95d75 18322 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
18323
18324 return retval;
6fb120a7
JS
18325}
18326
18327/**
18328 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18329 * @phba: pointer to lpfc hba data structure.
18330 * @mboxq: Pointer to mailbox object.
18331 *
18332 * This routine is invoked to manually add a single FCF record. The caller
18333 * must pass a completely initialized FCF_Record. This routine takes
18334 * care of the nonembedded mailbox operations.
18335 **/
18336static void
18337lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18338{
18339 void *virt_addr;
18340 union lpfc_sli4_cfg_shdr *shdr;
18341 uint32_t shdr_status, shdr_add_status;
18342
18343 virt_addr = mboxq->sge_array->addr[0];
18344 /* The IOCTL status is embedded in the mailbox subheader. */
18345 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18346 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18347 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18348
18349 if ((shdr_status || shdr_add_status) &&
18350 (shdr_status != STATUS_FCF_IN_USE))
18351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18352 "2558 ADD_FCF_RECORD mailbox failed with "
18353 "status x%x add_status x%x\n",
18354 shdr_status, shdr_add_status);
18355
18356 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18357}
18358
18359/**
18360 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18361 * @phba: pointer to lpfc hba data structure.
18362 * @fcf_record: pointer to the initialized fcf record to add.
18363 *
18364 * This routine is invoked to manually add a single FCF record. The caller
18365 * must pass a completely initialized FCF_Record. This routine takes
18366 * care of the nonembedded mailbox operations.
18367 **/
18368int
18369lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18370{
18371 int rc = 0;
18372 LPFC_MBOXQ_t *mboxq;
18373 uint8_t *bytep;
18374 void *virt_addr;
6fb120a7
JS
18375 struct lpfc_mbx_sge sge;
18376 uint32_t alloc_len, req_len;
18377 uint32_t fcfindex;
18378
18379 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18380 if (!mboxq) {
18381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18382 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18383 return -ENOMEM;
18384 }
18385
18386 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18387 sizeof(uint32_t);
18388
18389 /* Allocate DMA memory and set up the non-embedded mailbox command */
18390 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18391 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18392 req_len, LPFC_SLI4_MBX_NEMBED);
18393 if (alloc_len < req_len) {
18394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18395 "2523 Allocated DMA memory size (x%x) is "
18396 "less than the requested DMA memory "
18397 "size (x%x)\n", alloc_len, req_len);
18398 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18399 return -ENOMEM;
18400 }
18401
18402 /*
18403 * Get the first SGE entry from the non-embedded DMA memory. This
18404 * routine only uses a single SGE.
18405 */
18406 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
18407 virt_addr = mboxq->sge_array->addr[0];
18408 /*
18409 * Configure the FCF record for FCFI 0. This is the driver's
18410 * hardcoded default and gets used in nonFIP mode.
18411 */
18412 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18413 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18414 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18415
18416 /*
18417 * Copy the fcf_index and the FCF Record Data. The data starts after
18418 * the FCoE header plus word10. The data copy needs to be endian
18419 * correct.
18420 */
18421 bytep += sizeof(uint32_t);
18422 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18423 mboxq->vport = phba->pport;
18424 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18426 if (rc == MBX_NOT_FINISHED) {
18427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18428 "2515 ADD_FCF_RECORD mailbox failed with "
18429 "status 0x%x\n", rc);
18430 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18431 rc = -EIO;
18432 } else
18433 rc = 0;
18434
18435 return rc;
18436}
18437
18438/**
18439 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18440 * @phba: pointer to lpfc hba data structure.
18441 * @fcf_record: pointer to the fcf record to write the default data.
18442 * @fcf_index: FCF table entry index.
18443 *
18444 * This routine is invoked to build the driver's default FCF record. The
18445 * values used are hardcoded. This routine handles memory initialization.
18446 *
18447 **/
18448void
18449lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18450 struct fcf_record *fcf_record,
18451 uint16_t fcf_index)
18452{
18453 memset(fcf_record, 0, sizeof(struct fcf_record));
18454 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18455 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18456 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18457 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18458 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18459 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18460 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18461 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18462 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18463 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18464 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18465 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18466 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 18467 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
18468 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18469 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18470 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18471 /* Set the VLAN bit map */
18472 if (phba->valid_vlan) {
18473 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18474 = 1 << (phba->vlan_id % 8);
18475 }
18476}
18477
18478/**
0c9ab6f5 18479 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
18480 * @phba: pointer to lpfc hba data structure.
18481 * @fcf_index: FCF table entry offset.
18482 *
0c9ab6f5
JS
18483 * This routine is invoked to scan the entire FCF table by reading FCF
18484 * record and processing it one at a time starting from the @fcf_index
18485 * for initial FCF discovery or fast FCF failover rediscovery.
18486 *
25985edc 18487 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 18488 * otherwise.
6fb120a7
JS
18489 **/
18490int
0c9ab6f5 18491lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
18492{
18493 int rc = 0, error;
18494 LPFC_MBOXQ_t *mboxq;
6fb120a7 18495
32b9793f 18496 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 18497 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
18498 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18499 if (!mboxq) {
18500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18501 "2000 Failed to allocate mbox for "
18502 "READ_FCF cmd\n");
4d9ab994 18503 error = -ENOMEM;
0c9ab6f5 18504 goto fail_fcf_scan;
6fb120a7 18505 }
ecfd03c6 18506 /* Construct the read FCF record mailbox command */
0c9ab6f5 18507 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
18508 if (rc) {
18509 error = -EINVAL;
0c9ab6f5 18510 goto fail_fcf_scan;
6fb120a7 18511 }
ecfd03c6 18512 /* Issue the mailbox command asynchronously */
6fb120a7 18513 mboxq->vport = phba->pport;
0c9ab6f5 18514 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
18515
18516 spin_lock_irq(&phba->hbalock);
18517 phba->hba_flag |= FCF_TS_INPROG;
18518 spin_unlock_irq(&phba->hbalock);
18519
6fb120a7 18520 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 18521 if (rc == MBX_NOT_FINISHED)
6fb120a7 18522 error = -EIO;
ecfd03c6 18523 else {
38b92ef8
JS
18524 /* Reset eligible FCF count for new scan */
18525 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 18526 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 18527 error = 0;
32b9793f 18528 }
0c9ab6f5 18529fail_fcf_scan:
4d9ab994
JS
18530 if (error) {
18531 if (mboxq)
18532 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 18533 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 18534 spin_lock_irq(&phba->hbalock);
a93ff37a 18535 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
18536 spin_unlock_irq(&phba->hbalock);
18537 }
6fb120a7
JS
18538 return error;
18539}
a0c87cbd 18540
0c9ab6f5 18541/**
a93ff37a 18542 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
18543 * @phba: pointer to lpfc hba data structure.
18544 * @fcf_index: FCF table entry offset.
18545 *
18546 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 18547 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 18548 *
25985edc 18549 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18550 * otherwise.
18551 **/
18552int
18553lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18554{
18555 int rc = 0, error;
18556 LPFC_MBOXQ_t *mboxq;
18557
18558 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18559 if (!mboxq) {
18560 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18561 "2763 Failed to allocate mbox for "
18562 "READ_FCF cmd\n");
18563 error = -ENOMEM;
18564 goto fail_fcf_read;
18565 }
18566 /* Construct the read FCF record mailbox command */
18567 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18568 if (rc) {
18569 error = -EINVAL;
18570 goto fail_fcf_read;
18571 }
18572 /* Issue the mailbox command asynchronously */
18573 mboxq->vport = phba->pport;
18574 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18575 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18576 if (rc == MBX_NOT_FINISHED)
18577 error = -EIO;
18578 else
18579 error = 0;
18580
18581fail_fcf_read:
18582 if (error && mboxq)
18583 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18584 return error;
18585}
18586
18587/**
18588 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18589 * @phba: pointer to lpfc hba data structure.
18590 * @fcf_index: FCF table entry offset.
18591 *
18592 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 18593 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 18594 *
25985edc 18595 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18596 * otherwise.
18597 **/
18598int
18599lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18600{
18601 int rc = 0, error;
18602 LPFC_MBOXQ_t *mboxq;
18603
18604 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18605 if (!mboxq) {
18606 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18607 "2758 Failed to allocate mbox for "
18608 "READ_FCF cmd\n");
18609 error = -ENOMEM;
18610 goto fail_fcf_read;
18611 }
18612 /* Construct the read FCF record mailbox command */
18613 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18614 if (rc) {
18615 error = -EINVAL;
18616 goto fail_fcf_read;
18617 }
18618 /* Issue the mailbox command asynchronously */
18619 mboxq->vport = phba->pport;
18620 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18621 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18622 if (rc == MBX_NOT_FINISHED)
18623 error = -EIO;
18624 else
18625 error = 0;
18626
18627fail_fcf_read:
18628 if (error && mboxq)
18629 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18630 return error;
18631}
18632
7d791df7 18633/**
f5cb5304 18634 * lpfc_check_next_fcf_pri_level
7d791df7
JS
18635 * phba pointer to the lpfc_hba struct for this port.
18636 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18637 * routine when the rr_bmask is empty. The FCF indecies are put into the
18638 * rr_bmask based on their priority level. Starting from the highest priority
18639 * to the lowest. The most likely FCF candidate will be in the highest
18640 * priority group. When this routine is called it searches the fcf_pri list for
18641 * next lowest priority group and repopulates the rr_bmask with only those
18642 * fcf_indexes.
18643 * returns:
18644 * 1=success 0=failure
18645 **/
5d8b8167 18646static int
7d791df7
JS
18647lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18648{
18649 uint16_t next_fcf_pri;
18650 uint16_t last_index;
18651 struct lpfc_fcf_pri *fcf_pri;
18652 int rc;
18653 int ret = 0;
18654
18655 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18656 LPFC_SLI4_FCF_TBL_INDX_MAX);
18657 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18658 "3060 Last IDX %d\n", last_index);
2562669c
JS
18659
18660 /* Verify the priority list has 2 or more entries */
18661 spin_lock_irq(&phba->hbalock);
18662 if (list_empty(&phba->fcf.fcf_pri_list) ||
18663 list_is_singular(&phba->fcf.fcf_pri_list)) {
18664 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
18665 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18666 "3061 Last IDX %d\n", last_index);
18667 return 0; /* Empty rr list */
18668 }
2562669c
JS
18669 spin_unlock_irq(&phba->hbalock);
18670
7d791df7
JS
18671 next_fcf_pri = 0;
18672 /*
18673 * Clear the rr_bmask and set all of the bits that are at this
18674 * priority.
18675 */
18676 memset(phba->fcf.fcf_rr_bmask, 0,
18677 sizeof(*phba->fcf.fcf_rr_bmask));
18678 spin_lock_irq(&phba->hbalock);
18679 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18680 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18681 continue;
18682 /*
18683 * the 1st priority that has not FLOGI failed
18684 * will be the highest.
18685 */
18686 if (!next_fcf_pri)
18687 next_fcf_pri = fcf_pri->fcf_rec.priority;
18688 spin_unlock_irq(&phba->hbalock);
18689 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18690 rc = lpfc_sli4_fcf_rr_index_set(phba,
18691 fcf_pri->fcf_rec.fcf_index);
18692 if (rc)
18693 return 0;
18694 }
18695 spin_lock_irq(&phba->hbalock);
18696 }
18697 /*
18698 * if next_fcf_pri was not set above and the list is not empty then
18699 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18700 * and start at the beginning.
7d791df7
JS
18701 */
18702 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18703 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18704 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18705 /*
18706 * the 1st priority that has not FLOGI failed
18707 * will be the highest.
18708 */
18709 if (!next_fcf_pri)
18710 next_fcf_pri = fcf_pri->fcf_rec.priority;
18711 spin_unlock_irq(&phba->hbalock);
18712 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18713 rc = lpfc_sli4_fcf_rr_index_set(phba,
18714 fcf_pri->fcf_rec.fcf_index);
18715 if (rc)
18716 return 0;
18717 }
18718 spin_lock_irq(&phba->hbalock);
18719 }
18720 } else
18721 ret = 1;
18722 spin_unlock_irq(&phba->hbalock);
18723
18724 return ret;
18725}
0c9ab6f5
JS
18726/**
18727 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18728 * @phba: pointer to lpfc hba data structure.
18729 *
18730 * This routine is to get the next eligible FCF record index in a round
18731 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18732 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18733 * shall be returned, otherwise, the next eligible FCF record's index
18734 * shall be returned.
18735 **/
18736uint16_t
18737lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18738{
18739 uint16_t next_fcf_index;
18740
421c6622 18741initial_priority:
3804dc84 18742 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18743 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18744
7d791df7 18745next_priority:
421c6622
JS
18746 /* Determine the next fcf index to check */
18747 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18748 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18749 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18750 next_fcf_index);
18751
0c9ab6f5 18752 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18753 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18754 /*
18755 * If we have wrapped then we need to clear the bits that
18756 * have been tested so that we can detect when we should
18757 * change the priority level.
18758 */
0c9ab6f5
JS
18759 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18760 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18761 }
18762
3804dc84
JS
18763
18764 /* Check roundrobin failover list empty condition */
7d791df7
JS
18765 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18766 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18767 /*
18768 * If next fcf index is not found check if there are lower
18769 * Priority level fcf's in the fcf_priority list.
18770 * Set up the rr_bmask with all of the avaiable fcf bits
18771 * at that level and continue the selection process.
18772 */
18773 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18774 goto initial_priority;
3804dc84
JS
18775 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18776 "2844 No roundrobin failover FCF available\n");
036cad1f
JS
18777
18778 return LPFC_FCOE_FCF_NEXT_NONE;
3804dc84
JS
18779 }
18780
7d791df7
JS
18781 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18782 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18783 LPFC_FCF_FLOGI_FAILED) {
18784 if (list_is_singular(&phba->fcf.fcf_pri_list))
18785 return LPFC_FCOE_FCF_NEXT_NONE;
18786
7d791df7 18787 goto next_priority;
f5cb5304 18788 }
7d791df7 18789
3804dc84 18790 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18791 "2845 Get next roundrobin failover FCF (x%x)\n",
18792 next_fcf_index);
18793
0c9ab6f5
JS
18794 return next_fcf_index;
18795}
18796
18797/**
18798 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18799 * @phba: pointer to lpfc hba data structure.
18800 *
18801 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18802 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18803 * does not go beyond the range of the driver allocated bmask dimension
18804 * before setting the bit.
18805 *
18806 * Returns 0 if the index bit successfully set, otherwise, it returns
18807 * -EINVAL.
18808 **/
18809int
18810lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18811{
18812 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18813 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18814 "2610 FCF (x%x) reached driver's book "
18815 "keeping dimension:x%x\n",
0c9ab6f5
JS
18816 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18817 return -EINVAL;
18818 }
18819 /* Set the eligible FCF record index bmask */
18820 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18821
3804dc84 18822 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18823 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18824 "bmask\n", fcf_index);
18825
0c9ab6f5
JS
18826 return 0;
18827}
18828
18829/**
3804dc84 18830 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18831 * @phba: pointer to lpfc hba data structure.
18832 *
18833 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18834 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18835 * does not go beyond the range of the driver allocated bmask dimension
18836 * before clearing the bit.
18837 **/
18838void
18839lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18840{
9a803a74 18841 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18842 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18843 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18844 "2762 FCF (x%x) reached driver's book "
18845 "keeping dimension:x%x\n",
0c9ab6f5
JS
18846 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18847 return;
18848 }
18849 /* Clear the eligible FCF record index bmask */
7d791df7 18850 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18851 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18852 list) {
7d791df7
JS
18853 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18854 list_del_init(&fcf_pri->list);
18855 break;
18856 }
18857 }
18858 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18859 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18860
18861 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18862 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18863 "bmask\n", fcf_index);
0c9ab6f5
JS
18864}
18865
ecfd03c6
JS
18866/**
18867 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18868 * @phba: pointer to lpfc hba data structure.
18869 *
18870 * This routine is the completion routine for the rediscover FCF table mailbox
18871 * command. If the mailbox command returned failure, it will try to stop the
18872 * FCF rediscover wait timer.
18873 **/
5d8b8167 18874static void
ecfd03c6
JS
18875lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18876{
18877 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18878 uint32_t shdr_status, shdr_add_status;
18879
18880 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18881
18882 shdr_status = bf_get(lpfc_mbox_hdr_status,
18883 &redisc_fcf->header.cfg_shdr.response);
18884 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18885 &redisc_fcf->header.cfg_shdr.response);
18886 if (shdr_status || shdr_add_status) {
0c9ab6f5 18887 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18888 "2746 Requesting for FCF rediscovery failed "
18889 "status x%x add_status x%x\n",
18890 shdr_status, shdr_add_status);
0c9ab6f5 18891 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18892 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18893 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18894 spin_unlock_irq(&phba->hbalock);
18895 /*
18896 * CVL event triggered FCF rediscover request failed,
18897 * last resort to re-try current registered FCF entry.
18898 */
18899 lpfc_retry_pport_discovery(phba);
18900 } else {
18901 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18902 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18903 spin_unlock_irq(&phba->hbalock);
18904 /*
18905 * DEAD FCF event triggered FCF rediscover request
18906 * failed, last resort to fail over as a link down
18907 * to FCF registration.
18908 */
18909 lpfc_sli4_fcf_dead_failthrough(phba);
18910 }
0c9ab6f5
JS
18911 } else {
18912 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18913 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18914 /*
18915 * Start FCF rediscovery wait timer for pending FCF
18916 * before rescan FCF record table.
18917 */
18918 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18919 }
ecfd03c6
JS
18920
18921 mempool_free(mbox, phba->mbox_mem_pool);
18922}
18923
18924/**
3804dc84 18925 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18926 * @phba: pointer to lpfc hba data structure.
18927 *
18928 * This routine is invoked to request for rediscovery of the entire FCF table
18929 * by the port.
18930 **/
18931int
18932lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18933{
18934 LPFC_MBOXQ_t *mbox;
18935 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18936 int rc, length;
18937
0c9ab6f5
JS
18938 /* Cancel retry delay timers to all vports before FCF rediscover */
18939 lpfc_cancel_all_vport_retry_delay_timer(phba);
18940
ecfd03c6
JS
18941 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18942 if (!mbox) {
18943 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18944 "2745 Failed to allocate mbox for "
18945 "requesting FCF rediscover.\n");
18946 return -ENOMEM;
18947 }
18948
18949 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18950 sizeof(struct lpfc_sli4_cfg_mhdr));
18951 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18952 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18953 length, LPFC_SLI4_MBX_EMBED);
18954
18955 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18956 /* Set count to 0 for invalidating the entire FCF database */
18957 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18958
18959 /* Issue the mailbox command asynchronously */
18960 mbox->vport = phba->pport;
18961 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18962 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18963
18964 if (rc == MBX_NOT_FINISHED) {
18965 mempool_free(mbox, phba->mbox_mem_pool);
18966 return -EIO;
18967 }
18968 return 0;
18969}
18970
fc2b989b
JS
18971/**
18972 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18973 * @phba: pointer to lpfc hba data structure.
18974 *
18975 * This function is the failover routine as a last resort to the FCF DEAD
18976 * event when driver failed to perform fast FCF failover.
18977 **/
18978void
18979lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18980{
18981 uint32_t link_state;
18982
18983 /*
18984 * Last resort as FCF DEAD event failover will treat this as
18985 * a link down, but save the link state because we don't want
18986 * it to be changed to Link Down unless it is already down.
18987 */
18988 link_state = phba->link_state;
18989 lpfc_linkdown(phba);
18990 phba->link_state = link_state;
18991
18992 /* Unregister FCF if no devices connected to it */
18993 lpfc_unregister_unused_fcf(phba);
18994}
18995
a0c87cbd 18996/**
026abb87 18997 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18998 * @phba: pointer to lpfc hba data structure.
026abb87 18999 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 19000 *
026abb87
JS
19001 * This function gets SLI3 port configure region 23 data through memory dump
19002 * mailbox command. When it successfully retrieves data, the size of the data
19003 * will be returned, otherwise, 0 will be returned.
a0c87cbd 19004 **/
026abb87
JS
19005static uint32_t
19006lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
19007{
19008 LPFC_MBOXQ_t *pmb = NULL;
19009 MAILBOX_t *mb;
026abb87 19010 uint32_t offset = 0;
a0c87cbd
JS
19011 int rc;
19012
026abb87
JS
19013 if (!rgn23_data)
19014 return 0;
19015
a0c87cbd
JS
19016 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19017 if (!pmb) {
19018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
19019 "2600 failed to allocate mailbox memory\n");
19020 return 0;
a0c87cbd
JS
19021 }
19022 mb = &pmb->u.mb;
19023
a0c87cbd
JS
19024 do {
19025 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19026 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19027
19028 if (rc != MBX_SUCCESS) {
19029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
19030 "2601 failed to read config "
19031 "region 23, rc 0x%x Status 0x%x\n",
19032 rc, mb->mbxStatus);
a0c87cbd
JS
19033 mb->un.varDmp.word_cnt = 0;
19034 }
19035 /*
19036 * dump mem may return a zero when finished or we got a
19037 * mailbox error, either way we are done.
19038 */
19039 if (mb->un.varDmp.word_cnt == 0)
19040 break;
19041 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
19042 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
19043
19044 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
19045 rgn23_data + offset,
19046 mb->un.varDmp.word_cnt);
a0c87cbd
JS
19047 offset += mb->un.varDmp.word_cnt;
19048 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
19049
026abb87
JS
19050 mempool_free(pmb, phba->mbox_mem_pool);
19051 return offset;
19052}
19053
19054/**
19055 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19056 * @phba: pointer to lpfc hba data structure.
19057 * @rgn23_data: pointer to configure region 23 data.
19058 *
19059 * This function gets SLI4 port configure region 23 data through memory dump
19060 * mailbox command. When it successfully retrieves data, the size of the data
19061 * will be returned, otherwise, 0 will be returned.
19062 **/
19063static uint32_t
19064lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19065{
19066 LPFC_MBOXQ_t *mboxq = NULL;
19067 struct lpfc_dmabuf *mp = NULL;
19068 struct lpfc_mqe *mqe;
19069 uint32_t data_length = 0;
19070 int rc;
19071
19072 if (!rgn23_data)
19073 return 0;
19074
19075 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19076 if (!mboxq) {
19077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19078 "3105 failed to allocate mailbox memory\n");
19079 return 0;
19080 }
19081
19082 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19083 goto out;
19084 mqe = &mboxq->u.mqe;
3e1f0718 19085 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
026abb87
JS
19086 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19087 if (rc)
19088 goto out;
19089 data_length = mqe->un.mb_words[5];
19090 if (data_length == 0)
19091 goto out;
19092 if (data_length > DMP_RGN23_SIZE) {
19093 data_length = 0;
19094 goto out;
19095 }
19096 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19097out:
19098 mempool_free(mboxq, phba->mbox_mem_pool);
19099 if (mp) {
19100 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19101 kfree(mp);
19102 }
19103 return data_length;
19104}
19105
19106/**
19107 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19108 * @phba: pointer to lpfc hba data structure.
19109 *
19110 * This function read region 23 and parse TLV for port status to
19111 * decide if the user disaled the port. If the TLV indicates the
19112 * port is disabled, the hba_flag is set accordingly.
19113 **/
19114void
19115lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19116{
19117 uint8_t *rgn23_data = NULL;
19118 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19119 uint32_t offset = 0;
19120
19121 /* Get adapter Region 23 data */
19122 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19123 if (!rgn23_data)
19124 goto out;
19125
19126 if (phba->sli_rev < LPFC_SLI_REV4)
19127 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19128 else {
19129 if_type = bf_get(lpfc_sli_intf_if_type,
19130 &phba->sli4_hba.sli_intf);
19131 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19132 goto out;
19133 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19134 }
a0c87cbd
JS
19135
19136 if (!data_size)
19137 goto out;
19138
19139 /* Check the region signature first */
19140 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19142 "2619 Config region 23 has bad signature\n");
19143 goto out;
19144 }
19145 offset += 4;
19146
19147 /* Check the data structure version */
19148 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19150 "2620 Config region 23 has bad version\n");
19151 goto out;
19152 }
19153 offset += 4;
19154
19155 /* Parse TLV entries in the region */
19156 while (offset < data_size) {
19157 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19158 break;
19159 /*
19160 * If the TLV is not driver specific TLV or driver id is
19161 * not linux driver id, skip the record.
19162 */
19163 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19164 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19165 (rgn23_data[offset + 3] != 0)) {
19166 offset += rgn23_data[offset + 1] * 4 + 4;
19167 continue;
19168 }
19169
19170 /* Driver found a driver specific TLV in the config region */
19171 sub_tlv_len = rgn23_data[offset + 1] * 4;
19172 offset += 4;
19173 tlv_offset = 0;
19174
19175 /*
19176 * Search for configured port state sub-TLV.
19177 */
19178 while ((offset < data_size) &&
19179 (tlv_offset < sub_tlv_len)) {
19180 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19181 offset += 4;
19182 tlv_offset += 4;
19183 break;
19184 }
19185 if (rgn23_data[offset] != PORT_STE_TYPE) {
19186 offset += rgn23_data[offset + 1] * 4 + 4;
19187 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19188 continue;
19189 }
19190
19191 /* This HBA contains PORT_STE configured */
19192 if (!rgn23_data[offset + 2])
19193 phba->hba_flag |= LINK_DISABLED;
19194
19195 goto out;
19196 }
19197 }
026abb87 19198
a0c87cbd 19199out:
a0c87cbd
JS
19200 kfree(rgn23_data);
19201 return;
19202}
695a814e 19203
52d52440
JS
19204/**
19205 * lpfc_wr_object - write an object to the firmware
19206 * @phba: HBA structure that indicates port to create a queue on.
19207 * @dmabuf_list: list of dmabufs to write to the port.
19208 * @size: the total byte value of the objects to write to the port.
19209 * @offset: the current offset to be used to start the transfer.
19210 *
19211 * This routine will create a wr_object mailbox command to send to the port.
19212 * the mailbox command will be constructed using the dma buffers described in
19213 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19214 * BDEs that the imbedded mailbox can support. The @offset variable will be
19215 * used to indicate the starting offset of the transfer and will also return
19216 * the offset after the write object mailbox has completed. @size is used to
19217 * determine the end of the object and whether the eof bit should be set.
19218 *
19219 * Return 0 is successful and offset will contain the the new offset to use
19220 * for the next write.
19221 * Return negative value for error cases.
19222 **/
19223int
19224lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19225 uint32_t size, uint32_t *offset)
19226{
19227 struct lpfc_mbx_wr_object *wr_object;
19228 LPFC_MBOXQ_t *mbox;
19229 int rc = 0, i = 0;
19230 uint32_t shdr_status, shdr_add_status;
19231 uint32_t mbox_tmo;
19232 union lpfc_sli4_cfg_shdr *shdr;
19233 struct lpfc_dmabuf *dmabuf;
19234 uint32_t written = 0;
19235
19236 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19237 if (!mbox)
19238 return -ENOMEM;
19239
19240 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19241 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19242 sizeof(struct lpfc_mbx_wr_object) -
19243 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19244
19245 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19246 wr_object->u.request.write_offset = *offset;
19247 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19248 wr_object->u.request.object_name[0] =
19249 cpu_to_le32(wr_object->u.request.object_name[0]);
19250 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19251 list_for_each_entry(dmabuf, dmabuf_list, list) {
19252 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19253 break;
19254 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19255 wr_object->u.request.bde[i].addrHigh =
19256 putPaddrHigh(dmabuf->phys);
19257 if (written + SLI4_PAGE_SIZE >= size) {
19258 wr_object->u.request.bde[i].tus.f.bdeSize =
19259 (size - written);
19260 written += (size - written);
19261 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
19262 } else {
19263 wr_object->u.request.bde[i].tus.f.bdeSize =
19264 SLI4_PAGE_SIZE;
19265 written += SLI4_PAGE_SIZE;
19266 }
19267 i++;
19268 }
19269 wr_object->u.request.bde_count = i;
19270 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19271 if (!phba->sli4_hba.intr_enable)
19272 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19273 else {
a183a15f 19274 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
19275 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19276 }
19277 /* The IOCTL status is embedded in the mailbox subheader. */
19278 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
19279 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19280 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19281 if (rc != MBX_TIMEOUT)
19282 mempool_free(mbox, phba->mbox_mem_pool);
19283 if (shdr_status || shdr_add_status || rc) {
19284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19285 "3025 Write Object mailbox failed with "
19286 "status x%x add_status x%x, mbx status x%x\n",
19287 shdr_status, shdr_add_status, rc);
19288 rc = -ENXIO;
1feb8204 19289 *offset = shdr_add_status;
52d52440
JS
19290 } else
19291 *offset += wr_object->u.response.actual_write_length;
19292 return rc;
19293}
19294
695a814e
JS
19295/**
19296 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19297 * @vport: pointer to vport data structure.
19298 *
19299 * This function iterate through the mailboxq and clean up all REG_LOGIN
19300 * and REG_VPI mailbox commands associated with the vport. This function
19301 * is called when driver want to restart discovery of the vport due to
19302 * a Clear Virtual Link event.
19303 **/
19304void
19305lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19306{
19307 struct lpfc_hba *phba = vport->phba;
19308 LPFC_MBOXQ_t *mb, *nextmb;
19309 struct lpfc_dmabuf *mp;
78730cfe 19310 struct lpfc_nodelist *ndlp;
d439d286 19311 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 19312 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 19313 LIST_HEAD(mbox_cmd_list);
63e801ce 19314 uint8_t restart_loop;
695a814e 19315
d439d286 19316 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
19317 spin_lock_irq(&phba->hbalock);
19318 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19319 if (mb->vport != vport)
19320 continue;
19321
19322 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19323 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19324 continue;
19325
d439d286
JS
19326 list_del(&mb->list);
19327 list_add_tail(&mb->list, &mbox_cmd_list);
19328 }
19329 /* Clean up active mailbox command with the vport */
19330 mb = phba->sli.mbox_active;
19331 if (mb && (mb->vport == vport)) {
19332 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19333 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19334 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19335 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
3e1f0718 19336 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
d439d286
JS
19337 /* Put reference count for delayed processing */
19338 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19339 /* Unregister the RPI when mailbox complete */
19340 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19341 }
19342 }
63e801ce
JS
19343 /* Cleanup any mailbox completions which are not yet processed */
19344 do {
19345 restart_loop = 0;
19346 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19347 /*
19348 * If this mailox is already processed or it is
19349 * for another vport ignore it.
19350 */
19351 if ((mb->vport != vport) ||
19352 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19353 continue;
19354
19355 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19356 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19357 continue;
19358
19359 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19360 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
3e1f0718 19361 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
63e801ce
JS
19362 /* Unregister the RPI when mailbox complete */
19363 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19364 restart_loop = 1;
19365 spin_unlock_irq(&phba->hbalock);
19366 spin_lock(shost->host_lock);
19367 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19368 spin_unlock(shost->host_lock);
19369 spin_lock_irq(&phba->hbalock);
19370 break;
19371 }
19372 }
19373 } while (restart_loop);
19374
d439d286
JS
19375 spin_unlock_irq(&phba->hbalock);
19376
19377 /* Release the cleaned-up mailbox commands */
19378 while (!list_empty(&mbox_cmd_list)) {
19379 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e 19380 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
3e1f0718 19381 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
695a814e
JS
19382 if (mp) {
19383 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19384 kfree(mp);
19385 }
3e1f0718
JS
19386 mb->ctx_buf = NULL;
19387 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19388 mb->ctx_ndlp = NULL;
78730cfe 19389 if (ndlp) {
ec21b3b0 19390 spin_lock(shost->host_lock);
589a52d6 19391 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 19392 spin_unlock(shost->host_lock);
78730cfe 19393 lpfc_nlp_put(ndlp);
78730cfe 19394 }
695a814e 19395 }
695a814e
JS
19396 mempool_free(mb, phba->mbox_mem_pool);
19397 }
d439d286
JS
19398
19399 /* Release the ndlp with the cleaned-up active mailbox command */
19400 if (act_mbx_ndlp) {
19401 spin_lock(shost->host_lock);
19402 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19403 spin_unlock(shost->host_lock);
19404 lpfc_nlp_put(act_mbx_ndlp);
695a814e 19405 }
695a814e
JS
19406}
19407
2a9bf3d0
JS
19408/**
19409 * lpfc_drain_txq - Drain the txq
19410 * @phba: Pointer to HBA context object.
19411 *
19412 * This function attempt to submit IOCBs on the txq
19413 * to the adapter. For SLI4 adapters, the txq contains
19414 * ELS IOCBs that have been deferred because the there
19415 * are no SGLs. This congestion can occur with large
19416 * vport counts during node discovery.
19417 **/
19418
19419uint32_t
19420lpfc_drain_txq(struct lpfc_hba *phba)
19421{
19422 LIST_HEAD(completions);
895427bd 19423 struct lpfc_sli_ring *pring;
2e706377 19424 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
19425 unsigned long iflags = 0;
19426 char *fail_msg = NULL;
19427 struct lpfc_sglq *sglq;
205e8240 19428 union lpfc_wqe128 wqe;
a2fc4aef 19429 uint32_t txq_cnt = 0;
dc19e3b4 19430 struct lpfc_queue *wq;
2a9bf3d0 19431
dc19e3b4
JS
19432 if (phba->link_flag & LS_MDS_LOOPBACK) {
19433 /* MDS WQE are posted only to first WQ*/
19434 wq = phba->sli4_hba.fcp_wq[0];
19435 if (unlikely(!wq))
19436 return 0;
19437 pring = wq->pring;
19438 } else {
19439 wq = phba->sli4_hba.els_wq;
19440 if (unlikely(!wq))
19441 return 0;
19442 pring = lpfc_phba_elsring(phba);
19443 }
19444
19445 if (unlikely(!pring) || list_empty(&pring->txq))
1234a6d5 19446 return 0;
895427bd 19447
398d81c9 19448 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
19449 list_for_each_entry(piocbq, &pring->txq, list) {
19450 txq_cnt++;
19451 }
19452
19453 if (txq_cnt > pring->txq_max)
19454 pring->txq_max = txq_cnt;
2a9bf3d0 19455
398d81c9 19456 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19457
0e9bb8d7 19458 while (!list_empty(&pring->txq)) {
398d81c9 19459 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 19460
19ca7609 19461 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 19462 if (!piocbq) {
398d81c9 19463 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
19464 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19465 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 19466 txq_cnt);
a629852a
JS
19467 break;
19468 }
895427bd 19469 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 19470 if (!sglq) {
19ca7609 19471 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 19472 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19473 break;
2a9bf3d0 19474 }
0e9bb8d7 19475 txq_cnt--;
2a9bf3d0
JS
19476
19477 /* The xri and iocb resources secured,
19478 * attempt to issue request
19479 */
6d368e53 19480 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
19481 piocbq->sli4_xritag = sglq->sli4_xritag;
19482 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19483 fail_msg = "to convert bpl to sgl";
205e8240 19484 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
2a9bf3d0 19485 fail_msg = "to convert iocb to wqe";
dc19e3b4 19486 else if (lpfc_sli4_wq_put(wq, &wqe))
2a9bf3d0
JS
19487 fail_msg = " - Wq is full";
19488 else
19489 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19490
19491 if (fail_msg) {
19492 /* Failed means we can't issue and need to cancel */
19493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19494 "2822 IOCB failed %s iotag 0x%x "
19495 "xri 0x%x\n",
19496 fail_msg,
19497 piocbq->iotag, piocbq->sli4_xritag);
19498 list_add_tail(&piocbq->list, &completions);
19499 }
398d81c9 19500 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
19501 }
19502
2a9bf3d0
JS
19503 /* Cancel all the IOCBs that cannot be issued */
19504 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19505 IOERR_SLI_ABORTED);
19506
0e9bb8d7 19507 return txq_cnt;
2a9bf3d0 19508}
895427bd
JS
19509
19510/**
19511 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19512 * @phba: Pointer to HBA context object.
19513 * @pwqe: Pointer to command WQE.
19514 * @sglq: Pointer to the scatter gather queue object.
19515 *
19516 * This routine converts the bpl or bde that is in the WQE
19517 * to a sgl list for the sli4 hardware. The physical address
19518 * of the bpl/bde is converted back to a virtual address.
19519 * If the WQE contains a BPL then the list of BDE's is
19520 * converted to sli4_sge's. If the WQE contains a single
19521 * BDE then it is converted to a single sli_sge.
19522 * The WQE is still in cpu endianness so the contents of
19523 * the bpl can be used without byte swapping.
19524 *
19525 * Returns valid XRI = Success, NO_XRI = Failure.
19526 */
19527static uint16_t
19528lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19529 struct lpfc_sglq *sglq)
19530{
19531 uint16_t xritag = NO_XRI;
19532 struct ulp_bde64 *bpl = NULL;
19533 struct ulp_bde64 bde;
19534 struct sli4_sge *sgl = NULL;
19535 struct lpfc_dmabuf *dmabuf;
205e8240 19536 union lpfc_wqe128 *wqe;
895427bd
JS
19537 int numBdes = 0;
19538 int i = 0;
19539 uint32_t offset = 0; /* accumulated offset in the sg request list */
19540 int inbound = 0; /* number of sg reply entries inbound from firmware */
19541 uint32_t cmd;
19542
19543 if (!pwqeq || !sglq)
19544 return xritag;
19545
19546 sgl = (struct sli4_sge *)sglq->sgl;
19547 wqe = &pwqeq->wqe;
19548 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19549
19550 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19551 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19552 return sglq->sli4_xritag;
19553 numBdes = pwqeq->rsvd2;
19554 if (numBdes) {
19555 /* The addrHigh and addrLow fields within the WQE
19556 * have not been byteswapped yet so there is no
19557 * need to swap them back.
19558 */
19559 if (pwqeq->context3)
19560 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19561 else
19562 return xritag;
19563
19564 bpl = (struct ulp_bde64 *)dmabuf->virt;
19565 if (!bpl)
19566 return xritag;
19567
19568 for (i = 0; i < numBdes; i++) {
19569 /* Should already be byte swapped. */
19570 sgl->addr_hi = bpl->addrHigh;
19571 sgl->addr_lo = bpl->addrLow;
19572
19573 sgl->word2 = le32_to_cpu(sgl->word2);
19574 if ((i+1) == numBdes)
19575 bf_set(lpfc_sli4_sge_last, sgl, 1);
19576 else
19577 bf_set(lpfc_sli4_sge_last, sgl, 0);
19578 /* swap the size field back to the cpu so we
19579 * can assign it to the sgl.
19580 */
19581 bde.tus.w = le32_to_cpu(bpl->tus.w);
19582 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19583 /* The offsets in the sgl need to be accumulated
19584 * separately for the request and reply lists.
19585 * The request is always first, the reply follows.
19586 */
19587 switch (cmd) {
19588 case CMD_GEN_REQUEST64_WQE:
19589 /* add up the reply sg entries */
19590 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19591 inbound++;
19592 /* first inbound? reset the offset */
19593 if (inbound == 1)
19594 offset = 0;
19595 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19596 bf_set(lpfc_sli4_sge_type, sgl,
19597 LPFC_SGE_TYPE_DATA);
19598 offset += bde.tus.f.bdeSize;
19599 break;
19600 case CMD_FCP_TRSP64_WQE:
19601 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19602 bf_set(lpfc_sli4_sge_type, sgl,
19603 LPFC_SGE_TYPE_DATA);
19604 break;
19605 case CMD_FCP_TSEND64_WQE:
19606 case CMD_FCP_TRECEIVE64_WQE:
19607 bf_set(lpfc_sli4_sge_type, sgl,
19608 bpl->tus.f.bdeFlags);
19609 if (i < 3)
19610 offset = 0;
19611 else
19612 offset += bde.tus.f.bdeSize;
19613 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19614 break;
19615 }
19616 sgl->word2 = cpu_to_le32(sgl->word2);
19617 bpl++;
19618 sgl++;
19619 }
19620 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19621 /* The addrHigh and addrLow fields of the BDE have not
19622 * been byteswapped yet so they need to be swapped
19623 * before putting them in the sgl.
19624 */
19625 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19626 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19627 sgl->word2 = le32_to_cpu(sgl->word2);
19628 bf_set(lpfc_sli4_sge_last, sgl, 1);
19629 sgl->word2 = cpu_to_le32(sgl->word2);
19630 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19631 }
19632 return sglq->sli4_xritag;
19633}
19634
19635/**
19636 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19637 * @phba: Pointer to HBA context object.
19638 * @ring_number: Base sli ring number
19639 * @pwqe: Pointer to command WQE.
19640 **/
19641int
19642lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19643 struct lpfc_iocbq *pwqe)
19644{
205e8240 19645 union lpfc_wqe128 *wqe = &pwqe->wqe;
f358dd0c 19646 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
19647 struct lpfc_queue *wq;
19648 struct lpfc_sglq *sglq;
19649 struct lpfc_sli_ring *pring;
19650 unsigned long iflags;
cd22d605 19651 uint32_t ret = 0;
895427bd
JS
19652
19653 /* NVME_LS and NVME_LS ABTS requests. */
19654 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19655 pring = phba->sli4_hba.nvmels_wq->pring;
19656 spin_lock_irqsave(&pring->ring_lock, iflags);
19657 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19658 if (!sglq) {
19659 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19660 return WQE_BUSY;
19661 }
19662 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19663 pwqe->sli4_xritag = sglq->sli4_xritag;
19664 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19665 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19666 return WQE_ERROR;
19667 }
19668 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19669 pwqe->sli4_xritag);
cd22d605
DK
19670 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19671 if (ret) {
895427bd 19672 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19673 return ret;
895427bd 19674 }
cd22d605 19675
895427bd
JS
19676 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19677 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19678 return 0;
19679 }
19680
19681 /* NVME_FCREQ and NVME_ABTS requests */
19682 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19683 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19684 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19685
19686 spin_lock_irqsave(&pring->ring_lock, iflags);
19687 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19688 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19689 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19690 ret = lpfc_sli4_wq_put(wq, wqe);
19691 if (ret) {
895427bd 19692 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19693 return ret;
895427bd
JS
19694 }
19695 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19696 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19697 return 0;
19698 }
19699
f358dd0c
JS
19700 /* NVMET requests */
19701 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19702 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19703 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19704
19705 spin_lock_irqsave(&pring->ring_lock, iflags);
19706 ctxp = pwqe->context2;
6c621a22 19707 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19708 if (pwqe->sli4_xritag == NO_XRI) {
19709 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19710 pwqe->sli4_xritag = sglq->sli4_xritag;
19711 }
19712 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19713 pwqe->sli4_xritag);
19714 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19715 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19716 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19717 ret = lpfc_sli4_wq_put(wq, wqe);
19718 if (ret) {
f358dd0c 19719 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19720 return ret;
f358dd0c
JS
19721 }
19722 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19723 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19724 return 0;
19725 }
895427bd
JS
19726 return WQE_ERROR;
19727}