scsi: lpfc: Fix up log messages and stats counters in IO submit code path
[linux-block.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
128bddac 4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
d080abe0 5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
50611577 6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
c44ce173 9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 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 22 *******************************************************************/
23
dea3101e 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 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 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 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 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;
4f774513 148
2e90f4b5
JS
149 /* sanity check on queue memory */
150 if (unlikely(!q))
151 return -ENOMEM;
152 temp_wqe = q->qe[q->host_index].wqe;
153
4f774513 154 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
155 idx = ((q->host_index + 1) % q->entry_count);
156 if (idx == q->hba_index) {
b84daac9 157 q->WQ_overflow++;
cd22d605 158 return -EBUSY;
b84daac9
JS
159 }
160 q->WQ_posted++;
4f774513 161 /* set consumption flag every once in a while */
ff78d8f9 162 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 163 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
04673e38
JS
164 else
165 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
fedd3b7b
JS
166 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
167 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
48f8fdb4 168 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
1351e69f
JS
169 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
170 /* write to DPP aperture taking advatage of Combined Writes */
4c06619f
JS
171 tmp = (uint8_t *)temp_wqe;
172#ifdef __raw_writeq
1351e69f 173 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
4c06619f
JS
174 __raw_writeq(*((uint64_t *)(tmp + i)),
175 q->dpp_regaddr + i);
176#else
177 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
178 __raw_writel(*((uint32_t *)(tmp + i)),
179 q->dpp_regaddr + i);
180#endif
1351e69f
JS
181 }
182 /* ensure WQE bcopy and DPP flushed before doorbell write */
6b3b3bdb 183 wmb();
4f774513
JS
184
185 /* Update the host index before invoking device */
186 host_index = q->host_index;
027140ea
JS
187
188 q->host_index = idx;
4f774513
JS
189
190 /* Ring Doorbell */
191 doorbell.word0 = 0;
962bc51b 192 if (q->db_format == LPFC_DB_LIST_FORMAT) {
1351e69f
JS
193 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
194 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
195 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
196 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
197 q->dpp_id);
198 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
199 q->queue_id);
200 } else {
201 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
202 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
203 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
204 }
962bc51b
JS
205 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
206 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
207 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
208 } else {
209 return -EINVAL;
210 }
211 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
212
213 return 0;
214}
215
216/**
217 * lpfc_sli4_wq_release - Updates internal hba index for WQ
218 * @q: The Work Queue to operate on.
219 * @index: The index to advance the hba index to.
220 *
221 * This routine will update the HBA index of a queue to reflect consumption of
222 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
223 * an entry the host calls this function to update the queue's internal
224 * pointers. This routine returns the number of entries that were consumed by
225 * the HBA.
226 **/
227static uint32_t
228lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
229{
230 uint32_t released = 0;
231
2e90f4b5
JS
232 /* sanity check on queue memory */
233 if (unlikely(!q))
234 return 0;
235
4f774513
JS
236 if (q->hba_index == index)
237 return 0;
238 do {
239 q->hba_index = ((q->hba_index + 1) % q->entry_count);
240 released++;
241 } while (q->hba_index != index);
242 return released;
243}
244
245/**
246 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
247 * @q: The Mailbox Queue to operate on.
248 * @wqe: The Mailbox Queue Entry to put on the Work queue.
249 *
250 * This routine will copy the contents of @mqe to the next available entry on
251 * the @q. This function will then ring the Work Queue Doorbell to signal the
252 * HBA to start processing the Work Queue Entry. This function returns 0 if
253 * successful. If no entries are available on @q then this function will return
254 * -ENOMEM.
255 * The caller is expected to hold the hbalock when calling this routine.
256 **/
257static uint32_t
258lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
259{
2e90f4b5 260 struct lpfc_mqe *temp_mqe;
4f774513 261 struct lpfc_register doorbell;
4f774513 262
2e90f4b5
JS
263 /* sanity check on queue memory */
264 if (unlikely(!q))
265 return -ENOMEM;
266 temp_mqe = q->qe[q->host_index].mqe;
267
4f774513
JS
268 /* If the host has not yet processed the next entry then we are done */
269 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
270 return -ENOMEM;
48f8fdb4 271 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
4f774513
JS
272 /* Save off the mailbox pointer for completion */
273 q->phba->mbox = (MAILBOX_t *)temp_mqe;
274
275 /* Update the host index before invoking device */
4f774513
JS
276 q->host_index = ((q->host_index + 1) % q->entry_count);
277
278 /* Ring Doorbell */
279 doorbell.word0 = 0;
280 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
281 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
282 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
283 return 0;
284}
285
286/**
287 * lpfc_sli4_mq_release - Updates internal hba index for MQ
288 * @q: The Mailbox Queue to operate on.
289 *
290 * This routine will update the HBA index of a queue to reflect consumption of
291 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
292 * an entry the host calls this function to update the queue's internal
293 * pointers. This routine returns the number of entries that were consumed by
294 * the HBA.
295 **/
296static uint32_t
297lpfc_sli4_mq_release(struct lpfc_queue *q)
298{
2e90f4b5
JS
299 /* sanity check on queue memory */
300 if (unlikely(!q))
301 return 0;
302
4f774513
JS
303 /* Clear the mailbox pointer for completion */
304 q->phba->mbox = NULL;
305 q->hba_index = ((q->hba_index + 1) % q->entry_count);
306 return 1;
307}
308
309/**
310 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
311 * @q: The Event Queue to get the first valid EQE from
312 *
313 * This routine will get the first valid Event Queue Entry from @q, update
314 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
315 * the Queue (no more work to do), or the Queue is full of EQEs that have been
316 * processed, but not popped back to the HBA then this routine will return NULL.
317 **/
318static struct lpfc_eqe *
319lpfc_sli4_eq_get(struct lpfc_queue *q)
320{
7365f6fd 321 struct lpfc_hba *phba;
2e90f4b5 322 struct lpfc_eqe *eqe;
027140ea 323 uint32_t idx;
2e90f4b5
JS
324
325 /* sanity check on queue memory */
326 if (unlikely(!q))
327 return NULL;
7365f6fd 328 phba = q->phba;
2e90f4b5 329 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
330
331 /* If the next EQE is not valid then we are done */
7365f6fd 332 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
4f774513
JS
333 return NULL;
334 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
335 idx = ((q->hba_index + 1) % q->entry_count);
336 if (idx == q->host_index)
4f774513
JS
337 return NULL;
338
027140ea 339 q->hba_index = idx;
7365f6fd
JS
340 /* if the index wrapped around, toggle the valid bit */
341 if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
342 q->qe_valid = (q->qe_valid) ? 0 : 1;
343
27f344eb
JS
344
345 /*
346 * insert barrier for instruction interlock : data from the hardware
347 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
348 * upon. Speculative instructions were allowing a bcopy at the start
349 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
350 * after our return, to copy data before the valid bit check above
351 * was done. As such, some of the copied data was stale. The barrier
352 * ensures the check is before any data is copied.
27f344eb
JS
353 */
354 mb();
4f774513
JS
355 return eqe;
356}
357
ba20c853
JS
358/**
359 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
360 * @q: The Event Queue to disable interrupts
361 *
362 **/
b71413dd 363inline void
ba20c853
JS
364lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
365{
366 struct lpfc_register doorbell;
367
368 doorbell.word0 = 0;
369 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
370 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
371 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
372 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
373 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 374 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
ba20c853
JS
375}
376
27d6ac0a
JS
377/**
378 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
379 * @q: The Event Queue to disable interrupts
380 *
381 **/
382inline void
383lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
384{
385 struct lpfc_register doorbell;
386
387 doorbell.word0 = 0;
388 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
389 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
390 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
391 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
392 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
393 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
394}
395
4f774513
JS
396/**
397 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
398 * @q: The Event Queue that the host has completed processing for.
399 * @arm: Indicates whether the host wants to arms this CQ.
400 *
401 * This routine will mark all Event Queue Entries on @q, from the last
402 * known completed entry to the last entry that was processed, as completed
403 * by clearing the valid bit for each completion queue entry. Then it will
404 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
405 * The internal host index in the @q will be updated by this routine to indicate
406 * that the host has finished processing the entries. The @arm parameter
407 * indicates that the queue should be rearmed when ringing the doorbell.
408 *
409 * This function will return the number of EQEs that were popped.
410 **/
411uint32_t
412lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
413{
414 uint32_t released = 0;
7365f6fd 415 struct lpfc_hba *phba;
4f774513
JS
416 struct lpfc_eqe *temp_eqe;
417 struct lpfc_register doorbell;
418
2e90f4b5
JS
419 /* sanity check on queue memory */
420 if (unlikely(!q))
421 return 0;
7365f6fd 422 phba = q->phba;
2e90f4b5 423
4f774513
JS
424 /* while there are valid entries */
425 while (q->hba_index != q->host_index) {
7365f6fd
JS
426 if (!phba->sli4_hba.pc_sli4_params.eqav) {
427 temp_eqe = q->qe[q->host_index].eqe;
428 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
429 }
4f774513
JS
430 released++;
431 q->host_index = ((q->host_index + 1) % q->entry_count);
432 }
433 if (unlikely(released == 0 && !arm))
434 return 0;
435
436 /* ring doorbell for number popped */
437 doorbell.word0 = 0;
438 if (arm) {
439 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
440 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
441 }
442 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
443 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
444 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
445 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
446 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 447 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
a747c9ce
JS
448 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
449 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
9dd35425 450 readl(q->phba->sli4_hba.EQDBregaddr);
4f774513
JS
451 return released;
452}
453
27d6ac0a
JS
454/**
455 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
456 * @q: The Event Queue that the host has completed processing for.
457 * @arm: Indicates whether the host wants to arms this CQ.
458 *
459 * This routine will mark all Event Queue Entries on @q, from the last
460 * known completed entry to the last entry that was processed, as completed
461 * by clearing the valid bit for each completion queue entry. Then it will
462 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
463 * The internal host index in the @q will be updated by this routine to indicate
464 * that the host has finished processing the entries. The @arm parameter
465 * indicates that the queue should be rearmed when ringing the doorbell.
466 *
467 * This function will return the number of EQEs that were popped.
468 **/
469uint32_t
470lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
471{
472 uint32_t released = 0;
7365f6fd 473 struct lpfc_hba *phba;
27d6ac0a
JS
474 struct lpfc_eqe *temp_eqe;
475 struct lpfc_register doorbell;
476
477 /* sanity check on queue memory */
478 if (unlikely(!q))
479 return 0;
7365f6fd 480 phba = q->phba;
27d6ac0a
JS
481
482 /* while there are valid entries */
483 while (q->hba_index != q->host_index) {
7365f6fd
JS
484 if (!phba->sli4_hba.pc_sli4_params.eqav) {
485 temp_eqe = q->qe[q->host_index].eqe;
486 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
487 }
27d6ac0a
JS
488 released++;
489 q->host_index = ((q->host_index + 1) % q->entry_count);
490 }
491 if (unlikely(released == 0 && !arm))
492 return 0;
493
494 /* ring doorbell for number popped */
495 doorbell.word0 = 0;
496 if (arm)
497 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
498 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
499 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
500 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
501 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
502 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
503 readl(q->phba->sli4_hba.EQDBregaddr);
504 return released;
505}
506
4f774513
JS
507/**
508 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
509 * @q: The Completion Queue to get the first valid CQE from
510 *
511 * This routine will get the first valid Completion Queue Entry from @q, update
512 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
513 * the Queue (no more work to do), or the Queue is full of CQEs that have been
514 * processed, but not popped back to the HBA then this routine will return NULL.
515 **/
516static struct lpfc_cqe *
517lpfc_sli4_cq_get(struct lpfc_queue *q)
518{
7365f6fd 519 struct lpfc_hba *phba;
4f774513 520 struct lpfc_cqe *cqe;
027140ea 521 uint32_t idx;
4f774513 522
2e90f4b5
JS
523 /* sanity check on queue memory */
524 if (unlikely(!q))
525 return NULL;
7365f6fd
JS
526 phba = q->phba;
527 cqe = q->qe[q->hba_index].cqe;
2e90f4b5 528
4f774513 529 /* If the next CQE is not valid then we are done */
7365f6fd 530 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
4f774513
JS
531 return NULL;
532 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
533 idx = ((q->hba_index + 1) % q->entry_count);
534 if (idx == q->host_index)
4f774513
JS
535 return NULL;
536
027140ea 537 q->hba_index = idx;
7365f6fd
JS
538 /* if the index wrapped around, toggle the valid bit */
539 if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
540 q->qe_valid = (q->qe_valid) ? 0 : 1;
27f344eb
JS
541
542 /*
543 * insert barrier for instruction interlock : data from the hardware
544 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
545 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
546 * instructions allowing action on content before valid bit checked,
547 * add barrier here as well. May not be needed as "content" is a
548 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
549 */
550 mb();
4f774513
JS
551 return cqe;
552}
553
554/**
555 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
556 * @q: The Completion Queue that the host has completed processing for.
557 * @arm: Indicates whether the host wants to arms this CQ.
558 *
559 * This routine will mark all Completion queue entries on @q, from the last
560 * known completed entry to the last entry that was processed, as completed
561 * by clearing the valid bit for each completion queue entry. Then it will
562 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
563 * The internal host index in the @q will be updated by this routine to indicate
564 * that the host has finished processing the entries. The @arm parameter
565 * indicates that the queue should be rearmed when ringing the doorbell.
566 *
567 * This function will return the number of CQEs that were released.
568 **/
569uint32_t
570lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
571{
572 uint32_t released = 0;
7365f6fd 573 struct lpfc_hba *phba;
4f774513
JS
574 struct lpfc_cqe *temp_qe;
575 struct lpfc_register doorbell;
576
2e90f4b5
JS
577 /* sanity check on queue memory */
578 if (unlikely(!q))
579 return 0;
7365f6fd
JS
580 phba = q->phba;
581
4f774513
JS
582 /* while there are valid entries */
583 while (q->hba_index != q->host_index) {
7365f6fd
JS
584 if (!phba->sli4_hba.pc_sli4_params.cqav) {
585 temp_qe = q->qe[q->host_index].cqe;
586 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
587 }
4f774513
JS
588 released++;
589 q->host_index = ((q->host_index + 1) % q->entry_count);
590 }
591 if (unlikely(released == 0 && !arm))
592 return 0;
593
594 /* ring doorbell for number popped */
595 doorbell.word0 = 0;
596 if (arm)
597 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
598 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
599 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
600 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
601 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
602 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
9dd35425 603 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
4f774513
JS
604 return released;
605}
606
27d6ac0a
JS
607/**
608 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
609 * @q: The Completion Queue that the host has completed processing for.
610 * @arm: Indicates whether the host wants to arms this CQ.
611 *
612 * This routine will mark all Completion queue entries on @q, from the last
613 * known completed entry to the last entry that was processed, as completed
614 * by clearing the valid bit for each completion queue entry. Then it will
615 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
616 * The internal host index in the @q will be updated by this routine to indicate
617 * that the host has finished processing the entries. The @arm parameter
618 * indicates that the queue should be rearmed when ringing the doorbell.
619 *
620 * This function will return the number of CQEs that were released.
621 **/
622uint32_t
623lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
624{
625 uint32_t released = 0;
7365f6fd 626 struct lpfc_hba *phba;
27d6ac0a
JS
627 struct lpfc_cqe *temp_qe;
628 struct lpfc_register doorbell;
629
630 /* sanity check on queue memory */
631 if (unlikely(!q))
632 return 0;
7365f6fd
JS
633 phba = q->phba;
634
27d6ac0a
JS
635 /* while there are valid entries */
636 while (q->hba_index != q->host_index) {
7365f6fd
JS
637 if (!phba->sli4_hba.pc_sli4_params.cqav) {
638 temp_qe = q->qe[q->host_index].cqe;
639 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
640 }
27d6ac0a
JS
641 released++;
642 q->host_index = ((q->host_index + 1) % q->entry_count);
643 }
644 if (unlikely(released == 0 && !arm))
645 return 0;
646
647 /* ring doorbell for number popped */
648 doorbell.word0 = 0;
649 if (arm)
650 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
651 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
652 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
653 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
654 return released;
655}
656
4f774513
JS
657/**
658 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
659 * @q: The Header Receive Queue to operate on.
660 * @wqe: The Receive Queue Entry to put on the Receive queue.
661 *
662 * This routine will copy the contents of @wqe to the next available entry on
663 * the @q. This function will then ring the Receive Queue Doorbell to signal the
664 * HBA to start processing the Receive Queue Entry. This function returns the
665 * index that the rqe was copied to if successful. If no entries are available
666 * on @q then this function will return -ENOMEM.
667 * The caller is expected to hold the hbalock when calling this routine.
668 **/
895427bd 669int
4f774513
JS
670lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
671 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
672{
2e90f4b5
JS
673 struct lpfc_rqe *temp_hrqe;
674 struct lpfc_rqe *temp_drqe;
4f774513 675 struct lpfc_register doorbell;
cbc5de1b
JS
676 int hq_put_index;
677 int dq_put_index;
4f774513 678
2e90f4b5
JS
679 /* sanity check on queue memory */
680 if (unlikely(!hq) || unlikely(!dq))
681 return -ENOMEM;
cbc5de1b
JS
682 hq_put_index = hq->host_index;
683 dq_put_index = dq->host_index;
684 temp_hrqe = hq->qe[hq_put_index].rqe;
685 temp_drqe = dq->qe[dq_put_index].rqe;
2e90f4b5 686
4f774513
JS
687 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
688 return -EINVAL;
cbc5de1b 689 if (hq_put_index != dq_put_index)
4f774513
JS
690 return -EINVAL;
691 /* If the host has not yet processed the next entry then we are done */
cbc5de1b 692 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513 693 return -EBUSY;
48f8fdb4
JS
694 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
695 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
4f774513
JS
696
697 /* Update the host index to point to the next slot */
cbc5de1b
JS
698 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
699 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
61f3d4bf 700 hq->RQ_buf_posted++;
4f774513
JS
701
702 /* Ring The Header Receive Queue Doorbell */
73d91e50 703 if (!(hq->host_index % hq->entry_repost)) {
4f774513 704 doorbell.word0 = 0;
962bc51b
JS
705 if (hq->db_format == LPFC_DB_RING_FORMAT) {
706 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
707 hq->entry_repost);
708 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
709 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
710 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
711 hq->entry_repost);
712 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
713 hq->host_index);
714 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
715 } else {
716 return -EINVAL;
717 }
718 writel(doorbell.word0, hq->db_regaddr);
4f774513 719 }
cbc5de1b 720 return hq_put_index;
4f774513
JS
721}
722
723/**
724 * lpfc_sli4_rq_release - Updates internal hba index for RQ
725 * @q: The Header Receive Queue to operate on.
726 *
727 * This routine will update the HBA index of a queue to reflect consumption of
728 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
729 * consumed an entry the host calls this function to update the queue's
730 * internal pointers. This routine returns the number of entries that were
731 * consumed by the HBA.
732 **/
733static uint32_t
734lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
735{
2e90f4b5
JS
736 /* sanity check on queue memory */
737 if (unlikely(!hq) || unlikely(!dq))
738 return 0;
739
4f774513
JS
740 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
741 return 0;
742 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
743 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
744 return 1;
745}
746
e59058c4 747/**
3621a710 748 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
749 * @phba: Pointer to HBA context object.
750 * @pring: Pointer to driver SLI ring object.
751 *
752 * This function returns pointer to next command iocb entry
753 * in the command ring. The caller must hold hbalock to prevent
754 * other threads consume the next command iocb.
755 * SLI-2/SLI-3 provide different sized iocbs.
756 **/
ed957684
JS
757static inline IOCB_t *
758lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
759{
7e56aa25
JS
760 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
761 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
762}
763
e59058c4 764/**
3621a710 765 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
766 * @phba: Pointer to HBA context object.
767 * @pring: Pointer to driver SLI ring object.
768 *
769 * This function returns pointer to next response iocb entry
770 * in the response ring. The caller must hold hbalock to make sure
771 * that no other thread consume the next response iocb.
772 * SLI-2/SLI-3 provide different sized iocbs.
773 **/
ed957684
JS
774static inline IOCB_t *
775lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
776{
7e56aa25
JS
777 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
778 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
779}
780
e59058c4 781/**
3621a710 782 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
783 * @phba: Pointer to HBA context object.
784 *
785 * This function is called with hbalock held. This function
786 * allocates a new driver iocb object from the iocb pool. If the
787 * allocation is successful, it returns pointer to the newly
788 * allocated iocb object else it returns NULL.
789 **/
4f2e66c6 790struct lpfc_iocbq *
2e0fef85 791__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
792{
793 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
794 struct lpfc_iocbq * iocbq = NULL;
795
1c2ba475
JT
796 lockdep_assert_held(&phba->hbalock);
797
0bd4ca25 798 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
799 if (iocbq)
800 phba->iocb_cnt++;
801 if (phba->iocb_cnt > phba->iocb_max)
802 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
803 return iocbq;
804}
805
da0436e9
JS
806/**
807 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
808 * @phba: Pointer to HBA context object.
809 * @xritag: XRI value.
810 *
811 * This function clears the sglq pointer from the array of acive
812 * sglq's. The xritag that is passed in is used to index into the
813 * array. Before the xritag can be used it needs to be adjusted
814 * by subtracting the xribase.
815 *
816 * Returns sglq ponter = success, NULL = Failure.
817 **/
895427bd 818struct lpfc_sglq *
da0436e9
JS
819__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
820{
da0436e9 821 struct lpfc_sglq *sglq;
6d368e53
JS
822
823 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
824 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
825 return sglq;
826}
827
828/**
829 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
830 * @phba: Pointer to HBA context object.
831 * @xritag: XRI value.
832 *
833 * This function returns the sglq pointer from the array of acive
834 * sglq's. The xritag that is passed in is used to index into the
835 * array. Before the xritag can be used it needs to be adjusted
836 * by subtracting the xribase.
837 *
838 * Returns sglq ponter = success, NULL = Failure.
839 **/
0f65ff68 840struct lpfc_sglq *
da0436e9
JS
841__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
842{
da0436e9 843 struct lpfc_sglq *sglq;
6d368e53
JS
844
845 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
846 return sglq;
847}
848
19ca7609 849/**
1151e3ec 850 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
851 * @phba: Pointer to HBA context object.
852 * @xritag: xri used in this exchange.
853 * @rrq: The RRQ to be cleared.
854 *
19ca7609 855 **/
1151e3ec
JS
856void
857lpfc_clr_rrq_active(struct lpfc_hba *phba,
858 uint16_t xritag,
859 struct lpfc_node_rrq *rrq)
19ca7609 860{
1151e3ec 861 struct lpfc_nodelist *ndlp = NULL;
19ca7609 862
1151e3ec
JS
863 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
864 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
865
866 /* The target DID could have been swapped (cable swap)
867 * we should use the ndlp from the findnode if it is
868 * available.
869 */
1151e3ec 870 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
871 ndlp = rrq->ndlp;
872
1151e3ec
JS
873 if (!ndlp)
874 goto out;
875
cff261f6 876 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
877 rrq->send_rrq = 0;
878 rrq->xritag = 0;
879 rrq->rrq_stop_time = 0;
880 }
1151e3ec 881out:
19ca7609
JS
882 mempool_free(rrq, phba->rrq_pool);
883}
884
885/**
886 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
887 * @phba: Pointer to HBA context object.
888 *
889 * This function is called with hbalock held. This function
890 * Checks if stop_time (ratov from setting rrq active) has
891 * been reached, if it has and the send_rrq flag is set then
892 * it will call lpfc_send_rrq. If the send_rrq flag is not set
893 * then it will just call the routine to clear the rrq and
894 * free the rrq resource.
895 * The timer is set to the next rrq that is going to expire before
896 * leaving the routine.
897 *
898 **/
899void
900lpfc_handle_rrq_active(struct lpfc_hba *phba)
901{
902 struct lpfc_node_rrq *rrq;
903 struct lpfc_node_rrq *nextrrq;
904 unsigned long next_time;
905 unsigned long iflags;
1151e3ec 906 LIST_HEAD(send_rrq);
19ca7609
JS
907
908 spin_lock_irqsave(&phba->hbalock, iflags);
909 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 910 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 911 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
912 &phba->active_rrq_list, list) {
913 if (time_after(jiffies, rrq->rrq_stop_time))
914 list_move(&rrq->list, &send_rrq);
915 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
916 next_time = rrq->rrq_stop_time;
917 }
918 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
919 if ((!list_empty(&phba->active_rrq_list)) &&
920 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 921 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
922 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
923 list_del(&rrq->list);
924 if (!rrq->send_rrq)
925 /* this call will free the rrq */
926 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
927 else if (lpfc_send_rrq(phba, rrq)) {
928 /* if we send the rrq then the completion handler
929 * will clear the bit in the xribitmap.
930 */
931 lpfc_clr_rrq_active(phba, rrq->xritag,
932 rrq);
933 }
934 }
19ca7609
JS
935}
936
937/**
938 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
939 * @vport: Pointer to vport context object.
940 * @xri: The xri used in the exchange.
941 * @did: The targets DID for this exchange.
942 *
943 * returns NULL = rrq not found in the phba->active_rrq_list.
944 * rrq = rrq for this xri and target.
945 **/
946struct lpfc_node_rrq *
947lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
948{
949 struct lpfc_hba *phba = vport->phba;
950 struct lpfc_node_rrq *rrq;
951 struct lpfc_node_rrq *nextrrq;
952 unsigned long iflags;
953
954 if (phba->sli_rev != LPFC_SLI_REV4)
955 return NULL;
956 spin_lock_irqsave(&phba->hbalock, iflags);
957 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
958 if (rrq->vport == vport && rrq->xritag == xri &&
959 rrq->nlp_DID == did){
960 list_del(&rrq->list);
961 spin_unlock_irqrestore(&phba->hbalock, iflags);
962 return rrq;
963 }
964 }
965 spin_unlock_irqrestore(&phba->hbalock, iflags);
966 return NULL;
967}
968
969/**
970 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
971 * @vport: Pointer to vport context object.
1151e3ec
JS
972 * @ndlp: Pointer to the lpfc_node_list structure.
973 * If ndlp is NULL Remove all active RRQs for this vport from the
974 * phba->active_rrq_list and clear the rrq.
975 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
976 **/
977void
1151e3ec 978lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
979
980{
981 struct lpfc_hba *phba = vport->phba;
982 struct lpfc_node_rrq *rrq;
983 struct lpfc_node_rrq *nextrrq;
984 unsigned long iflags;
1151e3ec 985 LIST_HEAD(rrq_list);
19ca7609
JS
986
987 if (phba->sli_rev != LPFC_SLI_REV4)
988 return;
1151e3ec
JS
989 if (!ndlp) {
990 lpfc_sli4_vport_delete_els_xri_aborted(vport);
991 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 992 }
1151e3ec
JS
993 spin_lock_irqsave(&phba->hbalock, iflags);
994 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
995 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
996 list_move(&rrq->list, &rrq_list);
19ca7609 997 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
998
999 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1000 list_del(&rrq->list);
1001 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1002 }
19ca7609
JS
1003}
1004
19ca7609 1005/**
1151e3ec 1006 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
1007 * @phba: Pointer to HBA context object.
1008 * @ndlp: Targets nodelist pointer for this exchange.
1009 * @xritag the xri in the bitmap to test.
1010 *
1011 * This function is called with hbalock held. This function
1012 * returns 0 = rrq not active for this xri
1013 * 1 = rrq is valid for this xri.
1014 **/
1151e3ec
JS
1015int
1016lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
1017 uint16_t xritag)
1018{
1c2ba475 1019 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
1020 if (!ndlp)
1021 return 0;
cff261f6
JS
1022 if (!ndlp->active_rrqs_xri_bitmap)
1023 return 0;
1024 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
1025 return 1;
1026 else
1027 return 0;
1028}
1029
1030/**
1031 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1032 * @phba: Pointer to HBA context object.
1033 * @ndlp: nodelist pointer for this target.
1034 * @xritag: xri used in this exchange.
1035 * @rxid: Remote Exchange ID.
1036 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1037 *
1038 * This function takes the hbalock.
1039 * The active bit is always set in the active rrq xri_bitmap even
1040 * if there is no slot avaiable for the other rrq information.
1041 *
1042 * returns 0 rrq actived for this xri
1043 * < 0 No memory or invalid ndlp.
1044 **/
1045int
1046lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 1047 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 1048{
19ca7609 1049 unsigned long iflags;
b42c07c8
JS
1050 struct lpfc_node_rrq *rrq;
1051 int empty;
1052
1053 if (!ndlp)
1054 return -EINVAL;
1055
1056 if (!phba->cfg_enable_rrq)
1057 return -EINVAL;
19ca7609
JS
1058
1059 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
1060 if (phba->pport->load_flag & FC_UNLOADING) {
1061 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1062 goto out;
1063 }
1064
1065 /*
1066 * set the active bit even if there is no mem available.
1067 */
1068 if (NLP_CHK_FREE_REQ(ndlp))
1069 goto out;
1070
1071 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1072 goto out;
1073
cff261f6
JS
1074 if (!ndlp->active_rrqs_xri_bitmap)
1075 goto out;
1076
1077 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
1078 goto out;
1079
19ca7609 1080 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
1081 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1082 if (!rrq) {
1083 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1084 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1085 " DID:0x%x Send:%d\n",
1086 xritag, rxid, ndlp->nlp_DID, send_rrq);
1087 return -EINVAL;
1088 }
e5771b4d
JS
1089 if (phba->cfg_enable_rrq == 1)
1090 rrq->send_rrq = send_rrq;
1091 else
1092 rrq->send_rrq = 0;
b42c07c8 1093 rrq->xritag = xritag;
256ec0d0
JS
1094 rrq->rrq_stop_time = jiffies +
1095 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
1096 rrq->ndlp = ndlp;
1097 rrq->nlp_DID = ndlp->nlp_DID;
1098 rrq->vport = ndlp->vport;
1099 rrq->rxid = rxid;
b42c07c8
JS
1100 spin_lock_irqsave(&phba->hbalock, iflags);
1101 empty = list_empty(&phba->active_rrq_list);
1102 list_add_tail(&rrq->list, &phba->active_rrq_list);
1103 phba->hba_flag |= HBA_RRQ_ACTIVE;
1104 if (empty)
1105 lpfc_worker_wake_up(phba);
1106 spin_unlock_irqrestore(&phba->hbalock, iflags);
1107 return 0;
1108out:
1109 spin_unlock_irqrestore(&phba->hbalock, iflags);
1110 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1111 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1112 " DID:0x%x Send:%d\n",
1113 xritag, rxid, ndlp->nlp_DID, send_rrq);
1114 return -EINVAL;
19ca7609
JS
1115}
1116
da0436e9 1117/**
895427bd 1118 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 1119 * @phba: Pointer to HBA context object.
19ca7609 1120 * @piocb: Pointer to the iocbq.
da0436e9 1121 *
dafe8cea 1122 * This function is called with the ring lock held. This function
6d368e53 1123 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
1124 * list is not empty then it is successful, it returns pointer to the newly
1125 * allocated sglq object else it returns NULL.
1126 **/
1127static struct lpfc_sglq *
895427bd 1128__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 1129{
895427bd 1130 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 1131 struct lpfc_sglq *sglq = NULL;
19ca7609 1132 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
1133 struct lpfc_scsi_buf *lpfc_cmd;
1134 struct lpfc_nodelist *ndlp;
1135 int found = 0;
1136
1c2ba475
JT
1137 lockdep_assert_held(&phba->hbalock);
1138
19ca7609
JS
1139 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1140 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1141 ndlp = lpfc_cmd->rdata->pnode;
be858b65 1142 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 1143 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 1144 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
1145 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1146 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1147 ndlp = NULL;
1148 else
1149 ndlp = piocbq->context_un.ndlp;
1150 } else {
19ca7609 1151 ndlp = piocbq->context1;
6c7cf486 1152 }
19ca7609 1153
895427bd
JS
1154 spin_lock(&phba->sli4_hba.sgl_list_lock);
1155 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
1156 start_sglq = sglq;
1157 while (!found) {
1158 if (!sglq)
d11f54b7 1159 break;
895427bd
JS
1160 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1161 test_bit(sglq->sli4_lxritag,
1162 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
1163 /* This xri has an rrq outstanding for this DID.
1164 * put it back in the list and get another xri.
1165 */
895427bd 1166 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 1167 sglq = NULL;
895427bd 1168 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
1169 struct lpfc_sglq, list);
1170 if (sglq == start_sglq) {
14041bd1 1171 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
1172 sglq = NULL;
1173 break;
1174 } else
1175 continue;
1176 }
1177 sglq->ndlp = ndlp;
1178 found = 1;
6d368e53 1179 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
1180 sglq->state = SGL_ALLOCATED;
1181 }
895427bd 1182 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
1183 return sglq;
1184}
1185
f358dd0c
JS
1186/**
1187 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1188 * @phba: Pointer to HBA context object.
1189 * @piocb: Pointer to the iocbq.
1190 *
1191 * This function is called with the sgl_list lock held. This function
1192 * gets a new driver sglq object from the sglq list. If the
1193 * list is not empty then it is successful, it returns pointer to the newly
1194 * allocated sglq object else it returns NULL.
1195 **/
1196struct lpfc_sglq *
1197__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1198{
1199 struct list_head *lpfc_nvmet_sgl_list;
1200 struct lpfc_sglq *sglq = NULL;
1201
1202 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1203
1204 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1205
1206 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1207 if (!sglq)
1208 return NULL;
1209 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1210 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1211 return sglq;
1212}
1213
e59058c4 1214/**
3621a710 1215 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1216 * @phba: Pointer to HBA context object.
1217 *
1218 * This function is called with no lock held. This function
1219 * allocates a new driver iocb object from the iocb pool. If the
1220 * allocation is successful, it returns pointer to the newly
1221 * allocated iocb object else it returns NULL.
1222 **/
2e0fef85
JS
1223struct lpfc_iocbq *
1224lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1225{
1226 struct lpfc_iocbq * iocbq = NULL;
1227 unsigned long iflags;
1228
1229 spin_lock_irqsave(&phba->hbalock, iflags);
1230 iocbq = __lpfc_sli_get_iocbq(phba);
1231 spin_unlock_irqrestore(&phba->hbalock, iflags);
1232 return iocbq;
1233}
1234
4f774513
JS
1235/**
1236 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1237 * @phba: Pointer to HBA context object.
1238 * @iocbq: Pointer to driver iocb object.
1239 *
1240 * This function is called with hbalock held to release driver
1241 * iocb object to the iocb pool. The iotag in the iocb object
1242 * does not change for each use of the iocb object. This function
1243 * clears all other fields of the iocb object when it is freed.
1244 * The sqlq structure that holds the xritag and phys and virtual
1245 * mappings for the scatter gather list is retrieved from the
1246 * active array of sglq. The get of the sglq pointer also clears
1247 * the entry in the array. If the status of the IO indiactes that
1248 * this IO was aborted then the sglq entry it put on the
1249 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1250 * IO has good status or fails for any other reason then the sglq
895427bd 1251 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1252 **/
1253static void
1254__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1255{
1256 struct lpfc_sglq *sglq;
1257 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1258 unsigned long iflag = 0;
895427bd 1259 struct lpfc_sli_ring *pring;
4f774513 1260
1c2ba475
JT
1261 lockdep_assert_held(&phba->hbalock);
1262
4f774513
JS
1263 if (iocbq->sli4_xritag == NO_XRI)
1264 sglq = NULL;
1265 else
6d368e53
JS
1266 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1267
0e9bb8d7 1268
4f774513 1269 if (sglq) {
f358dd0c
JS
1270 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1271 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1272 iflag);
1273 sglq->state = SGL_FREED;
1274 sglq->ndlp = NULL;
1275 list_add_tail(&sglq->list,
1276 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1277 spin_unlock_irqrestore(
1278 &phba->sli4_hba.sgl_list_lock, iflag);
1279 goto out;
1280 }
1281
895427bd 1282 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1283 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1284 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1285 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1286 iflag);
4f774513 1287 list_add(&sglq->list,
895427bd 1288 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1289 spin_unlock_irqrestore(
895427bd 1290 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1291 } else {
895427bd
JS
1292 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1293 iflag);
0f65ff68 1294 sglq->state = SGL_FREED;
19ca7609 1295 sglq->ndlp = NULL;
fedd3b7b 1296 list_add_tail(&sglq->list,
895427bd
JS
1297 &phba->sli4_hba.lpfc_els_sgl_list);
1298 spin_unlock_irqrestore(
1299 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1300
1301 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1302 if (!list_empty(&pring->txq))
2a9bf3d0 1303 lpfc_worker_wake_up(phba);
0f65ff68 1304 }
4f774513
JS
1305 }
1306
f358dd0c 1307out:
4f774513
JS
1308 /*
1309 * Clean all volatile data fields, preserve iotag and node struct.
1310 */
1311 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1312 iocbq->sli4_lxritag = NO_XRI;
4f774513 1313 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1314 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1315 LPFC_IO_NVME_LS);
4f774513
JS
1316 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1317}
1318
2a9bf3d0 1319
e59058c4 1320/**
3772a991 1321 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1322 * @phba: Pointer to HBA context object.
1323 * @iocbq: Pointer to driver iocb object.
1324 *
1325 * This function is called with hbalock held to release driver
1326 * iocb object to the iocb pool. The iotag in the iocb object
1327 * does not change for each use of the iocb object. This function
1328 * clears all other fields of the iocb object when it is freed.
1329 **/
a6ababd2 1330static void
3772a991 1331__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1332{
2e0fef85 1333 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1334
1c2ba475 1335 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1336
604a3e30
JB
1337 /*
1338 * Clean all volatile data fields, preserve iotag and node struct.
1339 */
1340 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1341 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1342 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1343}
1344
3772a991
JS
1345/**
1346 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1347 * @phba: Pointer to HBA context object.
1348 * @iocbq: Pointer to driver iocb object.
1349 *
1350 * This function is called with hbalock held to release driver
1351 * iocb object to the iocb pool. The iotag in the iocb object
1352 * does not change for each use of the iocb object. This function
1353 * clears all other fields of the iocb object when it is freed.
1354 **/
1355static void
1356__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1357{
1c2ba475
JT
1358 lockdep_assert_held(&phba->hbalock);
1359
3772a991 1360 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1361 phba->iocb_cnt--;
3772a991
JS
1362}
1363
e59058c4 1364/**
3621a710 1365 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1366 * @phba: Pointer to HBA context object.
1367 * @iocbq: Pointer to driver iocb object.
1368 *
1369 * This function is called with no lock held to release the iocb to
1370 * iocb pool.
1371 **/
2e0fef85
JS
1372void
1373lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1374{
1375 unsigned long iflags;
1376
1377 /*
1378 * Clean all volatile data fields, preserve iotag and node struct.
1379 */
1380 spin_lock_irqsave(&phba->hbalock, iflags);
1381 __lpfc_sli_release_iocbq(phba, iocbq);
1382 spin_unlock_irqrestore(&phba->hbalock, iflags);
1383}
1384
a257bf90
JS
1385/**
1386 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1387 * @phba: Pointer to HBA context object.
1388 * @iocblist: List of IOCBs.
1389 * @ulpstatus: ULP status in IOCB command field.
1390 * @ulpWord4: ULP word-4 in IOCB command field.
1391 *
1392 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1393 * on the list by invoking the complete callback function associated with the
1394 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1395 * fields.
1396 **/
1397void
1398lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1399 uint32_t ulpstatus, uint32_t ulpWord4)
1400{
1401 struct lpfc_iocbq *piocb;
1402
1403 while (!list_empty(iocblist)) {
1404 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1405 if (!piocb->iocb_cmpl)
1406 lpfc_sli_release_iocbq(phba, piocb);
1407 else {
1408 piocb->iocb.ulpStatus = ulpstatus;
1409 piocb->iocb.un.ulpWord[4] = ulpWord4;
1410 (piocb->iocb_cmpl) (phba, piocb, piocb);
1411 }
1412 }
1413 return;
1414}
1415
e59058c4 1416/**
3621a710
JS
1417 * lpfc_sli_iocb_cmd_type - Get the iocb type
1418 * @iocb_cmnd: iocb command code.
e59058c4
JS
1419 *
1420 * This function is called by ring event handler function to get the iocb type.
1421 * This function translates the iocb command to an iocb command type used to
1422 * decide the final disposition of each completed IOCB.
1423 * The function returns
1424 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1425 * LPFC_SOL_IOCB if it is a solicited iocb completion
1426 * LPFC_ABORT_IOCB if it is an abort iocb
1427 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1428 *
1429 * The caller is not required to hold any lock.
1430 **/
dea3101e 1431static lpfc_iocb_type
1432lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1433{
1434 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1435
1436 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1437 return 0;
1438
1439 switch (iocb_cmnd) {
1440 case CMD_XMIT_SEQUENCE_CR:
1441 case CMD_XMIT_SEQUENCE_CX:
1442 case CMD_XMIT_BCAST_CN:
1443 case CMD_XMIT_BCAST_CX:
1444 case CMD_ELS_REQUEST_CR:
1445 case CMD_ELS_REQUEST_CX:
1446 case CMD_CREATE_XRI_CR:
1447 case CMD_CREATE_XRI_CX:
1448 case CMD_GET_RPI_CN:
1449 case CMD_XMIT_ELS_RSP_CX:
1450 case CMD_GET_RPI_CR:
1451 case CMD_FCP_IWRITE_CR:
1452 case CMD_FCP_IWRITE_CX:
1453 case CMD_FCP_IREAD_CR:
1454 case CMD_FCP_IREAD_CX:
1455 case CMD_FCP_ICMND_CR:
1456 case CMD_FCP_ICMND_CX:
f5603511
JS
1457 case CMD_FCP_TSEND_CX:
1458 case CMD_FCP_TRSP_CX:
1459 case CMD_FCP_TRECEIVE_CX:
1460 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 1461 case CMD_ADAPTER_MSG:
1462 case CMD_ADAPTER_DUMP:
1463 case CMD_XMIT_SEQUENCE64_CR:
1464 case CMD_XMIT_SEQUENCE64_CX:
1465 case CMD_XMIT_BCAST64_CN:
1466 case CMD_XMIT_BCAST64_CX:
1467 case CMD_ELS_REQUEST64_CR:
1468 case CMD_ELS_REQUEST64_CX:
1469 case CMD_FCP_IWRITE64_CR:
1470 case CMD_FCP_IWRITE64_CX:
1471 case CMD_FCP_IREAD64_CR:
1472 case CMD_FCP_IREAD64_CX:
1473 case CMD_FCP_ICMND64_CR:
1474 case CMD_FCP_ICMND64_CX:
f5603511
JS
1475 case CMD_FCP_TSEND64_CX:
1476 case CMD_FCP_TRSP64_CX:
1477 case CMD_FCP_TRECEIVE64_CX:
dea3101e 1478 case CMD_GEN_REQUEST64_CR:
1479 case CMD_GEN_REQUEST64_CX:
1480 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1481 case DSSCMD_IWRITE64_CR:
1482 case DSSCMD_IWRITE64_CX:
1483 case DSSCMD_IREAD64_CR:
1484 case DSSCMD_IREAD64_CX:
dea3101e 1485 type = LPFC_SOL_IOCB;
1486 break;
1487 case CMD_ABORT_XRI_CN:
1488 case CMD_ABORT_XRI_CX:
1489 case CMD_CLOSE_XRI_CN:
1490 case CMD_CLOSE_XRI_CX:
1491 case CMD_XRI_ABORTED_CX:
1492 case CMD_ABORT_MXRI64_CN:
6669f9bb 1493 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 1494 type = LPFC_ABORT_IOCB;
1495 break;
1496 case CMD_RCV_SEQUENCE_CX:
1497 case CMD_RCV_ELS_REQ_CX:
1498 case CMD_RCV_SEQUENCE64_CX:
1499 case CMD_RCV_ELS_REQ64_CX:
57127f15 1500 case CMD_ASYNC_STATUS:
ed957684
JS
1501 case CMD_IOCB_RCV_SEQ64_CX:
1502 case CMD_IOCB_RCV_ELS64_CX:
1503 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1504 case CMD_IOCB_RET_XRI64_CX:
dea3101e 1505 type = LPFC_UNSOL_IOCB;
1506 break;
3163f725
JS
1507 case CMD_IOCB_XMIT_MSEQ64_CR:
1508 case CMD_IOCB_XMIT_MSEQ64_CX:
1509 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1510 case CMD_IOCB_RCV_ELS_LIST64_CX:
1511 case CMD_IOCB_CLOSE_EXTENDED_CN:
1512 case CMD_IOCB_ABORT_EXTENDED_CN:
1513 case CMD_IOCB_RET_HBQE64_CN:
1514 case CMD_IOCB_FCP_IBIDIR64_CR:
1515 case CMD_IOCB_FCP_IBIDIR64_CX:
1516 case CMD_IOCB_FCP_ITASKMGT64_CX:
1517 case CMD_IOCB_LOGENTRY_CN:
1518 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1519 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1520 __func__, iocb_cmnd);
3163f725
JS
1521 type = LPFC_UNKNOWN_IOCB;
1522 break;
dea3101e 1523 default:
1524 type = LPFC_UNKNOWN_IOCB;
1525 break;
1526 }
1527
1528 return type;
1529}
1530
e59058c4 1531/**
3621a710 1532 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1533 * @phba: Pointer to HBA context object.
1534 *
1535 * This function is called from SLI initialization code
1536 * to configure every ring of the HBA's SLI interface. The
1537 * caller is not required to hold any lock. This function issues
1538 * a config_ring mailbox command for each ring.
1539 * This function returns zero if successful else returns a negative
1540 * error code.
1541 **/
dea3101e 1542static int
ed957684 1543lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 1544{
1545 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1546 LPFC_MBOXQ_t *pmb;
1547 MAILBOX_t *pmbox;
1548 int i, rc, ret = 0;
dea3101e 1549
ed957684
JS
1550 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1551 if (!pmb)
1552 return -ENOMEM;
04c68496 1553 pmbox = &pmb->u.mb;
ed957684 1554 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1555 for (i = 0; i < psli->num_rings; i++) {
dea3101e 1556 lpfc_config_ring(phba, i, pmb);
1557 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1558 if (rc != MBX_SUCCESS) {
92d7f7b0 1559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1560 "0446 Adapter failed to init (%d), "
dea3101e 1561 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1562 "ring %d\n",
e8b62011
JS
1563 rc, pmbox->mbxCommand,
1564 pmbox->mbxStatus, i);
2e0fef85 1565 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1566 ret = -ENXIO;
1567 break;
dea3101e 1568 }
1569 }
ed957684
JS
1570 mempool_free(pmb, phba->mbox_mem_pool);
1571 return ret;
dea3101e 1572}
1573
e59058c4 1574/**
3621a710 1575 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1576 * @phba: Pointer to HBA context object.
1577 * @pring: Pointer to driver SLI ring object.
1578 * @piocb: Pointer to the driver iocb object.
1579 *
1580 * This function is called with hbalock held. The function adds the
1581 * new iocb to txcmplq of the given ring. This function always returns
1582 * 0. If this function is called for ELS ring, this function checks if
1583 * there is a vport associated with the ELS command. This function also
1584 * starts els_tmofunc timer if this is an ELS command.
1585 **/
dea3101e 1586static int
2e0fef85
JS
1587lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1588 struct lpfc_iocbq *piocb)
dea3101e 1589{
1c2ba475
JT
1590 lockdep_assert_held(&phba->hbalock);
1591
2319f847 1592 BUG_ON(!piocb);
22466da5 1593
dea3101e 1594 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1595 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1596
92d7f7b0
JS
1597 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1598 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1599 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1600 BUG_ON(!piocb->vport);
1601 if (!(piocb->vport->load_flag & FC_UNLOADING))
1602 mod_timer(&piocb->vport->els_tmofunc,
1603 jiffies +
1604 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1605 }
dea3101e 1606
2e0fef85 1607 return 0;
dea3101e 1608}
1609
e59058c4 1610/**
3621a710 1611 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1612 * @phba: Pointer to HBA context object.
1613 * @pring: Pointer to driver SLI ring object.
1614 *
1615 * This function is called with hbalock held to get next
1616 * iocb in txq of the given ring. If there is any iocb in
1617 * the txq, the function returns first iocb in the list after
1618 * removing the iocb from the list, else it returns NULL.
1619 **/
2a9bf3d0 1620struct lpfc_iocbq *
2e0fef85 1621lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1622{
dea3101e 1623 struct lpfc_iocbq *cmd_iocb;
1624
1c2ba475
JT
1625 lockdep_assert_held(&phba->hbalock);
1626
858c9f6c 1627 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1628 return cmd_iocb;
dea3101e 1629}
1630
e59058c4 1631/**
3621a710 1632 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1633 * @phba: Pointer to HBA context object.
1634 * @pring: Pointer to driver SLI ring object.
1635 *
1636 * This function is called with hbalock held and the caller must post the
1637 * iocb without releasing the lock. If the caller releases the lock,
1638 * iocb slot returned by the function is not guaranteed to be available.
1639 * The function returns pointer to the next available iocb slot if there
1640 * is available slot in the ring, else it returns NULL.
1641 * If the get index of the ring is ahead of the put index, the function
1642 * will post an error attention event to the worker thread to take the
1643 * HBA to offline state.
1644 **/
dea3101e 1645static IOCB_t *
1646lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1647{
34b02dcd 1648 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1649 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1650
1651 lockdep_assert_held(&phba->hbalock);
1652
7e56aa25
JS
1653 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1654 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1655 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1656
7e56aa25
JS
1657 if (unlikely(pring->sli.sli3.local_getidx ==
1658 pring->sli.sli3.next_cmdidx)) {
dea3101e 1659
7e56aa25 1660 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1661
7e56aa25 1662 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1664 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1665 "is bigger than cmd ring %d\n",
e8b62011 1666 pring->ringno,
7e56aa25
JS
1667 pring->sli.sli3.local_getidx,
1668 max_cmd_idx);
dea3101e 1669
2e0fef85 1670 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1671 /*
1672 * All error attention handlers are posted to
1673 * worker thread
1674 */
1675 phba->work_ha |= HA_ERATT;
1676 phba->work_hs = HS_FFER3;
92d7f7b0 1677
5e9d9b82 1678 lpfc_worker_wake_up(phba);
dea3101e 1679
1680 return NULL;
1681 }
1682
7e56aa25 1683 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e 1684 return NULL;
1685 }
1686
ed957684 1687 return lpfc_cmd_iocb(phba, pring);
dea3101e 1688}
1689
e59058c4 1690/**
3621a710 1691 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1692 * @phba: Pointer to HBA context object.
1693 * @iocbq: Pointer to driver iocb object.
1694 *
1695 * This function gets an iotag for the iocb. If there is no unused iotag and
1696 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1697 * array and assigns a new iotag.
1698 * The function returns the allocated iotag if successful, else returns zero.
1699 * Zero is not a valid iotag.
1700 * The caller is not required to hold any lock.
1701 **/
604a3e30 1702uint16_t
2e0fef85 1703lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1704{
2e0fef85
JS
1705 struct lpfc_iocbq **new_arr;
1706 struct lpfc_iocbq **old_arr;
604a3e30
JB
1707 size_t new_len;
1708 struct lpfc_sli *psli = &phba->sli;
1709 uint16_t iotag;
dea3101e 1710
2e0fef85 1711 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1712 iotag = psli->last_iotag;
1713 if(++iotag < psli->iocbq_lookup_len) {
1714 psli->last_iotag = iotag;
1715 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1716 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1717 iocbq->iotag = iotag;
1718 return iotag;
2e0fef85 1719 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1720 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1721 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1722 spin_unlock_irq(&phba->hbalock);
1723 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1724 GFP_KERNEL);
1725 if (new_arr) {
2e0fef85 1726 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1727 old_arr = psli->iocbq_lookup;
1728 if (new_len <= psli->iocbq_lookup_len) {
1729 /* highly unprobable case */
1730 kfree(new_arr);
1731 iotag = psli->last_iotag;
1732 if(++iotag < psli->iocbq_lookup_len) {
1733 psli->last_iotag = iotag;
1734 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1735 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1736 iocbq->iotag = iotag;
1737 return iotag;
1738 }
2e0fef85 1739 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1740 return 0;
1741 }
1742 if (psli->iocbq_lookup)
1743 memcpy(new_arr, old_arr,
1744 ((psli->last_iotag + 1) *
311464ec 1745 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1746 psli->iocbq_lookup = new_arr;
1747 psli->iocbq_lookup_len = new_len;
1748 psli->last_iotag = iotag;
1749 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1750 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1751 iocbq->iotag = iotag;
1752 kfree(old_arr);
1753 return iotag;
1754 }
8f6d98d2 1755 } else
2e0fef85 1756 spin_unlock_irq(&phba->hbalock);
dea3101e 1757
bc73905a 1758 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1759 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1760 psli->last_iotag);
dea3101e 1761
604a3e30 1762 return 0;
dea3101e 1763}
1764
e59058c4 1765/**
3621a710 1766 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1767 * @phba: Pointer to HBA context object.
1768 * @pring: Pointer to driver SLI ring object.
1769 * @iocb: Pointer to iocb slot in the ring.
1770 * @nextiocb: Pointer to driver iocb object which need to be
1771 * posted to firmware.
1772 *
1773 * This function is called with hbalock held to post a new iocb to
1774 * the firmware. This function copies the new iocb to ring iocb slot and
1775 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1776 * a completion call back for this iocb else the function will free the
1777 * iocb object.
1778 **/
dea3101e 1779static void
1780lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1781 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1782{
1c2ba475 1783 lockdep_assert_held(&phba->hbalock);
dea3101e 1784 /*
604a3e30 1785 * Set up an iotag
dea3101e 1786 */
604a3e30 1787 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1788
e2a0a9d6 1789
a58cbd52
JS
1790 if (pring->ringno == LPFC_ELS_RING) {
1791 lpfc_debugfs_slow_ring_trc(phba,
1792 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1793 *(((uint32_t *) &nextiocb->iocb) + 4),
1794 *(((uint32_t *) &nextiocb->iocb) + 6),
1795 *(((uint32_t *) &nextiocb->iocb) + 7));
1796 }
1797
dea3101e 1798 /*
1799 * Issue iocb command to adapter
1800 */
92d7f7b0 1801 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1802 wmb();
1803 pring->stats.iocb_cmd++;
1804
1805 /*
1806 * If there is no completion routine to call, we can release the
1807 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1808 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1809 */
1810 if (nextiocb->iocb_cmpl)
1811 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1812 else
2e0fef85 1813 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1814
1815 /*
1816 * Let the HBA know what IOCB slot will be the next one the
1817 * driver will put a command into.
1818 */
7e56aa25
JS
1819 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1820 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1821}
1822
e59058c4 1823/**
3621a710 1824 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1825 * @phba: Pointer to HBA context object.
1826 * @pring: Pointer to driver SLI ring object.
1827 *
1828 * The caller is not required to hold any lock for calling this function.
1829 * This function updates the chip attention bits for the ring to inform firmware
1830 * that there are pending work to be done for this ring and requests an
1831 * interrupt when there is space available in the ring. This function is
1832 * called when the driver is unable to post more iocbs to the ring due
1833 * to unavailability of space in the ring.
1834 **/
dea3101e 1835static void
2e0fef85 1836lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1837{
1838 int ringno = pring->ringno;
1839
1840 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1841
1842 wmb();
1843
1844 /*
1845 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1846 * The HBA will tell us when an IOCB entry is available.
1847 */
1848 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1849 readl(phba->CAregaddr); /* flush */
1850
1851 pring->stats.iocb_cmd_full++;
1852}
1853
e59058c4 1854/**
3621a710 1855 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1856 * @phba: Pointer to HBA context object.
1857 * @pring: Pointer to driver SLI ring object.
1858 *
1859 * This function updates the chip attention register bit for the
1860 * given ring to inform HBA that there is more work to be done
1861 * in this ring. The caller is not required to hold any lock.
1862 **/
dea3101e 1863static void
2e0fef85 1864lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1865{
1866 int ringno = pring->ringno;
1867
1868 /*
1869 * Tell the HBA that there is work to do in this ring.
1870 */
34b02dcd
JS
1871 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1872 wmb();
1873 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1874 readl(phba->CAregaddr); /* flush */
1875 }
dea3101e 1876}
1877
e59058c4 1878/**
3621a710 1879 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1880 * @phba: Pointer to HBA context object.
1881 * @pring: Pointer to driver SLI ring object.
1882 *
1883 * This function is called with hbalock held to post pending iocbs
1884 * in the txq to the firmware. This function is called when driver
1885 * detects space available in the ring.
1886 **/
dea3101e 1887static void
2e0fef85 1888lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1889{
1890 IOCB_t *iocb;
1891 struct lpfc_iocbq *nextiocb;
1892
1c2ba475
JT
1893 lockdep_assert_held(&phba->hbalock);
1894
dea3101e 1895 /*
1896 * Check to see if:
1897 * (a) there is anything on the txq to send
1898 * (b) link is up
1899 * (c) link attention events can be processed (fcp ring only)
1900 * (d) IOCB processing is not blocked by the outstanding mbox command.
1901 */
0e9bb8d7
JS
1902
1903 if (lpfc_is_link_up(phba) &&
1904 (!list_empty(&pring->txq)) &&
895427bd 1905 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1906 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1907
1908 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1909 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1910 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1911
1912 if (iocb)
1913 lpfc_sli_update_ring(phba, pring);
1914 else
1915 lpfc_sli_update_full_ring(phba, pring);
1916 }
1917
1918 return;
1919}
1920
e59058c4 1921/**
3621a710 1922 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1923 * @phba: Pointer to HBA context object.
1924 * @hbqno: HBQ number.
1925 *
1926 * This function is called with hbalock held to get the next
1927 * available slot for the given HBQ. If there is free slot
1928 * available for the HBQ it will return pointer to the next available
1929 * HBQ entry else it will return NULL.
1930 **/
a6ababd2 1931static struct lpfc_hbq_entry *
ed957684
JS
1932lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1933{
1934 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1935
1c2ba475
JT
1936 lockdep_assert_held(&phba->hbalock);
1937
ed957684
JS
1938 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1939 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1940 hbqp->next_hbqPutIdx = 0;
1941
1942 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1943 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1944 uint32_t getidx = le32_to_cpu(raw_index);
1945
1946 hbqp->local_hbqGetIdx = getidx;
1947
1948 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1949 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1950 LOG_SLI | LOG_VPORT,
e8b62011 1951 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1952 "%u is > than hbqp->entry_count %u\n",
e8b62011 1953 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1954 hbqp->entry_count);
1955
1956 phba->link_state = LPFC_HBA_ERROR;
1957 return NULL;
1958 }
1959
1960 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1961 return NULL;
1962 }
1963
51ef4c26
JS
1964 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1965 hbqp->hbqPutIdx;
ed957684
JS
1966}
1967
e59058c4 1968/**
3621a710 1969 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1970 * @phba: Pointer to HBA context object.
1971 *
1972 * This function is called with no lock held to free all the
1973 * hbq buffers while uninitializing the SLI interface. It also
1974 * frees the HBQ buffers returned by the firmware but not yet
1975 * processed by the upper layers.
1976 **/
ed957684
JS
1977void
1978lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1979{
92d7f7b0
JS
1980 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1981 struct hbq_dmabuf *hbq_buf;
3163f725 1982 unsigned long flags;
51ef4c26 1983 int i, hbq_count;
ed957684 1984
51ef4c26 1985 hbq_count = lpfc_sli_hbq_count();
ed957684 1986 /* Return all memory used by all HBQs */
3163f725 1987 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1988 for (i = 0; i < hbq_count; ++i) {
1989 list_for_each_entry_safe(dmabuf, next_dmabuf,
1990 &phba->hbqs[i].hbq_buffer_list, list) {
1991 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1992 list_del(&hbq_buf->dbuf.list);
1993 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1994 }
a8adb832 1995 phba->hbqs[i].buffer_count = 0;
ed957684 1996 }
3163f725
JS
1997
1998 /* Mark the HBQs not in use */
1999 phba->hbq_in_use = 0;
2000 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
2001}
2002
e59058c4 2003/**
3621a710 2004 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
2005 * @phba: Pointer to HBA context object.
2006 * @hbqno: HBQ number.
2007 * @hbq_buf: Pointer to HBQ buffer.
2008 *
2009 * This function is called with the hbalock held to post a
2010 * hbq buffer to the firmware. If the function finds an empty
2011 * slot in the HBQ, it will post the buffer. The function will return
2012 * pointer to the hbq entry if it successfully post the buffer
2013 * else it will return NULL.
2014 **/
3772a991 2015static int
ed957684 2016lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 2017 struct hbq_dmabuf *hbq_buf)
3772a991 2018{
1c2ba475 2019 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2020 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2021}
2022
2023/**
2024 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2025 * @phba: Pointer to HBA context object.
2026 * @hbqno: HBQ number.
2027 * @hbq_buf: Pointer to HBQ buffer.
2028 *
2029 * This function is called with the hbalock held to post a hbq buffer to the
2030 * firmware. If the function finds an empty slot in the HBQ, it will post the
2031 * buffer and place it on the hbq_buffer_list. The function will return zero if
2032 * it successfully post the buffer else it will return an error.
2033 **/
2034static int
2035lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2036 struct hbq_dmabuf *hbq_buf)
ed957684
JS
2037{
2038 struct lpfc_hbq_entry *hbqe;
92d7f7b0 2039 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 2040
1c2ba475 2041 lockdep_assert_held(&phba->hbalock);
ed957684
JS
2042 /* Get next HBQ entry slot to use */
2043 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2044 if (hbqe) {
2045 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2046
92d7f7b0
JS
2047 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2048 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 2049 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 2050 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
2051 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2052 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2053 /* Sync SLIM */
ed957684
JS
2054 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2055 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 2056 /* flush */
ed957684 2057 readl(phba->hbq_put + hbqno);
51ef4c26 2058 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
2059 return 0;
2060 } else
2061 return -ENOMEM;
ed957684
JS
2062}
2063
4f774513
JS
2064/**
2065 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2066 * @phba: Pointer to HBA context object.
2067 * @hbqno: HBQ number.
2068 * @hbq_buf: Pointer to HBQ buffer.
2069 *
2070 * This function is called with the hbalock held to post an RQE to the SLI4
2071 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2072 * the hbq_buffer_list and return zero, otherwise it will return an error.
2073 **/
2074static int
2075lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2076 struct hbq_dmabuf *hbq_buf)
2077{
2078 int rc;
2079 struct lpfc_rqe hrqe;
2080 struct lpfc_rqe drqe;
895427bd
JS
2081 struct lpfc_queue *hrq;
2082 struct lpfc_queue *drq;
2083
2084 if (hbqno != LPFC_ELS_HBQ)
2085 return 1;
2086 hrq = phba->sli4_hba.hdr_rq;
2087 drq = phba->sli4_hba.dat_rq;
4f774513 2088
1c2ba475 2089 lockdep_assert_held(&phba->hbalock);
4f774513
JS
2090 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2091 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2092 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2093 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 2094 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
2095 if (rc < 0)
2096 return rc;
895427bd 2097 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
2098 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2099 return 0;
2100}
2101
e59058c4 2102/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
2103static struct lpfc_hbq_init lpfc_els_hbq = {
2104 .rn = 1,
def9c7a9 2105 .entry_count = 256,
92d7f7b0
JS
2106 .mask_count = 0,
2107 .profile = 0,
51ef4c26 2108 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 2109 .buffer_count = 0,
a257bf90
JS
2110 .init_count = 40,
2111 .add_count = 40,
92d7f7b0 2112};
ed957684 2113
e59058c4 2114/* Array of HBQs */
78b2d852 2115struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
2116 &lpfc_els_hbq,
2117};
ed957684 2118
e59058c4 2119/**
3621a710 2120 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
2121 * @phba: Pointer to HBA context object.
2122 * @hbqno: HBQ number.
2123 * @count: Number of HBQ buffers to be posted.
2124 *
d7c255b2
JS
2125 * This function is called with no lock held to post more hbq buffers to the
2126 * given HBQ. The function returns the number of HBQ buffers successfully
2127 * posted.
e59058c4 2128 **/
311464ec 2129static int
92d7f7b0 2130lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 2131{
d7c255b2 2132 uint32_t i, posted = 0;
3163f725 2133 unsigned long flags;
92d7f7b0 2134 struct hbq_dmabuf *hbq_buffer;
d7c255b2 2135 LIST_HEAD(hbq_buf_list);
eafe1df9 2136 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 2137 return 0;
51ef4c26 2138
d7c255b2
JS
2139 if ((phba->hbqs[hbqno].buffer_count + count) >
2140 lpfc_hbq_defs[hbqno]->entry_count)
2141 count = lpfc_hbq_defs[hbqno]->entry_count -
2142 phba->hbqs[hbqno].buffer_count;
2143 if (!count)
2144 return 0;
2145 /* Allocate HBQ entries */
2146 for (i = 0; i < count; i++) {
2147 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2148 if (!hbq_buffer)
2149 break;
2150 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2151 }
3163f725
JS
2152 /* Check whether HBQ is still in use */
2153 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 2154 if (!phba->hbq_in_use)
d7c255b2
JS
2155 goto err;
2156 while (!list_empty(&hbq_buf_list)) {
2157 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2158 dbuf.list);
2159 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2160 (hbqno << 16));
3772a991 2161 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 2162 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
2163 posted++;
2164 } else
51ef4c26 2165 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 2166 }
3163f725 2167 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2168 return posted;
2169err:
eafe1df9 2170 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2171 while (!list_empty(&hbq_buf_list)) {
2172 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2173 dbuf.list);
2174 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2175 }
2176 return 0;
ed957684
JS
2177}
2178
e59058c4 2179/**
3621a710 2180 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
2181 * @phba: Pointer to HBA context object.
2182 * @qno: HBQ number.
2183 *
2184 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
2185 * is called with no lock held. The function returns the number of HBQ entries
2186 * successfully allocated.
e59058c4 2187 **/
92d7f7b0
JS
2188int
2189lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 2190{
def9c7a9
JS
2191 if (phba->sli_rev == LPFC_SLI_REV4)
2192 return 0;
2193 else
2194 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2195 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 2196}
ed957684 2197
e59058c4 2198/**
3621a710 2199 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2200 * @phba: Pointer to HBA context object.
2201 * @qno: HBQ queue number.
2202 *
2203 * This function is called from SLI initialization code path with
2204 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2205 * function returns the number of HBQ entries successfully allocated.
e59058c4 2206 **/
a6ababd2 2207static int
92d7f7b0
JS
2208lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2209{
def9c7a9
JS
2210 if (phba->sli_rev == LPFC_SLI_REV4)
2211 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2212 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2213 else
2214 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2215 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2216}
2217
3772a991
JS
2218/**
2219 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2220 * @phba: Pointer to HBA context object.
2221 * @hbqno: HBQ number.
2222 *
2223 * This function removes the first hbq buffer on an hbq list and returns a
2224 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2225 **/
2226static struct hbq_dmabuf *
2227lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2228{
2229 struct lpfc_dmabuf *d_buf;
2230
2231 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2232 if (!d_buf)
2233 return NULL;
2234 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2235}
2236
2d7dbc4c
JS
2237/**
2238 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2239 * @phba: Pointer to HBA context object.
2240 * @hbqno: HBQ number.
2241 *
2242 * This function removes the first RQ buffer on an RQ buffer list and returns a
2243 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2244 **/
2245static struct rqb_dmabuf *
2246lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2247{
2248 struct lpfc_dmabuf *h_buf;
2249 struct lpfc_rqb *rqbp;
2250
2251 rqbp = hrq->rqbp;
2252 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2253 struct lpfc_dmabuf, list);
2254 if (!h_buf)
2255 return NULL;
2256 rqbp->buffer_count--;
2257 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2258}
2259
e59058c4 2260/**
3621a710 2261 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2262 * @phba: Pointer to HBA context object.
2263 * @tag: Tag of the hbq buffer.
2264 *
71892418
SH
2265 * This function searches for the hbq buffer associated with the given tag in
2266 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2267 * otherwise it returns NULL.
e59058c4 2268 **/
a6ababd2 2269static struct hbq_dmabuf *
92d7f7b0 2270lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2271{
92d7f7b0
JS
2272 struct lpfc_dmabuf *d_buf;
2273 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2274 uint32_t hbqno;
2275
2276 hbqno = tag >> 16;
a0a74e45 2277 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2278 return NULL;
ed957684 2279
3772a991 2280 spin_lock_irq(&phba->hbalock);
51ef4c26 2281 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2282 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2283 if (hbq_buf->tag == tag) {
3772a991 2284 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2285 return hbq_buf;
ed957684
JS
2286 }
2287 }
3772a991 2288 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2290 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2291 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2292 return NULL;
ed957684
JS
2293}
2294
e59058c4 2295/**
3621a710 2296 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2297 * @phba: Pointer to HBA context object.
2298 * @hbq_buffer: Pointer to HBQ buffer.
2299 *
2300 * This function is called with hbalock. This function gives back
2301 * the hbq buffer to firmware. If the HBQ does not have space to
2302 * post the buffer, it will free the buffer.
2303 **/
ed957684 2304void
51ef4c26 2305lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2306{
2307 uint32_t hbqno;
2308
51ef4c26
JS
2309 if (hbq_buffer) {
2310 hbqno = hbq_buffer->tag >> 16;
3772a991 2311 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2312 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2313 }
2314}
2315
e59058c4 2316/**
3621a710 2317 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2318 * @mbxCommand: mailbox command code.
2319 *
2320 * This function is called by the mailbox event handler function to verify
2321 * that the completed mailbox command is a legitimate mailbox command. If the
2322 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2323 * and the mailbox event handler will take the HBA offline.
2324 **/
dea3101e 2325static int
2326lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2327{
2328 uint8_t ret;
2329
2330 switch (mbxCommand) {
2331 case MBX_LOAD_SM:
2332 case MBX_READ_NV:
2333 case MBX_WRITE_NV:
a8adb832 2334 case MBX_WRITE_VPARMS:
dea3101e 2335 case MBX_RUN_BIU_DIAG:
2336 case MBX_INIT_LINK:
2337 case MBX_DOWN_LINK:
2338 case MBX_CONFIG_LINK:
2339 case MBX_CONFIG_RING:
2340 case MBX_RESET_RING:
2341 case MBX_READ_CONFIG:
2342 case MBX_READ_RCONFIG:
2343 case MBX_READ_SPARM:
2344 case MBX_READ_STATUS:
2345 case MBX_READ_RPI:
2346 case MBX_READ_XRI:
2347 case MBX_READ_REV:
2348 case MBX_READ_LNK_STAT:
2349 case MBX_REG_LOGIN:
2350 case MBX_UNREG_LOGIN:
dea3101e 2351 case MBX_CLEAR_LA:
2352 case MBX_DUMP_MEMORY:
2353 case MBX_DUMP_CONTEXT:
2354 case MBX_RUN_DIAGS:
2355 case MBX_RESTART:
2356 case MBX_UPDATE_CFG:
2357 case MBX_DOWN_LOAD:
2358 case MBX_DEL_LD_ENTRY:
2359 case MBX_RUN_PROGRAM:
2360 case MBX_SET_MASK:
09372820 2361 case MBX_SET_VARIABLE:
dea3101e 2362 case MBX_UNREG_D_ID:
41415862 2363 case MBX_KILL_BOARD:
dea3101e 2364 case MBX_CONFIG_FARP:
41415862 2365 case MBX_BEACON:
dea3101e 2366 case MBX_LOAD_AREA:
2367 case MBX_RUN_BIU_DIAG64:
2368 case MBX_CONFIG_PORT:
2369 case MBX_READ_SPARM64:
2370 case MBX_READ_RPI64:
2371 case MBX_REG_LOGIN64:
76a95d75 2372 case MBX_READ_TOPOLOGY:
09372820 2373 case MBX_WRITE_WWN:
dea3101e 2374 case MBX_SET_DEBUG:
2375 case MBX_LOAD_EXP_ROM:
57127f15 2376 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2377 case MBX_REG_VPI:
2378 case MBX_UNREG_VPI:
858c9f6c 2379 case MBX_HEARTBEAT:
84774a4d
JS
2380 case MBX_PORT_CAPABILITIES:
2381 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2382 case MBX_SLI4_CONFIG:
2383 case MBX_SLI4_REQ_FTRS:
2384 case MBX_REG_FCFI:
2385 case MBX_UNREG_FCFI:
2386 case MBX_REG_VFI:
2387 case MBX_UNREG_VFI:
2388 case MBX_INIT_VPI:
2389 case MBX_INIT_VFI:
2390 case MBX_RESUME_RPI:
c7495937
JS
2391 case MBX_READ_EVENT_LOG_STATUS:
2392 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2393 case MBX_SECURITY_MGMT:
2394 case MBX_AUTH_PORT:
940eb687 2395 case MBX_ACCESS_VDATA:
dea3101e 2396 ret = mbxCommand;
2397 break;
2398 default:
2399 ret = MBX_SHUTDOWN;
2400 break;
2401 }
2e0fef85 2402 return ret;
dea3101e 2403}
e59058c4
JS
2404
2405/**
3621a710 2406 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2407 * @phba: Pointer to HBA context object.
2408 * @pmboxq: Pointer to mailbox command.
2409 *
2410 * This is completion handler function for mailbox commands issued from
2411 * lpfc_sli_issue_mbox_wait function. This function is called by the
2412 * mailbox event handler function with no lock held. This function
2413 * will wake up thread waiting on the wait queue pointed by context1
2414 * of the mailbox.
2415 **/
04c68496 2416void
2e0fef85 2417lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 2418{
858c9f6c 2419 unsigned long drvr_flag;
e29d74f8 2420 struct completion *pmbox_done;
dea3101e 2421
2422 /*
e29d74f8 2423 * If pmbox_done is empty, the driver thread gave up waiting and
dea3101e 2424 * continued running.
2425 */
7054a606 2426 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2427 spin_lock_irqsave(&phba->hbalock, drvr_flag);
e29d74f8
JS
2428 pmbox_done = (struct completion *)pmboxq->context3;
2429 if (pmbox_done)
2430 complete(pmbox_done);
858c9f6c 2431 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 2432 return;
2433}
2434
e59058c4
JS
2435
2436/**
3621a710 2437 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2438 * @phba: Pointer to HBA context object.
2439 * @pmb: Pointer to mailbox object.
2440 *
2441 * This function is the default mailbox completion handler. It
2442 * frees the memory resources associated with the completed mailbox
2443 * command. If the completed command is a REG_LOGIN mailbox command,
2444 * this function will issue a UREG_LOGIN to re-claim the RPI.
2445 **/
dea3101e 2446void
2e0fef85 2447lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2448{
d439d286 2449 struct lpfc_vport *vport = pmb->vport;
dea3101e 2450 struct lpfc_dmabuf *mp;
d439d286 2451 struct lpfc_nodelist *ndlp;
5af5eee7 2452 struct Scsi_Host *shost;
04c68496 2453 uint16_t rpi, vpi;
7054a606
JS
2454 int rc;
2455
dea3101e 2456 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2457
dea3101e 2458 if (mp) {
2459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2460 kfree(mp);
2461 }
7054a606
JS
2462
2463 /*
2464 * If a REG_LOGIN succeeded after node is destroyed or node
2465 * is in re-discovery driver need to cleanup the RPI.
2466 */
2e0fef85 2467 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2468 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2469 !pmb->u.mb.mbxStatus) {
2470 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2471 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2472 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2473 pmb->vport = vport;
92d7f7b0 2474 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2475 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2476 if (rc != MBX_NOT_FINISHED)
2477 return;
2478 }
2479
695a814e
JS
2480 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2481 !(phba->pport->load_flag & FC_UNLOADING) &&
2482 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2483 shost = lpfc_shost_from_vport(vport);
2484 spin_lock_irq(shost->host_lock);
2485 vport->vpi_state |= LPFC_VPI_REGISTERED;
2486 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2487 spin_unlock_irq(shost->host_lock);
695a814e
JS
2488 }
2489
d439d286
JS
2490 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2491 ndlp = (struct lpfc_nodelist *)pmb->context2;
2492 lpfc_nlp_put(ndlp);
2493 pmb->context2 = NULL;
2494 }
2495
dcf2a4e0
JS
2496 /* Check security permission status on INIT_LINK mailbox command */
2497 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2498 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2499 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2500 "2860 SLI authentication is required "
2501 "for INIT_LINK but has not done yet\n");
2502
04c68496
JS
2503 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2504 lpfc_sli4_mbox_cmd_free(phba, pmb);
2505 else
2506 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2507}
be6bb941
JS
2508 /**
2509 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2510 * @phba: Pointer to HBA context object.
2511 * @pmb: Pointer to mailbox object.
2512 *
2513 * This function is the unreg rpi mailbox completion handler. It
2514 * frees the memory resources associated with the completed mailbox
2515 * command. An additional refrenece is put on the ndlp to prevent
2516 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2517 * the unreg mailbox command completes, this routine puts the
2518 * reference back.
2519 *
2520 **/
2521void
2522lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2523{
2524 struct lpfc_vport *vport = pmb->vport;
2525 struct lpfc_nodelist *ndlp;
2526
2527 ndlp = pmb->context1;
2528 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2529 if (phba->sli_rev == LPFC_SLI_REV4 &&
2530 (bf_get(lpfc_sli_intf_if_type,
27d6ac0a 2531 &phba->sli4_hba.sli_intf) >=
be6bb941
JS
2532 LPFC_SLI_INTF_IF_TYPE_2)) {
2533 if (ndlp) {
2534 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2535 "0010 UNREG_LOGIN vpi:%x "
2536 "rpi:%x DID:%x map:%x %p\n",
2537 vport->vpi, ndlp->nlp_rpi,
2538 ndlp->nlp_DID,
2539 ndlp->nlp_usg_map, ndlp);
7c5e518c 2540 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2541 lpfc_nlp_put(ndlp);
2542 }
2543 }
2544 }
2545
2546 mempool_free(pmb, phba->mbox_mem_pool);
2547}
dea3101e 2548
e59058c4 2549/**
3621a710 2550 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2551 * @phba: Pointer to HBA context object.
2552 *
2553 * This function is called with no lock held. This function processes all
2554 * the completed mailbox commands and gives it to upper layers. The interrupt
2555 * service routine processes mailbox completion interrupt and adds completed
2556 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2557 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2558 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2559 * function returns the mailbox commands to the upper layer by calling the
2560 * completion handler function of each mailbox.
2561 **/
dea3101e 2562int
2e0fef85 2563lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2564{
92d7f7b0 2565 MAILBOX_t *pmbox;
dea3101e 2566 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2567 int rc;
2568 LIST_HEAD(cmplq);
dea3101e 2569
2570 phba->sli.slistat.mbox_event++;
2571
92d7f7b0
JS
2572 /* Get all completed mailboxe buffers into the cmplq */
2573 spin_lock_irq(&phba->hbalock);
2574 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2575 spin_unlock_irq(&phba->hbalock);
dea3101e 2576
92d7f7b0
JS
2577 /* Get a Mailbox buffer to setup mailbox commands for callback */
2578 do {
2579 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2580 if (pmb == NULL)
2581 break;
2e0fef85 2582
04c68496 2583 pmbox = &pmb->u.mb;
dea3101e 2584
858c9f6c
JS
2585 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2586 if (pmb->vport) {
2587 lpfc_debugfs_disc_trc(pmb->vport,
2588 LPFC_DISC_TRC_MBOX_VPORT,
2589 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2590 (uint32_t)pmbox->mbxCommand,
2591 pmbox->un.varWords[0],
2592 pmbox->un.varWords[1]);
2593 }
2594 else {
2595 lpfc_debugfs_disc_trc(phba->pport,
2596 LPFC_DISC_TRC_MBOX,
2597 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2598 (uint32_t)pmbox->mbxCommand,
2599 pmbox->un.varWords[0],
2600 pmbox->un.varWords[1]);
2601 }
2602 }
2603
dea3101e 2604 /*
2605 * It is a fatal error if unknown mbox command completion.
2606 */
2607 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2608 MBX_SHUTDOWN) {
af901ca1 2609 /* Unknown mailbox command compl */
92d7f7b0 2610 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2611 "(%d):0323 Unknown Mailbox command "
a183a15f 2612 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2613 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2614 pmbox->mbxCommand,
a183a15f
JS
2615 lpfc_sli_config_mbox_subsys_get(phba,
2616 pmb),
2617 lpfc_sli_config_mbox_opcode_get(phba,
2618 pmb));
2e0fef85 2619 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2620 phba->work_hs = HS_FFER3;
2621 lpfc_handle_eratt(phba);
92d7f7b0 2622 continue;
dea3101e 2623 }
2624
dea3101e 2625 if (pmbox->mbxStatus) {
2626 phba->sli.slistat.mbox_stat_err++;
2627 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2628 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2629 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2630 LOG_MBOX | LOG_SLI,
2631 "(%d):0305 Mbox cmd cmpl "
2632 "error - RETRYing Data: x%x "
2633 "(x%x/x%x) x%x x%x x%x\n",
2634 pmb->vport ? pmb->vport->vpi : 0,
2635 pmbox->mbxCommand,
2636 lpfc_sli_config_mbox_subsys_get(phba,
2637 pmb),
2638 lpfc_sli_config_mbox_opcode_get(phba,
2639 pmb),
2640 pmbox->mbxStatus,
2641 pmbox->un.varWords[0],
2642 pmb->vport->port_state);
dea3101e 2643 pmbox->mbxStatus = 0;
2644 pmbox->mbxOwner = OWN_HOST;
dea3101e 2645 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2646 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2647 continue;
dea3101e 2648 }
2649 }
2650
2651 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2652 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2653 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2654 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2655 "x%x x%x x%x\n",
92d7f7b0 2656 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2657 pmbox->mbxCommand,
a183a15f
JS
2658 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2659 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e 2660 pmb->mbox_cmpl,
2661 *((uint32_t *) pmbox),
2662 pmbox->un.varWords[0],
2663 pmbox->un.varWords[1],
2664 pmbox->un.varWords[2],
2665 pmbox->un.varWords[3],
2666 pmbox->un.varWords[4],
2667 pmbox->un.varWords[5],
2668 pmbox->un.varWords[6],
e74c03c8
JS
2669 pmbox->un.varWords[7],
2670 pmbox->un.varWords[8],
2671 pmbox->un.varWords[9],
2672 pmbox->un.varWords[10]);
dea3101e 2673
92d7f7b0 2674 if (pmb->mbox_cmpl)
dea3101e 2675 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2676 } while (1);
2677 return 0;
2678}
dea3101e 2679
e59058c4 2680/**
3621a710 2681 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2682 * @phba: Pointer to HBA context object.
2683 * @pring: Pointer to driver SLI ring object.
2684 * @tag: buffer tag.
2685 *
2686 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2687 * is set in the tag the buffer is posted for a particular exchange,
2688 * the function will return the buffer without replacing the buffer.
2689 * If the buffer is for unsolicited ELS or CT traffic, this function
2690 * returns the buffer and also posts another buffer to the firmware.
2691 **/
76bb24ef
JS
2692static struct lpfc_dmabuf *
2693lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2694 struct lpfc_sli_ring *pring,
2695 uint32_t tag)
76bb24ef 2696{
9f1e1b50
JS
2697 struct hbq_dmabuf *hbq_entry;
2698
76bb24ef
JS
2699 if (tag & QUE_BUFTAG_BIT)
2700 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2701 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2702 if (!hbq_entry)
2703 return NULL;
2704 return &hbq_entry->dbuf;
76bb24ef 2705}
57127f15 2706
3772a991
JS
2707/**
2708 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2709 * @phba: Pointer to HBA context object.
2710 * @pring: Pointer to driver SLI ring object.
2711 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2712 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2713 * @fch_type: the type for the first frame of the sequence.
2714 *
2715 * This function is called with no lock held. This function uses the r_ctl and
2716 * type of the received sequence to find the correct callback function to call
2717 * to process the sequence.
2718 **/
2719static int
2720lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2721 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2722 uint32_t fch_type)
2723{
2724 int i;
2725
f358dd0c
JS
2726 switch (fch_type) {
2727 case FC_TYPE_NVME:
d613b6a7 2728 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2729 return 1;
2730 default:
2731 break;
2732 }
2733
3772a991
JS
2734 /* unSolicited Responses */
2735 if (pring->prt[0].profile) {
2736 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2737 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2738 saveq);
2739 return 1;
2740 }
2741 /* We must search, based on rctl / type
2742 for the right routine */
2743 for (i = 0; i < pring->num_mask; i++) {
2744 if ((pring->prt[i].rctl == fch_r_ctl) &&
2745 (pring->prt[i].type == fch_type)) {
2746 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2747 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2748 (phba, pring, saveq);
2749 return 1;
2750 }
2751 }
2752 return 0;
2753}
e59058c4
JS
2754
2755/**
3621a710 2756 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2757 * @phba: Pointer to HBA context object.
2758 * @pring: Pointer to driver SLI ring object.
2759 * @saveq: Pointer to the unsolicited iocb.
2760 *
2761 * This function is called with no lock held by the ring event handler
2762 * when there is an unsolicited iocb posted to the response ring by the
2763 * firmware. This function gets the buffer associated with the iocbs
2764 * and calls the event handler for the ring. This function handles both
2765 * qring buffers and hbq buffers.
2766 * When the function returns 1 the caller can free the iocb object otherwise
2767 * upper layer functions will free the iocb objects.
2768 **/
dea3101e 2769static int
2770lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2771 struct lpfc_iocbq *saveq)
2772{
2773 IOCB_t * irsp;
2774 WORD5 * w5p;
2775 uint32_t Rctl, Type;
76bb24ef 2776 struct lpfc_iocbq *iocbq;
3163f725 2777 struct lpfc_dmabuf *dmzbuf;
dea3101e 2778
dea3101e 2779 irsp = &(saveq->iocb);
57127f15
JS
2780
2781 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2782 if (pring->lpfc_sli_rcv_async_status)
2783 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2784 else
2785 lpfc_printf_log(phba,
2786 KERN_WARNING,
2787 LOG_SLI,
2788 "0316 Ring %d handler: unexpected "
2789 "ASYNC_STATUS iocb received evt_code "
2790 "0x%x\n",
2791 pring->ringno,
2792 irsp->un.asyncstat.evt_code);
2793 return 1;
2794 }
2795
3163f725
JS
2796 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2797 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2798 if (irsp->ulpBdeCount > 0) {
2799 dmzbuf = lpfc_sli_get_buff(phba, pring,
2800 irsp->un.ulpWord[3]);
2801 lpfc_in_buf_free(phba, dmzbuf);
2802 }
2803
2804 if (irsp->ulpBdeCount > 1) {
2805 dmzbuf = lpfc_sli_get_buff(phba, pring,
2806 irsp->unsli3.sli3Words[3]);
2807 lpfc_in_buf_free(phba, dmzbuf);
2808 }
2809
2810 if (irsp->ulpBdeCount > 2) {
2811 dmzbuf = lpfc_sli_get_buff(phba, pring,
2812 irsp->unsli3.sli3Words[7]);
2813 lpfc_in_buf_free(phba, dmzbuf);
2814 }
2815
2816 return 1;
2817 }
2818
92d7f7b0 2819 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2820 if (irsp->ulpBdeCount != 0) {
2821 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2822 irsp->un.ulpWord[3]);
2823 if (!saveq->context2)
2824 lpfc_printf_log(phba,
2825 KERN_ERR,
2826 LOG_SLI,
2827 "0341 Ring %d Cannot find buffer for "
2828 "an unsolicited iocb. tag 0x%x\n",
2829 pring->ringno,
2830 irsp->un.ulpWord[3]);
76bb24ef
JS
2831 }
2832 if (irsp->ulpBdeCount == 2) {
2833 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2834 irsp->unsli3.sli3Words[7]);
2835 if (!saveq->context3)
2836 lpfc_printf_log(phba,
2837 KERN_ERR,
2838 LOG_SLI,
2839 "0342 Ring %d Cannot find buffer for an"
2840 " unsolicited iocb. tag 0x%x\n",
2841 pring->ringno,
2842 irsp->unsli3.sli3Words[7]);
2843 }
2844 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2845 irsp = &(iocbq->iocb);
76bb24ef
JS
2846 if (irsp->ulpBdeCount != 0) {
2847 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2848 irsp->un.ulpWord[3]);
9c2face6 2849 if (!iocbq->context2)
76bb24ef
JS
2850 lpfc_printf_log(phba,
2851 KERN_ERR,
2852 LOG_SLI,
2853 "0343 Ring %d Cannot find "
2854 "buffer for an unsolicited iocb"
2855 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2856 irsp->un.ulpWord[3]);
76bb24ef
JS
2857 }
2858 if (irsp->ulpBdeCount == 2) {
2859 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2860 irsp->unsli3.sli3Words[7]);
9c2face6 2861 if (!iocbq->context3)
76bb24ef
JS
2862 lpfc_printf_log(phba,
2863 KERN_ERR,
2864 LOG_SLI,
2865 "0344 Ring %d Cannot find "
2866 "buffer for an unsolicited "
2867 "iocb. tag 0x%x\n",
2868 pring->ringno,
2869 irsp->unsli3.sli3Words[7]);
2870 }
2871 }
92d7f7b0 2872 }
9c2face6
JS
2873 if (irsp->ulpBdeCount != 0 &&
2874 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2875 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2876 int found = 0;
2877
2878 /* search continue save q for same XRI */
2879 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2880 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2881 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2882 list_add_tail(&saveq->list, &iocbq->list);
2883 found = 1;
2884 break;
2885 }
2886 }
2887 if (!found)
2888 list_add_tail(&saveq->clist,
2889 &pring->iocb_continue_saveq);
2890 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2891 list_del_init(&iocbq->clist);
2892 saveq = iocbq;
2893 irsp = &(saveq->iocb);
2894 } else
2895 return 0;
2896 }
2897 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2898 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2899 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2900 Rctl = FC_RCTL_ELS_REQ;
2901 Type = FC_TYPE_ELS;
9c2face6
JS
2902 } else {
2903 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2904 Rctl = w5p->hcsw.Rctl;
2905 Type = w5p->hcsw.Type;
2906
2907 /* Firmware Workaround */
2908 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2909 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2910 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2911 Rctl = FC_RCTL_ELS_REQ;
2912 Type = FC_TYPE_ELS;
9c2face6
JS
2913 w5p->hcsw.Rctl = Rctl;
2914 w5p->hcsw.Type = Type;
2915 }
2916 }
92d7f7b0 2917
3772a991 2918 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2919 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2920 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2921 "Type x%x received\n",
e8b62011 2922 pring->ringno, Rctl, Type);
3772a991 2923
92d7f7b0 2924 return 1;
dea3101e 2925}
2926
e59058c4 2927/**
3621a710 2928 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2929 * @phba: Pointer to HBA context object.
2930 * @pring: Pointer to driver SLI ring object.
2931 * @prspiocb: Pointer to response iocb object.
2932 *
2933 * This function looks up the iocb_lookup table to get the command iocb
2934 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2935 * response iocb. This function is called with the hbalock held
2936 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2937 * This function returns the command iocb object if it finds the command
2938 * iocb else returns NULL.
2939 **/
dea3101e 2940static struct lpfc_iocbq *
2e0fef85
JS
2941lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2942 struct lpfc_sli_ring *pring,
2943 struct lpfc_iocbq *prspiocb)
dea3101e 2944{
dea3101e 2945 struct lpfc_iocbq *cmd_iocb = NULL;
2946 uint16_t iotag;
1c2ba475 2947 lockdep_assert_held(&phba->hbalock);
dea3101e 2948
604a3e30
JB
2949 iotag = prspiocb->iocb.ulpIoTag;
2950
2951 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2952 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2953 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2954 /* remove from txcmpl queue list */
2955 list_del_init(&cmd_iocb->list);
4f2e66c6 2956 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2957 return cmd_iocb;
2a9bf3d0 2958 }
dea3101e 2959 }
2960
dea3101e 2961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2962 "0317 iotag x%x is out of "
604a3e30 2963 "range: max iotag x%x wd0 x%x\n",
e8b62011 2964 iotag, phba->sli.last_iotag,
604a3e30 2965 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2966 return NULL;
2967}
2968
3772a991
JS
2969/**
2970 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2971 * @phba: Pointer to HBA context object.
2972 * @pring: Pointer to driver SLI ring object.
2973 * @iotag: IOCB tag.
2974 *
2975 * This function looks up the iocb_lookup table to get the command iocb
2976 * corresponding to the given iotag. This function is called with the
2977 * hbalock held.
2978 * This function returns the command iocb object if it finds the command
2979 * iocb else returns NULL.
2980 **/
2981static struct lpfc_iocbq *
2982lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2983 struct lpfc_sli_ring *pring, uint16_t iotag)
2984{
895427bd 2985 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2986
1c2ba475 2987 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2988 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2989 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2990 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2991 /* remove from txcmpl queue list */
2992 list_del_init(&cmd_iocb->list);
2993 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2994 return cmd_iocb;
2a9bf3d0 2995 }
3772a991 2996 }
89533e9b 2997
3772a991 2998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2999 "0372 iotag x%x lookup error: max iotag (x%x) "
3000 "iocb_flag x%x\n",
3001 iotag, phba->sli.last_iotag,
3002 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
3003 return NULL;
3004}
3005
e59058c4 3006/**
3621a710 3007 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
3008 * @phba: Pointer to HBA context object.
3009 * @pring: Pointer to driver SLI ring object.
3010 * @saveq: Pointer to the response iocb to be processed.
3011 *
3012 * This function is called by the ring event handler for non-fcp
3013 * rings when there is a new response iocb in the response ring.
3014 * The caller is not required to hold any locks. This function
3015 * gets the command iocb associated with the response iocb and
3016 * calls the completion handler for the command iocb. If there
3017 * is no completion handler, the function will free the resources
3018 * associated with command iocb. If the response iocb is for
3019 * an already aborted command iocb, the status of the completion
3020 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3021 * This function always returns 1.
3022 **/
dea3101e 3023static int
2e0fef85 3024lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 3025 struct lpfc_iocbq *saveq)
3026{
2e0fef85 3027 struct lpfc_iocbq *cmdiocbp;
dea3101e 3028 int rc = 1;
3029 unsigned long iflag;
3030
3031 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
3032 if (phba->sli_rev == LPFC_SLI_REV4)
3033 spin_lock_irqsave(&pring->ring_lock, iflag);
3034 else
3035 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 3036 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
3037 if (phba->sli_rev == LPFC_SLI_REV4)
3038 spin_unlock_irqrestore(&pring->ring_lock, iflag);
3039 else
3040 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 3041
dea3101e 3042 if (cmdiocbp) {
3043 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
3044 /*
3045 * If an ELS command failed send an event to mgmt
3046 * application.
3047 */
3048 if (saveq->iocb.ulpStatus &&
3049 (pring->ringno == LPFC_ELS_RING) &&
3050 (cmdiocbp->iocb.ulpCommand ==
3051 CMD_ELS_REQUEST64_CR))
3052 lpfc_send_els_failure_event(phba,
3053 cmdiocbp, saveq);
3054
dea3101e 3055 /*
3056 * Post all ELS completions to the worker thread.
3057 * All other are passed to the completion callback.
3058 */
3059 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
3060 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3061 (cmdiocbp->iocb_flag &
3062 LPFC_DRIVER_ABORTED)) {
3063 spin_lock_irqsave(&phba->hbalock,
3064 iflag);
07951076
JS
3065 cmdiocbp->iocb_flag &=
3066 ~LPFC_DRIVER_ABORTED;
341af102
JS
3067 spin_unlock_irqrestore(&phba->hbalock,
3068 iflag);
07951076
JS
3069 saveq->iocb.ulpStatus =
3070 IOSTAT_LOCAL_REJECT;
3071 saveq->iocb.un.ulpWord[4] =
3072 IOERR_SLI_ABORTED;
0ff10d46
JS
3073
3074 /* Firmware could still be in progress
3075 * of DMAing payload, so don't free data
3076 * buffer till after a hbeat.
3077 */
341af102
JS
3078 spin_lock_irqsave(&phba->hbalock,
3079 iflag);
0ff10d46 3080 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
3081 spin_unlock_irqrestore(&phba->hbalock,
3082 iflag);
3083 }
0f65ff68
JS
3084 if (phba->sli_rev == LPFC_SLI_REV4) {
3085 if (saveq->iocb_flag &
3086 LPFC_EXCHANGE_BUSY) {
3087 /* Set cmdiocb flag for the
3088 * exchange busy so sgl (xri)
3089 * will not be released until
3090 * the abort xri is received
3091 * from hba.
3092 */
3093 spin_lock_irqsave(
3094 &phba->hbalock, iflag);
3095 cmdiocbp->iocb_flag |=
3096 LPFC_EXCHANGE_BUSY;
3097 spin_unlock_irqrestore(
3098 &phba->hbalock, iflag);
3099 }
3100 if (cmdiocbp->iocb_flag &
3101 LPFC_DRIVER_ABORTED) {
3102 /*
3103 * Clear LPFC_DRIVER_ABORTED
3104 * bit in case it was driver
3105 * initiated abort.
3106 */
3107 spin_lock_irqsave(
3108 &phba->hbalock, iflag);
3109 cmdiocbp->iocb_flag &=
3110 ~LPFC_DRIVER_ABORTED;
3111 spin_unlock_irqrestore(
3112 &phba->hbalock, iflag);
3113 cmdiocbp->iocb.ulpStatus =
3114 IOSTAT_LOCAL_REJECT;
3115 cmdiocbp->iocb.un.ulpWord[4] =
3116 IOERR_ABORT_REQUESTED;
3117 /*
3118 * For SLI4, irsiocb contains
3119 * NO_XRI in sli_xritag, it
3120 * shall not affect releasing
3121 * sgl (xri) process.
3122 */
3123 saveq->iocb.ulpStatus =
3124 IOSTAT_LOCAL_REJECT;
3125 saveq->iocb.un.ulpWord[4] =
3126 IOERR_SLI_ABORTED;
3127 spin_lock_irqsave(
3128 &phba->hbalock, iflag);
3129 saveq->iocb_flag |=
3130 LPFC_DELAY_MEM_FREE;
3131 spin_unlock_irqrestore(
3132 &phba->hbalock, iflag);
3133 }
07951076 3134 }
dea3101e 3135 }
2e0fef85 3136 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
3137 } else
3138 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 3139 } else {
3140 /*
3141 * Unknown initiating command based on the response iotag.
3142 * This could be the case on the ELS ring because of
3143 * lpfc_els_abort().
3144 */
3145 if (pring->ringno != LPFC_ELS_RING) {
3146 /*
3147 * Ring <ringno> handler: unexpected completion IoTag
3148 * <IoTag>
3149 */
a257bf90 3150 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
3151 "0322 Ring %d handler: "
3152 "unexpected completion IoTag x%x "
3153 "Data: x%x x%x x%x x%x\n",
3154 pring->ringno,
3155 saveq->iocb.ulpIoTag,
3156 saveq->iocb.ulpStatus,
3157 saveq->iocb.un.ulpWord[4],
3158 saveq->iocb.ulpCommand,
3159 saveq->iocb.ulpContext);
dea3101e 3160 }
3161 }
68876920 3162
dea3101e 3163 return rc;
3164}
3165
e59058c4 3166/**
3621a710 3167 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
3168 * @phba: Pointer to HBA context object.
3169 * @pring: Pointer to driver SLI ring object.
3170 *
3171 * This function is called from the iocb ring event handlers when
3172 * put pointer is ahead of the get pointer for a ring. This function signal
3173 * an error attention condition to the worker thread and the worker
3174 * thread will transition the HBA to offline state.
3175 **/
2e0fef85
JS
3176static void
3177lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 3178{
34b02dcd 3179 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 3180 /*
025dfdaf 3181 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
3182 * rsp ring <portRspMax>
3183 */
3184 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3185 "0312 Ring %d handler: portRspPut %d "
025dfdaf 3186 "is bigger than rsp ring %d\n",
e8b62011 3187 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 3188 pring->sli.sli3.numRiocb);
875fbdfe 3189
2e0fef85 3190 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
3191
3192 /*
3193 * All error attention handlers are posted to
3194 * worker thread
3195 */
3196 phba->work_ha |= HA_ERATT;
3197 phba->work_hs = HS_FFER3;
92d7f7b0 3198
5e9d9b82 3199 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3200
3201 return;
3202}
3203
9399627f 3204/**
3621a710 3205 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3206 * @ptr: Pointer to address of HBA context object.
3207 *
3208 * This function is invoked by the Error Attention polling timer when the
3209 * timer times out. It will check the SLI Error Attention register for
3210 * possible attention events. If so, it will post an Error Attention event
3211 * and wake up worker thread to process it. Otherwise, it will set up the
3212 * Error Attention polling timer for the next poll.
3213 **/
f22eb4d3 3214void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3215{
3216 struct lpfc_hba *phba;
eb016566 3217 uint32_t eratt = 0;
aa6fbb75 3218 uint64_t sli_intr, cnt;
9399627f 3219
f22eb4d3 3220 phba = from_timer(phba, t, eratt_poll);
9399627f 3221
aa6fbb75
JS
3222 /* Here we will also keep track of interrupts per sec of the hba */
3223 sli_intr = phba->sli.slistat.sli_intr;
3224
3225 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3226 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3227 sli_intr);
3228 else
3229 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3230
65791f1f
JS
3231 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3232 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3233 phba->sli.slistat.sli_ips = cnt;
3234
3235 phba->sli.slistat.sli_prev_intr = sli_intr;
3236
9399627f
JS
3237 /* Check chip HA register for error event */
3238 eratt = lpfc_sli_check_eratt(phba);
3239
3240 if (eratt)
3241 /* Tell the worker thread there is work to do */
3242 lpfc_worker_wake_up(phba);
3243 else
3244 /* Restart the timer for next eratt poll */
256ec0d0
JS
3245 mod_timer(&phba->eratt_poll,
3246 jiffies +
65791f1f 3247 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3248 return;
3249}
3250
875fbdfe 3251
e59058c4 3252/**
3621a710 3253 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3254 * @phba: Pointer to HBA context object.
3255 * @pring: Pointer to driver SLI ring object.
3256 * @mask: Host attention register mask for this ring.
3257 *
3258 * This function is called from the interrupt context when there is a ring
3259 * event for the fcp ring. The caller does not hold any lock.
3260 * The function processes each response iocb in the response ring until it
25985edc 3261 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3262 * LE bit set. The function will call the completion handler of the command iocb
3263 * if the response iocb indicates a completion for a command iocb or it is
3264 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3265 * function if this is an unsolicited iocb.
dea3101e 3266 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3267 * to check it explicitly.
3268 */
3269int
2e0fef85
JS
3270lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3271 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3272{
34b02dcd 3273 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3274 IOCB_t *irsp = NULL;
87f6eaff 3275 IOCB_t *entry = NULL;
dea3101e 3276 struct lpfc_iocbq *cmdiocbq = NULL;
3277 struct lpfc_iocbq rspiocbq;
dea3101e 3278 uint32_t status;
3279 uint32_t portRspPut, portRspMax;
3280 int rc = 1;
3281 lpfc_iocb_type type;
3282 unsigned long iflag;
3283 uint32_t rsp_cmpl = 0;
dea3101e 3284
2e0fef85 3285 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3286 pring->stats.iocb_event++;
3287
dea3101e 3288 /*
3289 * The next available response entry should never exceed the maximum
3290 * entries. If it does, treat it as an adapter hardware error.
3291 */
7e56aa25 3292 portRspMax = pring->sli.sli3.numRiocb;
dea3101e 3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3295 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3296 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 3297 return 1;
3298 }
45ed1190
JS
3299 if (phba->fcp_ring_in_use) {
3300 spin_unlock_irqrestore(&phba->hbalock, iflag);
3301 return 1;
3302 } else
3303 phba->fcp_ring_in_use = 1;
dea3101e 3304
3305 rmb();
7e56aa25 3306 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3307 /*
3308 * Fetch an entry off the ring and copy it into a local data
3309 * structure. The copy involves a byte-swap since the
3310 * network byte order and pci byte orders are different.
3311 */
ed957684 3312 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3313 phba->last_completion_time = jiffies;
875fbdfe 3314
7e56aa25
JS
3315 if (++pring->sli.sli3.rspidx >= portRspMax)
3316 pring->sli.sli3.rspidx = 0;
875fbdfe 3317
87f6eaff
JSEC
3318 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3319 (uint32_t *) &rspiocbq.iocb,
ed957684 3320 phba->iocb_rsp_size);
a4bc3379 3321 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3322 irsp = &rspiocbq.iocb;
3323
dea3101e 3324 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3325 pring->stats.iocb_rsp++;
3326 rsp_cmpl++;
3327
3328 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3329 /*
3330 * If resource errors reported from HBA, reduce
3331 * queuedepths of the SCSI device.
3332 */
3333 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3334 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3335 IOERR_NO_RESOURCES)) {
92d7f7b0 3336 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3337 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3338 spin_lock_irqsave(&phba->hbalock, iflag);
3339 }
3340
dea3101e 3341 /* Rsp ring <ringno> error: IOCB */
3342 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3343 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3344 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3345 pring->ringno,
92d7f7b0
JS
3346 irsp->un.ulpWord[0],
3347 irsp->un.ulpWord[1],
3348 irsp->un.ulpWord[2],
3349 irsp->un.ulpWord[3],
3350 irsp->un.ulpWord[4],
3351 irsp->un.ulpWord[5],
d7c255b2
JS
3352 *(uint32_t *)&irsp->un1,
3353 *((uint32_t *)&irsp->un1 + 1));
dea3101e 3354 }
3355
3356 switch (type) {
3357 case LPFC_ABORT_IOCB:
3358 case LPFC_SOL_IOCB:
3359 /*
3360 * Idle exchange closed via ABTS from port. No iocb
3361 * resources need to be recovered.
3362 */
3363 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3364 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3365 "0333 IOCB cmd 0x%x"
dca9479b 3366 " processed. Skipping"
92d7f7b0 3367 " completion\n",
dca9479b 3368 irsp->ulpCommand);
dea3101e 3369 break;
3370 }
3371
604a3e30
JB
3372 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3373 &rspiocbq);
0f65ff68
JS
3374 if (unlikely(!cmdiocbq))
3375 break;
3376 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3377 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3378 if (cmdiocbq->iocb_cmpl) {
3379 spin_unlock_irqrestore(&phba->hbalock, iflag);
3380 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3381 &rspiocbq);
3382 spin_lock_irqsave(&phba->hbalock, iflag);
3383 }
dea3101e 3384 break;
a4bc3379 3385 case LPFC_UNSOL_IOCB:
2e0fef85 3386 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3387 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3388 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3389 break;
dea3101e 3390 default:
3391 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3392 char adaptermsg[LPFC_MAX_ADPTMSG];
3393 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3394 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3395 MAX_MSG_DATA);
898eb71c
JP
3396 dev_warn(&((phba->pcidev)->dev),
3397 "lpfc%d: %s\n",
dea3101e 3398 phba->brd_no, adaptermsg);
3399 } else {
3400 /* Unknown IOCB command */
3401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3402 "0334 Unknown IOCB command "
92d7f7b0 3403 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3404 type, irsp->ulpCommand,
92d7f7b0
JS
3405 irsp->ulpStatus,
3406 irsp->ulpIoTag,
3407 irsp->ulpContext);
dea3101e 3408 }
3409 break;
3410 }
3411
3412 /*
3413 * The response IOCB has been processed. Update the ring
3414 * pointer in SLIM. If the port response put pointer has not
3415 * been updated, sync the pgp->rspPutInx and fetch the new port
3416 * response put pointer.
3417 */
7e56aa25
JS
3418 writel(pring->sli.sli3.rspidx,
3419 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3420
7e56aa25 3421 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e 3422 portRspPut = le32_to_cpu(pgp->rspPutInx);
3423 }
3424
3425 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3426 pring->stats.iocb_rsp_full++;
3427 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3428 writel(status, phba->CAregaddr);
3429 readl(phba->CAregaddr);
3430 }
3431 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3432 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3433 pring->stats.iocb_cmd_empty++;
3434
3435 /* Force update of the local copy of cmdGetInx */
7e56aa25 3436 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 3437 lpfc_sli_resume_iocb(phba, pring);
3438
3439 if ((pring->lpfc_sli_cmd_available))
3440 (pring->lpfc_sli_cmd_available) (phba, pring);
3441
3442 }
3443
45ed1190 3444 phba->fcp_ring_in_use = 0;
2e0fef85 3445 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 3446 return rc;
3447}
3448
e59058c4 3449/**
3772a991
JS
3450 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3451 * @phba: Pointer to HBA context object.
3452 * @pring: Pointer to driver SLI ring object.
3453 * @rspiocbp: Pointer to driver response IOCB object.
3454 *
3455 * This function is called from the worker thread when there is a slow-path
3456 * response IOCB to process. This function chains all the response iocbs until
3457 * seeing the iocb with the LE bit set. The function will call
3458 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3459 * completion of a command iocb. The function will call the
3460 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3461 * The function frees the resources or calls the completion handler if this
3462 * iocb is an abort completion. The function returns NULL when the response
3463 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3464 * this function shall chain the iocb on to the iocb_continueq and return the
3465 * response iocb passed in.
3466 **/
3467static struct lpfc_iocbq *
3468lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3469 struct lpfc_iocbq *rspiocbp)
3470{
3471 struct lpfc_iocbq *saveq;
3472 struct lpfc_iocbq *cmdiocbp;
3473 struct lpfc_iocbq *next_iocb;
3474 IOCB_t *irsp = NULL;
3475 uint32_t free_saveq;
3476 uint8_t iocb_cmd_type;
3477 lpfc_iocb_type type;
3478 unsigned long iflag;
3479 int rc;
3480
3481 spin_lock_irqsave(&phba->hbalock, iflag);
3482 /* First add the response iocb to the countinueq list */
3483 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3484 pring->iocb_continueq_cnt++;
3485
70f23fd6 3486 /* Now, determine whether the list is completed for processing */
3772a991
JS
3487 irsp = &rspiocbp->iocb;
3488 if (irsp->ulpLe) {
3489 /*
3490 * By default, the driver expects to free all resources
3491 * associated with this iocb completion.
3492 */
3493 free_saveq = 1;
3494 saveq = list_get_first(&pring->iocb_continueq,
3495 struct lpfc_iocbq, list);
3496 irsp = &(saveq->iocb);
3497 list_del_init(&pring->iocb_continueq);
3498 pring->iocb_continueq_cnt = 0;
3499
3500 pring->stats.iocb_rsp++;
3501
3502 /*
3503 * If resource errors reported from HBA, reduce
3504 * queuedepths of the SCSI device.
3505 */
3506 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3507 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3508 IOERR_NO_RESOURCES)) {
3772a991
JS
3509 spin_unlock_irqrestore(&phba->hbalock, iflag);
3510 phba->lpfc_rampdown_queue_depth(phba);
3511 spin_lock_irqsave(&phba->hbalock, iflag);
3512 }
3513
3514 if (irsp->ulpStatus) {
3515 /* Rsp ring <ringno> error: IOCB */
3516 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3517 "0328 Rsp Ring %d error: "
3518 "IOCB Data: "
3519 "x%x x%x x%x x%x "
3520 "x%x x%x x%x x%x "
3521 "x%x x%x x%x x%x "
3522 "x%x x%x x%x x%x\n",
3523 pring->ringno,
3524 irsp->un.ulpWord[0],
3525 irsp->un.ulpWord[1],
3526 irsp->un.ulpWord[2],
3527 irsp->un.ulpWord[3],
3528 irsp->un.ulpWord[4],
3529 irsp->un.ulpWord[5],
3530 *(((uint32_t *) irsp) + 6),
3531 *(((uint32_t *) irsp) + 7),
3532 *(((uint32_t *) irsp) + 8),
3533 *(((uint32_t *) irsp) + 9),
3534 *(((uint32_t *) irsp) + 10),
3535 *(((uint32_t *) irsp) + 11),
3536 *(((uint32_t *) irsp) + 12),
3537 *(((uint32_t *) irsp) + 13),
3538 *(((uint32_t *) irsp) + 14),
3539 *(((uint32_t *) irsp) + 15));
3540 }
3541
3542 /*
3543 * Fetch the IOCB command type and call the correct completion
3544 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3545 * get freed back to the lpfc_iocb_list by the discovery
3546 * kernel thread.
3547 */
3548 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3549 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3550 switch (type) {
3551 case LPFC_SOL_IOCB:
3552 spin_unlock_irqrestore(&phba->hbalock, iflag);
3553 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3554 spin_lock_irqsave(&phba->hbalock, iflag);
3555 break;
3556
3557 case LPFC_UNSOL_IOCB:
3558 spin_unlock_irqrestore(&phba->hbalock, iflag);
3559 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3560 spin_lock_irqsave(&phba->hbalock, iflag);
3561 if (!rc)
3562 free_saveq = 0;
3563 break;
3564
3565 case LPFC_ABORT_IOCB:
3566 cmdiocbp = NULL;
3567 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3568 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3569 saveq);
3570 if (cmdiocbp) {
3571 /* Call the specified completion routine */
3572 if (cmdiocbp->iocb_cmpl) {
3573 spin_unlock_irqrestore(&phba->hbalock,
3574 iflag);
3575 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3576 saveq);
3577 spin_lock_irqsave(&phba->hbalock,
3578 iflag);
3579 } else
3580 __lpfc_sli_release_iocbq(phba,
3581 cmdiocbp);
3582 }
3583 break;
3584
3585 case LPFC_UNKNOWN_IOCB:
3586 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3587 char adaptermsg[LPFC_MAX_ADPTMSG];
3588 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3589 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3590 MAX_MSG_DATA);
3591 dev_warn(&((phba->pcidev)->dev),
3592 "lpfc%d: %s\n",
3593 phba->brd_no, adaptermsg);
3594 } else {
3595 /* Unknown IOCB command */
3596 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3597 "0335 Unknown IOCB "
3598 "command Data: x%x "
3599 "x%x x%x x%x\n",
3600 irsp->ulpCommand,
3601 irsp->ulpStatus,
3602 irsp->ulpIoTag,
3603 irsp->ulpContext);
3604 }
3605 break;
3606 }
3607
3608 if (free_saveq) {
3609 list_for_each_entry_safe(rspiocbp, next_iocb,
3610 &saveq->list, list) {
61f35bff 3611 list_del_init(&rspiocbp->list);
3772a991
JS
3612 __lpfc_sli_release_iocbq(phba, rspiocbp);
3613 }
3614 __lpfc_sli_release_iocbq(phba, saveq);
3615 }
3616 rspiocbp = NULL;
3617 }
3618 spin_unlock_irqrestore(&phba->hbalock, iflag);
3619 return rspiocbp;
3620}
3621
3622/**
3623 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3624 * @phba: Pointer to HBA context object.
3625 * @pring: Pointer to driver SLI ring object.
3626 * @mask: Host attention register mask for this ring.
3627 *
3772a991
JS
3628 * This routine wraps the actual slow_ring event process routine from the
3629 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3630 **/
3772a991 3631void
2e0fef85
JS
3632lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3633 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3634{
3635 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3636}
3637
3638/**
3639 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3640 * @phba: Pointer to HBA context object.
3641 * @pring: Pointer to driver SLI ring object.
3642 * @mask: Host attention register mask for this ring.
3643 *
3644 * This function is called from the worker thread when there is a ring event
3645 * for non-fcp rings. The caller does not hold any lock. The function will
3646 * remove each response iocb in the response ring and calls the handle
3647 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3648 **/
3649static void
3650lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3651 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3652{
34b02dcd 3653 struct lpfc_pgp *pgp;
dea3101e 3654 IOCB_t *entry;
3655 IOCB_t *irsp = NULL;
3656 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3657 uint32_t portRspPut, portRspMax;
dea3101e 3658 unsigned long iflag;
3772a991 3659 uint32_t status;
dea3101e 3660
34b02dcd 3661 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3662 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3663 pring->stats.iocb_event++;
3664
dea3101e 3665 /*
3666 * The next available response entry should never exceed the maximum
3667 * entries. If it does, treat it as an adapter hardware error.
3668 */
7e56aa25 3669 portRspMax = pring->sli.sli3.numRiocb;
dea3101e 3670 portRspPut = le32_to_cpu(pgp->rspPutInx);
3671 if (portRspPut >= portRspMax) {
3672 /*
025dfdaf 3673 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 3674 * rsp ring <portRspMax>
3675 */
ed957684 3676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3677 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3678 "is bigger than rsp ring %d\n",
e8b62011 3679 pring->ringno, portRspPut, portRspMax);
dea3101e 3680
2e0fef85
JS
3681 phba->link_state = LPFC_HBA_ERROR;
3682 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 3683
3684 phba->work_hs = HS_FFER3;
3685 lpfc_handle_eratt(phba);
3686
3772a991 3687 return;
dea3101e 3688 }
3689
3690 rmb();
7e56aa25 3691 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e 3692 /*
3693 * Build a completion list and call the appropriate handler.
3694 * The process is to get the next available response iocb, get
3695 * a free iocb from the list, copy the response data into the
3696 * free iocb, insert to the continuation list, and update the
3697 * next response index to slim. This process makes response
3698 * iocb's in the ring available to DMA as fast as possible but
3699 * pays a penalty for a copy operation. Since the iocb is
3700 * only 32 bytes, this penalty is considered small relative to
3701 * the PCI reads for register values and a slim write. When
3702 * the ulpLe field is set, the entire Command has been
3703 * received.
3704 */
ed957684
JS
3705 entry = lpfc_resp_iocb(phba, pring);
3706
858c9f6c 3707 phba->last_completion_time = jiffies;
2e0fef85 3708 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 3709 if (rspiocbp == NULL) {
3710 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3711 "completion.\n", __func__);
dea3101e 3712 break;
3713 }
3714
ed957684
JS
3715 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3716 phba->iocb_rsp_size);
dea3101e 3717 irsp = &rspiocbp->iocb;
3718
7e56aa25
JS
3719 if (++pring->sli.sli3.rspidx >= portRspMax)
3720 pring->sli.sli3.rspidx = 0;
dea3101e 3721
a58cbd52
JS
3722 if (pring->ringno == LPFC_ELS_RING) {
3723 lpfc_debugfs_slow_ring_trc(phba,
3724 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3725 *(((uint32_t *) irsp) + 4),
3726 *(((uint32_t *) irsp) + 6),
3727 *(((uint32_t *) irsp) + 7));
3728 }
3729
7e56aa25
JS
3730 writel(pring->sli.sli3.rspidx,
3731 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3732
3772a991
JS
3733 spin_unlock_irqrestore(&phba->hbalock, iflag);
3734 /* Handle the response IOCB */
3735 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3736 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3737
3738 /*
3739 * If the port response put pointer has not been updated, sync
3740 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3741 * response put pointer.
3742 */
7e56aa25 3743 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e 3744 portRspPut = le32_to_cpu(pgp->rspPutInx);
3745 }
7e56aa25 3746 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3747
92d7f7b0 3748 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 3749 /* At least one response entry has been freed */
3750 pring->stats.iocb_rsp_full++;
3751 /* SET RxRE_RSP in Chip Att register */
3752 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3753 writel(status, phba->CAregaddr);
3754 readl(phba->CAregaddr); /* flush */
3755 }
3756 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3757 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3758 pring->stats.iocb_cmd_empty++;
3759
3760 /* Force update of the local copy of cmdGetInx */
7e56aa25 3761 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 3762 lpfc_sli_resume_iocb(phba, pring);
3763
3764 if ((pring->lpfc_sli_cmd_available))
3765 (pring->lpfc_sli_cmd_available) (phba, pring);
3766
3767 }
3768
2e0fef85 3769 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3770 return;
dea3101e 3771}
3772
4f774513
JS
3773/**
3774 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3775 * @phba: Pointer to HBA context object.
3776 * @pring: Pointer to driver SLI ring object.
3777 * @mask: Host attention register mask for this ring.
3778 *
3779 * This function is called from the worker thread when there is a pending
3780 * ELS response iocb on the driver internal slow-path response iocb worker
3781 * queue. The caller does not hold any lock. The function will remove each
3782 * response iocb from the response worker queue and calls the handle
3783 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3784 **/
3785static void
3786lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3787 struct lpfc_sli_ring *pring, uint32_t mask)
3788{
3789 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3790 struct hbq_dmabuf *dmabuf;
3791 struct lpfc_cq_event *cq_event;
4f774513
JS
3792 unsigned long iflag;
3793
45ed1190
JS
3794 spin_lock_irqsave(&phba->hbalock, iflag);
3795 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3796 spin_unlock_irqrestore(&phba->hbalock, iflag);
3797 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3798 /* Get the response iocb from the head of work queue */
3799 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3800 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3801 cq_event, struct lpfc_cq_event, list);
4f774513 3802 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3803
3804 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3805 case CQE_CODE_COMPL_WQE:
3806 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3807 cq_event);
45ed1190
JS
3808 /* Translate ELS WCQE to response IOCBQ */
3809 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3810 irspiocbq);
3811 if (irspiocbq)
3812 lpfc_sli_sp_handle_rspiocb(phba, pring,
3813 irspiocbq);
4d9ab994
JS
3814 break;
3815 case CQE_CODE_RECEIVE:
7851fe2c 3816 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3817 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3818 cq_event);
3819 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3820 break;
3821 default:
3822 break;
3823 }
4f774513
JS
3824 }
3825}
3826
e59058c4 3827/**
3621a710 3828 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3829 * @phba: Pointer to HBA context object.
3830 * @pring: Pointer to driver SLI ring object.
3831 *
3832 * This function aborts all iocbs in the given ring and frees all the iocb
3833 * objects in txq. This function issues an abort iocb for all the iocb commands
3834 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3835 * the return of this function. The caller is not required to hold any locks.
3836 **/
2e0fef85 3837void
dea3101e 3838lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3839{
2534ba75 3840 LIST_HEAD(completions);
dea3101e 3841 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3842
92d7f7b0
JS
3843 if (pring->ringno == LPFC_ELS_RING) {
3844 lpfc_fabric_abort_hba(phba);
3845 }
3846
dea3101e 3847 /* Error everything on txq and txcmplq
3848 * First do the txq.
3849 */
db55fba8
JS
3850 if (phba->sli_rev >= LPFC_SLI_REV4) {
3851 spin_lock_irq(&pring->ring_lock);
3852 list_splice_init(&pring->txq, &completions);
3853 pring->txq_cnt = 0;
3854 spin_unlock_irq(&pring->ring_lock);
dea3101e 3855
db55fba8
JS
3856 spin_lock_irq(&phba->hbalock);
3857 /* Next issue ABTS for everything on the txcmplq */
3858 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3859 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3860 spin_unlock_irq(&phba->hbalock);
3861 } else {
3862 spin_lock_irq(&phba->hbalock);
3863 list_splice_init(&pring->txq, &completions);
3864 pring->txq_cnt = 0;
dea3101e 3865
db55fba8
JS
3866 /* Next issue ABTS for everything on the txcmplq */
3867 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3868 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3869 spin_unlock_irq(&phba->hbalock);
3870 }
dea3101e 3871
a257bf90
JS
3872 /* Cancel all the IOCBs from the completions list */
3873 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3874 IOERR_SLI_ABORTED);
dea3101e 3875}
3876
895427bd
JS
3877/**
3878 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3879 * @phba: Pointer to HBA context object.
3880 * @pring: Pointer to driver SLI ring object.
3881 *
3882 * This function aborts all iocbs in the given ring and frees all the iocb
3883 * objects in txq. This function issues an abort iocb for all the iocb commands
3884 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3885 * the return of this function. The caller is not required to hold any locks.
3886 **/
3887void
3888lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3889{
3890 LIST_HEAD(completions);
3891 struct lpfc_iocbq *iocb, *next_iocb;
3892
3893 if (pring->ringno == LPFC_ELS_RING)
3894 lpfc_fabric_abort_hba(phba);
3895
3896 spin_lock_irq(&phba->hbalock);
3897 /* Next issue ABTS for everything on the txcmplq */
3898 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3899 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3900 spin_unlock_irq(&phba->hbalock);
3901}
3902
3903
db55fba8
JS
3904/**
3905 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3906 * @phba: Pointer to HBA context object.
3907 * @pring: Pointer to driver SLI ring object.
3908 *
3909 * This function aborts all iocbs in FCP rings and frees all the iocb
3910 * objects in txq. This function issues an abort iocb for all the iocb commands
3911 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3912 * the return of this function. The caller is not required to hold any locks.
3913 **/
3914void
3915lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3916{
3917 struct lpfc_sli *psli = &phba->sli;
3918 struct lpfc_sli_ring *pring;
3919 uint32_t i;
3920
3921 /* Look on all the FCP Rings for the iotag */
3922 if (phba->sli_rev >= LPFC_SLI_REV4) {
3923 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3924 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3925 lpfc_sli_abort_iocb_ring(phba, pring);
3926 }
3927 } else {
895427bd 3928 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3929 lpfc_sli_abort_iocb_ring(phba, pring);
3930 }
3931}
3932
895427bd
JS
3933/**
3934 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3935 * @phba: Pointer to HBA context object.
3936 *
3937 * This function aborts all wqes in NVME rings. This function issues an
3938 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3939 * the txcmplq is not guaranteed to complete before the return of this
3940 * function. The caller is not required to hold any locks.
3941 **/
3942void
3943lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3944{
3945 struct lpfc_sli_ring *pring;
3946 uint32_t i;
3947
3948 if (phba->sli_rev < LPFC_SLI_REV4)
3949 return;
3950
3951 /* Abort all IO on each NVME ring. */
3952 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3953 pring = phba->sli4_hba.nvme_wq[i]->pring;
3954 lpfc_sli_abort_wqe_ring(phba, pring);
3955 }
3956}
3957
db55fba8 3958
a8e497d5 3959/**
3621a710 3960 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3961 * @phba: Pointer to HBA context object.
3962 *
3963 * This function flushes all iocbs in the fcp ring and frees all the iocb
3964 * objects in txq and txcmplq. This function will not issue abort iocbs
3965 * for all the iocb commands in txcmplq, they will just be returned with
3966 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3967 * slot has been permanently disabled.
3968 **/
3969void
3970lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3971{
3972 LIST_HEAD(txq);
3973 LIST_HEAD(txcmplq);
a8e497d5
JS
3974 struct lpfc_sli *psli = &phba->sli;
3975 struct lpfc_sli_ring *pring;
db55fba8 3976 uint32_t i;
c1dd9111 3977 struct lpfc_iocbq *piocb, *next_iocb;
a8e497d5
JS
3978
3979 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3980 /* Indicate the I/O queues are flushed */
3981 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3982 spin_unlock_irq(&phba->hbalock);
3983
db55fba8
JS
3984 /* Look on all the FCP Rings for the iotag */
3985 if (phba->sli_rev >= LPFC_SLI_REV4) {
3986 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3987 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3988
3989 spin_lock_irq(&pring->ring_lock);
3990 /* Retrieve everything on txq */
3991 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
3992 list_for_each_entry_safe(piocb, next_iocb,
3993 &pring->txcmplq, list)
3994 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
3995 /* Retrieve everything on the txcmplq */
3996 list_splice_init(&pring->txcmplq, &txcmplq);
3997 pring->txq_cnt = 0;
3998 pring->txcmplq_cnt = 0;
3999 spin_unlock_irq(&pring->ring_lock);
4000
4001 /* Flush the txq */
4002 lpfc_sli_cancel_iocbs(phba, &txq,
4003 IOSTAT_LOCAL_REJECT,
4004 IOERR_SLI_DOWN);
4005 /* Flush the txcmpq */
4006 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4007 IOSTAT_LOCAL_REJECT,
4008 IOERR_SLI_DOWN);
4009 }
4010 } else {
895427bd 4011 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 4012
db55fba8
JS
4013 spin_lock_irq(&phba->hbalock);
4014 /* Retrieve everything on txq */
4015 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
4016 list_for_each_entry_safe(piocb, next_iocb,
4017 &pring->txcmplq, list)
4018 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
4019 /* Retrieve everything on the txcmplq */
4020 list_splice_init(&pring->txcmplq, &txcmplq);
4021 pring->txq_cnt = 0;
4022 pring->txcmplq_cnt = 0;
4023 spin_unlock_irq(&phba->hbalock);
4024
4025 /* Flush the txq */
4026 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4027 IOERR_SLI_DOWN);
4028 /* Flush the txcmpq */
4029 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4030 IOERR_SLI_DOWN);
4031 }
a8e497d5
JS
4032}
4033
895427bd
JS
4034/**
4035 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function flushes all wqes in the nvme rings and frees all resources
4039 * in the txcmplq. This function does not issue abort wqes for the IO
4040 * commands in txcmplq, they will just be returned with
4041 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4042 * slot has been permanently disabled.
4043 **/
4044void
4045lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4046{
4047 LIST_HEAD(txcmplq);
4048 struct lpfc_sli_ring *pring;
4049 uint32_t i;
c1dd9111 4050 struct lpfc_iocbq *piocb, *next_iocb;
895427bd
JS
4051
4052 if (phba->sli_rev < LPFC_SLI_REV4)
4053 return;
4054
4055 /* Hint to other driver operations that a flush is in progress. */
4056 spin_lock_irq(&phba->hbalock);
4057 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4058 spin_unlock_irq(&phba->hbalock);
4059
4060 /* Cycle through all NVME rings and complete each IO with
4061 * a local driver reason code. This is a flush so no
4062 * abort exchange to FW.
4063 */
4064 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4065 pring = phba->sli4_hba.nvme_wq[i]->pring;
4066
895427bd 4067 spin_lock_irq(&pring->ring_lock);
c1dd9111
JS
4068 list_for_each_entry_safe(piocb, next_iocb,
4069 &pring->txcmplq, list)
4070 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4071 /* Retrieve everything on the txcmplq */
895427bd
JS
4072 list_splice_init(&pring->txcmplq, &txcmplq);
4073 pring->txcmplq_cnt = 0;
4074 spin_unlock_irq(&pring->ring_lock);
4075
4076 /* Flush the txcmpq &&&PAE */
4077 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4078 IOSTAT_LOCAL_REJECT,
4079 IOERR_SLI_DOWN);
4080 }
4081}
4082
e59058c4 4083/**
3772a991 4084 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
4085 * @phba: Pointer to HBA context object.
4086 * @mask: Bit mask to be checked.
4087 *
4088 * This function reads the host status register and compares
4089 * with the provided bit mask to check if HBA completed
4090 * the restart. This function will wait in a loop for the
4091 * HBA to complete restart. If the HBA does not restart within
4092 * 15 iterations, the function will reset the HBA again. The
4093 * function returns 1 when HBA fail to restart otherwise returns
4094 * zero.
4095 **/
3772a991
JS
4096static int
4097lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 4098{
41415862
JW
4099 uint32_t status;
4100 int i = 0;
4101 int retval = 0;
dea3101e 4102
41415862 4103 /* Read the HBA Host Status Register */
9940b97b
JS
4104 if (lpfc_readl(phba->HSregaddr, &status))
4105 return 1;
dea3101e 4106
41415862
JW
4107 /*
4108 * Check status register every 100ms for 5 retries, then every
4109 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4110 * every 2.5 sec for 4.
4111 * Break our of the loop if errors occurred during init.
4112 */
4113 while (((status & mask) != mask) &&
4114 !(status & HS_FFERM) &&
4115 i++ < 20) {
dea3101e 4116
41415862
JW
4117 if (i <= 5)
4118 msleep(10);
4119 else if (i <= 10)
4120 msleep(500);
4121 else
4122 msleep(2500);
dea3101e 4123
41415862 4124 if (i == 15) {
2e0fef85 4125 /* Do post */
92d7f7b0 4126 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
4127 lpfc_sli_brdrestart(phba);
4128 }
4129 /* Read the HBA Host Status Register */
9940b97b
JS
4130 if (lpfc_readl(phba->HSregaddr, &status)) {
4131 retval = 1;
4132 break;
4133 }
41415862 4134 }
dea3101e 4135
41415862
JW
4136 /* Check to see if any errors occurred during init */
4137 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
4138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4139 "2751 Adapter failed to restart, "
4140 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4141 status,
4142 readl(phba->MBslimaddr + 0xa8),
4143 readl(phba->MBslimaddr + 0xac));
2e0fef85 4144 phba->link_state = LPFC_HBA_ERROR;
41415862 4145 retval = 1;
dea3101e 4146 }
dea3101e 4147
41415862
JW
4148 return retval;
4149}
dea3101e 4150
da0436e9
JS
4151/**
4152 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4153 * @phba: Pointer to HBA context object.
4154 * @mask: Bit mask to be checked.
4155 *
4156 * This function checks the host status register to check if HBA is
4157 * ready. This function will wait in a loop for the HBA to be ready
4158 * If the HBA is not ready , the function will will reset the HBA PCI
4159 * function again. The function returns 1 when HBA fail to be ready
4160 * otherwise returns zero.
4161 **/
4162static int
4163lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4164{
4165 uint32_t status;
4166 int retval = 0;
4167
4168 /* Read the HBA Host Status Register */
4169 status = lpfc_sli4_post_status_check(phba);
4170
4171 if (status) {
4172 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4173 lpfc_sli_brdrestart(phba);
4174 status = lpfc_sli4_post_status_check(phba);
4175 }
4176
4177 /* Check to see if any errors occurred during init */
4178 if (status) {
4179 phba->link_state = LPFC_HBA_ERROR;
4180 retval = 1;
4181 } else
4182 phba->sli4_hba.intr_enable = 0;
4183
4184 return retval;
4185}
4186
4187/**
4188 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4189 * @phba: Pointer to HBA context object.
4190 * @mask: Bit mask to be checked.
4191 *
4192 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4193 * from the API jump table function pointer from the lpfc_hba struct.
4194 **/
4195int
4196lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4197{
4198 return phba->lpfc_sli_brdready(phba, mask);
4199}
4200
9290831f
JS
4201#define BARRIER_TEST_PATTERN (0xdeadbeef)
4202
e59058c4 4203/**
3621a710 4204 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
4205 * @phba: Pointer to HBA context object.
4206 *
1b51197d
JS
4207 * This function is called before resetting an HBA. This function is called
4208 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 4209 **/
2e0fef85 4210void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4211{
65a29c16
JS
4212 uint32_t __iomem *resp_buf;
4213 uint32_t __iomem *mbox_buf;
9290831f 4214 volatile uint32_t mbox;
9940b97b 4215 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4216 int i;
4217 uint8_t hdrtype;
4218
1c2ba475
JT
4219 lockdep_assert_held(&phba->hbalock);
4220
9290831f
JS
4221 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4222 if (hdrtype != 0x80 ||
4223 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4224 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4225 return;
4226
4227 /*
4228 * Tell the other part of the chip to suspend temporarily all
4229 * its DMA activity.
4230 */
65a29c16 4231 resp_buf = phba->MBslimaddr;
9290831f
JS
4232
4233 /* Disable the error attention */
9940b97b
JS
4234 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4235 return;
9290831f
JS
4236 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4237 readl(phba->HCregaddr); /* flush */
2e0fef85 4238 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4239
9940b97b
JS
4240 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4241 return;
4242 if (ha_copy & HA_ERATT) {
9290831f
JS
4243 /* Clear Chip error bit */
4244 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4245 phba->pport->stopped = 1;
9290831f
JS
4246 }
4247
4248 mbox = 0;
4249 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4250 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4251
4252 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4253 mbox_buf = phba->MBslimaddr;
9290831f
JS
4254 writel(mbox, mbox_buf);
4255
9940b97b
JS
4256 for (i = 0; i < 50; i++) {
4257 if (lpfc_readl((resp_buf + 1), &resp_data))
4258 return;
4259 if (resp_data != ~(BARRIER_TEST_PATTERN))
4260 mdelay(1);
4261 else
4262 break;
4263 }
4264 resp_data = 0;
4265 if (lpfc_readl((resp_buf + 1), &resp_data))
4266 return;
4267 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4268 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4269 phba->pport->stopped)
9290831f
JS
4270 goto restore_hc;
4271 else
4272 goto clear_errat;
4273 }
4274
4275 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4276 resp_data = 0;
4277 for (i = 0; i < 500; i++) {
4278 if (lpfc_readl(resp_buf, &resp_data))
4279 return;
4280 if (resp_data != mbox)
4281 mdelay(1);
4282 else
4283 break;
4284 }
9290831f
JS
4285
4286clear_errat:
4287
9940b97b
JS
4288 while (++i < 500) {
4289 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4290 return;
4291 if (!(ha_copy & HA_ERATT))
4292 mdelay(1);
4293 else
4294 break;
4295 }
9290831f
JS
4296
4297 if (readl(phba->HAregaddr) & HA_ERATT) {
4298 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4299 phba->pport->stopped = 1;
9290831f
JS
4300 }
4301
4302restore_hc:
2e0fef85 4303 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4304 writel(hc_copy, phba->HCregaddr);
4305 readl(phba->HCregaddr); /* flush */
4306}
4307
e59058c4 4308/**
3621a710 4309 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4310 * @phba: Pointer to HBA context object.
4311 *
4312 * This function issues a kill_board mailbox command and waits for
4313 * the error attention interrupt. This function is called for stopping
4314 * the firmware processing. The caller is not required to hold any
4315 * locks. This function calls lpfc_hba_down_post function to free
4316 * any pending commands after the kill. The function will return 1 when it
4317 * fails to kill the board else will return 0.
4318 **/
41415862 4319int
2e0fef85 4320lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4321{
4322 struct lpfc_sli *psli;
4323 LPFC_MBOXQ_t *pmb;
4324 uint32_t status;
4325 uint32_t ha_copy;
4326 int retval;
4327 int i = 0;
dea3101e 4328
41415862 4329 psli = &phba->sli;
dea3101e 4330
41415862 4331 /* Kill HBA */
ed957684 4332 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4333 "0329 Kill HBA Data: x%x x%x\n",
4334 phba->pport->port_state, psli->sli_flag);
41415862 4335
98c9ea5c
JS
4336 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4337 if (!pmb)
41415862 4338 return 1;
41415862
JW
4339
4340 /* Disable the error attention */
2e0fef85 4341 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4342 if (lpfc_readl(phba->HCregaddr, &status)) {
4343 spin_unlock_irq(&phba->hbalock);
4344 mempool_free(pmb, phba->mbox_mem_pool);
4345 return 1;
4346 }
41415862
JW
4347 status &= ~HC_ERINT_ENA;
4348 writel(status, phba->HCregaddr);
4349 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4350 phba->link_flag |= LS_IGNORE_ERATT;
4351 spin_unlock_irq(&phba->hbalock);
41415862
JW
4352
4353 lpfc_kill_board(phba, pmb);
4354 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4355 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4356
4357 if (retval != MBX_SUCCESS) {
4358 if (retval != MBX_BUSY)
4359 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4361 "2752 KILL_BOARD command failed retval %d\n",
4362 retval);
2e0fef85
JS
4363 spin_lock_irq(&phba->hbalock);
4364 phba->link_flag &= ~LS_IGNORE_ERATT;
4365 spin_unlock_irq(&phba->hbalock);
41415862
JW
4366 return 1;
4367 }
4368
f4b4c68f
JS
4369 spin_lock_irq(&phba->hbalock);
4370 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4371 spin_unlock_irq(&phba->hbalock);
9290831f 4372
41415862
JW
4373 mempool_free(pmb, phba->mbox_mem_pool);
4374
4375 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4376 * attention every 100ms for 3 seconds. If we don't get ERATT after
4377 * 3 seconds we still set HBA_ERROR state because the status of the
4378 * board is now undefined.
4379 */
9940b97b
JS
4380 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4381 return 1;
41415862
JW
4382 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4383 mdelay(100);
9940b97b
JS
4384 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4385 return 1;
41415862
JW
4386 }
4387
4388 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4389 if (ha_copy & HA_ERATT) {
4390 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4391 phba->pport->stopped = 1;
9290831f 4392 }
2e0fef85 4393 spin_lock_irq(&phba->hbalock);
41415862 4394 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4395 psli->mbox_active = NULL;
2e0fef85
JS
4396 phba->link_flag &= ~LS_IGNORE_ERATT;
4397 spin_unlock_irq(&phba->hbalock);
41415862 4398
41415862 4399 lpfc_hba_down_post(phba);
2e0fef85 4400 phba->link_state = LPFC_HBA_ERROR;
41415862 4401
2e0fef85 4402 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 4403}
4404
e59058c4 4405/**
3772a991 4406 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function resets the HBA by writing HC_INITFF to the control
4410 * register. After the HBA resets, this function resets all the iocb ring
4411 * indices. This function disables PCI layer parity checking during
4412 * the reset.
4413 * This function returns 0 always.
4414 * The caller is not required to hold any locks.
4415 **/
41415862 4416int
2e0fef85 4417lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4418{
41415862 4419 struct lpfc_sli *psli;
dea3101e 4420 struct lpfc_sli_ring *pring;
41415862 4421 uint16_t cfg_value;
dea3101e 4422 int i;
dea3101e 4423
41415862 4424 psli = &phba->sli;
dea3101e 4425
41415862
JW
4426 /* Reset HBA */
4427 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4428 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4429 (phba->pport) ? phba->pport->port_state : 0,
4430 psli->sli_flag);
dea3101e 4431
4432 /* perform board reset */
4433 phba->fc_eventTag = 0;
4d9ab994 4434 phba->link_events = 0;
4492b739
JS
4435 if (phba->pport) {
4436 phba->pport->fc_myDID = 0;
4437 phba->pport->fc_prevDID = 0;
4438 }
dea3101e 4439
41415862
JW
4440 /* Turn off parity checking and serr during the physical reset */
4441 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4442 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4443 (cfg_value &
4444 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4445
3772a991
JS
4446 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4447
41415862
JW
4448 /* Now toggle INITFF bit in the Host Control Register */
4449 writel(HC_INITFF, phba->HCregaddr);
4450 mdelay(1);
4451 readl(phba->HCregaddr); /* flush */
4452 writel(0, phba->HCregaddr);
4453 readl(phba->HCregaddr); /* flush */
4454
4455 /* Restore PCI cmd register */
4456 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 4457
4458 /* Initialize relevant SLI info */
41415862 4459 for (i = 0; i < psli->num_rings; i++) {
895427bd 4460 pring = &psli->sli3_ring[i];
dea3101e 4461 pring->flag = 0;
7e56aa25
JS
4462 pring->sli.sli3.rspidx = 0;
4463 pring->sli.sli3.next_cmdidx = 0;
4464 pring->sli.sli3.local_getidx = 0;
4465 pring->sli.sli3.cmdidx = 0;
dea3101e 4466 pring->missbufcnt = 0;
4467 }
dea3101e 4468
2e0fef85 4469 phba->link_state = LPFC_WARM_START;
41415862
JW
4470 return 0;
4471}
4472
e59058c4 4473/**
da0436e9
JS
4474 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4475 * @phba: Pointer to HBA context object.
4476 *
4477 * This function resets a SLI4 HBA. This function disables PCI layer parity
4478 * checking during resets the device. The caller is not required to hold
4479 * any locks.
4480 *
4481 * This function returns 0 always.
4482 **/
4483int
4484lpfc_sli4_brdreset(struct lpfc_hba *phba)
4485{
4486 struct lpfc_sli *psli = &phba->sli;
4487 uint16_t cfg_value;
0293635e 4488 int rc = 0;
da0436e9
JS
4489
4490 /* Reset HBA */
4491 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4492 "0295 Reset HBA Data: x%x x%x x%x\n",
4493 phba->pport->port_state, psli->sli_flag,
4494 phba->hba_flag);
da0436e9
JS
4495
4496 /* perform board reset */
4497 phba->fc_eventTag = 0;
4d9ab994 4498 phba->link_events = 0;
da0436e9
JS
4499 phba->pport->fc_myDID = 0;
4500 phba->pport->fc_prevDID = 0;
4501
da0436e9
JS
4502 spin_lock_irq(&phba->hbalock);
4503 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4504 phba->fcf.fcf_flag = 0;
da0436e9
JS
4505 spin_unlock_irq(&phba->hbalock);
4506
0293635e
JS
4507 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4508 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4509 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4510 return rc;
4511 }
4512
da0436e9
JS
4513 /* Now physically reset the device */
4514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4515 "0389 Performing PCI function reset!\n");
be858b65
JS
4516
4517 /* Turn off parity checking and serr during the physical reset */
4518 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4519 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4520 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4521
88318816 4522 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4523 rc = lpfc_pci_function_reset(phba);
da0436e9 4524
be858b65
JS
4525 /* Restore PCI cmd register */
4526 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4527
27b01b82 4528 return rc;
da0436e9
JS
4529}
4530
4531/**
4532 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4533 * @phba: Pointer to HBA context object.
4534 *
4535 * This function is called in the SLI initialization code path to
4536 * restart the HBA. The caller is not required to hold any lock.
4537 * This function writes MBX_RESTART mailbox command to the SLIM and
4538 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4539 * function to free any pending commands. The function enables
4540 * POST only during the first initialization. The function returns zero.
4541 * The function does not guarantee completion of MBX_RESTART mailbox
4542 * command before the return of this function.
4543 **/
da0436e9
JS
4544static int
4545lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4546{
4547 MAILBOX_t *mb;
4548 struct lpfc_sli *psli;
41415862
JW
4549 volatile uint32_t word0;
4550 void __iomem *to_slim;
0d878419 4551 uint32_t hba_aer_enabled;
41415862 4552
2e0fef85 4553 spin_lock_irq(&phba->hbalock);
41415862 4554
0d878419
JS
4555 /* Take PCIe device Advanced Error Reporting (AER) state */
4556 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4557
41415862
JW
4558 psli = &phba->sli;
4559
4560 /* Restart HBA */
4561 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4562 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4563 (phba->pport) ? phba->pport->port_state : 0,
4564 psli->sli_flag);
41415862
JW
4565
4566 word0 = 0;
4567 mb = (MAILBOX_t *) &word0;
4568 mb->mbxCommand = MBX_RESTART;
4569 mb->mbxHc = 1;
4570
9290831f
JS
4571 lpfc_reset_barrier(phba);
4572
41415862
JW
4573 to_slim = phba->MBslimaddr;
4574 writel(*(uint32_t *) mb, to_slim);
4575 readl(to_slim); /* flush */
4576
4577 /* Only skip post after fc_ffinit is completed */
4492b739 4578 if (phba->pport && phba->pport->port_state)
41415862 4579 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4580 else
41415862 4581 word0 = 0; /* This is really setting up word1 */
65a29c16 4582 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4583 writel(*(uint32_t *) mb, to_slim);
4584 readl(to_slim); /* flush */
dea3101e 4585
41415862 4586 lpfc_sli_brdreset(phba);
4492b739
JS
4587 if (phba->pport)
4588 phba->pport->stopped = 0;
2e0fef85 4589 phba->link_state = LPFC_INIT_START;
da0436e9 4590 phba->hba_flag = 0;
2e0fef85 4591 spin_unlock_irq(&phba->hbalock);
41415862 4592
64ba8818
JS
4593 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4594 psli->stats_start = get_seconds();
4595
eaf15d5b
JS
4596 /* Give the INITFF and Post time to settle. */
4597 mdelay(100);
41415862 4598
0d878419
JS
4599 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4600 if (hba_aer_enabled)
4601 pci_disable_pcie_error_reporting(phba->pcidev);
4602
41415862 4603 lpfc_hba_down_post(phba);
dea3101e 4604
4605 return 0;
4606}
4607
da0436e9
JS
4608/**
4609 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4610 * @phba: Pointer to HBA context object.
4611 *
4612 * This function is called in the SLI initialization code path to restart
4613 * a SLI4 HBA. The caller is not required to hold any lock.
4614 * At the end of the function, it calls lpfc_hba_down_post function to
4615 * free any pending commands.
4616 **/
4617static int
4618lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4619{
4620 struct lpfc_sli *psli = &phba->sli;
75baf696 4621 uint32_t hba_aer_enabled;
27b01b82 4622 int rc;
da0436e9
JS
4623
4624 /* Restart HBA */
4625 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4626 "0296 Restart HBA Data: x%x x%x\n",
4627 phba->pport->port_state, psli->sli_flag);
4628
75baf696
JS
4629 /* Take PCIe device Advanced Error Reporting (AER) state */
4630 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4631
27b01b82 4632 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4633
4634 spin_lock_irq(&phba->hbalock);
4635 phba->pport->stopped = 0;
4636 phba->link_state = LPFC_INIT_START;
4637 phba->hba_flag = 0;
4638 spin_unlock_irq(&phba->hbalock);
4639
4640 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4641 psli->stats_start = get_seconds();
4642
75baf696
JS
4643 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4644 if (hba_aer_enabled)
4645 pci_disable_pcie_error_reporting(phba->pcidev);
4646
da0436e9 4647 lpfc_hba_down_post(phba);
569dbe84 4648 lpfc_sli4_queue_destroy(phba);
da0436e9 4649
27b01b82 4650 return rc;
da0436e9
JS
4651}
4652
4653/**
4654 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4655 * @phba: Pointer to HBA context object.
4656 *
4657 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4658 * API jump table function pointer from the lpfc_hba struct.
4659**/
4660int
4661lpfc_sli_brdrestart(struct lpfc_hba *phba)
4662{
4663 return phba->lpfc_sli_brdrestart(phba);
4664}
4665
e59058c4 4666/**
3621a710 4667 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4668 * @phba: Pointer to HBA context object.
4669 *
4670 * This function is called after a HBA restart to wait for successful
4671 * restart of the HBA. Successful restart of the HBA is indicated by
4672 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4673 * iteration, the function will restart the HBA again. The function returns
4674 * zero if HBA successfully restarted else returns negative error code.
4675 **/
4492b739 4676int
dea3101e 4677lpfc_sli_chipset_init(struct lpfc_hba *phba)
4678{
4679 uint32_t status, i = 0;
4680
4681 /* Read the HBA Host Status Register */
9940b97b
JS
4682 if (lpfc_readl(phba->HSregaddr, &status))
4683 return -EIO;
dea3101e 4684
4685 /* Check status register to see what current state is */
4686 i = 0;
4687 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4688
dcf2a4e0
JS
4689 /* Check every 10ms for 10 retries, then every 100ms for 90
4690 * retries, then every 1 sec for 50 retires for a total of
4691 * ~60 seconds before reset the board again and check every
4692 * 1 sec for 50 retries. The up to 60 seconds before the
4693 * board ready is required by the Falcon FIPS zeroization
4694 * complete, and any reset the board in between shall cause
4695 * restart of zeroization, further delay the board ready.
dea3101e 4696 */
dcf2a4e0 4697 if (i++ >= 200) {
dea3101e 4698 /* Adapter failed to init, timeout, status reg
4699 <status> */
ed957684 4700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4701 "0436 Adapter failed to init, "
09372820
JS
4702 "timeout, status reg x%x, "
4703 "FW Data: A8 x%x AC x%x\n", status,
4704 readl(phba->MBslimaddr + 0xa8),
4705 readl(phba->MBslimaddr + 0xac));
2e0fef85 4706 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4707 return -ETIMEDOUT;
4708 }
4709
4710 /* Check to see if any errors occurred during init */
4711 if (status & HS_FFERM) {
4712 /* ERROR: During chipset initialization */
4713 /* Adapter failed to init, chipset, status reg
4714 <status> */
ed957684 4715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4716 "0437 Adapter failed to init, "
09372820
JS
4717 "chipset, status reg x%x, "
4718 "FW Data: A8 x%x AC x%x\n", status,
4719 readl(phba->MBslimaddr + 0xa8),
4720 readl(phba->MBslimaddr + 0xac));
2e0fef85 4721 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4722 return -EIO;
4723 }
4724
dcf2a4e0 4725 if (i <= 10)
dea3101e 4726 msleep(10);
dcf2a4e0
JS
4727 else if (i <= 100)
4728 msleep(100);
4729 else
4730 msleep(1000);
dea3101e 4731
dcf2a4e0
JS
4732 if (i == 150) {
4733 /* Do post */
92d7f7b0 4734 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4735 lpfc_sli_brdrestart(phba);
dea3101e 4736 }
4737 /* Read the HBA Host Status Register */
9940b97b
JS
4738 if (lpfc_readl(phba->HSregaddr, &status))
4739 return -EIO;
dea3101e 4740 }
4741
4742 /* Check to see if any errors occurred during init */
4743 if (status & HS_FFERM) {
4744 /* ERROR: During chipset initialization */
4745 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4747 "0438 Adapter failed to init, chipset, "
09372820
JS
4748 "status reg x%x, "
4749 "FW Data: A8 x%x AC x%x\n", status,
4750 readl(phba->MBslimaddr + 0xa8),
4751 readl(phba->MBslimaddr + 0xac));
2e0fef85 4752 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4753 return -EIO;
4754 }
4755
4756 /* Clear all interrupt enable conditions */
4757 writel(0, phba->HCregaddr);
4758 readl(phba->HCregaddr); /* flush */
4759
4760 /* setup host attn register */
4761 writel(0xffffffff, phba->HAregaddr);
4762 readl(phba->HAregaddr); /* flush */
4763 return 0;
4764}
4765
e59058c4 4766/**
3621a710 4767 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4768 *
4769 * This function calculates and returns the number of HBQs required to be
4770 * configured.
4771 **/
78b2d852 4772int
ed957684
JS
4773lpfc_sli_hbq_count(void)
4774{
92d7f7b0 4775 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4776}
4777
e59058c4 4778/**
3621a710 4779 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4780 *
4781 * This function adds the number of hbq entries in every HBQ to get
4782 * the total number of hbq entries required for the HBA and returns
4783 * the total count.
4784 **/
ed957684
JS
4785static int
4786lpfc_sli_hbq_entry_count(void)
4787{
4788 int hbq_count = lpfc_sli_hbq_count();
4789 int count = 0;
4790 int i;
4791
4792 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4793 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4794 return count;
4795}
4796
e59058c4 4797/**
3621a710 4798 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4799 *
4800 * This function calculates amount of memory required for all hbq entries
4801 * to be configured and returns the total memory required.
4802 **/
dea3101e 4803int
ed957684
JS
4804lpfc_sli_hbq_size(void)
4805{
4806 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4807}
4808
e59058c4 4809/**
3621a710 4810 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4811 * @phba: Pointer to HBA context object.
4812 *
4813 * This function is called during the SLI initialization to configure
4814 * all the HBQs and post buffers to the HBQ. The caller is not
4815 * required to hold any locks. This function will return zero if successful
4816 * else it will return negative error code.
4817 **/
ed957684
JS
4818static int
4819lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4820{
4821 int hbq_count = lpfc_sli_hbq_count();
4822 LPFC_MBOXQ_t *pmb;
4823 MAILBOX_t *pmbox;
4824 uint32_t hbqno;
4825 uint32_t hbq_entry_index;
ed957684 4826
92d7f7b0
JS
4827 /* Get a Mailbox buffer to setup mailbox
4828 * commands for HBA initialization
4829 */
ed957684
JS
4830 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4831
4832 if (!pmb)
4833 return -ENOMEM;
4834
04c68496 4835 pmbox = &pmb->u.mb;
ed957684
JS
4836
4837 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4838 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4839 phba->hbq_in_use = 1;
ed957684
JS
4840
4841 hbq_entry_index = 0;
4842 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4843 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4844 phba->hbqs[hbqno].hbqPutIdx = 0;
4845 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4846 phba->hbqs[hbqno].entry_count =
92d7f7b0 4847 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4848 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4849 hbq_entry_index, pmb);
ed957684
JS
4850 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4851
4852 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4853 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4854 mbxStatus <status>, ring <num> */
4855
4856 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4857 LOG_SLI | LOG_VPORT,
e8b62011 4858 "1805 Adapter failed to init. "
ed957684 4859 "Data: x%x x%x x%x\n",
e8b62011 4860 pmbox->mbxCommand,
ed957684
JS
4861 pmbox->mbxStatus, hbqno);
4862
4863 phba->link_state = LPFC_HBA_ERROR;
4864 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4865 return -ENXIO;
ed957684
JS
4866 }
4867 }
4868 phba->hbq_count = hbq_count;
4869
ed957684
JS
4870 mempool_free(pmb, phba->mbox_mem_pool);
4871
92d7f7b0 4872 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4873 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4874 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4875 return 0;
4876}
4877
4f774513
JS
4878/**
4879 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4880 * @phba: Pointer to HBA context object.
4881 *
4882 * This function is called during the SLI initialization to configure
4883 * all the HBQs and post buffers to the HBQ. The caller is not
4884 * required to hold any locks. This function will return zero if successful
4885 * else it will return negative error code.
4886 **/
4887static int
4888lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4889{
4890 phba->hbq_in_use = 1;
895427bd
JS
4891 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4892 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4893 phba->hbq_count = 1;
895427bd 4894 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4895 /* Initially populate or replenish the HBQs */
4f774513
JS
4896 return 0;
4897}
4898
e59058c4 4899/**
3621a710 4900 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4901 * @phba: Pointer to HBA context object.
4902 * @sli_mode: sli mode - 2/3
4903 *
183b8021 4904 * This function is called by the sli initialization code path
e59058c4
JS
4905 * to issue config_port mailbox command. This function restarts the
4906 * HBA firmware and issues a config_port mailbox command to configure
4907 * the SLI interface in the sli mode specified by sli_mode
4908 * variable. The caller is not required to hold any locks.
4909 * The function returns 0 if successful, else returns negative error
4910 * code.
4911 **/
9399627f
JS
4912int
4913lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 4914{
4915 LPFC_MBOXQ_t *pmb;
4916 uint32_t resetcount = 0, rc = 0, done = 0;
4917
4918 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4919 if (!pmb) {
2e0fef85 4920 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4921 return -ENOMEM;
4922 }
4923
ed957684 4924 phba->sli_rev = sli_mode;
dea3101e 4925 while (resetcount < 2 && !done) {
2e0fef85 4926 spin_lock_irq(&phba->hbalock);
1c067a42 4927 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4928 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4929 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4930 lpfc_sli_brdrestart(phba);
dea3101e 4931 rc = lpfc_sli_chipset_init(phba);
4932 if (rc)
4933 break;
4934
2e0fef85 4935 spin_lock_irq(&phba->hbalock);
1c067a42 4936 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4937 spin_unlock_irq(&phba->hbalock);
dea3101e 4938 resetcount++;
4939
ed957684
JS
4940 /* Call pre CONFIG_PORT mailbox command initialization. A
4941 * value of 0 means the call was successful. Any other
4942 * nonzero value is a failure, but if ERESTART is returned,
4943 * the driver may reset the HBA and try again.
4944 */
dea3101e 4945 rc = lpfc_config_port_prep(phba);
4946 if (rc == -ERESTART) {
ed957684 4947 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4948 continue;
34b02dcd 4949 } else if (rc)
dea3101e 4950 break;
6d368e53 4951
2e0fef85 4952 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 4953 lpfc_config_port(phba, pmb);
4954 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4955 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4956 LPFC_SLI3_HBQ_ENABLED |
4957 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4958 LPFC_SLI3_BG_ENABLED |
4959 LPFC_SLI3_DSS_ENABLED);
ed957684 4960 if (rc != MBX_SUCCESS) {
dea3101e 4961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4962 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4963 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4964 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4965 spin_lock_irq(&phba->hbalock);
04c68496 4966 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4967 spin_unlock_irq(&phba->hbalock);
4968 rc = -ENXIO;
04c68496
JS
4969 } else {
4970 /* Allow asynchronous mailbox command to go through */
4971 spin_lock_irq(&phba->hbalock);
4972 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4973 spin_unlock_irq(&phba->hbalock);
ed957684 4974 done = 1;
cb69f7de
JS
4975
4976 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4977 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4978 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4979 "3110 Port did not grant ASABT\n");
04c68496 4980 }
dea3101e 4981 }
ed957684
JS
4982 if (!done) {
4983 rc = -EINVAL;
4984 goto do_prep_failed;
4985 }
04c68496
JS
4986 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4987 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4988 rc = -ENXIO;
4989 goto do_prep_failed;
4990 }
04c68496 4991 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4992 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4993 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4994 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4995 phba->max_vpi : phba->max_vports;
4996
34b02dcd
JS
4997 } else
4998 phba->max_vpi = 0;
bc73905a
JS
4999 phba->fips_level = 0;
5000 phba->fips_spec_rev = 0;
5001 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 5002 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
5003 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
5004 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
5005 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5006 "2850 Security Crypto Active. FIPS x%d "
5007 "(Spec Rev: x%d)",
5008 phba->fips_level, phba->fips_spec_rev);
5009 }
5010 if (pmb->u.mb.un.varCfgPort.sec_err) {
5011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5012 "2856 Config Port Security Crypto "
5013 "Error: x%x ",
5014 pmb->u.mb.un.varCfgPort.sec_err);
5015 }
04c68496 5016 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 5017 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 5018 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 5019 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
5020
5021 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5022 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6 5023
f44ac12f
JS
5024 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5025 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5026 phba->cfg_enable_bg = 0;
5027 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
e2a0a9d6
JS
5028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5029 "0443 Adapter did not grant "
5030 "BlockGuard\n");
f44ac12f 5031 }
e2a0a9d6 5032 }
34b02dcd 5033 } else {
8f34f4ce 5034 phba->hbq_get = NULL;
34b02dcd 5035 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 5036 phba->max_vpi = 0;
ed957684 5037 }
92d7f7b0 5038do_prep_failed:
ed957684
JS
5039 mempool_free(pmb, phba->mbox_mem_pool);
5040 return rc;
5041}
5042
e59058c4
JS
5043
5044/**
183b8021 5045 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
5046 * @phba: Pointer to HBA context object.
5047 *
183b8021
MY
5048 * This function is the main SLI initialization function. This function
5049 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
5050 * error attention handler code. Caller is not required to hold any
5051 * locks. This function issues config_port mailbox command to configure
5052 * the SLI, setup iocb rings and HBQ rings. In the end the function
5053 * calls the config_port_post function to issue init_link mailbox
5054 * command and to start the discovery. The function will return zero
5055 * if successful, else it will return negative error code.
5056 **/
ed957684
JS
5057int
5058lpfc_sli_hba_setup(struct lpfc_hba *phba)
5059{
5060 uint32_t rc;
6d368e53
JS
5061 int mode = 3, i;
5062 int longs;
ed957684 5063
12247e81 5064 switch (phba->cfg_sli_mode) {
ed957684 5065 case 2:
78b2d852 5066 if (phba->cfg_enable_npiv) {
92d7f7b0 5067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 5068 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 5069 "parameter (%d) to auto (0).\n",
12247e81 5070 phba->cfg_sli_mode);
92d7f7b0
JS
5071 break;
5072 }
ed957684
JS
5073 mode = 2;
5074 break;
5075 case 0:
5076 case 3:
5077 break;
5078 default:
92d7f7b0 5079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
5080 "1819 Unrecognized sli_mode parameter: %d.\n",
5081 phba->cfg_sli_mode);
ed957684
JS
5082
5083 break;
5084 }
b5c53958 5085 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 5086
9399627f
JS
5087 rc = lpfc_sli_config_port(phba, mode);
5088
12247e81 5089 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 5090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
5091 "1820 Unable to select SLI-3. "
5092 "Not supported by adapter.\n");
ed957684 5093 if (rc && mode != 2)
9399627f 5094 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
5095 else if (rc && mode == 2)
5096 rc = lpfc_sli_config_port(phba, 3);
ed957684 5097 if (rc)
dea3101e 5098 goto lpfc_sli_hba_setup_error;
5099
0d878419
JS
5100 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5101 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5102 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5103 if (!rc) {
5104 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5105 "2709 This device supports "
5106 "Advanced Error Reporting (AER)\n");
5107 spin_lock_irq(&phba->hbalock);
5108 phba->hba_flag |= HBA_AER_ENABLED;
5109 spin_unlock_irq(&phba->hbalock);
5110 } else {
5111 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5112 "2708 This device does not support "
b069d7eb
JS
5113 "Advanced Error Reporting (AER): %d\n",
5114 rc);
0d878419
JS
5115 phba->cfg_aer_support = 0;
5116 }
5117 }
5118
ed957684
JS
5119 if (phba->sli_rev == 3) {
5120 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5121 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
5122 } else {
5123 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5124 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 5125 phba->sli3_options = 0;
ed957684
JS
5126 }
5127
5128 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
5129 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5130 phba->sli_rev, phba->max_vpi);
ed957684 5131 rc = lpfc_sli_ring_map(phba);
dea3101e 5132
5133 if (rc)
5134 goto lpfc_sli_hba_setup_error;
5135
6d368e53
JS
5136 /* Initialize VPIs. */
5137 if (phba->sli_rev == LPFC_SLI_REV3) {
5138 /*
5139 * The VPI bitmask and physical ID array are allocated
5140 * and initialized once only - at driver load. A port
5141 * reset doesn't need to reinitialize this memory.
5142 */
5143 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5144 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5145 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5146 GFP_KERNEL);
5147 if (!phba->vpi_bmask) {
5148 rc = -ENOMEM;
5149 goto lpfc_sli_hba_setup_error;
5150 }
5151
5152 phba->vpi_ids = kzalloc(
5153 (phba->max_vpi+1) * sizeof(uint16_t),
5154 GFP_KERNEL);
5155 if (!phba->vpi_ids) {
5156 kfree(phba->vpi_bmask);
5157 rc = -ENOMEM;
5158 goto lpfc_sli_hba_setup_error;
5159 }
5160 for (i = 0; i < phba->max_vpi; i++)
5161 phba->vpi_ids[i] = i;
5162 }
5163 }
5164
9399627f 5165 /* Init HBQs */
ed957684
JS
5166 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5167 rc = lpfc_sli_hbq_setup(phba);
5168 if (rc)
5169 goto lpfc_sli_hba_setup_error;
5170 }
04c68496 5171 spin_lock_irq(&phba->hbalock);
dea3101e 5172 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 5173 spin_unlock_irq(&phba->hbalock);
dea3101e 5174
5175 rc = lpfc_config_port_post(phba);
5176 if (rc)
5177 goto lpfc_sli_hba_setup_error;
5178
ed957684
JS
5179 return rc;
5180
92d7f7b0 5181lpfc_sli_hba_setup_error:
2e0fef85 5182 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 5183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 5184 "0445 Firmware initialization failed\n");
dea3101e 5185 return rc;
5186}
5187
e59058c4 5188/**
da0436e9
JS
5189 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5190 * @phba: Pointer to HBA context object.
5191 * @mboxq: mailbox pointer.
5192 * This function issue a dump mailbox command to read config region
5193 * 23 and parse the records in the region and populate driver
5194 * data structure.
e59058c4 5195 **/
da0436e9 5196static int
ff78d8f9 5197lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 5198{
ff78d8f9 5199 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
5200 struct lpfc_dmabuf *mp;
5201 struct lpfc_mqe *mqe;
5202 uint32_t data_length;
5203 int rc;
dea3101e 5204
da0436e9
JS
5205 /* Program the default value of vlan_id and fc_map */
5206 phba->valid_vlan = 0;
5207 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5208 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5209 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 5210
ff78d8f9
JS
5211 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5212 if (!mboxq)
da0436e9
JS
5213 return -ENOMEM;
5214
ff78d8f9
JS
5215 mqe = &mboxq->u.mqe;
5216 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5217 rc = -ENOMEM;
5218 goto out_free_mboxq;
5219 }
5220
da0436e9
JS
5221 mp = (struct lpfc_dmabuf *) mboxq->context1;
5222 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5223
5224 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5225 "(%d):2571 Mailbox cmd x%x Status x%x "
5226 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5227 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5228 "CQ: x%x x%x x%x x%x\n",
5229 mboxq->vport ? mboxq->vport->vpi : 0,
5230 bf_get(lpfc_mqe_command, mqe),
5231 bf_get(lpfc_mqe_status, mqe),
5232 mqe->un.mb_words[0], mqe->un.mb_words[1],
5233 mqe->un.mb_words[2], mqe->un.mb_words[3],
5234 mqe->un.mb_words[4], mqe->un.mb_words[5],
5235 mqe->un.mb_words[6], mqe->un.mb_words[7],
5236 mqe->un.mb_words[8], mqe->un.mb_words[9],
5237 mqe->un.mb_words[10], mqe->un.mb_words[11],
5238 mqe->un.mb_words[12], mqe->un.mb_words[13],
5239 mqe->un.mb_words[14], mqe->un.mb_words[15],
5240 mqe->un.mb_words[16], mqe->un.mb_words[50],
5241 mboxq->mcqe.word0,
5242 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5243 mboxq->mcqe.trailer);
5244
5245 if (rc) {
5246 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5247 kfree(mp);
ff78d8f9
JS
5248 rc = -EIO;
5249 goto out_free_mboxq;
da0436e9
JS
5250 }
5251 data_length = mqe->un.mb_words[5];
a0c87cbd 5252 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5253 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5254 kfree(mp);
ff78d8f9
JS
5255 rc = -EIO;
5256 goto out_free_mboxq;
d11e31dd 5257 }
dea3101e 5258
da0436e9
JS
5259 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5260 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5261 kfree(mp);
ff78d8f9
JS
5262 rc = 0;
5263
5264out_free_mboxq:
5265 mempool_free(mboxq, phba->mbox_mem_pool);
5266 return rc;
da0436e9 5267}
e59058c4
JS
5268
5269/**
da0436e9
JS
5270 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5271 * @phba: pointer to lpfc hba data structure.
5272 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5273 * @vpd: pointer to the memory to hold resulting port vpd data.
5274 * @vpd_size: On input, the number of bytes allocated to @vpd.
5275 * On output, the number of data bytes in @vpd.
e59058c4 5276 *
da0436e9
JS
5277 * This routine executes a READ_REV SLI4 mailbox command. In
5278 * addition, this routine gets the port vpd data.
5279 *
5280 * Return codes
af901ca1 5281 * 0 - successful
d439d286 5282 * -ENOMEM - could not allocated memory.
e59058c4 5283 **/
da0436e9
JS
5284static int
5285lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5286 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5287{
da0436e9
JS
5288 int rc = 0;
5289 uint32_t dma_size;
5290 struct lpfc_dmabuf *dmabuf;
5291 struct lpfc_mqe *mqe;
dea3101e 5292
da0436e9
JS
5293 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5294 if (!dmabuf)
5295 return -ENOMEM;
5296
5297 /*
5298 * Get a DMA buffer for the vpd data resulting from the READ_REV
5299 * mailbox command.
a257bf90 5300 */
da0436e9 5301 dma_size = *vpd_size;
1aee383d
JP
5302 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5303 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5304 if (!dmabuf->virt) {
5305 kfree(dmabuf);
5306 return -ENOMEM;
a257bf90
JS
5307 }
5308
da0436e9
JS
5309 /*
5310 * The SLI4 implementation of READ_REV conflicts at word1,
5311 * bits 31:16 and SLI4 adds vpd functionality not present
5312 * in SLI3. This code corrects the conflicts.
1dcb58e5 5313 */
da0436e9
JS
5314 lpfc_read_rev(phba, mboxq);
5315 mqe = &mboxq->u.mqe;
5316 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5317 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5318 mqe->un.read_rev.word1 &= 0x0000FFFF;
5319 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5320 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5321
5322 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5323 if (rc) {
5324 dma_free_coherent(&phba->pcidev->dev, dma_size,
5325 dmabuf->virt, dmabuf->phys);
def9c7a9 5326 kfree(dmabuf);
da0436e9
JS
5327 return -EIO;
5328 }
1dcb58e5 5329
da0436e9
JS
5330 /*
5331 * The available vpd length cannot be bigger than the
5332 * DMA buffer passed to the port. Catch the less than
5333 * case and update the caller's size.
5334 */
5335 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5336 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5337
d7c47992
JS
5338 memcpy(vpd, dmabuf->virt, *vpd_size);
5339
da0436e9
JS
5340 dma_free_coherent(&phba->pcidev->dev, dma_size,
5341 dmabuf->virt, dmabuf->phys);
5342 kfree(dmabuf);
5343 return 0;
dea3101e 5344}
5345
cd1c8301
JS
5346/**
5347 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5348 * @phba: pointer to lpfc hba data structure.
5349 *
5350 * This routine retrieves SLI4 device physical port name this PCI function
5351 * is attached to.
5352 *
5353 * Return codes
4907cb7b 5354 * 0 - successful
cd1c8301
JS
5355 * otherwise - failed to retrieve physical port name
5356 **/
5357static int
5358lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5359{
5360 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5361 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5362 struct lpfc_controller_attribute *cntl_attr;
5363 struct lpfc_mbx_get_port_name *get_port_name;
5364 void *virtaddr = NULL;
5365 uint32_t alloclen, reqlen;
5366 uint32_t shdr_status, shdr_add_status;
5367 union lpfc_sli4_cfg_shdr *shdr;
5368 char cport_name = 0;
5369 int rc;
5370
5371 /* We assume nothing at this point */
5372 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5373 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5374
5375 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5376 if (!mboxq)
5377 return -ENOMEM;
cd1c8301 5378 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5379 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5380 lpfc_sli4_read_config(phba);
5381 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5382 goto retrieve_ppname;
cd1c8301
JS
5383
5384 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5385 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5386 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5387 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5388 LPFC_SLI4_MBX_NEMBED);
5389 if (alloclen < reqlen) {
5390 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5391 "3084 Allocated DMA memory size (%d) is "
5392 "less than the requested DMA memory size "
5393 "(%d)\n", alloclen, reqlen);
5394 rc = -ENOMEM;
5395 goto out_free_mboxq;
5396 }
5397 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5398 virtaddr = mboxq->sge_array->addr[0];
5399 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5400 shdr = &mbx_cntl_attr->cfg_shdr;
5401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5403 if (shdr_status || shdr_add_status || rc) {
5404 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5405 "3085 Mailbox x%x (x%x/x%x) failed, "
5406 "rc:x%x, status:x%x, add_status:x%x\n",
5407 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5408 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5409 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5410 rc, shdr_status, shdr_add_status);
5411 rc = -ENXIO;
5412 goto out_free_mboxq;
5413 }
5414 cntl_attr = &mbx_cntl_attr->cntl_attr;
5415 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5416 phba->sli4_hba.lnk_info.lnk_tp =
5417 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5418 phba->sli4_hba.lnk_info.lnk_no =
5419 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5420 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5421 "3086 lnk_type:%d, lnk_numb:%d\n",
5422 phba->sli4_hba.lnk_info.lnk_tp,
5423 phba->sli4_hba.lnk_info.lnk_no);
5424
5425retrieve_ppname:
5426 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5427 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5428 sizeof(struct lpfc_mbx_get_port_name) -
5429 sizeof(struct lpfc_sli4_cfg_mhdr),
5430 LPFC_SLI4_MBX_EMBED);
5431 get_port_name = &mboxq->u.mqe.un.get_port_name;
5432 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5433 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5434 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5435 phba->sli4_hba.lnk_info.lnk_tp);
5436 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5437 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5438 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5439 if (shdr_status || shdr_add_status || rc) {
5440 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5441 "3087 Mailbox x%x (x%x/x%x) failed: "
5442 "rc:x%x, status:x%x, add_status:x%x\n",
5443 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5444 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5445 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5446 rc, shdr_status, shdr_add_status);
5447 rc = -ENXIO;
5448 goto out_free_mboxq;
5449 }
5450 switch (phba->sli4_hba.lnk_info.lnk_no) {
5451 case LPFC_LINK_NUMBER_0:
5452 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5453 &get_port_name->u.response);
5454 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5455 break;
5456 case LPFC_LINK_NUMBER_1:
5457 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5458 &get_port_name->u.response);
5459 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5460 break;
5461 case LPFC_LINK_NUMBER_2:
5462 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5463 &get_port_name->u.response);
5464 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5465 break;
5466 case LPFC_LINK_NUMBER_3:
5467 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5468 &get_port_name->u.response);
5469 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5470 break;
5471 default:
5472 break;
5473 }
5474
5475 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5476 phba->Port[0] = cport_name;
5477 phba->Port[1] = '\0';
5478 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5479 "3091 SLI get port name: %s\n", phba->Port);
5480 }
5481
5482out_free_mboxq:
5483 if (rc != MBX_TIMEOUT) {
5484 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5485 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5486 else
5487 mempool_free(mboxq, phba->mbox_mem_pool);
5488 }
5489 return rc;
5490}
5491
e59058c4 5492/**
da0436e9
JS
5493 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5494 * @phba: pointer to lpfc hba data structure.
e59058c4 5495 *
da0436e9
JS
5496 * This routine is called to explicitly arm the SLI4 device's completion and
5497 * event queues
5498 **/
5499static void
5500lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5501{
895427bd 5502 int qidx;
b71413dd 5503 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
da0436e9 5504
b71413dd
JS
5505 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5506 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5507 if (sli4_hba->nvmels_cq)
5508 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
895427bd
JS
5509 LPFC_QUEUE_REARM);
5510
b71413dd 5511 if (sli4_hba->fcp_cq)
895427bd 5512 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
b71413dd 5513 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
895427bd
JS
5514 LPFC_QUEUE_REARM);
5515
b71413dd 5516 if (sli4_hba->nvme_cq)
895427bd 5517 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
b71413dd 5518 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
895427bd 5519 LPFC_QUEUE_REARM);
1ba981fd 5520
f38fa0bb 5521 if (phba->cfg_fof)
b71413dd 5522 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
1ba981fd 5523
b71413dd 5524 if (sli4_hba->hba_eq)
895427bd 5525 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
b71413dd
JS
5526 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5527 LPFC_QUEUE_REARM);
1ba981fd 5528
2d7dbc4c
JS
5529 if (phba->nvmet_support) {
5530 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
b71413dd
JS
5531 sli4_hba->sli4_cq_release(
5532 sli4_hba->nvmet_cqset[qidx],
2d7dbc4c
JS
5533 LPFC_QUEUE_REARM);
5534 }
2e90f4b5 5535 }
1ba981fd
JS
5536
5537 if (phba->cfg_fof)
b71413dd 5538 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5539}
5540
6d368e53
JS
5541/**
5542 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5543 * @phba: Pointer to HBA context object.
5544 * @type: The resource extent type.
b76f2dc9
JS
5545 * @extnt_count: buffer to hold port available extent count.
5546 * @extnt_size: buffer to hold element count per extent.
6d368e53 5547 *
b76f2dc9
JS
5548 * This function calls the port and retrievs the number of available
5549 * extents and their size for a particular extent type.
5550 *
5551 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5552 **/
b76f2dc9 5553int
6d368e53
JS
5554lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5555 uint16_t *extnt_count, uint16_t *extnt_size)
5556{
5557 int rc = 0;
5558 uint32_t length;
5559 uint32_t mbox_tmo;
5560 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5561 LPFC_MBOXQ_t *mbox;
5562
5563 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5564 if (!mbox)
5565 return -ENOMEM;
5566
5567 /* Find out how many extents are available for this resource type */
5568 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5569 sizeof(struct lpfc_sli4_cfg_mhdr));
5570 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5571 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5572 length, LPFC_SLI4_MBX_EMBED);
5573
5574 /* Send an extents count of 0 - the GET doesn't use it. */
5575 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5576 LPFC_SLI4_MBX_EMBED);
5577 if (unlikely(rc)) {
5578 rc = -EIO;
5579 goto err_exit;
5580 }
5581
5582 if (!phba->sli4_hba.intr_enable)
5583 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5584 else {
a183a15f 5585 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5586 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5587 }
5588 if (unlikely(rc)) {
5589 rc = -EIO;
5590 goto err_exit;
5591 }
5592
5593 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5594 if (bf_get(lpfc_mbox_hdr_status,
5595 &rsrc_info->header.cfg_shdr.response)) {
5596 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5597 "2930 Failed to get resource extents "
5598 "Status 0x%x Add'l Status 0x%x\n",
5599 bf_get(lpfc_mbox_hdr_status,
5600 &rsrc_info->header.cfg_shdr.response),
5601 bf_get(lpfc_mbox_hdr_add_status,
5602 &rsrc_info->header.cfg_shdr.response));
5603 rc = -EIO;
5604 goto err_exit;
5605 }
5606
5607 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5608 &rsrc_info->u.rsp);
5609 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5610 &rsrc_info->u.rsp);
8a9d2e80
JS
5611
5612 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5613 "3162 Retrieved extents type-%d from port: count:%d, "
5614 "size:%d\n", type, *extnt_count, *extnt_size);
5615
5616err_exit:
6d368e53
JS
5617 mempool_free(mbox, phba->mbox_mem_pool);
5618 return rc;
5619}
5620
5621/**
5622 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5623 * @phba: Pointer to HBA context object.
5624 * @type: The extent type to check.
5625 *
5626 * This function reads the current available extents from the port and checks
5627 * if the extent count or extent size has changed since the last access.
5628 * Callers use this routine post port reset to understand if there is a
5629 * extent reprovisioning requirement.
5630 *
5631 * Returns:
5632 * -Error: error indicates problem.
5633 * 1: Extent count or size has changed.
5634 * 0: No changes.
5635 **/
5636static int
5637lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5638{
5639 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5640 uint16_t size_diff, rsrc_ext_size;
5641 int rc = 0;
5642 struct lpfc_rsrc_blks *rsrc_entry;
5643 struct list_head *rsrc_blk_list = NULL;
5644
5645 size_diff = 0;
5646 curr_ext_cnt = 0;
5647 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5648 &rsrc_ext_cnt,
5649 &rsrc_ext_size);
5650 if (unlikely(rc))
5651 return -EIO;
5652
5653 switch (type) {
5654 case LPFC_RSC_TYPE_FCOE_RPI:
5655 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5656 break;
5657 case LPFC_RSC_TYPE_FCOE_VPI:
5658 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5659 break;
5660 case LPFC_RSC_TYPE_FCOE_XRI:
5661 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5662 break;
5663 case LPFC_RSC_TYPE_FCOE_VFI:
5664 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5665 break;
5666 default:
5667 break;
5668 }
5669
5670 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5671 curr_ext_cnt++;
5672 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5673 size_diff++;
5674 }
5675
5676 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5677 rc = 1;
5678
5679 return rc;
5680}
5681
5682/**
5683 * lpfc_sli4_cfg_post_extnts -
5684 * @phba: Pointer to HBA context object.
5685 * @extnt_cnt - number of available extents.
5686 * @type - the extent type (rpi, xri, vfi, vpi).
5687 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5688 * @mbox - pointer to the caller's allocated mailbox structure.
5689 *
5690 * This function executes the extents allocation request. It also
5691 * takes care of the amount of memory needed to allocate or get the
5692 * allocated extents. It is the caller's responsibility to evaluate
5693 * the response.
5694 *
5695 * Returns:
5696 * -Error: Error value describes the condition found.
5697 * 0: if successful
5698 **/
5699static int
8a9d2e80 5700lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5701 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5702{
5703 int rc = 0;
5704 uint32_t req_len;
5705 uint32_t emb_len;
5706 uint32_t alloc_len, mbox_tmo;
5707
5708 /* Calculate the total requested length of the dma memory */
8a9d2e80 5709 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5710
5711 /*
5712 * Calculate the size of an embedded mailbox. The uint32_t
5713 * accounts for extents-specific word.
5714 */
5715 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5716 sizeof(uint32_t);
5717
5718 /*
5719 * Presume the allocation and response will fit into an embedded
5720 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5721 */
5722 *emb = LPFC_SLI4_MBX_EMBED;
5723 if (req_len > emb_len) {
8a9d2e80 5724 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5725 sizeof(union lpfc_sli4_cfg_shdr) +
5726 sizeof(uint32_t);
5727 *emb = LPFC_SLI4_MBX_NEMBED;
5728 }
5729
5730 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5731 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5732 req_len, *emb);
5733 if (alloc_len < req_len) {
5734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5735 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5736 "less than the requested DMA memory "
5737 "size (x%x)\n", alloc_len, req_len);
5738 return -ENOMEM;
5739 }
8a9d2e80 5740 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5741 if (unlikely(rc))
5742 return -EIO;
5743
5744 if (!phba->sli4_hba.intr_enable)
5745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5746 else {
a183a15f 5747 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5748 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5749 }
5750
5751 if (unlikely(rc))
5752 rc = -EIO;
5753 return rc;
5754}
5755
5756/**
5757 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5758 * @phba: Pointer to HBA context object.
5759 * @type: The resource extent type to allocate.
5760 *
5761 * This function allocates the number of elements for the specified
5762 * resource type.
5763 **/
5764static int
5765lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5766{
5767 bool emb = false;
5768 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5769 uint16_t rsrc_id, rsrc_start, j, k;
5770 uint16_t *ids;
5771 int i, rc;
5772 unsigned long longs;
5773 unsigned long *bmask;
5774 struct lpfc_rsrc_blks *rsrc_blks;
5775 LPFC_MBOXQ_t *mbox;
5776 uint32_t length;
5777 struct lpfc_id_range *id_array = NULL;
5778 void *virtaddr = NULL;
5779 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5780 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5781 struct list_head *ext_blk_list;
5782
5783 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5784 &rsrc_cnt,
5785 &rsrc_size);
5786 if (unlikely(rc))
5787 return -EIO;
5788
5789 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5790 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5791 "3009 No available Resource Extents "
5792 "for resource type 0x%x: Count: 0x%x, "
5793 "Size 0x%x\n", type, rsrc_cnt,
5794 rsrc_size);
5795 return -ENOMEM;
5796 }
5797
8a9d2e80
JS
5798 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5799 "2903 Post resource extents type-0x%x: "
5800 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5801
5802 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5803 if (!mbox)
5804 return -ENOMEM;
5805
8a9d2e80 5806 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5807 if (unlikely(rc)) {
5808 rc = -EIO;
5809 goto err_exit;
5810 }
5811
5812 /*
5813 * Figure out where the response is located. Then get local pointers
5814 * to the response data. The port does not guarantee to respond to
5815 * all extents counts request so update the local variable with the
5816 * allocated count from the port.
5817 */
5818 if (emb == LPFC_SLI4_MBX_EMBED) {
5819 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5820 id_array = &rsrc_ext->u.rsp.id[0];
5821 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5822 } else {
5823 virtaddr = mbox->sge_array->addr[0];
5824 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5825 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5826 id_array = &n_rsrc->id;
5827 }
5828
5829 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5830 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5831
5832 /*
5833 * Based on the resource size and count, correct the base and max
5834 * resource values.
5835 */
5836 length = sizeof(struct lpfc_rsrc_blks);
5837 switch (type) {
5838 case LPFC_RSC_TYPE_FCOE_RPI:
5839 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5840 sizeof(unsigned long),
5841 GFP_KERNEL);
5842 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5843 rc = -ENOMEM;
5844 goto err_exit;
5845 }
5846 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5847 sizeof(uint16_t),
5848 GFP_KERNEL);
5849 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5850 kfree(phba->sli4_hba.rpi_bmask);
5851 rc = -ENOMEM;
5852 goto err_exit;
5853 }
5854
5855 /*
5856 * The next_rpi was initialized with the maximum available
5857 * count but the port may allocate a smaller number. Catch
5858 * that case and update the next_rpi.
5859 */
5860 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5861
5862 /* Initialize local ptrs for common extent processing later. */
5863 bmask = phba->sli4_hba.rpi_bmask;
5864 ids = phba->sli4_hba.rpi_ids;
5865 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5866 break;
5867 case LPFC_RSC_TYPE_FCOE_VPI:
5868 phba->vpi_bmask = kzalloc(longs *
5869 sizeof(unsigned long),
5870 GFP_KERNEL);
5871 if (unlikely(!phba->vpi_bmask)) {
5872 rc = -ENOMEM;
5873 goto err_exit;
5874 }
5875 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5876 sizeof(uint16_t),
5877 GFP_KERNEL);
5878 if (unlikely(!phba->vpi_ids)) {
5879 kfree(phba->vpi_bmask);
5880 rc = -ENOMEM;
5881 goto err_exit;
5882 }
5883
5884 /* Initialize local ptrs for common extent processing later. */
5885 bmask = phba->vpi_bmask;
5886 ids = phba->vpi_ids;
5887 ext_blk_list = &phba->lpfc_vpi_blk_list;
5888 break;
5889 case LPFC_RSC_TYPE_FCOE_XRI:
5890 phba->sli4_hba.xri_bmask = kzalloc(longs *
5891 sizeof(unsigned long),
5892 GFP_KERNEL);
5893 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5894 rc = -ENOMEM;
5895 goto err_exit;
5896 }
8a9d2e80 5897 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5898 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5899 sizeof(uint16_t),
5900 GFP_KERNEL);
5901 if (unlikely(!phba->sli4_hba.xri_ids)) {
5902 kfree(phba->sli4_hba.xri_bmask);
5903 rc = -ENOMEM;
5904 goto err_exit;
5905 }
5906
5907 /* Initialize local ptrs for common extent processing later. */
5908 bmask = phba->sli4_hba.xri_bmask;
5909 ids = phba->sli4_hba.xri_ids;
5910 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5911 break;
5912 case LPFC_RSC_TYPE_FCOE_VFI:
5913 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5914 sizeof(unsigned long),
5915 GFP_KERNEL);
5916 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5917 rc = -ENOMEM;
5918 goto err_exit;
5919 }
5920 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5921 sizeof(uint16_t),
5922 GFP_KERNEL);
5923 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5924 kfree(phba->sli4_hba.vfi_bmask);
5925 rc = -ENOMEM;
5926 goto err_exit;
5927 }
5928
5929 /* Initialize local ptrs for common extent processing later. */
5930 bmask = phba->sli4_hba.vfi_bmask;
5931 ids = phba->sli4_hba.vfi_ids;
5932 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5933 break;
5934 default:
5935 /* Unsupported Opcode. Fail call. */
5936 id_array = NULL;
5937 bmask = NULL;
5938 ids = NULL;
5939 ext_blk_list = NULL;
5940 goto err_exit;
5941 }
5942
5943 /*
5944 * Complete initializing the extent configuration with the
5945 * allocated ids assigned to this function. The bitmask serves
5946 * as an index into the array and manages the available ids. The
5947 * array just stores the ids communicated to the port via the wqes.
5948 */
5949 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5950 if ((i % 2) == 0)
5951 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5952 &id_array[k]);
5953 else
5954 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5955 &id_array[k]);
5956
5957 rsrc_blks = kzalloc(length, GFP_KERNEL);
5958 if (unlikely(!rsrc_blks)) {
5959 rc = -ENOMEM;
5960 kfree(bmask);
5961 kfree(ids);
5962 goto err_exit;
5963 }
5964 rsrc_blks->rsrc_start = rsrc_id;
5965 rsrc_blks->rsrc_size = rsrc_size;
5966 list_add_tail(&rsrc_blks->list, ext_blk_list);
5967 rsrc_start = rsrc_id;
895427bd 5968 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5969 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5970 lpfc_sli4_get_iocb_cnt(phba);
5971 phba->sli4_hba.nvme_xri_start =
5972 phba->sli4_hba.scsi_xri_start +
5973 phba->sli4_hba.scsi_xri_max;
5974 }
6d368e53
JS
5975
5976 while (rsrc_id < (rsrc_start + rsrc_size)) {
5977 ids[j] = rsrc_id;
5978 rsrc_id++;
5979 j++;
5980 }
5981 /* Entire word processed. Get next word.*/
5982 if ((i % 2) == 1)
5983 k++;
5984 }
5985 err_exit:
5986 lpfc_sli4_mbox_cmd_free(phba, mbox);
5987 return rc;
5988}
5989
895427bd
JS
5990
5991
6d368e53
JS
5992/**
5993 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5994 * @phba: Pointer to HBA context object.
5995 * @type: the extent's type.
5996 *
5997 * This function deallocates all extents of a particular resource type.
5998 * SLI4 does not allow for deallocating a particular extent range. It
5999 * is the caller's responsibility to release all kernel memory resources.
6000 **/
6001static int
6002lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6003{
6004 int rc;
6005 uint32_t length, mbox_tmo = 0;
6006 LPFC_MBOXQ_t *mbox;
6007 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6008 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6009
6010 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6011 if (!mbox)
6012 return -ENOMEM;
6013
6014 /*
6015 * This function sends an embedded mailbox because it only sends the
6016 * the resource type. All extents of this type are released by the
6017 * port.
6018 */
6019 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6020 sizeof(struct lpfc_sli4_cfg_mhdr));
6021 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6022 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6023 length, LPFC_SLI4_MBX_EMBED);
6024
6025 /* Send an extents count of 0 - the dealloc doesn't use it. */
6026 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6027 LPFC_SLI4_MBX_EMBED);
6028 if (unlikely(rc)) {
6029 rc = -EIO;
6030 goto out_free_mbox;
6031 }
6032 if (!phba->sli4_hba.intr_enable)
6033 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6034 else {
a183a15f 6035 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
6036 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6037 }
6038 if (unlikely(rc)) {
6039 rc = -EIO;
6040 goto out_free_mbox;
6041 }
6042
6043 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6044 if (bf_get(lpfc_mbox_hdr_status,
6045 &dealloc_rsrc->header.cfg_shdr.response)) {
6046 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6047 "2919 Failed to release resource extents "
6048 "for type %d - Status 0x%x Add'l Status 0x%x. "
6049 "Resource memory not released.\n",
6050 type,
6051 bf_get(lpfc_mbox_hdr_status,
6052 &dealloc_rsrc->header.cfg_shdr.response),
6053 bf_get(lpfc_mbox_hdr_add_status,
6054 &dealloc_rsrc->header.cfg_shdr.response));
6055 rc = -EIO;
6056 goto out_free_mbox;
6057 }
6058
6059 /* Release kernel memory resources for the specific type. */
6060 switch (type) {
6061 case LPFC_RSC_TYPE_FCOE_VPI:
6062 kfree(phba->vpi_bmask);
6063 kfree(phba->vpi_ids);
6064 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6065 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6066 &phba->lpfc_vpi_blk_list, list) {
6067 list_del_init(&rsrc_blk->list);
6068 kfree(rsrc_blk);
6069 }
16a3a208 6070 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6071 break;
6072 case LPFC_RSC_TYPE_FCOE_XRI:
6073 kfree(phba->sli4_hba.xri_bmask);
6074 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6075 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6076 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6077 list_del_init(&rsrc_blk->list);
6078 kfree(rsrc_blk);
6079 }
6080 break;
6081 case LPFC_RSC_TYPE_FCOE_VFI:
6082 kfree(phba->sli4_hba.vfi_bmask);
6083 kfree(phba->sli4_hba.vfi_ids);
6084 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6085 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6086 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6087 list_del_init(&rsrc_blk->list);
6088 kfree(rsrc_blk);
6089 }
6090 break;
6091 case LPFC_RSC_TYPE_FCOE_RPI:
6092 /* RPI bitmask and physical id array are cleaned up earlier. */
6093 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6094 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6095 list_del_init(&rsrc_blk->list);
6096 kfree(rsrc_blk);
6097 }
6098 break;
6099 default:
6100 break;
6101 }
6102
6103 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6104
6105 out_free_mbox:
6106 mempool_free(mbox, phba->mbox_mem_pool);
6107 return rc;
6108}
6109
bd4b3e5c 6110static void
7bdedb34
JS
6111lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6112 uint32_t feature)
65791f1f 6113{
65791f1f 6114 uint32_t len;
65791f1f 6115
65791f1f
JS
6116 len = sizeof(struct lpfc_mbx_set_feature) -
6117 sizeof(struct lpfc_sli4_cfg_mhdr);
6118 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6119 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6120 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
6121
6122 switch (feature) {
6123 case LPFC_SET_UE_RECOVERY:
6124 bf_set(lpfc_mbx_set_feature_UER,
6125 &mbox->u.mqe.un.set_feature, 1);
6126 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6127 mbox->u.mqe.un.set_feature.param_len = 8;
6128 break;
6129 case LPFC_SET_MDS_DIAGS:
6130 bf_set(lpfc_mbx_set_feature_mds,
6131 &mbox->u.mqe.un.set_feature, 1);
6132 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 6133 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
6134 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6135 mbox->u.mqe.un.set_feature.param_len = 8;
6136 break;
65791f1f 6137 }
7bdedb34
JS
6138
6139 return;
65791f1f
JS
6140}
6141
6d368e53
JS
6142/**
6143 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6144 * @phba: Pointer to HBA context object.
6145 *
6146 * This function allocates all SLI4 resource identifiers.
6147 **/
6148int
6149lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6150{
6151 int i, rc, error = 0;
6152 uint16_t count, base;
6153 unsigned long longs;
6154
ff78d8f9
JS
6155 if (!phba->sli4_hba.rpi_hdrs_in_use)
6156 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
6157 if (phba->sli4_hba.extents_in_use) {
6158 /*
6159 * The port supports resource extents. The XRI, VPI, VFI, RPI
6160 * resource extent count must be read and allocated before
6161 * provisioning the resource id arrays.
6162 */
6163 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6164 LPFC_IDX_RSRC_RDY) {
6165 /*
6166 * Extent-based resources are set - the driver could
6167 * be in a port reset. Figure out if any corrective
6168 * actions need to be taken.
6169 */
6170 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6171 LPFC_RSC_TYPE_FCOE_VFI);
6172 if (rc != 0)
6173 error++;
6174 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6175 LPFC_RSC_TYPE_FCOE_VPI);
6176 if (rc != 0)
6177 error++;
6178 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6179 LPFC_RSC_TYPE_FCOE_XRI);
6180 if (rc != 0)
6181 error++;
6182 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6183 LPFC_RSC_TYPE_FCOE_RPI);
6184 if (rc != 0)
6185 error++;
6186
6187 /*
6188 * It's possible that the number of resources
6189 * provided to this port instance changed between
6190 * resets. Detect this condition and reallocate
6191 * resources. Otherwise, there is no action.
6192 */
6193 if (error) {
6194 lpfc_printf_log(phba, KERN_INFO,
6195 LOG_MBOX | LOG_INIT,
6196 "2931 Detected extent resource "
6197 "change. Reallocating all "
6198 "extents.\n");
6199 rc = lpfc_sli4_dealloc_extent(phba,
6200 LPFC_RSC_TYPE_FCOE_VFI);
6201 rc = lpfc_sli4_dealloc_extent(phba,
6202 LPFC_RSC_TYPE_FCOE_VPI);
6203 rc = lpfc_sli4_dealloc_extent(phba,
6204 LPFC_RSC_TYPE_FCOE_XRI);
6205 rc = lpfc_sli4_dealloc_extent(phba,
6206 LPFC_RSC_TYPE_FCOE_RPI);
6207 } else
6208 return 0;
6209 }
6210
6211 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6212 if (unlikely(rc))
6213 goto err_exit;
6214
6215 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6216 if (unlikely(rc))
6217 goto err_exit;
6218
6219 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6220 if (unlikely(rc))
6221 goto err_exit;
6222
6223 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6224 if (unlikely(rc))
6225 goto err_exit;
6226 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6227 LPFC_IDX_RSRC_RDY);
6228 return rc;
6229 } else {
6230 /*
6231 * The port does not support resource extents. The XRI, VPI,
6232 * VFI, RPI resource ids were determined from READ_CONFIG.
6233 * Just allocate the bitmasks and provision the resource id
6234 * arrays. If a port reset is active, the resources don't
6235 * need any action - just exit.
6236 */
6237 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6238 LPFC_IDX_RSRC_RDY) {
6239 lpfc_sli4_dealloc_resource_identifiers(phba);
6240 lpfc_sli4_remove_rpis(phba);
6241 }
6d368e53
JS
6242 /* RPIs. */
6243 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6244 if (count <= 0) {
6245 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6246 "3279 Invalid provisioning of "
6247 "rpi:%d\n", count);
6248 rc = -EINVAL;
6249 goto err_exit;
6250 }
6d368e53
JS
6251 base = phba->sli4_hba.max_cfg_param.rpi_base;
6252 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6253 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6254 sizeof(unsigned long),
6255 GFP_KERNEL);
6256 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6257 rc = -ENOMEM;
6258 goto err_exit;
6259 }
6260 phba->sli4_hba.rpi_ids = kzalloc(count *
6261 sizeof(uint16_t),
6262 GFP_KERNEL);
6263 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6264 rc = -ENOMEM;
6265 goto free_rpi_bmask;
6266 }
6267
6268 for (i = 0; i < count; i++)
6269 phba->sli4_hba.rpi_ids[i] = base + i;
6270
6271 /* VPIs. */
6272 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6273 if (count <= 0) {
6274 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6275 "3280 Invalid provisioning of "
6276 "vpi:%d\n", count);
6277 rc = -EINVAL;
6278 goto free_rpi_ids;
6279 }
6d368e53
JS
6280 base = phba->sli4_hba.max_cfg_param.vpi_base;
6281 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6282 phba->vpi_bmask = kzalloc(longs *
6283 sizeof(unsigned long),
6284 GFP_KERNEL);
6285 if (unlikely(!phba->vpi_bmask)) {
6286 rc = -ENOMEM;
6287 goto free_rpi_ids;
6288 }
6289 phba->vpi_ids = kzalloc(count *
6290 sizeof(uint16_t),
6291 GFP_KERNEL);
6292 if (unlikely(!phba->vpi_ids)) {
6293 rc = -ENOMEM;
6294 goto free_vpi_bmask;
6295 }
6296
6297 for (i = 0; i < count; i++)
6298 phba->vpi_ids[i] = base + i;
6299
6300 /* XRIs. */
6301 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6302 if (count <= 0) {
6303 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6304 "3281 Invalid provisioning of "
6305 "xri:%d\n", count);
6306 rc = -EINVAL;
6307 goto free_vpi_ids;
6308 }
6d368e53
JS
6309 base = phba->sli4_hba.max_cfg_param.xri_base;
6310 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6311 phba->sli4_hba.xri_bmask = kzalloc(longs *
6312 sizeof(unsigned long),
6313 GFP_KERNEL);
6314 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6315 rc = -ENOMEM;
6316 goto free_vpi_ids;
6317 }
41899be7 6318 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6319 phba->sli4_hba.xri_ids = kzalloc(count *
6320 sizeof(uint16_t),
6321 GFP_KERNEL);
6322 if (unlikely(!phba->sli4_hba.xri_ids)) {
6323 rc = -ENOMEM;
6324 goto free_xri_bmask;
6325 }
6326
6327 for (i = 0; i < count; i++)
6328 phba->sli4_hba.xri_ids[i] = base + i;
6329
6330 /* VFIs. */
6331 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6332 if (count <= 0) {
6333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6334 "3282 Invalid provisioning of "
6335 "vfi:%d\n", count);
6336 rc = -EINVAL;
6337 goto free_xri_ids;
6338 }
6d368e53
JS
6339 base = phba->sli4_hba.max_cfg_param.vfi_base;
6340 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6341 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6342 sizeof(unsigned long),
6343 GFP_KERNEL);
6344 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6345 rc = -ENOMEM;
6346 goto free_xri_ids;
6347 }
6348 phba->sli4_hba.vfi_ids = kzalloc(count *
6349 sizeof(uint16_t),
6350 GFP_KERNEL);
6351 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6352 rc = -ENOMEM;
6353 goto free_vfi_bmask;
6354 }
6355
6356 for (i = 0; i < count; i++)
6357 phba->sli4_hba.vfi_ids[i] = base + i;
6358
6359 /*
6360 * Mark all resources ready. An HBA reset doesn't need
6361 * to reset the initialization.
6362 */
6363 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6364 LPFC_IDX_RSRC_RDY);
6365 return 0;
6366 }
6367
6368 free_vfi_bmask:
6369 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6370 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6371 free_xri_ids:
6372 kfree(phba->sli4_hba.xri_ids);
cd60be49 6373 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6374 free_xri_bmask:
6375 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6376 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6377 free_vpi_ids:
6378 kfree(phba->vpi_ids);
cd60be49 6379 phba->vpi_ids = NULL;
6d368e53
JS
6380 free_vpi_bmask:
6381 kfree(phba->vpi_bmask);
cd60be49 6382 phba->vpi_bmask = NULL;
6d368e53
JS
6383 free_rpi_ids:
6384 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6385 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6386 free_rpi_bmask:
6387 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6388 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6389 err_exit:
6390 return rc;
6391}
6392
6393/**
6394 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6395 * @phba: Pointer to HBA context object.
6396 *
6397 * This function allocates the number of elements for the specified
6398 * resource type.
6399 **/
6400int
6401lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6402{
6403 if (phba->sli4_hba.extents_in_use) {
6404 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6405 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6406 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6407 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6408 } else {
6409 kfree(phba->vpi_bmask);
16a3a208 6410 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6411 kfree(phba->vpi_ids);
6412 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6413 kfree(phba->sli4_hba.xri_bmask);
6414 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6415 kfree(phba->sli4_hba.vfi_bmask);
6416 kfree(phba->sli4_hba.vfi_ids);
6417 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6418 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6419 }
6420
6421 return 0;
6422}
6423
b76f2dc9
JS
6424/**
6425 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6426 * @phba: Pointer to HBA context object.
6427 * @type: The resource extent type.
6428 * @extnt_count: buffer to hold port extent count response
6429 * @extnt_size: buffer to hold port extent size response.
6430 *
6431 * This function calls the port to read the host allocated extents
6432 * for a particular type.
6433 **/
6434int
6435lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6436 uint16_t *extnt_cnt, uint16_t *extnt_size)
6437{
6438 bool emb;
6439 int rc = 0;
6440 uint16_t curr_blks = 0;
6441 uint32_t req_len, emb_len;
6442 uint32_t alloc_len, mbox_tmo;
6443 struct list_head *blk_list_head;
6444 struct lpfc_rsrc_blks *rsrc_blk;
6445 LPFC_MBOXQ_t *mbox;
6446 void *virtaddr = NULL;
6447 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6448 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6449 union lpfc_sli4_cfg_shdr *shdr;
6450
6451 switch (type) {
6452 case LPFC_RSC_TYPE_FCOE_VPI:
6453 blk_list_head = &phba->lpfc_vpi_blk_list;
6454 break;
6455 case LPFC_RSC_TYPE_FCOE_XRI:
6456 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6457 break;
6458 case LPFC_RSC_TYPE_FCOE_VFI:
6459 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6460 break;
6461 case LPFC_RSC_TYPE_FCOE_RPI:
6462 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6463 break;
6464 default:
6465 return -EIO;
6466 }
6467
6468 /* Count the number of extents currently allocatd for this type. */
6469 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6470 if (curr_blks == 0) {
6471 /*
6472 * The GET_ALLOCATED mailbox does not return the size,
6473 * just the count. The size should be just the size
6474 * stored in the current allocated block and all sizes
6475 * for an extent type are the same so set the return
6476 * value now.
6477 */
6478 *extnt_size = rsrc_blk->rsrc_size;
6479 }
6480 curr_blks++;
6481 }
6482
b76f2dc9
JS
6483 /*
6484 * Calculate the size of an embedded mailbox. The uint32_t
6485 * accounts for extents-specific word.
6486 */
6487 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6488 sizeof(uint32_t);
6489
6490 /*
6491 * Presume the allocation and response will fit into an embedded
6492 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6493 */
6494 emb = LPFC_SLI4_MBX_EMBED;
6495 req_len = emb_len;
6496 if (req_len > emb_len) {
6497 req_len = curr_blks * sizeof(uint16_t) +
6498 sizeof(union lpfc_sli4_cfg_shdr) +
6499 sizeof(uint32_t);
6500 emb = LPFC_SLI4_MBX_NEMBED;
6501 }
6502
6503 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6504 if (!mbox)
6505 return -ENOMEM;
6506 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6507
6508 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6509 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6510 req_len, emb);
6511 if (alloc_len < req_len) {
6512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6513 "2983 Allocated DMA memory size (x%x) is "
6514 "less than the requested DMA memory "
6515 "size (x%x)\n", alloc_len, req_len);
6516 rc = -ENOMEM;
6517 goto err_exit;
6518 }
6519 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6520 if (unlikely(rc)) {
6521 rc = -EIO;
6522 goto err_exit;
6523 }
6524
6525 if (!phba->sli4_hba.intr_enable)
6526 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6527 else {
a183a15f 6528 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6529 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6530 }
6531
6532 if (unlikely(rc)) {
6533 rc = -EIO;
6534 goto err_exit;
6535 }
6536
6537 /*
6538 * Figure out where the response is located. Then get local pointers
6539 * to the response data. The port does not guarantee to respond to
6540 * all extents counts request so update the local variable with the
6541 * allocated count from the port.
6542 */
6543 if (emb == LPFC_SLI4_MBX_EMBED) {
6544 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6545 shdr = &rsrc_ext->header.cfg_shdr;
6546 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6547 } else {
6548 virtaddr = mbox->sge_array->addr[0];
6549 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6550 shdr = &n_rsrc->cfg_shdr;
6551 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6552 }
6553
6554 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6555 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6556 "2984 Failed to read allocated resources "
6557 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6558 type,
6559 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6560 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6561 rc = -EIO;
6562 goto err_exit;
6563 }
6564 err_exit:
6565 lpfc_sli4_mbox_cmd_free(phba, mbox);
6566 return rc;
6567}
6568
8a9d2e80 6569/**
0ef69968 6570 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6571 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6572 * @pring: Pointer to driver SLI ring object.
6573 * @sgl_list: linked link of sgl buffers to post
6574 * @cnt: number of linked list buffers
8a9d2e80 6575 *
895427bd 6576 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6577 * repost them to the port by using SGL block post. This is needed after a
6578 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6579 * of buffer sgls which contains contiguous xris and uses the non-embedded
6580 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6581 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6582 * mailbox command for posting.
6583 *
6584 * Returns: 0 = success, non-zero failure.
6585 **/
6586static int
895427bd
JS
6587lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6588 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6589{
6590 struct lpfc_sglq *sglq_entry = NULL;
6591 struct lpfc_sglq *sglq_entry_next = NULL;
6592 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6593 int status, total_cnt;
6594 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6595 int last_xritag = NO_XRI;
6596 LIST_HEAD(prep_sgl_list);
6597 LIST_HEAD(blck_sgl_list);
6598 LIST_HEAD(allc_sgl_list);
6599 LIST_HEAD(post_sgl_list);
6600 LIST_HEAD(free_sgl_list);
6601
38c20673 6602 spin_lock_irq(&phba->hbalock);
895427bd
JS
6603 spin_lock(&phba->sli4_hba.sgl_list_lock);
6604 list_splice_init(sgl_list, &allc_sgl_list);
6605 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6606 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6607
895427bd 6608 total_cnt = cnt;
8a9d2e80
JS
6609 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6610 &allc_sgl_list, list) {
6611 list_del_init(&sglq_entry->list);
6612 block_cnt++;
6613 if ((last_xritag != NO_XRI) &&
6614 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6615 /* a hole in xri block, form a sgl posting block */
6616 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6617 post_cnt = block_cnt - 1;
6618 /* prepare list for next posting block */
6619 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6620 block_cnt = 1;
6621 } else {
6622 /* prepare list for next posting block */
6623 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6624 /* enough sgls for non-embed sgl mbox command */
6625 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6626 list_splice_init(&prep_sgl_list,
6627 &blck_sgl_list);
6628 post_cnt = block_cnt;
6629 block_cnt = 0;
6630 }
6631 }
6632 num_posted++;
6633
6634 /* keep track of last sgl's xritag */
6635 last_xritag = sglq_entry->sli4_xritag;
6636
895427bd
JS
6637 /* end of repost sgl list condition for buffers */
6638 if (num_posted == total_cnt) {
8a9d2e80
JS
6639 if (post_cnt == 0) {
6640 list_splice_init(&prep_sgl_list,
6641 &blck_sgl_list);
6642 post_cnt = block_cnt;
6643 } else if (block_cnt == 1) {
6644 status = lpfc_sli4_post_sgl(phba,
6645 sglq_entry->phys, 0,
6646 sglq_entry->sli4_xritag);
6647 if (!status) {
6648 /* successful, put sgl to posted list */
6649 list_add_tail(&sglq_entry->list,
6650 &post_sgl_list);
6651 } else {
6652 /* Failure, put sgl to free list */
6653 lpfc_printf_log(phba, KERN_WARNING,
6654 LOG_SLI,
895427bd 6655 "3159 Failed to post "
8a9d2e80
JS
6656 "sgl, xritag:x%x\n",
6657 sglq_entry->sli4_xritag);
6658 list_add_tail(&sglq_entry->list,
6659 &free_sgl_list);
711ea882 6660 total_cnt--;
8a9d2e80
JS
6661 }
6662 }
6663 }
6664
6665 /* continue until a nembed page worth of sgls */
6666 if (post_cnt == 0)
6667 continue;
6668
895427bd
JS
6669 /* post the buffer list sgls as a block */
6670 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6671 post_cnt);
8a9d2e80
JS
6672
6673 if (!status) {
6674 /* success, put sgl list to posted sgl list */
6675 list_splice_init(&blck_sgl_list, &post_sgl_list);
6676 } else {
6677 /* Failure, put sgl list to free sgl list */
6678 sglq_entry_first = list_first_entry(&blck_sgl_list,
6679 struct lpfc_sglq,
6680 list);
6681 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6682 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6683 "xritag:x%x-x%x\n",
6684 sglq_entry_first->sli4_xritag,
6685 (sglq_entry_first->sli4_xritag +
6686 post_cnt - 1));
6687 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6688 total_cnt -= post_cnt;
8a9d2e80
JS
6689 }
6690
6691 /* don't reset xirtag due to hole in xri block */
6692 if (block_cnt == 0)
6693 last_xritag = NO_XRI;
6694
895427bd 6695 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6696 post_cnt = 0;
6697 }
6698
895427bd 6699 /* free the sgls failed to post */
8a9d2e80
JS
6700 lpfc_free_sgl_list(phba, &free_sgl_list);
6701
895427bd 6702 /* push sgls posted to the available list */
8a9d2e80 6703 if (!list_empty(&post_sgl_list)) {
38c20673 6704 spin_lock_irq(&phba->hbalock);
895427bd
JS
6705 spin_lock(&phba->sli4_hba.sgl_list_lock);
6706 list_splice_init(&post_sgl_list, sgl_list);
6707 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6708 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6709 } else {
6710 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6711 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6712 return -EIO;
6713 }
895427bd
JS
6714
6715 /* return the number of XRIs actually posted */
6716 return total_cnt;
8a9d2e80
JS
6717}
6718
61bda8f7
JS
6719void
6720lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6721{
6722 uint32_t len;
6723
6724 len = sizeof(struct lpfc_mbx_set_host_data) -
6725 sizeof(struct lpfc_sli4_cfg_mhdr);
6726 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6727 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6728 LPFC_SLI4_MBX_EMBED);
6729
6730 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6731 mbox->u.mqe.un.set_host_data.param_len =
6732 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6733 snprintf(mbox->u.mqe.un.set_host_data.data,
6734 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6735 "Linux %s v"LPFC_DRIVER_VERSION,
6736 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6737}
6738
a8cf5dfe 6739int
6c621a22 6740lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 6741 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
6742{
6743 int rc, i;
6744 struct lpfc_rqe hrqe;
6745 struct lpfc_rqe drqe;
6746 struct lpfc_rqb *rqbp;
411de511 6747 unsigned long flags;
6c621a22
JS
6748 struct rqb_dmabuf *rqb_buffer;
6749 LIST_HEAD(rqb_buf_list);
6750
411de511 6751 spin_lock_irqsave(&phba->hbalock, flags);
6c621a22
JS
6752 rqbp = hrq->rqbp;
6753 for (i = 0; i < count; i++) {
6754 /* IF RQ is already full, don't bother */
6755 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6756 break;
6757 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6758 if (!rqb_buffer)
6759 break;
6760 rqb_buffer->hrq = hrq;
6761 rqb_buffer->drq = drq;
a8cf5dfe 6762 rqb_buffer->idx = idx;
6c621a22
JS
6763 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6764 }
6765 while (!list_empty(&rqb_buf_list)) {
6766 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6767 hbuf.list);
6768
6769 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6770 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6771 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6772 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6773 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6774 if (rc < 0) {
411de511
JS
6775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6776 "6421 Cannot post to HRQ %d: %x %x %x "
6777 "DRQ %x %x\n",
6778 hrq->queue_id,
6779 hrq->host_index,
6780 hrq->hba_index,
6781 hrq->entry_count,
6782 drq->host_index,
6783 drq->hba_index);
6c621a22
JS
6784 rqbp->rqb_free_buffer(phba, rqb_buffer);
6785 } else {
6786 list_add_tail(&rqb_buffer->hbuf.list,
6787 &rqbp->rqb_buffer_list);
6788 rqbp->buffer_count++;
6789 }
6790 }
411de511 6791 spin_unlock_irqrestore(&phba->hbalock, flags);
6c621a22
JS
6792 return 1;
6793}
6794
da0436e9 6795/**
183b8021 6796 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
6797 * @phba: Pointer to HBA context object.
6798 *
183b8021
MY
6799 * This function is the main SLI4 device initialization PCI function. This
6800 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
6801 * HBA error attention handler code. Caller is not required to hold any
6802 * locks.
6803 **/
6804int
6805lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6806{
6c621a22 6807 int rc, i, cnt;
da0436e9
JS
6808 LPFC_MBOXQ_t *mboxq;
6809 struct lpfc_mqe *mqe;
6810 uint8_t *vpd;
6811 uint32_t vpd_size;
6812 uint32_t ftr_rsp = 0;
6813 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6814 struct lpfc_vport *vport = phba->pport;
6815 struct lpfc_dmabuf *mp;
2d7dbc4c 6816 struct lpfc_rqb *rqbp;
da0436e9
JS
6817
6818 /* Perform a PCI function reset to start from clean */
6819 rc = lpfc_pci_function_reset(phba);
6820 if (unlikely(rc))
6821 return -ENODEV;
6822
6823 /* Check the HBA Host Status Register for readyness */
6824 rc = lpfc_sli4_post_status_check(phba);
6825 if (unlikely(rc))
6826 return -ENODEV;
6827 else {
6828 spin_lock_irq(&phba->hbalock);
6829 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6830 spin_unlock_irq(&phba->hbalock);
6831 }
6832
6833 /*
6834 * Allocate a single mailbox container for initializing the
6835 * port.
6836 */
6837 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6838 if (!mboxq)
6839 return -ENOMEM;
6840
da0436e9 6841 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6842 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6843 vpd = kzalloc(vpd_size, GFP_KERNEL);
6844 if (!vpd) {
6845 rc = -ENOMEM;
6846 goto out_free_mbox;
6847 }
6848
6849 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6850 if (unlikely(rc)) {
6851 kfree(vpd);
6852 goto out_free_mbox;
6853 }
572709e2 6854
da0436e9 6855 mqe = &mboxq->u.mqe;
f1126688 6856 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6857 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6858 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6859 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6860 } else {
76a95d75 6861 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6862 }
45ed1190
JS
6863
6864 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6865 LPFC_DCBX_CEE_MODE)
6866 phba->hba_flag |= HBA_FIP_SUPPORT;
6867 else
6868 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6869
4f2e66c6
JS
6870 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6871
c31098ce 6872 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6873 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6874 "0376 READ_REV Error. SLI Level %d "
6875 "FCoE enabled %d\n",
76a95d75 6876 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6877 rc = -EIO;
76a95d75
JS
6878 kfree(vpd);
6879 goto out_free_mbox;
da0436e9 6880 }
cd1c8301 6881
ff78d8f9
JS
6882 /*
6883 * Continue initialization with default values even if driver failed
6884 * to read FCoE param config regions, only read parameters if the
6885 * board is FCoE
6886 */
6887 if (phba->hba_flag & HBA_FCOE_MODE &&
6888 lpfc_sli4_read_fcoe_params(phba))
6889 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6890 "2570 Failed to read FCoE parameters\n");
6891
cd1c8301
JS
6892 /*
6893 * Retrieve sli4 device physical port name, failure of doing it
6894 * is considered as non-fatal.
6895 */
6896 rc = lpfc_sli4_retrieve_pport_name(phba);
6897 if (!rc)
6898 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6899 "3080 Successful retrieving SLI4 device "
6900 "physical port name: %s.\n", phba->Port);
6901
da0436e9
JS
6902 /*
6903 * Evaluate the read rev and vpd data. Populate the driver
6904 * state with the results. If this routine fails, the failure
6905 * is not fatal as the driver will use generic values.
6906 */
6907 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6908 if (unlikely(!rc)) {
6909 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6910 "0377 Error %d parsing vpd. "
6911 "Using defaults.\n", rc);
6912 rc = 0;
6913 }
76a95d75 6914 kfree(vpd);
da0436e9 6915
f1126688
JS
6916 /* Save information as VPD data */
6917 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6918 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4e565cf0
JS
6919
6920 /*
6921 * This is because first G7 ASIC doesn't support the standard
6922 * 0x5a NVME cmd descriptor type/subtype
6923 */
6924 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6925 LPFC_SLI_INTF_IF_TYPE_6) &&
6926 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6927 (phba->vpd.rev.smRev == 0) &&
6928 (phba->cfg_nvme_embed_cmd == 1))
6929 phba->cfg_nvme_embed_cmd = 0;
6930
f1126688
JS
6931 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6932 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6933 &mqe->un.read_rev);
6934 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6935 &mqe->un.read_rev);
6936 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6937 &mqe->un.read_rev);
6938 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6939 &mqe->un.read_rev);
6940 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6941 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6942 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6943 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6944 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6945 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6946 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6947 "(%d):0380 READ_REV Status x%x "
6948 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6949 mboxq->vport ? mboxq->vport->vpi : 0,
6950 bf_get(lpfc_mqe_status, mqe),
6951 phba->vpd.rev.opFwName,
6952 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6953 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6954
572709e2
JS
6955 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6956 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6957 if (phba->pport->cfg_lun_queue_depth > rc) {
6958 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6959 "3362 LUN queue depth changed from %d to %d\n",
6960 phba->pport->cfg_lun_queue_depth, rc);
6961 phba->pport->cfg_lun_queue_depth = rc;
6962 }
6963
65791f1f 6964 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6965 LPFC_SLI_INTF_IF_TYPE_0) {
6966 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6967 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6968 if (rc == MBX_SUCCESS) {
6969 phba->hba_flag |= HBA_RECOVERABLE_UE;
6970 /* Set 1Sec interval to detect UE */
6971 phba->eratt_poll_interval = 1;
6972 phba->sli4_hba.ue_to_sr = bf_get(
6973 lpfc_mbx_set_feature_UESR,
6974 &mboxq->u.mqe.un.set_feature);
6975 phba->sli4_hba.ue_to_rp = bf_get(
6976 lpfc_mbx_set_feature_UERP,
6977 &mboxq->u.mqe.un.set_feature);
6978 }
6979 }
6980
6981 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6982 /* Enable MDS Diagnostics only if the SLI Port supports it */
6983 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6984 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6985 if (rc != MBX_SUCCESS)
6986 phba->mds_diags_support = 0;
6987 }
572709e2 6988
da0436e9
JS
6989 /*
6990 * Discover the port's supported feature set and match it against the
6991 * hosts requests.
6992 */
6993 lpfc_request_features(phba, mboxq);
6994 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6995 if (unlikely(rc)) {
6996 rc = -EIO;
76a95d75 6997 goto out_free_mbox;
da0436e9
JS
6998 }
6999
7000 /*
7001 * The port must support FCP initiator mode as this is the
7002 * only mode running in the host.
7003 */
7004 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7005 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7006 "0378 No support for fcpi mode.\n");
7007 ftr_rsp++;
7008 }
0bc2b7c5
JS
7009
7010 /* Performance Hints are ONLY for FCoE */
7011 if (phba->hba_flag & HBA_FCOE_MODE) {
7012 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7013 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7014 else
7015 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7016 }
7017
da0436e9
JS
7018 /*
7019 * If the port cannot support the host's requested features
7020 * then turn off the global config parameters to disable the
7021 * feature in the driver. This is not a fatal error.
7022 */
f44ac12f
JS
7023 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7024 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7025 phba->cfg_enable_bg = 0;
7026 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
bf08611b 7027 ftr_rsp++;
f44ac12f 7028 }
bf08611b 7029 }
da0436e9
JS
7030
7031 if (phba->max_vpi && phba->cfg_enable_npiv &&
7032 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7033 ftr_rsp++;
7034
7035 if (ftr_rsp) {
7036 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7037 "0379 Feature Mismatch Data: x%08x %08x "
7038 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7039 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7040 phba->cfg_enable_npiv, phba->max_vpi);
7041 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7042 phba->cfg_enable_bg = 0;
7043 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7044 phba->cfg_enable_npiv = 0;
7045 }
7046
7047 /* These SLI3 features are assumed in SLI4 */
7048 spin_lock_irq(&phba->hbalock);
7049 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7050 spin_unlock_irq(&phba->hbalock);
7051
6d368e53
JS
7052 /*
7053 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7054 * calls depends on these resources to complete port setup.
7055 */
7056 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7057 if (rc) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7059 "2920 Failed to alloc Resource IDs "
7060 "rc = x%x\n", rc);
7061 goto out_free_mbox;
7062 }
7063
61bda8f7
JS
7064 lpfc_set_host_data(phba, mboxq);
7065
7066 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7067 if (rc) {
7068 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7069 "2134 Failed to set host os driver version %x",
7070 rc);
7071 }
7072
da0436e9 7073 /* Read the port's service parameters. */
9f1177a3
JS
7074 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7075 if (rc) {
7076 phba->link_state = LPFC_HBA_ERROR;
7077 rc = -ENOMEM;
76a95d75 7078 goto out_free_mbox;
9f1177a3
JS
7079 }
7080
da0436e9
JS
7081 mboxq->vport = vport;
7082 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7083 mp = (struct lpfc_dmabuf *) mboxq->context1;
7084 if (rc == MBX_SUCCESS) {
7085 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7086 rc = 0;
7087 }
7088
7089 /*
7090 * This memory was allocated by the lpfc_read_sparam routine. Release
7091 * it to the mbuf pool.
7092 */
7093 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7094 kfree(mp);
7095 mboxq->context1 = NULL;
7096 if (unlikely(rc)) {
7097 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7098 "0382 READ_SPARAM command failed "
7099 "status %d, mbxStatus x%x\n",
7100 rc, bf_get(lpfc_mqe_status, mqe));
7101 phba->link_state = LPFC_HBA_ERROR;
7102 rc = -EIO;
76a95d75 7103 goto out_free_mbox;
da0436e9
JS
7104 }
7105
0558056c 7106 lpfc_update_vport_wwn(vport);
da0436e9
JS
7107
7108 /* Update the fc_host data structures with new wwn. */
7109 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7110 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7111
895427bd
JS
7112 /* Create all the SLI4 queues */
7113 rc = lpfc_sli4_queue_create(phba);
7114 if (rc) {
7115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7116 "3089 Failed to allocate queues\n");
7117 rc = -ENODEV;
7118 goto out_free_mbox;
7119 }
7120 /* Set up all the queues to the device */
7121 rc = lpfc_sli4_queue_setup(phba);
7122 if (unlikely(rc)) {
7123 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7124 "0381 Error %d during queue setup.\n ", rc);
7125 goto out_stop_timers;
7126 }
7127 /* Initialize the driver internal SLI layer lists. */
7128 lpfc_sli4_setup(phba);
7129 lpfc_sli4_queue_init(phba);
7130
7131 /* update host els xri-sgl sizes and mappings */
7132 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
7133 if (unlikely(rc)) {
7134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7135 "1400 Failed to update xri-sgl size and "
7136 "mapping: %d\n", rc);
895427bd 7137 goto out_destroy_queue;
da0436e9
JS
7138 }
7139
8a9d2e80 7140 /* register the els sgl pool to the port */
895427bd
JS
7141 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7142 phba->sli4_hba.els_xri_cnt);
7143 if (unlikely(rc < 0)) {
8a9d2e80
JS
7144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7145 "0582 Error %d during els sgl post "
7146 "operation\n", rc);
7147 rc = -ENODEV;
895427bd 7148 goto out_destroy_queue;
8a9d2e80 7149 }
895427bd 7150 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 7151
f358dd0c
JS
7152 if (phba->nvmet_support) {
7153 /* update host nvmet xri-sgl sizes and mappings */
7154 rc = lpfc_sli4_nvmet_sgl_update(phba);
7155 if (unlikely(rc)) {
7156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7157 "6308 Failed to update nvmet-sgl size "
7158 "and mapping: %d\n", rc);
7159 goto out_destroy_queue;
7160 }
7161
7162 /* register the nvmet sgl pool to the port */
7163 rc = lpfc_sli4_repost_sgl_list(
7164 phba,
7165 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7166 phba->sli4_hba.nvmet_xri_cnt);
7167 if (unlikely(rc < 0)) {
7168 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7169 "3117 Error %d during nvmet "
7170 "sgl post\n", rc);
7171 rc = -ENODEV;
7172 goto out_destroy_queue;
7173 }
7174 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
7175
7176 cnt = phba->cfg_iocb_cnt * 1024;
7177 /* We need 1 iocbq for every SGL, for IO processing */
7178 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 7179 } else {
895427bd
JS
7180 /* update host scsi xri-sgl sizes and mappings */
7181 rc = lpfc_sli4_scsi_sgl_update(phba);
7182 if (unlikely(rc)) {
7183 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7184 "6309 Failed to update scsi-sgl size "
7185 "and mapping: %d\n", rc);
7186 goto out_destroy_queue;
7187 }
7188
7189 /* update host nvme xri-sgl sizes and mappings */
7190 rc = lpfc_sli4_nvme_sgl_update(phba);
7191 if (unlikely(rc)) {
7192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7193 "6082 Failed to update nvme-sgl size "
7194 "and mapping: %d\n", rc);
7195 goto out_destroy_queue;
7196 }
6c621a22
JS
7197
7198 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
7199 }
7200
7201 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
7202 /* Initialize and populate the iocb list per host */
7203 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 7204 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
7205 phba->cfg_iocb_cnt, cnt);
7206 rc = lpfc_init_iocb_list(phba, cnt);
7207 if (rc) {
7208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 7209 "1413 Failed to init iocb list.\n");
6c621a22
JS
7210 goto out_destroy_queue;
7211 }
895427bd
JS
7212 }
7213
11e644e2
JS
7214 if (phba->nvmet_support)
7215 lpfc_nvmet_create_targetport(phba);
7216
2d7dbc4c 7217 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
7218 /* Post initial buffers to all RQs created */
7219 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7220 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7221 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7222 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7223 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 7224 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
7225 rqbp->buffer_count = 0;
7226
2d7dbc4c
JS
7227 lpfc_post_rq_buffer(
7228 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7229 phba->sli4_hba.nvmet_mrq_data[i],
2448e484 7230 phba->cfg_nvmet_mrq_post, i);
2d7dbc4c
JS
7231 }
7232 }
7233
895427bd
JS
7234 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7235 /* register the allocated scsi sgl pool to the port */
7236 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7237 if (unlikely(rc)) {
7238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7239 "0383 Error %d during scsi sgl post "
7240 "operation\n", rc);
7241 /* Some Scsi buffers were moved to abort scsi list */
7242 /* A pci function reset will repost them */
7243 rc = -ENODEV;
7244 goto out_destroy_queue;
7245 }
da0436e9
JS
7246 }
7247
01649561
JS
7248 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7249 (phba->nvmet_support == 0)) {
7250
7251 /* register the allocated nvme sgl pool to the port */
7252 rc = lpfc_repost_nvme_sgl_list(phba);
7253 if (unlikely(rc)) {
7254 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7255 "6116 Error %d during nvme sgl post "
7256 "operation\n", rc);
7257 /* Some NVME buffers were moved to abort nvme list */
7258 /* A pci function reset will repost them */
7259 rc = -ENODEV;
7260 goto out_destroy_queue;
7261 }
da0436e9
JS
7262 }
7263
7264 /* Post the rpi header region to the device. */
7265 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7266 if (unlikely(rc)) {
7267 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7268 "0393 Error %d during rpi post operation\n",
7269 rc);
7270 rc = -ENODEV;
895427bd 7271 goto out_destroy_queue;
da0436e9 7272 }
97f2ecf1 7273 lpfc_sli4_node_prep(phba);
da0436e9 7274
895427bd 7275 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7276 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7277 /*
7278 * The FC Port needs to register FCFI (index 0)
7279 */
7280 lpfc_reg_fcfi(phba, mboxq);
7281 mboxq->vport = phba->pport;
7282 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7283 if (rc != MBX_SUCCESS)
7284 goto out_unset_queue;
7285 rc = 0;
7286 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7287 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7288 } else {
7289 /* We are a NVME Target mode with MRQ > 1 */
7290
7291 /* First register the FCFI */
7292 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7293 mboxq->vport = phba->pport;
7294 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7295 if (rc != MBX_SUCCESS)
7296 goto out_unset_queue;
7297 rc = 0;
7298 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7299 &mboxq->u.mqe.un.reg_fcfi_mrq);
7300
7301 /* Next register the MRQs */
7302 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7303 mboxq->vport = phba->pport;
7304 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7305 if (rc != MBX_SUCCESS)
7306 goto out_unset_queue;
7307 rc = 0;
895427bd
JS
7308 }
7309 /* Check if the port is configured to be disabled */
7310 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7311 }
7312
7313 /* Arm the CQs and then EQs on device */
7314 lpfc_sli4_arm_cqeq_intr(phba);
7315
7316 /* Indicate device interrupt mode */
7317 phba->sli4_hba.intr_enable = 1;
7318
7319 /* Allow asynchronous mailbox command to go through */
7320 spin_lock_irq(&phba->hbalock);
7321 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7322 spin_unlock_irq(&phba->hbalock);
7323
7324 /* Post receive buffers to the device */
7325 lpfc_sli4_rb_setup(phba);
7326
fc2b989b
JS
7327 /* Reset HBA FCF states after HBA reset */
7328 phba->fcf.fcf_flag = 0;
7329 phba->fcf.current_rec.flag = 0;
7330
da0436e9 7331 /* Start the ELS watchdog timer */
8fa38513 7332 mod_timer(&vport->els_tmofunc,
256ec0d0 7333 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7334
7335 /* Start heart beat timer */
7336 mod_timer(&phba->hb_tmofunc,
256ec0d0 7337 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7338 phba->hb_outstanding = 0;
7339 phba->last_completion_time = jiffies;
7340
7341 /* Start error attention (ERATT) polling timer */
256ec0d0 7342 mod_timer(&phba->eratt_poll,
65791f1f 7343 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7344
75baf696
JS
7345 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7346 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7347 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7348 if (!rc) {
7349 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7350 "2829 This device supports "
7351 "Advanced Error Reporting (AER)\n");
7352 spin_lock_irq(&phba->hbalock);
7353 phba->hba_flag |= HBA_AER_ENABLED;
7354 spin_unlock_irq(&phba->hbalock);
7355 } else {
7356 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7357 "2830 This device does not support "
7358 "Advanced Error Reporting (AER)\n");
7359 phba->cfg_aer_support = 0;
7360 }
0a96e975 7361 rc = 0;
75baf696
JS
7362 }
7363
da0436e9
JS
7364 /*
7365 * The port is ready, set the host's link state to LINK_DOWN
7366 * in preparation for link interrupts.
7367 */
da0436e9
JS
7368 spin_lock_irq(&phba->hbalock);
7369 phba->link_state = LPFC_LINK_DOWN;
7370 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7371 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7372 (phba->hba_flag & LINK_DISABLED)) {
7373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7374 "3103 Adapter Link is disabled.\n");
7375 lpfc_down_link(phba, mboxq);
7376 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7377 if (rc != MBX_SUCCESS) {
7378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7379 "3104 Adapter failed to issue "
7380 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7381 goto out_unset_queue;
7382 }
7383 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7384 /* don't perform init_link on SLI4 FC port loopback test */
7385 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7386 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7387 if (rc)
7388 goto out_unset_queue;
7389 }
5350d872
JS
7390 }
7391 mempool_free(mboxq, phba->mbox_mem_pool);
7392 return rc;
76a95d75 7393out_unset_queue:
da0436e9 7394 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7395 lpfc_sli4_queue_unset(phba);
7396out_destroy_queue:
6c621a22 7397 lpfc_free_iocb_list(phba);
5350d872 7398 lpfc_sli4_queue_destroy(phba);
da0436e9 7399out_stop_timers:
5350d872 7400 lpfc_stop_hba_timers(phba);
da0436e9
JS
7401out_free_mbox:
7402 mempool_free(mboxq, phba->mbox_mem_pool);
7403 return rc;
7404}
7405
7406/**
7407 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7408 * @ptr: context object - pointer to hba structure.
7409 *
7410 * This is the callback function for mailbox timer. The mailbox
7411 * timer is armed when a new mailbox command is issued and the timer
7412 * is deleted when the mailbox complete. The function is called by
7413 * the kernel timer code when a mailbox does not complete within
7414 * expected time. This function wakes up the worker thread to
7415 * process the mailbox timeout and returns. All the processing is
7416 * done by the worker thread function lpfc_mbox_timeout_handler.
7417 **/
7418void
f22eb4d3 7419lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7420{
f22eb4d3 7421 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7422 unsigned long iflag;
7423 uint32_t tmo_posted;
7424
7425 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7426 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7427 if (!tmo_posted)
7428 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7429 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7430
7431 if (!tmo_posted)
7432 lpfc_worker_wake_up(phba);
7433 return;
7434}
7435
e8d3c3b1
JS
7436/**
7437 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7438 * are pending
7439 * @phba: Pointer to HBA context object.
7440 *
7441 * This function checks if any mailbox completions are present on the mailbox
7442 * completion queue.
7443 **/
3bb11fc5 7444static bool
e8d3c3b1
JS
7445lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7446{
7447
7448 uint32_t idx;
7449 struct lpfc_queue *mcq;
7450 struct lpfc_mcqe *mcqe;
7451 bool pending_completions = false;
7365f6fd 7452 uint8_t qe_valid;
e8d3c3b1
JS
7453
7454 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7455 return false;
7456
7457 /* Check for completions on mailbox completion queue */
7458
7459 mcq = phba->sli4_hba.mbx_cq;
7460 idx = mcq->hba_index;
7365f6fd
JS
7461 qe_valid = mcq->qe_valid;
7462 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
e8d3c3b1
JS
7463 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7464 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7465 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7466 pending_completions = true;
7467 break;
7468 }
7469 idx = (idx + 1) % mcq->entry_count;
7470 if (mcq->hba_index == idx)
7471 break;
7365f6fd
JS
7472
7473 /* if the index wrapped around, toggle the valid bit */
7474 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7475 qe_valid = (qe_valid) ? 0 : 1;
e8d3c3b1
JS
7476 }
7477 return pending_completions;
7478
7479}
7480
7481/**
7482 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7483 * that were missed.
7484 * @phba: Pointer to HBA context object.
7485 *
7486 * For sli4, it is possible to miss an interrupt. As such mbox completions
7487 * maybe missed causing erroneous mailbox timeouts to occur. This function
7488 * checks to see if mbox completions are on the mailbox completion queue
7489 * and will process all the completions associated with the eq for the
7490 * mailbox completion queue.
7491 **/
7492bool
7493lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7494{
b71413dd 7495 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
e8d3c3b1
JS
7496 uint32_t eqidx;
7497 struct lpfc_queue *fpeq = NULL;
7498 struct lpfc_eqe *eqe;
7499 bool mbox_pending;
7500
7501 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7502 return false;
7503
7504 /* Find the eq associated with the mcq */
7505
b71413dd 7506 if (sli4_hba->hba_eq)
895427bd 7507 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
b71413dd
JS
7508 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7509 sli4_hba->mbx_cq->assoc_qid) {
7510 fpeq = sli4_hba->hba_eq[eqidx];
e8d3c3b1
JS
7511 break;
7512 }
7513 if (!fpeq)
7514 return false;
7515
7516 /* Turn off interrupts from this EQ */
7517
b71413dd 7518 sli4_hba->sli4_eq_clr_intr(fpeq);
e8d3c3b1
JS
7519
7520 /* Check to see if a mbox completion is pending */
7521
7522 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7523
7524 /*
7525 * If a mbox completion is pending, process all the events on EQ
7526 * associated with the mbox completion queue (this could include
7527 * mailbox commands, async events, els commands, receive queue data
7528 * and fcp commands)
7529 */
7530
7531 if (mbox_pending)
7532 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7533 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7534 fpeq->EQ_processed++;
7535 }
7536
7537 /* Always clear and re-arm the EQ */
7538
b71413dd 7539 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
e8d3c3b1
JS
7540
7541 return mbox_pending;
7542
7543}
da0436e9
JS
7544
7545/**
7546 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7547 * @phba: Pointer to HBA context object.
7548 *
7549 * This function is called from worker thread when a mailbox command times out.
7550 * The caller is not required to hold any locks. This function will reset the
7551 * HBA and recover all the pending commands.
7552 **/
7553void
7554lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7555{
7556 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7557 MAILBOX_t *mb = NULL;
7558
da0436e9 7559 struct lpfc_sli *psli = &phba->sli;
da0436e9 7560
e8d3c3b1
JS
7561 /* If the mailbox completed, process the completion and return */
7562 if (lpfc_sli4_process_missed_mbox_completions(phba))
7563 return;
7564
eb016566
JS
7565 if (pmbox != NULL)
7566 mb = &pmbox->u.mb;
da0436e9
JS
7567 /* Check the pmbox pointer first. There is a race condition
7568 * between the mbox timeout handler getting executed in the
7569 * worklist and the mailbox actually completing. When this
7570 * race condition occurs, the mbox_active will be NULL.
7571 */
7572 spin_lock_irq(&phba->hbalock);
7573 if (pmbox == NULL) {
7574 lpfc_printf_log(phba, KERN_WARNING,
7575 LOG_MBOX | LOG_SLI,
7576 "0353 Active Mailbox cleared - mailbox timeout "
7577 "exiting\n");
7578 spin_unlock_irq(&phba->hbalock);
7579 return;
7580 }
7581
7582 /* Mbox cmd <mbxCommand> timeout */
7583 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7584 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7585 mb->mbxCommand,
7586 phba->pport->port_state,
7587 phba->sli.sli_flag,
7588 phba->sli.mbox_active);
7589 spin_unlock_irq(&phba->hbalock);
7590
7591 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7592 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7593 * it to fail all outstanding SCSI IO.
da0436e9
JS
7594 */
7595 spin_lock_irq(&phba->pport->work_port_lock);
7596 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7597 spin_unlock_irq(&phba->pport->work_port_lock);
7598 spin_lock_irq(&phba->hbalock);
7599 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7600 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7601 spin_unlock_irq(&phba->hbalock);
7602
db55fba8 7603 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7604
7605 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7606 "0345 Resetting board due to mailbox timeout\n");
7607
7608 /* Reset the HBA device */
7609 lpfc_reset_hba(phba);
7610}
7611
7612/**
7613 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7614 * @phba: Pointer to HBA context object.
7615 * @pmbox: Pointer to mailbox object.
7616 * @flag: Flag indicating how the mailbox need to be processed.
7617 *
7618 * This function is called by discovery code and HBA management code
7619 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7620 * function gets the hbalock to protect the data structures.
7621 * The mailbox command can be submitted in polling mode, in which case
7622 * this function will wait in a polling loop for the completion of the
7623 * mailbox.
7624 * If the mailbox is submitted in no_wait mode (not polling) the
7625 * function will submit the command and returns immediately without waiting
7626 * for the mailbox completion. The no_wait is supported only when HBA
7627 * is in SLI2/SLI3 mode - interrupts are enabled.
7628 * The SLI interface allows only one mailbox pending at a time. If the
7629 * mailbox is issued in polling mode and there is already a mailbox
7630 * pending, then the function will return an error. If the mailbox is issued
7631 * in NO_WAIT mode and there is a mailbox pending already, the function
7632 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7633 * The sli layer owns the mailbox object until the completion of mailbox
7634 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7635 * return codes the caller owns the mailbox command after the return of
7636 * the function.
e59058c4 7637 **/
3772a991
JS
7638static int
7639lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7640 uint32_t flag)
dea3101e 7641{
bf07bdea 7642 MAILBOX_t *mbx;
2e0fef85 7643 struct lpfc_sli *psli = &phba->sli;
dea3101e 7644 uint32_t status, evtctr;
9940b97b 7645 uint32_t ha_copy, hc_copy;
dea3101e 7646 int i;
09372820 7647 unsigned long timeout;
dea3101e 7648 unsigned long drvr_flag = 0;
34b02dcd 7649 uint32_t word0, ldata;
dea3101e 7650 void __iomem *to_slim;
58da1ffb
JS
7651 int processing_queue = 0;
7652
7653 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7654 if (!pmbox) {
8568a4d2 7655 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7656 /* processing mbox queue from intr_handler */
3772a991
JS
7657 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7658 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7659 return MBX_SUCCESS;
7660 }
58da1ffb 7661 processing_queue = 1;
58da1ffb
JS
7662 pmbox = lpfc_mbox_get(phba);
7663 if (!pmbox) {
7664 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7665 return MBX_SUCCESS;
7666 }
7667 }
dea3101e 7668
ed957684 7669 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7670 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7671 if(!pmbox->vport) {
58da1ffb 7672 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7673 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7674 LOG_MBOX | LOG_VPORT,
e8b62011 7675 "1806 Mbox x%x failed. No vport\n",
3772a991 7676 pmbox->u.mb.mbxCommand);
ed957684 7677 dump_stack();
58da1ffb 7678 goto out_not_finished;
ed957684
JS
7679 }
7680 }
7681
8d63f375 7682 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7683 if (unlikely(pci_channel_offline(phba->pcidev))) {
7684 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7685 goto out_not_finished;
7686 }
8d63f375 7687
a257bf90
JS
7688 /* If HBA has a deferred error attention, fail the iocb. */
7689 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7691 goto out_not_finished;
7692 }
7693
dea3101e 7694 psli = &phba->sli;
92d7f7b0 7695
bf07bdea 7696 mbx = &pmbox->u.mb;
dea3101e 7697 status = MBX_SUCCESS;
7698
2e0fef85
JS
7699 if (phba->link_state == LPFC_HBA_ERROR) {
7700 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7701
7702 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7703 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7704 "(%d):0311 Mailbox command x%x cannot "
7705 "issue Data: x%x x%x\n",
7706 pmbox->vport ? pmbox->vport->vpi : 0,
7707 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7708 goto out_not_finished;
41415862
JW
7709 }
7710
bf07bdea 7711 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7712 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7713 !(hc_copy & HC_MBINT_ENA)) {
7714 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7715 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7716 "(%d):2528 Mailbox command x%x cannot "
7717 "issue Data: x%x x%x\n",
7718 pmbox->vport ? pmbox->vport->vpi : 0,
7719 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7720 goto out_not_finished;
7721 }
9290831f
JS
7722 }
7723
dea3101e 7724 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7725 /* Polling for a mbox command when another one is already active
7726 * is not allowed in SLI. Also, the driver must have established
7727 * SLI2 mode to queue and process multiple mbox commands.
7728 */
7729
7730 if (flag & MBX_POLL) {
2e0fef85 7731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7732
7733 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7734 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7735 "(%d):2529 Mailbox command x%x "
7736 "cannot issue Data: x%x x%x\n",
7737 pmbox->vport ? pmbox->vport->vpi : 0,
7738 pmbox->u.mb.mbxCommand,
7739 psli->sli_flag, flag);
58da1ffb 7740 goto out_not_finished;
dea3101e 7741 }
7742
3772a991 7743 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7744 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7745 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7747 "(%d):2530 Mailbox command x%x "
7748 "cannot issue Data: x%x x%x\n",
7749 pmbox->vport ? pmbox->vport->vpi : 0,
7750 pmbox->u.mb.mbxCommand,
7751 psli->sli_flag, flag);
58da1ffb 7752 goto out_not_finished;
dea3101e 7753 }
7754
dea3101e 7755 /* Another mailbox command is still being processed, queue this
7756 * command to be processed later.
7757 */
7758 lpfc_mbox_put(phba, pmbox);
7759
7760 /* Mbox cmd issue - BUSY */
ed957684 7761 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7762 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7763 "x%x x%x x%x x%x\n",
92d7f7b0 7764 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
7765 mbx->mbxCommand,
7766 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7767 psli->sli_flag, flag);
dea3101e 7768
7769 psli->slistat.mbox_busy++;
2e0fef85 7770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7771
858c9f6c
JS
7772 if (pmbox->vport) {
7773 lpfc_debugfs_disc_trc(pmbox->vport,
7774 LPFC_DISC_TRC_MBOX_VPORT,
7775 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7776 (uint32_t)mbx->mbxCommand,
7777 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7778 }
7779 else {
7780 lpfc_debugfs_disc_trc(phba->pport,
7781 LPFC_DISC_TRC_MBOX,
7782 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7783 (uint32_t)mbx->mbxCommand,
7784 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7785 }
7786
2e0fef85 7787 return MBX_BUSY;
dea3101e 7788 }
7789
dea3101e 7790 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7791
7792 /* If we are not polling, we MUST be in SLI2 mode */
7793 if (flag != MBX_POLL) {
3772a991 7794 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7795 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7796 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7797 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7798 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7799 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7800 "(%d):2531 Mailbox command x%x "
7801 "cannot issue Data: x%x x%x\n",
7802 pmbox->vport ? pmbox->vport->vpi : 0,
7803 pmbox->u.mb.mbxCommand,
7804 psli->sli_flag, flag);
58da1ffb 7805 goto out_not_finished;
dea3101e 7806 }
7807 /* timeout active mbox command */
256ec0d0
JS
7808 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7809 1000);
7810 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e 7811 }
7812
7813 /* Mailbox cmd <cmd> issue */
ed957684 7814 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7815 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7816 "x%x\n",
e8b62011 7817 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
7818 mbx->mbxCommand,
7819 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7820 psli->sli_flag, flag);
dea3101e 7821
bf07bdea 7822 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7823 if (pmbox->vport) {
7824 lpfc_debugfs_disc_trc(pmbox->vport,
7825 LPFC_DISC_TRC_MBOX_VPORT,
7826 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7827 (uint32_t)mbx->mbxCommand,
7828 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7829 }
7830 else {
7831 lpfc_debugfs_disc_trc(phba->pport,
7832 LPFC_DISC_TRC_MBOX,
7833 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7834 (uint32_t)mbx->mbxCommand,
7835 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7836 }
7837 }
7838
dea3101e 7839 psli->slistat.mbox_cmd++;
7840 evtctr = psli->slistat.mbox_event;
7841
7842 /* next set own bit for the adapter and copy over command word */
bf07bdea 7843 mbx->mbxOwner = OWN_CHIP;
dea3101e 7844
3772a991 7845 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7846 /* Populate mbox extension offset word. */
7847 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7848 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7849 = (uint8_t *)phba->mbox_ext
7850 - (uint8_t *)phba->mbox;
7851 }
7852
7853 /* Copy the mailbox extension data */
7854 if (pmbox->in_ext_byte_len && pmbox->context2) {
7855 lpfc_sli_pcimem_bcopy(pmbox->context2,
7856 (uint8_t *)phba->mbox_ext,
7857 pmbox->in_ext_byte_len);
7858 }
7859 /* Copy command data to host SLIM area */
bf07bdea 7860 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7861 } else {
7a470277
JS
7862 /* Populate mbox extension offset word. */
7863 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7864 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7865 = MAILBOX_HBA_EXT_OFFSET;
7866
7867 /* Copy the mailbox extension data */
895427bd 7868 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7869 lpfc_memcpy_to_slim(phba->MBslimaddr +
7870 MAILBOX_HBA_EXT_OFFSET,
7871 pmbox->context2, pmbox->in_ext_byte_len);
7872
895427bd 7873 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7874 /* copy command data into host mbox for cmpl */
895427bd
JS
7875 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7876 MAILBOX_CMD_SIZE);
dea3101e 7877
7878 /* First copy mbox command data to HBA SLIM, skip past first
7879 word */
7880 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7881 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e 7882 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7883
7884 /* Next copy over first word, with mbxOwner set */
bf07bdea 7885 ldata = *((uint32_t *)mbx);
dea3101e 7886 to_slim = phba->MBslimaddr;
7887 writel(ldata, to_slim);
7888 readl(to_slim); /* flush */
7889
895427bd 7890 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7891 /* switch over to host mailbox */
3772a991 7892 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 7893 }
7894
7895 wmb();
dea3101e 7896
7897 switch (flag) {
7898 case MBX_NOWAIT:
09372820 7899 /* Set up reference to mailbox command */
dea3101e 7900 psli->mbox_active = pmbox;
09372820
JS
7901 /* Interrupt board to do it */
7902 writel(CA_MBATT, phba->CAregaddr);
7903 readl(phba->CAregaddr); /* flush */
7904 /* Don't wait for it to finish, just return */
dea3101e 7905 break;
7906
7907 case MBX_POLL:
09372820 7908 /* Set up null reference to mailbox command */
dea3101e 7909 psli->mbox_active = NULL;
09372820
JS
7910 /* Interrupt board to do it */
7911 writel(CA_MBATT, phba->CAregaddr);
7912 readl(phba->CAregaddr); /* flush */
7913
3772a991 7914 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7915 /* First read mbox status word */
34b02dcd 7916 word0 = *((uint32_t *)phba->mbox);
dea3101e 7917 word0 = le32_to_cpu(word0);
7918 } else {
7919 /* First read mbox status word */
9940b97b
JS
7920 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7921 spin_unlock_irqrestore(&phba->hbalock,
7922 drvr_flag);
7923 goto out_not_finished;
7924 }
dea3101e 7925 }
7926
7927 /* Read the HBA Host Attention Register */
9940b97b
JS
7928 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7929 spin_unlock_irqrestore(&phba->hbalock,
7930 drvr_flag);
7931 goto out_not_finished;
7932 }
a183a15f
JS
7933 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7934 1000) + jiffies;
09372820 7935 i = 0;
dea3101e 7936 /* Wait for command to complete */
41415862
JW
7937 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7938 (!(ha_copy & HA_MBATT) &&
2e0fef85 7939 (phba->link_state > LPFC_WARM_START))) {
09372820 7940 if (time_after(jiffies, timeout)) {
dea3101e 7941 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7942 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7943 drvr_flag);
58da1ffb 7944 goto out_not_finished;
dea3101e 7945 }
7946
7947 /* Check if we took a mbox interrupt while we were
7948 polling */
7949 if (((word0 & OWN_CHIP) != OWN_CHIP)
7950 && (evtctr != psli->slistat.mbox_event))
7951 break;
7952
09372820
JS
7953 if (i++ > 10) {
7954 spin_unlock_irqrestore(&phba->hbalock,
7955 drvr_flag);
7956 msleep(1);
7957 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7958 }
dea3101e 7959
3772a991 7960 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7961 /* First copy command data */
34b02dcd 7962 word0 = *((uint32_t *)phba->mbox);
dea3101e 7963 word0 = le32_to_cpu(word0);
bf07bdea 7964 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7965 MAILBOX_t *slimmb;
34b02dcd 7966 uint32_t slimword0;
dea3101e 7967 /* Check real SLIM for any errors */
7968 slimword0 = readl(phba->MBslimaddr);
7969 slimmb = (MAILBOX_t *) & slimword0;
7970 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7971 && slimmb->mbxStatus) {
7972 psli->sli_flag &=
3772a991 7973 ~LPFC_SLI_ACTIVE;
dea3101e 7974 word0 = slimword0;
7975 }
7976 }
7977 } else {
7978 /* First copy command data */
7979 word0 = readl(phba->MBslimaddr);
7980 }
7981 /* Read the HBA Host Attention Register */
9940b97b
JS
7982 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7983 spin_unlock_irqrestore(&phba->hbalock,
7984 drvr_flag);
7985 goto out_not_finished;
7986 }
dea3101e 7987 }
7988
3772a991 7989 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7990 /* copy results back to user */
2ea259ee
JS
7991 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7992 MAILBOX_CMD_SIZE);
7a470277
JS
7993 /* Copy the mailbox extension data */
7994 if (pmbox->out_ext_byte_len && pmbox->context2) {
7995 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7996 pmbox->context2,
7997 pmbox->out_ext_byte_len);
7998 }
dea3101e 7999 } else {
8000 /* First copy command data */
bf07bdea 8001 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 8002 MAILBOX_CMD_SIZE);
7a470277
JS
8003 /* Copy the mailbox extension data */
8004 if (pmbox->out_ext_byte_len && pmbox->context2) {
8005 lpfc_memcpy_from_slim(pmbox->context2,
8006 phba->MBslimaddr +
8007 MAILBOX_HBA_EXT_OFFSET,
8008 pmbox->out_ext_byte_len);
dea3101e 8009 }
8010 }
8011
8012 writel(HA_MBATT, phba->HAregaddr);
8013 readl(phba->HAregaddr); /* flush */
8014
8015 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 8016 status = mbx->mbxStatus;
dea3101e 8017 }
8018
2e0fef85
JS
8019 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8020 return status;
58da1ffb
JS
8021
8022out_not_finished:
8023 if (processing_queue) {
da0436e9 8024 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
8025 lpfc_mbox_cmpl_put(phba, pmbox);
8026 }
8027 return MBX_NOT_FINISHED;
dea3101e 8028}
8029
f1126688
JS
8030/**
8031 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8032 * @phba: Pointer to HBA context object.
8033 *
8034 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8035 * the driver internal pending mailbox queue. It will then try to wait out the
8036 * possible outstanding mailbox command before return.
8037 *
8038 * Returns:
8039 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8040 * the outstanding mailbox command timed out.
8041 **/
8042static int
8043lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8044{
8045 struct lpfc_sli *psli = &phba->sli;
f1126688 8046 int rc = 0;
a183a15f 8047 unsigned long timeout = 0;
f1126688
JS
8048
8049 /* Mark the asynchronous mailbox command posting as blocked */
8050 spin_lock_irq(&phba->hbalock);
8051 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
8052 /* Determine how long we might wait for the active mailbox
8053 * command to be gracefully completed by firmware.
8054 */
a183a15f
JS
8055 if (phba->sli.mbox_active)
8056 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8057 phba->sli.mbox_active) *
8058 1000) + jiffies;
8059 spin_unlock_irq(&phba->hbalock);
8060
e8d3c3b1
JS
8061 /* Make sure the mailbox is really active */
8062 if (timeout)
8063 lpfc_sli4_process_missed_mbox_completions(phba);
8064
f1126688
JS
8065 /* Wait for the outstnading mailbox command to complete */
8066 while (phba->sli.mbox_active) {
8067 /* Check active mailbox complete status every 2ms */
8068 msleep(2);
8069 if (time_after(jiffies, timeout)) {
8070 /* Timeout, marked the outstanding cmd not complete */
8071 rc = 1;
8072 break;
8073 }
8074 }
8075
8076 /* Can not cleanly block async mailbox command, fails it */
8077 if (rc) {
8078 spin_lock_irq(&phba->hbalock);
8079 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8080 spin_unlock_irq(&phba->hbalock);
8081 }
8082 return rc;
8083}
8084
8085/**
8086 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8087 * @phba: Pointer to HBA context object.
8088 *
8089 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8090 * commands from the driver internal pending mailbox queue. It makes sure
8091 * that there is no outstanding mailbox command before resuming posting
8092 * asynchronous mailbox commands. If, for any reason, there is outstanding
8093 * mailbox command, it will try to wait it out before resuming asynchronous
8094 * mailbox command posting.
8095 **/
8096static void
8097lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8098{
8099 struct lpfc_sli *psli = &phba->sli;
8100
8101 spin_lock_irq(&phba->hbalock);
8102 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8103 /* Asynchronous mailbox posting is not blocked, do nothing */
8104 spin_unlock_irq(&phba->hbalock);
8105 return;
8106 }
8107
8108 /* Outstanding synchronous mailbox command is guaranteed to be done,
8109 * successful or timeout, after timing-out the outstanding mailbox
8110 * command shall always be removed, so just unblock posting async
8111 * mailbox command and resume
8112 */
8113 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8114 spin_unlock_irq(&phba->hbalock);
8115
8116 /* wake up worker thread to post asynchronlous mailbox command */
8117 lpfc_worker_wake_up(phba);
8118}
8119
2d843edc
JS
8120/**
8121 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8122 * @phba: Pointer to HBA context object.
8123 * @mboxq: Pointer to mailbox object.
8124 *
8125 * The function waits for the bootstrap mailbox register ready bit from
8126 * port for twice the regular mailbox command timeout value.
8127 *
8128 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8129 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8130 **/
8131static int
8132lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8133{
8134 uint32_t db_ready;
8135 unsigned long timeout;
8136 struct lpfc_register bmbx_reg;
8137
8138 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8139 * 1000) + jiffies;
8140
8141 do {
8142 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8143 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8144 if (!db_ready)
8145 msleep(2);
8146
8147 if (time_after(jiffies, timeout))
8148 return MBXERR_ERROR;
8149 } while (!db_ready);
8150
8151 return 0;
8152}
8153
da0436e9
JS
8154/**
8155 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8156 * @phba: Pointer to HBA context object.
8157 * @mboxq: Pointer to mailbox object.
8158 *
8159 * The function posts a mailbox to the port. The mailbox is expected
8160 * to be comletely filled in and ready for the port to operate on it.
8161 * This routine executes a synchronous completion operation on the
8162 * mailbox by polling for its completion.
8163 *
8164 * The caller must not be holding any locks when calling this routine.
8165 *
8166 * Returns:
8167 * MBX_SUCCESS - mailbox posted successfully
8168 * Any of the MBX error values.
8169 **/
8170static int
8171lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8172{
8173 int rc = MBX_SUCCESS;
8174 unsigned long iflag;
da0436e9
JS
8175 uint32_t mcqe_status;
8176 uint32_t mbx_cmnd;
da0436e9
JS
8177 struct lpfc_sli *psli = &phba->sli;
8178 struct lpfc_mqe *mb = &mboxq->u.mqe;
8179 struct lpfc_bmbx_create *mbox_rgn;
8180 struct dma_address *dma_address;
da0436e9
JS
8181
8182 /*
8183 * Only one mailbox can be active to the bootstrap mailbox region
8184 * at a time and there is no queueing provided.
8185 */
8186 spin_lock_irqsave(&phba->hbalock, iflag);
8187 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8188 spin_unlock_irqrestore(&phba->hbalock, iflag);
8189 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8190 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8191 "cannot issue Data: x%x x%x\n",
8192 mboxq->vport ? mboxq->vport->vpi : 0,
8193 mboxq->u.mb.mbxCommand,
a183a15f
JS
8194 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8195 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8196 psli->sli_flag, MBX_POLL);
8197 return MBXERR_ERROR;
8198 }
8199 /* The server grabs the token and owns it until release */
8200 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8201 phba->sli.mbox_active = mboxq;
8202 spin_unlock_irqrestore(&phba->hbalock, iflag);
8203
2d843edc
JS
8204 /* wait for bootstrap mbox register for readyness */
8205 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8206 if (rc)
8207 goto exit;
8208
da0436e9
JS
8209 /*
8210 * Initialize the bootstrap memory region to avoid stale data areas
8211 * in the mailbox post. Then copy the caller's mailbox contents to
8212 * the bmbx mailbox region.
8213 */
8214 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8215 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
48f8fdb4
JS
8216 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8217 sizeof(struct lpfc_mqe));
da0436e9
JS
8218
8219 /* Post the high mailbox dma address to the port and wait for ready. */
8220 dma_address = &phba->sli4_hba.bmbx.dma_address;
8221 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8222
2d843edc
JS
8223 /* wait for bootstrap mbox register for hi-address write done */
8224 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8225 if (rc)
8226 goto exit;
da0436e9
JS
8227
8228 /* Post the low mailbox dma address to the port. */
8229 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 8230
2d843edc
JS
8231 /* wait for bootstrap mbox register for low address write done */
8232 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8233 if (rc)
8234 goto exit;
da0436e9
JS
8235
8236 /*
8237 * Read the CQ to ensure the mailbox has completed.
8238 * If so, update the mailbox status so that the upper layers
8239 * can complete the request normally.
8240 */
48f8fdb4
JS
8241 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8242 sizeof(struct lpfc_mqe));
da0436e9 8243 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
48f8fdb4
JS
8244 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8245 sizeof(struct lpfc_mcqe));
da0436e9 8246 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
8247 /*
8248 * When the CQE status indicates a failure and the mailbox status
8249 * indicates success then copy the CQE status into the mailbox status
8250 * (and prefix it with x4000).
8251 */
da0436e9 8252 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8253 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8254 bf_set(lpfc_mqe_status, mb,
8255 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8256 rc = MBXERR_ERROR;
d7c47992
JS
8257 } else
8258 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8259
8260 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8261 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8262 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8263 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8264 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8265 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8266 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8267 bf_get(lpfc_mqe_status, mb),
8268 mb->un.mb_words[0], mb->un.mb_words[1],
8269 mb->un.mb_words[2], mb->un.mb_words[3],
8270 mb->un.mb_words[4], mb->un.mb_words[5],
8271 mb->un.mb_words[6], mb->un.mb_words[7],
8272 mb->un.mb_words[8], mb->un.mb_words[9],
8273 mb->un.mb_words[10], mb->un.mb_words[11],
8274 mb->un.mb_words[12], mboxq->mcqe.word0,
8275 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8276 mboxq->mcqe.trailer);
8277exit:
8278 /* We are holding the token, no needed for lock when release */
8279 spin_lock_irqsave(&phba->hbalock, iflag);
8280 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8281 phba->sli.mbox_active = NULL;
8282 spin_unlock_irqrestore(&phba->hbalock, iflag);
8283 return rc;
8284}
8285
8286/**
8287 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8288 * @phba: Pointer to HBA context object.
8289 * @pmbox: Pointer to mailbox object.
8290 * @flag: Flag indicating how the mailbox need to be processed.
8291 *
8292 * This function is called by discovery code and HBA management code to submit
8293 * a mailbox command to firmware with SLI-4 interface spec.
8294 *
8295 * Return codes the caller owns the mailbox command after the return of the
8296 * function.
8297 **/
8298static int
8299lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8300 uint32_t flag)
8301{
8302 struct lpfc_sli *psli = &phba->sli;
8303 unsigned long iflags;
8304 int rc;
8305
b76f2dc9
JS
8306 /* dump from issue mailbox command if setup */
8307 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8308
8fa38513
JS
8309 rc = lpfc_mbox_dev_check(phba);
8310 if (unlikely(rc)) {
8311 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8312 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8313 "cannot issue Data: x%x x%x\n",
8314 mboxq->vport ? mboxq->vport->vpi : 0,
8315 mboxq->u.mb.mbxCommand,
a183a15f
JS
8316 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8317 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8318 psli->sli_flag, flag);
8319 goto out_not_finished;
8320 }
8321
da0436e9
JS
8322 /* Detect polling mode and jump to a handler */
8323 if (!phba->sli4_hba.intr_enable) {
8324 if (flag == MBX_POLL)
8325 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8326 else
8327 rc = -EIO;
8328 if (rc != MBX_SUCCESS)
0558056c 8329 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8330 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8331 "(x%x/x%x) failure: "
8332 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8333 "Data: x%x x%x\n,",
da0436e9
JS
8334 mboxq->vport ? mboxq->vport->vpi : 0,
8335 mboxq->u.mb.mbxCommand,
a183a15f
JS
8336 lpfc_sli_config_mbox_subsys_get(phba,
8337 mboxq),
8338 lpfc_sli_config_mbox_opcode_get(phba,
8339 mboxq),
cc459f19
JS
8340 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8341 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8342 bf_get(lpfc_mcqe_ext_status,
8343 &mboxq->mcqe),
da0436e9
JS
8344 psli->sli_flag, flag);
8345 return rc;
8346 } else if (flag == MBX_POLL) {
f1126688
JS
8347 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8348 "(%d):2542 Try to issue mailbox command "
7365f6fd 8349 "x%x (x%x/x%x) synchronously ahead of async "
f1126688 8350 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8351 mboxq->vport ? mboxq->vport->vpi : 0,
8352 mboxq->u.mb.mbxCommand,
a183a15f
JS
8353 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8354 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8355 psli->sli_flag, flag);
f1126688
JS
8356 /* Try to block the asynchronous mailbox posting */
8357 rc = lpfc_sli4_async_mbox_block(phba);
8358 if (!rc) {
8359 /* Successfully blocked, now issue sync mbox cmd */
8360 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8361 if (rc != MBX_SUCCESS)
cc459f19 8362 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8363 LOG_MBOX | LOG_SLI,
cc459f19
JS
8364 "(%d):2597 Sync Mailbox command "
8365 "x%x (x%x/x%x) failure: "
8366 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8367 "Data: x%x x%x\n,",
8368 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8369 mboxq->u.mb.mbxCommand,
8370 lpfc_sli_config_mbox_subsys_get(phba,
8371 mboxq),
8372 lpfc_sli_config_mbox_opcode_get(phba,
8373 mboxq),
cc459f19
JS
8374 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8375 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8376 bf_get(lpfc_mcqe_ext_status,
8377 &mboxq->mcqe),
a183a15f 8378 psli->sli_flag, flag);
f1126688
JS
8379 /* Unblock the async mailbox posting afterward */
8380 lpfc_sli4_async_mbox_unblock(phba);
8381 }
8382 return rc;
da0436e9
JS
8383 }
8384
8385 /* Now, interrupt mode asynchrous mailbox command */
8386 rc = lpfc_mbox_cmd_check(phba, mboxq);
8387 if (rc) {
8388 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8389 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8390 "cannot issue Data: x%x x%x\n",
8391 mboxq->vport ? mboxq->vport->vpi : 0,
8392 mboxq->u.mb.mbxCommand,
a183a15f
JS
8393 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8394 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8395 psli->sli_flag, flag);
8396 goto out_not_finished;
8397 }
da0436e9
JS
8398
8399 /* Put the mailbox command to the driver internal FIFO */
8400 psli->slistat.mbox_busy++;
8401 spin_lock_irqsave(&phba->hbalock, iflags);
8402 lpfc_mbox_put(phba, mboxq);
8403 spin_unlock_irqrestore(&phba->hbalock, iflags);
8404 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8405 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8406 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8407 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8408 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8409 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8410 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8411 phba->pport->port_state,
8412 psli->sli_flag, MBX_NOWAIT);
8413 /* Wake up worker thread to transport mailbox command from head */
8414 lpfc_worker_wake_up(phba);
8415
8416 return MBX_BUSY;
8417
8418out_not_finished:
8419 return MBX_NOT_FINISHED;
8420}
8421
8422/**
8423 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8424 * @phba: Pointer to HBA context object.
8425 *
8426 * This function is called by worker thread to send a mailbox command to
8427 * SLI4 HBA firmware.
8428 *
8429 **/
8430int
8431lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8432{
8433 struct lpfc_sli *psli = &phba->sli;
8434 LPFC_MBOXQ_t *mboxq;
8435 int rc = MBX_SUCCESS;
8436 unsigned long iflags;
8437 struct lpfc_mqe *mqe;
8438 uint32_t mbx_cmnd;
8439
8440 /* Check interrupt mode before post async mailbox command */
8441 if (unlikely(!phba->sli4_hba.intr_enable))
8442 return MBX_NOT_FINISHED;
8443
8444 /* Check for mailbox command service token */
8445 spin_lock_irqsave(&phba->hbalock, iflags);
8446 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8447 spin_unlock_irqrestore(&phba->hbalock, iflags);
8448 return MBX_NOT_FINISHED;
8449 }
8450 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8451 spin_unlock_irqrestore(&phba->hbalock, iflags);
8452 return MBX_NOT_FINISHED;
8453 }
8454 if (unlikely(phba->sli.mbox_active)) {
8455 spin_unlock_irqrestore(&phba->hbalock, iflags);
8456 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8457 "0384 There is pending active mailbox cmd\n");
8458 return MBX_NOT_FINISHED;
8459 }
8460 /* Take the mailbox command service token */
8461 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8462
8463 /* Get the next mailbox command from head of queue */
8464 mboxq = lpfc_mbox_get(phba);
8465
8466 /* If no more mailbox command waiting for post, we're done */
8467 if (!mboxq) {
8468 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8469 spin_unlock_irqrestore(&phba->hbalock, iflags);
8470 return MBX_SUCCESS;
8471 }
8472 phba->sli.mbox_active = mboxq;
8473 spin_unlock_irqrestore(&phba->hbalock, iflags);
8474
8475 /* Check device readiness for posting mailbox command */
8476 rc = lpfc_mbox_dev_check(phba);
8477 if (unlikely(rc))
8478 /* Driver clean routine will clean up pending mailbox */
8479 goto out_not_finished;
8480
8481 /* Prepare the mbox command to be posted */
8482 mqe = &mboxq->u.mqe;
8483 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8484
8485 /* Start timer for the mbox_tmo and log some mailbox post messages */
8486 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8487 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8488
8489 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8490 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8491 "x%x x%x\n",
8492 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8493 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8494 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8495 phba->pport->port_state, psli->sli_flag);
8496
8497 if (mbx_cmnd != MBX_HEARTBEAT) {
8498 if (mboxq->vport) {
8499 lpfc_debugfs_disc_trc(mboxq->vport,
8500 LPFC_DISC_TRC_MBOX_VPORT,
8501 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8502 mbx_cmnd, mqe->un.mb_words[0],
8503 mqe->un.mb_words[1]);
8504 } else {
8505 lpfc_debugfs_disc_trc(phba->pport,
8506 LPFC_DISC_TRC_MBOX,
8507 "MBOX Send: cmd:x%x mb:x%x x%x",
8508 mbx_cmnd, mqe->un.mb_words[0],
8509 mqe->un.mb_words[1]);
8510 }
8511 }
8512 psli->slistat.mbox_cmd++;
8513
8514 /* Post the mailbox command to the port */
8515 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8516 if (rc != MBX_SUCCESS) {
8517 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8518 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8519 "cannot issue Data: x%x x%x\n",
8520 mboxq->vport ? mboxq->vport->vpi : 0,
8521 mboxq->u.mb.mbxCommand,
a183a15f
JS
8522 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8523 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8524 psli->sli_flag, MBX_NOWAIT);
8525 goto out_not_finished;
8526 }
8527
8528 return rc;
8529
8530out_not_finished:
8531 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8532 if (phba->sli.mbox_active) {
8533 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8534 __lpfc_mbox_cmpl_put(phba, mboxq);
8535 /* Release the token */
8536 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8537 phba->sli.mbox_active = NULL;
8538 }
da0436e9
JS
8539 spin_unlock_irqrestore(&phba->hbalock, iflags);
8540
8541 return MBX_NOT_FINISHED;
8542}
8543
8544/**
8545 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8546 * @phba: Pointer to HBA context object.
8547 * @pmbox: Pointer to mailbox object.
8548 * @flag: Flag indicating how the mailbox need to be processed.
8549 *
8550 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8551 * the API jump table function pointer from the lpfc_hba struct.
8552 *
8553 * Return codes the caller owns the mailbox command after the return of the
8554 * function.
8555 **/
8556int
8557lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8558{
8559 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8560}
8561
8562/**
25985edc 8563 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8564 * @phba: The hba struct for which this call is being executed.
8565 * @dev_grp: The HBA PCI-Device group number.
8566 *
8567 * This routine sets up the mbox interface API function jump table in @phba
8568 * struct.
8569 * Returns: 0 - success, -ENODEV - failure.
8570 **/
8571int
8572lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8573{
8574
8575 switch (dev_grp) {
8576 case LPFC_PCI_DEV_LP:
8577 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8578 phba->lpfc_sli_handle_slow_ring_event =
8579 lpfc_sli_handle_slow_ring_event_s3;
8580 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8581 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8582 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8583 break;
8584 case LPFC_PCI_DEV_OC:
8585 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8586 phba->lpfc_sli_handle_slow_ring_event =
8587 lpfc_sli_handle_slow_ring_event_s4;
8588 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8589 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8590 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8591 break;
8592 default:
8593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8594 "1420 Invalid HBA PCI-device group: 0x%x\n",
8595 dev_grp);
8596 return -ENODEV;
8597 break;
8598 }
8599 return 0;
8600}
8601
e59058c4 8602/**
3621a710 8603 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8604 * @phba: Pointer to HBA context object.
8605 * @pring: Pointer to driver SLI ring object.
8606 * @piocb: Pointer to address of newly added command iocb.
8607 *
8608 * This function is called with hbalock held to add a command
8609 * iocb to the txq when SLI layer cannot submit the command iocb
8610 * to the ring.
8611 **/
2a9bf3d0 8612void
92d7f7b0 8613__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8614 struct lpfc_iocbq *piocb)
dea3101e 8615{
1c2ba475 8616 lockdep_assert_held(&phba->hbalock);
dea3101e 8617 /* Insert the caller's iocb in the txq tail for later processing. */
8618 list_add_tail(&piocb->list, &pring->txq);
dea3101e 8619}
8620
e59058c4 8621/**
3621a710 8622 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8623 * @phba: Pointer to HBA context object.
8624 * @pring: Pointer to driver SLI ring object.
8625 * @piocb: Pointer to address of newly added command iocb.
8626 *
8627 * This function is called with hbalock held before a new
8628 * iocb is submitted to the firmware. This function checks
8629 * txq to flush the iocbs in txq to Firmware before
8630 * submitting new iocbs to the Firmware.
8631 * If there are iocbs in the txq which need to be submitted
8632 * to firmware, lpfc_sli_next_iocb returns the first element
8633 * of the txq after dequeuing it from txq.
8634 * If there is no iocb in the txq then the function will return
8635 * *piocb and *piocb is set to NULL. Caller needs to check
8636 * *piocb to find if there are more commands in the txq.
8637 **/
dea3101e 8638static struct lpfc_iocbq *
8639lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8640 struct lpfc_iocbq **piocb)
dea3101e 8641{
8642 struct lpfc_iocbq * nextiocb;
8643
1c2ba475
JT
8644 lockdep_assert_held(&phba->hbalock);
8645
dea3101e 8646 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8647 if (!nextiocb) {
8648 nextiocb = *piocb;
8649 *piocb = NULL;
8650 }
8651
8652 return nextiocb;
8653}
8654
e59058c4 8655/**
3772a991 8656 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8657 * @phba: Pointer to HBA context object.
3772a991 8658 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8659 * @piocb: Pointer to command iocb.
8660 * @flag: Flag indicating if this command can be put into txq.
8661 *
3772a991
JS
8662 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8663 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8664 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8665 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8666 * this function allows only iocbs for posting buffers. This function finds
8667 * next available slot in the command ring and posts the command to the
8668 * available slot and writes the port attention register to request HBA start
8669 * processing new iocb. If there is no slot available in the ring and
8670 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8671 * the function returns IOCB_BUSY.
e59058c4 8672 *
3772a991
JS
8673 * This function is called with hbalock held. The function will return success
8674 * after it successfully submit the iocb to firmware or after adding to the
8675 * txq.
e59058c4 8676 **/
98c9ea5c 8677static int
3772a991 8678__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 8679 struct lpfc_iocbq *piocb, uint32_t flag)
8680{
8681 struct lpfc_iocbq *nextiocb;
8682 IOCB_t *iocb;
895427bd 8683 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8684
1c2ba475
JT
8685 lockdep_assert_held(&phba->hbalock);
8686
92d7f7b0
JS
8687 if (piocb->iocb_cmpl && (!piocb->vport) &&
8688 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8689 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8690 lpfc_printf_log(phba, KERN_ERR,
8691 LOG_SLI | LOG_VPORT,
e8b62011 8692 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8693 piocb->iocb.ulpCommand);
8694 dump_stack();
8695 return IOCB_ERROR;
8696 }
8697
8698
8d63f375
LV
8699 /* If the PCI channel is in offline state, do not post iocbs. */
8700 if (unlikely(pci_channel_offline(phba->pcidev)))
8701 return IOCB_ERROR;
8702
a257bf90
JS
8703 /* If HBA has a deferred error attention, fail the iocb. */
8704 if (unlikely(phba->hba_flag & DEFER_ERATT))
8705 return IOCB_ERROR;
8706
dea3101e 8707 /*
8708 * We should never get an IOCB if we are in a < LINK_DOWN state
8709 */
2e0fef85 8710 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 8711 return IOCB_ERROR;
8712
8713 /*
8714 * Check to see if we are blocking IOCB processing because of a
0b727fea 8715 * outstanding event.
dea3101e 8716 */
0b727fea 8717 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 8718 goto iocb_busy;
8719
2e0fef85 8720 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8721 /*
2680eeaa 8722 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 8723 * can be issued if the link is not up.
8724 */
8725 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8726 case CMD_GEN_REQUEST64_CR:
8727 case CMD_GEN_REQUEST64_CX:
8728 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8729 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8730 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8731 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8732 MENLO_TRANSPORT_TYPE))
8733
8734 goto iocb_busy;
8735 break;
dea3101e 8736 case CMD_QUE_RING_BUF_CN:
8737 case CMD_QUE_RING_BUF64_CN:
dea3101e 8738 /*
8739 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8740 * completion, iocb_cmpl MUST be 0.
8741 */
8742 if (piocb->iocb_cmpl)
8743 piocb->iocb_cmpl = NULL;
8744 /*FALLTHROUGH*/
8745 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8746 case CMD_CLOSE_XRI_CN:
8747 case CMD_CLOSE_XRI_CX:
dea3101e 8748 break;
8749 default:
8750 goto iocb_busy;
8751 }
8752
8753 /*
8754 * For FCP commands, we must be in a state where we can process link
8755 * attention events.
8756 */
895427bd 8757 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8758 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8759 goto iocb_busy;
92d7f7b0 8760 }
dea3101e 8761
dea3101e 8762 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8763 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8764 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8765
8766 if (iocb)
8767 lpfc_sli_update_ring(phba, pring);
8768 else
8769 lpfc_sli_update_full_ring(phba, pring);
8770
8771 if (!piocb)
8772 return IOCB_SUCCESS;
8773
8774 goto out_busy;
8775
8776 iocb_busy:
8777 pring->stats.iocb_cmd_delay++;
8778
8779 out_busy:
8780
8781 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8782 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 8783 return IOCB_SUCCESS;
8784 }
8785
8786 return IOCB_BUSY;
8787}
8788
3772a991 8789/**
4f774513
JS
8790 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8791 * @phba: Pointer to HBA context object.
8792 * @piocb: Pointer to command iocb.
8793 * @sglq: Pointer to the scatter gather queue object.
8794 *
8795 * This routine converts the bpl or bde that is in the IOCB
8796 * to a sgl list for the sli4 hardware. The physical address
8797 * of the bpl/bde is converted back to a virtual address.
8798 * If the IOCB contains a BPL then the list of BDE's is
8799 * converted to sli4_sge's. If the IOCB contains a single
8800 * BDE then it is converted to a single sli_sge.
8801 * The IOCB is still in cpu endianess so the contents of
8802 * the bpl can be used without byte swapping.
8803 *
8804 * Returns valid XRI = Success, NO_XRI = Failure.
8805**/
8806static uint16_t
8807lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8808 struct lpfc_sglq *sglq)
3772a991 8809{
4f774513
JS
8810 uint16_t xritag = NO_XRI;
8811 struct ulp_bde64 *bpl = NULL;
8812 struct ulp_bde64 bde;
8813 struct sli4_sge *sgl = NULL;
1b51197d 8814 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8815 IOCB_t *icmd;
8816 int numBdes = 0;
8817 int i = 0;
63e801ce
JS
8818 uint32_t offset = 0; /* accumulated offset in the sg request list */
8819 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8820
4f774513
JS
8821 if (!piocbq || !sglq)
8822 return xritag;
8823
8824 sgl = (struct sli4_sge *)sglq->sgl;
8825 icmd = &piocbq->iocb;
6b5151fd
JS
8826 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8827 return sglq->sli4_xritag;
4f774513
JS
8828 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8829 numBdes = icmd->un.genreq64.bdl.bdeSize /
8830 sizeof(struct ulp_bde64);
8831 /* The addrHigh and addrLow fields within the IOCB
8832 * have not been byteswapped yet so there is no
8833 * need to swap them back.
8834 */
1b51197d
JS
8835 if (piocbq->context3)
8836 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8837 else
8838 return xritag;
4f774513 8839
1b51197d 8840 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8841 if (!bpl)
8842 return xritag;
8843
8844 for (i = 0; i < numBdes; i++) {
8845 /* Should already be byte swapped. */
28baac74
JS
8846 sgl->addr_hi = bpl->addrHigh;
8847 sgl->addr_lo = bpl->addrLow;
8848
0558056c 8849 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8850 if ((i+1) == numBdes)
8851 bf_set(lpfc_sli4_sge_last, sgl, 1);
8852 else
8853 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8854 /* swap the size field back to the cpu so we
8855 * can assign it to the sgl.
8856 */
8857 bde.tus.w = le32_to_cpu(bpl->tus.w);
8858 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8859 /* The offsets in the sgl need to be accumulated
8860 * separately for the request and reply lists.
8861 * The request is always first, the reply follows.
8862 */
8863 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8864 /* add up the reply sg entries */
8865 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8866 inbound++;
8867 /* first inbound? reset the offset */
8868 if (inbound == 1)
8869 offset = 0;
8870 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8871 bf_set(lpfc_sli4_sge_type, sgl,
8872 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8873 offset += bde.tus.f.bdeSize;
8874 }
546fc854 8875 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8876 bpl++;
8877 sgl++;
8878 }
8879 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8880 /* The addrHigh and addrLow fields of the BDE have not
8881 * been byteswapped yet so they need to be swapped
8882 * before putting them in the sgl.
8883 */
8884 sgl->addr_hi =
8885 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8886 sgl->addr_lo =
8887 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8888 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8889 bf_set(lpfc_sli4_sge_last, sgl, 1);
8890 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8891 sgl->sge_len =
8892 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8893 }
8894 return sglq->sli4_xritag;
3772a991 8895}
92d7f7b0 8896
e59058c4 8897/**
4f774513 8898 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8899 * @phba: Pointer to HBA context object.
4f774513
JS
8900 * @piocb: Pointer to command iocb.
8901 * @wqe: Pointer to the work queue entry.
e59058c4 8902 *
4f774513
JS
8903 * This routine converts the iocb command to its Work Queue Entry
8904 * equivalent. The wqe pointer should not have any fields set when
8905 * this routine is called because it will memcpy over them.
8906 * This routine does not set the CQ_ID or the WQEC bits in the
8907 * wqe.
e59058c4 8908 *
4f774513 8909 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8910 **/
cf5bf97e 8911static int
4f774513 8912lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
205e8240 8913 union lpfc_wqe128 *wqe)
cf5bf97e 8914{
5ffc266e 8915 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8916 uint8_t ct = 0;
8917 uint32_t fip;
8918 uint32_t abort_tag;
8919 uint8_t command_type = ELS_COMMAND_NON_FIP;
8920 uint8_t cmnd;
8921 uint16_t xritag;
dcf2a4e0
JS
8922 uint16_t abrt_iotag;
8923 struct lpfc_iocbq *abrtiocbq;
4f774513 8924 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8925 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8926 int numBdes, i;
8927 struct ulp_bde64 bde;
c31098ce 8928 struct lpfc_nodelist *ndlp;
ff78d8f9 8929 uint32_t *pcmd;
1b51197d 8930 uint32_t if_type;
4f774513 8931
45ed1190 8932 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8933 /* The fcp commands will set command type */
0c287589 8934 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8935 command_type = FCP_COMMAND;
c868595d 8936 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8937 command_type = ELS_COMMAND_FIP;
8938 else
8939 command_type = ELS_COMMAND_NON_FIP;
8940
b5c53958
JS
8941 if (phba->fcp_embed_io)
8942 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8943 /* Some of the fields are in the right position already */
8944 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
8945 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8946 /* The ct field has moved so reset */
8947 wqe->generic.wqe_com.word7 = 0;
8948 wqe->generic.wqe_com.word10 = 0;
8949 }
b5c53958
JS
8950
8951 abort_tag = (uint32_t) iocbq->iotag;
8952 xritag = iocbq->sli4_xritag;
4f774513
JS
8953 /* words0-2 bpl convert bde */
8954 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8955 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8956 sizeof(struct ulp_bde64);
4f774513
JS
8957 bpl = (struct ulp_bde64 *)
8958 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8959 if (!bpl)
8960 return IOCB_ERROR;
cf5bf97e 8961
4f774513
JS
8962 /* Should already be byte swapped. */
8963 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8964 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8965 /* swap the size field back to the cpu so we
8966 * can assign it to the sgl.
8967 */
8968 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8969 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8970 total_len = 0;
8971 for (i = 0; i < numBdes; i++) {
8972 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8973 total_len += bde.tus.f.bdeSize;
8974 }
4f774513 8975 } else
5ffc266e 8976 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8977
4f774513
JS
8978 iocbq->iocb.ulpIoTag = iocbq->iotag;
8979 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8980
4f774513
JS
8981 switch (iocbq->iocb.ulpCommand) {
8982 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8983 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8984 ndlp = iocbq->context_un.ndlp;
8985 else
8986 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8987 if (!iocbq->iocb.ulpLe) {
8988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8989 "2007 Only Limited Edition cmd Format"
8990 " supported 0x%x\n",
8991 iocbq->iocb.ulpCommand);
8992 return IOCB_ERROR;
8993 }
ff78d8f9 8994
5ffc266e 8995 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8996 /* Els_reguest64 has a TMO */
8997 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8998 iocbq->iocb.ulpTimeout);
8999 /* Need a VF for word 4 set the vf bit*/
9000 bf_set(els_req64_vf, &wqe->els_req, 0);
9001 /* And a VFID for word 12 */
9002 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 9003 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
9004 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9005 iocbq->iocb.ulpContext);
9006 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9007 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 9008 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 9009 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
9010 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9011 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
9012 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9013 iocbq->context2)->virt);
1b51197d
JS
9014 if_type = bf_get(lpfc_sli_intf_if_type,
9015 &phba->sli4_hba.sli_intf);
27d6ac0a 9016 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 9017 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 9018 *pcmd == ELS_CMD_SCR ||
6b5151fd 9019 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 9020 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
9021 *pcmd == ELS_CMD_PLOGI)) {
9022 bf_set(els_req64_sp, &wqe->els_req, 1);
9023 bf_set(els_req64_sid, &wqe->els_req,
9024 iocbq->vport->fc_myDID);
939723a4
JS
9025 if ((*pcmd == ELS_CMD_FLOGI) &&
9026 !(phba->fc_topology ==
9027 LPFC_TOPOLOGY_LOOP))
9028 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
9029 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9030 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 9031 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 9032 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
9033 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9034 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9035 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9036 }
c868595d 9037 }
6d368e53
JS
9038 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9039 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
9040 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9041 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9042 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9043 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9044 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9045 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 9046 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 9047 break;
5ffc266e 9048 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
9049 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9050 iocbq->iocb.un.ulpWord[3]);
9051 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 9052 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
9053 /* The entire sequence is transmitted for this IOCB */
9054 xmit_len = total_len;
9055 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
9056 if (phba->link_flag & LS_LOOPBACK_MODE)
9057 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 9058 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
9059 /* word3 iocb=io_tag32 wqe=reserved */
9060 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
9061 /* word4 relative_offset memcpy */
9062 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
9063 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9064 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9065 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9066 LPFC_WQE_IOD_WRITE);
9067 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9068 LPFC_WQE_LENLOC_WORD12);
9069 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
9070 wqe->xmit_sequence.xmit_len = xmit_len;
9071 command_type = OTHER_COMMAND;
7851fe2c 9072 break;
4f774513 9073 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
9074 /* word3 iocb=iotag32 wqe=seq_payload_len */
9075 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
9076 /* word4 iocb=rsvd wqe=rsvd */
9077 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9078 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 9079 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 9080 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
9081 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9082 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9083 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9084 LPFC_WQE_LENLOC_WORD3);
9085 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 9086 break;
4f774513
JS
9087 case CMD_FCP_IWRITE64_CR:
9088 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
9089 /* word3 iocb=iotag wqe=payload_offset_len */
9090 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9091 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9092 xmit_len + sizeof(struct fcp_rsp));
9093 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9094 0);
f0d9bccc
JS
9095 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9096 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9097 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9098 iocbq->iocb.ulpFCP2Rcvy);
9099 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9100 /* Always open the exchange */
f0d9bccc
JS
9101 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9102 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9103 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9104 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9105 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
9106 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9107 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
9108 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9109 if (iocbq->priority) {
9110 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9111 (iocbq->priority << 1));
9112 } else {
1ba981fd
JS
9113 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9114 (phba->cfg_XLanePriority << 1));
9115 }
9116 }
b5c53958
JS
9117 /* Note, word 10 is already initialized to 0 */
9118
0bc2b7c5
JS
9119 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9120 if (phba->fcp_embed_pbde)
9121 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9122 else
9123 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9124
b5c53958
JS
9125 if (phba->fcp_embed_io) {
9126 struct lpfc_scsi_buf *lpfc_cmd;
9127 struct sli4_sge *sgl;
b5c53958
JS
9128 struct fcp_cmnd *fcp_cmnd;
9129 uint32_t *ptr;
9130
9131 /* 128 byte wqe support here */
b5c53958
JS
9132
9133 lpfc_cmd = iocbq->context1;
9134 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9135 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9136
9137 /* Word 0-2 - FCP_CMND */
205e8240 9138 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9139 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9140 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9141 wqe->generic.bde.addrHigh = 0;
9142 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9143
205e8240
JS
9144 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9145 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
b5c53958
JS
9146
9147 /* Word 22-29 FCP CMND Payload */
205e8240 9148 ptr = &wqe->words[22];
b5c53958
JS
9149 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9150 }
7851fe2c 9151 break;
4f774513 9152 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
9153 /* word3 iocb=iotag wqe=payload_offset_len */
9154 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9155 bf_set(payload_offset_len, &wqe->fcp_iread,
9156 xmit_len + sizeof(struct fcp_rsp));
9157 bf_set(cmd_buff_len, &wqe->fcp_iread,
9158 0);
f0d9bccc
JS
9159 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9160 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9161 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9162 iocbq->iocb.ulpFCP2Rcvy);
9163 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 9164 /* Always open the exchange */
f0d9bccc
JS
9165 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9166 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9167 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9168 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9169 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
9170 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9171 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
9172 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9173 if (iocbq->priority) {
9174 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9175 (iocbq->priority << 1));
9176 } else {
1ba981fd
JS
9177 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9178 (phba->cfg_XLanePriority << 1));
9179 }
9180 }
b5c53958
JS
9181 /* Note, word 10 is already initialized to 0 */
9182
0bc2b7c5
JS
9183 /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9184 if (phba->fcp_embed_pbde)
9185 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9186 else
9187 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9188
b5c53958
JS
9189 if (phba->fcp_embed_io) {
9190 struct lpfc_scsi_buf *lpfc_cmd;
9191 struct sli4_sge *sgl;
b5c53958
JS
9192 struct fcp_cmnd *fcp_cmnd;
9193 uint32_t *ptr;
9194
9195 /* 128 byte wqe support here */
b5c53958
JS
9196
9197 lpfc_cmd = iocbq->context1;
9198 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9199 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9200
9201 /* Word 0-2 - FCP_CMND */
205e8240 9202 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9203 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9204 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9205 wqe->generic.bde.addrHigh = 0;
9206 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9207
205e8240
JS
9208 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9209 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
b5c53958
JS
9210
9211 /* Word 22-29 FCP CMND Payload */
205e8240 9212 ptr = &wqe->words[22];
b5c53958
JS
9213 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9214 }
7851fe2c 9215 break;
4f774513 9216 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
9217 /* word3 iocb=iotag wqe=payload_offset_len */
9218 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9219 bf_set(payload_offset_len, &wqe->fcp_icmd,
9220 xmit_len + sizeof(struct fcp_rsp));
9221 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9222 0);
f0d9bccc 9223 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 9224 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 9225 /* Always open the exchange */
f0d9bccc
JS
9226 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9227 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9228 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9229 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9230 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
9231 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9232 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
9233 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9234 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
9235 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9236 if (iocbq->priority) {
9237 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9238 (iocbq->priority << 1));
9239 } else {
1ba981fd
JS
9240 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9241 (phba->cfg_XLanePriority << 1));
9242 }
9243 }
b5c53958
JS
9244 /* Note, word 10 is already initialized to 0 */
9245
9246 if (phba->fcp_embed_io) {
9247 struct lpfc_scsi_buf *lpfc_cmd;
9248 struct sli4_sge *sgl;
b5c53958
JS
9249 struct fcp_cmnd *fcp_cmnd;
9250 uint32_t *ptr;
9251
9252 /* 128 byte wqe support here */
b5c53958
JS
9253
9254 lpfc_cmd = iocbq->context1;
9255 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9256 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9257
9258 /* Word 0-2 - FCP_CMND */
205e8240 9259 wqe->generic.bde.tus.f.bdeFlags =
b5c53958 9260 BUFF_TYPE_BDE_IMMED;
205e8240
JS
9261 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9262 wqe->generic.bde.addrHigh = 0;
9263 wqe->generic.bde.addrLow = 88; /* Word 22 */
b5c53958 9264
205e8240
JS
9265 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9266 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
b5c53958
JS
9267
9268 /* Word 22-29 FCP CMND Payload */
205e8240 9269 ptr = &wqe->words[22];
b5c53958
JS
9270 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9271 }
7851fe2c 9272 break;
4f774513 9273 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9274 /* For this command calculate the xmit length of the
9275 * request bde.
9276 */
9277 xmit_len = 0;
9278 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9279 sizeof(struct ulp_bde64);
9280 for (i = 0; i < numBdes; i++) {
63e801ce 9281 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9282 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9283 break;
63e801ce
JS
9284 xmit_len += bde.tus.f.bdeSize;
9285 }
f0d9bccc
JS
9286 /* word3 iocb=IO_TAG wqe=request_payload_len */
9287 wqe->gen_req.request_payload_len = xmit_len;
9288 /* word4 iocb=parameter wqe=relative_offset memcpy */
9289 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9290 /* word6 context tag copied in memcpy */
9291 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9292 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9294 "2015 Invalid CT %x command 0x%x\n",
9295 ct, iocbq->iocb.ulpCommand);
9296 return IOCB_ERROR;
9297 }
f0d9bccc
JS
9298 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9299 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9300 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9301 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9302 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9303 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9304 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9305 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9306 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9307 command_type = OTHER_COMMAND;
7851fe2c 9308 break;
4f774513 9309 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9310 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9311 /* words0-2 BDE memcpy */
f0d9bccc
JS
9312 /* word3 iocb=iotag32 wqe=response_payload_len */
9313 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9314 /* word4 */
9315 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9316 /* word5 iocb=rsvd wge=did */
9317 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9318 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9319
9320 if_type = bf_get(lpfc_sli_intf_if_type,
9321 &phba->sli4_hba.sli_intf);
27d6ac0a 9322 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
939723a4
JS
9323 if (iocbq->vport->fc_flag & FC_PT2PT) {
9324 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9325 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9326 iocbq->vport->fc_myDID);
9327 if (iocbq->vport->fc_myDID == Fabric_DID) {
9328 bf_set(wqe_els_did,
9329 &wqe->xmit_els_rsp.wqe_dest, 0);
9330 }
9331 }
9332 }
f0d9bccc
JS
9333 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9334 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9335 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9336 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9337 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9338 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9339 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9340 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9341 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9342 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9343 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9344 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9345 LPFC_WQE_LENLOC_WORD3);
9346 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9347 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9348 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9349 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9350 iocbq->context2)->virt);
9351 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9352 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9353 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9354 iocbq->vport->fc_myDID);
939723a4
JS
9355 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9356 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9357 phba->vpi_ids[phba->pport->vpi]);
9358 }
4f774513 9359 command_type = OTHER_COMMAND;
7851fe2c 9360 break;
4f774513
JS
9361 case CMD_CLOSE_XRI_CN:
9362 case CMD_ABORT_XRI_CN:
9363 case CMD_ABORT_XRI_CX:
9364 /* words 0-2 memcpy should be 0 rserved */
9365 /* port will send abts */
dcf2a4e0
JS
9366 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9367 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9368 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9369 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9370 } else
9371 fip = 0;
9372
9373 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9374 /*
dcf2a4e0
JS
9375 * The link is down, or the command was ELS_FIP
9376 * so the fw does not need to send abts
4f774513
JS
9377 * on the wire.
9378 */
9379 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9380 else
9381 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9382 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9383 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9384 wqe->abort_cmd.rsrvd5 = 0;
9385 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9386 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9387 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9388 /*
9389 * The abort handler will send us CMD_ABORT_XRI_CN or
9390 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9391 */
f0d9bccc
JS
9392 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9393 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9394 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9395 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9396 cmnd = CMD_ABORT_XRI_CX;
9397 command_type = OTHER_COMMAND;
9398 xritag = 0;
7851fe2c 9399 break;
6669f9bb 9400 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9401 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9402 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9403 * we re-construct this WQE here based on information in
9404 * iocbq from scratch.
9405 */
9406 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9407 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9408 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9409 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9410 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9411 LPFC_ABTS_UNSOL_INT) {
9412 /* ABTS sent by initiator to CT exchange, the
9413 * RX_ID field will be filled with the newly
9414 * allocated responder XRI.
9415 */
9416 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9417 iocbq->sli4_xritag);
9418 } else {
9419 /* ABTS sent by responder to CT exchange, the
9420 * RX_ID field will be filled with the responder
9421 * RX_ID from ABTS.
9422 */
9423 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9424 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9425 }
6669f9bb
JS
9426 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9427 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9428
9429 /* Use CT=VPI */
9430 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9431 ndlp->nlp_DID);
9432 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9433 iocbq->iocb.ulpContext);
9434 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9435 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9436 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9437 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9438 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9439 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9440 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9441 command_type = OTHER_COMMAND;
546fc854
JS
9442 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9443 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9444 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9445 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9446 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9447 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9448 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9449 }
9450
7851fe2c 9451 break;
ae9e28f3
JS
9452 case CMD_SEND_FRAME:
9453 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9454 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9455 return 0;
4f774513
JS
9456 case CMD_XRI_ABORTED_CX:
9457 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9458 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9459 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9460 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9461 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9462 default:
9463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9464 "2014 Invalid command 0x%x\n",
9465 iocbq->iocb.ulpCommand);
9466 return IOCB_ERROR;
7851fe2c 9467 break;
4f774513 9468 }
6d368e53 9469
8012cc38
JS
9470 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9471 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9472 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9473 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9474 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9475 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9476 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9477 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9478 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9479 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9480 wqe->generic.wqe_com.abort_tag = abort_tag;
9481 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9482 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9483 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9484 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9485 return 0;
9486}
9487
9488/**
9489 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9490 * @phba: Pointer to HBA context object.
9491 * @ring_number: SLI ring number to issue iocb on.
9492 * @piocb: Pointer to command iocb.
9493 * @flag: Flag indicating if this command can be put into txq.
9494 *
9495 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9496 * an iocb command to an HBA with SLI-4 interface spec.
9497 *
9498 * This function is called with hbalock held. The function will return success
9499 * after it successfully submit the iocb to firmware or after adding to the
9500 * txq.
9501 **/
9502static int
9503__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9504 struct lpfc_iocbq *piocb, uint32_t flag)
9505{
9506 struct lpfc_sglq *sglq;
205e8240 9507 union lpfc_wqe128 wqe;
1ba981fd 9508 struct lpfc_queue *wq;
895427bd 9509 struct lpfc_sli_ring *pring;
4f774513 9510
895427bd
JS
9511 /* Get the WQ */
9512 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9513 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9514 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9515 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9516 else
9517 wq = phba->sli4_hba.oas_wq;
9518 } else {
9519 wq = phba->sli4_hba.els_wq;
9520 }
9521
9522 /* Get corresponding ring */
9523 pring = wq->pring;
1c2ba475 9524
b5c53958
JS
9525 /*
9526 * The WQE can be either 64 or 128 bytes,
b5c53958 9527 */
b5c53958 9528
895427bd
JS
9529 lockdep_assert_held(&phba->hbalock);
9530
4f774513
JS
9531 if (piocb->sli4_xritag == NO_XRI) {
9532 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9533 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9534 sglq = NULL;
9535 else {
0e9bb8d7 9536 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9537 if (!(flag & SLI_IOCB_RET_IOCB)) {
9538 __lpfc_sli_ringtx_put(phba,
9539 pring, piocb);
9540 return IOCB_SUCCESS;
9541 } else {
9542 return IOCB_BUSY;
9543 }
9544 } else {
895427bd 9545 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9546 if (!sglq) {
9547 if (!(flag & SLI_IOCB_RET_IOCB)) {
9548 __lpfc_sli_ringtx_put(phba,
9549 pring,
9550 piocb);
9551 return IOCB_SUCCESS;
9552 } else
9553 return IOCB_BUSY;
9554 }
9555 }
4f774513 9556 }
2ea259ee 9557 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9558 /* These IO's already have an XRI and a mapped sgl. */
9559 sglq = NULL;
2ea259ee 9560 else {
6d368e53
JS
9561 /*
9562 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9563 * sglq is on the active list
9564 */
edccdc17 9565 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9566 if (!sglq)
9567 return IOCB_ERROR;
9568 }
9569
9570 if (sglq) {
6d368e53 9571 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9572 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9573 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9574 return IOCB_ERROR;
9575 }
9576
205e8240 9577 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
4f774513
JS
9578 return IOCB_ERROR;
9579
205e8240 9580 if (lpfc_sli4_wq_put(wq, &wqe))
895427bd 9581 return IOCB_ERROR;
4f774513
JS
9582 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9583
9584 return 0;
9585}
9586
9587/**
9588 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9589 *
9590 * This routine wraps the actual lockless version for issusing IOCB function
9591 * pointer from the lpfc_hba struct.
9592 *
9593 * Return codes:
b5c53958
JS
9594 * IOCB_ERROR - Error
9595 * IOCB_SUCCESS - Success
9596 * IOCB_BUSY - Busy
4f774513 9597 **/
2a9bf3d0 9598int
4f774513
JS
9599__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9600 struct lpfc_iocbq *piocb, uint32_t flag)
9601{
9602 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9603}
9604
9605/**
25985edc 9606 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9607 * @phba: The hba struct for which this call is being executed.
9608 * @dev_grp: The HBA PCI-Device group number.
9609 *
9610 * This routine sets up the SLI interface API function jump table in @phba
9611 * struct.
9612 * Returns: 0 - success, -ENODEV - failure.
9613 **/
9614int
9615lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9616{
9617
9618 switch (dev_grp) {
9619 case LPFC_PCI_DEV_LP:
9620 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9621 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9622 break;
9623 case LPFC_PCI_DEV_OC:
9624 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9625 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9626 break;
9627 default:
9628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9629 "1419 Invalid HBA PCI-device group: 0x%x\n",
9630 dev_grp);
9631 return -ENODEV;
9632 break;
9633 }
9634 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9635 return 0;
9636}
9637
a1efe163 9638/**
895427bd 9639 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9640 * @phba: Pointer to HBA context object.
a1efe163
JS
9641 * @piocb: Pointer to command iocb.
9642 *
895427bd
JS
9643 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9644 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9645 * Since ABORTS must go on the same WQ of the command they are
895427bd 9646 * aborting, we use command's hba_wqidx.
a1efe163 9647 */
895427bd
JS
9648struct lpfc_sli_ring *
9649lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9650{
895427bd 9651 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9652 if (!(phba->cfg_fof) ||
895427bd 9653 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9654 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9655 return NULL;
8b0dff14 9656 /*
895427bd 9657 * for abort iocb hba_wqidx should already
8b0dff14
JS
9658 * be setup based on what work queue we used.
9659 */
8e036a94 9660 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9661 piocb->hba_wqidx =
8b0dff14
JS
9662 lpfc_sli4_scmd_to_wqidx_distr(phba,
9663 piocb->context1);
8e036a94
DK
9664 piocb->hba_wqidx = piocb->hba_wqidx %
9665 phba->cfg_fcp_io_channel;
9666 }
895427bd 9667 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9668 } else {
9669 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9670 return NULL;
9671 piocb->hba_wqidx = 0;
9672 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9673 }
895427bd
JS
9674 } else {
9675 if (unlikely(!phba->sli4_hba.els_wq))
9676 return NULL;
9677 piocb->hba_wqidx = 0;
9678 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9679 }
9bd2bff5
JS
9680}
9681
4f774513
JS
9682/**
9683 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9684 * @phba: Pointer to HBA context object.
9685 * @pring: Pointer to driver SLI ring object.
9686 * @piocb: Pointer to command iocb.
9687 * @flag: Flag indicating if this command can be put into txq.
9688 *
9689 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9690 * function. This function gets the hbalock and calls
9691 * __lpfc_sli_issue_iocb function and will return the error returned
9692 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9693 * functions which do not hold hbalock.
9694 **/
9695int
9696lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9697 struct lpfc_iocbq *piocb, uint32_t flag)
9698{
895427bd 9699 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9700 struct lpfc_sli_ring *pring;
ba20c853
JS
9701 struct lpfc_queue *fpeq;
9702 struct lpfc_eqe *eqe;
4f774513 9703 unsigned long iflags;
2a76a283 9704 int rc, idx;
4f774513 9705
7e56aa25 9706 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9707 pring = lpfc_sli4_calc_ring(phba, piocb);
9708 if (unlikely(pring == NULL))
9bd2bff5 9709 return IOCB_ERROR;
ba20c853 9710
9bd2bff5
JS
9711 spin_lock_irqsave(&pring->ring_lock, iflags);
9712 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9713 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9714
9bd2bff5 9715 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9716 idx = piocb->hba_wqidx;
9717 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9718
895427bd 9719 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9720
9bd2bff5
JS
9721 /* Get associated EQ with this index */
9722 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9723
9bd2bff5 9724 /* Turn off interrupts from this EQ */
b71413dd 9725 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 9726
9bd2bff5
JS
9727 /*
9728 * Process all the events on FCP EQ
9729 */
9730 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9731 lpfc_sli4_hba_handle_eqe(phba,
9732 eqe, idx);
9733 fpeq->EQ_processed++;
ba20c853 9734 }
ba20c853 9735
9bd2bff5 9736 /* Always clear and re-arm the EQ */
b71413dd 9737 phba->sli4_hba.sli4_eq_release(fpeq,
9bd2bff5
JS
9738 LPFC_QUEUE_REARM);
9739 }
895427bd 9740 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9741 }
7e56aa25
JS
9742 } else {
9743 /* For now, SLI2/3 will still use hbalock */
9744 spin_lock_irqsave(&phba->hbalock, iflags);
9745 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9746 spin_unlock_irqrestore(&phba->hbalock, iflags);
9747 }
4f774513
JS
9748 return rc;
9749}
9750
9751/**
9752 * lpfc_extra_ring_setup - Extra ring setup function
9753 * @phba: Pointer to HBA context object.
9754 *
9755 * This function is called while driver attaches with the
9756 * HBA to setup the extra ring. The extra ring is used
9757 * only when driver needs to support target mode functionality
9758 * or IP over FC functionalities.
9759 *
895427bd 9760 * This function is called with no lock held. SLI3 only.
4f774513
JS
9761 **/
9762static int
9763lpfc_extra_ring_setup( struct lpfc_hba *phba)
9764{
9765 struct lpfc_sli *psli;
9766 struct lpfc_sli_ring *pring;
9767
9768 psli = &phba->sli;
9769
9770 /* Adjust cmd/rsp ring iocb entries more evenly */
9771
9772 /* Take some away from the FCP ring */
895427bd 9773 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9774 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9775 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9776 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9777 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9778
a4bc3379 9779 /* and give them to the extra ring */
895427bd 9780 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9781
7e56aa25
JS
9782 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9783 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9784 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9785 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9786
9787 /* Setup default profile for this ring */
9788 pring->iotag_max = 4096;
9789 pring->num_mask = 1;
9790 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9791 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9792 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9793 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9794 return 0;
9795}
9796
cb69f7de
JS
9797/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9798 * @phba: Pointer to HBA context object.
9799 * @iocbq: Pointer to iocb object.
9800 *
9801 * The async_event handler calls this routine when it receives
9802 * an ASYNC_STATUS_CN event from the port. The port generates
9803 * this event when an Abort Sequence request to an rport fails
9804 * twice in succession. The abort could be originated by the
9805 * driver or by the port. The ABTS could have been for an ELS
9806 * or FCP IO. The port only generates this event when an ABTS
9807 * fails to complete after one retry.
9808 */
9809static void
9810lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9811 struct lpfc_iocbq *iocbq)
9812{
9813 struct lpfc_nodelist *ndlp = NULL;
9814 uint16_t rpi = 0, vpi = 0;
9815 struct lpfc_vport *vport = NULL;
9816
9817 /* The rpi in the ulpContext is vport-sensitive. */
9818 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9819 rpi = iocbq->iocb.ulpContext;
9820
9821 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9822 "3092 Port generated ABTS async event "
9823 "on vpi %d rpi %d status 0x%x\n",
9824 vpi, rpi, iocbq->iocb.ulpStatus);
9825
9826 vport = lpfc_find_vport_by_vpid(phba, vpi);
9827 if (!vport)
9828 goto err_exit;
9829 ndlp = lpfc_findnode_rpi(vport, rpi);
9830 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9831 goto err_exit;
9832
9833 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9834 lpfc_sli_abts_recover_port(vport, ndlp);
9835 return;
9836
9837 err_exit:
9838 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9839 "3095 Event Context not found, no "
9840 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9841 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9842 vpi, rpi);
9843}
9844
9845/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9846 * @phba: pointer to HBA context object.
9847 * @ndlp: nodelist pointer for the impacted rport.
9848 * @axri: pointer to the wcqe containing the failed exchange.
9849 *
9850 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9851 * port. The port generates this event when an abort exchange request to an
9852 * rport fails twice in succession with no reply. The abort could be originated
9853 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9854 */
9855void
9856lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9857 struct lpfc_nodelist *ndlp,
9858 struct sli4_wcqe_xri_aborted *axri)
9859{
9860 struct lpfc_vport *vport;
5c1db2ac 9861 uint32_t ext_status = 0;
cb69f7de 9862
6b5151fd 9863 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9864 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9865 "3115 Node Context not found, driver "
9866 "ignoring abts err event\n");
6b5151fd
JS
9867 return;
9868 }
9869
cb69f7de
JS
9870 vport = ndlp->vport;
9871 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9872 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9873 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9874 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9875 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9876 bf_get(lpfc_wcqe_xa_status, axri),
9877 axri->parameter);
cb69f7de 9878
5c1db2ac
JS
9879 /*
9880 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9881 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9882 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9883 */
e3d2b802 9884 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9885 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9886 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9887 lpfc_sli_abts_recover_port(vport, ndlp);
9888}
9889
e59058c4 9890/**
3621a710 9891 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9892 * @phba: Pointer to HBA context object.
9893 * @pring: Pointer to driver SLI ring object.
9894 * @iocbq: Pointer to iocb object.
9895 *
9896 * This function is called by the slow ring event handler
9897 * function when there is an ASYNC event iocb in the ring.
9898 * This function is called with no lock held.
9899 * Currently this function handles only temperature related
9900 * ASYNC events. The function decodes the temperature sensor
9901 * event message and posts events for the management applications.
9902 **/
98c9ea5c 9903static void
57127f15
JS
9904lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9905 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9906{
9907 IOCB_t *icmd;
9908 uint16_t evt_code;
57127f15
JS
9909 struct temp_event temp_event_data;
9910 struct Scsi_Host *shost;
a257bf90 9911 uint32_t *iocb_w;
57127f15
JS
9912
9913 icmd = &iocbq->iocb;
9914 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9915
cb69f7de
JS
9916 switch (evt_code) {
9917 case ASYNC_TEMP_WARN:
9918 case ASYNC_TEMP_SAFE:
9919 temp_event_data.data = (uint32_t) icmd->ulpContext;
9920 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9921 if (evt_code == ASYNC_TEMP_WARN) {
9922 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9923 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9924 "0347 Adapter is very hot, please take "
9925 "corrective action. temperature : %d Celsius\n",
9926 (uint32_t) icmd->ulpContext);
9927 } else {
9928 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9929 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9930 "0340 Adapter temperature is OK now. "
9931 "temperature : %d Celsius\n",
9932 (uint32_t) icmd->ulpContext);
9933 }
9934
9935 /* Send temperature change event to applications */
9936 shost = lpfc_shost_from_vport(phba->pport);
9937 fc_host_post_vendor_event(shost, fc_get_event_number(),
9938 sizeof(temp_event_data), (char *) &temp_event_data,
9939 LPFC_NL_VENDOR_ID);
9940 break;
9941 case ASYNC_STATUS_CN:
9942 lpfc_sli_abts_err_handler(phba, iocbq);
9943 break;
9944 default:
a257bf90 9945 iocb_w = (uint32_t *) icmd;
cb69f7de 9946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9947 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9948 " evt_code 0x%x\n"
a257bf90
JS
9949 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9950 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9951 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9952 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9953 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9954 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9955 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9956 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9957 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9958
cb69f7de 9959 break;
57127f15 9960 }
57127f15
JS
9961}
9962
9963
e59058c4 9964/**
895427bd 9965 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9966 * @phba: Pointer to HBA context object.
9967 *
9968 * lpfc_sli_setup sets up rings of the SLI interface with
9969 * number of iocbs per ring and iotags. This function is
9970 * called while driver attach to the HBA and before the
9971 * interrupts are enabled. So there is no need for locking.
9972 *
9973 * This function always returns 0.
9974 **/
dea3101e 9975int
895427bd
JS
9976lpfc_sli4_setup(struct lpfc_hba *phba)
9977{
9978 struct lpfc_sli_ring *pring;
9979
9980 pring = phba->sli4_hba.els_wq->pring;
9981 pring->num_mask = LPFC_MAX_RING_MASK;
9982 pring->prt[0].profile = 0; /* Mask 0 */
9983 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9984 pring->prt[0].type = FC_TYPE_ELS;
9985 pring->prt[0].lpfc_sli_rcv_unsol_event =
9986 lpfc_els_unsol_event;
9987 pring->prt[1].profile = 0; /* Mask 1 */
9988 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9989 pring->prt[1].type = FC_TYPE_ELS;
9990 pring->prt[1].lpfc_sli_rcv_unsol_event =
9991 lpfc_els_unsol_event;
9992 pring->prt[2].profile = 0; /* Mask 2 */
9993 /* NameServer Inquiry */
9994 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9995 /* NameServer */
9996 pring->prt[2].type = FC_TYPE_CT;
9997 pring->prt[2].lpfc_sli_rcv_unsol_event =
9998 lpfc_ct_unsol_event;
9999 pring->prt[3].profile = 0; /* Mask 3 */
10000 /* NameServer response */
10001 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10002 /* NameServer */
10003 pring->prt[3].type = FC_TYPE_CT;
10004 pring->prt[3].lpfc_sli_rcv_unsol_event =
10005 lpfc_ct_unsol_event;
10006 return 0;
10007}
10008
10009/**
10010 * lpfc_sli_setup - SLI ring setup function
10011 * @phba: Pointer to HBA context object.
10012 *
10013 * lpfc_sli_setup sets up rings of the SLI interface with
10014 * number of iocbs per ring and iotags. This function is
10015 * called while driver attach to the HBA and before the
10016 * interrupts are enabled. So there is no need for locking.
10017 *
10018 * This function always returns 0. SLI3 only.
10019 **/
10020int
dea3101e 10021lpfc_sli_setup(struct lpfc_hba *phba)
10022{
ed957684 10023 int i, totiocbsize = 0;
dea3101e 10024 struct lpfc_sli *psli = &phba->sli;
10025 struct lpfc_sli_ring *pring;
10026
2a76a283 10027 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 10028 psli->sli_flag = 0;
dea3101e 10029
604a3e30
JB
10030 psli->iocbq_lookup = NULL;
10031 psli->iocbq_lookup_len = 0;
10032 psli->last_iotag = 0;
10033
dea3101e 10034 for (i = 0; i < psli->num_rings; i++) {
895427bd 10035 pring = &psli->sli3_ring[i];
dea3101e 10036 switch (i) {
10037 case LPFC_FCP_RING: /* ring 0 - FCP */
10038 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10039 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10040 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10041 pring->sli.sli3.numCiocb +=
10042 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10043 pring->sli.sli3.numRiocb +=
10044 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10045 pring->sli.sli3.numCiocb +=
10046 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10047 pring->sli.sli3.numRiocb +=
10048 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10049 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10050 SLI3_IOCB_CMD_SIZE :
10051 SLI2_IOCB_CMD_SIZE;
7e56aa25 10052 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10053 SLI3_IOCB_RSP_SIZE :
10054 SLI2_IOCB_RSP_SIZE;
dea3101e 10055 pring->iotag_ctr = 0;
10056 pring->iotag_max =
92d7f7b0 10057 (phba->cfg_hba_queue_depth * 2);
dea3101e 10058 pring->fast_iotag = pring->iotag_max;
10059 pring->num_mask = 0;
10060 break;
a4bc3379 10061 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 10062 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10063 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10064 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10065 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10066 SLI3_IOCB_CMD_SIZE :
10067 SLI2_IOCB_CMD_SIZE;
7e56aa25 10068 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10069 SLI3_IOCB_RSP_SIZE :
10070 SLI2_IOCB_RSP_SIZE;
2e0fef85 10071 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 10072 pring->num_mask = 0;
10073 break;
10074 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10075 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
10076 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10077 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10078 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10079 SLI3_IOCB_CMD_SIZE :
10080 SLI2_IOCB_CMD_SIZE;
7e56aa25 10081 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
10082 SLI3_IOCB_RSP_SIZE :
10083 SLI2_IOCB_RSP_SIZE;
dea3101e 10084 pring->fast_iotag = 0;
10085 pring->iotag_ctr = 0;
10086 pring->iotag_max = 4096;
57127f15
JS
10087 pring->lpfc_sli_rcv_async_status =
10088 lpfc_sli_async_event_handler;
6669f9bb 10089 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 10090 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
10091 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10092 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 10093 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 10094 lpfc_els_unsol_event;
dea3101e 10095 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
10096 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10097 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 10098 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 10099 lpfc_els_unsol_event;
dea3101e 10100 pring->prt[2].profile = 0; /* Mask 2 */
10101 /* NameServer Inquiry */
6a9c52cf 10102 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 10103 /* NameServer */
6a9c52cf 10104 pring->prt[2].type = FC_TYPE_CT;
dea3101e 10105 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 10106 lpfc_ct_unsol_event;
dea3101e 10107 pring->prt[3].profile = 0; /* Mask 3 */
10108 /* NameServer response */
6a9c52cf 10109 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 10110 /* NameServer */
6a9c52cf 10111 pring->prt[3].type = FC_TYPE_CT;
dea3101e 10112 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 10113 lpfc_ct_unsol_event;
dea3101e 10114 break;
10115 }
7e56aa25
JS
10116 totiocbsize += (pring->sli.sli3.numCiocb *
10117 pring->sli.sli3.sizeCiocb) +
10118 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 10119 }
ed957684 10120 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 10121 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
10122 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10123 "SLI2 SLIM Data: x%x x%lx\n",
10124 phba->brd_no, totiocbsize,
10125 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 10126 }
cf5bf97e
JW
10127 if (phba->cfg_multi_ring_support == 2)
10128 lpfc_extra_ring_setup(phba);
dea3101e 10129
10130 return 0;
10131}
10132
e59058c4 10133/**
895427bd 10134 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
10135 * @phba: Pointer to HBA context object.
10136 *
895427bd 10137 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
10138 * ring. This function also initializes ring indices of each ring.
10139 * This function is called during the initialization of the SLI
10140 * interface of an HBA.
10141 * This function is called with no lock held and always returns
10142 * 1.
10143 **/
895427bd
JS
10144void
10145lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e 10146{
10147 struct lpfc_sli *psli;
10148 struct lpfc_sli_ring *pring;
604a3e30 10149 int i;
dea3101e 10150
10151 psli = &phba->sli;
2e0fef85 10152 spin_lock_irq(&phba->hbalock);
dea3101e 10153 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10154 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 10155 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
10156 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10157 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 10158 pring->flag = 0;
895427bd 10159 pring->ringno = LPFC_FCP_RING;
dea3101e 10160 INIT_LIST_HEAD(&pring->txq);
10161 INIT_LIST_HEAD(&pring->txcmplq);
10162 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 10163 spin_lock_init(&pring->ring_lock);
dea3101e 10164 }
895427bd
JS
10165 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10166 pring = phba->sli4_hba.nvme_wq[i]->pring;
10167 pring->flag = 0;
10168 pring->ringno = LPFC_FCP_RING;
10169 INIT_LIST_HEAD(&pring->txq);
10170 INIT_LIST_HEAD(&pring->txcmplq);
10171 INIT_LIST_HEAD(&pring->iocb_continueq);
10172 spin_lock_init(&pring->ring_lock);
10173 }
10174 pring = phba->sli4_hba.els_wq->pring;
10175 pring->flag = 0;
10176 pring->ringno = LPFC_ELS_RING;
10177 INIT_LIST_HEAD(&pring->txq);
10178 INIT_LIST_HEAD(&pring->txcmplq);
10179 INIT_LIST_HEAD(&pring->iocb_continueq);
10180 spin_lock_init(&pring->ring_lock);
dea3101e 10181
895427bd
JS
10182 if (phba->cfg_nvme_io_channel) {
10183 pring = phba->sli4_hba.nvmels_wq->pring;
10184 pring->flag = 0;
10185 pring->ringno = LPFC_ELS_RING;
10186 INIT_LIST_HEAD(&pring->txq);
10187 INIT_LIST_HEAD(&pring->txcmplq);
10188 INIT_LIST_HEAD(&pring->iocb_continueq);
10189 spin_lock_init(&pring->ring_lock);
10190 }
10191
10192 if (phba->cfg_fof) {
10193 pring = phba->sli4_hba.oas_wq->pring;
10194 pring->flag = 0;
10195 pring->ringno = LPFC_FCP_RING;
10196 INIT_LIST_HEAD(&pring->txq);
10197 INIT_LIST_HEAD(&pring->txcmplq);
10198 INIT_LIST_HEAD(&pring->iocb_continueq);
10199 spin_lock_init(&pring->ring_lock);
10200 }
10201
10202 spin_unlock_irq(&phba->hbalock);
10203}
10204
10205/**
10206 * lpfc_sli_queue_init - Queue initialization function
10207 * @phba: Pointer to HBA context object.
10208 *
10209 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10210 * ring. This function also initializes ring indices of each ring.
10211 * This function is called during the initialization of the SLI
10212 * interface of an HBA.
10213 * This function is called with no lock held and always returns
10214 * 1.
10215 **/
10216void
10217lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e 10218{
10219 struct lpfc_sli *psli;
10220 struct lpfc_sli_ring *pring;
604a3e30 10221 int i;
dea3101e 10222
10223 psli = &phba->sli;
2e0fef85 10224 spin_lock_irq(&phba->hbalock);
dea3101e 10225 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10226 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 10227 /* Initialize list headers for txq and txcmplq as double linked lists */
10228 for (i = 0; i < psli->num_rings; i++) {
895427bd 10229 pring = &psli->sli3_ring[i];
dea3101e 10230 pring->ringno = i;
7e56aa25
JS
10231 pring->sli.sli3.next_cmdidx = 0;
10232 pring->sli.sli3.local_getidx = 0;
10233 pring->sli.sli3.cmdidx = 0;
dea3101e 10234 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 10235 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 10236 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
10237 pring->flag = 0;
10238 INIT_LIST_HEAD(&pring->txq);
10239 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 10240 spin_lock_init(&pring->ring_lock);
dea3101e 10241 }
2e0fef85 10242 spin_unlock_irq(&phba->hbalock);
dea3101e 10243}
10244
04c68496
JS
10245/**
10246 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10247 * @phba: Pointer to HBA context object.
10248 *
10249 * This routine flushes the mailbox command subsystem. It will unconditionally
10250 * flush all the mailbox commands in the three possible stages in the mailbox
10251 * command sub-system: pending mailbox command queue; the outstanding mailbox
10252 * command; and completed mailbox command queue. It is caller's responsibility
10253 * to make sure that the driver is in the proper state to flush the mailbox
10254 * command sub-system. Namely, the posting of mailbox commands into the
10255 * pending mailbox command queue from the various clients must be stopped;
10256 * either the HBA is in a state that it will never works on the outstanding
10257 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10258 * mailbox command has been completed.
10259 **/
10260static void
10261lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10262{
10263 LIST_HEAD(completions);
10264 struct lpfc_sli *psli = &phba->sli;
10265 LPFC_MBOXQ_t *pmb;
10266 unsigned long iflag;
10267
10268 /* Flush all the mailbox commands in the mbox system */
10269 spin_lock_irqsave(&phba->hbalock, iflag);
10270 /* The pending mailbox command queue */
10271 list_splice_init(&phba->sli.mboxq, &completions);
10272 /* The outstanding active mailbox command */
10273 if (psli->mbox_active) {
10274 list_add_tail(&psli->mbox_active->list, &completions);
10275 psli->mbox_active = NULL;
10276 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10277 }
10278 /* The completed mailbox command queue */
10279 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10280 spin_unlock_irqrestore(&phba->hbalock, iflag);
10281
10282 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10283 while (!list_empty(&completions)) {
10284 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10285 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10286 if (pmb->mbox_cmpl)
10287 pmb->mbox_cmpl(phba, pmb);
10288 }
10289}
10290
e59058c4 10291/**
3621a710 10292 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10293 * @vport: Pointer to virtual port object.
10294 *
10295 * lpfc_sli_host_down is called to clean up the resources
10296 * associated with a vport before destroying virtual
10297 * port data structures.
10298 * This function does following operations:
10299 * - Free discovery resources associated with this virtual
10300 * port.
10301 * - Free iocbs associated with this virtual port in
10302 * the txq.
10303 * - Send abort for all iocb commands associated with this
10304 * vport in txcmplq.
10305 *
10306 * This function is called with no lock held and always returns 1.
10307 **/
92d7f7b0
JS
10308int
10309lpfc_sli_host_down(struct lpfc_vport *vport)
10310{
858c9f6c 10311 LIST_HEAD(completions);
92d7f7b0
JS
10312 struct lpfc_hba *phba = vport->phba;
10313 struct lpfc_sli *psli = &phba->sli;
895427bd 10314 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10315 struct lpfc_sli_ring *pring;
10316 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10317 int i;
10318 unsigned long flags = 0;
10319 uint16_t prev_pring_flag;
10320
10321 lpfc_cleanup_discovery_resources(vport);
10322
10323 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10324
895427bd
JS
10325 /*
10326 * Error everything on the txq since these iocbs
10327 * have not been given to the FW yet.
10328 * Also issue ABTS for everything on the txcmplq
10329 */
10330 if (phba->sli_rev != LPFC_SLI_REV4) {
10331 for (i = 0; i < psli->num_rings; i++) {
10332 pring = &psli->sli3_ring[i];
10333 prev_pring_flag = pring->flag;
10334 /* Only slow rings */
10335 if (pring->ringno == LPFC_ELS_RING) {
10336 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10337 /* Set the lpfc data pending flag */
10338 set_bit(LPFC_DATA_READY, &phba->data_flags);
10339 }
10340 list_for_each_entry_safe(iocb, next_iocb,
10341 &pring->txq, list) {
10342 if (iocb->vport != vport)
10343 continue;
10344 list_move_tail(&iocb->list, &completions);
10345 }
10346 list_for_each_entry_safe(iocb, next_iocb,
10347 &pring->txcmplq, list) {
10348 if (iocb->vport != vport)
10349 continue;
10350 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10351 }
10352 pring->flag = prev_pring_flag;
10353 }
10354 } else {
10355 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10356 pring = qp->pring;
10357 if (!pring)
92d7f7b0 10358 continue;
895427bd
JS
10359 if (pring == phba->sli4_hba.els_wq->pring) {
10360 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10361 /* Set the lpfc data pending flag */
10362 set_bit(LPFC_DATA_READY, &phba->data_flags);
10363 }
10364 prev_pring_flag = pring->flag;
10365 spin_lock_irq(&pring->ring_lock);
10366 list_for_each_entry_safe(iocb, next_iocb,
10367 &pring->txq, list) {
10368 if (iocb->vport != vport)
10369 continue;
10370 list_move_tail(&iocb->list, &completions);
10371 }
10372 spin_unlock_irq(&pring->ring_lock);
10373 list_for_each_entry_safe(iocb, next_iocb,
10374 &pring->txcmplq, list) {
10375 if (iocb->vport != vport)
10376 continue;
10377 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10378 }
10379 pring->flag = prev_pring_flag;
92d7f7b0 10380 }
92d7f7b0 10381 }
92d7f7b0
JS
10382 spin_unlock_irqrestore(&phba->hbalock, flags);
10383
a257bf90
JS
10384 /* Cancel all the IOCBs from the completions list */
10385 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10386 IOERR_SLI_DOWN);
92d7f7b0
JS
10387 return 1;
10388}
10389
e59058c4 10390/**
3621a710 10391 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10392 * @phba: Pointer to HBA context object.
10393 *
10394 * This function cleans up all iocb, buffers, mailbox commands
10395 * while shutting down the HBA. This function is called with no
10396 * lock held and always returns 1.
10397 * This function does the following to cleanup driver resources:
10398 * - Free discovery resources for each virtual port
10399 * - Cleanup any pending fabric iocbs
10400 * - Iterate through the iocb txq and free each entry
10401 * in the list.
10402 * - Free up any buffer posted to the HBA
10403 * - Free mailbox commands in the mailbox queue.
10404 **/
dea3101e 10405int
2e0fef85 10406lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10407{
2534ba75 10408 LIST_HEAD(completions);
2e0fef85 10409 struct lpfc_sli *psli = &phba->sli;
895427bd 10410 struct lpfc_queue *qp = NULL;
dea3101e 10411 struct lpfc_sli_ring *pring;
0ff10d46 10412 struct lpfc_dmabuf *buf_ptr;
dea3101e 10413 unsigned long flags = 0;
04c68496
JS
10414 int i;
10415
10416 /* Shutdown the mailbox command sub-system */
618a5230 10417 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10418
dea3101e 10419 lpfc_hba_down_prep(phba);
10420
92d7f7b0
JS
10421 lpfc_fabric_abort_hba(phba);
10422
2e0fef85 10423 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10424
895427bd
JS
10425 /*
10426 * Error everything on the txq since these iocbs
10427 * have not been given to the FW yet.
10428 */
10429 if (phba->sli_rev != LPFC_SLI_REV4) {
10430 for (i = 0; i < psli->num_rings; i++) {
10431 pring = &psli->sli3_ring[i];
10432 /* Only slow rings */
10433 if (pring->ringno == LPFC_ELS_RING) {
10434 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10435 /* Set the lpfc data pending flag */
10436 set_bit(LPFC_DATA_READY, &phba->data_flags);
10437 }
10438 list_splice_init(&pring->txq, &completions);
10439 }
10440 } else {
10441 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10442 pring = qp->pring;
10443 if (!pring)
10444 continue;
10445 spin_lock_irq(&pring->ring_lock);
10446 list_splice_init(&pring->txq, &completions);
10447 spin_unlock_irq(&pring->ring_lock);
10448 if (pring == phba->sli4_hba.els_wq->pring) {
10449 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10450 /* Set the lpfc data pending flag */
10451 set_bit(LPFC_DATA_READY, &phba->data_flags);
10452 }
10453 }
2534ba75 10454 }
2e0fef85 10455 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10456
a257bf90
JS
10457 /* Cancel all the IOCBs from the completions list */
10458 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10459 IOERR_SLI_DOWN);
dea3101e 10460
0ff10d46
JS
10461 spin_lock_irqsave(&phba->hbalock, flags);
10462 list_splice_init(&phba->elsbuf, &completions);
10463 phba->elsbuf_cnt = 0;
10464 phba->elsbuf_prev_cnt = 0;
10465 spin_unlock_irqrestore(&phba->hbalock, flags);
10466
10467 while (!list_empty(&completions)) {
10468 list_remove_head(&completions, buf_ptr,
10469 struct lpfc_dmabuf, list);
10470 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10471 kfree(buf_ptr);
10472 }
10473
dea3101e 10474 /* Return any active mbox cmds */
10475 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10476
da0436e9 10477 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10478 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10479 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10480
da0436e9
JS
10481 return 1;
10482}
10483
e59058c4 10484/**
3621a710 10485 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10486 * @srcp: Source memory pointer.
10487 * @destp: Destination memory pointer.
10488 * @cnt: Number of words required to be copied.
10489 *
10490 * This function is used for copying data between driver memory
10491 * and the SLI memory. This function also changes the endianness
10492 * of each word if native endianness is different from SLI
10493 * endianness. This function can be called with or without
10494 * lock.
10495 **/
dea3101e 10496void
10497lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10498{
10499 uint32_t *src = srcp;
10500 uint32_t *dest = destp;
10501 uint32_t ldata;
10502 int i;
10503
10504 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10505 ldata = *src;
10506 ldata = le32_to_cpu(ldata);
10507 *dest = ldata;
10508 src++;
10509 dest++;
10510 }
10511}
10512
e59058c4 10513
a0c87cbd
JS
10514/**
10515 * lpfc_sli_bemem_bcopy - SLI memory copy function
10516 * @srcp: Source memory pointer.
10517 * @destp: Destination memory pointer.
10518 * @cnt: Number of words required to be copied.
10519 *
10520 * This function is used for copying data between a data structure
10521 * with big endian representation to local endianness.
10522 * This function can be called with or without lock.
10523 **/
10524void
10525lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10526{
10527 uint32_t *src = srcp;
10528 uint32_t *dest = destp;
10529 uint32_t ldata;
10530 int i;
10531
10532 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10533 ldata = *src;
10534 ldata = be32_to_cpu(ldata);
10535 *dest = ldata;
10536 src++;
10537 dest++;
10538 }
10539}
10540
e59058c4 10541/**
3621a710 10542 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10543 * @phba: Pointer to HBA context object.
10544 * @pring: Pointer to driver SLI ring object.
10545 * @mp: Pointer to driver buffer object.
10546 *
10547 * This function is called with no lock held.
10548 * It always return zero after adding the buffer to the postbufq
10549 * buffer list.
10550 **/
dea3101e 10551int
2e0fef85
JS
10552lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10553 struct lpfc_dmabuf *mp)
dea3101e 10554{
10555 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10556 later */
2e0fef85 10557 spin_lock_irq(&phba->hbalock);
dea3101e 10558 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10559 pring->postbufq_cnt++;
2e0fef85 10560 spin_unlock_irq(&phba->hbalock);
dea3101e 10561 return 0;
10562}
10563
e59058c4 10564/**
3621a710 10565 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10566 * @phba: Pointer to HBA context object.
10567 *
10568 * When HBQ is enabled, buffers are searched based on tags. This function
10569 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10570 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10571 * does not conflict with tags of buffer posted for unsolicited events.
10572 * The function returns the allocated tag. The function is called with
10573 * no locks held.
10574 **/
76bb24ef
JS
10575uint32_t
10576lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10577{
10578 spin_lock_irq(&phba->hbalock);
10579 phba->buffer_tag_count++;
10580 /*
10581 * Always set the QUE_BUFTAG_BIT to distiguish between
10582 * a tag assigned by HBQ.
10583 */
10584 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10585 spin_unlock_irq(&phba->hbalock);
10586 return phba->buffer_tag_count;
10587}
10588
e59058c4 10589/**
3621a710 10590 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10591 * @phba: Pointer to HBA context object.
10592 * @pring: Pointer to driver SLI ring object.
10593 * @tag: Buffer tag.
10594 *
10595 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10596 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10597 * iocb is posted to the response ring with the tag of the buffer.
10598 * This function searches the pring->postbufq list using the tag
10599 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10600 * iocb. If the buffer is found then lpfc_dmabuf object of the
10601 * buffer is returned to the caller else NULL is returned.
10602 * This function is called with no lock held.
10603 **/
76bb24ef
JS
10604struct lpfc_dmabuf *
10605lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10606 uint32_t tag)
10607{
10608 struct lpfc_dmabuf *mp, *next_mp;
10609 struct list_head *slp = &pring->postbufq;
10610
25985edc 10611 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10612 spin_lock_irq(&phba->hbalock);
10613 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10614 if (mp->buffer_tag == tag) {
10615 list_del_init(&mp->list);
10616 pring->postbufq_cnt--;
10617 spin_unlock_irq(&phba->hbalock);
10618 return mp;
10619 }
10620 }
10621
10622 spin_unlock_irq(&phba->hbalock);
10623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10624 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10625 "ring %d Data x%lx x%p x%p x%x\n",
10626 pring->ringno, (unsigned long) tag,
10627 slp->next, slp->prev, pring->postbufq_cnt);
10628
10629 return NULL;
10630}
dea3101e 10631
e59058c4 10632/**
3621a710 10633 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10634 * @phba: Pointer to HBA context object.
10635 * @pring: Pointer to driver SLI ring object.
10636 * @phys: DMA address of the buffer.
10637 *
10638 * This function searches the buffer list using the dma_address
10639 * of unsolicited event to find the driver's lpfc_dmabuf object
10640 * corresponding to the dma_address. The function returns the
10641 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10642 * This function is called by the ct and els unsolicited event
10643 * handlers to get the buffer associated with the unsolicited
10644 * event.
10645 *
10646 * This function is called with no lock held.
10647 **/
dea3101e 10648struct lpfc_dmabuf *
10649lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10650 dma_addr_t phys)
10651{
10652 struct lpfc_dmabuf *mp, *next_mp;
10653 struct list_head *slp = &pring->postbufq;
10654
25985edc 10655 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10656 spin_lock_irq(&phba->hbalock);
dea3101e 10657 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10658 if (mp->phys == phys) {
10659 list_del_init(&mp->list);
10660 pring->postbufq_cnt--;
2e0fef85 10661 spin_unlock_irq(&phba->hbalock);
dea3101e 10662 return mp;
10663 }
10664 }
10665
2e0fef85 10666 spin_unlock_irq(&phba->hbalock);
dea3101e 10667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10668 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10669 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10670 pring->ringno, (unsigned long long)phys,
dea3101e 10671 slp->next, slp->prev, pring->postbufq_cnt);
10672 return NULL;
10673}
10674
e59058c4 10675/**
3621a710 10676 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10677 * @phba: Pointer to HBA context object.
10678 * @cmdiocb: Pointer to driver command iocb object.
10679 * @rspiocb: Pointer to driver response iocb object.
10680 *
10681 * This function is the completion handler for the abort iocbs for
10682 * ELS commands. This function is called from the ELS ring event
10683 * handler with no lock held. This function frees memory resources
10684 * associated with the abort iocb.
10685 **/
dea3101e 10686static void
2e0fef85
JS
10687lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10688 struct lpfc_iocbq *rspiocb)
dea3101e 10689{
2e0fef85 10690 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10691 uint16_t abort_iotag, abort_context;
ff78d8f9 10692 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10693
10694 if (irsp->ulpStatus) {
ff78d8f9
JS
10695
10696 /*
10697 * Assume that the port already completed and returned, or
10698 * will return the iocb. Just Log the message.
10699 */
2680eeaa
JS
10700 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10701 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10702
2e0fef85 10703 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10704 if (phba->sli_rev < LPFC_SLI_REV4) {
10705 if (abort_iotag != 0 &&
10706 abort_iotag <= phba->sli.last_iotag)
10707 abort_iocb =
10708 phba->sli.iocbq_lookup[abort_iotag];
10709 } else
10710 /* For sli4 the abort_tag is the XRI,
10711 * so the abort routine puts the iotag of the iocb
10712 * being aborted in the context field of the abort
10713 * IOCB.
10714 */
10715 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10716
2a9bf3d0
JS
10717 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10718 "0327 Cannot abort els iocb %p "
10719 "with tag %x context %x, abort status %x, "
10720 "abort code %x\n",
10721 abort_iocb, abort_iotag, abort_context,
10722 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10723
ff78d8f9 10724 spin_unlock_irq(&phba->hbalock);
2680eeaa 10725 }
604a3e30 10726 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 10727 return;
10728}
10729
e59058c4 10730/**
3621a710 10731 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10732 * @phba: Pointer to HBA context object.
10733 * @cmdiocb: Pointer to driver command iocb object.
10734 * @rspiocb: Pointer to driver response iocb object.
10735 *
10736 * The function is called from SLI ring event handler with no
10737 * lock held. This function is the completion handler for ELS commands
10738 * which are aborted. The function frees memory resources used for
10739 * the aborted ELS commands.
10740 **/
92d7f7b0
JS
10741static void
10742lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10743 struct lpfc_iocbq *rspiocb)
10744{
10745 IOCB_t *irsp = &rspiocb->iocb;
10746
10747 /* ELS cmd tag <ulpIoTag> completes */
10748 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10749 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10750 "x%x x%x x%x\n",
e8b62011 10751 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10752 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10753 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10754 lpfc_ct_free_iocb(phba, cmdiocb);
10755 else
10756 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10757 return;
10758}
10759
e59058c4 10760/**
5af5eee7 10761 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10762 * @phba: Pointer to HBA context object.
10763 * @pring: Pointer to driver SLI ring object.
10764 * @cmdiocb: Pointer to driver command iocb object.
10765 *
5af5eee7
JS
10766 * This function issues an abort iocb for the provided command iocb down to
10767 * the port. Other than the case the outstanding command iocb is an abort
10768 * request, this function issues abort out unconditionally. This function is
10769 * called with hbalock held. The function returns 0 when it fails due to
10770 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10771 **/
5af5eee7
JS
10772static int
10773lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10774 struct lpfc_iocbq *cmdiocb)
dea3101e 10775{
2e0fef85 10776 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10777 struct lpfc_iocbq *abtsiocbp;
dea3101e 10778 IOCB_t *icmd = NULL;
10779 IOCB_t *iabt = NULL;
5af5eee7 10780 int retval;
7e56aa25 10781 unsigned long iflags;
07951076 10782
1c2ba475
JT
10783 lockdep_assert_held(&phba->hbalock);
10784
92d7f7b0
JS
10785 /*
10786 * There are certain command types we don't want to abort. And we
10787 * don't want to abort commands that are already in the process of
10788 * being aborted.
07951076
JS
10789 */
10790 icmd = &cmdiocb->iocb;
2e0fef85 10791 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10792 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10793 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10794 return 0;
10795
dea3101e 10796 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10797 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 10798 if (abtsiocbp == NULL)
10799 return 0;
dea3101e 10800
07951076 10801 /* This signals the response to set the correct status
341af102 10802 * before calling the completion handler
07951076
JS
10803 */
10804 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10805
dea3101e 10806 iabt = &abtsiocbp->iocb;
07951076
JS
10807 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10808 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10809 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10810 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10811 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10812 }
da0436e9
JS
10813 else
10814 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10815 iabt->ulpLe = 1;
10816 iabt->ulpClass = icmd->ulpClass;
dea3101e 10817
5ffc266e 10818 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10819 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10820 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10821 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10822 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10823 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10824
2e0fef85 10825 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10826 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10827 else
10828 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10829
07951076 10830 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10831 abtsiocbp->vport = vport;
5b8bd0c9 10832
e8b62011
JS
10833 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10834 "0339 Abort xri x%x, original iotag x%x, "
10835 "abort cmd iotag x%x\n",
2a9bf3d0 10836 iabt->un.acxri.abortIoTag,
e8b62011 10837 iabt->un.acxri.abortContextTag,
2a9bf3d0 10838 abtsiocbp->iotag);
7e56aa25
JS
10839
10840 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10841 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10842 if (unlikely(pring == NULL))
9bd2bff5 10843 return 0;
7e56aa25
JS
10844 /* Note: both hbalock and ring_lock need to be set here */
10845 spin_lock_irqsave(&pring->ring_lock, iflags);
10846 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10847 abtsiocbp, 0);
10848 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10849 } else {
10850 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10851 abtsiocbp, 0);
10852 }
dea3101e 10853
d7c255b2
JS
10854 if (retval)
10855 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10856
10857 /*
10858 * Caller to this routine should check for IOCB_ERROR
10859 * and handle it properly. This routine no longer removes
10860 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10861 */
10862 return retval;
10863}
10864
10865/**
10866 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10867 * @phba: Pointer to HBA context object.
10868 * @pring: Pointer to driver SLI ring object.
10869 * @cmdiocb: Pointer to driver command iocb object.
10870 *
10871 * This function issues an abort iocb for the provided command iocb. In case
10872 * of unloading, the abort iocb will not be issued to commands on the ELS
10873 * ring. Instead, the callback function shall be changed to those commands
10874 * so that nothing happens when them finishes. This function is called with
10875 * hbalock held. The function returns 0 when the command iocb is an abort
10876 * request.
10877 **/
10878int
10879lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10880 struct lpfc_iocbq *cmdiocb)
10881{
10882 struct lpfc_vport *vport = cmdiocb->vport;
10883 int retval = IOCB_ERROR;
10884 IOCB_t *icmd = NULL;
10885
1c2ba475
JT
10886 lockdep_assert_held(&phba->hbalock);
10887
5af5eee7
JS
10888 /*
10889 * There are certain command types we don't want to abort. And we
10890 * don't want to abort commands that are already in the process of
10891 * being aborted.
10892 */
10893 icmd = &cmdiocb->iocb;
10894 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10895 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10896 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10897 return 0;
10898
1234a6d5
DK
10899 if (!pring) {
10900 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10901 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10902 else
10903 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10904 goto abort_iotag_exit;
10905 }
10906
5af5eee7
JS
10907 /*
10908 * If we're unloading, don't abort iocb on the ELS ring, but change
10909 * the callback so that nothing happens when it finishes.
10910 */
10911 if ((vport->load_flag & FC_UNLOADING) &&
10912 (pring->ringno == LPFC_ELS_RING)) {
10913 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10914 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10915 else
10916 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10917 goto abort_iotag_exit;
10918 }
10919
10920 /* Now, we try to issue the abort to the cmdiocb out */
10921 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10922
07951076 10923abort_iotag_exit:
2e0fef85
JS
10924 /*
10925 * Caller to this routine should check for IOCB_ERROR
10926 * and handle it properly. This routine no longer removes
10927 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10928 */
2e0fef85 10929 return retval;
dea3101e 10930}
10931
895427bd
JS
10932/**
10933 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10934 * @phba: Pointer to HBA context object.
10935 * @pring: Pointer to driver SLI ring object.
10936 * @cmdiocb: Pointer to driver command iocb object.
10937 *
10938 * This function issues an abort iocb for the provided command iocb down to
10939 * the port. Other than the case the outstanding command iocb is an abort
10940 * request, this function issues abort out unconditionally. This function is
10941 * called with hbalock held. The function returns 0 when it fails due to
10942 * memory allocation failure or when the command iocb is an abort request.
10943 **/
10944static int
10945lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10946 struct lpfc_iocbq *cmdiocb)
10947{
10948 struct lpfc_vport *vport = cmdiocb->vport;
10949 struct lpfc_iocbq *abtsiocbp;
205e8240 10950 union lpfc_wqe128 *abts_wqe;
895427bd
JS
10951 int retval;
10952
10953 /*
10954 * There are certain command types we don't want to abort. And we
10955 * don't want to abort commands that are already in the process of
10956 * being aborted.
10957 */
10958 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10959 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10960 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10961 return 0;
10962
10963 /* issue ABTS for this io based on iotag */
10964 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10965 if (abtsiocbp == NULL)
10966 return 0;
10967
10968 /* This signals the response to set the correct status
10969 * before calling the completion handler
10970 */
10971 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10972
10973 /* Complete prepping the abort wqe and issue to the FW. */
10974 abts_wqe = &abtsiocbp->wqe;
10975 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10976 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10977
10978 /* Explicitly set reserved fields to zero.*/
10979 abts_wqe->abort_cmd.rsrvd4 = 0;
10980 abts_wqe->abort_cmd.rsrvd5 = 0;
10981
10982 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10983 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10984 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10985
10986 /* word 7 */
10987 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10988 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10989 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10990 cmdiocb->iocb.ulpClass);
10991
10992 /* word 8 - tell the FW to abort the IO associated with this
10993 * outstanding exchange ID.
10994 */
10995 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10996
10997 /* word 9 - this is the iotag for the abts_wqe completion. */
10998 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10999 abtsiocbp->iotag);
11000
11001 /* word 10 */
11002 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
11003 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
11004 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
11005
11006 /* word 11 */
11007 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11008 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
11009 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
11010
11011 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11012 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
11013 abtsiocbp->vport = vport;
01649561 11014 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 11015 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 11016 if (retval) {
895427bd
JS
11017 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11018 "6147 Failed abts issue_wqe with status x%x "
11019 "for oxid x%x\n",
11020 retval, cmdiocb->sli4_xritag);
11021 lpfc_sli_release_iocbq(phba, abtsiocbp);
11022 return retval;
11023 }
11024
11025 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11026 "6148 Drv Abort NVME Request Issued for "
11027 "ox_id x%x on reqtag x%x\n",
11028 cmdiocb->sli4_xritag,
11029 abtsiocbp->iotag);
11030
11031 return retval;
11032}
11033
5af5eee7
JS
11034/**
11035 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11036 * @phba: pointer to lpfc HBA data structure.
11037 *
11038 * This routine will abort all pending and outstanding iocbs to an HBA.
11039 **/
11040void
11041lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11042{
11043 struct lpfc_sli *psli = &phba->sli;
11044 struct lpfc_sli_ring *pring;
895427bd 11045 struct lpfc_queue *qp = NULL;
5af5eee7
JS
11046 int i;
11047
895427bd
JS
11048 if (phba->sli_rev != LPFC_SLI_REV4) {
11049 for (i = 0; i < psli->num_rings; i++) {
11050 pring = &psli->sli3_ring[i];
11051 lpfc_sli_abort_iocb_ring(phba, pring);
11052 }
11053 return;
11054 }
11055 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11056 pring = qp->pring;
11057 if (!pring)
11058 continue;
db55fba8 11059 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
11060 }
11061}
11062
e59058c4 11063/**
3621a710 11064 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
11065 * @iocbq: Pointer to driver iocb object.
11066 * @vport: Pointer to driver virtual port object.
11067 * @tgt_id: SCSI ID of the target.
11068 * @lun_id: LUN ID of the scsi device.
11069 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11070 *
3621a710 11071 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
11072 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11073 * 0 if the filtering criteria is met for the given iocb and will return
11074 * 1 if the filtering criteria is not met.
11075 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11076 * given iocb is for the SCSI device specified by vport, tgt_id and
11077 * lun_id parameter.
11078 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11079 * given iocb is for the SCSI target specified by vport and tgt_id
11080 * parameters.
11081 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11082 * given iocb is for the SCSI host associated with the given vport.
11083 * This function is called with no locks held.
11084 **/
dea3101e 11085static int
51ef4c26
JS
11086lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11087 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 11088 lpfc_ctx_cmd ctx_cmd)
dea3101e 11089{
0bd4ca25 11090 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11091 int rc = 1;
11092
0bd4ca25
JSEC
11093 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
11094 return rc;
11095
51ef4c26
JS
11096 if (iocbq->vport != vport)
11097 return rc;
11098
0bd4ca25 11099 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 11100
495a714c 11101 if (lpfc_cmd->pCmd == NULL)
dea3101e 11102 return rc;
11103
11104 switch (ctx_cmd) {
11105 case LPFC_CTX_LUN:
495a714c
JS
11106 if ((lpfc_cmd->rdata->pnode) &&
11107 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11108 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 11109 rc = 0;
11110 break;
11111 case LPFC_CTX_TGT:
495a714c
JS
11112 if ((lpfc_cmd->rdata->pnode) &&
11113 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 11114 rc = 0;
11115 break;
dea3101e 11116 case LPFC_CTX_HOST:
11117 rc = 0;
11118 break;
11119 default:
11120 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 11121 __func__, ctx_cmd);
dea3101e 11122 break;
11123 }
11124
11125 return rc;
11126}
11127
e59058c4 11128/**
3621a710 11129 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
11130 * @vport: Pointer to virtual port.
11131 * @tgt_id: SCSI ID of the target.
11132 * @lun_id: LUN ID of the scsi device.
11133 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11134 *
11135 * This function returns number of FCP commands pending for the vport.
11136 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11137 * commands pending on the vport associated with SCSI device specified
11138 * by tgt_id and lun_id parameters.
11139 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11140 * commands pending on the vport associated with SCSI target specified
11141 * by tgt_id parameter.
11142 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11143 * commands pending on the vport.
11144 * This function returns the number of iocbs which satisfy the filter.
11145 * This function is called without any lock held.
11146 **/
dea3101e 11147int
51ef4c26
JS
11148lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11149 lpfc_ctx_cmd ctx_cmd)
dea3101e 11150{
51ef4c26 11151 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11152 struct lpfc_iocbq *iocbq;
11153 int sum, i;
dea3101e 11154
31979008 11155 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
11156 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11157 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11158
51ef4c26
JS
11159 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11160 ctx_cmd) == 0)
0bd4ca25 11161 sum++;
dea3101e 11162 }
31979008 11163 spin_unlock_irq(&phba->hbalock);
0bd4ca25 11164
dea3101e 11165 return sum;
11166}
11167
e59058c4 11168/**
3621a710 11169 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
11170 * @phba: Pointer to HBA context object
11171 * @cmdiocb: Pointer to command iocb object.
11172 * @rspiocb: Pointer to response iocb object.
11173 *
11174 * This function is called when an aborted FCP iocb completes. This
11175 * function is called by the ring event handler with no lock held.
11176 * This function frees the iocb.
11177 **/
5eb95af0 11178void
2e0fef85
JS
11179lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11180 struct lpfc_iocbq *rspiocb)
5eb95af0 11181{
cb69f7de 11182 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 11183 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
11184 "original iotag x%x, abort cmd iotag x%x "
11185 "status 0x%x, reason 0x%x\n",
11186 cmdiocb->iocb.un.acxri.abortContextTag,
11187 cmdiocb->iocb.un.acxri.abortIoTag,
11188 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11189 rspiocb->iocb.un.ulpWord[4]);
604a3e30 11190 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
11191 return;
11192}
11193
e59058c4 11194/**
3621a710 11195 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
11196 * @vport: Pointer to virtual port.
11197 * @pring: Pointer to driver SLI ring object.
11198 * @tgt_id: SCSI ID of the target.
11199 * @lun_id: LUN ID of the scsi device.
11200 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11201 *
11202 * This function sends an abort command for every SCSI command
11203 * associated with the given virtual port pending on the ring
11204 * filtered by lpfc_sli_validate_fcp_iocb function.
11205 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11206 * FCP iocbs associated with lun specified by tgt_id and lun_id
11207 * parameters
11208 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11209 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11210 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11211 * FCP iocbs associated with virtual port.
11212 * This function returns number of iocbs it failed to abort.
11213 * This function is called with no locks held.
11214 **/
dea3101e 11215int
51ef4c26
JS
11216lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11217 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 11218{
51ef4c26 11219 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11220 struct lpfc_iocbq *iocbq;
11221 struct lpfc_iocbq *abtsiocb;
ecbb227e 11222 struct lpfc_sli_ring *pring_s4;
dea3101e 11223 IOCB_t *cmd = NULL;
dea3101e 11224 int errcnt = 0, ret_val = 0;
0bd4ca25 11225 int i;
dea3101e 11226
0bd4ca25
JSEC
11227 for (i = 1; i <= phba->sli.last_iotag; i++) {
11228 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11229
51ef4c26 11230 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 11231 abort_cmd) != 0)
dea3101e 11232 continue;
11233
afbd8d88
JS
11234 /*
11235 * If the iocbq is already being aborted, don't take a second
11236 * action, but do count it.
11237 */
11238 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11239 continue;
11240
dea3101e 11241 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 11242 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 11243 if (abtsiocb == NULL) {
11244 errcnt++;
11245 continue;
11246 }
dea3101e 11247
afbd8d88
JS
11248 /* indicate the IO is being aborted by the driver. */
11249 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11250
0bd4ca25 11251 cmd = &iocbq->iocb;
dea3101e 11252 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11253 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11254 if (phba->sli_rev == LPFC_SLI_REV4)
11255 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11256 else
11257 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 11258 abtsiocb->iocb.ulpLe = 1;
11259 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11260 abtsiocb->vport = vport;
dea3101e 11261
5ffc266e 11262 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11263 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11264 if (iocbq->iocb_flag & LPFC_IO_FCP)
11265 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11266 if (iocbq->iocb_flag & LPFC_IO_FOF)
11267 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11268
2e0fef85 11269 if (lpfc_is_link_up(phba))
dea3101e 11270 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11271 else
11272 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11273
5eb95af0
JSEC
11274 /* Setup callback routine and issue the command. */
11275 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11276 if (phba->sli_rev == LPFC_SLI_REV4) {
11277 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11278 if (!pring_s4)
11279 continue;
11280 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11281 abtsiocb, 0);
11282 } else
11283 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11284 abtsiocb, 0);
dea3101e 11285 if (ret_val == IOCB_ERROR) {
604a3e30 11286 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 11287 errcnt++;
11288 continue;
11289 }
11290 }
11291
11292 return errcnt;
11293}
11294
98912dda
JS
11295/**
11296 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11297 * @vport: Pointer to virtual port.
11298 * @pring: Pointer to driver SLI ring object.
11299 * @tgt_id: SCSI ID of the target.
11300 * @lun_id: LUN ID of the scsi device.
11301 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11302 *
11303 * This function sends an abort command for every SCSI command
11304 * associated with the given virtual port pending on the ring
11305 * filtered by lpfc_sli_validate_fcp_iocb function.
11306 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11307 * FCP iocbs associated with lun specified by tgt_id and lun_id
11308 * parameters
11309 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11310 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11311 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11312 * FCP iocbs associated with virtual port.
11313 * This function returns number of iocbs it aborted .
11314 * This function is called with no locks held right after a taskmgmt
11315 * command is sent.
11316 **/
11317int
11318lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11319 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11320{
11321 struct lpfc_hba *phba = vport->phba;
8c50d25c 11322 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11323 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11324 struct lpfc_nodelist *ndlp;
98912dda
JS
11325 struct lpfc_iocbq *iocbq;
11326 IOCB_t *icmd;
11327 int sum, i, ret_val;
11328 unsigned long iflags;
11329 struct lpfc_sli_ring *pring_s4;
98912dda 11330
59c68eaa 11331 spin_lock_irqsave(&phba->hbalock, iflags);
98912dda
JS
11332
11333 /* all I/Os are in process of being flushed */
11334 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
59c68eaa 11335 spin_unlock_irqrestore(&phba->hbalock, iflags);
98912dda
JS
11336 return 0;
11337 }
11338 sum = 0;
11339
11340 for (i = 1; i <= phba->sli.last_iotag; i++) {
11341 iocbq = phba->sli.iocbq_lookup[i];
11342
11343 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11344 cmd) != 0)
11345 continue;
11346
11347 /*
11348 * If the iocbq is already being aborted, don't take a second
11349 * action, but do count it.
11350 */
11351 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11352 continue;
11353
11354 /* issue ABTS for this IOCB based on iotag */
11355 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11356 if (abtsiocbq == NULL)
11357 continue;
11358
11359 icmd = &iocbq->iocb;
11360 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11361 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11362 if (phba->sli_rev == LPFC_SLI_REV4)
11363 abtsiocbq->iocb.un.acxri.abortIoTag =
11364 iocbq->sli4_xritag;
11365 else
11366 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11367 abtsiocbq->iocb.ulpLe = 1;
11368 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11369 abtsiocbq->vport = vport;
11370
11371 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11372 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11373 if (iocbq->iocb_flag & LPFC_IO_FCP)
11374 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11375 if (iocbq->iocb_flag & LPFC_IO_FOF)
11376 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11377
8c50d25c
JS
11378 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11379 ndlp = lpfc_cmd->rdata->pnode;
11380
11381 if (lpfc_is_link_up(phba) &&
11382 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11383 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11384 else
11385 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11386
11387 /* Setup callback routine and issue the command. */
11388 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11389
11390 /*
11391 * Indicate the IO is being aborted by the driver and set
11392 * the caller's flag into the aborted IO.
11393 */
11394 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11395
11396 if (phba->sli_rev == LPFC_SLI_REV4) {
59c68eaa
JS
11397 pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
11398 if (!pring_s4)
895427bd 11399 continue;
98912dda 11400 /* Note: both hbalock and ring_lock must be set here */
59c68eaa 11401 spin_lock(&pring_s4->ring_lock);
98912dda
JS
11402 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11403 abtsiocbq, 0);
59c68eaa 11404 spin_unlock(&pring_s4->ring_lock);
98912dda
JS
11405 } else {
11406 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11407 abtsiocbq, 0);
11408 }
11409
11410
11411 if (ret_val == IOCB_ERROR)
11412 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11413 else
11414 sum++;
11415 }
59c68eaa 11416 spin_unlock_irqrestore(&phba->hbalock, iflags);
98912dda
JS
11417 return sum;
11418}
11419
e59058c4 11420/**
3621a710 11421 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11422 * @phba: Pointer to HBA context object.
11423 * @cmdiocbq: Pointer to command iocb.
11424 * @rspiocbq: Pointer to response iocb.
11425 *
11426 * This function is the completion handler for iocbs issued using
11427 * lpfc_sli_issue_iocb_wait function. This function is called by the
11428 * ring event handler function without any lock held. This function
11429 * can be called from both worker thread context and interrupt
11430 * context. This function also can be called from other thread which
11431 * cleans up the SLI layer objects.
11432 * This function copy the contents of the response iocb to the
11433 * response iocb memory object provided by the caller of
11434 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11435 * sleeps for the iocb completion.
11436 **/
68876920
JSEC
11437static void
11438lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11439 struct lpfc_iocbq *cmdiocbq,
11440 struct lpfc_iocbq *rspiocbq)
dea3101e 11441{
68876920
JSEC
11442 wait_queue_head_t *pdone_q;
11443 unsigned long iflags;
0f65ff68 11444 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11445
2e0fef85 11446 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11447 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11448
11449 /*
11450 * A time out has occurred for the iocb. If a time out
11451 * completion handler has been supplied, call it. Otherwise,
11452 * just free the iocbq.
11453 */
11454
11455 spin_unlock_irqrestore(&phba->hbalock, iflags);
11456 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11457 cmdiocbq->wait_iocb_cmpl = NULL;
11458 if (cmdiocbq->iocb_cmpl)
11459 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11460 else
11461 lpfc_sli_release_iocbq(phba, cmdiocbq);
11462 return;
11463 }
11464
68876920
JSEC
11465 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11466 if (cmdiocbq->context2 && rspiocbq)
11467 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11468 &rspiocbq->iocb, sizeof(IOCB_t));
11469
0f65ff68
JS
11470 /* Set the exchange busy flag for task management commands */
11471 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11472 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11473 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11474 cur_iocbq);
11475 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11476 }
11477
68876920 11478 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11479 if (pdone_q)
11480 wake_up(pdone_q);
858c9f6c 11481 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 11482 return;
11483}
11484
d11e31dd
JS
11485/**
11486 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11487 * @phba: Pointer to HBA context object..
11488 * @piocbq: Pointer to command iocb.
11489 * @flag: Flag to test.
11490 *
11491 * This routine grabs the hbalock and then test the iocb_flag to
11492 * see if the passed in flag is set.
11493 * Returns:
11494 * 1 if flag is set.
11495 * 0 if flag is not set.
11496 **/
11497static int
11498lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11499 struct lpfc_iocbq *piocbq, uint32_t flag)
11500{
11501 unsigned long iflags;
11502 int ret;
11503
11504 spin_lock_irqsave(&phba->hbalock, iflags);
11505 ret = piocbq->iocb_flag & flag;
11506 spin_unlock_irqrestore(&phba->hbalock, iflags);
11507 return ret;
11508
11509}
11510
e59058c4 11511/**
3621a710 11512 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11513 * @phba: Pointer to HBA context object..
11514 * @pring: Pointer to sli ring.
11515 * @piocb: Pointer to command iocb.
11516 * @prspiocbq: Pointer to response iocb.
11517 * @timeout: Timeout in number of seconds.
11518 *
11519 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11520 * iocb to complete. The iocb_cmpl field of the shall be used
11521 * to handle iocbs which time out. If the field is NULL, the
11522 * function shall free the iocbq structure. If more clean up is
11523 * needed, the caller is expected to provide a completion function
11524 * that will provide the needed clean up. If the iocb command is
11525 * not completed within timeout seconds, the function will either
11526 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11527 * completion function set in the iocb_cmpl field and then return
11528 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11529 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11530 * The function waits for the iocb completion using an
11531 * non-interruptible wait.
11532 * This function will sleep while waiting for iocb completion.
11533 * So, this function should not be called from any context which
11534 * does not allow sleeping. Due to the same reason, this function
11535 * cannot be called with interrupt disabled.
11536 * This function assumes that the iocb completions occur while
11537 * this function sleep. So, this function cannot be called from
11538 * the thread which process iocb completion for this ring.
11539 * This function clears the iocb_flag of the iocb object before
11540 * issuing the iocb and the iocb completion handler sets this
11541 * flag and wakes this thread when the iocb completes.
11542 * The contents of the response iocb will be copied to prspiocbq
11543 * by the completion handler when the command completes.
11544 * This function returns IOCB_SUCCESS when success.
11545 * This function is called with no lock held.
11546 **/
dea3101e 11547int
2e0fef85 11548lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11549 uint32_t ring_number,
2e0fef85
JS
11550 struct lpfc_iocbq *piocb,
11551 struct lpfc_iocbq *prspiocbq,
68876920 11552 uint32_t timeout)
dea3101e 11553{
7259f0d0 11554 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11555 long timeleft, timeout_req = 0;
11556 int retval = IOCB_SUCCESS;
875fbdfe 11557 uint32_t creg_val;
0e9bb8d7
JS
11558 struct lpfc_iocbq *iocb;
11559 int txq_cnt = 0;
11560 int txcmplq_cnt = 0;
895427bd 11561 struct lpfc_sli_ring *pring;
5a0916b4
JS
11562 unsigned long iflags;
11563 bool iocb_completed = true;
11564
895427bd
JS
11565 if (phba->sli_rev >= LPFC_SLI_REV4)
11566 pring = lpfc_sli4_calc_ring(phba, piocb);
11567 else
11568 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11569 /*
68876920
JSEC
11570 * If the caller has provided a response iocbq buffer, then context2
11571 * is NULL or its an error.
dea3101e 11572 */
68876920
JSEC
11573 if (prspiocbq) {
11574 if (piocb->context2)
11575 return IOCB_ERROR;
11576 piocb->context2 = prspiocbq;
dea3101e 11577 }
11578
5a0916b4 11579 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11580 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11581 piocb->context_un.wait_queue = &done_q;
5a0916b4 11582 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11583
875fbdfe 11584 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11585 if (lpfc_readl(phba->HCregaddr, &creg_val))
11586 return IOCB_ERROR;
875fbdfe
JSEC
11587 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11588 writel(creg_val, phba->HCregaddr);
11589 readl(phba->HCregaddr); /* flush */
11590 }
11591
2a9bf3d0
JS
11592 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11593 SLI_IOCB_RET_IOCB);
68876920 11594 if (retval == IOCB_SUCCESS) {
256ec0d0 11595 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11596 timeleft = wait_event_timeout(done_q,
d11e31dd 11597 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11598 timeout_req);
5a0916b4
JS
11599 spin_lock_irqsave(&phba->hbalock, iflags);
11600 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11601
11602 /*
11603 * IOCB timed out. Inform the wake iocb wait
11604 * completion function and set local status
11605 */
dea3101e 11606
5a0916b4
JS
11607 iocb_completed = false;
11608 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11609 }
11610 spin_unlock_irqrestore(&phba->hbalock, iflags);
11611 if (iocb_completed) {
7054a606 11612 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11613 "0331 IOCB wake signaled\n");
53151bbb
JS
11614 /* Note: we are not indicating if the IOCB has a success
11615 * status or not - that's for the caller to check.
11616 * IOCB_SUCCESS means just that the command was sent and
11617 * completed. Not that it completed successfully.
11618 * */
7054a606 11619 } else if (timeleft == 0) {
68876920 11620 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11621 "0338 IOCB wait timeout error - no "
11622 "wake response Data x%x\n", timeout);
68876920 11623 retval = IOCB_TIMEDOUT;
7054a606 11624 } else {
68876920 11625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11626 "0330 IOCB wake NOT set, "
11627 "Data x%x x%lx\n",
68876920
JSEC
11628 timeout, (timeleft / jiffies));
11629 retval = IOCB_TIMEDOUT;
dea3101e 11630 }
2a9bf3d0 11631 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11632 if (phba->cfg_log_verbose & LOG_SLI) {
11633 list_for_each_entry(iocb, &pring->txq, list) {
11634 txq_cnt++;
11635 }
11636 list_for_each_entry(iocb, &pring->txcmplq, list) {
11637 txcmplq_cnt++;
11638 }
11639 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11640 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11641 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11642 }
2a9bf3d0 11643 return retval;
68876920
JSEC
11644 } else {
11645 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11646 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11647 retval);
68876920 11648 retval = IOCB_ERROR;
dea3101e 11649 }
11650
875fbdfe 11651 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11652 if (lpfc_readl(phba->HCregaddr, &creg_val))
11653 return IOCB_ERROR;
875fbdfe
JSEC
11654 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11655 writel(creg_val, phba->HCregaddr);
11656 readl(phba->HCregaddr); /* flush */
11657 }
11658
68876920
JSEC
11659 if (prspiocbq)
11660 piocb->context2 = NULL;
11661
11662 piocb->context_un.wait_queue = NULL;
11663 piocb->iocb_cmpl = NULL;
dea3101e 11664 return retval;
11665}
68876920 11666
e59058c4 11667/**
3621a710 11668 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11669 * @phba: Pointer to HBA context object.
11670 * @pmboxq: Pointer to driver mailbox object.
11671 * @timeout: Timeout in number of seconds.
11672 *
11673 * This function issues the mailbox to firmware and waits for the
11674 * mailbox command to complete. If the mailbox command is not
11675 * completed within timeout seconds, it returns MBX_TIMEOUT.
11676 * The function waits for the mailbox completion using an
11677 * interruptible wait. If the thread is woken up due to a
11678 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11679 * should not free the mailbox resources, if this function returns
11680 * MBX_TIMEOUT.
11681 * This function will sleep while waiting for mailbox completion.
11682 * So, this function should not be called from any context which
11683 * does not allow sleeping. Due to the same reason, this function
11684 * cannot be called with interrupt disabled.
11685 * This function assumes that the mailbox completion occurs while
11686 * this function sleep. So, this function cannot be called from
11687 * the worker thread which processes mailbox completion.
11688 * This function is called in the context of HBA management
11689 * applications.
11690 * This function returns MBX_SUCCESS when successful.
11691 * This function is called with no lock held.
11692 **/
dea3101e 11693int
2e0fef85 11694lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 11695 uint32_t timeout)
11696{
e29d74f8 11697 struct completion mbox_done;
dea3101e 11698 int retval;
858c9f6c 11699 unsigned long flag;
dea3101e 11700
495a714c 11701 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 11702 /* setup wake call as IOCB callback */
11703 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea3101e 11704
e29d74f8
JS
11705 /* setup context3 field to pass wait_queue pointer to wake function */
11706 init_completion(&mbox_done);
11707 pmboxq->context3 = &mbox_done;
dea3101e 11708 /* now issue the command */
11709 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11710 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
e29d74f8
JS
11711 wait_for_completion_timeout(&mbox_done,
11712 msecs_to_jiffies(timeout * 1000));
7054a606 11713
858c9f6c 11714 spin_lock_irqsave(&phba->hbalock, flag);
e29d74f8 11715 pmboxq->context3 = NULL;
7054a606
JS
11716 /*
11717 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11718 * else do not free the resources.
11719 */
d7c47992 11720 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11721 retval = MBX_SUCCESS;
d7c47992 11722 } else {
7054a606 11723 retval = MBX_TIMEOUT;
858c9f6c
JS
11724 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11725 }
11726 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 11727 }
dea3101e 11728 return retval;
11729}
11730
e59058c4 11731/**
3772a991 11732 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11733 * @phba: Pointer to HBA context.
11734 *
3772a991
JS
11735 * This function is called to shutdown the driver's mailbox sub-system.
11736 * It first marks the mailbox sub-system is in a block state to prevent
11737 * the asynchronous mailbox command from issued off the pending mailbox
11738 * command queue. If the mailbox command sub-system shutdown is due to
11739 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11740 * the mailbox sub-system flush routine to forcefully bring down the
11741 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11742 * as with offline or HBA function reset), this routine will wait for the
11743 * outstanding mailbox command to complete before invoking the mailbox
11744 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11745 **/
3772a991 11746void
618a5230 11747lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11748{
3772a991 11749 struct lpfc_sli *psli = &phba->sli;
3772a991 11750 unsigned long timeout;
b4c02652 11751
618a5230
JS
11752 if (mbx_action == LPFC_MBX_NO_WAIT) {
11753 /* delay 100ms for port state */
11754 msleep(100);
11755 lpfc_sli_mbox_sys_flush(phba);
11756 return;
11757 }
a183a15f 11758 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11759
3772a991
JS
11760 spin_lock_irq(&phba->hbalock);
11761 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11762
3772a991 11763 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11764 /* Determine how long we might wait for the active mailbox
11765 * command to be gracefully completed by firmware.
11766 */
a183a15f
JS
11767 if (phba->sli.mbox_active)
11768 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11769 phba->sli.mbox_active) *
11770 1000) + jiffies;
11771 spin_unlock_irq(&phba->hbalock);
11772
3772a991
JS
11773 while (phba->sli.mbox_active) {
11774 /* Check active mailbox complete status every 2ms */
11775 msleep(2);
11776 if (time_after(jiffies, timeout))
11777 /* Timeout, let the mailbox flush routine to
11778 * forcefully release active mailbox command
11779 */
11780 break;
11781 }
d7069f09
JS
11782 } else
11783 spin_unlock_irq(&phba->hbalock);
11784
3772a991
JS
11785 lpfc_sli_mbox_sys_flush(phba);
11786}
ed957684 11787
3772a991
JS
11788/**
11789 * lpfc_sli_eratt_read - read sli-3 error attention events
11790 * @phba: Pointer to HBA context.
11791 *
11792 * This function is called to read the SLI3 device error attention registers
11793 * for possible error attention events. The caller must hold the hostlock
11794 * with spin_lock_irq().
11795 *
25985edc 11796 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11797 * Register and returns 0 otherwise.
11798 **/
11799static int
11800lpfc_sli_eratt_read(struct lpfc_hba *phba)
11801{
11802 uint32_t ha_copy;
b4c02652 11803
3772a991 11804 /* Read chip Host Attention (HA) register */
9940b97b
JS
11805 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11806 goto unplug_err;
11807
3772a991
JS
11808 if (ha_copy & HA_ERATT) {
11809 /* Read host status register to retrieve error event */
9940b97b
JS
11810 if (lpfc_sli_read_hs(phba))
11811 goto unplug_err;
b4c02652 11812
3772a991
JS
11813 /* Check if there is a deferred error condition is active */
11814 if ((HS_FFER1 & phba->work_hs) &&
11815 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11816 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11817 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11818 /* Clear all interrupt enable conditions */
11819 writel(0, phba->HCregaddr);
11820 readl(phba->HCregaddr);
11821 }
11822
11823 /* Set the driver HA work bitmap */
3772a991
JS
11824 phba->work_ha |= HA_ERATT;
11825 /* Indicate polling handles this ERATT */
11826 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11827 return 1;
11828 }
11829 return 0;
9940b97b
JS
11830
11831unplug_err:
11832 /* Set the driver HS work bitmap */
11833 phba->work_hs |= UNPLUG_ERR;
11834 /* Set the driver HA work bitmap */
11835 phba->work_ha |= HA_ERATT;
11836 /* Indicate polling handles this ERATT */
11837 phba->hba_flag |= HBA_ERATT_HANDLED;
11838 return 1;
b4c02652
JS
11839}
11840
da0436e9
JS
11841/**
11842 * lpfc_sli4_eratt_read - read sli-4 error attention events
11843 * @phba: Pointer to HBA context.
11844 *
11845 * This function is called to read the SLI4 device error attention registers
11846 * for possible error attention events. The caller must hold the hostlock
11847 * with spin_lock_irq().
11848 *
25985edc 11849 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11850 * Register and returns 0 otherwise.
11851 **/
11852static int
11853lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11854{
11855 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11856 uint32_t if_type, portsmphr;
11857 struct lpfc_register portstat_reg;
da0436e9 11858
2fcee4bf
JS
11859 /*
11860 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11861 * registers for error attention. This can be changed later.
11862 */
2fcee4bf
JS
11863 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11864 switch (if_type) {
11865 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11866 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11867 &uerr_sta_lo) ||
11868 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11869 &uerr_sta_hi)) {
11870 phba->work_hs |= UNPLUG_ERR;
11871 phba->work_ha |= HA_ERATT;
11872 phba->hba_flag |= HBA_ERATT_HANDLED;
11873 return 1;
11874 }
2fcee4bf
JS
11875 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11876 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11878 "1423 HBA Unrecoverable error: "
11879 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11880 "ue_mask_lo_reg=0x%x, "
11881 "ue_mask_hi_reg=0x%x\n",
11882 uerr_sta_lo, uerr_sta_hi,
11883 phba->sli4_hba.ue_mask_lo,
11884 phba->sli4_hba.ue_mask_hi);
11885 phba->work_status[0] = uerr_sta_lo;
11886 phba->work_status[1] = uerr_sta_hi;
11887 phba->work_ha |= HA_ERATT;
11888 phba->hba_flag |= HBA_ERATT_HANDLED;
11889 return 1;
11890 }
11891 break;
11892 case LPFC_SLI_INTF_IF_TYPE_2:
27d6ac0a 11893 case LPFC_SLI_INTF_IF_TYPE_6:
9940b97b
JS
11894 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11895 &portstat_reg.word0) ||
11896 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11897 &portsmphr)){
11898 phba->work_hs |= UNPLUG_ERR;
11899 phba->work_ha |= HA_ERATT;
11900 phba->hba_flag |= HBA_ERATT_HANDLED;
11901 return 1;
11902 }
2fcee4bf
JS
11903 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11904 phba->work_status[0] =
11905 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11906 phba->work_status[1] =
11907 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11909 "2885 Port Status Event: "
2fcee4bf
JS
11910 "port status reg 0x%x, "
11911 "port smphr reg 0x%x, "
11912 "error 1=0x%x, error 2=0x%x\n",
11913 portstat_reg.word0,
11914 portsmphr,
11915 phba->work_status[0],
11916 phba->work_status[1]);
11917 phba->work_ha |= HA_ERATT;
11918 phba->hba_flag |= HBA_ERATT_HANDLED;
11919 return 1;
11920 }
11921 break;
11922 case LPFC_SLI_INTF_IF_TYPE_1:
11923 default:
a747c9ce 11924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11925 "2886 HBA Error Attention on unsupported "
11926 "if type %d.", if_type);
a747c9ce 11927 return 1;
da0436e9 11928 }
2fcee4bf 11929
da0436e9
JS
11930 return 0;
11931}
11932
e59058c4 11933/**
3621a710 11934 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11935 * @phba: Pointer to HBA context.
11936 *
3772a991 11937 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11938 * error attention register bit for error attention events.
11939 *
25985edc 11940 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11941 * Register and returns 0 otherwise.
11942 **/
11943int
11944lpfc_sli_check_eratt(struct lpfc_hba *phba)
11945{
11946 uint32_t ha_copy;
11947
11948 /* If somebody is waiting to handle an eratt, don't process it
11949 * here. The brdkill function will do this.
11950 */
11951 if (phba->link_flag & LS_IGNORE_ERATT)
11952 return 0;
11953
11954 /* Check if interrupt handler handles this ERATT */
11955 spin_lock_irq(&phba->hbalock);
11956 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11957 /* Interrupt handler has handled ERATT */
11958 spin_unlock_irq(&phba->hbalock);
11959 return 0;
11960 }
11961
a257bf90
JS
11962 /*
11963 * If there is deferred error attention, do not check for error
11964 * attention
11965 */
11966 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11967 spin_unlock_irq(&phba->hbalock);
11968 return 0;
11969 }
11970
3772a991
JS
11971 /* If PCI channel is offline, don't process it */
11972 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11973 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11974 return 0;
11975 }
11976
11977 switch (phba->sli_rev) {
11978 case LPFC_SLI_REV2:
11979 case LPFC_SLI_REV3:
11980 /* Read chip Host Attention (HA) register */
11981 ha_copy = lpfc_sli_eratt_read(phba);
11982 break;
da0436e9 11983 case LPFC_SLI_REV4:
2fcee4bf 11984 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11985 ha_copy = lpfc_sli4_eratt_read(phba);
11986 break;
3772a991
JS
11987 default:
11988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11989 "0299 Invalid SLI revision (%d)\n",
11990 phba->sli_rev);
11991 ha_copy = 0;
11992 break;
9399627f
JS
11993 }
11994 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11995
11996 return ha_copy;
11997}
11998
11999/**
12000 * lpfc_intr_state_check - Check device state for interrupt handling
12001 * @phba: Pointer to HBA context.
12002 *
12003 * This inline routine checks whether a device or its PCI slot is in a state
12004 * that the interrupt should be handled.
12005 *
12006 * This function returns 0 if the device or the PCI slot is in a state that
12007 * interrupt should be handled, otherwise -EIO.
12008 */
12009static inline int
12010lpfc_intr_state_check(struct lpfc_hba *phba)
12011{
12012 /* If the pci channel is offline, ignore all the interrupts */
12013 if (unlikely(pci_channel_offline(phba->pcidev)))
12014 return -EIO;
12015
12016 /* Update device level interrupt statistics */
12017 phba->sli.slistat.sli_intr++;
12018
12019 /* Ignore all interrupts during initialization. */
12020 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12021 return -EIO;
12022
9399627f
JS
12023 return 0;
12024}
12025
12026/**
3772a991 12027 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
12028 * @irq: Interrupt number.
12029 * @dev_id: The device context pointer.
12030 *
9399627f 12031 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12032 * service routine when device with SLI-3 interface spec is enabled with
12033 * MSI-X multi-message interrupt mode and there are slow-path events in
12034 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12035 * interrupt mode, this function is called as part of the device-level
12036 * interrupt handler. When the PCI slot is in error recovery or the HBA
12037 * is undergoing initialization, the interrupt handler will not process
12038 * the interrupt. The link attention and ELS ring attention events are
12039 * handled by the worker thread. The interrupt handler signals the worker
12040 * thread and returns for these events. This function is called without
12041 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
12042 * structures.
12043 *
12044 * This function returns IRQ_HANDLED when interrupt is handled else it
12045 * returns IRQ_NONE.
e59058c4 12046 **/
dea3101e 12047irqreturn_t
3772a991 12048lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 12049{
2e0fef85 12050 struct lpfc_hba *phba;
a747c9ce 12051 uint32_t ha_copy, hc_copy;
dea3101e 12052 uint32_t work_ha_copy;
12053 unsigned long status;
5b75da2f 12054 unsigned long iflag;
dea3101e 12055 uint32_t control;
12056
92d7f7b0 12057 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
12058 struct lpfc_vport *vport;
12059 struct lpfc_nodelist *ndlp;
12060 struct lpfc_dmabuf *mp;
92d7f7b0
JS
12061 LPFC_MBOXQ_t *pmb;
12062 int rc;
12063
dea3101e 12064 /*
12065 * Get the driver's phba structure from the dev_id and
12066 * assume the HBA is not interrupting.
12067 */
9399627f 12068 phba = (struct lpfc_hba *)dev_id;
dea3101e 12069
12070 if (unlikely(!phba))
12071 return IRQ_NONE;
12072
dea3101e 12073 /*
9399627f
JS
12074 * Stuff needs to be attented to when this function is invoked as an
12075 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 12076 */
9399627f 12077 if (phba->intr_type == MSIX) {
3772a991
JS
12078 /* Check device state for handling interrupt */
12079 if (lpfc_intr_state_check(phba))
9399627f
JS
12080 return IRQ_NONE;
12081 /* Need to read HA REG for slow-path events */
5b75da2f 12082 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12083 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12084 goto unplug_error;
9399627f
JS
12085 /* If somebody is waiting to handle an eratt don't process it
12086 * here. The brdkill function will do this.
12087 */
12088 if (phba->link_flag & LS_IGNORE_ERATT)
12089 ha_copy &= ~HA_ERATT;
12090 /* Check the need for handling ERATT in interrupt handler */
12091 if (ha_copy & HA_ERATT) {
12092 if (phba->hba_flag & HBA_ERATT_HANDLED)
12093 /* ERATT polling has handled ERATT */
12094 ha_copy &= ~HA_ERATT;
12095 else
12096 /* Indicate interrupt handler handles ERATT */
12097 phba->hba_flag |= HBA_ERATT_HANDLED;
12098 }
a257bf90
JS
12099
12100 /*
12101 * If there is deferred error attention, do not check for any
12102 * interrupt.
12103 */
12104 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12105 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12106 return IRQ_NONE;
12107 }
12108
9399627f 12109 /* Clear up only attention source related to slow-path */
9940b97b
JS
12110 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12111 goto unplug_error;
12112
a747c9ce
JS
12113 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12114 HC_LAINT_ENA | HC_ERINT_ENA),
12115 phba->HCregaddr);
9399627f
JS
12116 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12117 phba->HAregaddr);
a747c9ce 12118 writel(hc_copy, phba->HCregaddr);
9399627f 12119 readl(phba->HAregaddr); /* flush */
5b75da2f 12120 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12121 } else
12122 ha_copy = phba->ha_copy;
dea3101e 12123
dea3101e 12124 work_ha_copy = ha_copy & phba->work_ha_mask;
12125
9399627f 12126 if (work_ha_copy) {
dea3101e 12127 if (work_ha_copy & HA_LATT) {
12128 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12129 /*
12130 * Turn off Link Attention interrupts
12131 * until CLEAR_LA done
12132 */
5b75da2f 12133 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12134 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
12135 if (lpfc_readl(phba->HCregaddr, &control))
12136 goto unplug_error;
dea3101e 12137 control &= ~HC_LAINT_ENA;
12138 writel(control, phba->HCregaddr);
12139 readl(phba->HCregaddr); /* flush */
5b75da2f 12140 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 12141 }
12142 else
12143 work_ha_copy &= ~HA_LATT;
12144 }
12145
9399627f 12146 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
12147 /*
12148 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12149 * the only slow ring.
12150 */
12151 status = (work_ha_copy &
12152 (HA_RXMASK << (4*LPFC_ELS_RING)));
12153 status >>= (4*LPFC_ELS_RING);
12154 if (status & HA_RXMASK) {
5b75da2f 12155 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12156 if (lpfc_readl(phba->HCregaddr, &control))
12157 goto unplug_error;
a58cbd52
JS
12158
12159 lpfc_debugfs_slow_ring_trc(phba,
12160 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12161 control, status,
12162 (uint32_t)phba->sli.slistat.sli_intr);
12163
858c9f6c 12164 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
12165 lpfc_debugfs_slow_ring_trc(phba,
12166 "ISR Disable ring:"
12167 "pwork:x%x hawork:x%x wait:x%x",
12168 phba->work_ha, work_ha_copy,
12169 (uint32_t)((unsigned long)
5e9d9b82 12170 &phba->work_waitq));
a58cbd52 12171
858c9f6c
JS
12172 control &=
12173 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 12174 writel(control, phba->HCregaddr);
12175 readl(phba->HCregaddr); /* flush */
dea3101e 12176 }
a58cbd52
JS
12177 else {
12178 lpfc_debugfs_slow_ring_trc(phba,
12179 "ISR slow ring: pwork:"
12180 "x%x hawork:x%x wait:x%x",
12181 phba->work_ha, work_ha_copy,
12182 (uint32_t)((unsigned long)
5e9d9b82 12183 &phba->work_waitq));
a58cbd52 12184 }
5b75da2f 12185 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 12186 }
12187 }
5b75da2f 12188 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 12189 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
12190 if (lpfc_sli_read_hs(phba))
12191 goto unplug_error;
a257bf90
JS
12192 /*
12193 * Check if there is a deferred error condition
12194 * is active
12195 */
12196 if ((HS_FFER1 & phba->work_hs) &&
12197 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
12198 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12199 phba->work_hs)) {
a257bf90
JS
12200 phba->hba_flag |= DEFER_ERATT;
12201 /* Clear all interrupt enable conditions */
12202 writel(0, phba->HCregaddr);
12203 readl(phba->HCregaddr);
12204 }
12205 }
12206
9399627f 12207 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 12208 pmb = phba->sli.mbox_active;
04c68496 12209 pmbox = &pmb->u.mb;
34b02dcd 12210 mbox = phba->mbox;
858c9f6c 12211 vport = pmb->vport;
92d7f7b0
JS
12212
12213 /* First check out the status word */
12214 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12215 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 12216 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
12217 /*
12218 * Stray Mailbox Interrupt, mbxCommand <cmd>
12219 * mbxStatus <status>
12220 */
09372820 12221 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 12222 LOG_SLI,
e8b62011 12223 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
12224 "Interrupt mbxCommand x%x "
12225 "mbxStatus x%x\n",
e8b62011 12226 (vport ? vport->vpi : 0),
92d7f7b0
JS
12227 pmbox->mbxCommand,
12228 pmbox->mbxStatus);
09372820
JS
12229 /* clear mailbox attention bit */
12230 work_ha_copy &= ~HA_MBATT;
12231 } else {
97eab634 12232 phba->sli.mbox_active = NULL;
5b75da2f 12233 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
12234 phba->last_completion_time = jiffies;
12235 del_timer(&phba->sli.mbox_tmo);
09372820
JS
12236 if (pmb->mbox_cmpl) {
12237 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12238 MAILBOX_CMD_SIZE);
7a470277
JS
12239 if (pmb->out_ext_byte_len &&
12240 pmb->context2)
12241 lpfc_sli_pcimem_bcopy(
12242 phba->mbox_ext,
12243 pmb->context2,
12244 pmb->out_ext_byte_len);
09372820
JS
12245 }
12246 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12247 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12248
12249 lpfc_debugfs_disc_trc(vport,
12250 LPFC_DISC_TRC_MBOX_VPORT,
12251 "MBOX dflt rpi: : "
12252 "status:x%x rpi:x%x",
12253 (uint32_t)pmbox->mbxStatus,
12254 pmbox->un.varWords[0], 0);
12255
12256 if (!pmbox->mbxStatus) {
12257 mp = (struct lpfc_dmabuf *)
12258 (pmb->context1);
12259 ndlp = (struct lpfc_nodelist *)
12260 pmb->context2;
12261
12262 /* Reg_LOGIN of dflt RPI was
12263 * successful. new lets get
12264 * rid of the RPI using the
12265 * same mbox buffer.
12266 */
12267 lpfc_unreg_login(phba,
12268 vport->vpi,
12269 pmbox->un.varWords[0],
12270 pmb);
12271 pmb->mbox_cmpl =
12272 lpfc_mbx_cmpl_dflt_rpi;
12273 pmb->context1 = mp;
12274 pmb->context2 = ndlp;
12275 pmb->vport = vport;
58da1ffb
JS
12276 rc = lpfc_sli_issue_mbox(phba,
12277 pmb,
12278 MBX_NOWAIT);
12279 if (rc != MBX_BUSY)
12280 lpfc_printf_log(phba,
12281 KERN_ERR,
12282 LOG_MBOX | LOG_SLI,
d7c255b2 12283 "0350 rc should have"
6a9c52cf 12284 "been MBX_BUSY\n");
3772a991
JS
12285 if (rc != MBX_NOT_FINISHED)
12286 goto send_current_mbox;
09372820 12287 }
858c9f6c 12288 }
5b75da2f
JS
12289 spin_lock_irqsave(
12290 &phba->pport->work_port_lock,
12291 iflag);
09372820
JS
12292 phba->pport->work_port_events &=
12293 ~WORKER_MBOX_TMO;
5b75da2f
JS
12294 spin_unlock_irqrestore(
12295 &phba->pport->work_port_lock,
12296 iflag);
09372820 12297 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12298 }
97eab634 12299 } else
5b75da2f 12300 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12301
92d7f7b0
JS
12302 if ((work_ha_copy & HA_MBATT) &&
12303 (phba->sli.mbox_active == NULL)) {
858c9f6c 12304send_current_mbox:
92d7f7b0 12305 /* Process next mailbox command if there is one */
58da1ffb
JS
12306 do {
12307 rc = lpfc_sli_issue_mbox(phba, NULL,
12308 MBX_NOWAIT);
12309 } while (rc == MBX_NOT_FINISHED);
12310 if (rc != MBX_SUCCESS)
12311 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12312 LOG_SLI, "0349 rc should be "
6a9c52cf 12313 "MBX_SUCCESS\n");
92d7f7b0
JS
12314 }
12315
5b75da2f 12316 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12317 phba->work_ha |= work_ha_copy;
5b75da2f 12318 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12319 lpfc_worker_wake_up(phba);
dea3101e 12320 }
9399627f 12321 return IRQ_HANDLED;
9940b97b
JS
12322unplug_error:
12323 spin_unlock_irqrestore(&phba->hbalock, iflag);
12324 return IRQ_HANDLED;
dea3101e 12325
3772a991 12326} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12327
12328/**
3772a991 12329 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12330 * @irq: Interrupt number.
12331 * @dev_id: The device context pointer.
12332 *
12333 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12334 * service routine when device with SLI-3 interface spec is enabled with
12335 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12336 * ring event in the HBA. However, when the device is enabled with either
12337 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12338 * device-level interrupt handler. When the PCI slot is in error recovery
12339 * or the HBA is undergoing initialization, the interrupt handler will not
12340 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12341 * the intrrupt context. This function is called without any lock held.
12342 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12343 *
12344 * This function returns IRQ_HANDLED when interrupt is handled else it
12345 * returns IRQ_NONE.
12346 **/
12347irqreturn_t
3772a991 12348lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12349{
12350 struct lpfc_hba *phba;
12351 uint32_t ha_copy;
12352 unsigned long status;
5b75da2f 12353 unsigned long iflag;
895427bd 12354 struct lpfc_sli_ring *pring;
9399627f
JS
12355
12356 /* Get the driver's phba structure from the dev_id and
12357 * assume the HBA is not interrupting.
12358 */
12359 phba = (struct lpfc_hba *) dev_id;
12360
12361 if (unlikely(!phba))
12362 return IRQ_NONE;
12363
12364 /*
12365 * Stuff needs to be attented to when this function is invoked as an
12366 * individual interrupt handler in MSI-X multi-message interrupt mode
12367 */
12368 if (phba->intr_type == MSIX) {
3772a991
JS
12369 /* Check device state for handling interrupt */
12370 if (lpfc_intr_state_check(phba))
9399627f
JS
12371 return IRQ_NONE;
12372 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12373 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12374 return IRQ_HANDLED;
9399627f 12375 /* Clear up only attention source related to fast-path */
5b75da2f 12376 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12377 /*
12378 * If there is deferred error attention, do not check for
12379 * any interrupt.
12380 */
12381 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12382 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12383 return IRQ_NONE;
12384 }
9399627f
JS
12385 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12386 phba->HAregaddr);
12387 readl(phba->HAregaddr); /* flush */
5b75da2f 12388 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12389 } else
12390 ha_copy = phba->ha_copy;
dea3101e 12391
12392 /*
9399627f 12393 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12394 */
9399627f
JS
12395 ha_copy &= ~(phba->work_ha_mask);
12396
12397 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12398 status >>= (4*LPFC_FCP_RING);
895427bd 12399 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12400 if (status & HA_RXMASK)
895427bd 12401 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12402
12403 if (phba->cfg_multi_ring_support == 2) {
12404 /*
9399627f
JS
12405 * Process all events on extra ring. Take the optimized path
12406 * for extra ring IO.
a4bc3379 12407 */
9399627f 12408 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12409 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12410 if (status & HA_RXMASK) {
a4bc3379 12411 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12412 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12413 status);
12414 }
12415 }
dea3101e 12416 return IRQ_HANDLED;
3772a991 12417} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12418
12419/**
3772a991 12420 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12421 * @irq: Interrupt number.
12422 * @dev_id: The device context pointer.
12423 *
3772a991
JS
12424 * This function is the HBA device-level interrupt handler to device with
12425 * SLI-3 interface spec, called from the PCI layer when either MSI or
12426 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12427 * requires driver attention. This function invokes the slow-path interrupt
12428 * attention handling function and fast-path interrupt attention handling
12429 * function in turn to process the relevant HBA attention events. This
12430 * function is called without any lock held. It gets the hbalock to access
12431 * and update SLI data structures.
9399627f
JS
12432 *
12433 * This function returns IRQ_HANDLED when interrupt is handled, else it
12434 * returns IRQ_NONE.
12435 **/
12436irqreturn_t
3772a991 12437lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12438{
12439 struct lpfc_hba *phba;
12440 irqreturn_t sp_irq_rc, fp_irq_rc;
12441 unsigned long status1, status2;
a747c9ce 12442 uint32_t hc_copy;
9399627f
JS
12443
12444 /*
12445 * Get the driver's phba structure from the dev_id and
12446 * assume the HBA is not interrupting.
12447 */
12448 phba = (struct lpfc_hba *) dev_id;
12449
12450 if (unlikely(!phba))
12451 return IRQ_NONE;
12452
3772a991
JS
12453 /* Check device state for handling interrupt */
12454 if (lpfc_intr_state_check(phba))
9399627f
JS
12455 return IRQ_NONE;
12456
12457 spin_lock(&phba->hbalock);
9940b97b
JS
12458 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12459 spin_unlock(&phba->hbalock);
12460 return IRQ_HANDLED;
12461 }
12462
9399627f
JS
12463 if (unlikely(!phba->ha_copy)) {
12464 spin_unlock(&phba->hbalock);
12465 return IRQ_NONE;
12466 } else if (phba->ha_copy & HA_ERATT) {
12467 if (phba->hba_flag & HBA_ERATT_HANDLED)
12468 /* ERATT polling has handled ERATT */
12469 phba->ha_copy &= ~HA_ERATT;
12470 else
12471 /* Indicate interrupt handler handles ERATT */
12472 phba->hba_flag |= HBA_ERATT_HANDLED;
12473 }
12474
a257bf90
JS
12475 /*
12476 * If there is deferred error attention, do not check for any interrupt.
12477 */
12478 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12479 spin_unlock(&phba->hbalock);
a257bf90
JS
12480 return IRQ_NONE;
12481 }
12482
9399627f 12483 /* Clear attention sources except link and error attentions */
9940b97b
JS
12484 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12485 spin_unlock(&phba->hbalock);
12486 return IRQ_HANDLED;
12487 }
a747c9ce
JS
12488 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12489 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12490 phba->HCregaddr);
9399627f 12491 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12492 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12493 readl(phba->HAregaddr); /* flush */
12494 spin_unlock(&phba->hbalock);
12495
12496 /*
12497 * Invokes slow-path host attention interrupt handling as appropriate.
12498 */
12499
12500 /* status of events with mailbox and link attention */
12501 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12502
12503 /* status of events with ELS ring */
12504 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12505 status2 >>= (4*LPFC_ELS_RING);
12506
12507 if (status1 || (status2 & HA_RXMASK))
3772a991 12508 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12509 else
12510 sp_irq_rc = IRQ_NONE;
12511
12512 /*
12513 * Invoke fast-path host attention interrupt handling as appropriate.
12514 */
12515
12516 /* status of events with FCP ring */
12517 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12518 status1 >>= (4*LPFC_FCP_RING);
12519
12520 /* status of events with extra ring */
12521 if (phba->cfg_multi_ring_support == 2) {
12522 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12523 status2 >>= (4*LPFC_EXTRA_RING);
12524 } else
12525 status2 = 0;
12526
12527 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12528 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12529 else
12530 fp_irq_rc = IRQ_NONE;
dea3101e 12531
9399627f
JS
12532 /* Return device-level interrupt handling status */
12533 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12534} /* lpfc_sli_intr_handler */
4f774513
JS
12535
12536/**
12537 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12538 * @phba: pointer to lpfc hba data structure.
12539 *
12540 * This routine is invoked by the worker thread to process all the pending
12541 * SLI4 FCP abort XRI events.
12542 **/
12543void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12544{
12545 struct lpfc_cq_event *cq_event;
12546
12547 /* First, declare the fcp xri abort event has been handled */
12548 spin_lock_irq(&phba->hbalock);
12549 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12550 spin_unlock_irq(&phba->hbalock);
12551 /* Now, handle all the fcp xri abort events */
12552 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12553 /* Get the first event from the head of the event queue */
12554 spin_lock_irq(&phba->hbalock);
12555 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12556 cq_event, struct lpfc_cq_event, list);
12557 spin_unlock_irq(&phba->hbalock);
12558 /* Notify aborted XRI for FCP work queue */
12559 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12560 /* Free the event processed back to the free pool */
12561 lpfc_sli4_cq_event_release(phba, cq_event);
12562 }
12563}
12564
12565/**
12566 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12567 * @phba: pointer to lpfc hba data structure.
12568 *
12569 * This routine is invoked by the worker thread to process all the pending
12570 * SLI4 els abort xri events.
12571 **/
12572void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12573{
12574 struct lpfc_cq_event *cq_event;
12575
12576 /* First, declare the els xri abort event has been handled */
12577 spin_lock_irq(&phba->hbalock);
12578 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12579 spin_unlock_irq(&phba->hbalock);
12580 /* Now, handle all the els xri abort events */
12581 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12582 /* Get the first event from the head of the event queue */
12583 spin_lock_irq(&phba->hbalock);
12584 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12585 cq_event, struct lpfc_cq_event, list);
12586 spin_unlock_irq(&phba->hbalock);
12587 /* Notify aborted XRI for ELS work queue */
12588 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12589 /* Free the event processed back to the free pool */
12590 lpfc_sli4_cq_event_release(phba, cq_event);
12591 }
12592}
12593
341af102
JS
12594/**
12595 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12596 * @phba: pointer to lpfc hba data structure
12597 * @pIocbIn: pointer to the rspiocbq
12598 * @pIocbOut: pointer to the cmdiocbq
12599 * @wcqe: pointer to the complete wcqe
12600 *
12601 * This routine transfers the fields of a command iocbq to a response iocbq
12602 * by copying all the IOCB fields from command iocbq and transferring the
12603 * completion status information from the complete wcqe.
12604 **/
4f774513 12605static void
341af102
JS
12606lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12607 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12608 struct lpfc_iocbq *pIocbOut,
12609 struct lpfc_wcqe_complete *wcqe)
12610{
af22741c 12611 int numBdes, i;
341af102 12612 unsigned long iflags;
af22741c
JS
12613 uint32_t status, max_response;
12614 struct lpfc_dmabuf *dmabuf;
12615 struct ulp_bde64 *bpl, bde;
4f774513
JS
12616 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12617
12618 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12619 sizeof(struct lpfc_iocbq) - offset);
4f774513 12620 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12621 status = bf_get(lpfc_wcqe_c_status, wcqe);
12622 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12623 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12624 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12625 pIocbIn->iocb.un.fcpi.fcpi_parm =
12626 pIocbOut->iocb.un.fcpi.fcpi_parm -
12627 wcqe->total_data_placed;
12628 else
12629 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12630 else {
4f774513 12631 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12632 switch (pIocbOut->iocb.ulpCommand) {
12633 case CMD_ELS_REQUEST64_CR:
12634 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12635 bpl = (struct ulp_bde64 *)dmabuf->virt;
12636 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12637 max_response = bde.tus.f.bdeSize;
12638 break;
12639 case CMD_GEN_REQUEST64_CR:
12640 max_response = 0;
12641 if (!pIocbOut->context3)
12642 break;
12643 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12644 sizeof(struct ulp_bde64);
12645 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12646 bpl = (struct ulp_bde64 *)dmabuf->virt;
12647 for (i = 0; i < numBdes; i++) {
12648 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12649 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12650 max_response += bde.tus.f.bdeSize;
12651 }
12652 break;
12653 default:
12654 max_response = wcqe->total_data_placed;
12655 break;
12656 }
12657 if (max_response < wcqe->total_data_placed)
12658 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12659 else
12660 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12661 wcqe->total_data_placed;
695a814e 12662 }
341af102 12663
acd6859b
JS
12664 /* Convert BG errors for completion status */
12665 if (status == CQE_STATUS_DI_ERROR) {
12666 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12667
12668 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12669 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12670 else
12671 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12672
12673 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12674 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12675 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12676 BGS_GUARD_ERR_MASK;
12677 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12678 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12679 BGS_APPTAG_ERR_MASK;
12680 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12681 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12682 BGS_REFTAG_ERR_MASK;
12683
12684 /* Check to see if there was any good data before the error */
12685 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12686 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12687 BGS_HI_WATER_MARK_PRESENT_MASK;
12688 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12689 wcqe->total_data_placed;
12690 }
12691
12692 /*
12693 * Set ALL the error bits to indicate we don't know what
12694 * type of error it is.
12695 */
12696 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12697 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12698 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12699 BGS_GUARD_ERR_MASK);
12700 }
12701
341af102
JS
12702 /* Pick up HBA exchange busy condition */
12703 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12704 spin_lock_irqsave(&phba->hbalock, iflags);
12705 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12706 spin_unlock_irqrestore(&phba->hbalock, iflags);
12707 }
4f774513
JS
12708}
12709
45ed1190
JS
12710/**
12711 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12712 * @phba: Pointer to HBA context object.
12713 * @wcqe: Pointer to work-queue completion queue entry.
12714 *
12715 * This routine handles an ELS work-queue completion event and construct
12716 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12717 * discovery engine to handle.
12718 *
12719 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12720 **/
12721static struct lpfc_iocbq *
12722lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12723 struct lpfc_iocbq *irspiocbq)
12724{
895427bd 12725 struct lpfc_sli_ring *pring;
45ed1190
JS
12726 struct lpfc_iocbq *cmdiocbq;
12727 struct lpfc_wcqe_complete *wcqe;
12728 unsigned long iflags;
12729
895427bd 12730 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12731 if (unlikely(!pring))
12732 return NULL;
895427bd 12733
45ed1190 12734 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12735 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12736 pring->stats.iocb_event++;
12737 /* Look up the ELS command IOCB and create pseudo response IOCB */
12738 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12739 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12740 if (unlikely(!cmdiocbq)) {
401bb416 12741 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12742 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12743 "0386 ELS complete with no corresponding "
401bb416
DK
12744 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12745 wcqe->word0, wcqe->total_data_placed,
12746 wcqe->parameter, wcqe->word3);
45ed1190
JS
12747 lpfc_sli_release_iocbq(phba, irspiocbq);
12748 return NULL;
12749 }
12750
401bb416
DK
12751 /* Put the iocb back on the txcmplq */
12752 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12753 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12754
45ed1190 12755 /* Fake the irspiocbq and copy necessary response information */
341af102 12756 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12757
12758 return irspiocbq;
12759}
12760
8a5ca109
JS
12761inline struct lpfc_cq_event *
12762lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12763{
12764 struct lpfc_cq_event *cq_event;
12765
12766 /* Allocate a new internal CQ_EVENT entry */
12767 cq_event = lpfc_sli4_cq_event_alloc(phba);
12768 if (!cq_event) {
12769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12770 "0602 Failed to alloc CQ_EVENT entry\n");
12771 return NULL;
12772 }
12773
12774 /* Move the CQE into the event */
12775 memcpy(&cq_event->cqe, entry, size);
12776 return cq_event;
12777}
12778
04c68496
JS
12779/**
12780 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12781 * @phba: Pointer to HBA context object.
12782 * @cqe: Pointer to mailbox completion queue entry.
12783 *
12784 * This routine process a mailbox completion queue entry with asynchrous
12785 * event.
12786 *
12787 * Return: true if work posted to worker thread, otherwise false.
12788 **/
12789static bool
12790lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12791{
12792 struct lpfc_cq_event *cq_event;
12793 unsigned long iflags;
12794
12795 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12796 "0392 Async Event: word0:x%x, word1:x%x, "
12797 "word2:x%x, word3:x%x\n", mcqe->word0,
12798 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12799
8a5ca109
JS
12800 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12801 if (!cq_event)
04c68496 12802 return false;
04c68496
JS
12803 spin_lock_irqsave(&phba->hbalock, iflags);
12804 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12805 /* Set the async event flag */
12806 phba->hba_flag |= ASYNC_EVENT;
12807 spin_unlock_irqrestore(&phba->hbalock, iflags);
12808
12809 return true;
12810}
12811
12812/**
12813 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12814 * @phba: Pointer to HBA context object.
12815 * @cqe: Pointer to mailbox completion queue entry.
12816 *
12817 * This routine process a mailbox completion queue entry with mailbox
12818 * completion event.
12819 *
12820 * Return: true if work posted to worker thread, otherwise false.
12821 **/
12822static bool
12823lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12824{
12825 uint32_t mcqe_status;
12826 MAILBOX_t *mbox, *pmbox;
12827 struct lpfc_mqe *mqe;
12828 struct lpfc_vport *vport;
12829 struct lpfc_nodelist *ndlp;
12830 struct lpfc_dmabuf *mp;
12831 unsigned long iflags;
12832 LPFC_MBOXQ_t *pmb;
12833 bool workposted = false;
12834 int rc;
12835
12836 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12837 if (!bf_get(lpfc_trailer_completed, mcqe))
12838 goto out_no_mqe_complete;
12839
12840 /* Get the reference to the active mbox command */
12841 spin_lock_irqsave(&phba->hbalock, iflags);
12842 pmb = phba->sli.mbox_active;
12843 if (unlikely(!pmb)) {
12844 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12845 "1832 No pending MBOX command to handle\n");
12846 spin_unlock_irqrestore(&phba->hbalock, iflags);
12847 goto out_no_mqe_complete;
12848 }
12849 spin_unlock_irqrestore(&phba->hbalock, iflags);
12850 mqe = &pmb->u.mqe;
12851 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12852 mbox = phba->mbox;
12853 vport = pmb->vport;
12854
12855 /* Reset heartbeat timer */
12856 phba->last_completion_time = jiffies;
12857 del_timer(&phba->sli.mbox_tmo);
12858
12859 /* Move mbox data to caller's mailbox region, do endian swapping */
12860 if (pmb->mbox_cmpl && mbox)
48f8fdb4 12861 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12862
73d91e50
JS
12863 /*
12864 * For mcqe errors, conditionally move a modified error code to
12865 * the mbox so that the error will not be missed.
12866 */
12867 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12868 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12869 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12870 bf_set(lpfc_mqe_status, mqe,
12871 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12872 }
04c68496
JS
12873 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12874 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12875 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12876 "MBOX dflt rpi: status:x%x rpi:x%x",
12877 mcqe_status,
12878 pmbox->un.varWords[0], 0);
12879 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12880 mp = (struct lpfc_dmabuf *)(pmb->context1);
12881 ndlp = (struct lpfc_nodelist *)pmb->context2;
12882 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12883 * RID of the PPI using the same mbox buffer.
12884 */
12885 lpfc_unreg_login(phba, vport->vpi,
12886 pmbox->un.varWords[0], pmb);
12887 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12888 pmb->context1 = mp;
12889 pmb->context2 = ndlp;
12890 pmb->vport = vport;
12891 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12892 if (rc != MBX_BUSY)
12893 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12894 LOG_SLI, "0385 rc should "
12895 "have been MBX_BUSY\n");
12896 if (rc != MBX_NOT_FINISHED)
12897 goto send_current_mbox;
12898 }
12899 }
12900 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12901 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12902 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12903
12904 /* There is mailbox completion work to do */
12905 spin_lock_irqsave(&phba->hbalock, iflags);
12906 __lpfc_mbox_cmpl_put(phba, pmb);
12907 phba->work_ha |= HA_MBATT;
12908 spin_unlock_irqrestore(&phba->hbalock, iflags);
12909 workposted = true;
12910
12911send_current_mbox:
12912 spin_lock_irqsave(&phba->hbalock, iflags);
12913 /* Release the mailbox command posting token */
12914 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12915 /* Setting active mailbox pointer need to be in sync to flag clear */
12916 phba->sli.mbox_active = NULL;
12917 spin_unlock_irqrestore(&phba->hbalock, iflags);
12918 /* Wake up worker thread to post the next pending mailbox command */
12919 lpfc_worker_wake_up(phba);
12920out_no_mqe_complete:
12921 if (bf_get(lpfc_trailer_consumed, mcqe))
12922 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12923 return workposted;
12924}
12925
12926/**
12927 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12928 * @phba: Pointer to HBA context object.
12929 * @cqe: Pointer to mailbox completion queue entry.
12930 *
12931 * This routine process a mailbox completion queue entry, it invokes the
12932 * proper mailbox complete handling or asynchrous event handling routine
12933 * according to the MCQE's async bit.
12934 *
12935 * Return: true if work posted to worker thread, otherwise false.
12936 **/
12937static bool
12938lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12939{
12940 struct lpfc_mcqe mcqe;
12941 bool workposted;
12942
12943 /* Copy the mailbox MCQE and convert endian order as needed */
48f8fdb4 12944 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
04c68496
JS
12945
12946 /* Invoke the proper event handling routine */
12947 if (!bf_get(lpfc_trailer_async, &mcqe))
12948 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12949 else
12950 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12951 return workposted;
12952}
12953
4f774513
JS
12954/**
12955 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12956 * @phba: Pointer to HBA context object.
2a76a283 12957 * @cq: Pointer to associated CQ
4f774513
JS
12958 * @wcqe: Pointer to work-queue completion queue entry.
12959 *
12960 * This routine handles an ELS work-queue completion event.
12961 *
12962 * Return: true if work posted to worker thread, otherwise false.
12963 **/
12964static bool
2a76a283 12965lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12966 struct lpfc_wcqe_complete *wcqe)
12967{
4f774513
JS
12968 struct lpfc_iocbq *irspiocbq;
12969 unsigned long iflags;
2a76a283 12970 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12971 int txq_cnt = 0;
12972 int txcmplq_cnt = 0;
12973 int fcp_txcmplq_cnt = 0;
4f774513 12974
45ed1190 12975 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12976 irspiocbq = lpfc_sli_get_iocbq(phba);
12977 if (!irspiocbq) {
0e9bb8d7
JS
12978 if (!list_empty(&pring->txq))
12979 txq_cnt++;
12980 if (!list_empty(&pring->txcmplq))
12981 txcmplq_cnt++;
4f774513 12982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12983 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12984 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12985 txq_cnt, phba->iocb_cnt,
12986 fcp_txcmplq_cnt,
12987 txcmplq_cnt);
45ed1190 12988 return false;
4f774513 12989 }
4f774513 12990
45ed1190
JS
12991 /* Save off the slow-path queue event for work thread to process */
12992 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12993 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12994 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12995 &phba->sli4_hba.sp_queue_event);
12996 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12997 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12998
45ed1190 12999 return true;
4f774513
JS
13000}
13001
13002/**
13003 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13004 * @phba: Pointer to HBA context object.
13005 * @wcqe: Pointer to work-queue completion queue entry.
13006 *
3f8b6fb7 13007 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
13008 * proper WQ release routine to the slow-path WQ.
13009 **/
13010static void
13011lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13012 struct lpfc_wcqe_release *wcqe)
13013{
2e90f4b5
JS
13014 /* sanity check on queue memory */
13015 if (unlikely(!phba->sli4_hba.els_wq))
13016 return;
4f774513
JS
13017 /* Check for the slow-path ELS work queue */
13018 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13019 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13020 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13021 else
13022 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13023 "2579 Slow-path wqe consume event carries "
13024 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13025 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13026 phba->sli4_hba.els_wq->queue_id);
13027}
13028
13029/**
13030 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13031 * @phba: Pointer to HBA context object.
13032 * @cq: Pointer to a WQ completion queue.
13033 * @wcqe: Pointer to work-queue completion queue entry.
13034 *
13035 * This routine handles an XRI abort event.
13036 *
13037 * Return: true if work posted to worker thread, otherwise false.
13038 **/
13039static bool
13040lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13041 struct lpfc_queue *cq,
13042 struct sli4_wcqe_xri_aborted *wcqe)
13043{
13044 bool workposted = false;
13045 struct lpfc_cq_event *cq_event;
13046 unsigned long iflags;
13047
4f774513
JS
13048 switch (cq->subtype) {
13049 case LPFC_FCP:
8a5ca109
JS
13050 cq_event = lpfc_cq_event_setup(
13051 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13052 if (!cq_event)
13053 return false;
4f774513
JS
13054 spin_lock_irqsave(&phba->hbalock, iflags);
13055 list_add_tail(&cq_event->list,
13056 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13057 /* Set the fcp xri abort event flag */
13058 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13059 spin_unlock_irqrestore(&phba->hbalock, iflags);
13060 workposted = true;
13061 break;
422c4cb7 13062 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 13063 case LPFC_ELS:
8a5ca109
JS
13064 cq_event = lpfc_cq_event_setup(
13065 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13066 if (!cq_event)
13067 return false;
4f774513
JS
13068 spin_lock_irqsave(&phba->hbalock, iflags);
13069 list_add_tail(&cq_event->list,
13070 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13071 /* Set the els xri abort event flag */
13072 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13073 spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 workposted = true;
13075 break;
318083ad 13076 case LPFC_NVME:
8a5ca109
JS
13077 /* Notify aborted XRI for NVME work queue */
13078 if (phba->nvmet_support)
13079 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13080 else
13081 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13082
13083 workposted = false;
318083ad 13084 break;
4f774513
JS
13085 default:
13086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
13087 "0603 Invalid CQ subtype %d: "
13088 "%08x %08x %08x %08x\n",
13089 cq->subtype, wcqe->word0, wcqe->parameter,
13090 wcqe->word2, wcqe->word3);
4f774513
JS
13091 workposted = false;
13092 break;
13093 }
13094 return workposted;
13095}
13096
4f774513
JS
13097/**
13098 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13099 * @phba: Pointer to HBA context object.
13100 * @rcqe: Pointer to receive-queue completion queue entry.
13101 *
13102 * This routine process a receive-queue completion queue entry.
13103 *
13104 * Return: true if work posted to worker thread, otherwise false.
13105 **/
13106static bool
4d9ab994 13107lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 13108{
4f774513 13109 bool workposted = false;
895427bd 13110 struct fc_frame_header *fc_hdr;
4f774513
JS
13111 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13112 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 13113 struct lpfc_nvmet_tgtport *tgtp;
4f774513 13114 struct hbq_dmabuf *dma_buf;
7851fe2c 13115 uint32_t status, rq_id;
4f774513
JS
13116 unsigned long iflags;
13117
2e90f4b5
JS
13118 /* sanity check on queue memory */
13119 if (unlikely(!hrq) || unlikely(!drq))
13120 return workposted;
13121
7851fe2c
JS
13122 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13123 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13124 else
13125 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13126 if (rq_id != hrq->queue_id)
4f774513
JS
13127 goto out;
13128
4d9ab994 13129 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
13130 switch (status) {
13131 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13133 "2537 Receive Frame Truncated!!\n");
13134 case FC_STATUS_RQ_SUCCESS:
13135 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13136 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
13137 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13138 if (!dma_buf) {
b84daac9 13139 hrq->RQ_no_buf_found++;
4f774513
JS
13140 spin_unlock_irqrestore(&phba->hbalock, iflags);
13141 goto out;
13142 }
b84daac9 13143 hrq->RQ_rcv_buf++;
547077a4 13144 hrq->RQ_buf_posted--;
4d9ab994 13145 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
13146
13147 /* If a NVME LS event (type 0x28), treat it as Fast path */
13148 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13149
4f774513 13150 /* save off the frame for the word thread to process */
4d9ab994 13151 list_add_tail(&dma_buf->cq_event.list,
45ed1190 13152 &phba->sli4_hba.sp_queue_event);
4f774513 13153 /* Frame received */
45ed1190 13154 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
13155 spin_unlock_irqrestore(&phba->hbalock, iflags);
13156 workposted = true;
13157 break;
4f774513 13158 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13159 if (phba->nvmet_support) {
13160 tgtp = phba->targetport->private;
13161 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13162 "6402 RQE Error x%x, posted %d err_cnt "
13163 "%d: %x %x %x\n",
13164 status, hrq->RQ_buf_posted,
13165 hrq->RQ_no_posted_buf,
13166 atomic_read(&tgtp->rcv_fcp_cmd_in),
13167 atomic_read(&tgtp->rcv_fcp_cmd_out),
13168 atomic_read(&tgtp->xmt_fcp_release));
13169 }
13170 /* fallthrough */
13171
13172 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 13173 hrq->RQ_no_posted_buf++;
4f774513
JS
13174 /* Post more buffers if possible */
13175 spin_lock_irqsave(&phba->hbalock, iflags);
13176 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13177 spin_unlock_irqrestore(&phba->hbalock, iflags);
13178 workposted = true;
13179 break;
13180 }
13181out:
13182 return workposted;
4f774513
JS
13183}
13184
4d9ab994
JS
13185/**
13186 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13187 * @phba: Pointer to HBA context object.
13188 * @cq: Pointer to the completion queue.
13189 * @wcqe: Pointer to a completion queue entry.
13190 *
25985edc 13191 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
13192 * entry.
13193 *
13194 * Return: true if work posted to worker thread, otherwise false.
13195 **/
13196static bool
13197lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13198 struct lpfc_cqe *cqe)
13199{
45ed1190 13200 struct lpfc_cqe cqevt;
4d9ab994
JS
13201 bool workposted = false;
13202
13203 /* Copy the work queue CQE and convert endian order if needed */
48f8fdb4 13204 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
13205
13206 /* Check and process for different type of WCQE and dispatch */
45ed1190 13207 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 13208 case CQE_CODE_COMPL_WQE:
45ed1190 13209 /* Process the WQ/RQ complete event */
bc73905a 13210 phba->last_completion_time = jiffies;
2a76a283 13211 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 13212 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
13213 break;
13214 case CQE_CODE_RELEASE_WQE:
13215 /* Process the WQ release event */
13216 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 13217 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
13218 break;
13219 case CQE_CODE_XRI_ABORTED:
13220 /* Process the WQ XRI abort event */
bc73905a 13221 phba->last_completion_time = jiffies;
4d9ab994 13222 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 13223 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
13224 break;
13225 case CQE_CODE_RECEIVE:
7851fe2c 13226 case CQE_CODE_RECEIVE_V1:
4d9ab994 13227 /* Process the RQ event */
bc73905a 13228 phba->last_completion_time = jiffies;
4d9ab994 13229 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 13230 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
13231 break;
13232 default:
13233 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13234 "0388 Not a valid WCQE code: x%x\n",
45ed1190 13235 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
13236 break;
13237 }
13238 return workposted;
13239}
13240
4f774513
JS
13241/**
13242 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13243 * @phba: Pointer to HBA context object.
13244 * @eqe: Pointer to fast-path event queue entry.
13245 *
13246 * This routine process a event queue entry from the slow-path event queue.
13247 * It will check the MajorCode and MinorCode to determine this is for a
13248 * completion event on a completion queue, if not, an error shall be logged
13249 * and just return. Otherwise, it will get to the corresponding completion
13250 * queue and process all the entries on that completion queue, rearm the
13251 * completion queue, and then return.
13252 *
13253 **/
f485c18d 13254static void
67d12733
JS
13255lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13256 struct lpfc_queue *speq)
4f774513 13257{
67d12733 13258 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13259 uint16_t cqid;
13260
4f774513 13261 /* Get the reference to the corresponding CQ */
cb5172ea 13262 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13263
4f774513
JS
13264 list_for_each_entry(childq, &speq->child_list, list) {
13265 if (childq->queue_id == cqid) {
13266 cq = childq;
13267 break;
13268 }
13269 }
13270 if (unlikely(!cq)) {
75baf696
JS
13271 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13273 "0365 Slow-path CQ identifier "
13274 "(%d) does not exist\n", cqid);
f485c18d 13275 return;
4f774513
JS
13276 }
13277
895427bd
JS
13278 /* Save EQ associated with this CQ */
13279 cq->assoc_qp = speq;
13280
f485c18d
DK
13281 if (!queue_work(phba->wq, &cq->spwork))
13282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13283 "0390 Cannot schedule soft IRQ "
13284 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13285 cqid, cq->queue_id, smp_processor_id());
13286}
13287
13288/**
13289 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13290 * @phba: Pointer to HBA context object.
13291 *
13292 * This routine process a event queue entry from the slow-path event queue.
13293 * It will check the MajorCode and MinorCode to determine this is for a
13294 * completion event on a completion queue, if not, an error shall be logged
13295 * and just return. Otherwise, it will get to the corresponding completion
13296 * queue and process all the entries on that completion queue, rearm the
13297 * completion queue, and then return.
13298 *
13299 **/
13300static void
13301lpfc_sli4_sp_process_cq(struct work_struct *work)
13302{
13303 struct lpfc_queue *cq =
13304 container_of(work, struct lpfc_queue, spwork);
13305 struct lpfc_hba *phba = cq->phba;
13306 struct lpfc_cqe *cqe;
13307 bool workposted = false;
13308 int ccount = 0;
13309
4f774513
JS
13310 /* Process all the entries to the CQ */
13311 switch (cq->type) {
13312 case LPFC_MCQ:
13313 while ((cqe = lpfc_sli4_cq_get(cq))) {
13314 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13315 if (!(++ccount % cq->entry_repost))
7869da18 13316 break;
b84daac9 13317 cq->CQ_mbox++;
4f774513
JS
13318 }
13319 break;
13320 case LPFC_WCQ:
13321 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13322 if (cq->subtype == LPFC_FCP ||
13323 cq->subtype == LPFC_NVME) {
13324#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13325 if (phba->ktime_on)
13326 cq->isr_timestamp = ktime_get_ns();
13327 else
13328 cq->isr_timestamp = 0;
13329#endif
895427bd 13330 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13331 cqe);
c8a4ce0b 13332 } else {
0558056c
JS
13333 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13334 cqe);
c8a4ce0b 13335 }
f485c18d 13336 if (!(++ccount % cq->entry_repost))
7869da18 13337 break;
4f774513 13338 }
b84daac9
JS
13339
13340 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13341 if (ccount > cq->CQ_max_cqe)
13342 cq->CQ_max_cqe = ccount;
4f774513
JS
13343 break;
13344 default:
13345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13346 "0370 Invalid completion queue type (%d)\n",
13347 cq->type);
f485c18d 13348 return;
4f774513
JS
13349 }
13350
13351 /* Catch the no cq entry condition, log an error */
f485c18d 13352 if (unlikely(ccount == 0))
4f774513
JS
13353 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13354 "0371 No entry from the CQ: identifier "
13355 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13356
13357 /* In any case, flash and re-arm the RCQ */
b71413dd 13358 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13359
13360 /* wake up worker thread if there are works to be done */
13361 if (workposted)
13362 lpfc_worker_wake_up(phba);
13363}
13364
13365/**
13366 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13367 * @phba: Pointer to HBA context object.
13368 * @cq: Pointer to associated CQ
13369 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13370 *
13371 * This routine process a fast-path work queue completion entry from fast-path
13372 * event queue for FCP command response completion.
13373 **/
13374static void
2a76a283 13375lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13376 struct lpfc_wcqe_complete *wcqe)
13377{
2a76a283 13378 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13379 struct lpfc_iocbq *cmdiocbq;
13380 struct lpfc_iocbq irspiocbq;
13381 unsigned long iflags;
13382
4f774513
JS
13383 /* Check for response status */
13384 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13385 /* If resource errors reported from HBA, reduce queue
13386 * depth of the SCSI device.
13387 */
e3d2b802
JS
13388 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13389 IOSTAT_LOCAL_REJECT)) &&
13390 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13391 IOERR_NO_RESOURCES))
4f774513 13392 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13393
4f774513
JS
13394 /* Log the error status */
13395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13396 "0373 FCP complete error: status=x%x, "
13397 "hw_status=x%x, total_data_specified=%d, "
13398 "parameter=x%x, word3=x%x\n",
13399 bf_get(lpfc_wcqe_c_status, wcqe),
13400 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13401 wcqe->total_data_placed, wcqe->parameter,
13402 wcqe->word3);
13403 }
13404
13405 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13406 spin_lock_irqsave(&pring->ring_lock, iflags);
13407 pring->stats.iocb_event++;
4f774513
JS
13408 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13409 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13410 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13411 if (unlikely(!cmdiocbq)) {
13412 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13413 "0374 FCP complete with no corresponding "
13414 "cmdiocb: iotag (%d)\n",
13415 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13416 return;
13417 }
c8a4ce0b
DK
13418#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13419 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13420#endif
895427bd
JS
13421 if (cmdiocbq->iocb_cmpl == NULL) {
13422 if (cmdiocbq->wqe_cmpl) {
13423 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13424 spin_lock_irqsave(&phba->hbalock, iflags);
13425 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13426 spin_unlock_irqrestore(&phba->hbalock, iflags);
13427 }
13428
13429 /* Pass the cmd_iocb and the wcqe to the upper layer */
13430 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13431 return;
13432 }
4f774513
JS
13433 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13434 "0375 FCP cmdiocb not callback function "
13435 "iotag: (%d)\n",
13436 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13437 return;
13438 }
13439
13440 /* Fake the irspiocb and copy necessary response information */
341af102 13441 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13442
0f65ff68
JS
13443 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13444 spin_lock_irqsave(&phba->hbalock, iflags);
13445 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13446 spin_unlock_irqrestore(&phba->hbalock, iflags);
13447 }
13448
4f774513
JS
13449 /* Pass the cmd_iocb and the rsp state to the upper layer */
13450 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13451}
13452
13453/**
13454 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13455 * @phba: Pointer to HBA context object.
13456 * @cq: Pointer to completion queue.
13457 * @wcqe: Pointer to work-queue completion queue entry.
13458 *
3f8b6fb7 13459 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13460 * proper WQ release routine to the slow-path WQ.
13461 **/
13462static void
13463lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13464 struct lpfc_wcqe_release *wcqe)
13465{
13466 struct lpfc_queue *childwq;
13467 bool wqid_matched = false;
895427bd 13468 uint16_t hba_wqid;
4f774513
JS
13469
13470 /* Check for fast-path FCP work queue release */
895427bd 13471 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13472 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13473 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13474 lpfc_sli4_wq_release(childwq,
13475 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
6e8e1c14
JS
13476 if (childwq->q_flag & HBA_NVMET_WQFULL)
13477 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13478 wqid_matched = true;
13479 break;
13480 }
13481 }
13482 /* Report warning log message if no match found */
13483 if (wqid_matched != true)
13484 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13485 "2580 Fast-path wqe consume event carries "
895427bd 13486 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13487}
13488
13489/**
2d7dbc4c
JS
13490 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13491 * @phba: Pointer to HBA context object.
13492 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13493 *
2d7dbc4c
JS
13494 * This routine process a receive-queue completion queue entry.
13495 *
13496 * Return: true if work posted to worker thread, otherwise false.
13497 **/
13498static bool
13499lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13500 struct lpfc_rcqe *rcqe)
13501{
13502 bool workposted = false;
13503 struct lpfc_queue *hrq;
13504 struct lpfc_queue *drq;
13505 struct rqb_dmabuf *dma_buf;
13506 struct fc_frame_header *fc_hdr;
547077a4 13507 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13508 uint32_t status, rq_id;
13509 unsigned long iflags;
13510 uint32_t fctl, idx;
13511
13512 if ((phba->nvmet_support == 0) ||
13513 (phba->sli4_hba.nvmet_cqset == NULL))
13514 return workposted;
13515
13516 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13517 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13518 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13519
13520 /* sanity check on queue memory */
13521 if (unlikely(!hrq) || unlikely(!drq))
13522 return workposted;
13523
13524 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13525 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13526 else
13527 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13528
13529 if ((phba->nvmet_support == 0) ||
13530 (rq_id != hrq->queue_id))
13531 return workposted;
13532
13533 status = bf_get(lpfc_rcqe_status, rcqe);
13534 switch (status) {
13535 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13537 "6126 Receive Frame Truncated!!\n");
78e1d200 13538 /* Drop thru */
2d7dbc4c 13539 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13540 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13541 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13542 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13543 if (!dma_buf) {
13544 hrq->RQ_no_buf_found++;
13545 spin_unlock_irqrestore(&phba->hbalock, iflags);
13546 goto out;
13547 }
13548 spin_unlock_irqrestore(&phba->hbalock, iflags);
13549 hrq->RQ_rcv_buf++;
547077a4 13550 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13551 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13552
13553 /* Just some basic sanity checks on FCP Command frame */
13554 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13555 fc_hdr->fh_f_ctl[1] << 8 |
13556 fc_hdr->fh_f_ctl[2]);
13557 if (((fctl &
13558 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13559 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13560 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13561 goto drop;
13562
13563 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13564 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13565 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13566 phba, idx, dma_buf,
c8a4ce0b 13567 cq->isr_timestamp);
2d7dbc4c
JS
13568 return false;
13569 }
13570drop:
13571 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13572 break;
2d7dbc4c 13573 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13574 if (phba->nvmet_support) {
13575 tgtp = phba->targetport->private;
13576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13577 "6401 RQE Error x%x, posted %d err_cnt "
13578 "%d: %x %x %x\n",
13579 status, hrq->RQ_buf_posted,
13580 hrq->RQ_no_posted_buf,
13581 atomic_read(&tgtp->rcv_fcp_cmd_in),
13582 atomic_read(&tgtp->rcv_fcp_cmd_out),
13583 atomic_read(&tgtp->xmt_fcp_release));
13584 }
13585 /* fallthrough */
13586
13587 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13588 hrq->RQ_no_posted_buf++;
13589 /* Post more buffers if possible */
2d7dbc4c
JS
13590 break;
13591 }
13592out:
13593 return workposted;
13594}
13595
4f774513 13596/**
895427bd 13597 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13598 * @cq: Pointer to the completion queue.
13599 * @eqe: Pointer to fast-path completion queue entry.
13600 *
13601 * This routine process a fast-path work queue completion entry from fast-path
13602 * event queue for FCP command response completion.
13603 **/
13604static int
895427bd 13605lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13606 struct lpfc_cqe *cqe)
13607{
13608 struct lpfc_wcqe_release wcqe;
13609 bool workposted = false;
13610
13611 /* Copy the work queue CQE and convert endian order if needed */
48f8fdb4 13612 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
4f774513
JS
13613
13614 /* Check and process for different type of WCQE and dispatch */
13615 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13616 case CQE_CODE_COMPL_WQE:
895427bd 13617 case CQE_CODE_NVME_ERSP:
b84daac9 13618 cq->CQ_wq++;
4f774513 13619 /* Process the WQ complete event */
98fc5dd9 13620 phba->last_completion_time = jiffies;
895427bd
JS
13621 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13622 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13623 (struct lpfc_wcqe_complete *)&wcqe);
13624 if (cq->subtype == LPFC_NVME_LS)
13625 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13626 (struct lpfc_wcqe_complete *)&wcqe);
13627 break;
13628 case CQE_CODE_RELEASE_WQE:
b84daac9 13629 cq->CQ_release_wqe++;
4f774513
JS
13630 /* Process the WQ release event */
13631 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13632 (struct lpfc_wcqe_release *)&wcqe);
13633 break;
13634 case CQE_CODE_XRI_ABORTED:
b84daac9 13635 cq->CQ_xri_aborted++;
4f774513 13636 /* Process the WQ XRI abort event */
bc73905a 13637 phba->last_completion_time = jiffies;
4f774513
JS
13638 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13639 (struct sli4_wcqe_xri_aborted *)&wcqe);
13640 break;
895427bd
JS
13641 case CQE_CODE_RECEIVE_V1:
13642 case CQE_CODE_RECEIVE:
13643 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13644 if (cq->subtype == LPFC_NVMET) {
13645 workposted = lpfc_sli4_nvmet_handle_rcqe(
13646 phba, cq, (struct lpfc_rcqe *)&wcqe);
13647 }
895427bd 13648 break;
4f774513
JS
13649 default:
13650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13651 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13652 bf_get(lpfc_wcqe_c_code, &wcqe));
13653 break;
13654 }
13655 return workposted;
13656}
13657
13658/**
67d12733 13659 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13660 * @phba: Pointer to HBA context object.
13661 * @eqe: Pointer to fast-path event queue entry.
13662 *
13663 * This routine process a event queue entry from the fast-path event queue.
13664 * It will check the MajorCode and MinorCode to determine this is for a
13665 * completion event on a completion queue, if not, an error shall be logged
13666 * and just return. Otherwise, it will get to the corresponding completion
13667 * queue and process all the entries on the completion queue, rearm the
13668 * completion queue, and then return.
13669 **/
f485c18d 13670static void
67d12733
JS
13671lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13672 uint32_t qidx)
4f774513 13673{
895427bd 13674 struct lpfc_queue *cq = NULL;
2d7dbc4c 13675 uint16_t cqid, id;
4f774513 13676
cb5172ea 13677 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13678 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13679 "0366 Not a valid completion "
4f774513 13680 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13681 bf_get_le32(lpfc_eqe_major_code, eqe),
13682 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13683 return;
4f774513
JS
13684 }
13685
67d12733
JS
13686 /* Get the reference to the corresponding CQ */
13687 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13688
2d7dbc4c
JS
13689 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13690 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13691 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13692 /* Process NVMET unsol rcv */
13693 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13694 goto process_cq;
13695 }
67d12733
JS
13696 }
13697
895427bd
JS
13698 if (phba->sli4_hba.nvme_cq_map &&
13699 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13700 /* Process NVME / NVMET command completion */
895427bd
JS
13701 cq = phba->sli4_hba.nvme_cq[qidx];
13702 goto process_cq;
2e90f4b5 13703 }
67d12733 13704
895427bd
JS
13705 if (phba->sli4_hba.fcp_cq_map &&
13706 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13707 /* Process FCP command completion */
13708 cq = phba->sli4_hba.fcp_cq[qidx];
13709 goto process_cq;
2e90f4b5 13710 }
895427bd
JS
13711
13712 if (phba->sli4_hba.nvmels_cq &&
13713 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13714 /* Process NVME unsol rcv */
13715 cq = phba->sli4_hba.nvmels_cq;
13716 }
13717
13718 /* Otherwise this is a Slow path event */
13719 if (cq == NULL) {
f485c18d
DK
13720 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13721 return;
4f774513
JS
13722 }
13723
895427bd 13724process_cq:
4f774513
JS
13725 if (unlikely(cqid != cq->queue_id)) {
13726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13727 "0368 Miss-matched fast-path completion "
13728 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13729 cqid, cq->queue_id);
f485c18d 13730 return;
4f774513
JS
13731 }
13732
895427bd
JS
13733 /* Save EQ associated with this CQ */
13734 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13735
f485c18d
DK
13736 if (!queue_work(phba->wq, &cq->irqwork))
13737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13738 "0363 Cannot schedule soft IRQ "
13739 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13740 cqid, cq->queue_id, smp_processor_id());
13741}
13742
13743/**
13744 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13745 * @phba: Pointer to HBA context object.
13746 * @eqe: Pointer to fast-path event queue entry.
13747 *
13748 * This routine process a event queue entry from the fast-path event queue.
13749 * It will check the MajorCode and MinorCode to determine this is for a
13750 * completion event on a completion queue, if not, an error shall be logged
13751 * and just return. Otherwise, it will get to the corresponding completion
13752 * queue and process all the entries on the completion queue, rearm the
13753 * completion queue, and then return.
13754 **/
13755static void
13756lpfc_sli4_hba_process_cq(struct work_struct *work)
13757{
13758 struct lpfc_queue *cq =
13759 container_of(work, struct lpfc_queue, irqwork);
13760 struct lpfc_hba *phba = cq->phba;
13761 struct lpfc_cqe *cqe;
13762 bool workposted = false;
13763 int ccount = 0;
13764
4f774513
JS
13765 /* Process all the entries to the CQ */
13766 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13767#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13768 if (phba->ktime_on)
13769 cq->isr_timestamp = ktime_get_ns();
13770 else
13771 cq->isr_timestamp = 0;
13772#endif
895427bd 13773 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13774 if (!(++ccount % cq->entry_repost))
7869da18 13775 break;
4f774513
JS
13776 }
13777
b84daac9 13778 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13779 if (ccount > cq->CQ_max_cqe)
13780 cq->CQ_max_cqe = ccount;
13781 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13782
4f774513 13783 /* Catch the no cq entry condition */
f485c18d 13784 if (unlikely(ccount == 0))
4f774513
JS
13785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13786 "0369 No entry from fast-path completion "
13787 "queue fcpcqid=%d\n", cq->queue_id);
13788
13789 /* In any case, flash and re-arm the CQ */
b71413dd 13790 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13791
13792 /* wake up worker thread if there are works to be done */
13793 if (workposted)
13794 lpfc_worker_wake_up(phba);
13795}
13796
13797static void
13798lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13799{
13800 struct lpfc_eqe *eqe;
13801
13802 /* walk all the EQ entries and drop on the floor */
13803 while ((eqe = lpfc_sli4_eq_get(eq)))
13804 ;
13805
13806 /* Clear and re-arm the EQ */
b71413dd 13807 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
4f774513
JS
13808}
13809
1ba981fd
JS
13810
13811/**
13812 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13813 * entry
13814 * @phba: Pointer to HBA context object.
13815 * @eqe: Pointer to fast-path event queue entry.
13816 *
13817 * This routine process a event queue entry from the Flash Optimized Fabric
13818 * event queue. It will check the MajorCode and MinorCode to determine this
13819 * is for a completion event on a completion queue, if not, an error shall be
13820 * logged and just return. Otherwise, it will get to the corresponding
13821 * completion queue and process all the entries on the completion queue, rearm
13822 * the completion queue, and then return.
13823 **/
13824static void
13825lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13826{
13827 struct lpfc_queue *cq;
1ba981fd 13828 uint16_t cqid;
1ba981fd
JS
13829
13830 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13831 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13832 "9147 Not a valid completion "
13833 "event: majorcode=x%x, minorcode=x%x\n",
13834 bf_get_le32(lpfc_eqe_major_code, eqe),
13835 bf_get_le32(lpfc_eqe_minor_code, eqe));
13836 return;
13837 }
13838
13839 /* Get the reference to the corresponding CQ */
13840 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13841
13842 /* Next check for OAS */
13843 cq = phba->sli4_hba.oas_cq;
13844 if (unlikely(!cq)) {
13845 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13846 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13847 "9148 OAS completion queue "
13848 "does not exist\n");
13849 return;
13850 }
13851
13852 if (unlikely(cqid != cq->queue_id)) {
13853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13854 "9149 Miss-matched fast-path compl "
13855 "queue id: eqcqid=%d, fcpcqid=%d\n",
13856 cqid, cq->queue_id);
13857 return;
13858 }
13859
d41b65bc
JS
13860 /* Save EQ associated with this CQ */
13861 cq->assoc_qp = phba->sli4_hba.fof_eq;
13862
f485c18d
DK
13863 /* CQ work will be processed on CPU affinitized to this IRQ */
13864 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13865 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13866 "0367 Cannot schedule soft IRQ "
13867 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13868 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13869}
13870
13871/**
13872 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13873 * @irq: Interrupt number.
13874 * @dev_id: The device context pointer.
13875 *
13876 * This function is directly called from the PCI layer as an interrupt
13877 * service routine when device with SLI-4 interface spec is enabled with
13878 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13879 * IOCB ring event in the HBA. However, when the device is enabled with either
13880 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13881 * device-level interrupt handler. When the PCI slot is in error recovery
13882 * or the HBA is undergoing initialization, the interrupt handler will not
13883 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13884 * the intrrupt context. This function is called without any lock held.
13885 * It gets the hbalock to access and update SLI data structures. Note that,
13886 * the EQ to CQ are one-to-one map such that the EQ index is
13887 * equal to that of CQ index.
13888 *
13889 * This function returns IRQ_HANDLED when interrupt is handled else it
13890 * returns IRQ_NONE.
13891 **/
13892irqreturn_t
13893lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13894{
13895 struct lpfc_hba *phba;
895427bd 13896 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13897 struct lpfc_queue *eq;
13898 struct lpfc_eqe *eqe;
13899 unsigned long iflag;
13900 int ecount = 0;
1ba981fd
JS
13901
13902 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13903 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13904 phba = hba_eq_hdl->phba;
1ba981fd
JS
13905
13906 if (unlikely(!phba))
13907 return IRQ_NONE;
13908
13909 /* Get to the EQ struct associated with this vector */
13910 eq = phba->sli4_hba.fof_eq;
13911 if (unlikely(!eq))
13912 return IRQ_NONE;
13913
13914 /* Check device state for handling interrupt */
13915 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13916 /* Check again for link_state with lock held */
13917 spin_lock_irqsave(&phba->hbalock, iflag);
13918 if (phba->link_state < LPFC_LINK_DOWN)
13919 /* Flush, clear interrupt, and rearm the EQ */
13920 lpfc_sli4_eq_flush(phba, eq);
13921 spin_unlock_irqrestore(&phba->hbalock, iflag);
13922 return IRQ_NONE;
13923 }
13924
13925 /*
13926 * Process all the event on FCP fast-path EQ
13927 */
13928 while ((eqe = lpfc_sli4_eq_get(eq))) {
13929 lpfc_sli4_fof_handle_eqe(phba, eqe);
13930 if (!(++ecount % eq->entry_repost))
7869da18 13931 break;
1ba981fd
JS
13932 eq->EQ_processed++;
13933 }
13934
13935 /* Track the max number of EQEs processed in 1 intr */
13936 if (ecount > eq->EQ_max_eqe)
13937 eq->EQ_max_eqe = ecount;
13938
13939
13940 if (unlikely(ecount == 0)) {
13941 eq->EQ_no_entry++;
13942
13943 if (phba->intr_type == MSIX)
13944 /* MSI-X treated interrupt served as no EQ share INT */
13945 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13946 "9145 MSI-X interrupt with no EQE\n");
13947 else {
13948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13949 "9146 ISR interrupt with no EQE\n");
13950 /* Non MSI-X treated on interrupt as EQ share INT */
13951 return IRQ_NONE;
13952 }
13953 }
13954 /* Always clear and re-arm the fast-path EQ */
b71413dd 13955 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
1ba981fd
JS
13956 return IRQ_HANDLED;
13957}
13958
4f774513 13959/**
67d12733 13960 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13961 * @irq: Interrupt number.
13962 * @dev_id: The device context pointer.
13963 *
13964 * This function is directly called from the PCI layer as an interrupt
13965 * service routine when device with SLI-4 interface spec is enabled with
13966 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13967 * ring event in the HBA. However, when the device is enabled with either
13968 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13969 * device-level interrupt handler. When the PCI slot is in error recovery
13970 * or the HBA is undergoing initialization, the interrupt handler will not
13971 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13972 * the intrrupt context. This function is called without any lock held.
13973 * It gets the hbalock to access and update SLI data structures. Note that,
13974 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13975 * equal to that of FCP CQ index.
13976 *
67d12733
JS
13977 * The link attention and ELS ring attention events are handled
13978 * by the worker thread. The interrupt handler signals the worker thread
13979 * and returns for these events. This function is called without any lock
13980 * held. It gets the hbalock to access and update SLI data structures.
13981 *
4f774513
JS
13982 * This function returns IRQ_HANDLED when interrupt is handled else it
13983 * returns IRQ_NONE.
13984 **/
13985irqreturn_t
67d12733 13986lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13987{
13988 struct lpfc_hba *phba;
895427bd 13989 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13990 struct lpfc_queue *fpeq;
13991 struct lpfc_eqe *eqe;
13992 unsigned long iflag;
13993 int ecount = 0;
895427bd 13994 int hba_eqidx;
4f774513
JS
13995
13996 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13997 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13998 phba = hba_eq_hdl->phba;
13999 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
14000
14001 if (unlikely(!phba))
14002 return IRQ_NONE;
67d12733 14003 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 14004 return IRQ_NONE;
4f774513
JS
14005
14006 /* Get to the EQ struct associated with this vector */
895427bd 14007 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
14008 if (unlikely(!fpeq))
14009 return IRQ_NONE;
4f774513 14010
ba20c853 14011 if (lpfc_fcp_look_ahead) {
895427bd 14012 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
b71413dd 14013 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 14014 else {
895427bd 14015 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14016 return IRQ_NONE;
14017 }
14018 }
14019
4f774513
JS
14020 /* Check device state for handling interrupt */
14021 if (unlikely(lpfc_intr_state_check(phba))) {
14022 /* Check again for link_state with lock held */
14023 spin_lock_irqsave(&phba->hbalock, iflag);
14024 if (phba->link_state < LPFC_LINK_DOWN)
14025 /* Flush, clear interrupt, and rearm the EQ */
14026 lpfc_sli4_eq_flush(phba, fpeq);
14027 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 14028 if (lpfc_fcp_look_ahead)
895427bd 14029 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
14030 return IRQ_NONE;
14031 }
14032
14033 /*
14034 * Process all the event on FCP fast-path EQ
14035 */
14036 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
14037 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14038 if (!(++ecount % fpeq->entry_repost))
7869da18 14039 break;
b84daac9 14040 fpeq->EQ_processed++;
4f774513
JS
14041 }
14042
b84daac9
JS
14043 /* Track the max number of EQEs processed in 1 intr */
14044 if (ecount > fpeq->EQ_max_eqe)
14045 fpeq->EQ_max_eqe = ecount;
14046
4f774513 14047 /* Always clear and re-arm the fast-path EQ */
b71413dd 14048 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
4f774513
JS
14049
14050 if (unlikely(ecount == 0)) {
b84daac9 14051 fpeq->EQ_no_entry++;
ba20c853
JS
14052
14053 if (lpfc_fcp_look_ahead) {
895427bd 14054 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
14055 return IRQ_NONE;
14056 }
14057
4f774513
JS
14058 if (phba->intr_type == MSIX)
14059 /* MSI-X treated interrupt served as no EQ share INT */
14060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14061 "0358 MSI-X interrupt with no EQE\n");
14062 else
14063 /* Non MSI-X treated on interrupt as EQ share INT */
14064 return IRQ_NONE;
14065 }
14066
ba20c853 14067 if (lpfc_fcp_look_ahead)
895427bd
JS
14068 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14069
4f774513
JS
14070 return IRQ_HANDLED;
14071} /* lpfc_sli4_fp_intr_handler */
14072
14073/**
14074 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14075 * @irq: Interrupt number.
14076 * @dev_id: The device context pointer.
14077 *
14078 * This function is the device-level interrupt handler to device with SLI-4
14079 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14080 * interrupt mode is enabled and there is an event in the HBA which requires
14081 * driver attention. This function invokes the slow-path interrupt attention
14082 * handling function and fast-path interrupt attention handling function in
14083 * turn to process the relevant HBA attention events. This function is called
14084 * without any lock held. It gets the hbalock to access and update SLI data
14085 * structures.
14086 *
14087 * This function returns IRQ_HANDLED when interrupt is handled, else it
14088 * returns IRQ_NONE.
14089 **/
14090irqreturn_t
14091lpfc_sli4_intr_handler(int irq, void *dev_id)
14092{
14093 struct lpfc_hba *phba;
67d12733
JS
14094 irqreturn_t hba_irq_rc;
14095 bool hba_handled = false;
895427bd 14096 int qidx;
4f774513
JS
14097
14098 /* Get the driver's phba structure from the dev_id */
14099 phba = (struct lpfc_hba *)dev_id;
14100
14101 if (unlikely(!phba))
14102 return IRQ_NONE;
14103
4f774513
JS
14104 /*
14105 * Invoke fast-path host attention interrupt handling as appropriate.
14106 */
895427bd 14107 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 14108 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 14109 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
14110 if (hba_irq_rc == IRQ_HANDLED)
14111 hba_handled |= true;
4f774513
JS
14112 }
14113
1ba981fd
JS
14114 if (phba->cfg_fof) {
14115 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 14116 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
14117 if (hba_irq_rc == IRQ_HANDLED)
14118 hba_handled |= true;
14119 }
14120
67d12733 14121 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
14122} /* lpfc_sli4_intr_handler */
14123
14124/**
14125 * lpfc_sli4_queue_free - free a queue structure and associated memory
14126 * @queue: The queue structure to free.
14127 *
b595076a 14128 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
14129 * the host resident queue. This function must be called after destroying the
14130 * queue on the HBA.
14131 **/
14132void
14133lpfc_sli4_queue_free(struct lpfc_queue *queue)
14134{
14135 struct lpfc_dmabuf *dmabuf;
14136
14137 if (!queue)
14138 return;
14139
14140 while (!list_empty(&queue->page_list)) {
14141 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14142 list);
81b96eda 14143 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
14144 dmabuf->virt, dmabuf->phys);
14145 kfree(dmabuf);
14146 }
895427bd
JS
14147 if (queue->rqbp) {
14148 lpfc_free_rq_buffer(queue->phba, queue);
14149 kfree(queue->rqbp);
14150 }
d1f525aa
JS
14151
14152 if (!list_empty(&queue->wq_list))
14153 list_del(&queue->wq_list);
14154
4f774513
JS
14155 kfree(queue);
14156 return;
14157}
14158
14159/**
14160 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14161 * @phba: The HBA that this queue is being created on.
81b96eda 14162 * @page_size: The size of a queue page
4f774513
JS
14163 * @entry_size: The size of each queue entry for this queue.
14164 * @entry count: The number of entries that this queue will handle.
14165 *
14166 * This function allocates a queue structure and the DMAable memory used for
14167 * the host resident queue. This function must be called before creating the
14168 * queue on the HBA.
14169 **/
14170struct lpfc_queue *
81b96eda
JS
14171lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14172 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
14173{
14174 struct lpfc_queue *queue;
14175 struct lpfc_dmabuf *dmabuf;
14176 int x, total_qe_count;
14177 void *dma_pointer;
cb5172ea 14178 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 14179
cb5172ea 14180 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14181 hw_page_size = page_size;
cb5172ea 14182
4f774513
JS
14183 queue = kzalloc(sizeof(struct lpfc_queue) +
14184 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14185 if (!queue)
14186 return NULL;
cb5172ea
JS
14187 queue->page_count = (ALIGN(entry_size * entry_count,
14188 hw_page_size))/hw_page_size;
895427bd
JS
14189
14190 /* If needed, Adjust page count to match the max the adapter supports */
14191 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14192 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14193
4f774513 14194 INIT_LIST_HEAD(&queue->list);
895427bd 14195 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 14196 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
14197 INIT_LIST_HEAD(&queue->page_list);
14198 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
14199
14200 /* Set queue parameters now. If the system cannot provide memory
14201 * resources, the free routine needs to know what was allocated.
14202 */
14203 queue->entry_size = entry_size;
14204 queue->entry_count = entry_count;
14205 queue->page_size = hw_page_size;
14206 queue->phba = phba;
14207
4f774513
JS
14208 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14209 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14210 if (!dmabuf)
14211 goto out_fail;
1aee383d
JP
14212 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14213 hw_page_size, &dmabuf->phys,
14214 GFP_KERNEL);
4f774513
JS
14215 if (!dmabuf->virt) {
14216 kfree(dmabuf);
14217 goto out_fail;
14218 }
14219 dmabuf->buffer_tag = x;
14220 list_add_tail(&dmabuf->list, &queue->page_list);
14221 /* initialize queue's entry array */
14222 dma_pointer = dmabuf->virt;
14223 for (; total_qe_count < entry_count &&
cb5172ea 14224 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
14225 total_qe_count++, dma_pointer += entry_size) {
14226 queue->qe[total_qe_count].address = dma_pointer;
14227 }
14228 }
f485c18d
DK
14229 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14230 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 14231
64eb4dcb
JS
14232 /* entry_repost will be set during q creation */
14233
4f774513
JS
14234 return queue;
14235out_fail:
14236 lpfc_sli4_queue_free(queue);
14237 return NULL;
14238}
14239
962bc51b
JS
14240/**
14241 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14242 * @phba: HBA structure that indicates port to create a queue on.
14243 * @pci_barset: PCI BAR set flag.
14244 *
14245 * This function shall perform iomap of the specified PCI BAR address to host
14246 * memory address if not already done so and return it. The returned host
14247 * memory address can be NULL.
14248 */
14249static void __iomem *
14250lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14251{
962bc51b
JS
14252 if (!phba->pcidev)
14253 return NULL;
962bc51b
JS
14254
14255 switch (pci_barset) {
14256 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14257 return phba->pci_bar0_memmap_p;
14258 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14259 return phba->pci_bar2_memmap_p;
14260 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14261 return phba->pci_bar4_memmap_p;
14262 default:
14263 break;
14264 }
14265 return NULL;
14266}
14267
173edbb2 14268/**
895427bd 14269 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14270 * @phba: HBA structure that indicates port to create a queue on.
14271 * @startq: The starting FCP EQ to modify
14272 *
14273 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14274 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14275 * updated in one mailbox command.
173edbb2
JS
14276 *
14277 * The @phba struct is used to send mailbox command to HBA. The @startq
14278 * is used to get the starting FCP EQ to change.
14279 * This function is asynchronous and will wait for the mailbox
14280 * command to finish before continuing.
14281 *
14282 * On success this function will return a zero. If unable to allocate enough
14283 * memory this function will return -ENOMEM. If the queue create mailbox command
14284 * fails this function will return -ENXIO.
14285 **/
a2fc4aef 14286int
0cf07f84
JS
14287lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14288 uint32_t numq, uint32_t imax)
173edbb2
JS
14289{
14290 struct lpfc_mbx_modify_eq_delay *eq_delay;
14291 LPFC_MBOXQ_t *mbox;
14292 struct lpfc_queue *eq;
14293 int cnt, rc, length, status = 0;
14294 uint32_t shdr_status, shdr_add_status;
0cf07f84 14295 uint32_t result, val;
895427bd 14296 int qidx;
173edbb2
JS
14297 union lpfc_sli4_cfg_shdr *shdr;
14298 uint16_t dmult;
14299
895427bd 14300 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14301 return 0;
14302
14303 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14304 if (!mbox)
14305 return -ENOMEM;
14306 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14307 sizeof(struct lpfc_sli4_cfg_mhdr));
14308 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14309 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14310 length, LPFC_SLI4_MBX_EMBED);
14311 eq_delay = &mbox->u.mqe.un.eq_delay;
14312
14313 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14314 result = imax / phba->io_channel_irqs;
895427bd 14315 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14316 dmult = 0;
14317 else
14318 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14319 if (dmult > LPFC_DMULT_MAX)
14320 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14321
14322 cnt = 0;
895427bd
JS
14323 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14324 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14325 if (!eq)
14326 continue;
0cf07f84 14327 eq->q_mode = imax;
173edbb2
JS
14328 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14329 eq_delay->u.request.eq[cnt].phase = 0;
14330 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14331 cnt++;
0cf07f84
JS
14332
14333 /* q_mode is only used for auto_imax */
14334 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14335 /* Use EQ Delay Register method for q_mode */
14336
14337 /* Convert for EQ Delay register */
14338 val = phba->cfg_fcp_imax;
14339 if (val) {
14340 /* First, interrupts per sec per EQ */
14341 val = phba->cfg_fcp_imax /
14342 phba->io_channel_irqs;
14343
14344 /* us delay between each interrupt */
14345 val = LPFC_SEC_TO_USEC / val;
14346 }
14347 eq->q_mode = val;
14348 } else {
14349 eq->q_mode = imax;
14350 }
14351
14352 if (cnt >= numq)
173edbb2
JS
14353 break;
14354 }
14355 eq_delay->u.request.num_eq = cnt;
14356
14357 mbox->vport = phba->pport;
14358 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14359 mbox->context1 = NULL;
14360 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14361 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14362 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14363 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14364 if (shdr_status || shdr_add_status || rc) {
14365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14366 "2512 MODIFY_EQ_DELAY mailbox failed with "
14367 "status x%x add_status x%x, mbx status x%x\n",
14368 shdr_status, shdr_add_status, rc);
14369 status = -ENXIO;
14370 }
14371 mempool_free(mbox, phba->mbox_mem_pool);
14372 return status;
14373}
14374
4f774513
JS
14375/**
14376 * lpfc_eq_create - Create an Event Queue on the HBA
14377 * @phba: HBA structure that indicates port to create a queue on.
14378 * @eq: The queue structure to use to create the event queue.
14379 * @imax: The maximum interrupt per second limit.
14380 *
14381 * This function creates an event queue, as detailed in @eq, on a port,
14382 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14383 *
14384 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14385 * is used to get the entry count and entry size that are necessary to
14386 * determine the number of pages to allocate and use for this queue. This
14387 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14388 * event queue. This function is asynchronous and will wait for the mailbox
14389 * command to finish before continuing.
14390 *
14391 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14392 * memory this function will return -ENOMEM. If the queue create mailbox command
14393 * fails this function will return -ENXIO.
4f774513 14394 **/
a2fc4aef 14395int
ee02006b 14396lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14397{
14398 struct lpfc_mbx_eq_create *eq_create;
14399 LPFC_MBOXQ_t *mbox;
14400 int rc, length, status = 0;
14401 struct lpfc_dmabuf *dmabuf;
14402 uint32_t shdr_status, shdr_add_status;
14403 union lpfc_sli4_cfg_shdr *shdr;
14404 uint16_t dmult;
49198b37
JS
14405 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14406
2e90f4b5
JS
14407 /* sanity check on queue memory */
14408 if (!eq)
14409 return -ENODEV;
49198b37
JS
14410 if (!phba->sli4_hba.pc_sli4_params.supported)
14411 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14412
14413 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14414 if (!mbox)
14415 return -ENOMEM;
14416 length = (sizeof(struct lpfc_mbx_eq_create) -
14417 sizeof(struct lpfc_sli4_cfg_mhdr));
14418 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14419 LPFC_MBOX_OPCODE_EQ_CREATE,
14420 length, LPFC_SLI4_MBX_EMBED);
14421 eq_create = &mbox->u.mqe.un.eq_create;
7365f6fd 14422 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
4f774513
JS
14423 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14424 eq->page_count);
14425 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14426 LPFC_EQE_SIZE);
14427 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
7365f6fd
JS
14428
14429 /* Use version 2 of CREATE_EQ if eqav is set */
14430 if (phba->sli4_hba.pc_sli4_params.eqav) {
14431 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14432 LPFC_Q_CREATE_VERSION_2);
14433 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14434 phba->sli4_hba.pc_sli4_params.eqav);
14435 }
14436
2c9c5a00
JS
14437 /* don't setup delay multiplier using EQ_CREATE */
14438 dmult = 0;
4f774513
JS
14439 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14440 dmult);
14441 switch (eq->entry_count) {
14442 default:
14443 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14444 "0360 Unsupported EQ count. (%d)\n",
14445 eq->entry_count);
14446 if (eq->entry_count < 256)
14447 return -EINVAL;
14448 /* otherwise default to smallest count (drop through) */
14449 case 256:
14450 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14451 LPFC_EQ_CNT_256);
14452 break;
14453 case 512:
14454 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14455 LPFC_EQ_CNT_512);
14456 break;
14457 case 1024:
14458 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14459 LPFC_EQ_CNT_1024);
14460 break;
14461 case 2048:
14462 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14463 LPFC_EQ_CNT_2048);
14464 break;
14465 case 4096:
14466 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14467 LPFC_EQ_CNT_4096);
14468 break;
14469 }
14470 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14471 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14472 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14473 putPaddrLow(dmabuf->phys);
14474 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14475 putPaddrHigh(dmabuf->phys);
14476 }
14477 mbox->vport = phba->pport;
14478 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14479 mbox->context1 = NULL;
14480 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4f774513
JS
14481 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14482 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14483 if (shdr_status || shdr_add_status || rc) {
14484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14485 "2500 EQ_CREATE mailbox failed with "
14486 "status x%x add_status x%x, mbx status x%x\n",
14487 shdr_status, shdr_add_status, rc);
14488 status = -ENXIO;
14489 }
14490 eq->type = LPFC_EQ;
14491 eq->subtype = LPFC_NONE;
14492 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14493 if (eq->queue_id == 0xFFFF)
14494 status = -ENXIO;
14495 eq->host_index = 0;
14496 eq->hba_index = 0;
64eb4dcb 14497 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14498
8fa38513 14499 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14500 return status;
14501}
14502
14503/**
14504 * lpfc_cq_create - Create a Completion Queue on the HBA
14505 * @phba: HBA structure that indicates port to create a queue on.
14506 * @cq: The queue structure to use to create the completion queue.
14507 * @eq: The event queue to bind this completion queue to.
14508 *
14509 * This function creates a completion queue, as detailed in @wq, on a port,
14510 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14511 *
14512 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14513 * is used to get the entry count and entry size that are necessary to
14514 * determine the number of pages to allocate and use for this queue. The @eq
14515 * is used to indicate which event queue to bind this completion queue to. This
14516 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14517 * completion queue. This function is asynchronous and will wait for the mailbox
14518 * command to finish before continuing.
14519 *
14520 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14521 * memory this function will return -ENOMEM. If the queue create mailbox command
14522 * fails this function will return -ENXIO.
4f774513 14523 **/
a2fc4aef 14524int
4f774513
JS
14525lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14526 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14527{
14528 struct lpfc_mbx_cq_create *cq_create;
14529 struct lpfc_dmabuf *dmabuf;
14530 LPFC_MBOXQ_t *mbox;
14531 int rc, length, status = 0;
14532 uint32_t shdr_status, shdr_add_status;
14533 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14534 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14535
2e90f4b5
JS
14536 /* sanity check on queue memory */
14537 if (!cq || !eq)
14538 return -ENODEV;
49198b37 14539 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14540 hw_page_size = cq->page_size;
49198b37 14541
4f774513
JS
14542 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14543 if (!mbox)
14544 return -ENOMEM;
14545 length = (sizeof(struct lpfc_mbx_cq_create) -
14546 sizeof(struct lpfc_sli4_cfg_mhdr));
14547 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14548 LPFC_MBOX_OPCODE_CQ_CREATE,
14549 length, LPFC_SLI4_MBX_EMBED);
14550 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14551 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14552 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14553 cq->page_count);
14554 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14555 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14556 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14557 phba->sli4_hba.pc_sli4_params.cqv);
14558 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14559 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14560 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14561 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14562 eq->queue_id);
7365f6fd
JS
14563 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14564 phba->sli4_hba.pc_sli4_params.cqav);
5a6f133e
JS
14565 } else {
14566 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14567 eq->queue_id);
14568 }
4f774513 14569 switch (cq->entry_count) {
81b96eda
JS
14570 case 2048:
14571 case 4096:
14572 if (phba->sli4_hba.pc_sli4_params.cqv ==
14573 LPFC_Q_CREATE_VERSION_2) {
14574 cq_create->u.request.context.lpfc_cq_context_count =
14575 cq->entry_count;
14576 bf_set(lpfc_cq_context_count,
14577 &cq_create->u.request.context,
14578 LPFC_CQ_CNT_WORD7);
14579 break;
14580 }
14581 /* Fall Thru */
4f774513
JS
14582 default:
14583 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14584 "0361 Unsupported CQ count: "
64eb4dcb 14585 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14586 cq->entry_count, cq->entry_size,
64eb4dcb 14587 cq->page_count);
4f4c1863
JS
14588 if (cq->entry_count < 256) {
14589 status = -EINVAL;
14590 goto out;
14591 }
4f774513
JS
14592 /* otherwise default to smallest count (drop through) */
14593 case 256:
14594 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14595 LPFC_CQ_CNT_256);
14596 break;
14597 case 512:
14598 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14599 LPFC_CQ_CNT_512);
14600 break;
14601 case 1024:
14602 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14603 LPFC_CQ_CNT_1024);
14604 break;
14605 }
14606 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14607 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14608 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14609 putPaddrLow(dmabuf->phys);
14610 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14611 putPaddrHigh(dmabuf->phys);
14612 }
14613 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14614
14615 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14616 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14617 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14618 if (shdr_status || shdr_add_status || rc) {
14619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14620 "2501 CQ_CREATE mailbox failed with "
14621 "status x%x add_status x%x, mbx status x%x\n",
14622 shdr_status, shdr_add_status, rc);
14623 status = -ENXIO;
14624 goto out;
14625 }
14626 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14627 if (cq->queue_id == 0xFFFF) {
14628 status = -ENXIO;
14629 goto out;
14630 }
14631 /* link the cq onto the parent eq child list */
14632 list_add_tail(&cq->list, &eq->child_list);
14633 /* Set up completion queue's type and subtype */
14634 cq->type = type;
14635 cq->subtype = subtype;
14636 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14637 cq->assoc_qid = eq->queue_id;
4f774513
JS
14638 cq->host_index = 0;
14639 cq->hba_index = 0;
64eb4dcb 14640 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14641
8fa38513
JS
14642out:
14643 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14644 return status;
14645}
14646
2d7dbc4c
JS
14647/**
14648 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14649 * @phba: HBA structure that indicates port to create a queue on.
14650 * @cqp: The queue structure array to use to create the completion queues.
14651 * @eqp: The event queue array to bind these completion queues to.
14652 *
14653 * This function creates a set of completion queue, s to support MRQ
14654 * as detailed in @cqp, on a port,
14655 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14656 *
14657 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14658 * is used to get the entry count and entry size that are necessary to
14659 * determine the number of pages to allocate and use for this queue. The @eq
14660 * is used to indicate which event queue to bind this completion queue to. This
14661 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14662 * completion queue. This function is asynchronous and will wait for the mailbox
14663 * command to finish before continuing.
14664 *
14665 * On success this function will return a zero. If unable to allocate enough
14666 * memory this function will return -ENOMEM. If the queue create mailbox command
14667 * fails this function will return -ENXIO.
14668 **/
14669int
14670lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14671 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14672{
14673 struct lpfc_queue *cq;
14674 struct lpfc_queue *eq;
14675 struct lpfc_mbx_cq_create_set *cq_set;
14676 struct lpfc_dmabuf *dmabuf;
14677 LPFC_MBOXQ_t *mbox;
14678 int rc, length, alloclen, status = 0;
14679 int cnt, idx, numcq, page_idx = 0;
14680 uint32_t shdr_status, shdr_add_status;
14681 union lpfc_sli4_cfg_shdr *shdr;
14682 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14683
14684 /* sanity check on queue memory */
14685 numcq = phba->cfg_nvmet_mrq;
14686 if (!cqp || !eqp || !numcq)
14687 return -ENODEV;
2d7dbc4c
JS
14688
14689 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14690 if (!mbox)
14691 return -ENOMEM;
14692
14693 length = sizeof(struct lpfc_mbx_cq_create_set);
14694 length += ((numcq * cqp[0]->page_count) *
14695 sizeof(struct dma_address));
14696 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14697 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14698 LPFC_SLI4_MBX_NEMBED);
14699 if (alloclen < length) {
14700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14701 "3098 Allocated DMA memory size (%d) is "
14702 "less than the requested DMA memory size "
14703 "(%d)\n", alloclen, length);
14704 status = -ENOMEM;
14705 goto out;
14706 }
14707 cq_set = mbox->sge_array->addr[0];
14708 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14709 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14710
14711 for (idx = 0; idx < numcq; idx++) {
14712 cq = cqp[idx];
14713 eq = eqp[idx];
14714 if (!cq || !eq) {
14715 status = -ENOMEM;
14716 goto out;
14717 }
81b96eda
JS
14718 if (!phba->sli4_hba.pc_sli4_params.supported)
14719 hw_page_size = cq->page_size;
2d7dbc4c
JS
14720
14721 switch (idx) {
14722 case 0:
14723 bf_set(lpfc_mbx_cq_create_set_page_size,
14724 &cq_set->u.request,
14725 (hw_page_size / SLI4_PAGE_SIZE));
14726 bf_set(lpfc_mbx_cq_create_set_num_pages,
14727 &cq_set->u.request, cq->page_count);
14728 bf_set(lpfc_mbx_cq_create_set_evt,
14729 &cq_set->u.request, 1);
14730 bf_set(lpfc_mbx_cq_create_set_valid,
14731 &cq_set->u.request, 1);
14732 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14733 &cq_set->u.request, 0);
14734 bf_set(lpfc_mbx_cq_create_set_num_cq,
14735 &cq_set->u.request, numcq);
7365f6fd
JS
14736 bf_set(lpfc_mbx_cq_create_set_autovalid,
14737 &cq_set->u.request,
14738 phba->sli4_hba.pc_sli4_params.cqav);
2d7dbc4c 14739 switch (cq->entry_count) {
81b96eda
JS
14740 case 2048:
14741 case 4096:
14742 if (phba->sli4_hba.pc_sli4_params.cqv ==
14743 LPFC_Q_CREATE_VERSION_2) {
14744 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14745 &cq_set->u.request,
14746 cq->entry_count);
14747 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14748 &cq_set->u.request,
14749 LPFC_CQ_CNT_WORD7);
14750 break;
14751 }
14752 /* Fall Thru */
2d7dbc4c
JS
14753 default:
14754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14755 "3118 Bad CQ count. (%d)\n",
14756 cq->entry_count);
14757 if (cq->entry_count < 256) {
14758 status = -EINVAL;
14759 goto out;
14760 }
14761 /* otherwise default to smallest (drop thru) */
14762 case 256:
14763 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14764 &cq_set->u.request, LPFC_CQ_CNT_256);
14765 break;
14766 case 512:
14767 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14768 &cq_set->u.request, LPFC_CQ_CNT_512);
14769 break;
14770 case 1024:
14771 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14772 &cq_set->u.request, LPFC_CQ_CNT_1024);
14773 break;
14774 }
14775 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14776 &cq_set->u.request, eq->queue_id);
14777 break;
14778 case 1:
14779 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14780 &cq_set->u.request, eq->queue_id);
14781 break;
14782 case 2:
14783 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14784 &cq_set->u.request, eq->queue_id);
14785 break;
14786 case 3:
14787 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14788 &cq_set->u.request, eq->queue_id);
14789 break;
14790 case 4:
14791 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14792 &cq_set->u.request, eq->queue_id);
14793 break;
14794 case 5:
14795 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14796 &cq_set->u.request, eq->queue_id);
14797 break;
14798 case 6:
14799 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14800 &cq_set->u.request, eq->queue_id);
14801 break;
14802 case 7:
14803 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14804 &cq_set->u.request, eq->queue_id);
14805 break;
14806 case 8:
14807 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14808 &cq_set->u.request, eq->queue_id);
14809 break;
14810 case 9:
14811 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14812 &cq_set->u.request, eq->queue_id);
14813 break;
14814 case 10:
14815 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14816 &cq_set->u.request, eq->queue_id);
14817 break;
14818 case 11:
14819 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14820 &cq_set->u.request, eq->queue_id);
14821 break;
14822 case 12:
14823 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14824 &cq_set->u.request, eq->queue_id);
14825 break;
14826 case 13:
14827 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14828 &cq_set->u.request, eq->queue_id);
14829 break;
14830 case 14:
14831 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14832 &cq_set->u.request, eq->queue_id);
14833 break;
14834 case 15:
14835 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14836 &cq_set->u.request, eq->queue_id);
14837 break;
14838 }
14839
14840 /* link the cq onto the parent eq child list */
14841 list_add_tail(&cq->list, &eq->child_list);
14842 /* Set up completion queue's type and subtype */
14843 cq->type = type;
14844 cq->subtype = subtype;
14845 cq->assoc_qid = eq->queue_id;
14846 cq->host_index = 0;
14847 cq->hba_index = 0;
64eb4dcb 14848 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14849 cq->chann = idx;
2d7dbc4c
JS
14850
14851 rc = 0;
14852 list_for_each_entry(dmabuf, &cq->page_list, list) {
14853 memset(dmabuf->virt, 0, hw_page_size);
14854 cnt = page_idx + dmabuf->buffer_tag;
14855 cq_set->u.request.page[cnt].addr_lo =
14856 putPaddrLow(dmabuf->phys);
14857 cq_set->u.request.page[cnt].addr_hi =
14858 putPaddrHigh(dmabuf->phys);
14859 rc++;
14860 }
14861 page_idx += rc;
14862 }
14863
14864 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14865
14866 /* The IOCTL status is embedded in the mailbox subheader. */
14867 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14868 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14869 if (shdr_status || shdr_add_status || rc) {
14870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14871 "3119 CQ_CREATE_SET mailbox failed with "
14872 "status x%x add_status x%x, mbx status x%x\n",
14873 shdr_status, shdr_add_status, rc);
14874 status = -ENXIO;
14875 goto out;
14876 }
14877 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14878 if (rc == 0xFFFF) {
14879 status = -ENXIO;
14880 goto out;
14881 }
14882
14883 for (idx = 0; idx < numcq; idx++) {
14884 cq = cqp[idx];
14885 cq->queue_id = rc + idx;
14886 }
14887
14888out:
14889 lpfc_sli4_mbox_cmd_free(phba, mbox);
14890 return status;
14891}
14892
b19a061a
JS
14893/**
14894 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14895 * @phba: HBA structure that indicates port to create a queue on.
14896 * @mq: The queue structure to use to create the mailbox queue.
14897 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14898 * @cq: The completion queue to associate with this cq.
14899 *
14900 * This function provides failback (fb) functionality when the
14901 * mq_create_ext fails on older FW generations. It's purpose is identical
14902 * to mq_create_ext otherwise.
14903 *
14904 * This routine cannot fail as all attributes were previously accessed and
14905 * initialized in mq_create_ext.
14906 **/
14907static void
14908lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14909 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14910{
14911 struct lpfc_mbx_mq_create *mq_create;
14912 struct lpfc_dmabuf *dmabuf;
14913 int length;
14914
14915 length = (sizeof(struct lpfc_mbx_mq_create) -
14916 sizeof(struct lpfc_sli4_cfg_mhdr));
14917 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14918 LPFC_MBOX_OPCODE_MQ_CREATE,
14919 length, LPFC_SLI4_MBX_EMBED);
14920 mq_create = &mbox->u.mqe.un.mq_create;
14921 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14922 mq->page_count);
14923 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14924 cq->queue_id);
14925 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14926 switch (mq->entry_count) {
14927 case 16:
5a6f133e
JS
14928 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14929 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14930 break;
14931 case 32:
5a6f133e
JS
14932 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14933 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14934 break;
14935 case 64:
5a6f133e
JS
14936 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14937 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14938 break;
14939 case 128:
5a6f133e
JS
14940 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14941 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14942 break;
14943 }
14944 list_for_each_entry(dmabuf, &mq->page_list, list) {
14945 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14946 putPaddrLow(dmabuf->phys);
14947 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14948 putPaddrHigh(dmabuf->phys);
14949 }
14950}
14951
04c68496
JS
14952/**
14953 * lpfc_mq_create - Create a mailbox Queue on the HBA
14954 * @phba: HBA structure that indicates port to create a queue on.
14955 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14956 * @cq: The completion queue to associate with this cq.
14957 * @subtype: The queue's subtype.
04c68496
JS
14958 *
14959 * This function creates a mailbox queue, as detailed in @mq, on a port,
14960 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14961 *
14962 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14963 * is used to get the entry count and entry size that are necessary to
14964 * determine the number of pages to allocate and use for this queue. This
14965 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14966 * mailbox queue. This function is asynchronous and will wait for the mailbox
14967 * command to finish before continuing.
14968 *
14969 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14970 * memory this function will return -ENOMEM. If the queue create mailbox command
14971 * fails this function will return -ENXIO.
04c68496 14972 **/
b19a061a 14973int32_t
04c68496
JS
14974lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14975 struct lpfc_queue *cq, uint32_t subtype)
14976{
14977 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14978 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14979 struct lpfc_dmabuf *dmabuf;
14980 LPFC_MBOXQ_t *mbox;
14981 int rc, length, status = 0;
14982 uint32_t shdr_status, shdr_add_status;
14983 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14984 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14985
2e90f4b5
JS
14986 /* sanity check on queue memory */
14987 if (!mq || !cq)
14988 return -ENODEV;
49198b37
JS
14989 if (!phba->sli4_hba.pc_sli4_params.supported)
14990 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14991
04c68496
JS
14992 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14993 if (!mbox)
14994 return -ENOMEM;
b19a061a 14995 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14996 sizeof(struct lpfc_sli4_cfg_mhdr));
14997 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14998 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14999 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
15000
15001 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 15002 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
15003 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15004 &mq_create_ext->u.request, mq->page_count);
15005 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15006 &mq_create_ext->u.request, 1);
15007 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
15008 &mq_create_ext->u.request, 1);
15009 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15010 &mq_create_ext->u.request, 1);
70f3c073
JS
15011 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15012 &mq_create_ext->u.request, 1);
15013 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15014 &mq_create_ext->u.request, 1);
b19a061a 15015 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
15016 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15017 phba->sli4_hba.pc_sli4_params.mqv);
15018 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15019 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15020 cq->queue_id);
15021 else
15022 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15023 cq->queue_id);
04c68496
JS
15024 switch (mq->entry_count) {
15025 default:
15026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15027 "0362 Unsupported MQ count. (%d)\n",
15028 mq->entry_count);
4f4c1863
JS
15029 if (mq->entry_count < 16) {
15030 status = -EINVAL;
15031 goto out;
15032 }
04c68496
JS
15033 /* otherwise default to smallest count (drop through) */
15034 case 16:
5a6f133e
JS
15035 bf_set(lpfc_mq_context_ring_size,
15036 &mq_create_ext->u.request.context,
15037 LPFC_MQ_RING_SIZE_16);
04c68496
JS
15038 break;
15039 case 32:
5a6f133e
JS
15040 bf_set(lpfc_mq_context_ring_size,
15041 &mq_create_ext->u.request.context,
15042 LPFC_MQ_RING_SIZE_32);
04c68496
JS
15043 break;
15044 case 64:
5a6f133e
JS
15045 bf_set(lpfc_mq_context_ring_size,
15046 &mq_create_ext->u.request.context,
15047 LPFC_MQ_RING_SIZE_64);
04c68496
JS
15048 break;
15049 case 128:
5a6f133e
JS
15050 bf_set(lpfc_mq_context_ring_size,
15051 &mq_create_ext->u.request.context,
15052 LPFC_MQ_RING_SIZE_128);
04c68496
JS
15053 break;
15054 }
15055 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 15056 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 15057 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 15058 putPaddrLow(dmabuf->phys);
b19a061a 15059 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
15060 putPaddrHigh(dmabuf->phys);
15061 }
15062 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
15063 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15064 &mq_create_ext->u.response);
15065 if (rc != MBX_SUCCESS) {
15066 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15067 "2795 MQ_CREATE_EXT failed with "
15068 "status x%x. Failback to MQ_CREATE.\n",
15069 rc);
15070 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15071 mq_create = &mbox->u.mqe.un.mq_create;
15072 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15073 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15074 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15075 &mq_create->u.response);
15076 }
15077
04c68496 15078 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
15079 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15080 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15081 if (shdr_status || shdr_add_status || rc) {
15082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15083 "2502 MQ_CREATE mailbox failed with "
15084 "status x%x add_status x%x, mbx status x%x\n",
15085 shdr_status, shdr_add_status, rc);
15086 status = -ENXIO;
15087 goto out;
15088 }
04c68496
JS
15089 if (mq->queue_id == 0xFFFF) {
15090 status = -ENXIO;
15091 goto out;
15092 }
15093 mq->type = LPFC_MQ;
2a622bfb 15094 mq->assoc_qid = cq->queue_id;
04c68496
JS
15095 mq->subtype = subtype;
15096 mq->host_index = 0;
15097 mq->hba_index = 0;
64eb4dcb 15098 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
15099
15100 /* link the mq onto the parent cq child list */
15101 list_add_tail(&mq->list, &cq->child_list);
15102out:
8fa38513 15103 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
15104 return status;
15105}
15106
4f774513
JS
15107/**
15108 * lpfc_wq_create - Create a Work Queue on the HBA
15109 * @phba: HBA structure that indicates port to create a queue on.
15110 * @wq: The queue structure to use to create the work queue.
15111 * @cq: The completion queue to bind this work queue to.
15112 * @subtype: The subtype of the work queue indicating its functionality.
15113 *
15114 * This function creates a work queue, as detailed in @wq, on a port, described
15115 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15116 *
15117 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15118 * is used to get the entry count and entry size that are necessary to
15119 * determine the number of pages to allocate and use for this queue. The @cq
15120 * is used to indicate which completion queue to bind this work queue to. This
15121 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15122 * work queue. This function is asynchronous and will wait for the mailbox
15123 * command to finish before continuing.
15124 *
15125 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15126 * memory this function will return -ENOMEM. If the queue create mailbox command
15127 * fails this function will return -ENXIO.
4f774513 15128 **/
a2fc4aef 15129int
4f774513
JS
15130lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15131 struct lpfc_queue *cq, uint32_t subtype)
15132{
15133 struct lpfc_mbx_wq_create *wq_create;
15134 struct lpfc_dmabuf *dmabuf;
15135 LPFC_MBOXQ_t *mbox;
15136 int rc, length, status = 0;
15137 uint32_t shdr_status, shdr_add_status;
15138 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15139 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 15140 struct dma_address *page;
962bc51b
JS
15141 void __iomem *bar_memmap_p;
15142 uint32_t db_offset;
15143 uint16_t pci_barset;
1351e69f
JS
15144 uint8_t dpp_barset;
15145 uint32_t dpp_offset;
15146 unsigned long pg_addr;
81b96eda 15147 uint8_t wq_create_version;
49198b37 15148
2e90f4b5
JS
15149 /* sanity check on queue memory */
15150 if (!wq || !cq)
15151 return -ENODEV;
49198b37 15152 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 15153 hw_page_size = wq->page_size;
4f774513
JS
15154
15155 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15156 if (!mbox)
15157 return -ENOMEM;
15158 length = (sizeof(struct lpfc_mbx_wq_create) -
15159 sizeof(struct lpfc_sli4_cfg_mhdr));
15160 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15161 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15162 length, LPFC_SLI4_MBX_EMBED);
15163 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 15164 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
15165 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15166 wq->page_count);
15167 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15168 cq->queue_id);
0c651878
JS
15169
15170 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
15171 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15172 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 15173
c176ffa0
JS
15174 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15175 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
15176 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15177 else
15178 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15179
0c651878 15180
1351e69f
JS
15181 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15182 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15183 else
15184 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15185
15186 switch (wq_create_version) {
0c651878 15187 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
15188 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15189 wq->entry_count);
3f247de7
JS
15190 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15191 LPFC_Q_CREATE_VERSION_1);
15192
5a6f133e
JS
15193 switch (wq->entry_size) {
15194 default:
15195 case 64:
15196 bf_set(lpfc_mbx_wq_create_wqe_size,
15197 &wq_create->u.request_1,
15198 LPFC_WQ_WQE_SIZE_64);
15199 break;
15200 case 128:
15201 bf_set(lpfc_mbx_wq_create_wqe_size,
15202 &wq_create->u.request_1,
15203 LPFC_WQ_WQE_SIZE_128);
15204 break;
15205 }
1351e69f
JS
15206 /* Request DPP by default */
15207 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
8ea73db4
JS
15208 bf_set(lpfc_mbx_wq_create_page_size,
15209 &wq_create->u.request_1,
81b96eda 15210 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 15211 page = wq_create->u.request_1.page;
0c651878
JS
15212 break;
15213 default:
1351e69f
JS
15214 page = wq_create->u.request.page;
15215 break;
5a6f133e 15216 }
0c651878 15217
4f774513 15218 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 15219 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
15220 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15221 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 15222 }
962bc51b
JS
15223
15224 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15225 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15226
4f774513
JS
15227 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15228 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15229 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15230 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15231 if (shdr_status || shdr_add_status || rc) {
15232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15233 "2503 WQ_CREATE mailbox failed with "
15234 "status x%x add_status x%x, mbx status x%x\n",
15235 shdr_status, shdr_add_status, rc);
15236 status = -ENXIO;
15237 goto out;
15238 }
1351e69f
JS
15239
15240 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15241 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15242 &wq_create->u.response);
15243 else
15244 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15245 &wq_create->u.response_1);
15246
4f774513
JS
15247 if (wq->queue_id == 0xFFFF) {
15248 status = -ENXIO;
15249 goto out;
15250 }
1351e69f
JS
15251
15252 wq->db_format = LPFC_DB_LIST_FORMAT;
15253 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15254 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15255 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15256 &wq_create->u.response);
15257 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15258 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15260 "3265 WQ[%d] doorbell format "
15261 "not supported: x%x\n",
15262 wq->queue_id, wq->db_format);
15263 status = -EINVAL;
15264 goto out;
15265 }
15266 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15267 &wq_create->u.response);
15268 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15269 pci_barset);
15270 if (!bar_memmap_p) {
15271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15272 "3263 WQ[%d] failed to memmap "
15273 "pci barset:x%x\n",
15274 wq->queue_id, pci_barset);
15275 status = -ENOMEM;
15276 goto out;
15277 }
15278 db_offset = wq_create->u.response.doorbell_offset;
15279 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15280 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15282 "3252 WQ[%d] doorbell offset "
15283 "not supported: x%x\n",
15284 wq->queue_id, db_offset);
15285 status = -EINVAL;
15286 goto out;
15287 }
15288 wq->db_regaddr = bar_memmap_p + db_offset;
15289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15290 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15291 "format:x%x\n", wq->queue_id,
15292 pci_barset, db_offset, wq->db_format);
15293 } else
15294 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15295 } else {
1351e69f
JS
15296 /* Check if DPP was honored by the firmware */
15297 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15298 &wq_create->u.response_1);
15299 if (wq->dpp_enable) {
15300 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15301 &wq_create->u.response_1);
15302 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15303 pci_barset);
15304 if (!bar_memmap_p) {
15305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15306 "3267 WQ[%d] failed to memmap "
15307 "pci barset:x%x\n",
15308 wq->queue_id, pci_barset);
15309 status = -ENOMEM;
15310 goto out;
15311 }
15312 db_offset = wq_create->u.response_1.doorbell_offset;
15313 wq->db_regaddr = bar_memmap_p + db_offset;
15314 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15315 &wq_create->u.response_1);
15316 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15317 &wq_create->u.response_1);
15318 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15319 dpp_barset);
15320 if (!bar_memmap_p) {
15321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15322 "3268 WQ[%d] failed to memmap "
15323 "pci barset:x%x\n",
15324 wq->queue_id, dpp_barset);
15325 status = -ENOMEM;
15326 goto out;
15327 }
15328 dpp_offset = wq_create->u.response_1.dpp_offset;
15329 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15330 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15331 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15332 "dpp_id:x%x dpp_barset:x%x "
15333 "dpp_offset:x%x\n",
15334 wq->queue_id, pci_barset, db_offset,
15335 wq->dpp_id, dpp_barset, dpp_offset);
15336
15337 /* Enable combined writes for DPP aperture */
15338 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15339#ifdef CONFIG_X86
15340 rc = set_memory_wc(pg_addr, 1);
15341 if (rc) {
15342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15343 "3272 Cannot setup Combined "
15344 "Write on WQ[%d] - disable DPP\n",
15345 wq->queue_id);
15346 phba->cfg_enable_dpp = 0;
15347 }
15348#else
15349 phba->cfg_enable_dpp = 0;
15350#endif
15351 } else
15352 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
962bc51b 15353 }
895427bd
JS
15354 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15355 if (wq->pring == NULL) {
15356 status = -ENOMEM;
15357 goto out;
15358 }
4f774513 15359 wq->type = LPFC_WQ;
2a622bfb 15360 wq->assoc_qid = cq->queue_id;
4f774513
JS
15361 wq->subtype = subtype;
15362 wq->host_index = 0;
15363 wq->hba_index = 0;
ff78d8f9 15364 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15365
15366 /* link the wq onto the parent cq child list */
15367 list_add_tail(&wq->list, &cq->child_list);
15368out:
8fa38513 15369 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15370 return status;
15371}
15372
15373/**
15374 * lpfc_rq_create - Create a Receive Queue on the HBA
15375 * @phba: HBA structure that indicates port to create a queue on.
15376 * @hrq: The queue structure to use to create the header receive queue.
15377 * @drq: The queue structure to use to create the data receive queue.
15378 * @cq: The completion queue to bind this work queue to.
15379 *
15380 * This function creates a receive buffer queue pair , as detailed in @hrq and
15381 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15382 * to the HBA.
15383 *
15384 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15385 * struct is used to get the entry count that is necessary to determine the
15386 * number of pages to use for this queue. The @cq is used to indicate which
15387 * completion queue to bind received buffers that are posted to these queues to.
15388 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15389 * receive queue pair. This function is asynchronous and will wait for the
15390 * mailbox command to finish before continuing.
15391 *
15392 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15393 * memory this function will return -ENOMEM. If the queue create mailbox command
15394 * fails this function will return -ENXIO.
4f774513 15395 **/
a2fc4aef 15396int
4f774513
JS
15397lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15398 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15399{
15400 struct lpfc_mbx_rq_create *rq_create;
15401 struct lpfc_dmabuf *dmabuf;
15402 LPFC_MBOXQ_t *mbox;
15403 int rc, length, status = 0;
15404 uint32_t shdr_status, shdr_add_status;
15405 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15406 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15407 void __iomem *bar_memmap_p;
15408 uint32_t db_offset;
15409 uint16_t pci_barset;
49198b37 15410
2e90f4b5
JS
15411 /* sanity check on queue memory */
15412 if (!hrq || !drq || !cq)
15413 return -ENODEV;
49198b37
JS
15414 if (!phba->sli4_hba.pc_sli4_params.supported)
15415 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15416
15417 if (hrq->entry_count != drq->entry_count)
15418 return -EINVAL;
15419 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15420 if (!mbox)
15421 return -ENOMEM;
15422 length = (sizeof(struct lpfc_mbx_rq_create) -
15423 sizeof(struct lpfc_sli4_cfg_mhdr));
15424 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15425 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15426 length, LPFC_SLI4_MBX_EMBED);
15427 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15428 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15429 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15430 phba->sli4_hba.pc_sli4_params.rqv);
15431 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15432 bf_set(lpfc_rq_context_rqe_count_1,
15433 &rq_create->u.request.context,
15434 hrq->entry_count);
15435 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15436 bf_set(lpfc_rq_context_rqe_size,
15437 &rq_create->u.request.context,
15438 LPFC_RQE_SIZE_8);
15439 bf_set(lpfc_rq_context_page_size,
15440 &rq_create->u.request.context,
8ea73db4 15441 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15442 } else {
15443 switch (hrq->entry_count) {
15444 default:
15445 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15446 "2535 Unsupported RQ count. (%d)\n",
15447 hrq->entry_count);
4f4c1863
JS
15448 if (hrq->entry_count < 512) {
15449 status = -EINVAL;
15450 goto out;
15451 }
5a6f133e
JS
15452 /* otherwise default to smallest count (drop through) */
15453 case 512:
15454 bf_set(lpfc_rq_context_rqe_count,
15455 &rq_create->u.request.context,
15456 LPFC_RQ_RING_SIZE_512);
15457 break;
15458 case 1024:
15459 bf_set(lpfc_rq_context_rqe_count,
15460 &rq_create->u.request.context,
15461 LPFC_RQ_RING_SIZE_1024);
15462 break;
15463 case 2048:
15464 bf_set(lpfc_rq_context_rqe_count,
15465 &rq_create->u.request.context,
15466 LPFC_RQ_RING_SIZE_2048);
15467 break;
15468 case 4096:
15469 bf_set(lpfc_rq_context_rqe_count,
15470 &rq_create->u.request.context,
15471 LPFC_RQ_RING_SIZE_4096);
15472 break;
15473 }
15474 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15475 LPFC_HDR_BUF_SIZE);
4f774513
JS
15476 }
15477 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15478 cq->queue_id);
15479 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15480 hrq->page_count);
4f774513 15481 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15482 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15483 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15484 putPaddrLow(dmabuf->phys);
15485 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15486 putPaddrHigh(dmabuf->phys);
15487 }
962bc51b
JS
15488 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15489 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15490
4f774513
JS
15491 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15492 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15493 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15494 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15495 if (shdr_status || shdr_add_status || rc) {
15496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15497 "2504 RQ_CREATE mailbox failed with "
15498 "status x%x add_status x%x, mbx status x%x\n",
15499 shdr_status, shdr_add_status, rc);
15500 status = -ENXIO;
15501 goto out;
15502 }
15503 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15504 if (hrq->queue_id == 0xFFFF) {
15505 status = -ENXIO;
15506 goto out;
15507 }
962bc51b
JS
15508
15509 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15510 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15511 &rq_create->u.response);
15512 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15513 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15515 "3262 RQ [%d] doorbell format not "
15516 "supported: x%x\n", hrq->queue_id,
15517 hrq->db_format);
15518 status = -EINVAL;
15519 goto out;
15520 }
15521
15522 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15523 &rq_create->u.response);
15524 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15525 if (!bar_memmap_p) {
15526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15527 "3269 RQ[%d] failed to memmap pci "
15528 "barset:x%x\n", hrq->queue_id,
15529 pci_barset);
15530 status = -ENOMEM;
15531 goto out;
15532 }
15533
15534 db_offset = rq_create->u.response.doorbell_offset;
15535 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15536 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15538 "3270 RQ[%d] doorbell offset not "
15539 "supported: x%x\n", hrq->queue_id,
15540 db_offset);
15541 status = -EINVAL;
15542 goto out;
15543 }
15544 hrq->db_regaddr = bar_memmap_p + db_offset;
15545 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15546 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15547 "format:x%x\n", hrq->queue_id, pci_barset,
15548 db_offset, hrq->db_format);
962bc51b
JS
15549 } else {
15550 hrq->db_format = LPFC_DB_RING_FORMAT;
15551 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15552 }
4f774513 15553 hrq->type = LPFC_HRQ;
2a622bfb 15554 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15555 hrq->subtype = subtype;
15556 hrq->host_index = 0;
15557 hrq->hba_index = 0;
61f3d4bf 15558 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15559
15560 /* now create the data queue */
15561 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15562 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15563 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15564 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15565 phba->sli4_hba.pc_sli4_params.rqv);
15566 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15567 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15568 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15569 if (subtype == LPFC_NVMET)
15570 rq_create->u.request.context.buffer_size =
15571 LPFC_NVMET_DATA_BUF_SIZE;
15572 else
15573 rq_create->u.request.context.buffer_size =
15574 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15575 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15576 LPFC_RQE_SIZE_8);
15577 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15578 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15579 } else {
15580 switch (drq->entry_count) {
15581 default:
15582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15583 "2536 Unsupported RQ count. (%d)\n",
15584 drq->entry_count);
4f4c1863
JS
15585 if (drq->entry_count < 512) {
15586 status = -EINVAL;
15587 goto out;
15588 }
5a6f133e
JS
15589 /* otherwise default to smallest count (drop through) */
15590 case 512:
15591 bf_set(lpfc_rq_context_rqe_count,
15592 &rq_create->u.request.context,
15593 LPFC_RQ_RING_SIZE_512);
15594 break;
15595 case 1024:
15596 bf_set(lpfc_rq_context_rqe_count,
15597 &rq_create->u.request.context,
15598 LPFC_RQ_RING_SIZE_1024);
15599 break;
15600 case 2048:
15601 bf_set(lpfc_rq_context_rqe_count,
15602 &rq_create->u.request.context,
15603 LPFC_RQ_RING_SIZE_2048);
15604 break;
15605 case 4096:
15606 bf_set(lpfc_rq_context_rqe_count,
15607 &rq_create->u.request.context,
15608 LPFC_RQ_RING_SIZE_4096);
15609 break;
15610 }
3c603be9
JS
15611 if (subtype == LPFC_NVMET)
15612 bf_set(lpfc_rq_context_buf_size,
15613 &rq_create->u.request.context,
15614 LPFC_NVMET_DATA_BUF_SIZE);
15615 else
15616 bf_set(lpfc_rq_context_buf_size,
15617 &rq_create->u.request.context,
15618 LPFC_DATA_BUF_SIZE);
4f774513
JS
15619 }
15620 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15621 cq->queue_id);
15622 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15623 drq->page_count);
4f774513
JS
15624 list_for_each_entry(dmabuf, &drq->page_list, list) {
15625 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15626 putPaddrLow(dmabuf->phys);
15627 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15628 putPaddrHigh(dmabuf->phys);
15629 }
962bc51b
JS
15630 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15631 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15632 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15633 /* The IOCTL status is embedded in the mailbox subheader. */
15634 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15635 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15636 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15637 if (shdr_status || shdr_add_status || rc) {
15638 status = -ENXIO;
15639 goto out;
15640 }
15641 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15642 if (drq->queue_id == 0xFFFF) {
15643 status = -ENXIO;
15644 goto out;
15645 }
15646 drq->type = LPFC_DRQ;
2a622bfb 15647 drq->assoc_qid = cq->queue_id;
4f774513
JS
15648 drq->subtype = subtype;
15649 drq->host_index = 0;
15650 drq->hba_index = 0;
61f3d4bf 15651 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15652
15653 /* link the header and data RQs onto the parent cq child list */
15654 list_add_tail(&hrq->list, &cq->child_list);
15655 list_add_tail(&drq->list, &cq->child_list);
15656
15657out:
8fa38513 15658 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15659 return status;
15660}
15661
2d7dbc4c
JS
15662/**
15663 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15664 * @phba: HBA structure that indicates port to create a queue on.
15665 * @hrqp: The queue structure array to use to create the header receive queues.
15666 * @drqp: The queue structure array to use to create the data receive queues.
15667 * @cqp: The completion queue array to bind these receive queues to.
15668 *
15669 * This function creates a receive buffer queue pair , as detailed in @hrq and
15670 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15671 * to the HBA.
15672 *
15673 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15674 * struct is used to get the entry count that is necessary to determine the
15675 * number of pages to use for this queue. The @cq is used to indicate which
15676 * completion queue to bind received buffers that are posted to these queues to.
15677 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15678 * receive queue pair. This function is asynchronous and will wait for the
15679 * mailbox command to finish before continuing.
15680 *
15681 * On success this function will return a zero. If unable to allocate enough
15682 * memory this function will return -ENOMEM. If the queue create mailbox command
15683 * fails this function will return -ENXIO.
15684 **/
15685int
15686lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15687 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15688 uint32_t subtype)
15689{
15690 struct lpfc_queue *hrq, *drq, *cq;
15691 struct lpfc_mbx_rq_create_v2 *rq_create;
15692 struct lpfc_dmabuf *dmabuf;
15693 LPFC_MBOXQ_t *mbox;
15694 int rc, length, alloclen, status = 0;
15695 int cnt, idx, numrq, page_idx = 0;
15696 uint32_t shdr_status, shdr_add_status;
15697 union lpfc_sli4_cfg_shdr *shdr;
15698 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15699
15700 numrq = phba->cfg_nvmet_mrq;
15701 /* sanity check on array memory */
15702 if (!hrqp || !drqp || !cqp || !numrq)
15703 return -ENODEV;
15704 if (!phba->sli4_hba.pc_sli4_params.supported)
15705 hw_page_size = SLI4_PAGE_SIZE;
15706
15707 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15708 if (!mbox)
15709 return -ENOMEM;
15710
15711 length = sizeof(struct lpfc_mbx_rq_create_v2);
15712 length += ((2 * numrq * hrqp[0]->page_count) *
15713 sizeof(struct dma_address));
15714
15715 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15716 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15717 LPFC_SLI4_MBX_NEMBED);
15718 if (alloclen < length) {
15719 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15720 "3099 Allocated DMA memory size (%d) is "
15721 "less than the requested DMA memory size "
15722 "(%d)\n", alloclen, length);
15723 status = -ENOMEM;
15724 goto out;
15725 }
15726
15727
15728
15729 rq_create = mbox->sge_array->addr[0];
15730 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15731
15732 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15733 cnt = 0;
15734
15735 for (idx = 0; idx < numrq; idx++) {
15736 hrq = hrqp[idx];
15737 drq = drqp[idx];
15738 cq = cqp[idx];
15739
2d7dbc4c
JS
15740 /* sanity check on queue memory */
15741 if (!hrq || !drq || !cq) {
15742 status = -ENODEV;
15743 goto out;
15744 }
15745
7aabe84b
JS
15746 if (hrq->entry_count != drq->entry_count) {
15747 status = -EINVAL;
15748 goto out;
15749 }
15750
2d7dbc4c
JS
15751 if (idx == 0) {
15752 bf_set(lpfc_mbx_rq_create_num_pages,
15753 &rq_create->u.request,
15754 hrq->page_count);
15755 bf_set(lpfc_mbx_rq_create_rq_cnt,
15756 &rq_create->u.request, (numrq * 2));
15757 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15758 1);
15759 bf_set(lpfc_rq_context_base_cq,
15760 &rq_create->u.request.context,
15761 cq->queue_id);
15762 bf_set(lpfc_rq_context_data_size,
15763 &rq_create->u.request.context,
3c603be9 15764 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15765 bf_set(lpfc_rq_context_hdr_size,
15766 &rq_create->u.request.context,
15767 LPFC_HDR_BUF_SIZE);
15768 bf_set(lpfc_rq_context_rqe_count_1,
15769 &rq_create->u.request.context,
15770 hrq->entry_count);
15771 bf_set(lpfc_rq_context_rqe_size,
15772 &rq_create->u.request.context,
15773 LPFC_RQE_SIZE_8);
15774 bf_set(lpfc_rq_context_page_size,
15775 &rq_create->u.request.context,
15776 (PAGE_SIZE/SLI4_PAGE_SIZE));
15777 }
15778 rc = 0;
15779 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15780 memset(dmabuf->virt, 0, hw_page_size);
15781 cnt = page_idx + dmabuf->buffer_tag;
15782 rq_create->u.request.page[cnt].addr_lo =
15783 putPaddrLow(dmabuf->phys);
15784 rq_create->u.request.page[cnt].addr_hi =
15785 putPaddrHigh(dmabuf->phys);
15786 rc++;
15787 }
15788 page_idx += rc;
15789
15790 rc = 0;
15791 list_for_each_entry(dmabuf, &drq->page_list, list) {
15792 memset(dmabuf->virt, 0, hw_page_size);
15793 cnt = page_idx + dmabuf->buffer_tag;
15794 rq_create->u.request.page[cnt].addr_lo =
15795 putPaddrLow(dmabuf->phys);
15796 rq_create->u.request.page[cnt].addr_hi =
15797 putPaddrHigh(dmabuf->phys);
15798 rc++;
15799 }
15800 page_idx += rc;
15801
15802 hrq->db_format = LPFC_DB_RING_FORMAT;
15803 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15804 hrq->type = LPFC_HRQ;
15805 hrq->assoc_qid = cq->queue_id;
15806 hrq->subtype = subtype;
15807 hrq->host_index = 0;
15808 hrq->hba_index = 0;
61f3d4bf 15809 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15810
15811 drq->db_format = LPFC_DB_RING_FORMAT;
15812 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15813 drq->type = LPFC_DRQ;
15814 drq->assoc_qid = cq->queue_id;
15815 drq->subtype = subtype;
15816 drq->host_index = 0;
15817 drq->hba_index = 0;
61f3d4bf 15818 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15819
15820 list_add_tail(&hrq->list, &cq->child_list);
15821 list_add_tail(&drq->list, &cq->child_list);
15822 }
15823
15824 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15825 /* The IOCTL status is embedded in the mailbox subheader. */
15826 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15827 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15828 if (shdr_status || shdr_add_status || rc) {
15829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15830 "3120 RQ_CREATE mailbox failed with "
15831 "status x%x add_status x%x, mbx status x%x\n",
15832 shdr_status, shdr_add_status, rc);
15833 status = -ENXIO;
15834 goto out;
15835 }
15836 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15837 if (rc == 0xFFFF) {
15838 status = -ENXIO;
15839 goto out;
15840 }
15841
15842 /* Initialize all RQs with associated queue id */
15843 for (idx = 0; idx < numrq; idx++) {
15844 hrq = hrqp[idx];
15845 hrq->queue_id = rc + (2 * idx);
15846 drq = drqp[idx];
15847 drq->queue_id = rc + (2 * idx) + 1;
15848 }
15849
15850out:
15851 lpfc_sli4_mbox_cmd_free(phba, mbox);
15852 return status;
15853}
15854
4f774513
JS
15855/**
15856 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15857 * @eq: The queue structure associated with the queue to destroy.
15858 *
15859 * This function destroys a queue, as detailed in @eq by sending an mailbox
15860 * command, specific to the type of queue, to the HBA.
15861 *
15862 * The @eq struct is used to get the queue ID of the queue to destroy.
15863 *
15864 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15865 * command fails this function will return -ENXIO.
4f774513 15866 **/
a2fc4aef 15867int
4f774513
JS
15868lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15869{
15870 LPFC_MBOXQ_t *mbox;
15871 int rc, length, status = 0;
15872 uint32_t shdr_status, shdr_add_status;
15873 union lpfc_sli4_cfg_shdr *shdr;
15874
2e90f4b5 15875 /* sanity check on queue memory */
4f774513
JS
15876 if (!eq)
15877 return -ENODEV;
15878 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15879 if (!mbox)
15880 return -ENOMEM;
15881 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15882 sizeof(struct lpfc_sli4_cfg_mhdr));
15883 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15884 LPFC_MBOX_OPCODE_EQ_DESTROY,
15885 length, LPFC_SLI4_MBX_EMBED);
15886 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15887 eq->queue_id);
15888 mbox->vport = eq->phba->pport;
15889 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15890
15891 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15892 /* The IOCTL status is embedded in the mailbox subheader. */
15893 shdr = (union lpfc_sli4_cfg_shdr *)
15894 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15895 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15896 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15897 if (shdr_status || shdr_add_status || rc) {
15898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15899 "2505 EQ_DESTROY mailbox failed with "
15900 "status x%x add_status x%x, mbx status x%x\n",
15901 shdr_status, shdr_add_status, rc);
15902 status = -ENXIO;
15903 }
15904
15905 /* Remove eq from any list */
15906 list_del_init(&eq->list);
8fa38513 15907 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15908 return status;
15909}
15910
15911/**
15912 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15913 * @cq: The queue structure associated with the queue to destroy.
15914 *
15915 * This function destroys a queue, as detailed in @cq by sending an mailbox
15916 * command, specific to the type of queue, to the HBA.
15917 *
15918 * The @cq struct is used to get the queue ID of the queue to destroy.
15919 *
15920 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15921 * command fails this function will return -ENXIO.
4f774513 15922 **/
a2fc4aef 15923int
4f774513
JS
15924lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15925{
15926 LPFC_MBOXQ_t *mbox;
15927 int rc, length, status = 0;
15928 uint32_t shdr_status, shdr_add_status;
15929 union lpfc_sli4_cfg_shdr *shdr;
15930
2e90f4b5 15931 /* sanity check on queue memory */
4f774513
JS
15932 if (!cq)
15933 return -ENODEV;
15934 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15935 if (!mbox)
15936 return -ENOMEM;
15937 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15938 sizeof(struct lpfc_sli4_cfg_mhdr));
15939 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15940 LPFC_MBOX_OPCODE_CQ_DESTROY,
15941 length, LPFC_SLI4_MBX_EMBED);
15942 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15943 cq->queue_id);
15944 mbox->vport = cq->phba->pport;
15945 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15946 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15947 /* The IOCTL status is embedded in the mailbox subheader. */
15948 shdr = (union lpfc_sli4_cfg_shdr *)
15949 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15950 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15951 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15952 if (shdr_status || shdr_add_status || rc) {
15953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15954 "2506 CQ_DESTROY mailbox failed with "
15955 "status x%x add_status x%x, mbx status x%x\n",
15956 shdr_status, shdr_add_status, rc);
15957 status = -ENXIO;
15958 }
15959 /* Remove cq from any list */
15960 list_del_init(&cq->list);
8fa38513 15961 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15962 return status;
15963}
15964
04c68496
JS
15965/**
15966 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15967 * @qm: The queue structure associated with the queue to destroy.
15968 *
15969 * This function destroys a queue, as detailed in @mq by sending an mailbox
15970 * command, specific to the type of queue, to the HBA.
15971 *
15972 * The @mq struct is used to get the queue ID of the queue to destroy.
15973 *
15974 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15975 * command fails this function will return -ENXIO.
04c68496 15976 **/
a2fc4aef 15977int
04c68496
JS
15978lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15979{
15980 LPFC_MBOXQ_t *mbox;
15981 int rc, length, status = 0;
15982 uint32_t shdr_status, shdr_add_status;
15983 union lpfc_sli4_cfg_shdr *shdr;
15984
2e90f4b5 15985 /* sanity check on queue memory */
04c68496
JS
15986 if (!mq)
15987 return -ENODEV;
15988 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15989 if (!mbox)
15990 return -ENOMEM;
15991 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15992 sizeof(struct lpfc_sli4_cfg_mhdr));
15993 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15994 LPFC_MBOX_OPCODE_MQ_DESTROY,
15995 length, LPFC_SLI4_MBX_EMBED);
15996 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15997 mq->queue_id);
15998 mbox->vport = mq->phba->pport;
15999 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16000 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16001 /* The IOCTL status is embedded in the mailbox subheader. */
16002 shdr = (union lpfc_sli4_cfg_shdr *)
16003 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16004 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16005 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16006 if (shdr_status || shdr_add_status || rc) {
16007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16008 "2507 MQ_DESTROY mailbox failed with "
16009 "status x%x add_status x%x, mbx status x%x\n",
16010 shdr_status, shdr_add_status, rc);
16011 status = -ENXIO;
16012 }
16013 /* Remove mq from any list */
16014 list_del_init(&mq->list);
8fa38513 16015 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
16016 return status;
16017}
16018
4f774513
JS
16019/**
16020 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16021 * @wq: The queue structure associated with the queue to destroy.
16022 *
16023 * This function destroys a queue, as detailed in @wq by sending an mailbox
16024 * command, specific to the type of queue, to the HBA.
16025 *
16026 * The @wq struct is used to get the queue ID of the queue to destroy.
16027 *
16028 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16029 * command fails this function will return -ENXIO.
4f774513 16030 **/
a2fc4aef 16031int
4f774513
JS
16032lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16033{
16034 LPFC_MBOXQ_t *mbox;
16035 int rc, length, status = 0;
16036 uint32_t shdr_status, shdr_add_status;
16037 union lpfc_sli4_cfg_shdr *shdr;
16038
2e90f4b5 16039 /* sanity check on queue memory */
4f774513
JS
16040 if (!wq)
16041 return -ENODEV;
16042 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16043 if (!mbox)
16044 return -ENOMEM;
16045 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16046 sizeof(struct lpfc_sli4_cfg_mhdr));
16047 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16048 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16049 length, LPFC_SLI4_MBX_EMBED);
16050 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16051 wq->queue_id);
16052 mbox->vport = wq->phba->pport;
16053 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16054 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16055 shdr = (union lpfc_sli4_cfg_shdr *)
16056 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16057 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16058 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16059 if (shdr_status || shdr_add_status || rc) {
16060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16061 "2508 WQ_DESTROY mailbox failed with "
16062 "status x%x add_status x%x, mbx status x%x\n",
16063 shdr_status, shdr_add_status, rc);
16064 status = -ENXIO;
16065 }
16066 /* Remove wq from any list */
16067 list_del_init(&wq->list);
d1f525aa
JS
16068 kfree(wq->pring);
16069 wq->pring = NULL;
8fa38513 16070 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
16071 return status;
16072}
16073
16074/**
16075 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16076 * @rq: The queue structure associated with the queue to destroy.
16077 *
16078 * This function destroys a queue, as detailed in @rq by sending an mailbox
16079 * command, specific to the type of queue, to the HBA.
16080 *
16081 * The @rq struct is used to get the queue ID of the queue to destroy.
16082 *
16083 * On success this function will return a zero. If the queue destroy mailbox
d439d286 16084 * command fails this function will return -ENXIO.
4f774513 16085 **/
a2fc4aef 16086int
4f774513
JS
16087lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16088 struct lpfc_queue *drq)
16089{
16090 LPFC_MBOXQ_t *mbox;
16091 int rc, length, status = 0;
16092 uint32_t shdr_status, shdr_add_status;
16093 union lpfc_sli4_cfg_shdr *shdr;
16094
2e90f4b5 16095 /* sanity check on queue memory */
4f774513
JS
16096 if (!hrq || !drq)
16097 return -ENODEV;
16098 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16099 if (!mbox)
16100 return -ENOMEM;
16101 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 16102 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
16103 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16104 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16105 length, LPFC_SLI4_MBX_EMBED);
16106 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16107 hrq->queue_id);
16108 mbox->vport = hrq->phba->pport;
16109 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16110 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16111 /* The IOCTL status is embedded in the mailbox subheader. */
16112 shdr = (union lpfc_sli4_cfg_shdr *)
16113 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16114 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16115 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16116 if (shdr_status || shdr_add_status || rc) {
16117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16118 "2509 RQ_DESTROY mailbox failed with "
16119 "status x%x add_status x%x, mbx status x%x\n",
16120 shdr_status, shdr_add_status, rc);
16121 if (rc != MBX_TIMEOUT)
16122 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16123 return -ENXIO;
16124 }
16125 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16126 drq->queue_id);
16127 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16128 shdr = (union lpfc_sli4_cfg_shdr *)
16129 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16130 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16131 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16132 if (shdr_status || shdr_add_status || rc) {
16133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16134 "2510 RQ_DESTROY mailbox failed with "
16135 "status x%x add_status x%x, mbx status x%x\n",
16136 shdr_status, shdr_add_status, rc);
16137 status = -ENXIO;
16138 }
16139 list_del_init(&hrq->list);
16140 list_del_init(&drq->list);
8fa38513 16141 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
16142 return status;
16143}
16144
16145/**
16146 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16147 * @phba: The virtual port for which this call being executed.
16148 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16149 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16150 * @xritag: the xritag that ties this io to the SGL pages.
16151 *
16152 * This routine will post the sgl pages for the IO that has the xritag
16153 * that is in the iocbq structure. The xritag is assigned during iocbq
16154 * creation and persists for as long as the driver is loaded.
16155 * if the caller has fewer than 256 scatter gather segments to map then
16156 * pdma_phys_addr1 should be 0.
16157 * If the caller needs to map more than 256 scatter gather segment then
16158 * pdma_phys_addr1 should be a valid physical address.
16159 * physical address for SGLs must be 64 byte aligned.
16160 * If you are going to map 2 SGL's then the first one must have 256 entries
16161 * the second sgl can have between 1 and 256 entries.
16162 *
16163 * Return codes:
16164 * 0 - Success
16165 * -ENXIO, -ENOMEM - Failure
16166 **/
16167int
16168lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16169 dma_addr_t pdma_phys_addr0,
16170 dma_addr_t pdma_phys_addr1,
16171 uint16_t xritag)
16172{
16173 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16174 LPFC_MBOXQ_t *mbox;
16175 int rc;
16176 uint32_t shdr_status, shdr_add_status;
6d368e53 16177 uint32_t mbox_tmo;
4f774513
JS
16178 union lpfc_sli4_cfg_shdr *shdr;
16179
16180 if (xritag == NO_XRI) {
16181 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16182 "0364 Invalid param:\n");
16183 return -EINVAL;
16184 }
16185
16186 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16187 if (!mbox)
16188 return -ENOMEM;
16189
16190 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16191 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16192 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 16193 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
16194
16195 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16196 &mbox->u.mqe.un.post_sgl_pages;
16197 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16198 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16199
16200 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16201 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16202 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16203 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16204
16205 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16206 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16207 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16208 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16209 if (!phba->sli4_hba.intr_enable)
16210 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 16211 else {
a183a15f 16212 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
16213 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16214 }
4f774513
JS
16215 /* The IOCTL status is embedded in the mailbox subheader. */
16216 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16219 if (rc != MBX_TIMEOUT)
16220 mempool_free(mbox, phba->mbox_mem_pool);
16221 if (shdr_status || shdr_add_status || rc) {
16222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16223 "2511 POST_SGL mailbox failed with "
16224 "status x%x add_status x%x, mbx status x%x\n",
16225 shdr_status, shdr_add_status, rc);
4f774513
JS
16226 }
16227 return 0;
16228}
4f774513 16229
6d368e53 16230/**
88a2cfbb 16231 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
16232 * @phba: pointer to lpfc hba data structure.
16233 *
16234 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
16235 * HBA consistent with the SLI-4 interface spec. This routine
16236 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16237 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 16238 *
88a2cfbb
JS
16239 * Returns
16240 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16241 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16242 **/
5d8b8167 16243static uint16_t
6d368e53
JS
16244lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16245{
16246 unsigned long xri;
16247
16248 /*
16249 * Fetch the next logical xri. Because this index is logical,
16250 * the driver starts at 0 each time.
16251 */
16252 spin_lock_irq(&phba->hbalock);
16253 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16254 phba->sli4_hba.max_cfg_param.max_xri, 0);
16255 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16256 spin_unlock_irq(&phba->hbalock);
16257 return NO_XRI;
16258 } else {
16259 set_bit(xri, phba->sli4_hba.xri_bmask);
16260 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 16261 }
6d368e53
JS
16262 spin_unlock_irq(&phba->hbalock);
16263 return xri;
16264}
16265
16266/**
16267 * lpfc_sli4_free_xri - Release an xri for reuse.
16268 * @phba: pointer to lpfc hba data structure.
16269 *
16270 * This routine is invoked to release an xri to the pool of
16271 * available rpis maintained by the driver.
16272 **/
5d8b8167 16273static void
6d368e53
JS
16274__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16275{
16276 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
16277 phba->sli4_hba.max_cfg_param.xri_used--;
16278 }
16279}
16280
16281/**
16282 * lpfc_sli4_free_xri - Release an xri for reuse.
16283 * @phba: pointer to lpfc hba data structure.
16284 *
16285 * This routine is invoked to release an xri to the pool of
16286 * available rpis maintained by the driver.
16287 **/
16288void
16289lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16290{
16291 spin_lock_irq(&phba->hbalock);
16292 __lpfc_sli4_free_xri(phba, xri);
16293 spin_unlock_irq(&phba->hbalock);
16294}
16295
4f774513
JS
16296/**
16297 * lpfc_sli4_next_xritag - Get an xritag for the io
16298 * @phba: Pointer to HBA context object.
16299 *
16300 * This function gets an xritag for the iocb. If there is no unused xritag
16301 * it will return 0xffff.
16302 * The function returns the allocated xritag if successful, else returns zero.
16303 * Zero is not a valid xritag.
16304 * The caller is not required to hold any lock.
16305 **/
16306uint16_t
16307lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16308{
6d368e53 16309 uint16_t xri_index;
4f774513 16310
6d368e53 16311 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16312 if (xri_index == NO_XRI)
16313 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16314 "2004 Failed to allocate XRI.last XRITAG is %d"
16315 " Max XRI is %d, Used XRI is %d\n",
16316 xri_index,
16317 phba->sli4_hba.max_cfg_param.max_xri,
16318 phba->sli4_hba.max_cfg_param.xri_used);
16319 return xri_index;
4f774513
JS
16320}
16321
16322/**
895427bd 16323 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16324 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16325 * @post_sgl_list: pointer to els sgl entry list.
16326 * @count: number of els sgl entries on the list.
4f774513
JS
16327 *
16328 * This routine is invoked to post a block of driver's sgl pages to the
16329 * HBA using non-embedded mailbox command. No Lock is held. This routine
16330 * is only called when the driver is loading and after all IO has been
16331 * stopped.
16332 **/
8a9d2e80 16333static int
895427bd 16334lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16335 struct list_head *post_sgl_list,
16336 int post_cnt)
4f774513 16337{
8a9d2e80 16338 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16339 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16340 struct sgl_page_pairs *sgl_pg_pairs;
16341 void *viraddr;
16342 LPFC_MBOXQ_t *mbox;
16343 uint32_t reqlen, alloclen, pg_pairs;
16344 uint32_t mbox_tmo;
8a9d2e80
JS
16345 uint16_t xritag_start = 0;
16346 int rc = 0;
4f774513
JS
16347 uint32_t shdr_status, shdr_add_status;
16348 union lpfc_sli4_cfg_shdr *shdr;
16349
895427bd 16350 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16351 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16352 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16354 "2559 Block sgl registration required DMA "
16355 "size (%d) great than a page\n", reqlen);
16356 return -ENOMEM;
16357 }
895427bd 16358
4f774513 16359 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16360 if (!mbox)
4f774513 16361 return -ENOMEM;
4f774513
JS
16362
16363 /* Allocate DMA memory and set up the non-embedded mailbox command */
16364 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16365 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16366 LPFC_SLI4_MBX_NEMBED);
16367
16368 if (alloclen < reqlen) {
16369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16370 "0285 Allocated DMA memory size (%d) is "
16371 "less than the requested DMA memory "
16372 "size (%d)\n", alloclen, reqlen);
16373 lpfc_sli4_mbox_cmd_free(phba, mbox);
16374 return -ENOMEM;
16375 }
4f774513 16376 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16377 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16378 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16379 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16380
8a9d2e80
JS
16381 pg_pairs = 0;
16382 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16383 /* Set up the sge entry */
16384 sgl_pg_pairs->sgl_pg0_addr_lo =
16385 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16386 sgl_pg_pairs->sgl_pg0_addr_hi =
16387 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16388 sgl_pg_pairs->sgl_pg1_addr_lo =
16389 cpu_to_le32(putPaddrLow(0));
16390 sgl_pg_pairs->sgl_pg1_addr_hi =
16391 cpu_to_le32(putPaddrHigh(0));
6d368e53 16392
4f774513
JS
16393 /* Keep the first xritag on the list */
16394 if (pg_pairs == 0)
16395 xritag_start = sglq_entry->sli4_xritag;
16396 sgl_pg_pairs++;
8a9d2e80 16397 pg_pairs++;
4f774513 16398 }
6d368e53
JS
16399
16400 /* Complete initialization and perform endian conversion. */
4f774513 16401 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16402 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16403 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16404
4f774513
JS
16405 if (!phba->sli4_hba.intr_enable)
16406 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16407 else {
a183a15f 16408 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16409 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16410 }
16411 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16412 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16413 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16414 if (rc != MBX_TIMEOUT)
16415 lpfc_sli4_mbox_cmd_free(phba, mbox);
16416 if (shdr_status || shdr_add_status || rc) {
16417 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16418 "2513 POST_SGL_BLOCK mailbox command failed "
16419 "status x%x add_status x%x mbx status x%x\n",
16420 shdr_status, shdr_add_status, rc);
16421 rc = -ENXIO;
16422 }
16423 return rc;
16424}
16425
16426/**
16427 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16428 * @phba: pointer to lpfc hba data structure.
16429 * @sblist: pointer to scsi buffer list.
16430 * @count: number of scsi buffers on the list.
16431 *
16432 * This routine is invoked to post a block of @count scsi sgl pages from a
16433 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16434 * No Lock is held.
16435 *
16436 **/
16437int
8a9d2e80
JS
16438lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16439 struct list_head *sblist,
16440 int count)
4f774513
JS
16441{
16442 struct lpfc_scsi_buf *psb;
16443 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16444 struct sgl_page_pairs *sgl_pg_pairs;
16445 void *viraddr;
16446 LPFC_MBOXQ_t *mbox;
16447 uint32_t reqlen, alloclen, pg_pairs;
16448 uint32_t mbox_tmo;
16449 uint16_t xritag_start = 0;
16450 int rc = 0;
16451 uint32_t shdr_status, shdr_add_status;
16452 dma_addr_t pdma_phys_bpl1;
16453 union lpfc_sli4_cfg_shdr *shdr;
16454
16455 /* Calculate the requested length of the dma memory */
8a9d2e80 16456 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16457 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16458 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16459 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16460 "0217 Block sgl registration required DMA "
16461 "size (%d) great than a page\n", reqlen);
16462 return -ENOMEM;
16463 }
16464 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16465 if (!mbox) {
16466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16467 "0283 Failed to allocate mbox cmd memory\n");
16468 return -ENOMEM;
16469 }
16470
16471 /* Allocate DMA memory and set up the non-embedded mailbox command */
16472 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16473 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16474 LPFC_SLI4_MBX_NEMBED);
16475
16476 if (alloclen < reqlen) {
16477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16478 "2561 Allocated DMA memory size (%d) is "
16479 "less than the requested DMA memory "
16480 "size (%d)\n", alloclen, reqlen);
16481 lpfc_sli4_mbox_cmd_free(phba, mbox);
16482 return -ENOMEM;
16483 }
6d368e53 16484
4f774513 16485 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16486 viraddr = mbox->sge_array->addr[0];
16487
16488 /* Set up the SGL pages in the non-embedded DMA pages */
16489 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16490 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16491
16492 pg_pairs = 0;
16493 list_for_each_entry(psb, sblist, list) {
16494 /* Set up the sge entry */
16495 sgl_pg_pairs->sgl_pg0_addr_lo =
16496 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16497 sgl_pg_pairs->sgl_pg0_addr_hi =
16498 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16499 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16500 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16501 else
16502 pdma_phys_bpl1 = 0;
16503 sgl_pg_pairs->sgl_pg1_addr_lo =
16504 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16505 sgl_pg_pairs->sgl_pg1_addr_hi =
16506 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16507 /* Keep the first xritag on the list */
16508 if (pg_pairs == 0)
16509 xritag_start = psb->cur_iocbq.sli4_xritag;
16510 sgl_pg_pairs++;
16511 pg_pairs++;
16512 }
16513 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16514 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16515 /* Perform endian conversion if necessary */
16516 sgl->word0 = cpu_to_le32(sgl->word0);
16517
16518 if (!phba->sli4_hba.intr_enable)
16519 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16520 else {
a183a15f 16521 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16522 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16523 }
16524 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16525 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16526 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16527 if (rc != MBX_TIMEOUT)
16528 lpfc_sli4_mbox_cmd_free(phba, mbox);
16529 if (shdr_status || shdr_add_status || rc) {
16530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16531 "2564 POST_SGL_BLOCK mailbox command failed "
16532 "status x%x add_status x%x mbx status x%x\n",
16533 shdr_status, shdr_add_status, rc);
16534 rc = -ENXIO;
16535 }
16536 return rc;
16537}
16538
16539/**
16540 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16541 * @phba: pointer to lpfc_hba struct that the frame was received on
16542 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16543 *
16544 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16545 * valid type of frame that the LPFC driver will handle. This function will
16546 * return a zero if the frame is a valid frame or a non zero value when the
16547 * frame does not pass the check.
16548 **/
16549static int
16550lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16551{
474ffb74 16552 /* make rctl_names static to save stack space */
4f774513 16553 struct fc_vft_header *fc_vft_hdr;
546fc854 16554 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16555
ae9e28f3
JS
16556#define FC_RCTL_MDS_DIAGS 0xF4
16557
4f774513
JS
16558 switch (fc_hdr->fh_r_ctl) {
16559 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16560 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16561 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16562 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16563 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16564 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16565 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16566 case FC_RCTL_DD_CMD_STATUS: /* command status */
16567 case FC_RCTL_ELS_REQ: /* extended link services request */
16568 case FC_RCTL_ELS_REP: /* extended link services reply */
16569 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16570 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16571 case FC_RCTL_BA_NOP: /* basic link service NOP */
16572 case FC_RCTL_BA_ABTS: /* basic link service abort */
16573 case FC_RCTL_BA_RMC: /* remove connection */
16574 case FC_RCTL_BA_ACC: /* basic accept */
16575 case FC_RCTL_BA_RJT: /* basic reject */
16576 case FC_RCTL_BA_PRMT:
16577 case FC_RCTL_ACK_1: /* acknowledge_1 */
16578 case FC_RCTL_ACK_0: /* acknowledge_0 */
16579 case FC_RCTL_P_RJT: /* port reject */
16580 case FC_RCTL_F_RJT: /* fabric reject */
16581 case FC_RCTL_P_BSY: /* port busy */
16582 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16583 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16584 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16585 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16586 case FC_RCTL_END: /* end */
16587 break;
16588 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16589 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16590 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16591 return lpfc_fc_frame_check(phba, fc_hdr);
16592 default:
16593 goto drop;
16594 }
ae9e28f3
JS
16595
16596#define FC_TYPE_VENDOR_UNIQUE 0xFF
16597
4f774513
JS
16598 switch (fc_hdr->fh_type) {
16599 case FC_TYPE_BLS:
16600 case FC_TYPE_ELS:
16601 case FC_TYPE_FCP:
16602 case FC_TYPE_CT:
895427bd 16603 case FC_TYPE_NVME:
ae9e28f3 16604 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16605 break;
16606 case FC_TYPE_IP:
16607 case FC_TYPE_ILS:
16608 default:
16609 goto drop;
16610 }
546fc854 16611
4f774513 16612 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16613 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16614 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16615 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16616 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16617 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16618 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16619 be32_to_cpu(header[6]));
4f774513
JS
16620 return 0;
16621drop:
16622 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16623 "2539 Dropped frame rctl:x%x type:x%x\n",
16624 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16625 return 1;
16626}
16627
16628/**
16629 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16630 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16631 *
16632 * This function processes the FC header to retrieve the VFI from the VF
16633 * header, if one exists. This function will return the VFI if one exists
16634 * or 0 if no VSAN Header exists.
16635 **/
16636static uint32_t
16637lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16638{
16639 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16640
16641 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16642 return 0;
16643 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16644}
16645
16646/**
16647 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16648 * @phba: Pointer to the HBA structure to search for the vport on
16649 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16650 * @fcfi: The FC Fabric ID that the frame came from
16651 *
16652 * This function searches the @phba for a vport that matches the content of the
16653 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16654 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16655 * returns the matching vport pointer or NULL if unable to match frame to a
16656 * vport.
16657 **/
16658static struct lpfc_vport *
16659lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16660 uint16_t fcfi, uint32_t did)
4f774513
JS
16661{
16662 struct lpfc_vport **vports;
16663 struct lpfc_vport *vport = NULL;
16664 int i;
939723a4 16665
bf08611b
JS
16666 if (did == Fabric_DID)
16667 return phba->pport;
939723a4
JS
16668 if ((phba->pport->fc_flag & FC_PT2PT) &&
16669 !(phba->link_state == LPFC_HBA_READY))
16670 return phba->pport;
16671
4f774513 16672 vports = lpfc_create_vport_work_array(phba);
895427bd 16673 if (vports != NULL) {
4f774513
JS
16674 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16675 if (phba->fcf.fcfi == fcfi &&
16676 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16677 vports[i]->fc_myDID == did) {
16678 vport = vports[i];
16679 break;
16680 }
16681 }
895427bd 16682 }
4f774513
JS
16683 lpfc_destroy_vport_work_array(phba, vports);
16684 return vport;
16685}
16686
45ed1190
JS
16687/**
16688 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16689 * @vport: The vport to work on.
16690 *
16691 * This function updates the receive sequence time stamp for this vport. The
16692 * receive sequence time stamp indicates the time that the last frame of the
16693 * the sequence that has been idle for the longest amount of time was received.
16694 * the driver uses this time stamp to indicate if any received sequences have
16695 * timed out.
16696 **/
5d8b8167 16697static void
45ed1190
JS
16698lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16699{
16700 struct lpfc_dmabuf *h_buf;
16701 struct hbq_dmabuf *dmabuf = NULL;
16702
16703 /* get the oldest sequence on the rcv list */
16704 h_buf = list_get_first(&vport->rcv_buffer_list,
16705 struct lpfc_dmabuf, list);
16706 if (!h_buf)
16707 return;
16708 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16709 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16710}
16711
16712/**
16713 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16714 * @vport: The vport that the received sequences were sent to.
16715 *
16716 * This function cleans up all outstanding received sequences. This is called
16717 * by the driver when a link event or user action invalidates all the received
16718 * sequences.
16719 **/
16720void
16721lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16722{
16723 struct lpfc_dmabuf *h_buf, *hnext;
16724 struct lpfc_dmabuf *d_buf, *dnext;
16725 struct hbq_dmabuf *dmabuf = NULL;
16726
16727 /* start with the oldest sequence on the rcv list */
16728 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16729 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16730 list_del_init(&dmabuf->hbuf.list);
16731 list_for_each_entry_safe(d_buf, dnext,
16732 &dmabuf->dbuf.list, list) {
16733 list_del_init(&d_buf->list);
16734 lpfc_in_buf_free(vport->phba, d_buf);
16735 }
16736 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16737 }
16738}
16739
16740/**
16741 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16742 * @vport: The vport that the received sequences were sent to.
16743 *
16744 * This function determines whether any received sequences have timed out by
16745 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16746 * indicates that there is at least one timed out sequence this routine will
16747 * go through the received sequences one at a time from most inactive to most
16748 * active to determine which ones need to be cleaned up. Once it has determined
16749 * that a sequence needs to be cleaned up it will simply free up the resources
16750 * without sending an abort.
16751 **/
16752void
16753lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16754{
16755 struct lpfc_dmabuf *h_buf, *hnext;
16756 struct lpfc_dmabuf *d_buf, *dnext;
16757 struct hbq_dmabuf *dmabuf = NULL;
16758 unsigned long timeout;
16759 int abort_count = 0;
16760
16761 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16762 vport->rcv_buffer_time_stamp);
16763 if (list_empty(&vport->rcv_buffer_list) ||
16764 time_before(jiffies, timeout))
16765 return;
16766 /* start with the oldest sequence on the rcv list */
16767 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16768 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16769 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16770 dmabuf->time_stamp);
16771 if (time_before(jiffies, timeout))
16772 break;
16773 abort_count++;
16774 list_del_init(&dmabuf->hbuf.list);
16775 list_for_each_entry_safe(d_buf, dnext,
16776 &dmabuf->dbuf.list, list) {
16777 list_del_init(&d_buf->list);
16778 lpfc_in_buf_free(vport->phba, d_buf);
16779 }
16780 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16781 }
16782 if (abort_count)
16783 lpfc_update_rcv_time_stamp(vport);
16784}
16785
4f774513
JS
16786/**
16787 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16788 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16789 *
16790 * This function searches through the existing incomplete sequences that have
16791 * been sent to this @vport. If the frame matches one of the incomplete
16792 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16793 * make up that sequence. If no sequence is found that matches this frame then
16794 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16795 * This function returns a pointer to the first dmabuf in the sequence list that
16796 * the frame was linked to.
16797 **/
16798static struct hbq_dmabuf *
16799lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16800{
16801 struct fc_frame_header *new_hdr;
16802 struct fc_frame_header *temp_hdr;
16803 struct lpfc_dmabuf *d_buf;
16804 struct lpfc_dmabuf *h_buf;
16805 struct hbq_dmabuf *seq_dmabuf = NULL;
16806 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16807 uint8_t found = 0;
4f774513 16808
4d9ab994 16809 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16810 dmabuf->time_stamp = jiffies;
4f774513 16811 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16812
4f774513
JS
16813 /* Use the hdr_buf to find the sequence that this frame belongs to */
16814 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16815 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16816 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16817 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16818 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16819 continue;
16820 /* found a pending sequence that matches this frame */
16821 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16822 break;
16823 }
16824 if (!seq_dmabuf) {
16825 /*
16826 * This indicates first frame received for this sequence.
16827 * Queue the buffer on the vport's rcv_buffer_list.
16828 */
16829 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16830 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16831 return dmabuf;
16832 }
16833 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16834 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16835 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16836 list_del_init(&seq_dmabuf->hbuf.list);
16837 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16838 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16839 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16840 return dmabuf;
16841 }
45ed1190
JS
16842 /* move this sequence to the tail to indicate a young sequence */
16843 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16844 seq_dmabuf->time_stamp = jiffies;
16845 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16846 if (list_empty(&seq_dmabuf->dbuf.list)) {
16847 temp_hdr = dmabuf->hbuf.virt;
16848 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16849 return seq_dmabuf;
16850 }
4f774513 16851 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16852 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16853 while (!found) {
4f774513
JS
16854 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16855 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16856 /*
16857 * If the frame's sequence count is greater than the frame on
16858 * the list then insert the frame right after this frame
16859 */
eeead811
JS
16860 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16861 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16862 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16863 found = 1;
16864 break;
4f774513 16865 }
4360ca9c
JS
16866
16867 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16868 break;
16869 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16870 }
4360ca9c
JS
16871
16872 if (found)
16873 return seq_dmabuf;
4f774513
JS
16874 return NULL;
16875}
16876
6669f9bb
JS
16877/**
16878 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16879 * @vport: pointer to a vitural port
16880 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16881 *
16882 * This function tries to abort from the partially assembed sequence, described
16883 * by the information from basic abbort @dmabuf. It checks to see whether such
16884 * partially assembled sequence held by the driver. If so, it shall free up all
16885 * the frames from the partially assembled sequence.
16886 *
16887 * Return
16888 * true -- if there is matching partially assembled sequence present and all
16889 * the frames freed with the sequence;
16890 * false -- if there is no matching partially assembled sequence present so
16891 * nothing got aborted in the lower layer driver
16892 **/
16893static bool
16894lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16895 struct hbq_dmabuf *dmabuf)
16896{
16897 struct fc_frame_header *new_hdr;
16898 struct fc_frame_header *temp_hdr;
16899 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16900 struct hbq_dmabuf *seq_dmabuf = NULL;
16901
16902 /* Use the hdr_buf to find the sequence that matches this frame */
16903 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16904 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16905 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16906 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16907 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16908 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16909 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16910 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16911 continue;
16912 /* found a pending sequence that matches this frame */
16913 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16914 break;
16915 }
16916
16917 /* Free up all the frames from the partially assembled sequence */
16918 if (seq_dmabuf) {
16919 list_for_each_entry_safe(d_buf, n_buf,
16920 &seq_dmabuf->dbuf.list, list) {
16921 list_del_init(&d_buf->list);
16922 lpfc_in_buf_free(vport->phba, d_buf);
16923 }
16924 return true;
16925 }
16926 return false;
16927}
16928
6dd9e31c
JS
16929/**
16930 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16931 * @vport: pointer to a vitural port
16932 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16933 *
16934 * This function tries to abort from the assembed sequence from upper level
16935 * protocol, described by the information from basic abbort @dmabuf. It
16936 * checks to see whether such pending context exists at upper level protocol.
16937 * If so, it shall clean up the pending context.
16938 *
16939 * Return
16940 * true -- if there is matching pending context of the sequence cleaned
16941 * at ulp;
16942 * false -- if there is no matching pending context of the sequence present
16943 * at ulp.
16944 **/
16945static bool
16946lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16947{
16948 struct lpfc_hba *phba = vport->phba;
16949 int handled;
16950
16951 /* Accepting abort at ulp with SLI4 only */
16952 if (phba->sli_rev < LPFC_SLI_REV4)
16953 return false;
16954
16955 /* Register all caring upper level protocols to attend abort */
16956 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16957 if (handled)
16958 return true;
16959
16960 return false;
16961}
16962
6669f9bb 16963/**
546fc854 16964 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16965 * @phba: Pointer to HBA context object.
16966 * @cmd_iocbq: pointer to the command iocbq structure.
16967 * @rsp_iocbq: pointer to the response iocbq structure.
16968 *
546fc854 16969 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16970 * event. It properly releases the memory allocated to the sequence abort
16971 * accept iocb.
16972 **/
16973static void
546fc854 16974lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16975 struct lpfc_iocbq *cmd_iocbq,
16976 struct lpfc_iocbq *rsp_iocbq)
16977{
6dd9e31c
JS
16978 struct lpfc_nodelist *ndlp;
16979
16980 if (cmd_iocbq) {
16981 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16982 lpfc_nlp_put(ndlp);
16983 lpfc_nlp_not_used(ndlp);
6669f9bb 16984 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16985 }
6b5151fd
JS
16986
16987 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16988 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16990 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16991 rsp_iocbq->iocb.ulpStatus,
16992 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16993}
16994
6d368e53
JS
16995/**
16996 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16997 * @phba: Pointer to HBA context object.
16998 * @xri: xri id in transaction.
16999 *
17000 * This function validates the xri maps to the known range of XRIs allocated an
17001 * used by the driver.
17002 **/
7851fe2c 17003uint16_t
6d368e53
JS
17004lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17005 uint16_t xri)
17006{
a2fc4aef 17007 uint16_t i;
6d368e53
JS
17008
17009 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17010 if (xri == phba->sli4_hba.xri_ids[i])
17011 return i;
17012 }
17013 return NO_XRI;
17014}
17015
6669f9bb 17016/**
546fc854 17017 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
17018 * @phba: Pointer to HBA context object.
17019 * @fc_hdr: pointer to a FC frame header.
17020 *
546fc854 17021 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
17022 * event after aborting the sequence handling.
17023 **/
86c67379 17024void
6dd9e31c
JS
17025lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17026 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 17027{
6dd9e31c 17028 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
17029 struct lpfc_iocbq *ctiocb = NULL;
17030 struct lpfc_nodelist *ndlp;
ee0f4fe1 17031 uint16_t oxid, rxid, xri, lxri;
5ffc266e 17032 uint32_t sid, fctl;
6669f9bb 17033 IOCB_t *icmd;
546fc854 17034 int rc;
6669f9bb
JS
17035
17036 if (!lpfc_is_link_up(phba))
17037 return;
17038
17039 sid = sli4_sid_from_fc_hdr(fc_hdr);
17040 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 17041 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 17042
6dd9e31c 17043 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 17044 if (!ndlp) {
9d3d340d 17045 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
17046 if (!ndlp) {
17047 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17048 "1268 Failed to allocate ndlp for "
17049 "oxid:x%x SID:x%x\n", oxid, sid);
17050 return;
17051 }
6dd9e31c
JS
17052 /* Put ndlp onto pport node list */
17053 lpfc_enqueue_node(vport, ndlp);
17054 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17055 /* re-setup ndlp without removing from node list */
17056 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17057 if (!ndlp) {
17058 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17059 "3275 Failed to active ndlp found "
17060 "for oxid:x%x SID:x%x\n", oxid, sid);
17061 return;
17062 }
6669f9bb
JS
17063 }
17064
546fc854 17065 /* Allocate buffer for rsp iocb */
6669f9bb
JS
17066 ctiocb = lpfc_sli_get_iocbq(phba);
17067 if (!ctiocb)
17068 return;
17069
5ffc266e
JS
17070 /* Extract the F_CTL field from FC_HDR */
17071 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17072
6669f9bb 17073 icmd = &ctiocb->iocb;
6669f9bb 17074 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 17075 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
17076 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17077 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17078 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17079
17080 /* Fill in the rest of iocb fields */
17081 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17082 icmd->ulpBdeCount = 0;
17083 icmd->ulpLe = 1;
17084 icmd->ulpClass = CLASS3;
6d368e53 17085 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 17086 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 17087
6669f9bb
JS
17088 ctiocb->iocb_cmpl = NULL;
17089 ctiocb->vport = phba->pport;
546fc854 17090 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 17091 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
17092 ctiocb->sli4_xritag = NO_XRI;
17093
ee0f4fe1
JS
17094 if (fctl & FC_FC_EX_CTX)
17095 /* Exchange responder sent the abort so we
17096 * own the oxid.
17097 */
17098 xri = oxid;
17099 else
17100 xri = rxid;
17101 lxri = lpfc_sli4_xri_inrange(phba, xri);
17102 if (lxri != NO_XRI)
17103 lpfc_set_rrq_active(phba, ndlp, lxri,
17104 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
17105 /* For BA_ABTS from exchange responder, if the logical xri with
17106 * the oxid maps to the FCP XRI range, the port no longer has
17107 * that exchange context, send a BLS_RJT. Override the IOCB for
17108 * a BA_RJT.
17109 */
17110 if ((fctl & FC_FC_EX_CTX) &&
895427bd 17111 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
17112 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17113 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17114 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17115 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17116 }
17117
17118 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17119 * the driver no longer has that exchange, send a BLS_RJT. Override
17120 * the IOCB for a BA_RJT.
546fc854 17121 */
6dd9e31c 17122 if (aborted == false) {
546fc854
JS
17123 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17124 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17125 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17126 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17127 }
6669f9bb 17128
5ffc266e
JS
17129 if (fctl & FC_FC_EX_CTX) {
17130 /* ABTS sent by responder to CT exchange, construction
17131 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17132 * field and RX_ID from ABTS for RX_ID field.
17133 */
546fc854 17134 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
17135 } else {
17136 /* ABTS sent by initiator to CT exchange, construction
17137 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 17138 * XRI_TAG field.
5ffc266e 17139 */
546fc854 17140 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 17141 }
f09c3acc 17142 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 17143 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 17144
546fc854 17145 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
17146 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17147 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17148 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
17149
17150 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17151 if (rc == IOCB_ERROR) {
6dd9e31c
JS
17152 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17153 "2925 Failed to issue CT ABTS RSP x%x on "
17154 "xri x%x, Data x%x\n",
17155 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17156 phba->link_state);
17157 lpfc_nlp_put(ndlp);
17158 ctiocb->context1 = NULL;
546fc854
JS
17159 lpfc_sli_release_iocbq(phba, ctiocb);
17160 }
6669f9bb
JS
17161}
17162
17163/**
17164 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17165 * @vport: Pointer to the vport on which this sequence was received
17166 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17167 *
17168 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17169 * receive sequence is only partially assembed by the driver, it shall abort
17170 * the partially assembled frames for the sequence. Otherwise, if the
17171 * unsolicited receive sequence has been completely assembled and passed to
17172 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17173 * unsolicited sequence has been aborted. After that, it will issue a basic
17174 * accept to accept the abort.
17175 **/
5d8b8167 17176static void
6669f9bb
JS
17177lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17178 struct hbq_dmabuf *dmabuf)
17179{
17180 struct lpfc_hba *phba = vport->phba;
17181 struct fc_frame_header fc_hdr;
5ffc266e 17182 uint32_t fctl;
6dd9e31c 17183 bool aborted;
6669f9bb 17184
6669f9bb
JS
17185 /* Make a copy of fc_hdr before the dmabuf being released */
17186 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 17187 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 17188
5ffc266e 17189 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
17190 /* ABTS by responder to exchange, no cleanup needed */
17191 aborted = true;
5ffc266e 17192 } else {
6dd9e31c
JS
17193 /* ABTS by initiator to exchange, need to do cleanup */
17194 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17195 if (aborted == false)
17196 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 17197 }
6dd9e31c
JS
17198 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17199
86c67379
JS
17200 if (phba->nvmet_support) {
17201 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17202 return;
17203 }
17204
6dd9e31c
JS
17205 /* Respond with BA_ACC or BA_RJT accordingly */
17206 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
17207}
17208
4f774513
JS
17209/**
17210 * lpfc_seq_complete - Indicates if a sequence is complete
17211 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17212 *
17213 * This function checks the sequence, starting with the frame described by
17214 * @dmabuf, to see if all the frames associated with this sequence are present.
17215 * the frames associated with this sequence are linked to the @dmabuf using the
17216 * dbuf list. This function looks for two major things. 1) That the first frame
17217 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17218 * set. 3) That there are no holes in the sequence count. The function will
17219 * return 1 when the sequence is complete, otherwise it will return 0.
17220 **/
17221static int
17222lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17223{
17224 struct fc_frame_header *hdr;
17225 struct lpfc_dmabuf *d_buf;
17226 struct hbq_dmabuf *seq_dmabuf;
17227 uint32_t fctl;
17228 int seq_count = 0;
17229
17230 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17231 /* make sure first fame of sequence has a sequence count of zero */
17232 if (hdr->fh_seq_cnt != seq_count)
17233 return 0;
17234 fctl = (hdr->fh_f_ctl[0] << 16 |
17235 hdr->fh_f_ctl[1] << 8 |
17236 hdr->fh_f_ctl[2]);
17237 /* If last frame of sequence we can return success. */
17238 if (fctl & FC_FC_END_SEQ)
17239 return 1;
17240 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17241 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17242 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17243 /* If there is a hole in the sequence count then fail. */
eeead811 17244 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
17245 return 0;
17246 fctl = (hdr->fh_f_ctl[0] << 16 |
17247 hdr->fh_f_ctl[1] << 8 |
17248 hdr->fh_f_ctl[2]);
17249 /* If last frame of sequence we can return success. */
17250 if (fctl & FC_FC_END_SEQ)
17251 return 1;
17252 }
17253 return 0;
17254}
17255
17256/**
17257 * lpfc_prep_seq - Prep sequence for ULP processing
17258 * @vport: Pointer to the vport on which this sequence was received
17259 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17260 *
17261 * This function takes a sequence, described by a list of frames, and creates
17262 * a list of iocbq structures to describe the sequence. This iocbq list will be
17263 * used to issue to the generic unsolicited sequence handler. This routine
17264 * returns a pointer to the first iocbq in the list. If the function is unable
17265 * to allocate an iocbq then it throw out the received frames that were not
17266 * able to be described and return a pointer to the first iocbq. If unable to
17267 * allocate any iocbqs (including the first) this function will return NULL.
17268 **/
17269static struct lpfc_iocbq *
17270lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17271{
7851fe2c 17272 struct hbq_dmabuf *hbq_buf;
4f774513
JS
17273 struct lpfc_dmabuf *d_buf, *n_buf;
17274 struct lpfc_iocbq *first_iocbq, *iocbq;
17275 struct fc_frame_header *fc_hdr;
17276 uint32_t sid;
7851fe2c 17277 uint32_t len, tot_len;
eeead811 17278 struct ulp_bde64 *pbde;
4f774513
JS
17279
17280 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17281 /* remove from receive buffer list */
17282 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 17283 lpfc_update_rcv_time_stamp(vport);
4f774513 17284 /* get the Remote Port's SID */
6669f9bb 17285 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 17286 tot_len = 0;
4f774513
JS
17287 /* Get an iocbq struct to fill in. */
17288 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17289 if (first_iocbq) {
17290 /* Initialize the first IOCB. */
8fa38513 17291 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 17292 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 17293 first_iocbq->vport = vport;
939723a4
JS
17294
17295 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17296 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17297 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17298 first_iocbq->iocb.un.rcvels.parmRo =
17299 sli4_did_from_fc_hdr(fc_hdr);
17300 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17301 } else
17302 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17303 first_iocbq->iocb.ulpContext = NO_XRI;
17304 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17305 be16_to_cpu(fc_hdr->fh_ox_id);
17306 /* iocbq is prepped for internal consumption. Physical vpi. */
17307 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17308 vport->phba->vpi_ids[vport->vpi];
4f774513 17309 /* put the first buffer into the first IOCBq */
48a5a664
JS
17310 tot_len = bf_get(lpfc_rcqe_length,
17311 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17312
4f774513
JS
17313 first_iocbq->context2 = &seq_dmabuf->dbuf;
17314 first_iocbq->context3 = NULL;
17315 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17316 if (tot_len > LPFC_DATA_BUF_SIZE)
17317 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17318 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17319 else
17320 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17321
4f774513 17322 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17323
7851fe2c 17324 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17325 }
17326 iocbq = first_iocbq;
17327 /*
17328 * Each IOCBq can have two Buffers assigned, so go through the list
17329 * of buffers for this sequence and save two buffers in each IOCBq
17330 */
17331 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17332 if (!iocbq) {
17333 lpfc_in_buf_free(vport->phba, d_buf);
17334 continue;
17335 }
17336 if (!iocbq->context3) {
17337 iocbq->context3 = d_buf;
17338 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17339 /* We need to get the size out of the right CQE */
17340 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17341 len = bf_get(lpfc_rcqe_length,
17342 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17343 pbde = (struct ulp_bde64 *)
17344 &iocbq->iocb.unsli3.sli3Words[4];
17345 if (len > LPFC_DATA_BUF_SIZE)
17346 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17347 else
17348 pbde->tus.f.bdeSize = len;
17349
7851fe2c
JS
17350 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17351 tot_len += len;
4f774513
JS
17352 } else {
17353 iocbq = lpfc_sli_get_iocbq(vport->phba);
17354 if (!iocbq) {
17355 if (first_iocbq) {
17356 first_iocbq->iocb.ulpStatus =
17357 IOSTAT_FCP_RSP_ERROR;
17358 first_iocbq->iocb.un.ulpWord[4] =
17359 IOERR_NO_RESOURCES;
17360 }
17361 lpfc_in_buf_free(vport->phba, d_buf);
17362 continue;
17363 }
48a5a664
JS
17364 /* We need to get the size out of the right CQE */
17365 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17366 len = bf_get(lpfc_rcqe_length,
17367 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17368 iocbq->context2 = d_buf;
17369 iocbq->context3 = NULL;
17370 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17371 if (len > LPFC_DATA_BUF_SIZE)
17372 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17373 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17374 else
17375 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17376
7851fe2c
JS
17377 tot_len += len;
17378 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17379
4f774513
JS
17380 iocbq->iocb.un.rcvels.remoteID = sid;
17381 list_add_tail(&iocbq->list, &first_iocbq->list);
17382 }
17383 }
17384 return first_iocbq;
17385}
17386
6669f9bb
JS
17387static void
17388lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17389 struct hbq_dmabuf *seq_dmabuf)
17390{
17391 struct fc_frame_header *fc_hdr;
17392 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17393 struct lpfc_hba *phba = vport->phba;
17394
17395 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17396 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17397 if (!iocbq) {
17398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17399 "2707 Ring %d handler: Failed to allocate "
17400 "iocb Rctl x%x Type x%x received\n",
17401 LPFC_ELS_RING,
17402 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17403 return;
17404 }
17405 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17406 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17407 iocbq, fc_hdr->fh_r_ctl,
17408 fc_hdr->fh_type))
6d368e53 17409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17410 "2540 Ring %d handler: unexpected Rctl "
17411 "x%x Type x%x received\n",
17412 LPFC_ELS_RING,
17413 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17414
17415 /* Free iocb created in lpfc_prep_seq */
17416 list_for_each_entry_safe(curr_iocb, next_iocb,
17417 &iocbq->list, list) {
17418 list_del_init(&curr_iocb->list);
17419 lpfc_sli_release_iocbq(phba, curr_iocb);
17420 }
17421 lpfc_sli_release_iocbq(phba, iocbq);
17422}
17423
ae9e28f3
JS
17424static void
17425lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17426 struct lpfc_iocbq *rspiocb)
17427{
17428 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17429
17430 if (pcmd && pcmd->virt)
771db5c0 17431 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17432 kfree(pcmd);
17433 lpfc_sli_release_iocbq(phba, cmdiocb);
17434}
17435
17436static void
17437lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17438 struct hbq_dmabuf *dmabuf)
17439{
17440 struct fc_frame_header *fc_hdr;
17441 struct lpfc_hba *phba = vport->phba;
17442 struct lpfc_iocbq *iocbq = NULL;
17443 union lpfc_wqe *wqe;
17444 struct lpfc_dmabuf *pcmd = NULL;
17445 uint32_t frame_len;
17446 int rc;
17447
17448 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17449 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17450
17451 /* Send the received frame back */
17452 iocbq = lpfc_sli_get_iocbq(phba);
17453 if (!iocbq)
17454 goto exit;
17455
17456 /* Allocate buffer for command payload */
17457 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17458 if (pcmd)
771db5c0 17459 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17460 &pcmd->phys);
17461 if (!pcmd || !pcmd->virt)
17462 goto exit;
17463
17464 INIT_LIST_HEAD(&pcmd->list);
17465
17466 /* copyin the payload */
17467 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17468
17469 /* fill in BDE's for command */
17470 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17471 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17472 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17473 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17474
17475 iocbq->context2 = pcmd;
17476 iocbq->vport = vport;
17477 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17478 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17479
17480 /*
17481 * Setup rest of the iocb as though it were a WQE
17482 * Build the SEND_FRAME WQE
17483 */
17484 wqe = (union lpfc_wqe *)&iocbq->iocb;
17485
17486 wqe->send_frame.frame_len = frame_len;
17487 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17488 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17489 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17490 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17491 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17492 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17493
17494 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17495 iocbq->iocb.ulpLe = 1;
17496 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17497 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17498 if (rc == IOCB_ERROR)
17499 goto exit;
17500
17501 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17502 return;
17503
17504exit:
17505 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17506 "2023 Unable to process MDS loopback frame\n");
17507 if (pcmd && pcmd->virt)
771db5c0 17508 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17509 kfree(pcmd);
401bb416
DK
17510 if (iocbq)
17511 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17512 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17513}
17514
4f774513
JS
17515/**
17516 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17517 * @phba: Pointer to HBA context object.
17518 *
17519 * This function is called with no lock held. This function processes all
17520 * the received buffers and gives it to upper layers when a received buffer
17521 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17522 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17523 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17524 * appropriate receive function when the final frame in a sequence is received.
17525 **/
4d9ab994
JS
17526void
17527lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17528 struct hbq_dmabuf *dmabuf)
4f774513 17529{
4d9ab994 17530 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17531 struct fc_frame_header *fc_hdr;
17532 struct lpfc_vport *vport;
17533 uint32_t fcfi;
939723a4 17534 uint32_t did;
4f774513 17535
4f774513 17536 /* Process each received buffer */
4d9ab994 17537 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17538
4d9ab994
JS
17539 /* check to see if this a valid type of frame */
17540 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17541 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17542 return;
17543 }
2ea259ee 17544
7851fe2c
JS
17545 if ((bf_get(lpfc_cqe_code,
17546 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17547 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17548 &dmabuf->cq_event.cqe.rcqe_cmpl);
17549 else
17550 fcfi = bf_get(lpfc_rcqe_fcf_id,
17551 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17552
ae9e28f3
JS
17553 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17554 vport = phba->pport;
17555 /* Handle MDS Loopback frames */
17556 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17557 return;
17558 }
17559
895427bd
JS
17560 /* d_id this frame is directed to */
17561 did = sli4_did_from_fc_hdr(fc_hdr);
17562
17563 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17564 if (!vport) {
4d9ab994
JS
17565 /* throw out the frame */
17566 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17567 return;
17568 }
939723a4 17569
939723a4
JS
17570 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17571 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17572 (did != Fabric_DID)) {
17573 /*
17574 * Throw out the frame if we are not pt2pt.
17575 * The pt2pt protocol allows for discovery frames
17576 * to be received without a registered VPI.
17577 */
17578 if (!(vport->fc_flag & FC_PT2PT) ||
17579 (phba->link_state == LPFC_HBA_READY)) {
17580 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17581 return;
17582 }
17583 }
17584
6669f9bb
JS
17585 /* Handle the basic abort sequence (BA_ABTS) event */
17586 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17587 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17588 return;
17589 }
17590
4d9ab994
JS
17591 /* Link this frame */
17592 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17593 if (!seq_dmabuf) {
17594 /* unable to add frame to vport - throw it out */
17595 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17596 return;
17597 }
17598 /* If not last frame in sequence continue processing frames. */
def9c7a9 17599 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17600 return;
def9c7a9 17601
6669f9bb
JS
17602 /* Send the complete sequence to the upper layer protocol */
17603 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17604}
6fb120a7
JS
17605
17606/**
17607 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17608 * @phba: pointer to lpfc hba data structure.
17609 *
17610 * This routine is invoked to post rpi header templates to the
17611 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17612 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17613 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17614 *
17615 * This routine does not require any locks. It's usage is expected
17616 * to be driver load or reset recovery when the driver is
17617 * sequential.
17618 *
17619 * Return codes
af901ca1 17620 * 0 - successful
d439d286 17621 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17622 * When this error occurs, the driver is not guaranteed
17623 * to have any rpi regions posted to the device and
17624 * must either attempt to repost the regions or take a
17625 * fatal error.
17626 **/
17627int
17628lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17629{
17630 struct lpfc_rpi_hdr *rpi_page;
17631 uint32_t rc = 0;
6d368e53
JS
17632 uint16_t lrpi = 0;
17633
17634 /* SLI4 ports that support extents do not require RPI headers. */
17635 if (!phba->sli4_hba.rpi_hdrs_in_use)
17636 goto exit;
17637 if (phba->sli4_hba.extents_in_use)
17638 return -EIO;
6fb120a7 17639
6fb120a7 17640 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17641 /*
17642 * Assign the rpi headers a physical rpi only if the driver
17643 * has not initialized those resources. A port reset only
17644 * needs the headers posted.
17645 */
17646 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17647 LPFC_RPI_RSRC_RDY)
17648 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17649
6fb120a7
JS
17650 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17651 if (rc != MBX_SUCCESS) {
17652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17653 "2008 Error %d posting all rpi "
17654 "headers\n", rc);
17655 rc = -EIO;
17656 break;
17657 }
17658 }
17659
6d368e53
JS
17660 exit:
17661 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17662 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17663 return rc;
17664}
17665
17666/**
17667 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17668 * @phba: pointer to lpfc hba data structure.
17669 * @rpi_page: pointer to the rpi memory region.
17670 *
17671 * This routine is invoked to post a single rpi header to the
17672 * HBA consistent with the SLI-4 interface spec. This memory region
17673 * maps up to 64 rpi context regions.
17674 *
17675 * Return codes
af901ca1 17676 * 0 - successful
d439d286
JS
17677 * -ENOMEM - No available memory
17678 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17679 **/
17680int
17681lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17682{
17683 LPFC_MBOXQ_t *mboxq;
17684 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17685 uint32_t rc = 0;
6fb120a7
JS
17686 uint32_t shdr_status, shdr_add_status;
17687 union lpfc_sli4_cfg_shdr *shdr;
17688
6d368e53
JS
17689 /* SLI4 ports that support extents do not require RPI headers. */
17690 if (!phba->sli4_hba.rpi_hdrs_in_use)
17691 return rc;
17692 if (phba->sli4_hba.extents_in_use)
17693 return -EIO;
17694
6fb120a7
JS
17695 /* The port is notified of the header region via a mailbox command. */
17696 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17697 if (!mboxq) {
17698 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17699 "2001 Unable to allocate memory for issuing "
17700 "SLI_CONFIG_SPECIAL mailbox command\n");
17701 return -ENOMEM;
17702 }
17703
17704 /* Post all rpi memory regions to the port. */
17705 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17706 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17707 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17708 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17709 sizeof(struct lpfc_sli4_cfg_mhdr),
17710 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17711
17712
17713 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17714 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17715 rpi_page->start_rpi);
6d368e53
JS
17716 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17717 hdr_tmpl, rpi_page->page_count);
17718
6fb120a7
JS
17719 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17720 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17721 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17722 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17723 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17724 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17725 if (rc != MBX_TIMEOUT)
17726 mempool_free(mboxq, phba->mbox_mem_pool);
17727 if (shdr_status || shdr_add_status || rc) {
17728 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17729 "2514 POST_RPI_HDR mailbox failed with "
17730 "status x%x add_status x%x, mbx status x%x\n",
17731 shdr_status, shdr_add_status, rc);
17732 rc = -ENXIO;
845d9e8d
JS
17733 } else {
17734 /*
17735 * The next_rpi stores the next logical module-64 rpi value used
17736 * to post physical rpis in subsequent rpi postings.
17737 */
17738 spin_lock_irq(&phba->hbalock);
17739 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17740 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17741 }
17742 return rc;
17743}
17744
17745/**
17746 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17747 * @phba: pointer to lpfc hba data structure.
17748 *
17749 * This routine is invoked to post rpi header templates to the
17750 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17751 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17752 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17753 *
17754 * Returns
af901ca1 17755 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17756 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17757 **/
17758int
17759lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17760{
6d368e53
JS
17761 unsigned long rpi;
17762 uint16_t max_rpi, rpi_limit;
17763 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17764 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17765 unsigned long iflag;
6fb120a7 17766
6fb120a7 17767 /*
6d368e53
JS
17768 * Fetch the next logical rpi. Because this index is logical,
17769 * the driver starts at 0 each time.
6fb120a7 17770 */
4902b381 17771 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17772 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17773 rpi_limit = phba->sli4_hba.next_rpi;
17774
6d368e53
JS
17775 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17776 if (rpi >= rpi_limit)
6fb120a7
JS
17777 rpi = LPFC_RPI_ALLOC_ERROR;
17778 else {
17779 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17780 phba->sli4_hba.max_cfg_param.rpi_used++;
17781 phba->sli4_hba.rpi_count++;
17782 }
be6bb941
JS
17783 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17784 "0001 rpi:%x max:%x lim:%x\n",
17785 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17786
17787 /*
17788 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17789 * has been exhausted.
6fb120a7
JS
17790 */
17791 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17792 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17793 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17794 return rpi;
17795 }
17796
6d368e53
JS
17797 /*
17798 * RPI header postings are not required for SLI4 ports capable of
17799 * extents.
17800 */
17801 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17802 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17803 return rpi;
17804 }
17805
6fb120a7
JS
17806 /*
17807 * If the driver is running low on rpi resources, allocate another
17808 * page now. Note that the next_rpi value is used because
17809 * it represents how many are actually in use whereas max_rpi notes
17810 * how many are supported max by the device.
17811 */
6d368e53 17812 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17813 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17814 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17815 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17816 if (!rpi_hdr) {
17817 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17818 "2002 Error Could not grow rpi "
17819 "count\n");
17820 } else {
6d368e53
JS
17821 lrpi = rpi_hdr->start_rpi;
17822 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17823 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17824 }
17825 }
17826
17827 return rpi;
17828}
17829
d7c47992
JS
17830/**
17831 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17832 * @phba: pointer to lpfc hba data structure.
17833 *
17834 * This routine is invoked to release an rpi to the pool of
17835 * available rpis maintained by the driver.
17836 **/
5d8b8167 17837static void
d7c47992
JS
17838__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17839{
17840 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17841 phba->sli4_hba.rpi_count--;
17842 phba->sli4_hba.max_cfg_param.rpi_used--;
17843 }
17844}
17845
6fb120a7
JS
17846/**
17847 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17848 * @phba: pointer to lpfc hba data structure.
17849 *
17850 * This routine is invoked to release an rpi to the pool of
17851 * available rpis maintained by the driver.
17852 **/
17853void
17854lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17855{
17856 spin_lock_irq(&phba->hbalock);
d7c47992 17857 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17858 spin_unlock_irq(&phba->hbalock);
17859}
17860
17861/**
17862 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17863 * @phba: pointer to lpfc hba data structure.
17864 *
17865 * This routine is invoked to remove the memory region that
17866 * provided rpi via a bitmask.
17867 **/
17868void
17869lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17870{
17871 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17872 kfree(phba->sli4_hba.rpi_ids);
17873 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17874}
17875
17876/**
17877 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17878 * @phba: pointer to lpfc hba data structure.
17879 *
17880 * This routine is invoked to remove the memory region that
17881 * provided rpi via a bitmask.
17882 **/
17883int
6b5151fd
JS
17884lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17885 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17886{
17887 LPFC_MBOXQ_t *mboxq;
17888 struct lpfc_hba *phba = ndlp->phba;
17889 int rc;
17890
17891 /* The port is notified of the header region via a mailbox command. */
17892 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17893 if (!mboxq)
17894 return -ENOMEM;
17895
17896 /* Post all rpi memory regions to the port. */
17897 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17898 if (cmpl) {
17899 mboxq->mbox_cmpl = cmpl;
17900 mboxq->context1 = arg;
17901 mboxq->context2 = ndlp;
72859909
JS
17902 } else
17903 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17904 mboxq->vport = ndlp->vport;
6fb120a7
JS
17905 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17906 if (rc == MBX_NOT_FINISHED) {
17907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17908 "2010 Resume RPI Mailbox failed "
17909 "status %d, mbxStatus x%x\n", rc,
17910 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17911 mempool_free(mboxq, phba->mbox_mem_pool);
17912 return -EIO;
17913 }
17914 return 0;
17915}
17916
17917/**
17918 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17919 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17920 *
76a95d75 17921 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17922 *
17923 * Returns:
17924 * 0 success
17925 * -Evalue otherwise
17926 **/
17927int
76a95d75 17928lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17929{
17930 LPFC_MBOXQ_t *mboxq;
17931 int rc = 0;
6a9c52cf 17932 int retval = MBX_SUCCESS;
6fb120a7 17933 uint32_t mbox_tmo;
76a95d75 17934 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17935 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17936 if (!mboxq)
17937 return -ENOMEM;
76a95d75 17938 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17939 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17940 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17941 if (rc != MBX_SUCCESS) {
76a95d75 17942 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17943 "2022 INIT VPI Mailbox failed "
17944 "status %d, mbxStatus x%x\n", rc,
17945 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17946 retval = -EIO;
6fb120a7 17947 }
6a9c52cf 17948 if (rc != MBX_TIMEOUT)
76a95d75 17949 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17950
17951 return retval;
6fb120a7
JS
17952}
17953
17954/**
17955 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17956 * @phba: pointer to lpfc hba data structure.
17957 * @mboxq: Pointer to mailbox object.
17958 *
17959 * This routine is invoked to manually add a single FCF record. The caller
17960 * must pass a completely initialized FCF_Record. This routine takes
17961 * care of the nonembedded mailbox operations.
17962 **/
17963static void
17964lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17965{
17966 void *virt_addr;
17967 union lpfc_sli4_cfg_shdr *shdr;
17968 uint32_t shdr_status, shdr_add_status;
17969
17970 virt_addr = mboxq->sge_array->addr[0];
17971 /* The IOCTL status is embedded in the mailbox subheader. */
17972 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17973 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17974 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17975
17976 if ((shdr_status || shdr_add_status) &&
17977 (shdr_status != STATUS_FCF_IN_USE))
17978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17979 "2558 ADD_FCF_RECORD mailbox failed with "
17980 "status x%x add_status x%x\n",
17981 shdr_status, shdr_add_status);
17982
17983 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17984}
17985
17986/**
17987 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17988 * @phba: pointer to lpfc hba data structure.
17989 * @fcf_record: pointer to the initialized fcf record to add.
17990 *
17991 * This routine is invoked to manually add a single FCF record. The caller
17992 * must pass a completely initialized FCF_Record. This routine takes
17993 * care of the nonembedded mailbox operations.
17994 **/
17995int
17996lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17997{
17998 int rc = 0;
17999 LPFC_MBOXQ_t *mboxq;
18000 uint8_t *bytep;
18001 void *virt_addr;
6fb120a7
JS
18002 struct lpfc_mbx_sge sge;
18003 uint32_t alloc_len, req_len;
18004 uint32_t fcfindex;
18005
18006 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18007 if (!mboxq) {
18008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18009 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18010 return -ENOMEM;
18011 }
18012
18013 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18014 sizeof(uint32_t);
18015
18016 /* Allocate DMA memory and set up the non-embedded mailbox command */
18017 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18018 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18019 req_len, LPFC_SLI4_MBX_NEMBED);
18020 if (alloc_len < req_len) {
18021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18022 "2523 Allocated DMA memory size (x%x) is "
18023 "less than the requested DMA memory "
18024 "size (x%x)\n", alloc_len, req_len);
18025 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18026 return -ENOMEM;
18027 }
18028
18029 /*
18030 * Get the first SGE entry from the non-embedded DMA memory. This
18031 * routine only uses a single SGE.
18032 */
18033 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
18034 virt_addr = mboxq->sge_array->addr[0];
18035 /*
18036 * Configure the FCF record for FCFI 0. This is the driver's
18037 * hardcoded default and gets used in nonFIP mode.
18038 */
18039 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18040 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18041 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18042
18043 /*
18044 * Copy the fcf_index and the FCF Record Data. The data starts after
18045 * the FCoE header plus word10. The data copy needs to be endian
18046 * correct.
18047 */
18048 bytep += sizeof(uint32_t);
18049 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18050 mboxq->vport = phba->pport;
18051 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18052 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18053 if (rc == MBX_NOT_FINISHED) {
18054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18055 "2515 ADD_FCF_RECORD mailbox failed with "
18056 "status 0x%x\n", rc);
18057 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18058 rc = -EIO;
18059 } else
18060 rc = 0;
18061
18062 return rc;
18063}
18064
18065/**
18066 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18067 * @phba: pointer to lpfc hba data structure.
18068 * @fcf_record: pointer to the fcf record to write the default data.
18069 * @fcf_index: FCF table entry index.
18070 *
18071 * This routine is invoked to build the driver's default FCF record. The
18072 * values used are hardcoded. This routine handles memory initialization.
18073 *
18074 **/
18075void
18076lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18077 struct fcf_record *fcf_record,
18078 uint16_t fcf_index)
18079{
18080 memset(fcf_record, 0, sizeof(struct fcf_record));
18081 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18082 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18083 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18084 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18085 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18086 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18087 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18088 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18089 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18090 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18091 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18092 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18093 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 18094 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
18095 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18096 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18097 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18098 /* Set the VLAN bit map */
18099 if (phba->valid_vlan) {
18100 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18101 = 1 << (phba->vlan_id % 8);
18102 }
18103}
18104
18105/**
0c9ab6f5 18106 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
18107 * @phba: pointer to lpfc hba data structure.
18108 * @fcf_index: FCF table entry offset.
18109 *
0c9ab6f5
JS
18110 * This routine is invoked to scan the entire FCF table by reading FCF
18111 * record and processing it one at a time starting from the @fcf_index
18112 * for initial FCF discovery or fast FCF failover rediscovery.
18113 *
25985edc 18114 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 18115 * otherwise.
6fb120a7
JS
18116 **/
18117int
0c9ab6f5 18118lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
18119{
18120 int rc = 0, error;
18121 LPFC_MBOXQ_t *mboxq;
6fb120a7 18122
32b9793f 18123 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 18124 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
18125 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18126 if (!mboxq) {
18127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18128 "2000 Failed to allocate mbox for "
18129 "READ_FCF cmd\n");
4d9ab994 18130 error = -ENOMEM;
0c9ab6f5 18131 goto fail_fcf_scan;
6fb120a7 18132 }
ecfd03c6 18133 /* Construct the read FCF record mailbox command */
0c9ab6f5 18134 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
18135 if (rc) {
18136 error = -EINVAL;
0c9ab6f5 18137 goto fail_fcf_scan;
6fb120a7 18138 }
ecfd03c6 18139 /* Issue the mailbox command asynchronously */
6fb120a7 18140 mboxq->vport = phba->pport;
0c9ab6f5 18141 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
18142
18143 spin_lock_irq(&phba->hbalock);
18144 phba->hba_flag |= FCF_TS_INPROG;
18145 spin_unlock_irq(&phba->hbalock);
18146
6fb120a7 18147 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 18148 if (rc == MBX_NOT_FINISHED)
6fb120a7 18149 error = -EIO;
ecfd03c6 18150 else {
38b92ef8
JS
18151 /* Reset eligible FCF count for new scan */
18152 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 18153 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 18154 error = 0;
32b9793f 18155 }
0c9ab6f5 18156fail_fcf_scan:
4d9ab994
JS
18157 if (error) {
18158 if (mboxq)
18159 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 18160 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 18161 spin_lock_irq(&phba->hbalock);
a93ff37a 18162 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
18163 spin_unlock_irq(&phba->hbalock);
18164 }
6fb120a7
JS
18165 return error;
18166}
a0c87cbd 18167
0c9ab6f5 18168/**
a93ff37a 18169 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
18170 * @phba: pointer to lpfc hba data structure.
18171 * @fcf_index: FCF table entry offset.
18172 *
18173 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 18174 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 18175 *
25985edc 18176 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18177 * otherwise.
18178 **/
18179int
18180lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18181{
18182 int rc = 0, error;
18183 LPFC_MBOXQ_t *mboxq;
18184
18185 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18186 if (!mboxq) {
18187 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18188 "2763 Failed to allocate mbox for "
18189 "READ_FCF cmd\n");
18190 error = -ENOMEM;
18191 goto fail_fcf_read;
18192 }
18193 /* Construct the read FCF record mailbox command */
18194 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18195 if (rc) {
18196 error = -EINVAL;
18197 goto fail_fcf_read;
18198 }
18199 /* Issue the mailbox command asynchronously */
18200 mboxq->vport = phba->pport;
18201 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18203 if (rc == MBX_NOT_FINISHED)
18204 error = -EIO;
18205 else
18206 error = 0;
18207
18208fail_fcf_read:
18209 if (error && mboxq)
18210 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18211 return error;
18212}
18213
18214/**
18215 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18216 * @phba: pointer to lpfc hba data structure.
18217 * @fcf_index: FCF table entry offset.
18218 *
18219 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 18220 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 18221 *
25985edc 18222 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18223 * otherwise.
18224 **/
18225int
18226lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18227{
18228 int rc = 0, error;
18229 LPFC_MBOXQ_t *mboxq;
18230
18231 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18232 if (!mboxq) {
18233 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18234 "2758 Failed to allocate mbox for "
18235 "READ_FCF cmd\n");
18236 error = -ENOMEM;
18237 goto fail_fcf_read;
18238 }
18239 /* Construct the read FCF record mailbox command */
18240 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18241 if (rc) {
18242 error = -EINVAL;
18243 goto fail_fcf_read;
18244 }
18245 /* Issue the mailbox command asynchronously */
18246 mboxq->vport = phba->pport;
18247 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18248 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18249 if (rc == MBX_NOT_FINISHED)
18250 error = -EIO;
18251 else
18252 error = 0;
18253
18254fail_fcf_read:
18255 if (error && mboxq)
18256 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18257 return error;
18258}
18259
7d791df7 18260/**
f5cb5304 18261 * lpfc_check_next_fcf_pri_level
7d791df7
JS
18262 * phba pointer to the lpfc_hba struct for this port.
18263 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18264 * routine when the rr_bmask is empty. The FCF indecies are put into the
18265 * rr_bmask based on their priority level. Starting from the highest priority
18266 * to the lowest. The most likely FCF candidate will be in the highest
18267 * priority group. When this routine is called it searches the fcf_pri list for
18268 * next lowest priority group and repopulates the rr_bmask with only those
18269 * fcf_indexes.
18270 * returns:
18271 * 1=success 0=failure
18272 **/
5d8b8167 18273static int
7d791df7
JS
18274lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18275{
18276 uint16_t next_fcf_pri;
18277 uint16_t last_index;
18278 struct lpfc_fcf_pri *fcf_pri;
18279 int rc;
18280 int ret = 0;
18281
18282 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18283 LPFC_SLI4_FCF_TBL_INDX_MAX);
18284 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18285 "3060 Last IDX %d\n", last_index);
2562669c
JS
18286
18287 /* Verify the priority list has 2 or more entries */
18288 spin_lock_irq(&phba->hbalock);
18289 if (list_empty(&phba->fcf.fcf_pri_list) ||
18290 list_is_singular(&phba->fcf.fcf_pri_list)) {
18291 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
18292 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18293 "3061 Last IDX %d\n", last_index);
18294 return 0; /* Empty rr list */
18295 }
2562669c
JS
18296 spin_unlock_irq(&phba->hbalock);
18297
7d791df7
JS
18298 next_fcf_pri = 0;
18299 /*
18300 * Clear the rr_bmask and set all of the bits that are at this
18301 * priority.
18302 */
18303 memset(phba->fcf.fcf_rr_bmask, 0,
18304 sizeof(*phba->fcf.fcf_rr_bmask));
18305 spin_lock_irq(&phba->hbalock);
18306 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18307 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18308 continue;
18309 /*
18310 * the 1st priority that has not FLOGI failed
18311 * will be the highest.
18312 */
18313 if (!next_fcf_pri)
18314 next_fcf_pri = fcf_pri->fcf_rec.priority;
18315 spin_unlock_irq(&phba->hbalock);
18316 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18317 rc = lpfc_sli4_fcf_rr_index_set(phba,
18318 fcf_pri->fcf_rec.fcf_index);
18319 if (rc)
18320 return 0;
18321 }
18322 spin_lock_irq(&phba->hbalock);
18323 }
18324 /*
18325 * if next_fcf_pri was not set above and the list is not empty then
18326 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18327 * and start at the beginning.
7d791df7
JS
18328 */
18329 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18330 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18331 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18332 /*
18333 * the 1st priority that has not FLOGI failed
18334 * will be the highest.
18335 */
18336 if (!next_fcf_pri)
18337 next_fcf_pri = fcf_pri->fcf_rec.priority;
18338 spin_unlock_irq(&phba->hbalock);
18339 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18340 rc = lpfc_sli4_fcf_rr_index_set(phba,
18341 fcf_pri->fcf_rec.fcf_index);
18342 if (rc)
18343 return 0;
18344 }
18345 spin_lock_irq(&phba->hbalock);
18346 }
18347 } else
18348 ret = 1;
18349 spin_unlock_irq(&phba->hbalock);
18350
18351 return ret;
18352}
0c9ab6f5
JS
18353/**
18354 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18355 * @phba: pointer to lpfc hba data structure.
18356 *
18357 * This routine is to get the next eligible FCF record index in a round
18358 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18359 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18360 * shall be returned, otherwise, the next eligible FCF record's index
18361 * shall be returned.
18362 **/
18363uint16_t
18364lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18365{
18366 uint16_t next_fcf_index;
18367
421c6622 18368initial_priority:
3804dc84 18369 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18370 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18371
7d791df7 18372next_priority:
421c6622
JS
18373 /* Determine the next fcf index to check */
18374 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18375 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18376 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18377 next_fcf_index);
18378
0c9ab6f5 18379 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18380 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18381 /*
18382 * If we have wrapped then we need to clear the bits that
18383 * have been tested so that we can detect when we should
18384 * change the priority level.
18385 */
0c9ab6f5
JS
18386 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18387 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18388 }
18389
3804dc84
JS
18390
18391 /* Check roundrobin failover list empty condition */
7d791df7
JS
18392 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18393 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18394 /*
18395 * If next fcf index is not found check if there are lower
18396 * Priority level fcf's in the fcf_priority list.
18397 * Set up the rr_bmask with all of the avaiable fcf bits
18398 * at that level and continue the selection process.
18399 */
18400 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18401 goto initial_priority;
3804dc84
JS
18402 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18403 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18404 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18405 return LPFC_FCOE_FCF_NEXT_NONE;
18406 else {
18407 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18408 "3063 Only FCF available idx %d, flag %x\n",
18409 next_fcf_index,
18410 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18411 return next_fcf_index;
18412 }
3804dc84
JS
18413 }
18414
7d791df7
JS
18415 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18416 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18417 LPFC_FCF_FLOGI_FAILED) {
18418 if (list_is_singular(&phba->fcf.fcf_pri_list))
18419 return LPFC_FCOE_FCF_NEXT_NONE;
18420
7d791df7 18421 goto next_priority;
f5cb5304 18422 }
7d791df7 18423
3804dc84 18424 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18425 "2845 Get next roundrobin failover FCF (x%x)\n",
18426 next_fcf_index);
18427
0c9ab6f5
JS
18428 return next_fcf_index;
18429}
18430
18431/**
18432 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18433 * @phba: pointer to lpfc hba data structure.
18434 *
18435 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18436 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18437 * does not go beyond the range of the driver allocated bmask dimension
18438 * before setting the bit.
18439 *
18440 * Returns 0 if the index bit successfully set, otherwise, it returns
18441 * -EINVAL.
18442 **/
18443int
18444lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18445{
18446 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18447 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18448 "2610 FCF (x%x) reached driver's book "
18449 "keeping dimension:x%x\n",
0c9ab6f5
JS
18450 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18451 return -EINVAL;
18452 }
18453 /* Set the eligible FCF record index bmask */
18454 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18455
3804dc84 18456 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18457 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18458 "bmask\n", fcf_index);
18459
0c9ab6f5
JS
18460 return 0;
18461}
18462
18463/**
3804dc84 18464 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18465 * @phba: pointer to lpfc hba data structure.
18466 *
18467 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18468 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18469 * does not go beyond the range of the driver allocated bmask dimension
18470 * before clearing the bit.
18471 **/
18472void
18473lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18474{
9a803a74 18475 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18476 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18477 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18478 "2762 FCF (x%x) reached driver's book "
18479 "keeping dimension:x%x\n",
0c9ab6f5
JS
18480 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18481 return;
18482 }
18483 /* Clear the eligible FCF record index bmask */
7d791df7 18484 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18485 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18486 list) {
7d791df7
JS
18487 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18488 list_del_init(&fcf_pri->list);
18489 break;
18490 }
18491 }
18492 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18493 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18494
18495 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18496 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18497 "bmask\n", fcf_index);
0c9ab6f5
JS
18498}
18499
ecfd03c6
JS
18500/**
18501 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18502 * @phba: pointer to lpfc hba data structure.
18503 *
18504 * This routine is the completion routine for the rediscover FCF table mailbox
18505 * command. If the mailbox command returned failure, it will try to stop the
18506 * FCF rediscover wait timer.
18507 **/
5d8b8167 18508static void
ecfd03c6
JS
18509lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18510{
18511 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18512 uint32_t shdr_status, shdr_add_status;
18513
18514 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18515
18516 shdr_status = bf_get(lpfc_mbox_hdr_status,
18517 &redisc_fcf->header.cfg_shdr.response);
18518 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18519 &redisc_fcf->header.cfg_shdr.response);
18520 if (shdr_status || shdr_add_status) {
0c9ab6f5 18521 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18522 "2746 Requesting for FCF rediscovery failed "
18523 "status x%x add_status x%x\n",
18524 shdr_status, shdr_add_status);
0c9ab6f5 18525 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18526 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18527 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18528 spin_unlock_irq(&phba->hbalock);
18529 /*
18530 * CVL event triggered FCF rediscover request failed,
18531 * last resort to re-try current registered FCF entry.
18532 */
18533 lpfc_retry_pport_discovery(phba);
18534 } else {
18535 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18536 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18537 spin_unlock_irq(&phba->hbalock);
18538 /*
18539 * DEAD FCF event triggered FCF rediscover request
18540 * failed, last resort to fail over as a link down
18541 * to FCF registration.
18542 */
18543 lpfc_sli4_fcf_dead_failthrough(phba);
18544 }
0c9ab6f5
JS
18545 } else {
18546 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18547 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18548 /*
18549 * Start FCF rediscovery wait timer for pending FCF
18550 * before rescan FCF record table.
18551 */
18552 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18553 }
ecfd03c6
JS
18554
18555 mempool_free(mbox, phba->mbox_mem_pool);
18556}
18557
18558/**
3804dc84 18559 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18560 * @phba: pointer to lpfc hba data structure.
18561 *
18562 * This routine is invoked to request for rediscovery of the entire FCF table
18563 * by the port.
18564 **/
18565int
18566lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18567{
18568 LPFC_MBOXQ_t *mbox;
18569 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18570 int rc, length;
18571
0c9ab6f5
JS
18572 /* Cancel retry delay timers to all vports before FCF rediscover */
18573 lpfc_cancel_all_vport_retry_delay_timer(phba);
18574
ecfd03c6
JS
18575 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18576 if (!mbox) {
18577 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18578 "2745 Failed to allocate mbox for "
18579 "requesting FCF rediscover.\n");
18580 return -ENOMEM;
18581 }
18582
18583 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18584 sizeof(struct lpfc_sli4_cfg_mhdr));
18585 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18586 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18587 length, LPFC_SLI4_MBX_EMBED);
18588
18589 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18590 /* Set count to 0 for invalidating the entire FCF database */
18591 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18592
18593 /* Issue the mailbox command asynchronously */
18594 mbox->vport = phba->pport;
18595 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18596 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18597
18598 if (rc == MBX_NOT_FINISHED) {
18599 mempool_free(mbox, phba->mbox_mem_pool);
18600 return -EIO;
18601 }
18602 return 0;
18603}
18604
fc2b989b
JS
18605/**
18606 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18607 * @phba: pointer to lpfc hba data structure.
18608 *
18609 * This function is the failover routine as a last resort to the FCF DEAD
18610 * event when driver failed to perform fast FCF failover.
18611 **/
18612void
18613lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18614{
18615 uint32_t link_state;
18616
18617 /*
18618 * Last resort as FCF DEAD event failover will treat this as
18619 * a link down, but save the link state because we don't want
18620 * it to be changed to Link Down unless it is already down.
18621 */
18622 link_state = phba->link_state;
18623 lpfc_linkdown(phba);
18624 phba->link_state = link_state;
18625
18626 /* Unregister FCF if no devices connected to it */
18627 lpfc_unregister_unused_fcf(phba);
18628}
18629
a0c87cbd 18630/**
026abb87 18631 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18632 * @phba: pointer to lpfc hba data structure.
026abb87 18633 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18634 *
026abb87
JS
18635 * This function gets SLI3 port configure region 23 data through memory dump
18636 * mailbox command. When it successfully retrieves data, the size of the data
18637 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18638 **/
026abb87
JS
18639static uint32_t
18640lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18641{
18642 LPFC_MBOXQ_t *pmb = NULL;
18643 MAILBOX_t *mb;
026abb87 18644 uint32_t offset = 0;
a0c87cbd
JS
18645 int rc;
18646
026abb87
JS
18647 if (!rgn23_data)
18648 return 0;
18649
a0c87cbd
JS
18650 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18651 if (!pmb) {
18652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18653 "2600 failed to allocate mailbox memory\n");
18654 return 0;
a0c87cbd
JS
18655 }
18656 mb = &pmb->u.mb;
18657
a0c87cbd
JS
18658 do {
18659 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18660 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18661
18662 if (rc != MBX_SUCCESS) {
18663 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18664 "2601 failed to read config "
18665 "region 23, rc 0x%x Status 0x%x\n",
18666 rc, mb->mbxStatus);
a0c87cbd
JS
18667 mb->un.varDmp.word_cnt = 0;
18668 }
18669 /*
18670 * dump mem may return a zero when finished or we got a
18671 * mailbox error, either way we are done.
18672 */
18673 if (mb->un.varDmp.word_cnt == 0)
18674 break;
18675 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18676 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18677
18678 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18679 rgn23_data + offset,
18680 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18681 offset += mb->un.varDmp.word_cnt;
18682 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18683
026abb87
JS
18684 mempool_free(pmb, phba->mbox_mem_pool);
18685 return offset;
18686}
18687
18688/**
18689 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18690 * @phba: pointer to lpfc hba data structure.
18691 * @rgn23_data: pointer to configure region 23 data.
18692 *
18693 * This function gets SLI4 port configure region 23 data through memory dump
18694 * mailbox command. When it successfully retrieves data, the size of the data
18695 * will be returned, otherwise, 0 will be returned.
18696 **/
18697static uint32_t
18698lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18699{
18700 LPFC_MBOXQ_t *mboxq = NULL;
18701 struct lpfc_dmabuf *mp = NULL;
18702 struct lpfc_mqe *mqe;
18703 uint32_t data_length = 0;
18704 int rc;
18705
18706 if (!rgn23_data)
18707 return 0;
18708
18709 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18710 if (!mboxq) {
18711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18712 "3105 failed to allocate mailbox memory\n");
18713 return 0;
18714 }
18715
18716 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18717 goto out;
18718 mqe = &mboxq->u.mqe;
18719 mp = (struct lpfc_dmabuf *) mboxq->context1;
18720 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18721 if (rc)
18722 goto out;
18723 data_length = mqe->un.mb_words[5];
18724 if (data_length == 0)
18725 goto out;
18726 if (data_length > DMP_RGN23_SIZE) {
18727 data_length = 0;
18728 goto out;
18729 }
18730 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18731out:
18732 mempool_free(mboxq, phba->mbox_mem_pool);
18733 if (mp) {
18734 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18735 kfree(mp);
18736 }
18737 return data_length;
18738}
18739
18740/**
18741 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18742 * @phba: pointer to lpfc hba data structure.
18743 *
18744 * This function read region 23 and parse TLV for port status to
18745 * decide if the user disaled the port. If the TLV indicates the
18746 * port is disabled, the hba_flag is set accordingly.
18747 **/
18748void
18749lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18750{
18751 uint8_t *rgn23_data = NULL;
18752 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18753 uint32_t offset = 0;
18754
18755 /* Get adapter Region 23 data */
18756 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18757 if (!rgn23_data)
18758 goto out;
18759
18760 if (phba->sli_rev < LPFC_SLI_REV4)
18761 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18762 else {
18763 if_type = bf_get(lpfc_sli_intf_if_type,
18764 &phba->sli4_hba.sli_intf);
18765 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18766 goto out;
18767 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18768 }
a0c87cbd
JS
18769
18770 if (!data_size)
18771 goto out;
18772
18773 /* Check the region signature first */
18774 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18776 "2619 Config region 23 has bad signature\n");
18777 goto out;
18778 }
18779 offset += 4;
18780
18781 /* Check the data structure version */
18782 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18784 "2620 Config region 23 has bad version\n");
18785 goto out;
18786 }
18787 offset += 4;
18788
18789 /* Parse TLV entries in the region */
18790 while (offset < data_size) {
18791 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18792 break;
18793 /*
18794 * If the TLV is not driver specific TLV or driver id is
18795 * not linux driver id, skip the record.
18796 */
18797 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18798 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18799 (rgn23_data[offset + 3] != 0)) {
18800 offset += rgn23_data[offset + 1] * 4 + 4;
18801 continue;
18802 }
18803
18804 /* Driver found a driver specific TLV in the config region */
18805 sub_tlv_len = rgn23_data[offset + 1] * 4;
18806 offset += 4;
18807 tlv_offset = 0;
18808
18809 /*
18810 * Search for configured port state sub-TLV.
18811 */
18812 while ((offset < data_size) &&
18813 (tlv_offset < sub_tlv_len)) {
18814 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18815 offset += 4;
18816 tlv_offset += 4;
18817 break;
18818 }
18819 if (rgn23_data[offset] != PORT_STE_TYPE) {
18820 offset += rgn23_data[offset + 1] * 4 + 4;
18821 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18822 continue;
18823 }
18824
18825 /* This HBA contains PORT_STE configured */
18826 if (!rgn23_data[offset + 2])
18827 phba->hba_flag |= LINK_DISABLED;
18828
18829 goto out;
18830 }
18831 }
026abb87 18832
a0c87cbd 18833out:
a0c87cbd
JS
18834 kfree(rgn23_data);
18835 return;
18836}
695a814e 18837
52d52440
JS
18838/**
18839 * lpfc_wr_object - write an object to the firmware
18840 * @phba: HBA structure that indicates port to create a queue on.
18841 * @dmabuf_list: list of dmabufs to write to the port.
18842 * @size: the total byte value of the objects to write to the port.
18843 * @offset: the current offset to be used to start the transfer.
18844 *
18845 * This routine will create a wr_object mailbox command to send to the port.
18846 * the mailbox command will be constructed using the dma buffers described in
18847 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18848 * BDEs that the imbedded mailbox can support. The @offset variable will be
18849 * used to indicate the starting offset of the transfer and will also return
18850 * the offset after the write object mailbox has completed. @size is used to
18851 * determine the end of the object and whether the eof bit should be set.
18852 *
18853 * Return 0 is successful and offset will contain the the new offset to use
18854 * for the next write.
18855 * Return negative value for error cases.
18856 **/
18857int
18858lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18859 uint32_t size, uint32_t *offset)
18860{
18861 struct lpfc_mbx_wr_object *wr_object;
18862 LPFC_MBOXQ_t *mbox;
18863 int rc = 0, i = 0;
18864 uint32_t shdr_status, shdr_add_status;
18865 uint32_t mbox_tmo;
18866 union lpfc_sli4_cfg_shdr *shdr;
18867 struct lpfc_dmabuf *dmabuf;
18868 uint32_t written = 0;
18869
18870 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18871 if (!mbox)
18872 return -ENOMEM;
18873
18874 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18875 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18876 sizeof(struct lpfc_mbx_wr_object) -
18877 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18878
18879 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18880 wr_object->u.request.write_offset = *offset;
18881 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18882 wr_object->u.request.object_name[0] =
18883 cpu_to_le32(wr_object->u.request.object_name[0]);
18884 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18885 list_for_each_entry(dmabuf, dmabuf_list, list) {
18886 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18887 break;
18888 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18889 wr_object->u.request.bde[i].addrHigh =
18890 putPaddrHigh(dmabuf->phys);
18891 if (written + SLI4_PAGE_SIZE >= size) {
18892 wr_object->u.request.bde[i].tus.f.bdeSize =
18893 (size - written);
18894 written += (size - written);
18895 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18896 } else {
18897 wr_object->u.request.bde[i].tus.f.bdeSize =
18898 SLI4_PAGE_SIZE;
18899 written += SLI4_PAGE_SIZE;
18900 }
18901 i++;
18902 }
18903 wr_object->u.request.bde_count = i;
18904 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18905 if (!phba->sli4_hba.intr_enable)
18906 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18907 else {
a183a15f 18908 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18909 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18910 }
18911 /* The IOCTL status is embedded in the mailbox subheader. */
18912 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18913 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18914 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18915 if (rc != MBX_TIMEOUT)
18916 mempool_free(mbox, phba->mbox_mem_pool);
18917 if (shdr_status || shdr_add_status || rc) {
18918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18919 "3025 Write Object mailbox failed with "
18920 "status x%x add_status x%x, mbx status x%x\n",
18921 shdr_status, shdr_add_status, rc);
18922 rc = -ENXIO;
1feb8204 18923 *offset = shdr_add_status;
52d52440
JS
18924 } else
18925 *offset += wr_object->u.response.actual_write_length;
18926 return rc;
18927}
18928
695a814e
JS
18929/**
18930 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18931 * @vport: pointer to vport data structure.
18932 *
18933 * This function iterate through the mailboxq and clean up all REG_LOGIN
18934 * and REG_VPI mailbox commands associated with the vport. This function
18935 * is called when driver want to restart discovery of the vport due to
18936 * a Clear Virtual Link event.
18937 **/
18938void
18939lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18940{
18941 struct lpfc_hba *phba = vport->phba;
18942 LPFC_MBOXQ_t *mb, *nextmb;
18943 struct lpfc_dmabuf *mp;
78730cfe 18944 struct lpfc_nodelist *ndlp;
d439d286 18945 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18946 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18947 LIST_HEAD(mbox_cmd_list);
63e801ce 18948 uint8_t restart_loop;
695a814e 18949
d439d286 18950 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18951 spin_lock_irq(&phba->hbalock);
18952 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18953 if (mb->vport != vport)
18954 continue;
18955
18956 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18957 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18958 continue;
18959
d439d286
JS
18960 list_del(&mb->list);
18961 list_add_tail(&mb->list, &mbox_cmd_list);
18962 }
18963 /* Clean up active mailbox command with the vport */
18964 mb = phba->sli.mbox_active;
18965 if (mb && (mb->vport == vport)) {
18966 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18967 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18968 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18969 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18970 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18971 /* Put reference count for delayed processing */
18972 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18973 /* Unregister the RPI when mailbox complete */
18974 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18975 }
18976 }
63e801ce
JS
18977 /* Cleanup any mailbox completions which are not yet processed */
18978 do {
18979 restart_loop = 0;
18980 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18981 /*
18982 * If this mailox is already processed or it is
18983 * for another vport ignore it.
18984 */
18985 if ((mb->vport != vport) ||
18986 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18987 continue;
18988
18989 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18990 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18991 continue;
18992
18993 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18994 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18995 ndlp = (struct lpfc_nodelist *)mb->context2;
18996 /* Unregister the RPI when mailbox complete */
18997 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18998 restart_loop = 1;
18999 spin_unlock_irq(&phba->hbalock);
19000 spin_lock(shost->host_lock);
19001 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19002 spin_unlock(shost->host_lock);
19003 spin_lock_irq(&phba->hbalock);
19004 break;
19005 }
19006 }
19007 } while (restart_loop);
19008
d439d286
JS
19009 spin_unlock_irq(&phba->hbalock);
19010
19011 /* Release the cleaned-up mailbox commands */
19012 while (!list_empty(&mbox_cmd_list)) {
19013 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
19014 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
19015 mp = (struct lpfc_dmabuf *) (mb->context1);
19016 if (mp) {
19017 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19018 kfree(mp);
19019 }
78730cfe 19020 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 19021 mb->context2 = NULL;
78730cfe 19022 if (ndlp) {
ec21b3b0 19023 spin_lock(shost->host_lock);
589a52d6 19024 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 19025 spin_unlock(shost->host_lock);
78730cfe 19026 lpfc_nlp_put(ndlp);
78730cfe 19027 }
695a814e 19028 }
695a814e
JS
19029 mempool_free(mb, phba->mbox_mem_pool);
19030 }
d439d286
JS
19031
19032 /* Release the ndlp with the cleaned-up active mailbox command */
19033 if (act_mbx_ndlp) {
19034 spin_lock(shost->host_lock);
19035 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19036 spin_unlock(shost->host_lock);
19037 lpfc_nlp_put(act_mbx_ndlp);
695a814e 19038 }
695a814e
JS
19039}
19040
2a9bf3d0
JS
19041/**
19042 * lpfc_drain_txq - Drain the txq
19043 * @phba: Pointer to HBA context object.
19044 *
19045 * This function attempt to submit IOCBs on the txq
19046 * to the adapter. For SLI4 adapters, the txq contains
19047 * ELS IOCBs that have been deferred because the there
19048 * are no SGLs. This congestion can occur with large
19049 * vport counts during node discovery.
19050 **/
19051
19052uint32_t
19053lpfc_drain_txq(struct lpfc_hba *phba)
19054{
19055 LIST_HEAD(completions);
895427bd 19056 struct lpfc_sli_ring *pring;
2e706377 19057 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
19058 unsigned long iflags = 0;
19059 char *fail_msg = NULL;
19060 struct lpfc_sglq *sglq;
205e8240 19061 union lpfc_wqe128 wqe;
a2fc4aef 19062 uint32_t txq_cnt = 0;
2a9bf3d0 19063
895427bd 19064 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
19065 if (unlikely(!pring))
19066 return 0;
895427bd 19067
398d81c9 19068 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
19069 list_for_each_entry(piocbq, &pring->txq, list) {
19070 txq_cnt++;
19071 }
19072
19073 if (txq_cnt > pring->txq_max)
19074 pring->txq_max = txq_cnt;
2a9bf3d0 19075
398d81c9 19076 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19077
0e9bb8d7 19078 while (!list_empty(&pring->txq)) {
398d81c9 19079 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 19080
19ca7609 19081 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 19082 if (!piocbq) {
398d81c9 19083 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
19084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19085 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 19086 txq_cnt);
a629852a
JS
19087 break;
19088 }
895427bd 19089 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 19090 if (!sglq) {
19ca7609 19091 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 19092 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 19093 break;
2a9bf3d0 19094 }
0e9bb8d7 19095 txq_cnt--;
2a9bf3d0
JS
19096
19097 /* The xri and iocb resources secured,
19098 * attempt to issue request
19099 */
6d368e53 19100 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
19101 piocbq->sli4_xritag = sglq->sli4_xritag;
19102 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19103 fail_msg = "to convert bpl to sgl";
205e8240 19104 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
2a9bf3d0 19105 fail_msg = "to convert iocb to wqe";
205e8240 19106 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
2a9bf3d0
JS
19107 fail_msg = " - Wq is full";
19108 else
19109 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19110
19111 if (fail_msg) {
19112 /* Failed means we can't issue and need to cancel */
19113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19114 "2822 IOCB failed %s iotag 0x%x "
19115 "xri 0x%x\n",
19116 fail_msg,
19117 piocbq->iotag, piocbq->sli4_xritag);
19118 list_add_tail(&piocbq->list, &completions);
19119 }
398d81c9 19120 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
19121 }
19122
2a9bf3d0
JS
19123 /* Cancel all the IOCBs that cannot be issued */
19124 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19125 IOERR_SLI_ABORTED);
19126
0e9bb8d7 19127 return txq_cnt;
2a9bf3d0 19128}
895427bd
JS
19129
19130/**
19131 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19132 * @phba: Pointer to HBA context object.
19133 * @pwqe: Pointer to command WQE.
19134 * @sglq: Pointer to the scatter gather queue object.
19135 *
19136 * This routine converts the bpl or bde that is in the WQE
19137 * to a sgl list for the sli4 hardware. The physical address
19138 * of the bpl/bde is converted back to a virtual address.
19139 * If the WQE contains a BPL then the list of BDE's is
19140 * converted to sli4_sge's. If the WQE contains a single
19141 * BDE then it is converted to a single sli_sge.
19142 * The WQE is still in cpu endianness so the contents of
19143 * the bpl can be used without byte swapping.
19144 *
19145 * Returns valid XRI = Success, NO_XRI = Failure.
19146 */
19147static uint16_t
19148lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19149 struct lpfc_sglq *sglq)
19150{
19151 uint16_t xritag = NO_XRI;
19152 struct ulp_bde64 *bpl = NULL;
19153 struct ulp_bde64 bde;
19154 struct sli4_sge *sgl = NULL;
19155 struct lpfc_dmabuf *dmabuf;
205e8240 19156 union lpfc_wqe128 *wqe;
895427bd
JS
19157 int numBdes = 0;
19158 int i = 0;
19159 uint32_t offset = 0; /* accumulated offset in the sg request list */
19160 int inbound = 0; /* number of sg reply entries inbound from firmware */
19161 uint32_t cmd;
19162
19163 if (!pwqeq || !sglq)
19164 return xritag;
19165
19166 sgl = (struct sli4_sge *)sglq->sgl;
19167 wqe = &pwqeq->wqe;
19168 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19169
19170 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19171 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19172 return sglq->sli4_xritag;
19173 numBdes = pwqeq->rsvd2;
19174 if (numBdes) {
19175 /* The addrHigh and addrLow fields within the WQE
19176 * have not been byteswapped yet so there is no
19177 * need to swap them back.
19178 */
19179 if (pwqeq->context3)
19180 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19181 else
19182 return xritag;
19183
19184 bpl = (struct ulp_bde64 *)dmabuf->virt;
19185 if (!bpl)
19186 return xritag;
19187
19188 for (i = 0; i < numBdes; i++) {
19189 /* Should already be byte swapped. */
19190 sgl->addr_hi = bpl->addrHigh;
19191 sgl->addr_lo = bpl->addrLow;
19192
19193 sgl->word2 = le32_to_cpu(sgl->word2);
19194 if ((i+1) == numBdes)
19195 bf_set(lpfc_sli4_sge_last, sgl, 1);
19196 else
19197 bf_set(lpfc_sli4_sge_last, sgl, 0);
19198 /* swap the size field back to the cpu so we
19199 * can assign it to the sgl.
19200 */
19201 bde.tus.w = le32_to_cpu(bpl->tus.w);
19202 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19203 /* The offsets in the sgl need to be accumulated
19204 * separately for the request and reply lists.
19205 * The request is always first, the reply follows.
19206 */
19207 switch (cmd) {
19208 case CMD_GEN_REQUEST64_WQE:
19209 /* add up the reply sg entries */
19210 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19211 inbound++;
19212 /* first inbound? reset the offset */
19213 if (inbound == 1)
19214 offset = 0;
19215 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19216 bf_set(lpfc_sli4_sge_type, sgl,
19217 LPFC_SGE_TYPE_DATA);
19218 offset += bde.tus.f.bdeSize;
19219 break;
19220 case CMD_FCP_TRSP64_WQE:
19221 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19222 bf_set(lpfc_sli4_sge_type, sgl,
19223 LPFC_SGE_TYPE_DATA);
19224 break;
19225 case CMD_FCP_TSEND64_WQE:
19226 case CMD_FCP_TRECEIVE64_WQE:
19227 bf_set(lpfc_sli4_sge_type, sgl,
19228 bpl->tus.f.bdeFlags);
19229 if (i < 3)
19230 offset = 0;
19231 else
19232 offset += bde.tus.f.bdeSize;
19233 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19234 break;
19235 }
19236 sgl->word2 = cpu_to_le32(sgl->word2);
19237 bpl++;
19238 sgl++;
19239 }
19240 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19241 /* The addrHigh and addrLow fields of the BDE have not
19242 * been byteswapped yet so they need to be swapped
19243 * before putting them in the sgl.
19244 */
19245 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19246 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19247 sgl->word2 = le32_to_cpu(sgl->word2);
19248 bf_set(lpfc_sli4_sge_last, sgl, 1);
19249 sgl->word2 = cpu_to_le32(sgl->word2);
19250 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19251 }
19252 return sglq->sli4_xritag;
19253}
19254
19255/**
19256 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19257 * @phba: Pointer to HBA context object.
19258 * @ring_number: Base sli ring number
19259 * @pwqe: Pointer to command WQE.
19260 **/
19261int
19262lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19263 struct lpfc_iocbq *pwqe)
19264{
205e8240 19265 union lpfc_wqe128 *wqe = &pwqe->wqe;
f358dd0c 19266 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
19267 struct lpfc_queue *wq;
19268 struct lpfc_sglq *sglq;
19269 struct lpfc_sli_ring *pring;
19270 unsigned long iflags;
cd22d605 19271 uint32_t ret = 0;
895427bd
JS
19272
19273 /* NVME_LS and NVME_LS ABTS requests. */
19274 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19275 pring = phba->sli4_hba.nvmels_wq->pring;
19276 spin_lock_irqsave(&pring->ring_lock, iflags);
19277 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19278 if (!sglq) {
19279 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19280 return WQE_BUSY;
19281 }
19282 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19283 pwqe->sli4_xritag = sglq->sli4_xritag;
19284 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19285 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19286 return WQE_ERROR;
19287 }
19288 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19289 pwqe->sli4_xritag);
cd22d605
DK
19290 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19291 if (ret) {
895427bd 19292 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19293 return ret;
895427bd 19294 }
cd22d605 19295
895427bd
JS
19296 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19297 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19298 return 0;
19299 }
19300
19301 /* NVME_FCREQ and NVME_ABTS requests */
19302 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19303 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19304 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19305
19306 spin_lock_irqsave(&pring->ring_lock, iflags);
19307 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19308 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19309 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19310 ret = lpfc_sli4_wq_put(wq, wqe);
19311 if (ret) {
895427bd 19312 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19313 return ret;
895427bd
JS
19314 }
19315 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19316 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19317 return 0;
19318 }
19319
f358dd0c
JS
19320 /* NVMET requests */
19321 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19322 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19323 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19324
19325 spin_lock_irqsave(&pring->ring_lock, iflags);
19326 ctxp = pwqe->context2;
6c621a22 19327 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19328 if (pwqe->sli4_xritag == NO_XRI) {
19329 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19330 pwqe->sli4_xritag = sglq->sli4_xritag;
19331 }
19332 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19333 pwqe->sli4_xritag);
19334 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19335 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19336 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19337 ret = lpfc_sli4_wq_put(wq, wqe);
19338 if (ret) {
f358dd0c 19339 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19340 return ret;
f358dd0c
JS
19341 }
19342 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19343 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19344 return 0;
19345 }
895427bd
JS
19346 return WQE_ERROR;
19347}