]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Add SLI-4 if_type=6 support to the code base
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
128bddac 4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
d080abe0 5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
50611577 6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
c44ce173 9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
10 * *
11 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea3101e
JB
22 *******************************************************************/
23
dea3101e
JB
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
5a0e3ad6 28#include <linux/slab.h>
1c2ba475 29#include <linux/lockdep.h>
dea3101e 30
91886523 31#include <scsi/scsi.h>
dea3101e
JB
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
f888ba3c 35#include <scsi/scsi_transport_fc.h>
da0436e9 36#include <scsi/fc/fc_fs.h>
0d878419 37#include <linux/aer.h>
dea3101e 38
895427bd
JS
39#include <linux/nvme-fc-driver.h>
40
da0436e9 41#include "lpfc_hw4.h"
dea3101e
JB
42#include "lpfc_hw.h"
43#include "lpfc_sli.h"
da0436e9 44#include "lpfc_sli4.h"
ea2151b4 45#include "lpfc_nl.h"
dea3101e 46#include "lpfc_disc.h"
dea3101e 47#include "lpfc.h"
895427bd
JS
48#include "lpfc_scsi.h"
49#include "lpfc_nvme.h"
f358dd0c 50#include "lpfc_nvmet.h"
dea3101e
JB
51#include "lpfc_crtn.h"
52#include "lpfc_logmsg.h"
53#include "lpfc_compat.h"
858c9f6c 54#include "lpfc_debugfs.h"
04c68496 55#include "lpfc_vport.h"
61bda8f7 56#include "lpfc_version.h"
dea3101e
JB
57
58/* There are only four IOCB completion types. */
59typedef enum _lpfc_iocb_type {
60 LPFC_UNKNOWN_IOCB,
61 LPFC_UNSOL_IOCB,
62 LPFC_SOL_IOCB,
63 LPFC_ABORT_IOCB
64} lpfc_iocb_type;
65
4f774513
JS
66
67/* Provide function prototypes local to this module. */
68static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
69 uint32_t);
70static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
71 uint8_t *, uint32_t *);
72static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
73 struct lpfc_iocbq *);
6669f9bb
JS
74static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
75 struct hbq_dmabuf *);
ae9e28f3
JS
76static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
77 struct hbq_dmabuf *dmabuf);
895427bd 78static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
0558056c 79 struct lpfc_cqe *);
895427bd 80static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
8a9d2e80 81 int);
f485c18d
DK
82static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
83 struct lpfc_eqe *eqe, uint32_t qidx);
e8d3c3b1
JS
84static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
85static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
895427bd
JS
86static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
87 struct lpfc_sli_ring *pring,
88 struct lpfc_iocbq *cmdiocb);
0558056c 89
4f774513
JS
90static IOCB_t *
91lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
92{
93 return &iocbq->iocb;
94}
95
96/**
97 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
98 * @q: The Work Queue to operate on.
99 * @wqe: The work Queue Entry to put on the Work queue.
100 *
101 * This routine will copy the contents of @wqe to the next available entry on
102 * the @q. This function will then ring the Work Queue Doorbell to signal the
103 * HBA to start processing the Work Queue Entry. This function returns 0 if
104 * successful. If no entries are available on @q then this function will return
105 * -ENOMEM.
106 * The caller is expected to hold the hbalock when calling this routine.
107 **/
cd22d605 108static int
4f774513
JS
109lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
110{
2e90f4b5 111 union lpfc_wqe *temp_wqe;
4f774513
JS
112 struct lpfc_register doorbell;
113 uint32_t host_index;
027140ea 114 uint32_t idx;
4f774513 115
2e90f4b5
JS
116 /* sanity check on queue memory */
117 if (unlikely(!q))
118 return -ENOMEM;
119 temp_wqe = q->qe[q->host_index].wqe;
120
4f774513 121 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
122 idx = ((q->host_index + 1) % q->entry_count);
123 if (idx == q->hba_index) {
b84daac9 124 q->WQ_overflow++;
cd22d605 125 return -EBUSY;
b84daac9
JS
126 }
127 q->WQ_posted++;
4f774513 128 /* set consumption flag every once in a while */
ff78d8f9 129 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 130 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
04673e38
JS
131 else
132 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
fedd3b7b
JS
133 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
134 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513 135 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
6b3b3bdb
JS
136 /* ensure WQE bcopy flushed before doorbell write */
137 wmb();
4f774513
JS
138
139 /* Update the host index before invoking device */
140 host_index = q->host_index;
027140ea
JS
141
142 q->host_index = idx;
4f774513
JS
143
144 /* Ring Doorbell */
145 doorbell.word0 = 0;
962bc51b
JS
146 if (q->db_format == LPFC_DB_LIST_FORMAT) {
147 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
148 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
149 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
150 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
151 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
152 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
153 } else {
154 return -EINVAL;
155 }
156 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
157
158 return 0;
159}
160
161/**
162 * lpfc_sli4_wq_release - Updates internal hba index for WQ
163 * @q: The Work Queue to operate on.
164 * @index: The index to advance the hba index to.
165 *
166 * This routine will update the HBA index of a queue to reflect consumption of
167 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
168 * an entry the host calls this function to update the queue's internal
169 * pointers. This routine returns the number of entries that were consumed by
170 * the HBA.
171 **/
172static uint32_t
173lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
174{
175 uint32_t released = 0;
176
2e90f4b5
JS
177 /* sanity check on queue memory */
178 if (unlikely(!q))
179 return 0;
180
4f774513
JS
181 if (q->hba_index == index)
182 return 0;
183 do {
184 q->hba_index = ((q->hba_index + 1) % q->entry_count);
185 released++;
186 } while (q->hba_index != index);
187 return released;
188}
189
190/**
191 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
192 * @q: The Mailbox Queue to operate on.
193 * @wqe: The Mailbox Queue Entry to put on the Work queue.
194 *
195 * This routine will copy the contents of @mqe to the next available entry on
196 * the @q. This function will then ring the Work Queue Doorbell to signal the
197 * HBA to start processing the Work Queue Entry. This function returns 0 if
198 * successful. If no entries are available on @q then this function will return
199 * -ENOMEM.
200 * The caller is expected to hold the hbalock when calling this routine.
201 **/
202static uint32_t
203lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
204{
2e90f4b5 205 struct lpfc_mqe *temp_mqe;
4f774513 206 struct lpfc_register doorbell;
4f774513 207
2e90f4b5
JS
208 /* sanity check on queue memory */
209 if (unlikely(!q))
210 return -ENOMEM;
211 temp_mqe = q->qe[q->host_index].mqe;
212
4f774513
JS
213 /* If the host has not yet processed the next entry then we are done */
214 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
215 return -ENOMEM;
216 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
217 /* Save off the mailbox pointer for completion */
218 q->phba->mbox = (MAILBOX_t *)temp_mqe;
219
220 /* Update the host index before invoking device */
4f774513
JS
221 q->host_index = ((q->host_index + 1) % q->entry_count);
222
223 /* Ring Doorbell */
224 doorbell.word0 = 0;
225 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
226 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
227 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
228 return 0;
229}
230
231/**
232 * lpfc_sli4_mq_release - Updates internal hba index for MQ
233 * @q: The Mailbox Queue to operate on.
234 *
235 * This routine will update the HBA index of a queue to reflect consumption of
236 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
237 * an entry the host calls this function to update the queue's internal
238 * pointers. This routine returns the number of entries that were consumed by
239 * the HBA.
240 **/
241static uint32_t
242lpfc_sli4_mq_release(struct lpfc_queue *q)
243{
2e90f4b5
JS
244 /* sanity check on queue memory */
245 if (unlikely(!q))
246 return 0;
247
4f774513
JS
248 /* Clear the mailbox pointer for completion */
249 q->phba->mbox = NULL;
250 q->hba_index = ((q->hba_index + 1) % q->entry_count);
251 return 1;
252}
253
254/**
255 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
256 * @q: The Event Queue to get the first valid EQE from
257 *
258 * This routine will get the first valid Event Queue Entry from @q, update
259 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
260 * the Queue (no more work to do), or the Queue is full of EQEs that have been
261 * processed, but not popped back to the HBA then this routine will return NULL.
262 **/
263static struct lpfc_eqe *
264lpfc_sli4_eq_get(struct lpfc_queue *q)
265{
2e90f4b5 266 struct lpfc_eqe *eqe;
027140ea 267 uint32_t idx;
2e90f4b5
JS
268
269 /* sanity check on queue memory */
270 if (unlikely(!q))
271 return NULL;
272 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
273
274 /* If the next EQE is not valid then we are done */
cb5172ea 275 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
276 return NULL;
277 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
278 idx = ((q->hba_index + 1) % q->entry_count);
279 if (idx == q->host_index)
4f774513
JS
280 return NULL;
281
027140ea 282 q->hba_index = idx;
27f344eb
JS
283
284 /*
285 * insert barrier for instruction interlock : data from the hardware
286 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
287 * upon. Speculative instructions were allowing a bcopy at the start
288 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
289 * after our return, to copy data before the valid bit check above
290 * was done. As such, some of the copied data was stale. The barrier
291 * ensures the check is before any data is copied.
27f344eb
JS
292 */
293 mb();
4f774513
JS
294 return eqe;
295}
296
ba20c853
JS
297/**
298 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
299 * @q: The Event Queue to disable interrupts
300 *
301 **/
b71413dd 302inline void
ba20c853
JS
303lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
304{
305 struct lpfc_register doorbell;
306
307 doorbell.word0 = 0;
308 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
309 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
310 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
311 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
312 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 313 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
ba20c853
JS
314}
315
27d6ac0a
JS
316/**
317 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
318 * @q: The Event Queue to disable interrupts
319 *
320 **/
321inline void
322lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
323{
324 struct lpfc_register doorbell;
325
326 doorbell.word0 = 0;
327 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
329 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
330 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
331 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
332 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
333}
334
4f774513
JS
335/**
336 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
337 * @q: The Event Queue that the host has completed processing for.
338 * @arm: Indicates whether the host wants to arms this CQ.
339 *
340 * This routine will mark all Event Queue Entries on @q, from the last
341 * known completed entry to the last entry that was processed, as completed
342 * by clearing the valid bit for each completion queue entry. Then it will
343 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
344 * The internal host index in the @q will be updated by this routine to indicate
345 * that the host has finished processing the entries. The @arm parameter
346 * indicates that the queue should be rearmed when ringing the doorbell.
347 *
348 * This function will return the number of EQEs that were popped.
349 **/
350uint32_t
351lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
352{
353 uint32_t released = 0;
354 struct lpfc_eqe *temp_eqe;
355 struct lpfc_register doorbell;
356
2e90f4b5
JS
357 /* sanity check on queue memory */
358 if (unlikely(!q))
359 return 0;
360
4f774513
JS
361 /* while there are valid entries */
362 while (q->hba_index != q->host_index) {
363 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 364 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
365 released++;
366 q->host_index = ((q->host_index + 1) % q->entry_count);
367 }
368 if (unlikely(released == 0 && !arm))
369 return 0;
370
371 /* ring doorbell for number popped */
372 doorbell.word0 = 0;
373 if (arm) {
374 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
375 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
376 }
377 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
378 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
379 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
380 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
381 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
9dd35425 382 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
a747c9ce
JS
383 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
384 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
9dd35425 385 readl(q->phba->sli4_hba.EQDBregaddr);
4f774513
JS
386 return released;
387}
388
27d6ac0a
JS
389/**
390 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
391 * @q: The Event Queue that the host has completed processing for.
392 * @arm: Indicates whether the host wants to arms this CQ.
393 *
394 * This routine will mark all Event Queue Entries on @q, from the last
395 * known completed entry to the last entry that was processed, as completed
396 * by clearing the valid bit for each completion queue entry. Then it will
397 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
398 * The internal host index in the @q will be updated by this routine to indicate
399 * that the host has finished processing the entries. The @arm parameter
400 * indicates that the queue should be rearmed when ringing the doorbell.
401 *
402 * This function will return the number of EQEs that were popped.
403 **/
404uint32_t
405lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
406{
407 uint32_t released = 0;
408 struct lpfc_eqe *temp_eqe;
409 struct lpfc_register doorbell;
410
411 /* sanity check on queue memory */
412 if (unlikely(!q))
413 return 0;
414
415 /* while there are valid entries */
416 while (q->hba_index != q->host_index) {
417 temp_eqe = q->qe[q->host_index].eqe;
418 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
419 released++;
420 q->host_index = ((q->host_index + 1) % q->entry_count);
421 }
422 if (unlikely(released == 0 && !arm))
423 return 0;
424
425 /* ring doorbell for number popped */
426 doorbell.word0 = 0;
427 if (arm)
428 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
429 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
430 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
431 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
432 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
433 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
434 readl(q->phba->sli4_hba.EQDBregaddr);
435 return released;
436}
437
4f774513
JS
438/**
439 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
440 * @q: The Completion Queue to get the first valid CQE from
441 *
442 * This routine will get the first valid Completion Queue Entry from @q, update
443 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
444 * the Queue (no more work to do), or the Queue is full of CQEs that have been
445 * processed, but not popped back to the HBA then this routine will return NULL.
446 **/
447static struct lpfc_cqe *
448lpfc_sli4_cq_get(struct lpfc_queue *q)
449{
450 struct lpfc_cqe *cqe;
027140ea 451 uint32_t idx;
4f774513 452
2e90f4b5
JS
453 /* sanity check on queue memory */
454 if (unlikely(!q))
455 return NULL;
456
4f774513 457 /* If the next CQE is not valid then we are done */
cb5172ea 458 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
459 return NULL;
460 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
461 idx = ((q->hba_index + 1) % q->entry_count);
462 if (idx == q->host_index)
4f774513
JS
463 return NULL;
464
465 cqe = q->qe[q->hba_index].cqe;
027140ea 466 q->hba_index = idx;
27f344eb
JS
467
468 /*
469 * insert barrier for instruction interlock : data from the hardware
470 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
471 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
472 * instructions allowing action on content before valid bit checked,
473 * add barrier here as well. May not be needed as "content" is a
474 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
475 */
476 mb();
4f774513
JS
477 return cqe;
478}
479
480/**
481 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
482 * @q: The Completion Queue that the host has completed processing for.
483 * @arm: Indicates whether the host wants to arms this CQ.
484 *
485 * This routine will mark all Completion queue entries on @q, from the last
486 * known completed entry to the last entry that was processed, as completed
487 * by clearing the valid bit for each completion queue entry. Then it will
488 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
489 * The internal host index in the @q will be updated by this routine to indicate
490 * that the host has finished processing the entries. The @arm parameter
491 * indicates that the queue should be rearmed when ringing the doorbell.
492 *
493 * This function will return the number of CQEs that were released.
494 **/
495uint32_t
496lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
497{
498 uint32_t released = 0;
499 struct lpfc_cqe *temp_qe;
500 struct lpfc_register doorbell;
501
2e90f4b5
JS
502 /* sanity check on queue memory */
503 if (unlikely(!q))
504 return 0;
4f774513
JS
505 /* while there are valid entries */
506 while (q->hba_index != q->host_index) {
507 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 508 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
509 released++;
510 q->host_index = ((q->host_index + 1) % q->entry_count);
511 }
512 if (unlikely(released == 0 && !arm))
513 return 0;
514
515 /* ring doorbell for number popped */
516 doorbell.word0 = 0;
517 if (arm)
518 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
519 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
520 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
521 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
522 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
523 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
9dd35425 524 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
4f774513
JS
525 return released;
526}
527
27d6ac0a
JS
528/**
529 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
530 * @q: The Completion Queue that the host has completed processing for.
531 * @arm: Indicates whether the host wants to arms this CQ.
532 *
533 * This routine will mark all Completion queue entries on @q, from the last
534 * known completed entry to the last entry that was processed, as completed
535 * by clearing the valid bit for each completion queue entry. Then it will
536 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
537 * The internal host index in the @q will be updated by this routine to indicate
538 * that the host has finished processing the entries. The @arm parameter
539 * indicates that the queue should be rearmed when ringing the doorbell.
540 *
541 * This function will return the number of CQEs that were released.
542 **/
543uint32_t
544lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
545{
546 uint32_t released = 0;
547 struct lpfc_cqe *temp_qe;
548 struct lpfc_register doorbell;
549
550 /* sanity check on queue memory */
551 if (unlikely(!q))
552 return 0;
553 /* while there are valid entries */
554 while (q->hba_index != q->host_index) {
555 temp_qe = q->qe[q->host_index].cqe;
556 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
557 released++;
558 q->host_index = ((q->host_index + 1) % q->entry_count);
559 }
560 if (unlikely(released == 0 && !arm))
561 return 0;
562
563 /* ring doorbell for number popped */
564 doorbell.word0 = 0;
565 if (arm)
566 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
567 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
568 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
569 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
570 return released;
571}
572
4f774513
JS
573/**
574 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
575 * @q: The Header Receive Queue to operate on.
576 * @wqe: The Receive Queue Entry to put on the Receive queue.
577 *
578 * This routine will copy the contents of @wqe to the next available entry on
579 * the @q. This function will then ring the Receive Queue Doorbell to signal the
580 * HBA to start processing the Receive Queue Entry. This function returns the
581 * index that the rqe was copied to if successful. If no entries are available
582 * on @q then this function will return -ENOMEM.
583 * The caller is expected to hold the hbalock when calling this routine.
584 **/
895427bd 585int
4f774513
JS
586lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
587 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
588{
2e90f4b5
JS
589 struct lpfc_rqe *temp_hrqe;
590 struct lpfc_rqe *temp_drqe;
4f774513 591 struct lpfc_register doorbell;
cbc5de1b
JS
592 int hq_put_index;
593 int dq_put_index;
4f774513 594
2e90f4b5
JS
595 /* sanity check on queue memory */
596 if (unlikely(!hq) || unlikely(!dq))
597 return -ENOMEM;
cbc5de1b
JS
598 hq_put_index = hq->host_index;
599 dq_put_index = dq->host_index;
600 temp_hrqe = hq->qe[hq_put_index].rqe;
601 temp_drqe = dq->qe[dq_put_index].rqe;
2e90f4b5 602
4f774513
JS
603 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
604 return -EINVAL;
cbc5de1b 605 if (hq_put_index != dq_put_index)
4f774513
JS
606 return -EINVAL;
607 /* If the host has not yet processed the next entry then we are done */
cbc5de1b 608 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513
JS
609 return -EBUSY;
610 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
611 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
612
613 /* Update the host index to point to the next slot */
cbc5de1b
JS
614 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
615 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
61f3d4bf 616 hq->RQ_buf_posted++;
4f774513
JS
617
618 /* Ring The Header Receive Queue Doorbell */
73d91e50 619 if (!(hq->host_index % hq->entry_repost)) {
4f774513 620 doorbell.word0 = 0;
962bc51b
JS
621 if (hq->db_format == LPFC_DB_RING_FORMAT) {
622 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
623 hq->entry_repost);
624 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
625 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
626 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
627 hq->entry_repost);
628 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
629 hq->host_index);
630 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
631 } else {
632 return -EINVAL;
633 }
634 writel(doorbell.word0, hq->db_regaddr);
4f774513 635 }
cbc5de1b 636 return hq_put_index;
4f774513
JS
637}
638
639/**
640 * lpfc_sli4_rq_release - Updates internal hba index for RQ
641 * @q: The Header Receive Queue to operate on.
642 *
643 * This routine will update the HBA index of a queue to reflect consumption of
644 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
645 * consumed an entry the host calls this function to update the queue's
646 * internal pointers. This routine returns the number of entries that were
647 * consumed by the HBA.
648 **/
649static uint32_t
650lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
651{
2e90f4b5
JS
652 /* sanity check on queue memory */
653 if (unlikely(!hq) || unlikely(!dq))
654 return 0;
655
4f774513
JS
656 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
657 return 0;
658 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
659 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
660 return 1;
661}
662
e59058c4 663/**
3621a710 664 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
665 * @phba: Pointer to HBA context object.
666 * @pring: Pointer to driver SLI ring object.
667 *
668 * This function returns pointer to next command iocb entry
669 * in the command ring. The caller must hold hbalock to prevent
670 * other threads consume the next command iocb.
671 * SLI-2/SLI-3 provide different sized iocbs.
672 **/
ed957684
JS
673static inline IOCB_t *
674lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
675{
7e56aa25
JS
676 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
677 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
678}
679
e59058c4 680/**
3621a710 681 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
682 * @phba: Pointer to HBA context object.
683 * @pring: Pointer to driver SLI ring object.
684 *
685 * This function returns pointer to next response iocb entry
686 * in the response ring. The caller must hold hbalock to make sure
687 * that no other thread consume the next response iocb.
688 * SLI-2/SLI-3 provide different sized iocbs.
689 **/
ed957684
JS
690static inline IOCB_t *
691lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
692{
7e56aa25
JS
693 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
694 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
695}
696
e59058c4 697/**
3621a710 698 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
699 * @phba: Pointer to HBA context object.
700 *
701 * This function is called with hbalock held. This function
702 * allocates a new driver iocb object from the iocb pool. If the
703 * allocation is successful, it returns pointer to the newly
704 * allocated iocb object else it returns NULL.
705 **/
4f2e66c6 706struct lpfc_iocbq *
2e0fef85 707__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
708{
709 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
710 struct lpfc_iocbq * iocbq = NULL;
711
1c2ba475
JT
712 lockdep_assert_held(&phba->hbalock);
713
0bd4ca25 714 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
715 if (iocbq)
716 phba->iocb_cnt++;
717 if (phba->iocb_cnt > phba->iocb_max)
718 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
719 return iocbq;
720}
721
da0436e9
JS
722/**
723 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
724 * @phba: Pointer to HBA context object.
725 * @xritag: XRI value.
726 *
727 * This function clears the sglq pointer from the array of acive
728 * sglq's. The xritag that is passed in is used to index into the
729 * array. Before the xritag can be used it needs to be adjusted
730 * by subtracting the xribase.
731 *
732 * Returns sglq ponter = success, NULL = Failure.
733 **/
895427bd 734struct lpfc_sglq *
da0436e9
JS
735__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
736{
da0436e9 737 struct lpfc_sglq *sglq;
6d368e53
JS
738
739 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
740 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
741 return sglq;
742}
743
744/**
745 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
746 * @phba: Pointer to HBA context object.
747 * @xritag: XRI value.
748 *
749 * This function returns the sglq pointer from the array of acive
750 * sglq's. The xritag that is passed in is used to index into the
751 * array. Before the xritag can be used it needs to be adjusted
752 * by subtracting the xribase.
753 *
754 * Returns sglq ponter = success, NULL = Failure.
755 **/
0f65ff68 756struct lpfc_sglq *
da0436e9
JS
757__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
758{
da0436e9 759 struct lpfc_sglq *sglq;
6d368e53
JS
760
761 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
762 return sglq;
763}
764
19ca7609 765/**
1151e3ec 766 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
767 * @phba: Pointer to HBA context object.
768 * @xritag: xri used in this exchange.
769 * @rrq: The RRQ to be cleared.
770 *
19ca7609 771 **/
1151e3ec
JS
772void
773lpfc_clr_rrq_active(struct lpfc_hba *phba,
774 uint16_t xritag,
775 struct lpfc_node_rrq *rrq)
19ca7609 776{
1151e3ec 777 struct lpfc_nodelist *ndlp = NULL;
19ca7609 778
1151e3ec
JS
779 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
780 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
781
782 /* The target DID could have been swapped (cable swap)
783 * we should use the ndlp from the findnode if it is
784 * available.
785 */
1151e3ec 786 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
787 ndlp = rrq->ndlp;
788
1151e3ec
JS
789 if (!ndlp)
790 goto out;
791
cff261f6 792 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
793 rrq->send_rrq = 0;
794 rrq->xritag = 0;
795 rrq->rrq_stop_time = 0;
796 }
1151e3ec 797out:
19ca7609
JS
798 mempool_free(rrq, phba->rrq_pool);
799}
800
801/**
802 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
803 * @phba: Pointer to HBA context object.
804 *
805 * This function is called with hbalock held. This function
806 * Checks if stop_time (ratov from setting rrq active) has
807 * been reached, if it has and the send_rrq flag is set then
808 * it will call lpfc_send_rrq. If the send_rrq flag is not set
809 * then it will just call the routine to clear the rrq and
810 * free the rrq resource.
811 * The timer is set to the next rrq that is going to expire before
812 * leaving the routine.
813 *
814 **/
815void
816lpfc_handle_rrq_active(struct lpfc_hba *phba)
817{
818 struct lpfc_node_rrq *rrq;
819 struct lpfc_node_rrq *nextrrq;
820 unsigned long next_time;
821 unsigned long iflags;
1151e3ec 822 LIST_HEAD(send_rrq);
19ca7609
JS
823
824 spin_lock_irqsave(&phba->hbalock, iflags);
825 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 826 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 827 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
828 &phba->active_rrq_list, list) {
829 if (time_after(jiffies, rrq->rrq_stop_time))
830 list_move(&rrq->list, &send_rrq);
831 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
832 next_time = rrq->rrq_stop_time;
833 }
834 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
835 if ((!list_empty(&phba->active_rrq_list)) &&
836 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 837 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
838 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
839 list_del(&rrq->list);
840 if (!rrq->send_rrq)
841 /* this call will free the rrq */
842 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
843 else if (lpfc_send_rrq(phba, rrq)) {
844 /* if we send the rrq then the completion handler
845 * will clear the bit in the xribitmap.
846 */
847 lpfc_clr_rrq_active(phba, rrq->xritag,
848 rrq);
849 }
850 }
19ca7609
JS
851}
852
853/**
854 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
855 * @vport: Pointer to vport context object.
856 * @xri: The xri used in the exchange.
857 * @did: The targets DID for this exchange.
858 *
859 * returns NULL = rrq not found in the phba->active_rrq_list.
860 * rrq = rrq for this xri and target.
861 **/
862struct lpfc_node_rrq *
863lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
864{
865 struct lpfc_hba *phba = vport->phba;
866 struct lpfc_node_rrq *rrq;
867 struct lpfc_node_rrq *nextrrq;
868 unsigned long iflags;
869
870 if (phba->sli_rev != LPFC_SLI_REV4)
871 return NULL;
872 spin_lock_irqsave(&phba->hbalock, iflags);
873 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
874 if (rrq->vport == vport && rrq->xritag == xri &&
875 rrq->nlp_DID == did){
876 list_del(&rrq->list);
877 spin_unlock_irqrestore(&phba->hbalock, iflags);
878 return rrq;
879 }
880 }
881 spin_unlock_irqrestore(&phba->hbalock, iflags);
882 return NULL;
883}
884
885/**
886 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
887 * @vport: Pointer to vport context object.
1151e3ec
JS
888 * @ndlp: Pointer to the lpfc_node_list structure.
889 * If ndlp is NULL Remove all active RRQs for this vport from the
890 * phba->active_rrq_list and clear the rrq.
891 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
892 **/
893void
1151e3ec 894lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
895
896{
897 struct lpfc_hba *phba = vport->phba;
898 struct lpfc_node_rrq *rrq;
899 struct lpfc_node_rrq *nextrrq;
900 unsigned long iflags;
1151e3ec 901 LIST_HEAD(rrq_list);
19ca7609
JS
902
903 if (phba->sli_rev != LPFC_SLI_REV4)
904 return;
1151e3ec
JS
905 if (!ndlp) {
906 lpfc_sli4_vport_delete_els_xri_aborted(vport);
907 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 908 }
1151e3ec
JS
909 spin_lock_irqsave(&phba->hbalock, iflags);
910 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
911 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
912 list_move(&rrq->list, &rrq_list);
19ca7609 913 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
914
915 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
916 list_del(&rrq->list);
917 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
918 }
19ca7609
JS
919}
920
19ca7609 921/**
1151e3ec 922 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
923 * @phba: Pointer to HBA context object.
924 * @ndlp: Targets nodelist pointer for this exchange.
925 * @xritag the xri in the bitmap to test.
926 *
927 * This function is called with hbalock held. This function
928 * returns 0 = rrq not active for this xri
929 * 1 = rrq is valid for this xri.
930 **/
1151e3ec
JS
931int
932lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
933 uint16_t xritag)
934{
1c2ba475 935 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
936 if (!ndlp)
937 return 0;
cff261f6
JS
938 if (!ndlp->active_rrqs_xri_bitmap)
939 return 0;
940 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
941 return 1;
942 else
943 return 0;
944}
945
946/**
947 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
948 * @phba: Pointer to HBA context object.
949 * @ndlp: nodelist pointer for this target.
950 * @xritag: xri used in this exchange.
951 * @rxid: Remote Exchange ID.
952 * @send_rrq: Flag used to determine if we should send rrq els cmd.
953 *
954 * This function takes the hbalock.
955 * The active bit is always set in the active rrq xri_bitmap even
956 * if there is no slot avaiable for the other rrq information.
957 *
958 * returns 0 rrq actived for this xri
959 * < 0 No memory or invalid ndlp.
960 **/
961int
962lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 963 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 964{
19ca7609 965 unsigned long iflags;
b42c07c8
JS
966 struct lpfc_node_rrq *rrq;
967 int empty;
968
969 if (!ndlp)
970 return -EINVAL;
971
972 if (!phba->cfg_enable_rrq)
973 return -EINVAL;
19ca7609
JS
974
975 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
976 if (phba->pport->load_flag & FC_UNLOADING) {
977 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
978 goto out;
979 }
980
981 /*
982 * set the active bit even if there is no mem available.
983 */
984 if (NLP_CHK_FREE_REQ(ndlp))
985 goto out;
986
987 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
988 goto out;
989
cff261f6
JS
990 if (!ndlp->active_rrqs_xri_bitmap)
991 goto out;
992
993 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
994 goto out;
995
19ca7609 996 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
997 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
998 if (!rrq) {
999 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1000 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1001 " DID:0x%x Send:%d\n",
1002 xritag, rxid, ndlp->nlp_DID, send_rrq);
1003 return -EINVAL;
1004 }
e5771b4d
JS
1005 if (phba->cfg_enable_rrq == 1)
1006 rrq->send_rrq = send_rrq;
1007 else
1008 rrq->send_rrq = 0;
b42c07c8 1009 rrq->xritag = xritag;
256ec0d0
JS
1010 rrq->rrq_stop_time = jiffies +
1011 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
1012 rrq->ndlp = ndlp;
1013 rrq->nlp_DID = ndlp->nlp_DID;
1014 rrq->vport = ndlp->vport;
1015 rrq->rxid = rxid;
b42c07c8
JS
1016 spin_lock_irqsave(&phba->hbalock, iflags);
1017 empty = list_empty(&phba->active_rrq_list);
1018 list_add_tail(&rrq->list, &phba->active_rrq_list);
1019 phba->hba_flag |= HBA_RRQ_ACTIVE;
1020 if (empty)
1021 lpfc_worker_wake_up(phba);
1022 spin_unlock_irqrestore(&phba->hbalock, iflags);
1023 return 0;
1024out:
1025 spin_unlock_irqrestore(&phba->hbalock, iflags);
1026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1027 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1028 " DID:0x%x Send:%d\n",
1029 xritag, rxid, ndlp->nlp_DID, send_rrq);
1030 return -EINVAL;
19ca7609
JS
1031}
1032
da0436e9 1033/**
895427bd 1034 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 1035 * @phba: Pointer to HBA context object.
19ca7609 1036 * @piocb: Pointer to the iocbq.
da0436e9 1037 *
dafe8cea 1038 * This function is called with the ring lock held. This function
6d368e53 1039 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
1040 * list is not empty then it is successful, it returns pointer to the newly
1041 * allocated sglq object else it returns NULL.
1042 **/
1043static struct lpfc_sglq *
895427bd 1044__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 1045{
895427bd 1046 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 1047 struct lpfc_sglq *sglq = NULL;
19ca7609 1048 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
1049 struct lpfc_scsi_buf *lpfc_cmd;
1050 struct lpfc_nodelist *ndlp;
1051 int found = 0;
1052
1c2ba475
JT
1053 lockdep_assert_held(&phba->hbalock);
1054
19ca7609
JS
1055 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1056 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1057 ndlp = lpfc_cmd->rdata->pnode;
be858b65 1058 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 1059 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 1060 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
1061 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1062 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1063 ndlp = NULL;
1064 else
1065 ndlp = piocbq->context_un.ndlp;
1066 } else {
19ca7609 1067 ndlp = piocbq->context1;
6c7cf486 1068 }
19ca7609 1069
895427bd
JS
1070 spin_lock(&phba->sli4_hba.sgl_list_lock);
1071 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
1072 start_sglq = sglq;
1073 while (!found) {
1074 if (!sglq)
d11f54b7 1075 break;
895427bd
JS
1076 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1077 test_bit(sglq->sli4_lxritag,
1078 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
1079 /* This xri has an rrq outstanding for this DID.
1080 * put it back in the list and get another xri.
1081 */
895427bd 1082 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 1083 sglq = NULL;
895427bd 1084 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
1085 struct lpfc_sglq, list);
1086 if (sglq == start_sglq) {
14041bd1 1087 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
1088 sglq = NULL;
1089 break;
1090 } else
1091 continue;
1092 }
1093 sglq->ndlp = ndlp;
1094 found = 1;
6d368e53 1095 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
1096 sglq->state = SGL_ALLOCATED;
1097 }
895427bd 1098 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
1099 return sglq;
1100}
1101
f358dd0c
JS
1102/**
1103 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1104 * @phba: Pointer to HBA context object.
1105 * @piocb: Pointer to the iocbq.
1106 *
1107 * This function is called with the sgl_list lock held. This function
1108 * gets a new driver sglq object from the sglq list. If the
1109 * list is not empty then it is successful, it returns pointer to the newly
1110 * allocated sglq object else it returns NULL.
1111 **/
1112struct lpfc_sglq *
1113__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1114{
1115 struct list_head *lpfc_nvmet_sgl_list;
1116 struct lpfc_sglq *sglq = NULL;
1117
1118 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1119
1120 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1121
1122 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1123 if (!sglq)
1124 return NULL;
1125 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1126 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1127 return sglq;
1128}
1129
e59058c4 1130/**
3621a710 1131 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1132 * @phba: Pointer to HBA context object.
1133 *
1134 * This function is called with no lock held. This function
1135 * allocates a new driver iocb object from the iocb pool. If the
1136 * allocation is successful, it returns pointer to the newly
1137 * allocated iocb object else it returns NULL.
1138 **/
2e0fef85
JS
1139struct lpfc_iocbq *
1140lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1141{
1142 struct lpfc_iocbq * iocbq = NULL;
1143 unsigned long iflags;
1144
1145 spin_lock_irqsave(&phba->hbalock, iflags);
1146 iocbq = __lpfc_sli_get_iocbq(phba);
1147 spin_unlock_irqrestore(&phba->hbalock, iflags);
1148 return iocbq;
1149}
1150
4f774513
JS
1151/**
1152 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1153 * @phba: Pointer to HBA context object.
1154 * @iocbq: Pointer to driver iocb object.
1155 *
1156 * This function is called with hbalock held to release driver
1157 * iocb object to the iocb pool. The iotag in the iocb object
1158 * does not change for each use of the iocb object. This function
1159 * clears all other fields of the iocb object when it is freed.
1160 * The sqlq structure that holds the xritag and phys and virtual
1161 * mappings for the scatter gather list is retrieved from the
1162 * active array of sglq. The get of the sglq pointer also clears
1163 * the entry in the array. If the status of the IO indiactes that
1164 * this IO was aborted then the sglq entry it put on the
1165 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1166 * IO has good status or fails for any other reason then the sglq
895427bd 1167 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1168 **/
1169static void
1170__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1171{
1172 struct lpfc_sglq *sglq;
1173 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1174 unsigned long iflag = 0;
895427bd 1175 struct lpfc_sli_ring *pring;
4f774513 1176
1c2ba475
JT
1177 lockdep_assert_held(&phba->hbalock);
1178
4f774513
JS
1179 if (iocbq->sli4_xritag == NO_XRI)
1180 sglq = NULL;
1181 else
6d368e53
JS
1182 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1183
0e9bb8d7 1184
4f774513 1185 if (sglq) {
f358dd0c
JS
1186 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1187 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1188 iflag);
1189 sglq->state = SGL_FREED;
1190 sglq->ndlp = NULL;
1191 list_add_tail(&sglq->list,
1192 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1193 spin_unlock_irqrestore(
1194 &phba->sli4_hba.sgl_list_lock, iflag);
1195 goto out;
1196 }
1197
895427bd 1198 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1199 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1200 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1201 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1202 iflag);
4f774513 1203 list_add(&sglq->list,
895427bd 1204 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1205 spin_unlock_irqrestore(
895427bd 1206 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1207 } else {
895427bd
JS
1208 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1209 iflag);
0f65ff68 1210 sglq->state = SGL_FREED;
19ca7609 1211 sglq->ndlp = NULL;
fedd3b7b 1212 list_add_tail(&sglq->list,
895427bd
JS
1213 &phba->sli4_hba.lpfc_els_sgl_list);
1214 spin_unlock_irqrestore(
1215 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1216
1217 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1218 if (!list_empty(&pring->txq))
2a9bf3d0 1219 lpfc_worker_wake_up(phba);
0f65ff68 1220 }
4f774513
JS
1221 }
1222
f358dd0c 1223out:
4f774513
JS
1224 /*
1225 * Clean all volatile data fields, preserve iotag and node struct.
1226 */
1227 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1228 iocbq->sli4_lxritag = NO_XRI;
4f774513 1229 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1230 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1231 LPFC_IO_NVME_LS);
4f774513
JS
1232 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1233}
1234
2a9bf3d0 1235
e59058c4 1236/**
3772a991 1237 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1238 * @phba: Pointer to HBA context object.
1239 * @iocbq: Pointer to driver iocb object.
1240 *
1241 * This function is called with hbalock held to release driver
1242 * iocb object to the iocb pool. The iotag in the iocb object
1243 * does not change for each use of the iocb object. This function
1244 * clears all other fields of the iocb object when it is freed.
1245 **/
a6ababd2 1246static void
3772a991 1247__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1248{
2e0fef85 1249 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1250
1c2ba475 1251 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1252
604a3e30
JB
1253 /*
1254 * Clean all volatile data fields, preserve iotag and node struct.
1255 */
1256 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1257 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1258 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1259}
1260
3772a991
JS
1261/**
1262 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1263 * @phba: Pointer to HBA context object.
1264 * @iocbq: Pointer to driver iocb object.
1265 *
1266 * This function is called with hbalock held to release driver
1267 * iocb object to the iocb pool. The iotag in the iocb object
1268 * does not change for each use of the iocb object. This function
1269 * clears all other fields of the iocb object when it is freed.
1270 **/
1271static void
1272__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1273{
1c2ba475
JT
1274 lockdep_assert_held(&phba->hbalock);
1275
3772a991 1276 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1277 phba->iocb_cnt--;
3772a991
JS
1278}
1279
e59058c4 1280/**
3621a710 1281 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1282 * @phba: Pointer to HBA context object.
1283 * @iocbq: Pointer to driver iocb object.
1284 *
1285 * This function is called with no lock held to release the iocb to
1286 * iocb pool.
1287 **/
2e0fef85
JS
1288void
1289lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1290{
1291 unsigned long iflags;
1292
1293 /*
1294 * Clean all volatile data fields, preserve iotag and node struct.
1295 */
1296 spin_lock_irqsave(&phba->hbalock, iflags);
1297 __lpfc_sli_release_iocbq(phba, iocbq);
1298 spin_unlock_irqrestore(&phba->hbalock, iflags);
1299}
1300
a257bf90
JS
1301/**
1302 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1303 * @phba: Pointer to HBA context object.
1304 * @iocblist: List of IOCBs.
1305 * @ulpstatus: ULP status in IOCB command field.
1306 * @ulpWord4: ULP word-4 in IOCB command field.
1307 *
1308 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1309 * on the list by invoking the complete callback function associated with the
1310 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1311 * fields.
1312 **/
1313void
1314lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1315 uint32_t ulpstatus, uint32_t ulpWord4)
1316{
1317 struct lpfc_iocbq *piocb;
1318
1319 while (!list_empty(iocblist)) {
1320 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1321 if (!piocb->iocb_cmpl)
1322 lpfc_sli_release_iocbq(phba, piocb);
1323 else {
1324 piocb->iocb.ulpStatus = ulpstatus;
1325 piocb->iocb.un.ulpWord[4] = ulpWord4;
1326 (piocb->iocb_cmpl) (phba, piocb, piocb);
1327 }
1328 }
1329 return;
1330}
1331
e59058c4 1332/**
3621a710
JS
1333 * lpfc_sli_iocb_cmd_type - Get the iocb type
1334 * @iocb_cmnd: iocb command code.
e59058c4
JS
1335 *
1336 * This function is called by ring event handler function to get the iocb type.
1337 * This function translates the iocb command to an iocb command type used to
1338 * decide the final disposition of each completed IOCB.
1339 * The function returns
1340 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1341 * LPFC_SOL_IOCB if it is a solicited iocb completion
1342 * LPFC_ABORT_IOCB if it is an abort iocb
1343 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1344 *
1345 * The caller is not required to hold any lock.
1346 **/
dea3101e
JB
1347static lpfc_iocb_type
1348lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1349{
1350 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1351
1352 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1353 return 0;
1354
1355 switch (iocb_cmnd) {
1356 case CMD_XMIT_SEQUENCE_CR:
1357 case CMD_XMIT_SEQUENCE_CX:
1358 case CMD_XMIT_BCAST_CN:
1359 case CMD_XMIT_BCAST_CX:
1360 case CMD_ELS_REQUEST_CR:
1361 case CMD_ELS_REQUEST_CX:
1362 case CMD_CREATE_XRI_CR:
1363 case CMD_CREATE_XRI_CX:
1364 case CMD_GET_RPI_CN:
1365 case CMD_XMIT_ELS_RSP_CX:
1366 case CMD_GET_RPI_CR:
1367 case CMD_FCP_IWRITE_CR:
1368 case CMD_FCP_IWRITE_CX:
1369 case CMD_FCP_IREAD_CR:
1370 case CMD_FCP_IREAD_CX:
1371 case CMD_FCP_ICMND_CR:
1372 case CMD_FCP_ICMND_CX:
f5603511
JS
1373 case CMD_FCP_TSEND_CX:
1374 case CMD_FCP_TRSP_CX:
1375 case CMD_FCP_TRECEIVE_CX:
1376 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1377 case CMD_ADAPTER_MSG:
1378 case CMD_ADAPTER_DUMP:
1379 case CMD_XMIT_SEQUENCE64_CR:
1380 case CMD_XMIT_SEQUENCE64_CX:
1381 case CMD_XMIT_BCAST64_CN:
1382 case CMD_XMIT_BCAST64_CX:
1383 case CMD_ELS_REQUEST64_CR:
1384 case CMD_ELS_REQUEST64_CX:
1385 case CMD_FCP_IWRITE64_CR:
1386 case CMD_FCP_IWRITE64_CX:
1387 case CMD_FCP_IREAD64_CR:
1388 case CMD_FCP_IREAD64_CX:
1389 case CMD_FCP_ICMND64_CR:
1390 case CMD_FCP_ICMND64_CX:
f5603511
JS
1391 case CMD_FCP_TSEND64_CX:
1392 case CMD_FCP_TRSP64_CX:
1393 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1394 case CMD_GEN_REQUEST64_CR:
1395 case CMD_GEN_REQUEST64_CX:
1396 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1397 case DSSCMD_IWRITE64_CR:
1398 case DSSCMD_IWRITE64_CX:
1399 case DSSCMD_IREAD64_CR:
1400 case DSSCMD_IREAD64_CX:
dea3101e
JB
1401 type = LPFC_SOL_IOCB;
1402 break;
1403 case CMD_ABORT_XRI_CN:
1404 case CMD_ABORT_XRI_CX:
1405 case CMD_CLOSE_XRI_CN:
1406 case CMD_CLOSE_XRI_CX:
1407 case CMD_XRI_ABORTED_CX:
1408 case CMD_ABORT_MXRI64_CN:
6669f9bb 1409 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1410 type = LPFC_ABORT_IOCB;
1411 break;
1412 case CMD_RCV_SEQUENCE_CX:
1413 case CMD_RCV_ELS_REQ_CX:
1414 case CMD_RCV_SEQUENCE64_CX:
1415 case CMD_RCV_ELS_REQ64_CX:
57127f15 1416 case CMD_ASYNC_STATUS:
ed957684
JS
1417 case CMD_IOCB_RCV_SEQ64_CX:
1418 case CMD_IOCB_RCV_ELS64_CX:
1419 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1420 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1421 type = LPFC_UNSOL_IOCB;
1422 break;
3163f725
JS
1423 case CMD_IOCB_XMIT_MSEQ64_CR:
1424 case CMD_IOCB_XMIT_MSEQ64_CX:
1425 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1426 case CMD_IOCB_RCV_ELS_LIST64_CX:
1427 case CMD_IOCB_CLOSE_EXTENDED_CN:
1428 case CMD_IOCB_ABORT_EXTENDED_CN:
1429 case CMD_IOCB_RET_HBQE64_CN:
1430 case CMD_IOCB_FCP_IBIDIR64_CR:
1431 case CMD_IOCB_FCP_IBIDIR64_CX:
1432 case CMD_IOCB_FCP_ITASKMGT64_CX:
1433 case CMD_IOCB_LOGENTRY_CN:
1434 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1435 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1436 __func__, iocb_cmnd);
3163f725
JS
1437 type = LPFC_UNKNOWN_IOCB;
1438 break;
dea3101e
JB
1439 default:
1440 type = LPFC_UNKNOWN_IOCB;
1441 break;
1442 }
1443
1444 return type;
1445}
1446
e59058c4 1447/**
3621a710 1448 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1449 * @phba: Pointer to HBA context object.
1450 *
1451 * This function is called from SLI initialization code
1452 * to configure every ring of the HBA's SLI interface. The
1453 * caller is not required to hold any lock. This function issues
1454 * a config_ring mailbox command for each ring.
1455 * This function returns zero if successful else returns a negative
1456 * error code.
1457 **/
dea3101e 1458static int
ed957684 1459lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1460{
1461 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1462 LPFC_MBOXQ_t *pmb;
1463 MAILBOX_t *pmbox;
1464 int i, rc, ret = 0;
dea3101e 1465
ed957684
JS
1466 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1467 if (!pmb)
1468 return -ENOMEM;
04c68496 1469 pmbox = &pmb->u.mb;
ed957684 1470 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1471 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1472 lpfc_config_ring(phba, i, pmb);
1473 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1474 if (rc != MBX_SUCCESS) {
92d7f7b0 1475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1476 "0446 Adapter failed to init (%d), "
dea3101e
JB
1477 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1478 "ring %d\n",
e8b62011
JS
1479 rc, pmbox->mbxCommand,
1480 pmbox->mbxStatus, i);
2e0fef85 1481 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1482 ret = -ENXIO;
1483 break;
dea3101e
JB
1484 }
1485 }
ed957684
JS
1486 mempool_free(pmb, phba->mbox_mem_pool);
1487 return ret;
dea3101e
JB
1488}
1489
e59058c4 1490/**
3621a710 1491 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1492 * @phba: Pointer to HBA context object.
1493 * @pring: Pointer to driver SLI ring object.
1494 * @piocb: Pointer to the driver iocb object.
1495 *
1496 * This function is called with hbalock held. The function adds the
1497 * new iocb to txcmplq of the given ring. This function always returns
1498 * 0. If this function is called for ELS ring, this function checks if
1499 * there is a vport associated with the ELS command. This function also
1500 * starts els_tmofunc timer if this is an ELS command.
1501 **/
dea3101e 1502static int
2e0fef85
JS
1503lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1504 struct lpfc_iocbq *piocb)
dea3101e 1505{
1c2ba475
JT
1506 lockdep_assert_held(&phba->hbalock);
1507
2319f847 1508 BUG_ON(!piocb);
22466da5 1509
dea3101e 1510 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1511 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1512
92d7f7b0
JS
1513 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1514 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1515 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1516 BUG_ON(!piocb->vport);
1517 if (!(piocb->vport->load_flag & FC_UNLOADING))
1518 mod_timer(&piocb->vport->els_tmofunc,
1519 jiffies +
1520 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1521 }
dea3101e 1522
2e0fef85 1523 return 0;
dea3101e
JB
1524}
1525
e59058c4 1526/**
3621a710 1527 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1528 * @phba: Pointer to HBA context object.
1529 * @pring: Pointer to driver SLI ring object.
1530 *
1531 * This function is called with hbalock held to get next
1532 * iocb in txq of the given ring. If there is any iocb in
1533 * the txq, the function returns first iocb in the list after
1534 * removing the iocb from the list, else it returns NULL.
1535 **/
2a9bf3d0 1536struct lpfc_iocbq *
2e0fef85 1537lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1538{
dea3101e
JB
1539 struct lpfc_iocbq *cmd_iocb;
1540
1c2ba475
JT
1541 lockdep_assert_held(&phba->hbalock);
1542
858c9f6c 1543 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1544 return cmd_iocb;
dea3101e
JB
1545}
1546
e59058c4 1547/**
3621a710 1548 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1549 * @phba: Pointer to HBA context object.
1550 * @pring: Pointer to driver SLI ring object.
1551 *
1552 * This function is called with hbalock held and the caller must post the
1553 * iocb without releasing the lock. If the caller releases the lock,
1554 * iocb slot returned by the function is not guaranteed to be available.
1555 * The function returns pointer to the next available iocb slot if there
1556 * is available slot in the ring, else it returns NULL.
1557 * If the get index of the ring is ahead of the put index, the function
1558 * will post an error attention event to the worker thread to take the
1559 * HBA to offline state.
1560 **/
dea3101e
JB
1561static IOCB_t *
1562lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1563{
34b02dcd 1564 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1565 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1566
1567 lockdep_assert_held(&phba->hbalock);
1568
7e56aa25
JS
1569 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1570 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1571 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1572
7e56aa25
JS
1573 if (unlikely(pring->sli.sli3.local_getidx ==
1574 pring->sli.sli3.next_cmdidx)) {
dea3101e 1575
7e56aa25 1576 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1577
7e56aa25 1578 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1580 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1581 "is bigger than cmd ring %d\n",
e8b62011 1582 pring->ringno,
7e56aa25
JS
1583 pring->sli.sli3.local_getidx,
1584 max_cmd_idx);
dea3101e 1585
2e0fef85 1586 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1587 /*
1588 * All error attention handlers are posted to
1589 * worker thread
1590 */
1591 phba->work_ha |= HA_ERATT;
1592 phba->work_hs = HS_FFER3;
92d7f7b0 1593
5e9d9b82 1594 lpfc_worker_wake_up(phba);
dea3101e
JB
1595
1596 return NULL;
1597 }
1598
7e56aa25 1599 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1600 return NULL;
1601 }
1602
ed957684 1603 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1604}
1605
e59058c4 1606/**
3621a710 1607 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1608 * @phba: Pointer to HBA context object.
1609 * @iocbq: Pointer to driver iocb object.
1610 *
1611 * This function gets an iotag for the iocb. If there is no unused iotag and
1612 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1613 * array and assigns a new iotag.
1614 * The function returns the allocated iotag if successful, else returns zero.
1615 * Zero is not a valid iotag.
1616 * The caller is not required to hold any lock.
1617 **/
604a3e30 1618uint16_t
2e0fef85 1619lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1620{
2e0fef85
JS
1621 struct lpfc_iocbq **new_arr;
1622 struct lpfc_iocbq **old_arr;
604a3e30
JB
1623 size_t new_len;
1624 struct lpfc_sli *psli = &phba->sli;
1625 uint16_t iotag;
dea3101e 1626
2e0fef85 1627 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1628 iotag = psli->last_iotag;
1629 if(++iotag < psli->iocbq_lookup_len) {
1630 psli->last_iotag = iotag;
1631 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1632 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1633 iocbq->iotag = iotag;
1634 return iotag;
2e0fef85 1635 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1636 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1637 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1638 spin_unlock_irq(&phba->hbalock);
1639 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1640 GFP_KERNEL);
1641 if (new_arr) {
2e0fef85 1642 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1643 old_arr = psli->iocbq_lookup;
1644 if (new_len <= psli->iocbq_lookup_len) {
1645 /* highly unprobable case */
1646 kfree(new_arr);
1647 iotag = psli->last_iotag;
1648 if(++iotag < psli->iocbq_lookup_len) {
1649 psli->last_iotag = iotag;
1650 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1651 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1652 iocbq->iotag = iotag;
1653 return iotag;
1654 }
2e0fef85 1655 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1656 return 0;
1657 }
1658 if (psli->iocbq_lookup)
1659 memcpy(new_arr, old_arr,
1660 ((psli->last_iotag + 1) *
311464ec 1661 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1662 psli->iocbq_lookup = new_arr;
1663 psli->iocbq_lookup_len = new_len;
1664 psli->last_iotag = iotag;
1665 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1666 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1667 iocbq->iotag = iotag;
1668 kfree(old_arr);
1669 return iotag;
1670 }
8f6d98d2 1671 } else
2e0fef85 1672 spin_unlock_irq(&phba->hbalock);
dea3101e 1673
bc73905a 1674 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1675 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1676 psli->last_iotag);
dea3101e 1677
604a3e30 1678 return 0;
dea3101e
JB
1679}
1680
e59058c4 1681/**
3621a710 1682 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1683 * @phba: Pointer to HBA context object.
1684 * @pring: Pointer to driver SLI ring object.
1685 * @iocb: Pointer to iocb slot in the ring.
1686 * @nextiocb: Pointer to driver iocb object which need to be
1687 * posted to firmware.
1688 *
1689 * This function is called with hbalock held to post a new iocb to
1690 * the firmware. This function copies the new iocb to ring iocb slot and
1691 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1692 * a completion call back for this iocb else the function will free the
1693 * iocb object.
1694 **/
dea3101e
JB
1695static void
1696lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1697 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1698{
1c2ba475 1699 lockdep_assert_held(&phba->hbalock);
dea3101e 1700 /*
604a3e30 1701 * Set up an iotag
dea3101e 1702 */
604a3e30 1703 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1704
e2a0a9d6 1705
a58cbd52
JS
1706 if (pring->ringno == LPFC_ELS_RING) {
1707 lpfc_debugfs_slow_ring_trc(phba,
1708 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1709 *(((uint32_t *) &nextiocb->iocb) + 4),
1710 *(((uint32_t *) &nextiocb->iocb) + 6),
1711 *(((uint32_t *) &nextiocb->iocb) + 7));
1712 }
1713
dea3101e
JB
1714 /*
1715 * Issue iocb command to adapter
1716 */
92d7f7b0 1717 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1718 wmb();
1719 pring->stats.iocb_cmd++;
1720
1721 /*
1722 * If there is no completion routine to call, we can release the
1723 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1724 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1725 */
1726 if (nextiocb->iocb_cmpl)
1727 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1728 else
2e0fef85 1729 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1730
1731 /*
1732 * Let the HBA know what IOCB slot will be the next one the
1733 * driver will put a command into.
1734 */
7e56aa25
JS
1735 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1736 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1737}
1738
e59058c4 1739/**
3621a710 1740 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1741 * @phba: Pointer to HBA context object.
1742 * @pring: Pointer to driver SLI ring object.
1743 *
1744 * The caller is not required to hold any lock for calling this function.
1745 * This function updates the chip attention bits for the ring to inform firmware
1746 * that there are pending work to be done for this ring and requests an
1747 * interrupt when there is space available in the ring. This function is
1748 * called when the driver is unable to post more iocbs to the ring due
1749 * to unavailability of space in the ring.
1750 **/
dea3101e 1751static void
2e0fef85 1752lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1753{
1754 int ringno = pring->ringno;
1755
1756 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1757
1758 wmb();
1759
1760 /*
1761 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1762 * The HBA will tell us when an IOCB entry is available.
1763 */
1764 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1765 readl(phba->CAregaddr); /* flush */
1766
1767 pring->stats.iocb_cmd_full++;
1768}
1769
e59058c4 1770/**
3621a710 1771 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1772 * @phba: Pointer to HBA context object.
1773 * @pring: Pointer to driver SLI ring object.
1774 *
1775 * This function updates the chip attention register bit for the
1776 * given ring to inform HBA that there is more work to be done
1777 * in this ring. The caller is not required to hold any lock.
1778 **/
dea3101e 1779static void
2e0fef85 1780lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1781{
1782 int ringno = pring->ringno;
1783
1784 /*
1785 * Tell the HBA that there is work to do in this ring.
1786 */
34b02dcd
JS
1787 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1788 wmb();
1789 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1790 readl(phba->CAregaddr); /* flush */
1791 }
dea3101e
JB
1792}
1793
e59058c4 1794/**
3621a710 1795 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1796 * @phba: Pointer to HBA context object.
1797 * @pring: Pointer to driver SLI ring object.
1798 *
1799 * This function is called with hbalock held to post pending iocbs
1800 * in the txq to the firmware. This function is called when driver
1801 * detects space available in the ring.
1802 **/
dea3101e 1803static void
2e0fef85 1804lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1805{
1806 IOCB_t *iocb;
1807 struct lpfc_iocbq *nextiocb;
1808
1c2ba475
JT
1809 lockdep_assert_held(&phba->hbalock);
1810
dea3101e
JB
1811 /*
1812 * Check to see if:
1813 * (a) there is anything on the txq to send
1814 * (b) link is up
1815 * (c) link attention events can be processed (fcp ring only)
1816 * (d) IOCB processing is not blocked by the outstanding mbox command.
1817 */
0e9bb8d7
JS
1818
1819 if (lpfc_is_link_up(phba) &&
1820 (!list_empty(&pring->txq)) &&
895427bd 1821 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1822 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1823
1824 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1825 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1826 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1827
1828 if (iocb)
1829 lpfc_sli_update_ring(phba, pring);
1830 else
1831 lpfc_sli_update_full_ring(phba, pring);
1832 }
1833
1834 return;
1835}
1836
e59058c4 1837/**
3621a710 1838 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1839 * @phba: Pointer to HBA context object.
1840 * @hbqno: HBQ number.
1841 *
1842 * This function is called with hbalock held to get the next
1843 * available slot for the given HBQ. If there is free slot
1844 * available for the HBQ it will return pointer to the next available
1845 * HBQ entry else it will return NULL.
1846 **/
a6ababd2 1847static struct lpfc_hbq_entry *
ed957684
JS
1848lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1849{
1850 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1851
1c2ba475
JT
1852 lockdep_assert_held(&phba->hbalock);
1853
ed957684
JS
1854 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1855 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1856 hbqp->next_hbqPutIdx = 0;
1857
1858 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1859 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1860 uint32_t getidx = le32_to_cpu(raw_index);
1861
1862 hbqp->local_hbqGetIdx = getidx;
1863
1864 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1865 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1866 LOG_SLI | LOG_VPORT,
e8b62011 1867 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1868 "%u is > than hbqp->entry_count %u\n",
e8b62011 1869 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1870 hbqp->entry_count);
1871
1872 phba->link_state = LPFC_HBA_ERROR;
1873 return NULL;
1874 }
1875
1876 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1877 return NULL;
1878 }
1879
51ef4c26
JS
1880 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1881 hbqp->hbqPutIdx;
ed957684
JS
1882}
1883
e59058c4 1884/**
3621a710 1885 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1886 * @phba: Pointer to HBA context object.
1887 *
1888 * This function is called with no lock held to free all the
1889 * hbq buffers while uninitializing the SLI interface. It also
1890 * frees the HBQ buffers returned by the firmware but not yet
1891 * processed by the upper layers.
1892 **/
ed957684
JS
1893void
1894lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1895{
92d7f7b0
JS
1896 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1897 struct hbq_dmabuf *hbq_buf;
3163f725 1898 unsigned long flags;
51ef4c26 1899 int i, hbq_count;
ed957684 1900
51ef4c26 1901 hbq_count = lpfc_sli_hbq_count();
ed957684 1902 /* Return all memory used by all HBQs */
3163f725 1903 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1904 for (i = 0; i < hbq_count; ++i) {
1905 list_for_each_entry_safe(dmabuf, next_dmabuf,
1906 &phba->hbqs[i].hbq_buffer_list, list) {
1907 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1908 list_del(&hbq_buf->dbuf.list);
1909 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1910 }
a8adb832 1911 phba->hbqs[i].buffer_count = 0;
ed957684 1912 }
3163f725
JS
1913
1914 /* Mark the HBQs not in use */
1915 phba->hbq_in_use = 0;
1916 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1917}
1918
e59058c4 1919/**
3621a710 1920 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1921 * @phba: Pointer to HBA context object.
1922 * @hbqno: HBQ number.
1923 * @hbq_buf: Pointer to HBQ buffer.
1924 *
1925 * This function is called with the hbalock held to post a
1926 * hbq buffer to the firmware. If the function finds an empty
1927 * slot in the HBQ, it will post the buffer. The function will return
1928 * pointer to the hbq entry if it successfully post the buffer
1929 * else it will return NULL.
1930 **/
3772a991 1931static int
ed957684 1932lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1933 struct hbq_dmabuf *hbq_buf)
3772a991 1934{
1c2ba475 1935 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1936 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1937}
1938
1939/**
1940 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1941 * @phba: Pointer to HBA context object.
1942 * @hbqno: HBQ number.
1943 * @hbq_buf: Pointer to HBQ buffer.
1944 *
1945 * This function is called with the hbalock held to post a hbq buffer to the
1946 * firmware. If the function finds an empty slot in the HBQ, it will post the
1947 * buffer and place it on the hbq_buffer_list. The function will return zero if
1948 * it successfully post the buffer else it will return an error.
1949 **/
1950static int
1951lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1952 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1953{
1954 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1955 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 1956
1c2ba475 1957 lockdep_assert_held(&phba->hbalock);
ed957684
JS
1958 /* Get next HBQ entry slot to use */
1959 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1960 if (hbqe) {
1961 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1962
92d7f7b0
JS
1963 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1964 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 1965 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 1966 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1967 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1968 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1969 /* Sync SLIM */
ed957684
JS
1970 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1971 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1972 /* flush */
ed957684 1973 readl(phba->hbq_put + hbqno);
51ef4c26 1974 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1975 return 0;
1976 } else
1977 return -ENOMEM;
ed957684
JS
1978}
1979
4f774513
JS
1980/**
1981 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1982 * @phba: Pointer to HBA context object.
1983 * @hbqno: HBQ number.
1984 * @hbq_buf: Pointer to HBQ buffer.
1985 *
1986 * This function is called with the hbalock held to post an RQE to the SLI4
1987 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1988 * the hbq_buffer_list and return zero, otherwise it will return an error.
1989 **/
1990static int
1991lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1992 struct hbq_dmabuf *hbq_buf)
1993{
1994 int rc;
1995 struct lpfc_rqe hrqe;
1996 struct lpfc_rqe drqe;
895427bd
JS
1997 struct lpfc_queue *hrq;
1998 struct lpfc_queue *drq;
1999
2000 if (hbqno != LPFC_ELS_HBQ)
2001 return 1;
2002 hrq = phba->sli4_hba.hdr_rq;
2003 drq = phba->sli4_hba.dat_rq;
4f774513 2004
1c2ba475 2005 lockdep_assert_held(&phba->hbalock);
4f774513
JS
2006 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2007 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2008 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2009 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 2010 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
2011 if (rc < 0)
2012 return rc;
895427bd 2013 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
2014 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2015 return 0;
2016}
2017
e59058c4 2018/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
2019static struct lpfc_hbq_init lpfc_els_hbq = {
2020 .rn = 1,
def9c7a9 2021 .entry_count = 256,
92d7f7b0
JS
2022 .mask_count = 0,
2023 .profile = 0,
51ef4c26 2024 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 2025 .buffer_count = 0,
a257bf90
JS
2026 .init_count = 40,
2027 .add_count = 40,
92d7f7b0 2028};
ed957684 2029
e59058c4 2030/* Array of HBQs */
78b2d852 2031struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
2032 &lpfc_els_hbq,
2033};
ed957684 2034
e59058c4 2035/**
3621a710 2036 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
2037 * @phba: Pointer to HBA context object.
2038 * @hbqno: HBQ number.
2039 * @count: Number of HBQ buffers to be posted.
2040 *
d7c255b2
JS
2041 * This function is called with no lock held to post more hbq buffers to the
2042 * given HBQ. The function returns the number of HBQ buffers successfully
2043 * posted.
e59058c4 2044 **/
311464ec 2045static int
92d7f7b0 2046lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 2047{
d7c255b2 2048 uint32_t i, posted = 0;
3163f725 2049 unsigned long flags;
92d7f7b0 2050 struct hbq_dmabuf *hbq_buffer;
d7c255b2 2051 LIST_HEAD(hbq_buf_list);
eafe1df9 2052 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 2053 return 0;
51ef4c26 2054
d7c255b2
JS
2055 if ((phba->hbqs[hbqno].buffer_count + count) >
2056 lpfc_hbq_defs[hbqno]->entry_count)
2057 count = lpfc_hbq_defs[hbqno]->entry_count -
2058 phba->hbqs[hbqno].buffer_count;
2059 if (!count)
2060 return 0;
2061 /* Allocate HBQ entries */
2062 for (i = 0; i < count; i++) {
2063 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2064 if (!hbq_buffer)
2065 break;
2066 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2067 }
3163f725
JS
2068 /* Check whether HBQ is still in use */
2069 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 2070 if (!phba->hbq_in_use)
d7c255b2
JS
2071 goto err;
2072 while (!list_empty(&hbq_buf_list)) {
2073 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2074 dbuf.list);
2075 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2076 (hbqno << 16));
3772a991 2077 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 2078 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
2079 posted++;
2080 } else
51ef4c26 2081 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 2082 }
3163f725 2083 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2084 return posted;
2085err:
eafe1df9 2086 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
2087 while (!list_empty(&hbq_buf_list)) {
2088 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2089 dbuf.list);
2090 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2091 }
2092 return 0;
ed957684
JS
2093}
2094
e59058c4 2095/**
3621a710 2096 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
2097 * @phba: Pointer to HBA context object.
2098 * @qno: HBQ number.
2099 *
2100 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
2101 * is called with no lock held. The function returns the number of HBQ entries
2102 * successfully allocated.
e59058c4 2103 **/
92d7f7b0
JS
2104int
2105lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 2106{
def9c7a9
JS
2107 if (phba->sli_rev == LPFC_SLI_REV4)
2108 return 0;
2109 else
2110 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2111 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 2112}
ed957684 2113
e59058c4 2114/**
3621a710 2115 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2116 * @phba: Pointer to HBA context object.
2117 * @qno: HBQ queue number.
2118 *
2119 * This function is called from SLI initialization code path with
2120 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2121 * function returns the number of HBQ entries successfully allocated.
e59058c4 2122 **/
a6ababd2 2123static int
92d7f7b0
JS
2124lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2125{
def9c7a9
JS
2126 if (phba->sli_rev == LPFC_SLI_REV4)
2127 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2128 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2129 else
2130 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2131 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2132}
2133
3772a991
JS
2134/**
2135 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2136 * @phba: Pointer to HBA context object.
2137 * @hbqno: HBQ number.
2138 *
2139 * This function removes the first hbq buffer on an hbq list and returns a
2140 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2141 **/
2142static struct hbq_dmabuf *
2143lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2144{
2145 struct lpfc_dmabuf *d_buf;
2146
2147 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2148 if (!d_buf)
2149 return NULL;
2150 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2151}
2152
2d7dbc4c
JS
2153/**
2154 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2155 * @phba: Pointer to HBA context object.
2156 * @hbqno: HBQ number.
2157 *
2158 * This function removes the first RQ buffer on an RQ buffer list and returns a
2159 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2160 **/
2161static struct rqb_dmabuf *
2162lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2163{
2164 struct lpfc_dmabuf *h_buf;
2165 struct lpfc_rqb *rqbp;
2166
2167 rqbp = hrq->rqbp;
2168 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2169 struct lpfc_dmabuf, list);
2170 if (!h_buf)
2171 return NULL;
2172 rqbp->buffer_count--;
2173 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2174}
2175
e59058c4 2176/**
3621a710 2177 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2178 * @phba: Pointer to HBA context object.
2179 * @tag: Tag of the hbq buffer.
2180 *
71892418
SH
2181 * This function searches for the hbq buffer associated with the given tag in
2182 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2183 * otherwise it returns NULL.
e59058c4 2184 **/
a6ababd2 2185static struct hbq_dmabuf *
92d7f7b0 2186lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2187{
92d7f7b0
JS
2188 struct lpfc_dmabuf *d_buf;
2189 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2190 uint32_t hbqno;
2191
2192 hbqno = tag >> 16;
a0a74e45 2193 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2194 return NULL;
ed957684 2195
3772a991 2196 spin_lock_irq(&phba->hbalock);
51ef4c26 2197 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2198 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2199 if (hbq_buf->tag == tag) {
3772a991 2200 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2201 return hbq_buf;
ed957684
JS
2202 }
2203 }
3772a991 2204 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2206 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2207 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2208 return NULL;
ed957684
JS
2209}
2210
e59058c4 2211/**
3621a710 2212 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2213 * @phba: Pointer to HBA context object.
2214 * @hbq_buffer: Pointer to HBQ buffer.
2215 *
2216 * This function is called with hbalock. This function gives back
2217 * the hbq buffer to firmware. If the HBQ does not have space to
2218 * post the buffer, it will free the buffer.
2219 **/
ed957684 2220void
51ef4c26 2221lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2222{
2223 uint32_t hbqno;
2224
51ef4c26
JS
2225 if (hbq_buffer) {
2226 hbqno = hbq_buffer->tag >> 16;
3772a991 2227 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2228 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2229 }
2230}
2231
e59058c4 2232/**
3621a710 2233 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2234 * @mbxCommand: mailbox command code.
2235 *
2236 * This function is called by the mailbox event handler function to verify
2237 * that the completed mailbox command is a legitimate mailbox command. If the
2238 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2239 * and the mailbox event handler will take the HBA offline.
2240 **/
dea3101e
JB
2241static int
2242lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2243{
2244 uint8_t ret;
2245
2246 switch (mbxCommand) {
2247 case MBX_LOAD_SM:
2248 case MBX_READ_NV:
2249 case MBX_WRITE_NV:
a8adb832 2250 case MBX_WRITE_VPARMS:
dea3101e
JB
2251 case MBX_RUN_BIU_DIAG:
2252 case MBX_INIT_LINK:
2253 case MBX_DOWN_LINK:
2254 case MBX_CONFIG_LINK:
2255 case MBX_CONFIG_RING:
2256 case MBX_RESET_RING:
2257 case MBX_READ_CONFIG:
2258 case MBX_READ_RCONFIG:
2259 case MBX_READ_SPARM:
2260 case MBX_READ_STATUS:
2261 case MBX_READ_RPI:
2262 case MBX_READ_XRI:
2263 case MBX_READ_REV:
2264 case MBX_READ_LNK_STAT:
2265 case MBX_REG_LOGIN:
2266 case MBX_UNREG_LOGIN:
dea3101e
JB
2267 case MBX_CLEAR_LA:
2268 case MBX_DUMP_MEMORY:
2269 case MBX_DUMP_CONTEXT:
2270 case MBX_RUN_DIAGS:
2271 case MBX_RESTART:
2272 case MBX_UPDATE_CFG:
2273 case MBX_DOWN_LOAD:
2274 case MBX_DEL_LD_ENTRY:
2275 case MBX_RUN_PROGRAM:
2276 case MBX_SET_MASK:
09372820 2277 case MBX_SET_VARIABLE:
dea3101e 2278 case MBX_UNREG_D_ID:
41415862 2279 case MBX_KILL_BOARD:
dea3101e 2280 case MBX_CONFIG_FARP:
41415862 2281 case MBX_BEACON:
dea3101e
JB
2282 case MBX_LOAD_AREA:
2283 case MBX_RUN_BIU_DIAG64:
2284 case MBX_CONFIG_PORT:
2285 case MBX_READ_SPARM64:
2286 case MBX_READ_RPI64:
2287 case MBX_REG_LOGIN64:
76a95d75 2288 case MBX_READ_TOPOLOGY:
09372820 2289 case MBX_WRITE_WWN:
dea3101e
JB
2290 case MBX_SET_DEBUG:
2291 case MBX_LOAD_EXP_ROM:
57127f15 2292 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2293 case MBX_REG_VPI:
2294 case MBX_UNREG_VPI:
858c9f6c 2295 case MBX_HEARTBEAT:
84774a4d
JS
2296 case MBX_PORT_CAPABILITIES:
2297 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2298 case MBX_SLI4_CONFIG:
2299 case MBX_SLI4_REQ_FTRS:
2300 case MBX_REG_FCFI:
2301 case MBX_UNREG_FCFI:
2302 case MBX_REG_VFI:
2303 case MBX_UNREG_VFI:
2304 case MBX_INIT_VPI:
2305 case MBX_INIT_VFI:
2306 case MBX_RESUME_RPI:
c7495937
JS
2307 case MBX_READ_EVENT_LOG_STATUS:
2308 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2309 case MBX_SECURITY_MGMT:
2310 case MBX_AUTH_PORT:
940eb687 2311 case MBX_ACCESS_VDATA:
dea3101e
JB
2312 ret = mbxCommand;
2313 break;
2314 default:
2315 ret = MBX_SHUTDOWN;
2316 break;
2317 }
2e0fef85 2318 return ret;
dea3101e 2319}
e59058c4
JS
2320
2321/**
3621a710 2322 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2323 * @phba: Pointer to HBA context object.
2324 * @pmboxq: Pointer to mailbox command.
2325 *
2326 * This is completion handler function for mailbox commands issued from
2327 * lpfc_sli_issue_mbox_wait function. This function is called by the
2328 * mailbox event handler function with no lock held. This function
2329 * will wake up thread waiting on the wait queue pointed by context1
2330 * of the mailbox.
2331 **/
04c68496 2332void
2e0fef85 2333lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2334{
2335 wait_queue_head_t *pdone_q;
858c9f6c 2336 unsigned long drvr_flag;
dea3101e
JB
2337
2338 /*
2339 * If pdone_q is empty, the driver thread gave up waiting and
2340 * continued running.
2341 */
7054a606 2342 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2343 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2344 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2345 if (pdone_q)
2346 wake_up_interruptible(pdone_q);
858c9f6c 2347 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2348 return;
2349}
2350
e59058c4
JS
2351
2352/**
3621a710 2353 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2354 * @phba: Pointer to HBA context object.
2355 * @pmb: Pointer to mailbox object.
2356 *
2357 * This function is the default mailbox completion handler. It
2358 * frees the memory resources associated with the completed mailbox
2359 * command. If the completed command is a REG_LOGIN mailbox command,
2360 * this function will issue a UREG_LOGIN to re-claim the RPI.
2361 **/
dea3101e 2362void
2e0fef85 2363lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2364{
d439d286 2365 struct lpfc_vport *vport = pmb->vport;
dea3101e 2366 struct lpfc_dmabuf *mp;
d439d286 2367 struct lpfc_nodelist *ndlp;
5af5eee7 2368 struct Scsi_Host *shost;
04c68496 2369 uint16_t rpi, vpi;
7054a606
JS
2370 int rc;
2371
dea3101e 2372 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2373
dea3101e
JB
2374 if (mp) {
2375 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2376 kfree(mp);
2377 }
7054a606
JS
2378
2379 /*
2380 * If a REG_LOGIN succeeded after node is destroyed or node
2381 * is in re-discovery driver need to cleanup the RPI.
2382 */
2e0fef85 2383 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2384 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2385 !pmb->u.mb.mbxStatus) {
2386 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2387 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2388 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2389 pmb->vport = vport;
92d7f7b0 2390 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2391 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2392 if (rc != MBX_NOT_FINISHED)
2393 return;
2394 }
2395
695a814e
JS
2396 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2397 !(phba->pport->load_flag & FC_UNLOADING) &&
2398 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2399 shost = lpfc_shost_from_vport(vport);
2400 spin_lock_irq(shost->host_lock);
2401 vport->vpi_state |= LPFC_VPI_REGISTERED;
2402 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2403 spin_unlock_irq(shost->host_lock);
695a814e
JS
2404 }
2405
d439d286
JS
2406 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2407 ndlp = (struct lpfc_nodelist *)pmb->context2;
2408 lpfc_nlp_put(ndlp);
2409 pmb->context2 = NULL;
2410 }
2411
dcf2a4e0
JS
2412 /* Check security permission status on INIT_LINK mailbox command */
2413 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2414 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2415 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2416 "2860 SLI authentication is required "
2417 "for INIT_LINK but has not done yet\n");
2418
04c68496
JS
2419 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2420 lpfc_sli4_mbox_cmd_free(phba, pmb);
2421 else
2422 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2423}
be6bb941
JS
2424 /**
2425 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2426 * @phba: Pointer to HBA context object.
2427 * @pmb: Pointer to mailbox object.
2428 *
2429 * This function is the unreg rpi mailbox completion handler. It
2430 * frees the memory resources associated with the completed mailbox
2431 * command. An additional refrenece is put on the ndlp to prevent
2432 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2433 * the unreg mailbox command completes, this routine puts the
2434 * reference back.
2435 *
2436 **/
2437void
2438lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2439{
2440 struct lpfc_vport *vport = pmb->vport;
2441 struct lpfc_nodelist *ndlp;
2442
2443 ndlp = pmb->context1;
2444 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2445 if (phba->sli_rev == LPFC_SLI_REV4 &&
2446 (bf_get(lpfc_sli_intf_if_type,
27d6ac0a 2447 &phba->sli4_hba.sli_intf) >=
be6bb941
JS
2448 LPFC_SLI_INTF_IF_TYPE_2)) {
2449 if (ndlp) {
2450 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2451 "0010 UNREG_LOGIN vpi:%x "
2452 "rpi:%x DID:%x map:%x %p\n",
2453 vport->vpi, ndlp->nlp_rpi,
2454 ndlp->nlp_DID,
2455 ndlp->nlp_usg_map, ndlp);
7c5e518c 2456 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2457 lpfc_nlp_put(ndlp);
2458 }
2459 }
2460 }
2461
2462 mempool_free(pmb, phba->mbox_mem_pool);
2463}
dea3101e 2464
e59058c4 2465/**
3621a710 2466 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2467 * @phba: Pointer to HBA context object.
2468 *
2469 * This function is called with no lock held. This function processes all
2470 * the completed mailbox commands and gives it to upper layers. The interrupt
2471 * service routine processes mailbox completion interrupt and adds completed
2472 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2473 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2474 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2475 * function returns the mailbox commands to the upper layer by calling the
2476 * completion handler function of each mailbox.
2477 **/
dea3101e 2478int
2e0fef85 2479lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2480{
92d7f7b0 2481 MAILBOX_t *pmbox;
dea3101e 2482 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2483 int rc;
2484 LIST_HEAD(cmplq);
dea3101e
JB
2485
2486 phba->sli.slistat.mbox_event++;
2487
92d7f7b0
JS
2488 /* Get all completed mailboxe buffers into the cmplq */
2489 spin_lock_irq(&phba->hbalock);
2490 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2491 spin_unlock_irq(&phba->hbalock);
dea3101e 2492
92d7f7b0
JS
2493 /* Get a Mailbox buffer to setup mailbox commands for callback */
2494 do {
2495 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2496 if (pmb == NULL)
2497 break;
2e0fef85 2498
04c68496 2499 pmbox = &pmb->u.mb;
dea3101e 2500
858c9f6c
JS
2501 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2502 if (pmb->vport) {
2503 lpfc_debugfs_disc_trc(pmb->vport,
2504 LPFC_DISC_TRC_MBOX_VPORT,
2505 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2506 (uint32_t)pmbox->mbxCommand,
2507 pmbox->un.varWords[0],
2508 pmbox->un.varWords[1]);
2509 }
2510 else {
2511 lpfc_debugfs_disc_trc(phba->pport,
2512 LPFC_DISC_TRC_MBOX,
2513 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2514 (uint32_t)pmbox->mbxCommand,
2515 pmbox->un.varWords[0],
2516 pmbox->un.varWords[1]);
2517 }
2518 }
2519
dea3101e
JB
2520 /*
2521 * It is a fatal error if unknown mbox command completion.
2522 */
2523 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2524 MBX_SHUTDOWN) {
af901ca1 2525 /* Unknown mailbox command compl */
92d7f7b0 2526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2527 "(%d):0323 Unknown Mailbox command "
a183a15f 2528 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2529 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2530 pmbox->mbxCommand,
a183a15f
JS
2531 lpfc_sli_config_mbox_subsys_get(phba,
2532 pmb),
2533 lpfc_sli_config_mbox_opcode_get(phba,
2534 pmb));
2e0fef85 2535 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2536 phba->work_hs = HS_FFER3;
2537 lpfc_handle_eratt(phba);
92d7f7b0 2538 continue;
dea3101e
JB
2539 }
2540
dea3101e
JB
2541 if (pmbox->mbxStatus) {
2542 phba->sli.slistat.mbox_stat_err++;
2543 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2544 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2545 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2546 LOG_MBOX | LOG_SLI,
2547 "(%d):0305 Mbox cmd cmpl "
2548 "error - RETRYing Data: x%x "
2549 "(x%x/x%x) x%x x%x x%x\n",
2550 pmb->vport ? pmb->vport->vpi : 0,
2551 pmbox->mbxCommand,
2552 lpfc_sli_config_mbox_subsys_get(phba,
2553 pmb),
2554 lpfc_sli_config_mbox_opcode_get(phba,
2555 pmb),
2556 pmbox->mbxStatus,
2557 pmbox->un.varWords[0],
2558 pmb->vport->port_state);
dea3101e
JB
2559 pmbox->mbxStatus = 0;
2560 pmbox->mbxOwner = OWN_HOST;
dea3101e 2561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2562 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2563 continue;
dea3101e
JB
2564 }
2565 }
2566
2567 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2568 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2569 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2570 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2571 "x%x x%x x%x\n",
92d7f7b0 2572 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2573 pmbox->mbxCommand,
a183a15f
JS
2574 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2575 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2576 pmb->mbox_cmpl,
2577 *((uint32_t *) pmbox),
2578 pmbox->un.varWords[0],
2579 pmbox->un.varWords[1],
2580 pmbox->un.varWords[2],
2581 pmbox->un.varWords[3],
2582 pmbox->un.varWords[4],
2583 pmbox->un.varWords[5],
2584 pmbox->un.varWords[6],
e74c03c8
JS
2585 pmbox->un.varWords[7],
2586 pmbox->un.varWords[8],
2587 pmbox->un.varWords[9],
2588 pmbox->un.varWords[10]);
dea3101e 2589
92d7f7b0 2590 if (pmb->mbox_cmpl)
dea3101e 2591 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2592 } while (1);
2593 return 0;
2594}
dea3101e 2595
e59058c4 2596/**
3621a710 2597 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2598 * @phba: Pointer to HBA context object.
2599 * @pring: Pointer to driver SLI ring object.
2600 * @tag: buffer tag.
2601 *
2602 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2603 * is set in the tag the buffer is posted for a particular exchange,
2604 * the function will return the buffer without replacing the buffer.
2605 * If the buffer is for unsolicited ELS or CT traffic, this function
2606 * returns the buffer and also posts another buffer to the firmware.
2607 **/
76bb24ef
JS
2608static struct lpfc_dmabuf *
2609lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2610 struct lpfc_sli_ring *pring,
2611 uint32_t tag)
76bb24ef 2612{
9f1e1b50
JS
2613 struct hbq_dmabuf *hbq_entry;
2614
76bb24ef
JS
2615 if (tag & QUE_BUFTAG_BIT)
2616 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2617 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2618 if (!hbq_entry)
2619 return NULL;
2620 return &hbq_entry->dbuf;
76bb24ef 2621}
57127f15 2622
3772a991
JS
2623/**
2624 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2625 * @phba: Pointer to HBA context object.
2626 * @pring: Pointer to driver SLI ring object.
2627 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2628 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2629 * @fch_type: the type for the first frame of the sequence.
2630 *
2631 * This function is called with no lock held. This function uses the r_ctl and
2632 * type of the received sequence to find the correct callback function to call
2633 * to process the sequence.
2634 **/
2635static int
2636lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2637 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2638 uint32_t fch_type)
2639{
2640 int i;
2641
f358dd0c
JS
2642 switch (fch_type) {
2643 case FC_TYPE_NVME:
d613b6a7 2644 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2645 return 1;
2646 default:
2647 break;
2648 }
2649
3772a991
JS
2650 /* unSolicited Responses */
2651 if (pring->prt[0].profile) {
2652 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2653 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2654 saveq);
2655 return 1;
2656 }
2657 /* We must search, based on rctl / type
2658 for the right routine */
2659 for (i = 0; i < pring->num_mask; i++) {
2660 if ((pring->prt[i].rctl == fch_r_ctl) &&
2661 (pring->prt[i].type == fch_type)) {
2662 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2663 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2664 (phba, pring, saveq);
2665 return 1;
2666 }
2667 }
2668 return 0;
2669}
e59058c4
JS
2670
2671/**
3621a710 2672 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2673 * @phba: Pointer to HBA context object.
2674 * @pring: Pointer to driver SLI ring object.
2675 * @saveq: Pointer to the unsolicited iocb.
2676 *
2677 * This function is called with no lock held by the ring event handler
2678 * when there is an unsolicited iocb posted to the response ring by the
2679 * firmware. This function gets the buffer associated with the iocbs
2680 * and calls the event handler for the ring. This function handles both
2681 * qring buffers and hbq buffers.
2682 * When the function returns 1 the caller can free the iocb object otherwise
2683 * upper layer functions will free the iocb objects.
2684 **/
dea3101e
JB
2685static int
2686lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2687 struct lpfc_iocbq *saveq)
2688{
2689 IOCB_t * irsp;
2690 WORD5 * w5p;
2691 uint32_t Rctl, Type;
76bb24ef 2692 struct lpfc_iocbq *iocbq;
3163f725 2693 struct lpfc_dmabuf *dmzbuf;
dea3101e 2694
dea3101e 2695 irsp = &(saveq->iocb);
57127f15
JS
2696
2697 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2698 if (pring->lpfc_sli_rcv_async_status)
2699 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2700 else
2701 lpfc_printf_log(phba,
2702 KERN_WARNING,
2703 LOG_SLI,
2704 "0316 Ring %d handler: unexpected "
2705 "ASYNC_STATUS iocb received evt_code "
2706 "0x%x\n",
2707 pring->ringno,
2708 irsp->un.asyncstat.evt_code);
2709 return 1;
2710 }
2711
3163f725
JS
2712 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2713 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2714 if (irsp->ulpBdeCount > 0) {
2715 dmzbuf = lpfc_sli_get_buff(phba, pring,
2716 irsp->un.ulpWord[3]);
2717 lpfc_in_buf_free(phba, dmzbuf);
2718 }
2719
2720 if (irsp->ulpBdeCount > 1) {
2721 dmzbuf = lpfc_sli_get_buff(phba, pring,
2722 irsp->unsli3.sli3Words[3]);
2723 lpfc_in_buf_free(phba, dmzbuf);
2724 }
2725
2726 if (irsp->ulpBdeCount > 2) {
2727 dmzbuf = lpfc_sli_get_buff(phba, pring,
2728 irsp->unsli3.sli3Words[7]);
2729 lpfc_in_buf_free(phba, dmzbuf);
2730 }
2731
2732 return 1;
2733 }
2734
92d7f7b0 2735 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2736 if (irsp->ulpBdeCount != 0) {
2737 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2738 irsp->un.ulpWord[3]);
2739 if (!saveq->context2)
2740 lpfc_printf_log(phba,
2741 KERN_ERR,
2742 LOG_SLI,
2743 "0341 Ring %d Cannot find buffer for "
2744 "an unsolicited iocb. tag 0x%x\n",
2745 pring->ringno,
2746 irsp->un.ulpWord[3]);
76bb24ef
JS
2747 }
2748 if (irsp->ulpBdeCount == 2) {
2749 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2750 irsp->unsli3.sli3Words[7]);
2751 if (!saveq->context3)
2752 lpfc_printf_log(phba,
2753 KERN_ERR,
2754 LOG_SLI,
2755 "0342 Ring %d Cannot find buffer for an"
2756 " unsolicited iocb. tag 0x%x\n",
2757 pring->ringno,
2758 irsp->unsli3.sli3Words[7]);
2759 }
2760 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2761 irsp = &(iocbq->iocb);
76bb24ef
JS
2762 if (irsp->ulpBdeCount != 0) {
2763 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2764 irsp->un.ulpWord[3]);
9c2face6 2765 if (!iocbq->context2)
76bb24ef
JS
2766 lpfc_printf_log(phba,
2767 KERN_ERR,
2768 LOG_SLI,
2769 "0343 Ring %d Cannot find "
2770 "buffer for an unsolicited iocb"
2771 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2772 irsp->un.ulpWord[3]);
76bb24ef
JS
2773 }
2774 if (irsp->ulpBdeCount == 2) {
2775 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2776 irsp->unsli3.sli3Words[7]);
9c2face6 2777 if (!iocbq->context3)
76bb24ef
JS
2778 lpfc_printf_log(phba,
2779 KERN_ERR,
2780 LOG_SLI,
2781 "0344 Ring %d Cannot find "
2782 "buffer for an unsolicited "
2783 "iocb. tag 0x%x\n",
2784 pring->ringno,
2785 irsp->unsli3.sli3Words[7]);
2786 }
2787 }
92d7f7b0 2788 }
9c2face6
JS
2789 if (irsp->ulpBdeCount != 0 &&
2790 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2791 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2792 int found = 0;
2793
2794 /* search continue save q for same XRI */
2795 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2796 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2797 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2798 list_add_tail(&saveq->list, &iocbq->list);
2799 found = 1;
2800 break;
2801 }
2802 }
2803 if (!found)
2804 list_add_tail(&saveq->clist,
2805 &pring->iocb_continue_saveq);
2806 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2807 list_del_init(&iocbq->clist);
2808 saveq = iocbq;
2809 irsp = &(saveq->iocb);
2810 } else
2811 return 0;
2812 }
2813 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2814 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2815 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2816 Rctl = FC_RCTL_ELS_REQ;
2817 Type = FC_TYPE_ELS;
9c2face6
JS
2818 } else {
2819 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2820 Rctl = w5p->hcsw.Rctl;
2821 Type = w5p->hcsw.Type;
2822
2823 /* Firmware Workaround */
2824 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2825 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2826 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2827 Rctl = FC_RCTL_ELS_REQ;
2828 Type = FC_TYPE_ELS;
9c2face6
JS
2829 w5p->hcsw.Rctl = Rctl;
2830 w5p->hcsw.Type = Type;
2831 }
2832 }
92d7f7b0 2833
3772a991 2834 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2835 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2836 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2837 "Type x%x received\n",
e8b62011 2838 pring->ringno, Rctl, Type);
3772a991 2839
92d7f7b0 2840 return 1;
dea3101e
JB
2841}
2842
e59058c4 2843/**
3621a710 2844 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2845 * @phba: Pointer to HBA context object.
2846 * @pring: Pointer to driver SLI ring object.
2847 * @prspiocb: Pointer to response iocb object.
2848 *
2849 * This function looks up the iocb_lookup table to get the command iocb
2850 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2851 * response iocb. This function is called with the hbalock held
2852 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2853 * This function returns the command iocb object if it finds the command
2854 * iocb else returns NULL.
2855 **/
dea3101e 2856static struct lpfc_iocbq *
2e0fef85
JS
2857lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2858 struct lpfc_sli_ring *pring,
2859 struct lpfc_iocbq *prspiocb)
dea3101e 2860{
dea3101e
JB
2861 struct lpfc_iocbq *cmd_iocb = NULL;
2862 uint16_t iotag;
1c2ba475 2863 lockdep_assert_held(&phba->hbalock);
dea3101e 2864
604a3e30
JB
2865 iotag = prspiocb->iocb.ulpIoTag;
2866
2867 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2868 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2869 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2870 /* remove from txcmpl queue list */
2871 list_del_init(&cmd_iocb->list);
4f2e66c6 2872 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2873 return cmd_iocb;
2a9bf3d0 2874 }
dea3101e
JB
2875 }
2876
dea3101e 2877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2878 "0317 iotag x%x is out of "
604a3e30 2879 "range: max iotag x%x wd0 x%x\n",
e8b62011 2880 iotag, phba->sli.last_iotag,
604a3e30 2881 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2882 return NULL;
2883}
2884
3772a991
JS
2885/**
2886 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2887 * @phba: Pointer to HBA context object.
2888 * @pring: Pointer to driver SLI ring object.
2889 * @iotag: IOCB tag.
2890 *
2891 * This function looks up the iocb_lookup table to get the command iocb
2892 * corresponding to the given iotag. This function is called with the
2893 * hbalock held.
2894 * This function returns the command iocb object if it finds the command
2895 * iocb else returns NULL.
2896 **/
2897static struct lpfc_iocbq *
2898lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2899 struct lpfc_sli_ring *pring, uint16_t iotag)
2900{
895427bd 2901 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2902
1c2ba475 2903 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2904 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2905 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2906 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2907 /* remove from txcmpl queue list */
2908 list_del_init(&cmd_iocb->list);
2909 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2910 return cmd_iocb;
2a9bf3d0 2911 }
3772a991 2912 }
89533e9b 2913
3772a991 2914 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2915 "0372 iotag x%x lookup error: max iotag (x%x) "
2916 "iocb_flag x%x\n",
2917 iotag, phba->sli.last_iotag,
2918 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
2919 return NULL;
2920}
2921
e59058c4 2922/**
3621a710 2923 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2924 * @phba: Pointer to HBA context object.
2925 * @pring: Pointer to driver SLI ring object.
2926 * @saveq: Pointer to the response iocb to be processed.
2927 *
2928 * This function is called by the ring event handler for non-fcp
2929 * rings when there is a new response iocb in the response ring.
2930 * The caller is not required to hold any locks. This function
2931 * gets the command iocb associated with the response iocb and
2932 * calls the completion handler for the command iocb. If there
2933 * is no completion handler, the function will free the resources
2934 * associated with command iocb. If the response iocb is for
2935 * an already aborted command iocb, the status of the completion
2936 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2937 * This function always returns 1.
2938 **/
dea3101e 2939static int
2e0fef85 2940lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2941 struct lpfc_iocbq *saveq)
2942{
2e0fef85 2943 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2944 int rc = 1;
2945 unsigned long iflag;
2946
2947 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
2948 if (phba->sli_rev == LPFC_SLI_REV4)
2949 spin_lock_irqsave(&pring->ring_lock, iflag);
2950 else
2951 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2952 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
2953 if (phba->sli_rev == LPFC_SLI_REV4)
2954 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2955 else
2956 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 2957
dea3101e
JB
2958 if (cmdiocbp) {
2959 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2960 /*
2961 * If an ELS command failed send an event to mgmt
2962 * application.
2963 */
2964 if (saveq->iocb.ulpStatus &&
2965 (pring->ringno == LPFC_ELS_RING) &&
2966 (cmdiocbp->iocb.ulpCommand ==
2967 CMD_ELS_REQUEST64_CR))
2968 lpfc_send_els_failure_event(phba,
2969 cmdiocbp, saveq);
2970
dea3101e
JB
2971 /*
2972 * Post all ELS completions to the worker thread.
2973 * All other are passed to the completion callback.
2974 */
2975 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2976 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2977 (cmdiocbp->iocb_flag &
2978 LPFC_DRIVER_ABORTED)) {
2979 spin_lock_irqsave(&phba->hbalock,
2980 iflag);
07951076
JS
2981 cmdiocbp->iocb_flag &=
2982 ~LPFC_DRIVER_ABORTED;
341af102
JS
2983 spin_unlock_irqrestore(&phba->hbalock,
2984 iflag);
07951076
JS
2985 saveq->iocb.ulpStatus =
2986 IOSTAT_LOCAL_REJECT;
2987 saveq->iocb.un.ulpWord[4] =
2988 IOERR_SLI_ABORTED;
0ff10d46
JS
2989
2990 /* Firmware could still be in progress
2991 * of DMAing payload, so don't free data
2992 * buffer till after a hbeat.
2993 */
341af102
JS
2994 spin_lock_irqsave(&phba->hbalock,
2995 iflag);
0ff10d46 2996 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2997 spin_unlock_irqrestore(&phba->hbalock,
2998 iflag);
2999 }
0f65ff68
JS
3000 if (phba->sli_rev == LPFC_SLI_REV4) {
3001 if (saveq->iocb_flag &
3002 LPFC_EXCHANGE_BUSY) {
3003 /* Set cmdiocb flag for the
3004 * exchange busy so sgl (xri)
3005 * will not be released until
3006 * the abort xri is received
3007 * from hba.
3008 */
3009 spin_lock_irqsave(
3010 &phba->hbalock, iflag);
3011 cmdiocbp->iocb_flag |=
3012 LPFC_EXCHANGE_BUSY;
3013 spin_unlock_irqrestore(
3014 &phba->hbalock, iflag);
3015 }
3016 if (cmdiocbp->iocb_flag &
3017 LPFC_DRIVER_ABORTED) {
3018 /*
3019 * Clear LPFC_DRIVER_ABORTED
3020 * bit in case it was driver
3021 * initiated abort.
3022 */
3023 spin_lock_irqsave(
3024 &phba->hbalock, iflag);
3025 cmdiocbp->iocb_flag &=
3026 ~LPFC_DRIVER_ABORTED;
3027 spin_unlock_irqrestore(
3028 &phba->hbalock, iflag);
3029 cmdiocbp->iocb.ulpStatus =
3030 IOSTAT_LOCAL_REJECT;
3031 cmdiocbp->iocb.un.ulpWord[4] =
3032 IOERR_ABORT_REQUESTED;
3033 /*
3034 * For SLI4, irsiocb contains
3035 * NO_XRI in sli_xritag, it
3036 * shall not affect releasing
3037 * sgl (xri) process.
3038 */
3039 saveq->iocb.ulpStatus =
3040 IOSTAT_LOCAL_REJECT;
3041 saveq->iocb.un.ulpWord[4] =
3042 IOERR_SLI_ABORTED;
3043 spin_lock_irqsave(
3044 &phba->hbalock, iflag);
3045 saveq->iocb_flag |=
3046 LPFC_DELAY_MEM_FREE;
3047 spin_unlock_irqrestore(
3048 &phba->hbalock, iflag);
3049 }
07951076 3050 }
dea3101e 3051 }
2e0fef85 3052 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
3053 } else
3054 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
3055 } else {
3056 /*
3057 * Unknown initiating command based on the response iotag.
3058 * This could be the case on the ELS ring because of
3059 * lpfc_els_abort().
3060 */
3061 if (pring->ringno != LPFC_ELS_RING) {
3062 /*
3063 * Ring <ringno> handler: unexpected completion IoTag
3064 * <IoTag>
3065 */
a257bf90 3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
3067 "0322 Ring %d handler: "
3068 "unexpected completion IoTag x%x "
3069 "Data: x%x x%x x%x x%x\n",
3070 pring->ringno,
3071 saveq->iocb.ulpIoTag,
3072 saveq->iocb.ulpStatus,
3073 saveq->iocb.un.ulpWord[4],
3074 saveq->iocb.ulpCommand,
3075 saveq->iocb.ulpContext);
dea3101e
JB
3076 }
3077 }
68876920 3078
dea3101e
JB
3079 return rc;
3080}
3081
e59058c4 3082/**
3621a710 3083 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
3084 * @phba: Pointer to HBA context object.
3085 * @pring: Pointer to driver SLI ring object.
3086 *
3087 * This function is called from the iocb ring event handlers when
3088 * put pointer is ahead of the get pointer for a ring. This function signal
3089 * an error attention condition to the worker thread and the worker
3090 * thread will transition the HBA to offline state.
3091 **/
2e0fef85
JS
3092static void
3093lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 3094{
34b02dcd 3095 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 3096 /*
025dfdaf 3097 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
3098 * rsp ring <portRspMax>
3099 */
3100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3101 "0312 Ring %d handler: portRspPut %d "
025dfdaf 3102 "is bigger than rsp ring %d\n",
e8b62011 3103 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 3104 pring->sli.sli3.numRiocb);
875fbdfe 3105
2e0fef85 3106 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
3107
3108 /*
3109 * All error attention handlers are posted to
3110 * worker thread
3111 */
3112 phba->work_ha |= HA_ERATT;
3113 phba->work_hs = HS_FFER3;
92d7f7b0 3114
5e9d9b82 3115 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3116
3117 return;
3118}
3119
9399627f 3120/**
3621a710 3121 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3122 * @ptr: Pointer to address of HBA context object.
3123 *
3124 * This function is invoked by the Error Attention polling timer when the
3125 * timer times out. It will check the SLI Error Attention register for
3126 * possible attention events. If so, it will post an Error Attention event
3127 * and wake up worker thread to process it. Otherwise, it will set up the
3128 * Error Attention polling timer for the next poll.
3129 **/
f22eb4d3 3130void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3131{
3132 struct lpfc_hba *phba;
eb016566 3133 uint32_t eratt = 0;
aa6fbb75 3134 uint64_t sli_intr, cnt;
9399627f 3135
f22eb4d3 3136 phba = from_timer(phba, t, eratt_poll);
9399627f 3137
aa6fbb75
JS
3138 /* Here we will also keep track of interrupts per sec of the hba */
3139 sli_intr = phba->sli.slistat.sli_intr;
3140
3141 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3142 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3143 sli_intr);
3144 else
3145 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3146
65791f1f
JS
3147 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3148 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3149 phba->sli.slistat.sli_ips = cnt;
3150
3151 phba->sli.slistat.sli_prev_intr = sli_intr;
3152
9399627f
JS
3153 /* Check chip HA register for error event */
3154 eratt = lpfc_sli_check_eratt(phba);
3155
3156 if (eratt)
3157 /* Tell the worker thread there is work to do */
3158 lpfc_worker_wake_up(phba);
3159 else
3160 /* Restart the timer for next eratt poll */
256ec0d0
JS
3161 mod_timer(&phba->eratt_poll,
3162 jiffies +
65791f1f 3163 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3164 return;
3165}
3166
875fbdfe 3167
e59058c4 3168/**
3621a710 3169 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3170 * @phba: Pointer to HBA context object.
3171 * @pring: Pointer to driver SLI ring object.
3172 * @mask: Host attention register mask for this ring.
3173 *
3174 * This function is called from the interrupt context when there is a ring
3175 * event for the fcp ring. The caller does not hold any lock.
3176 * The function processes each response iocb in the response ring until it
25985edc 3177 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3178 * LE bit set. The function will call the completion handler of the command iocb
3179 * if the response iocb indicates a completion for a command iocb or it is
3180 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3181 * function if this is an unsolicited iocb.
dea3101e 3182 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3183 * to check it explicitly.
3184 */
3185int
2e0fef85
JS
3186lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3187 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3188{
34b02dcd 3189 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3190 IOCB_t *irsp = NULL;
87f6eaff 3191 IOCB_t *entry = NULL;
dea3101e
JB
3192 struct lpfc_iocbq *cmdiocbq = NULL;
3193 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3194 uint32_t status;
3195 uint32_t portRspPut, portRspMax;
3196 int rc = 1;
3197 lpfc_iocb_type type;
3198 unsigned long iflag;
3199 uint32_t rsp_cmpl = 0;
dea3101e 3200
2e0fef85 3201 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3202 pring->stats.iocb_event++;
3203
dea3101e
JB
3204 /*
3205 * The next available response entry should never exceed the maximum
3206 * entries. If it does, treat it as an adapter hardware error.
3207 */
7e56aa25 3208 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3209 portRspPut = le32_to_cpu(pgp->rspPutInx);
3210 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3211 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3212 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3213 return 1;
3214 }
45ed1190
JS
3215 if (phba->fcp_ring_in_use) {
3216 spin_unlock_irqrestore(&phba->hbalock, iflag);
3217 return 1;
3218 } else
3219 phba->fcp_ring_in_use = 1;
dea3101e
JB
3220
3221 rmb();
7e56aa25 3222 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3223 /*
3224 * Fetch an entry off the ring and copy it into a local data
3225 * structure. The copy involves a byte-swap since the
3226 * network byte order and pci byte orders are different.
3227 */
ed957684 3228 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3229 phba->last_completion_time = jiffies;
875fbdfe 3230
7e56aa25
JS
3231 if (++pring->sli.sli3.rspidx >= portRspMax)
3232 pring->sli.sli3.rspidx = 0;
875fbdfe 3233
87f6eaff
JSEC
3234 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3235 (uint32_t *) &rspiocbq.iocb,
ed957684 3236 phba->iocb_rsp_size);
a4bc3379 3237 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3238 irsp = &rspiocbq.iocb;
3239
dea3101e
JB
3240 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3241 pring->stats.iocb_rsp++;
3242 rsp_cmpl++;
3243
3244 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3245 /*
3246 * If resource errors reported from HBA, reduce
3247 * queuedepths of the SCSI device.
3248 */
3249 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3250 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3251 IOERR_NO_RESOURCES)) {
92d7f7b0 3252 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3253 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3254 spin_lock_irqsave(&phba->hbalock, iflag);
3255 }
3256
dea3101e
JB
3257 /* Rsp ring <ringno> error: IOCB */
3258 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3259 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3260 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3261 pring->ringno,
92d7f7b0
JS
3262 irsp->un.ulpWord[0],
3263 irsp->un.ulpWord[1],
3264 irsp->un.ulpWord[2],
3265 irsp->un.ulpWord[3],
3266 irsp->un.ulpWord[4],
3267 irsp->un.ulpWord[5],
d7c255b2
JS
3268 *(uint32_t *)&irsp->un1,
3269 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3270 }
3271
3272 switch (type) {
3273 case LPFC_ABORT_IOCB:
3274 case LPFC_SOL_IOCB:
3275 /*
3276 * Idle exchange closed via ABTS from port. No iocb
3277 * resources need to be recovered.
3278 */
3279 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3280 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3281 "0333 IOCB cmd 0x%x"
dca9479b 3282 " processed. Skipping"
92d7f7b0 3283 " completion\n",
dca9479b 3284 irsp->ulpCommand);
dea3101e
JB
3285 break;
3286 }
3287
604a3e30
JB
3288 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3289 &rspiocbq);
0f65ff68
JS
3290 if (unlikely(!cmdiocbq))
3291 break;
3292 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3293 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3294 if (cmdiocbq->iocb_cmpl) {
3295 spin_unlock_irqrestore(&phba->hbalock, iflag);
3296 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3297 &rspiocbq);
3298 spin_lock_irqsave(&phba->hbalock, iflag);
3299 }
dea3101e 3300 break;
a4bc3379 3301 case LPFC_UNSOL_IOCB:
2e0fef85 3302 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3303 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3304 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3305 break;
dea3101e
JB
3306 default:
3307 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3308 char adaptermsg[LPFC_MAX_ADPTMSG];
3309 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3310 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3311 MAX_MSG_DATA);
898eb71c
JP
3312 dev_warn(&((phba->pcidev)->dev),
3313 "lpfc%d: %s\n",
dea3101e
JB
3314 phba->brd_no, adaptermsg);
3315 } else {
3316 /* Unknown IOCB command */
3317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3318 "0334 Unknown IOCB command "
92d7f7b0 3319 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3320 type, irsp->ulpCommand,
92d7f7b0
JS
3321 irsp->ulpStatus,
3322 irsp->ulpIoTag,
3323 irsp->ulpContext);
dea3101e
JB
3324 }
3325 break;
3326 }
3327
3328 /*
3329 * The response IOCB has been processed. Update the ring
3330 * pointer in SLIM. If the port response put pointer has not
3331 * been updated, sync the pgp->rspPutInx and fetch the new port
3332 * response put pointer.
3333 */
7e56aa25
JS
3334 writel(pring->sli.sli3.rspidx,
3335 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3336
7e56aa25 3337 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3338 portRspPut = le32_to_cpu(pgp->rspPutInx);
3339 }
3340
3341 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3342 pring->stats.iocb_rsp_full++;
3343 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3344 writel(status, phba->CAregaddr);
3345 readl(phba->CAregaddr);
3346 }
3347 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3348 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3349 pring->stats.iocb_cmd_empty++;
3350
3351 /* Force update of the local copy of cmdGetInx */
7e56aa25 3352 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3353 lpfc_sli_resume_iocb(phba, pring);
3354
3355 if ((pring->lpfc_sli_cmd_available))
3356 (pring->lpfc_sli_cmd_available) (phba, pring);
3357
3358 }
3359
45ed1190 3360 phba->fcp_ring_in_use = 0;
2e0fef85 3361 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3362 return rc;
3363}
3364
e59058c4 3365/**
3772a991
JS
3366 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3367 * @phba: Pointer to HBA context object.
3368 * @pring: Pointer to driver SLI ring object.
3369 * @rspiocbp: Pointer to driver response IOCB object.
3370 *
3371 * This function is called from the worker thread when there is a slow-path
3372 * response IOCB to process. This function chains all the response iocbs until
3373 * seeing the iocb with the LE bit set. The function will call
3374 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3375 * completion of a command iocb. The function will call the
3376 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3377 * The function frees the resources or calls the completion handler if this
3378 * iocb is an abort completion. The function returns NULL when the response
3379 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3380 * this function shall chain the iocb on to the iocb_continueq and return the
3381 * response iocb passed in.
3382 **/
3383static struct lpfc_iocbq *
3384lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3385 struct lpfc_iocbq *rspiocbp)
3386{
3387 struct lpfc_iocbq *saveq;
3388 struct lpfc_iocbq *cmdiocbp;
3389 struct lpfc_iocbq *next_iocb;
3390 IOCB_t *irsp = NULL;
3391 uint32_t free_saveq;
3392 uint8_t iocb_cmd_type;
3393 lpfc_iocb_type type;
3394 unsigned long iflag;
3395 int rc;
3396
3397 spin_lock_irqsave(&phba->hbalock, iflag);
3398 /* First add the response iocb to the countinueq list */
3399 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3400 pring->iocb_continueq_cnt++;
3401
70f23fd6 3402 /* Now, determine whether the list is completed for processing */
3772a991
JS
3403 irsp = &rspiocbp->iocb;
3404 if (irsp->ulpLe) {
3405 /*
3406 * By default, the driver expects to free all resources
3407 * associated with this iocb completion.
3408 */
3409 free_saveq = 1;
3410 saveq = list_get_first(&pring->iocb_continueq,
3411 struct lpfc_iocbq, list);
3412 irsp = &(saveq->iocb);
3413 list_del_init(&pring->iocb_continueq);
3414 pring->iocb_continueq_cnt = 0;
3415
3416 pring->stats.iocb_rsp++;
3417
3418 /*
3419 * If resource errors reported from HBA, reduce
3420 * queuedepths of the SCSI device.
3421 */
3422 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3423 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3424 IOERR_NO_RESOURCES)) {
3772a991
JS
3425 spin_unlock_irqrestore(&phba->hbalock, iflag);
3426 phba->lpfc_rampdown_queue_depth(phba);
3427 spin_lock_irqsave(&phba->hbalock, iflag);
3428 }
3429
3430 if (irsp->ulpStatus) {
3431 /* Rsp ring <ringno> error: IOCB */
3432 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3433 "0328 Rsp Ring %d error: "
3434 "IOCB Data: "
3435 "x%x x%x x%x x%x "
3436 "x%x x%x x%x x%x "
3437 "x%x x%x x%x x%x "
3438 "x%x x%x x%x x%x\n",
3439 pring->ringno,
3440 irsp->un.ulpWord[0],
3441 irsp->un.ulpWord[1],
3442 irsp->un.ulpWord[2],
3443 irsp->un.ulpWord[3],
3444 irsp->un.ulpWord[4],
3445 irsp->un.ulpWord[5],
3446 *(((uint32_t *) irsp) + 6),
3447 *(((uint32_t *) irsp) + 7),
3448 *(((uint32_t *) irsp) + 8),
3449 *(((uint32_t *) irsp) + 9),
3450 *(((uint32_t *) irsp) + 10),
3451 *(((uint32_t *) irsp) + 11),
3452 *(((uint32_t *) irsp) + 12),
3453 *(((uint32_t *) irsp) + 13),
3454 *(((uint32_t *) irsp) + 14),
3455 *(((uint32_t *) irsp) + 15));
3456 }
3457
3458 /*
3459 * Fetch the IOCB command type and call the correct completion
3460 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3461 * get freed back to the lpfc_iocb_list by the discovery
3462 * kernel thread.
3463 */
3464 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3465 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3466 switch (type) {
3467 case LPFC_SOL_IOCB:
3468 spin_unlock_irqrestore(&phba->hbalock, iflag);
3469 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3470 spin_lock_irqsave(&phba->hbalock, iflag);
3471 break;
3472
3473 case LPFC_UNSOL_IOCB:
3474 spin_unlock_irqrestore(&phba->hbalock, iflag);
3475 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3476 spin_lock_irqsave(&phba->hbalock, iflag);
3477 if (!rc)
3478 free_saveq = 0;
3479 break;
3480
3481 case LPFC_ABORT_IOCB:
3482 cmdiocbp = NULL;
3483 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3484 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3485 saveq);
3486 if (cmdiocbp) {
3487 /* Call the specified completion routine */
3488 if (cmdiocbp->iocb_cmpl) {
3489 spin_unlock_irqrestore(&phba->hbalock,
3490 iflag);
3491 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3492 saveq);
3493 spin_lock_irqsave(&phba->hbalock,
3494 iflag);
3495 } else
3496 __lpfc_sli_release_iocbq(phba,
3497 cmdiocbp);
3498 }
3499 break;
3500
3501 case LPFC_UNKNOWN_IOCB:
3502 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3503 char adaptermsg[LPFC_MAX_ADPTMSG];
3504 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3505 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3506 MAX_MSG_DATA);
3507 dev_warn(&((phba->pcidev)->dev),
3508 "lpfc%d: %s\n",
3509 phba->brd_no, adaptermsg);
3510 } else {
3511 /* Unknown IOCB command */
3512 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3513 "0335 Unknown IOCB "
3514 "command Data: x%x "
3515 "x%x x%x x%x\n",
3516 irsp->ulpCommand,
3517 irsp->ulpStatus,
3518 irsp->ulpIoTag,
3519 irsp->ulpContext);
3520 }
3521 break;
3522 }
3523
3524 if (free_saveq) {
3525 list_for_each_entry_safe(rspiocbp, next_iocb,
3526 &saveq->list, list) {
61f35bff 3527 list_del_init(&rspiocbp->list);
3772a991
JS
3528 __lpfc_sli_release_iocbq(phba, rspiocbp);
3529 }
3530 __lpfc_sli_release_iocbq(phba, saveq);
3531 }
3532 rspiocbp = NULL;
3533 }
3534 spin_unlock_irqrestore(&phba->hbalock, iflag);
3535 return rspiocbp;
3536}
3537
3538/**
3539 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3540 * @phba: Pointer to HBA context object.
3541 * @pring: Pointer to driver SLI ring object.
3542 * @mask: Host attention register mask for this ring.
3543 *
3772a991
JS
3544 * This routine wraps the actual slow_ring event process routine from the
3545 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3546 **/
3772a991 3547void
2e0fef85
JS
3548lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3549 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3550{
3551 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3552}
3553
3554/**
3555 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3556 * @phba: Pointer to HBA context object.
3557 * @pring: Pointer to driver SLI ring object.
3558 * @mask: Host attention register mask for this ring.
3559 *
3560 * This function is called from the worker thread when there is a ring event
3561 * for non-fcp rings. The caller does not hold any lock. The function will
3562 * remove each response iocb in the response ring and calls the handle
3563 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3564 **/
3565static void
3566lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3567 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3568{
34b02dcd 3569 struct lpfc_pgp *pgp;
dea3101e
JB
3570 IOCB_t *entry;
3571 IOCB_t *irsp = NULL;
3572 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3573 uint32_t portRspPut, portRspMax;
dea3101e 3574 unsigned long iflag;
3772a991 3575 uint32_t status;
dea3101e 3576
34b02dcd 3577 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3578 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3579 pring->stats.iocb_event++;
3580
dea3101e
JB
3581 /*
3582 * The next available response entry should never exceed the maximum
3583 * entries. If it does, treat it as an adapter hardware error.
3584 */
7e56aa25 3585 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3586 portRspPut = le32_to_cpu(pgp->rspPutInx);
3587 if (portRspPut >= portRspMax) {
3588 /*
025dfdaf 3589 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3590 * rsp ring <portRspMax>
3591 */
ed957684 3592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3593 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3594 "is bigger than rsp ring %d\n",
e8b62011 3595 pring->ringno, portRspPut, portRspMax);
dea3101e 3596
2e0fef85
JS
3597 phba->link_state = LPFC_HBA_ERROR;
3598 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3599
3600 phba->work_hs = HS_FFER3;
3601 lpfc_handle_eratt(phba);
3602
3772a991 3603 return;
dea3101e
JB
3604 }
3605
3606 rmb();
7e56aa25 3607 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3608 /*
3609 * Build a completion list and call the appropriate handler.
3610 * The process is to get the next available response iocb, get
3611 * a free iocb from the list, copy the response data into the
3612 * free iocb, insert to the continuation list, and update the
3613 * next response index to slim. This process makes response
3614 * iocb's in the ring available to DMA as fast as possible but
3615 * pays a penalty for a copy operation. Since the iocb is
3616 * only 32 bytes, this penalty is considered small relative to
3617 * the PCI reads for register values and a slim write. When
3618 * the ulpLe field is set, the entire Command has been
3619 * received.
3620 */
ed957684
JS
3621 entry = lpfc_resp_iocb(phba, pring);
3622
858c9f6c 3623 phba->last_completion_time = jiffies;
2e0fef85 3624 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3625 if (rspiocbp == NULL) {
3626 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3627 "completion.\n", __func__);
dea3101e
JB
3628 break;
3629 }
3630
ed957684
JS
3631 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3632 phba->iocb_rsp_size);
dea3101e
JB
3633 irsp = &rspiocbp->iocb;
3634
7e56aa25
JS
3635 if (++pring->sli.sli3.rspidx >= portRspMax)
3636 pring->sli.sli3.rspidx = 0;
dea3101e 3637
a58cbd52
JS
3638 if (pring->ringno == LPFC_ELS_RING) {
3639 lpfc_debugfs_slow_ring_trc(phba,
3640 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3641 *(((uint32_t *) irsp) + 4),
3642 *(((uint32_t *) irsp) + 6),
3643 *(((uint32_t *) irsp) + 7));
3644 }
3645
7e56aa25
JS
3646 writel(pring->sli.sli3.rspidx,
3647 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3648
3772a991
JS
3649 spin_unlock_irqrestore(&phba->hbalock, iflag);
3650 /* Handle the response IOCB */
3651 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3652 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3653
3654 /*
3655 * If the port response put pointer has not been updated, sync
3656 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3657 * response put pointer.
3658 */
7e56aa25 3659 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3660 portRspPut = le32_to_cpu(pgp->rspPutInx);
3661 }
7e56aa25 3662 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3663
92d7f7b0 3664 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3665 /* At least one response entry has been freed */
3666 pring->stats.iocb_rsp_full++;
3667 /* SET RxRE_RSP in Chip Att register */
3668 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3669 writel(status, phba->CAregaddr);
3670 readl(phba->CAregaddr); /* flush */
3671 }
3672 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3673 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3674 pring->stats.iocb_cmd_empty++;
3675
3676 /* Force update of the local copy of cmdGetInx */
7e56aa25 3677 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3678 lpfc_sli_resume_iocb(phba, pring);
3679
3680 if ((pring->lpfc_sli_cmd_available))
3681 (pring->lpfc_sli_cmd_available) (phba, pring);
3682
3683 }
3684
2e0fef85 3685 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3686 return;
dea3101e
JB
3687}
3688
4f774513
JS
3689/**
3690 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3691 * @phba: Pointer to HBA context object.
3692 * @pring: Pointer to driver SLI ring object.
3693 * @mask: Host attention register mask for this ring.
3694 *
3695 * This function is called from the worker thread when there is a pending
3696 * ELS response iocb on the driver internal slow-path response iocb worker
3697 * queue. The caller does not hold any lock. The function will remove each
3698 * response iocb from the response worker queue and calls the handle
3699 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3700 **/
3701static void
3702lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3703 struct lpfc_sli_ring *pring, uint32_t mask)
3704{
3705 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3706 struct hbq_dmabuf *dmabuf;
3707 struct lpfc_cq_event *cq_event;
4f774513
JS
3708 unsigned long iflag;
3709
45ed1190
JS
3710 spin_lock_irqsave(&phba->hbalock, iflag);
3711 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3712 spin_unlock_irqrestore(&phba->hbalock, iflag);
3713 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3714 /* Get the response iocb from the head of work queue */
3715 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3716 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3717 cq_event, struct lpfc_cq_event, list);
4f774513 3718 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3719
3720 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3721 case CQE_CODE_COMPL_WQE:
3722 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3723 cq_event);
45ed1190
JS
3724 /* Translate ELS WCQE to response IOCBQ */
3725 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3726 irspiocbq);
3727 if (irspiocbq)
3728 lpfc_sli_sp_handle_rspiocb(phba, pring,
3729 irspiocbq);
4d9ab994
JS
3730 break;
3731 case CQE_CODE_RECEIVE:
7851fe2c 3732 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3733 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3734 cq_event);
3735 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3736 break;
3737 default:
3738 break;
3739 }
4f774513
JS
3740 }
3741}
3742
e59058c4 3743/**
3621a710 3744 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3745 * @phba: Pointer to HBA context object.
3746 * @pring: Pointer to driver SLI ring object.
3747 *
3748 * This function aborts all iocbs in the given ring and frees all the iocb
3749 * objects in txq. This function issues an abort iocb for all the iocb commands
3750 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3751 * the return of this function. The caller is not required to hold any locks.
3752 **/
2e0fef85 3753void
dea3101e
JB
3754lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3755{
2534ba75 3756 LIST_HEAD(completions);
dea3101e 3757 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3758
92d7f7b0
JS
3759 if (pring->ringno == LPFC_ELS_RING) {
3760 lpfc_fabric_abort_hba(phba);
3761 }
3762
dea3101e
JB
3763 /* Error everything on txq and txcmplq
3764 * First do the txq.
3765 */
db55fba8
JS
3766 if (phba->sli_rev >= LPFC_SLI_REV4) {
3767 spin_lock_irq(&pring->ring_lock);
3768 list_splice_init(&pring->txq, &completions);
3769 pring->txq_cnt = 0;
3770 spin_unlock_irq(&pring->ring_lock);
dea3101e 3771
db55fba8
JS
3772 spin_lock_irq(&phba->hbalock);
3773 /* Next issue ABTS for everything on the txcmplq */
3774 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3775 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3776 spin_unlock_irq(&phba->hbalock);
3777 } else {
3778 spin_lock_irq(&phba->hbalock);
3779 list_splice_init(&pring->txq, &completions);
3780 pring->txq_cnt = 0;
dea3101e 3781
db55fba8
JS
3782 /* Next issue ABTS for everything on the txcmplq */
3783 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3784 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3785 spin_unlock_irq(&phba->hbalock);
3786 }
dea3101e 3787
a257bf90
JS
3788 /* Cancel all the IOCBs from the completions list */
3789 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3790 IOERR_SLI_ABORTED);
dea3101e
JB
3791}
3792
895427bd
JS
3793/**
3794 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3795 * @phba: Pointer to HBA context object.
3796 * @pring: Pointer to driver SLI ring object.
3797 *
3798 * This function aborts all iocbs in the given ring and frees all the iocb
3799 * objects in txq. This function issues an abort iocb for all the iocb commands
3800 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3801 * the return of this function. The caller is not required to hold any locks.
3802 **/
3803void
3804lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3805{
3806 LIST_HEAD(completions);
3807 struct lpfc_iocbq *iocb, *next_iocb;
3808
3809 if (pring->ringno == LPFC_ELS_RING)
3810 lpfc_fabric_abort_hba(phba);
3811
3812 spin_lock_irq(&phba->hbalock);
3813 /* Next issue ABTS for everything on the txcmplq */
3814 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3815 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3816 spin_unlock_irq(&phba->hbalock);
3817}
3818
3819
db55fba8
JS
3820/**
3821 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3822 * @phba: Pointer to HBA context object.
3823 * @pring: Pointer to driver SLI ring object.
3824 *
3825 * This function aborts all iocbs in FCP rings and frees all the iocb
3826 * objects in txq. This function issues an abort iocb for all the iocb commands
3827 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3828 * the return of this function. The caller is not required to hold any locks.
3829 **/
3830void
3831lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3832{
3833 struct lpfc_sli *psli = &phba->sli;
3834 struct lpfc_sli_ring *pring;
3835 uint32_t i;
3836
3837 /* Look on all the FCP Rings for the iotag */
3838 if (phba->sli_rev >= LPFC_SLI_REV4) {
3839 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3840 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3841 lpfc_sli_abort_iocb_ring(phba, pring);
3842 }
3843 } else {
895427bd 3844 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3845 lpfc_sli_abort_iocb_ring(phba, pring);
3846 }
3847}
3848
895427bd
JS
3849/**
3850 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3851 * @phba: Pointer to HBA context object.
3852 *
3853 * This function aborts all wqes in NVME rings. This function issues an
3854 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3855 * the txcmplq is not guaranteed to complete before the return of this
3856 * function. The caller is not required to hold any locks.
3857 **/
3858void
3859lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3860{
3861 struct lpfc_sli_ring *pring;
3862 uint32_t i;
3863
3864 if (phba->sli_rev < LPFC_SLI_REV4)
3865 return;
3866
3867 /* Abort all IO on each NVME ring. */
3868 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3869 pring = phba->sli4_hba.nvme_wq[i]->pring;
3870 lpfc_sli_abort_wqe_ring(phba, pring);
3871 }
3872}
3873
db55fba8 3874
a8e497d5 3875/**
3621a710 3876 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3877 * @phba: Pointer to HBA context object.
3878 *
3879 * This function flushes all iocbs in the fcp ring and frees all the iocb
3880 * objects in txq and txcmplq. This function will not issue abort iocbs
3881 * for all the iocb commands in txcmplq, they will just be returned with
3882 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3883 * slot has been permanently disabled.
3884 **/
3885void
3886lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3887{
3888 LIST_HEAD(txq);
3889 LIST_HEAD(txcmplq);
a8e497d5
JS
3890 struct lpfc_sli *psli = &phba->sli;
3891 struct lpfc_sli_ring *pring;
db55fba8 3892 uint32_t i;
c1dd9111 3893 struct lpfc_iocbq *piocb, *next_iocb;
a8e497d5
JS
3894
3895 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3896 /* Indicate the I/O queues are flushed */
3897 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3898 spin_unlock_irq(&phba->hbalock);
3899
db55fba8
JS
3900 /* Look on all the FCP Rings for the iotag */
3901 if (phba->sli_rev >= LPFC_SLI_REV4) {
3902 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3903 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3904
3905 spin_lock_irq(&pring->ring_lock);
3906 /* Retrieve everything on txq */
3907 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
3908 list_for_each_entry_safe(piocb, next_iocb,
3909 &pring->txcmplq, list)
3910 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
3911 /* Retrieve everything on the txcmplq */
3912 list_splice_init(&pring->txcmplq, &txcmplq);
3913 pring->txq_cnt = 0;
3914 pring->txcmplq_cnt = 0;
3915 spin_unlock_irq(&pring->ring_lock);
3916
3917 /* Flush the txq */
3918 lpfc_sli_cancel_iocbs(phba, &txq,
3919 IOSTAT_LOCAL_REJECT,
3920 IOERR_SLI_DOWN);
3921 /* Flush the txcmpq */
3922 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3923 IOSTAT_LOCAL_REJECT,
3924 IOERR_SLI_DOWN);
3925 }
3926 } else {
895427bd 3927 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 3928
db55fba8
JS
3929 spin_lock_irq(&phba->hbalock);
3930 /* Retrieve everything on txq */
3931 list_splice_init(&pring->txq, &txq);
c1dd9111
JS
3932 list_for_each_entry_safe(piocb, next_iocb,
3933 &pring->txcmplq, list)
3934 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
db55fba8
JS
3935 /* Retrieve everything on the txcmplq */
3936 list_splice_init(&pring->txcmplq, &txcmplq);
3937 pring->txq_cnt = 0;
3938 pring->txcmplq_cnt = 0;
3939 spin_unlock_irq(&phba->hbalock);
3940
3941 /* Flush the txq */
3942 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3943 IOERR_SLI_DOWN);
3944 /* Flush the txcmpq */
3945 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3946 IOERR_SLI_DOWN);
3947 }
a8e497d5
JS
3948}
3949
895427bd
JS
3950/**
3951 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3952 * @phba: Pointer to HBA context object.
3953 *
3954 * This function flushes all wqes in the nvme rings and frees all resources
3955 * in the txcmplq. This function does not issue abort wqes for the IO
3956 * commands in txcmplq, they will just be returned with
3957 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3958 * slot has been permanently disabled.
3959 **/
3960void
3961lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3962{
3963 LIST_HEAD(txcmplq);
3964 struct lpfc_sli_ring *pring;
3965 uint32_t i;
c1dd9111 3966 struct lpfc_iocbq *piocb, *next_iocb;
895427bd
JS
3967
3968 if (phba->sli_rev < LPFC_SLI_REV4)
3969 return;
3970
3971 /* Hint to other driver operations that a flush is in progress. */
3972 spin_lock_irq(&phba->hbalock);
3973 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3974 spin_unlock_irq(&phba->hbalock);
3975
3976 /* Cycle through all NVME rings and complete each IO with
3977 * a local driver reason code. This is a flush so no
3978 * abort exchange to FW.
3979 */
3980 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3981 pring = phba->sli4_hba.nvme_wq[i]->pring;
3982
895427bd 3983 spin_lock_irq(&pring->ring_lock);
c1dd9111
JS
3984 list_for_each_entry_safe(piocb, next_iocb,
3985 &pring->txcmplq, list)
3986 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3987 /* Retrieve everything on the txcmplq */
895427bd
JS
3988 list_splice_init(&pring->txcmplq, &txcmplq);
3989 pring->txcmplq_cnt = 0;
3990 spin_unlock_irq(&pring->ring_lock);
3991
3992 /* Flush the txcmpq &&&PAE */
3993 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3994 IOSTAT_LOCAL_REJECT,
3995 IOERR_SLI_DOWN);
3996 }
3997}
3998
e59058c4 3999/**
3772a991 4000 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
4001 * @phba: Pointer to HBA context object.
4002 * @mask: Bit mask to be checked.
4003 *
4004 * This function reads the host status register and compares
4005 * with the provided bit mask to check if HBA completed
4006 * the restart. This function will wait in a loop for the
4007 * HBA to complete restart. If the HBA does not restart within
4008 * 15 iterations, the function will reset the HBA again. The
4009 * function returns 1 when HBA fail to restart otherwise returns
4010 * zero.
4011 **/
3772a991
JS
4012static int
4013lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 4014{
41415862
JW
4015 uint32_t status;
4016 int i = 0;
4017 int retval = 0;
dea3101e 4018
41415862 4019 /* Read the HBA Host Status Register */
9940b97b
JS
4020 if (lpfc_readl(phba->HSregaddr, &status))
4021 return 1;
dea3101e 4022
41415862
JW
4023 /*
4024 * Check status register every 100ms for 5 retries, then every
4025 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4026 * every 2.5 sec for 4.
4027 * Break our of the loop if errors occurred during init.
4028 */
4029 while (((status & mask) != mask) &&
4030 !(status & HS_FFERM) &&
4031 i++ < 20) {
dea3101e 4032
41415862
JW
4033 if (i <= 5)
4034 msleep(10);
4035 else if (i <= 10)
4036 msleep(500);
4037 else
4038 msleep(2500);
dea3101e 4039
41415862 4040 if (i == 15) {
2e0fef85 4041 /* Do post */
92d7f7b0 4042 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
4043 lpfc_sli_brdrestart(phba);
4044 }
4045 /* Read the HBA Host Status Register */
9940b97b
JS
4046 if (lpfc_readl(phba->HSregaddr, &status)) {
4047 retval = 1;
4048 break;
4049 }
41415862 4050 }
dea3101e 4051
41415862
JW
4052 /* Check to see if any errors occurred during init */
4053 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
4054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4055 "2751 Adapter failed to restart, "
4056 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4057 status,
4058 readl(phba->MBslimaddr + 0xa8),
4059 readl(phba->MBslimaddr + 0xac));
2e0fef85 4060 phba->link_state = LPFC_HBA_ERROR;
41415862 4061 retval = 1;
dea3101e 4062 }
dea3101e 4063
41415862
JW
4064 return retval;
4065}
dea3101e 4066
da0436e9
JS
4067/**
4068 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4069 * @phba: Pointer to HBA context object.
4070 * @mask: Bit mask to be checked.
4071 *
4072 * This function checks the host status register to check if HBA is
4073 * ready. This function will wait in a loop for the HBA to be ready
4074 * If the HBA is not ready , the function will will reset the HBA PCI
4075 * function again. The function returns 1 when HBA fail to be ready
4076 * otherwise returns zero.
4077 **/
4078static int
4079lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4080{
4081 uint32_t status;
4082 int retval = 0;
4083
4084 /* Read the HBA Host Status Register */
4085 status = lpfc_sli4_post_status_check(phba);
4086
4087 if (status) {
4088 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4089 lpfc_sli_brdrestart(phba);
4090 status = lpfc_sli4_post_status_check(phba);
4091 }
4092
4093 /* Check to see if any errors occurred during init */
4094 if (status) {
4095 phba->link_state = LPFC_HBA_ERROR;
4096 retval = 1;
4097 } else
4098 phba->sli4_hba.intr_enable = 0;
4099
4100 return retval;
4101}
4102
4103/**
4104 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4105 * @phba: Pointer to HBA context object.
4106 * @mask: Bit mask to be checked.
4107 *
4108 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4109 * from the API jump table function pointer from the lpfc_hba struct.
4110 **/
4111int
4112lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4113{
4114 return phba->lpfc_sli_brdready(phba, mask);
4115}
4116
9290831f
JS
4117#define BARRIER_TEST_PATTERN (0xdeadbeef)
4118
e59058c4 4119/**
3621a710 4120 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
4121 * @phba: Pointer to HBA context object.
4122 *
1b51197d
JS
4123 * This function is called before resetting an HBA. This function is called
4124 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 4125 **/
2e0fef85 4126void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4127{
65a29c16
JS
4128 uint32_t __iomem *resp_buf;
4129 uint32_t __iomem *mbox_buf;
9290831f 4130 volatile uint32_t mbox;
9940b97b 4131 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4132 int i;
4133 uint8_t hdrtype;
4134
1c2ba475
JT
4135 lockdep_assert_held(&phba->hbalock);
4136
9290831f
JS
4137 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4138 if (hdrtype != 0x80 ||
4139 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4140 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4141 return;
4142
4143 /*
4144 * Tell the other part of the chip to suspend temporarily all
4145 * its DMA activity.
4146 */
65a29c16 4147 resp_buf = phba->MBslimaddr;
9290831f
JS
4148
4149 /* Disable the error attention */
9940b97b
JS
4150 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4151 return;
9290831f
JS
4152 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4153 readl(phba->HCregaddr); /* flush */
2e0fef85 4154 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4155
9940b97b
JS
4156 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4157 return;
4158 if (ha_copy & HA_ERATT) {
9290831f
JS
4159 /* Clear Chip error bit */
4160 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4161 phba->pport->stopped = 1;
9290831f
JS
4162 }
4163
4164 mbox = 0;
4165 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4166 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4167
4168 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4169 mbox_buf = phba->MBslimaddr;
9290831f
JS
4170 writel(mbox, mbox_buf);
4171
9940b97b
JS
4172 for (i = 0; i < 50; i++) {
4173 if (lpfc_readl((resp_buf + 1), &resp_data))
4174 return;
4175 if (resp_data != ~(BARRIER_TEST_PATTERN))
4176 mdelay(1);
4177 else
4178 break;
4179 }
4180 resp_data = 0;
4181 if (lpfc_readl((resp_buf + 1), &resp_data))
4182 return;
4183 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4184 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4185 phba->pport->stopped)
9290831f
JS
4186 goto restore_hc;
4187 else
4188 goto clear_errat;
4189 }
4190
4191 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4192 resp_data = 0;
4193 for (i = 0; i < 500; i++) {
4194 if (lpfc_readl(resp_buf, &resp_data))
4195 return;
4196 if (resp_data != mbox)
4197 mdelay(1);
4198 else
4199 break;
4200 }
9290831f
JS
4201
4202clear_errat:
4203
9940b97b
JS
4204 while (++i < 500) {
4205 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4206 return;
4207 if (!(ha_copy & HA_ERATT))
4208 mdelay(1);
4209 else
4210 break;
4211 }
9290831f
JS
4212
4213 if (readl(phba->HAregaddr) & HA_ERATT) {
4214 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4215 phba->pport->stopped = 1;
9290831f
JS
4216 }
4217
4218restore_hc:
2e0fef85 4219 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4220 writel(hc_copy, phba->HCregaddr);
4221 readl(phba->HCregaddr); /* flush */
4222}
4223
e59058c4 4224/**
3621a710 4225 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4226 * @phba: Pointer to HBA context object.
4227 *
4228 * This function issues a kill_board mailbox command and waits for
4229 * the error attention interrupt. This function is called for stopping
4230 * the firmware processing. The caller is not required to hold any
4231 * locks. This function calls lpfc_hba_down_post function to free
4232 * any pending commands after the kill. The function will return 1 when it
4233 * fails to kill the board else will return 0.
4234 **/
41415862 4235int
2e0fef85 4236lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4237{
4238 struct lpfc_sli *psli;
4239 LPFC_MBOXQ_t *pmb;
4240 uint32_t status;
4241 uint32_t ha_copy;
4242 int retval;
4243 int i = 0;
dea3101e 4244
41415862 4245 psli = &phba->sli;
dea3101e 4246
41415862 4247 /* Kill HBA */
ed957684 4248 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4249 "0329 Kill HBA Data: x%x x%x\n",
4250 phba->pport->port_state, psli->sli_flag);
41415862 4251
98c9ea5c
JS
4252 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4253 if (!pmb)
41415862 4254 return 1;
41415862
JW
4255
4256 /* Disable the error attention */
2e0fef85 4257 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4258 if (lpfc_readl(phba->HCregaddr, &status)) {
4259 spin_unlock_irq(&phba->hbalock);
4260 mempool_free(pmb, phba->mbox_mem_pool);
4261 return 1;
4262 }
41415862
JW
4263 status &= ~HC_ERINT_ENA;
4264 writel(status, phba->HCregaddr);
4265 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4266 phba->link_flag |= LS_IGNORE_ERATT;
4267 spin_unlock_irq(&phba->hbalock);
41415862
JW
4268
4269 lpfc_kill_board(phba, pmb);
4270 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4271 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4272
4273 if (retval != MBX_SUCCESS) {
4274 if (retval != MBX_BUSY)
4275 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4277 "2752 KILL_BOARD command failed retval %d\n",
4278 retval);
2e0fef85
JS
4279 spin_lock_irq(&phba->hbalock);
4280 phba->link_flag &= ~LS_IGNORE_ERATT;
4281 spin_unlock_irq(&phba->hbalock);
41415862
JW
4282 return 1;
4283 }
4284
f4b4c68f
JS
4285 spin_lock_irq(&phba->hbalock);
4286 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4287 spin_unlock_irq(&phba->hbalock);
9290831f 4288
41415862
JW
4289 mempool_free(pmb, phba->mbox_mem_pool);
4290
4291 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4292 * attention every 100ms for 3 seconds. If we don't get ERATT after
4293 * 3 seconds we still set HBA_ERROR state because the status of the
4294 * board is now undefined.
4295 */
9940b97b
JS
4296 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4297 return 1;
41415862
JW
4298 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4299 mdelay(100);
9940b97b
JS
4300 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4301 return 1;
41415862
JW
4302 }
4303
4304 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4305 if (ha_copy & HA_ERATT) {
4306 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4307 phba->pport->stopped = 1;
9290831f 4308 }
2e0fef85 4309 spin_lock_irq(&phba->hbalock);
41415862 4310 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4311 psli->mbox_active = NULL;
2e0fef85
JS
4312 phba->link_flag &= ~LS_IGNORE_ERATT;
4313 spin_unlock_irq(&phba->hbalock);
41415862 4314
41415862 4315 lpfc_hba_down_post(phba);
2e0fef85 4316 phba->link_state = LPFC_HBA_ERROR;
41415862 4317
2e0fef85 4318 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4319}
4320
e59058c4 4321/**
3772a991 4322 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4323 * @phba: Pointer to HBA context object.
4324 *
4325 * This function resets the HBA by writing HC_INITFF to the control
4326 * register. After the HBA resets, this function resets all the iocb ring
4327 * indices. This function disables PCI layer parity checking during
4328 * the reset.
4329 * This function returns 0 always.
4330 * The caller is not required to hold any locks.
4331 **/
41415862 4332int
2e0fef85 4333lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4334{
41415862 4335 struct lpfc_sli *psli;
dea3101e 4336 struct lpfc_sli_ring *pring;
41415862 4337 uint16_t cfg_value;
dea3101e 4338 int i;
dea3101e 4339
41415862 4340 psli = &phba->sli;
dea3101e 4341
41415862
JW
4342 /* Reset HBA */
4343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4344 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4345 (phba->pport) ? phba->pport->port_state : 0,
4346 psli->sli_flag);
dea3101e
JB
4347
4348 /* perform board reset */
4349 phba->fc_eventTag = 0;
4d9ab994 4350 phba->link_events = 0;
4492b739
JS
4351 if (phba->pport) {
4352 phba->pport->fc_myDID = 0;
4353 phba->pport->fc_prevDID = 0;
4354 }
dea3101e 4355
41415862
JW
4356 /* Turn off parity checking and serr during the physical reset */
4357 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4358 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4359 (cfg_value &
4360 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4361
3772a991
JS
4362 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4363
41415862
JW
4364 /* Now toggle INITFF bit in the Host Control Register */
4365 writel(HC_INITFF, phba->HCregaddr);
4366 mdelay(1);
4367 readl(phba->HCregaddr); /* flush */
4368 writel(0, phba->HCregaddr);
4369 readl(phba->HCregaddr); /* flush */
4370
4371 /* Restore PCI cmd register */
4372 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4373
4374 /* Initialize relevant SLI info */
41415862 4375 for (i = 0; i < psli->num_rings; i++) {
895427bd 4376 pring = &psli->sli3_ring[i];
dea3101e 4377 pring->flag = 0;
7e56aa25
JS
4378 pring->sli.sli3.rspidx = 0;
4379 pring->sli.sli3.next_cmdidx = 0;
4380 pring->sli.sli3.local_getidx = 0;
4381 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4382 pring->missbufcnt = 0;
4383 }
dea3101e 4384
2e0fef85 4385 phba->link_state = LPFC_WARM_START;
41415862
JW
4386 return 0;
4387}
4388
e59058c4 4389/**
da0436e9
JS
4390 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4391 * @phba: Pointer to HBA context object.
4392 *
4393 * This function resets a SLI4 HBA. This function disables PCI layer parity
4394 * checking during resets the device. The caller is not required to hold
4395 * any locks.
4396 *
4397 * This function returns 0 always.
4398 **/
4399int
4400lpfc_sli4_brdreset(struct lpfc_hba *phba)
4401{
4402 struct lpfc_sli *psli = &phba->sli;
4403 uint16_t cfg_value;
0293635e 4404 int rc = 0;
da0436e9
JS
4405
4406 /* Reset HBA */
4407 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4408 "0295 Reset HBA Data: x%x x%x x%x\n",
4409 phba->pport->port_state, psli->sli_flag,
4410 phba->hba_flag);
da0436e9
JS
4411
4412 /* perform board reset */
4413 phba->fc_eventTag = 0;
4d9ab994 4414 phba->link_events = 0;
da0436e9
JS
4415 phba->pport->fc_myDID = 0;
4416 phba->pport->fc_prevDID = 0;
4417
da0436e9
JS
4418 spin_lock_irq(&phba->hbalock);
4419 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4420 phba->fcf.fcf_flag = 0;
da0436e9
JS
4421 spin_unlock_irq(&phba->hbalock);
4422
0293635e
JS
4423 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4424 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4425 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4426 return rc;
4427 }
4428
da0436e9
JS
4429 /* Now physically reset the device */
4430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4431 "0389 Performing PCI function reset!\n");
be858b65
JS
4432
4433 /* Turn off parity checking and serr during the physical reset */
4434 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4435 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4436 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4437
88318816 4438 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4439 rc = lpfc_pci_function_reset(phba);
da0436e9 4440
be858b65
JS
4441 /* Restore PCI cmd register */
4442 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4443
27b01b82 4444 return rc;
da0436e9
JS
4445}
4446
4447/**
4448 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4449 * @phba: Pointer to HBA context object.
4450 *
4451 * This function is called in the SLI initialization code path to
4452 * restart the HBA. The caller is not required to hold any lock.
4453 * This function writes MBX_RESTART mailbox command to the SLIM and
4454 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4455 * function to free any pending commands. The function enables
4456 * POST only during the first initialization. The function returns zero.
4457 * The function does not guarantee completion of MBX_RESTART mailbox
4458 * command before the return of this function.
4459 **/
da0436e9
JS
4460static int
4461lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4462{
4463 MAILBOX_t *mb;
4464 struct lpfc_sli *psli;
41415862
JW
4465 volatile uint32_t word0;
4466 void __iomem *to_slim;
0d878419 4467 uint32_t hba_aer_enabled;
41415862 4468
2e0fef85 4469 spin_lock_irq(&phba->hbalock);
41415862 4470
0d878419
JS
4471 /* Take PCIe device Advanced Error Reporting (AER) state */
4472 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4473
41415862
JW
4474 psli = &phba->sli;
4475
4476 /* Restart HBA */
4477 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4478 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4479 (phba->pport) ? phba->pport->port_state : 0,
4480 psli->sli_flag);
41415862
JW
4481
4482 word0 = 0;
4483 mb = (MAILBOX_t *) &word0;
4484 mb->mbxCommand = MBX_RESTART;
4485 mb->mbxHc = 1;
4486
9290831f
JS
4487 lpfc_reset_barrier(phba);
4488
41415862
JW
4489 to_slim = phba->MBslimaddr;
4490 writel(*(uint32_t *) mb, to_slim);
4491 readl(to_slim); /* flush */
4492
4493 /* Only skip post after fc_ffinit is completed */
4492b739 4494 if (phba->pport && phba->pport->port_state)
41415862 4495 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4496 else
41415862 4497 word0 = 0; /* This is really setting up word1 */
65a29c16 4498 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4499 writel(*(uint32_t *) mb, to_slim);
4500 readl(to_slim); /* flush */
dea3101e 4501
41415862 4502 lpfc_sli_brdreset(phba);
4492b739
JS
4503 if (phba->pport)
4504 phba->pport->stopped = 0;
2e0fef85 4505 phba->link_state = LPFC_INIT_START;
da0436e9 4506 phba->hba_flag = 0;
2e0fef85 4507 spin_unlock_irq(&phba->hbalock);
41415862 4508
64ba8818
JS
4509 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4510 psli->stats_start = get_seconds();
4511
eaf15d5b
JS
4512 /* Give the INITFF and Post time to settle. */
4513 mdelay(100);
41415862 4514
0d878419
JS
4515 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4516 if (hba_aer_enabled)
4517 pci_disable_pcie_error_reporting(phba->pcidev);
4518
41415862 4519 lpfc_hba_down_post(phba);
dea3101e
JB
4520
4521 return 0;
4522}
4523
da0436e9
JS
4524/**
4525 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4526 * @phba: Pointer to HBA context object.
4527 *
4528 * This function is called in the SLI initialization code path to restart
4529 * a SLI4 HBA. The caller is not required to hold any lock.
4530 * At the end of the function, it calls lpfc_hba_down_post function to
4531 * free any pending commands.
4532 **/
4533static int
4534lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4535{
4536 struct lpfc_sli *psli = &phba->sli;
75baf696 4537 uint32_t hba_aer_enabled;
27b01b82 4538 int rc;
da0436e9
JS
4539
4540 /* Restart HBA */
4541 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4542 "0296 Restart HBA Data: x%x x%x\n",
4543 phba->pport->port_state, psli->sli_flag);
4544
75baf696
JS
4545 /* Take PCIe device Advanced Error Reporting (AER) state */
4546 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4547
27b01b82 4548 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4549
4550 spin_lock_irq(&phba->hbalock);
4551 phba->pport->stopped = 0;
4552 phba->link_state = LPFC_INIT_START;
4553 phba->hba_flag = 0;
4554 spin_unlock_irq(&phba->hbalock);
4555
4556 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4557 psli->stats_start = get_seconds();
4558
75baf696
JS
4559 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4560 if (hba_aer_enabled)
4561 pci_disable_pcie_error_reporting(phba->pcidev);
4562
da0436e9 4563 lpfc_hba_down_post(phba);
569dbe84 4564 lpfc_sli4_queue_destroy(phba);
da0436e9 4565
27b01b82 4566 return rc;
da0436e9
JS
4567}
4568
4569/**
4570 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4571 * @phba: Pointer to HBA context object.
4572 *
4573 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4574 * API jump table function pointer from the lpfc_hba struct.
4575**/
4576int
4577lpfc_sli_brdrestart(struct lpfc_hba *phba)
4578{
4579 return phba->lpfc_sli_brdrestart(phba);
4580}
4581
e59058c4 4582/**
3621a710 4583 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4584 * @phba: Pointer to HBA context object.
4585 *
4586 * This function is called after a HBA restart to wait for successful
4587 * restart of the HBA. Successful restart of the HBA is indicated by
4588 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4589 * iteration, the function will restart the HBA again. The function returns
4590 * zero if HBA successfully restarted else returns negative error code.
4591 **/
4492b739 4592int
dea3101e
JB
4593lpfc_sli_chipset_init(struct lpfc_hba *phba)
4594{
4595 uint32_t status, i = 0;
4596
4597 /* Read the HBA Host Status Register */
9940b97b
JS
4598 if (lpfc_readl(phba->HSregaddr, &status))
4599 return -EIO;
dea3101e
JB
4600
4601 /* Check status register to see what current state is */
4602 i = 0;
4603 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4604
dcf2a4e0
JS
4605 /* Check every 10ms for 10 retries, then every 100ms for 90
4606 * retries, then every 1 sec for 50 retires for a total of
4607 * ~60 seconds before reset the board again and check every
4608 * 1 sec for 50 retries. The up to 60 seconds before the
4609 * board ready is required by the Falcon FIPS zeroization
4610 * complete, and any reset the board in between shall cause
4611 * restart of zeroization, further delay the board ready.
dea3101e 4612 */
dcf2a4e0 4613 if (i++ >= 200) {
dea3101e
JB
4614 /* Adapter failed to init, timeout, status reg
4615 <status> */
ed957684 4616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4617 "0436 Adapter failed to init, "
09372820
JS
4618 "timeout, status reg x%x, "
4619 "FW Data: A8 x%x AC x%x\n", status,
4620 readl(phba->MBslimaddr + 0xa8),
4621 readl(phba->MBslimaddr + 0xac));
2e0fef85 4622 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4623 return -ETIMEDOUT;
4624 }
4625
4626 /* Check to see if any errors occurred during init */
4627 if (status & HS_FFERM) {
4628 /* ERROR: During chipset initialization */
4629 /* Adapter failed to init, chipset, status reg
4630 <status> */
ed957684 4631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4632 "0437 Adapter failed to init, "
09372820
JS
4633 "chipset, status reg x%x, "
4634 "FW Data: A8 x%x AC x%x\n", status,
4635 readl(phba->MBslimaddr + 0xa8),
4636 readl(phba->MBslimaddr + 0xac));
2e0fef85 4637 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4638 return -EIO;
4639 }
4640
dcf2a4e0 4641 if (i <= 10)
dea3101e 4642 msleep(10);
dcf2a4e0
JS
4643 else if (i <= 100)
4644 msleep(100);
4645 else
4646 msleep(1000);
dea3101e 4647
dcf2a4e0
JS
4648 if (i == 150) {
4649 /* Do post */
92d7f7b0 4650 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4651 lpfc_sli_brdrestart(phba);
dea3101e
JB
4652 }
4653 /* Read the HBA Host Status Register */
9940b97b
JS
4654 if (lpfc_readl(phba->HSregaddr, &status))
4655 return -EIO;
dea3101e
JB
4656 }
4657
4658 /* Check to see if any errors occurred during init */
4659 if (status & HS_FFERM) {
4660 /* ERROR: During chipset initialization */
4661 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4663 "0438 Adapter failed to init, chipset, "
09372820
JS
4664 "status reg x%x, "
4665 "FW Data: A8 x%x AC x%x\n", status,
4666 readl(phba->MBslimaddr + 0xa8),
4667 readl(phba->MBslimaddr + 0xac));
2e0fef85 4668 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4669 return -EIO;
4670 }
4671
4672 /* Clear all interrupt enable conditions */
4673 writel(0, phba->HCregaddr);
4674 readl(phba->HCregaddr); /* flush */
4675
4676 /* setup host attn register */
4677 writel(0xffffffff, phba->HAregaddr);
4678 readl(phba->HAregaddr); /* flush */
4679 return 0;
4680}
4681
e59058c4 4682/**
3621a710 4683 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4684 *
4685 * This function calculates and returns the number of HBQs required to be
4686 * configured.
4687 **/
78b2d852 4688int
ed957684
JS
4689lpfc_sli_hbq_count(void)
4690{
92d7f7b0 4691 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4692}
4693
e59058c4 4694/**
3621a710 4695 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4696 *
4697 * This function adds the number of hbq entries in every HBQ to get
4698 * the total number of hbq entries required for the HBA and returns
4699 * the total count.
4700 **/
ed957684
JS
4701static int
4702lpfc_sli_hbq_entry_count(void)
4703{
4704 int hbq_count = lpfc_sli_hbq_count();
4705 int count = 0;
4706 int i;
4707
4708 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4709 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4710 return count;
4711}
4712
e59058c4 4713/**
3621a710 4714 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4715 *
4716 * This function calculates amount of memory required for all hbq entries
4717 * to be configured and returns the total memory required.
4718 **/
dea3101e 4719int
ed957684
JS
4720lpfc_sli_hbq_size(void)
4721{
4722 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4723}
4724
e59058c4 4725/**
3621a710 4726 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4727 * @phba: Pointer to HBA context object.
4728 *
4729 * This function is called during the SLI initialization to configure
4730 * all the HBQs and post buffers to the HBQ. The caller is not
4731 * required to hold any locks. This function will return zero if successful
4732 * else it will return negative error code.
4733 **/
ed957684
JS
4734static int
4735lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4736{
4737 int hbq_count = lpfc_sli_hbq_count();
4738 LPFC_MBOXQ_t *pmb;
4739 MAILBOX_t *pmbox;
4740 uint32_t hbqno;
4741 uint32_t hbq_entry_index;
ed957684 4742
92d7f7b0
JS
4743 /* Get a Mailbox buffer to setup mailbox
4744 * commands for HBA initialization
4745 */
ed957684
JS
4746 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4747
4748 if (!pmb)
4749 return -ENOMEM;
4750
04c68496 4751 pmbox = &pmb->u.mb;
ed957684
JS
4752
4753 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4754 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4755 phba->hbq_in_use = 1;
ed957684
JS
4756
4757 hbq_entry_index = 0;
4758 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4759 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4760 phba->hbqs[hbqno].hbqPutIdx = 0;
4761 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4762 phba->hbqs[hbqno].entry_count =
92d7f7b0 4763 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4764 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4765 hbq_entry_index, pmb);
ed957684
JS
4766 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4767
4768 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4769 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4770 mbxStatus <status>, ring <num> */
4771
4772 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4773 LOG_SLI | LOG_VPORT,
e8b62011 4774 "1805 Adapter failed to init. "
ed957684 4775 "Data: x%x x%x x%x\n",
e8b62011 4776 pmbox->mbxCommand,
ed957684
JS
4777 pmbox->mbxStatus, hbqno);
4778
4779 phba->link_state = LPFC_HBA_ERROR;
4780 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4781 return -ENXIO;
ed957684
JS
4782 }
4783 }
4784 phba->hbq_count = hbq_count;
4785
ed957684
JS
4786 mempool_free(pmb, phba->mbox_mem_pool);
4787
92d7f7b0 4788 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4789 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4790 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4791 return 0;
4792}
4793
4f774513
JS
4794/**
4795 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4796 * @phba: Pointer to HBA context object.
4797 *
4798 * This function is called during the SLI initialization to configure
4799 * all the HBQs and post buffers to the HBQ. The caller is not
4800 * required to hold any locks. This function will return zero if successful
4801 * else it will return negative error code.
4802 **/
4803static int
4804lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4805{
4806 phba->hbq_in_use = 1;
895427bd
JS
4807 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4808 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4809 phba->hbq_count = 1;
895427bd 4810 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4811 /* Initially populate or replenish the HBQs */
4f774513
JS
4812 return 0;
4813}
4814
e59058c4 4815/**
3621a710 4816 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4817 * @phba: Pointer to HBA context object.
4818 * @sli_mode: sli mode - 2/3
4819 *
183b8021 4820 * This function is called by the sli initialization code path
e59058c4
JS
4821 * to issue config_port mailbox command. This function restarts the
4822 * HBA firmware and issues a config_port mailbox command to configure
4823 * the SLI interface in the sli mode specified by sli_mode
4824 * variable. The caller is not required to hold any locks.
4825 * The function returns 0 if successful, else returns negative error
4826 * code.
4827 **/
9399627f
JS
4828int
4829lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4830{
4831 LPFC_MBOXQ_t *pmb;
4832 uint32_t resetcount = 0, rc = 0, done = 0;
4833
4834 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4835 if (!pmb) {
2e0fef85 4836 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4837 return -ENOMEM;
4838 }
4839
ed957684 4840 phba->sli_rev = sli_mode;
dea3101e 4841 while (resetcount < 2 && !done) {
2e0fef85 4842 spin_lock_irq(&phba->hbalock);
1c067a42 4843 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4844 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4845 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4846 lpfc_sli_brdrestart(phba);
dea3101e
JB
4847 rc = lpfc_sli_chipset_init(phba);
4848 if (rc)
4849 break;
4850
2e0fef85 4851 spin_lock_irq(&phba->hbalock);
1c067a42 4852 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4853 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4854 resetcount++;
4855
ed957684
JS
4856 /* Call pre CONFIG_PORT mailbox command initialization. A
4857 * value of 0 means the call was successful. Any other
4858 * nonzero value is a failure, but if ERESTART is returned,
4859 * the driver may reset the HBA and try again.
4860 */
dea3101e
JB
4861 rc = lpfc_config_port_prep(phba);
4862 if (rc == -ERESTART) {
ed957684 4863 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4864 continue;
34b02dcd 4865 } else if (rc)
dea3101e 4866 break;
6d368e53 4867
2e0fef85 4868 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4869 lpfc_config_port(phba, pmb);
4870 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4871 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4872 LPFC_SLI3_HBQ_ENABLED |
4873 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4874 LPFC_SLI3_BG_ENABLED |
4875 LPFC_SLI3_DSS_ENABLED);
ed957684 4876 if (rc != MBX_SUCCESS) {
dea3101e 4877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4878 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4879 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4880 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4881 spin_lock_irq(&phba->hbalock);
04c68496 4882 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4883 spin_unlock_irq(&phba->hbalock);
4884 rc = -ENXIO;
04c68496
JS
4885 } else {
4886 /* Allow asynchronous mailbox command to go through */
4887 spin_lock_irq(&phba->hbalock);
4888 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4889 spin_unlock_irq(&phba->hbalock);
ed957684 4890 done = 1;
cb69f7de
JS
4891
4892 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4893 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4894 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4895 "3110 Port did not grant ASABT\n");
04c68496 4896 }
dea3101e 4897 }
ed957684
JS
4898 if (!done) {
4899 rc = -EINVAL;
4900 goto do_prep_failed;
4901 }
04c68496
JS
4902 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4903 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4904 rc = -ENXIO;
4905 goto do_prep_failed;
4906 }
04c68496 4907 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4908 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4909 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4910 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4911 phba->max_vpi : phba->max_vports;
4912
34b02dcd
JS
4913 } else
4914 phba->max_vpi = 0;
bc73905a
JS
4915 phba->fips_level = 0;
4916 phba->fips_spec_rev = 0;
4917 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4918 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4919 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4920 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4921 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4922 "2850 Security Crypto Active. FIPS x%d "
4923 "(Spec Rev: x%d)",
4924 phba->fips_level, phba->fips_spec_rev);
4925 }
4926 if (pmb->u.mb.un.varCfgPort.sec_err) {
4927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4928 "2856 Config Port Security Crypto "
4929 "Error: x%x ",
4930 pmb->u.mb.un.varCfgPort.sec_err);
4931 }
04c68496 4932 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4933 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4934 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4935 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4936
4937 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4938 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4939
4940 if (phba->cfg_enable_bg) {
04c68496 4941 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4942 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4943 else
4944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4945 "0443 Adapter did not grant "
4946 "BlockGuard\n");
4947 }
34b02dcd 4948 } else {
8f34f4ce 4949 phba->hbq_get = NULL;
34b02dcd 4950 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4951 phba->max_vpi = 0;
ed957684 4952 }
92d7f7b0 4953do_prep_failed:
ed957684
JS
4954 mempool_free(pmb, phba->mbox_mem_pool);
4955 return rc;
4956}
4957
e59058c4
JS
4958
4959/**
183b8021 4960 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
4961 * @phba: Pointer to HBA context object.
4962 *
183b8021
MY
4963 * This function is the main SLI initialization function. This function
4964 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
4965 * error attention handler code. Caller is not required to hold any
4966 * locks. This function issues config_port mailbox command to configure
4967 * the SLI, setup iocb rings and HBQ rings. In the end the function
4968 * calls the config_port_post function to issue init_link mailbox
4969 * command and to start the discovery. The function will return zero
4970 * if successful, else it will return negative error code.
4971 **/
ed957684
JS
4972int
4973lpfc_sli_hba_setup(struct lpfc_hba *phba)
4974{
4975 uint32_t rc;
6d368e53
JS
4976 int mode = 3, i;
4977 int longs;
ed957684 4978
12247e81 4979 switch (phba->cfg_sli_mode) {
ed957684 4980 case 2:
78b2d852 4981 if (phba->cfg_enable_npiv) {
92d7f7b0 4982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 4983 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 4984 "parameter (%d) to auto (0).\n",
12247e81 4985 phba->cfg_sli_mode);
92d7f7b0
JS
4986 break;
4987 }
ed957684
JS
4988 mode = 2;
4989 break;
4990 case 0:
4991 case 3:
4992 break;
4993 default:
92d7f7b0 4994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
4995 "1819 Unrecognized sli_mode parameter: %d.\n",
4996 phba->cfg_sli_mode);
ed957684
JS
4997
4998 break;
4999 }
b5c53958 5000 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 5001
9399627f
JS
5002 rc = lpfc_sli_config_port(phba, mode);
5003
12247e81 5004 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 5005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
5006 "1820 Unable to select SLI-3. "
5007 "Not supported by adapter.\n");
ed957684 5008 if (rc && mode != 2)
9399627f 5009 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
5010 else if (rc && mode == 2)
5011 rc = lpfc_sli_config_port(phba, 3);
ed957684 5012 if (rc)
dea3101e
JB
5013 goto lpfc_sli_hba_setup_error;
5014
0d878419
JS
5015 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5016 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5017 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5018 if (!rc) {
5019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5020 "2709 This device supports "
5021 "Advanced Error Reporting (AER)\n");
5022 spin_lock_irq(&phba->hbalock);
5023 phba->hba_flag |= HBA_AER_ENABLED;
5024 spin_unlock_irq(&phba->hbalock);
5025 } else {
5026 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5027 "2708 This device does not support "
b069d7eb
JS
5028 "Advanced Error Reporting (AER): %d\n",
5029 rc);
0d878419
JS
5030 phba->cfg_aer_support = 0;
5031 }
5032 }
5033
ed957684
JS
5034 if (phba->sli_rev == 3) {
5035 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5036 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
5037 } else {
5038 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5039 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 5040 phba->sli3_options = 0;
ed957684
JS
5041 }
5042
5043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
5044 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5045 phba->sli_rev, phba->max_vpi);
ed957684 5046 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
5047
5048 if (rc)
5049 goto lpfc_sli_hba_setup_error;
5050
6d368e53
JS
5051 /* Initialize VPIs. */
5052 if (phba->sli_rev == LPFC_SLI_REV3) {
5053 /*
5054 * The VPI bitmask and physical ID array are allocated
5055 * and initialized once only - at driver load. A port
5056 * reset doesn't need to reinitialize this memory.
5057 */
5058 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5059 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5060 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5061 GFP_KERNEL);
5062 if (!phba->vpi_bmask) {
5063 rc = -ENOMEM;
5064 goto lpfc_sli_hba_setup_error;
5065 }
5066
5067 phba->vpi_ids = kzalloc(
5068 (phba->max_vpi+1) * sizeof(uint16_t),
5069 GFP_KERNEL);
5070 if (!phba->vpi_ids) {
5071 kfree(phba->vpi_bmask);
5072 rc = -ENOMEM;
5073 goto lpfc_sli_hba_setup_error;
5074 }
5075 for (i = 0; i < phba->max_vpi; i++)
5076 phba->vpi_ids[i] = i;
5077 }
5078 }
5079
9399627f 5080 /* Init HBQs */
ed957684
JS
5081 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5082 rc = lpfc_sli_hbq_setup(phba);
5083 if (rc)
5084 goto lpfc_sli_hba_setup_error;
5085 }
04c68496 5086 spin_lock_irq(&phba->hbalock);
dea3101e 5087 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 5088 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
5089
5090 rc = lpfc_config_port_post(phba);
5091 if (rc)
5092 goto lpfc_sli_hba_setup_error;
5093
ed957684
JS
5094 return rc;
5095
92d7f7b0 5096lpfc_sli_hba_setup_error:
2e0fef85 5097 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 5098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 5099 "0445 Firmware initialization failed\n");
dea3101e
JB
5100 return rc;
5101}
5102
e59058c4 5103/**
da0436e9
JS
5104 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5105 * @phba: Pointer to HBA context object.
5106 * @mboxq: mailbox pointer.
5107 * This function issue a dump mailbox command to read config region
5108 * 23 and parse the records in the region and populate driver
5109 * data structure.
e59058c4 5110 **/
da0436e9 5111static int
ff78d8f9 5112lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 5113{
ff78d8f9 5114 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
5115 struct lpfc_dmabuf *mp;
5116 struct lpfc_mqe *mqe;
5117 uint32_t data_length;
5118 int rc;
dea3101e 5119
da0436e9
JS
5120 /* Program the default value of vlan_id and fc_map */
5121 phba->valid_vlan = 0;
5122 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5123 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5124 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 5125
ff78d8f9
JS
5126 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5127 if (!mboxq)
da0436e9
JS
5128 return -ENOMEM;
5129
ff78d8f9
JS
5130 mqe = &mboxq->u.mqe;
5131 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5132 rc = -ENOMEM;
5133 goto out_free_mboxq;
5134 }
5135
da0436e9
JS
5136 mp = (struct lpfc_dmabuf *) mboxq->context1;
5137 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5138
5139 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5140 "(%d):2571 Mailbox cmd x%x Status x%x "
5141 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5142 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5143 "CQ: x%x x%x x%x x%x\n",
5144 mboxq->vport ? mboxq->vport->vpi : 0,
5145 bf_get(lpfc_mqe_command, mqe),
5146 bf_get(lpfc_mqe_status, mqe),
5147 mqe->un.mb_words[0], mqe->un.mb_words[1],
5148 mqe->un.mb_words[2], mqe->un.mb_words[3],
5149 mqe->un.mb_words[4], mqe->un.mb_words[5],
5150 mqe->un.mb_words[6], mqe->un.mb_words[7],
5151 mqe->un.mb_words[8], mqe->un.mb_words[9],
5152 mqe->un.mb_words[10], mqe->un.mb_words[11],
5153 mqe->un.mb_words[12], mqe->un.mb_words[13],
5154 mqe->un.mb_words[14], mqe->un.mb_words[15],
5155 mqe->un.mb_words[16], mqe->un.mb_words[50],
5156 mboxq->mcqe.word0,
5157 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5158 mboxq->mcqe.trailer);
5159
5160 if (rc) {
5161 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5162 kfree(mp);
ff78d8f9
JS
5163 rc = -EIO;
5164 goto out_free_mboxq;
da0436e9
JS
5165 }
5166 data_length = mqe->un.mb_words[5];
a0c87cbd 5167 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5168 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5169 kfree(mp);
ff78d8f9
JS
5170 rc = -EIO;
5171 goto out_free_mboxq;
d11e31dd 5172 }
dea3101e 5173
da0436e9
JS
5174 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5175 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5176 kfree(mp);
ff78d8f9
JS
5177 rc = 0;
5178
5179out_free_mboxq:
5180 mempool_free(mboxq, phba->mbox_mem_pool);
5181 return rc;
da0436e9 5182}
e59058c4
JS
5183
5184/**
da0436e9
JS
5185 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5186 * @phba: pointer to lpfc hba data structure.
5187 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5188 * @vpd: pointer to the memory to hold resulting port vpd data.
5189 * @vpd_size: On input, the number of bytes allocated to @vpd.
5190 * On output, the number of data bytes in @vpd.
e59058c4 5191 *
da0436e9
JS
5192 * This routine executes a READ_REV SLI4 mailbox command. In
5193 * addition, this routine gets the port vpd data.
5194 *
5195 * Return codes
af901ca1 5196 * 0 - successful
d439d286 5197 * -ENOMEM - could not allocated memory.
e59058c4 5198 **/
da0436e9
JS
5199static int
5200lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5201 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5202{
da0436e9
JS
5203 int rc = 0;
5204 uint32_t dma_size;
5205 struct lpfc_dmabuf *dmabuf;
5206 struct lpfc_mqe *mqe;
dea3101e 5207
da0436e9
JS
5208 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5209 if (!dmabuf)
5210 return -ENOMEM;
5211
5212 /*
5213 * Get a DMA buffer for the vpd data resulting from the READ_REV
5214 * mailbox command.
a257bf90 5215 */
da0436e9 5216 dma_size = *vpd_size;
1aee383d
JP
5217 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5218 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5219 if (!dmabuf->virt) {
5220 kfree(dmabuf);
5221 return -ENOMEM;
a257bf90
JS
5222 }
5223
da0436e9
JS
5224 /*
5225 * The SLI4 implementation of READ_REV conflicts at word1,
5226 * bits 31:16 and SLI4 adds vpd functionality not present
5227 * in SLI3. This code corrects the conflicts.
1dcb58e5 5228 */
da0436e9
JS
5229 lpfc_read_rev(phba, mboxq);
5230 mqe = &mboxq->u.mqe;
5231 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5232 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5233 mqe->un.read_rev.word1 &= 0x0000FFFF;
5234 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5235 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5236
5237 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5238 if (rc) {
5239 dma_free_coherent(&phba->pcidev->dev, dma_size,
5240 dmabuf->virt, dmabuf->phys);
def9c7a9 5241 kfree(dmabuf);
da0436e9
JS
5242 return -EIO;
5243 }
1dcb58e5 5244
da0436e9
JS
5245 /*
5246 * The available vpd length cannot be bigger than the
5247 * DMA buffer passed to the port. Catch the less than
5248 * case and update the caller's size.
5249 */
5250 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5251 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5252
d7c47992
JS
5253 memcpy(vpd, dmabuf->virt, *vpd_size);
5254
da0436e9
JS
5255 dma_free_coherent(&phba->pcidev->dev, dma_size,
5256 dmabuf->virt, dmabuf->phys);
5257 kfree(dmabuf);
5258 return 0;
dea3101e
JB
5259}
5260
cd1c8301
JS
5261/**
5262 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5263 * @phba: pointer to lpfc hba data structure.
5264 *
5265 * This routine retrieves SLI4 device physical port name this PCI function
5266 * is attached to.
5267 *
5268 * Return codes
4907cb7b 5269 * 0 - successful
cd1c8301
JS
5270 * otherwise - failed to retrieve physical port name
5271 **/
5272static int
5273lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5274{
5275 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5276 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5277 struct lpfc_controller_attribute *cntl_attr;
5278 struct lpfc_mbx_get_port_name *get_port_name;
5279 void *virtaddr = NULL;
5280 uint32_t alloclen, reqlen;
5281 uint32_t shdr_status, shdr_add_status;
5282 union lpfc_sli4_cfg_shdr *shdr;
5283 char cport_name = 0;
5284 int rc;
5285
5286 /* We assume nothing at this point */
5287 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5288 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5289
5290 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5291 if (!mboxq)
5292 return -ENOMEM;
cd1c8301 5293 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5294 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5295 lpfc_sli4_read_config(phba);
5296 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5297 goto retrieve_ppname;
cd1c8301
JS
5298
5299 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5300 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5301 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5302 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5303 LPFC_SLI4_MBX_NEMBED);
5304 if (alloclen < reqlen) {
5305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5306 "3084 Allocated DMA memory size (%d) is "
5307 "less than the requested DMA memory size "
5308 "(%d)\n", alloclen, reqlen);
5309 rc = -ENOMEM;
5310 goto out_free_mboxq;
5311 }
5312 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5313 virtaddr = mboxq->sge_array->addr[0];
5314 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5315 shdr = &mbx_cntl_attr->cfg_shdr;
5316 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5317 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5318 if (shdr_status || shdr_add_status || rc) {
5319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5320 "3085 Mailbox x%x (x%x/x%x) failed, "
5321 "rc:x%x, status:x%x, add_status:x%x\n",
5322 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5323 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5324 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5325 rc, shdr_status, shdr_add_status);
5326 rc = -ENXIO;
5327 goto out_free_mboxq;
5328 }
5329 cntl_attr = &mbx_cntl_attr->cntl_attr;
5330 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5331 phba->sli4_hba.lnk_info.lnk_tp =
5332 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5333 phba->sli4_hba.lnk_info.lnk_no =
5334 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5335 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5336 "3086 lnk_type:%d, lnk_numb:%d\n",
5337 phba->sli4_hba.lnk_info.lnk_tp,
5338 phba->sli4_hba.lnk_info.lnk_no);
5339
5340retrieve_ppname:
5341 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5342 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5343 sizeof(struct lpfc_mbx_get_port_name) -
5344 sizeof(struct lpfc_sli4_cfg_mhdr),
5345 LPFC_SLI4_MBX_EMBED);
5346 get_port_name = &mboxq->u.mqe.un.get_port_name;
5347 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5348 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5349 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5350 phba->sli4_hba.lnk_info.lnk_tp);
5351 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5354 if (shdr_status || shdr_add_status || rc) {
5355 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5356 "3087 Mailbox x%x (x%x/x%x) failed: "
5357 "rc:x%x, status:x%x, add_status:x%x\n",
5358 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5359 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5360 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5361 rc, shdr_status, shdr_add_status);
5362 rc = -ENXIO;
5363 goto out_free_mboxq;
5364 }
5365 switch (phba->sli4_hba.lnk_info.lnk_no) {
5366 case LPFC_LINK_NUMBER_0:
5367 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5368 &get_port_name->u.response);
5369 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5370 break;
5371 case LPFC_LINK_NUMBER_1:
5372 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5373 &get_port_name->u.response);
5374 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5375 break;
5376 case LPFC_LINK_NUMBER_2:
5377 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5378 &get_port_name->u.response);
5379 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5380 break;
5381 case LPFC_LINK_NUMBER_3:
5382 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5383 &get_port_name->u.response);
5384 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5385 break;
5386 default:
5387 break;
5388 }
5389
5390 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5391 phba->Port[0] = cport_name;
5392 phba->Port[1] = '\0';
5393 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5394 "3091 SLI get port name: %s\n", phba->Port);
5395 }
5396
5397out_free_mboxq:
5398 if (rc != MBX_TIMEOUT) {
5399 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5400 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5401 else
5402 mempool_free(mboxq, phba->mbox_mem_pool);
5403 }
5404 return rc;
5405}
5406
e59058c4 5407/**
da0436e9
JS
5408 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5409 * @phba: pointer to lpfc hba data structure.
e59058c4 5410 *
da0436e9
JS
5411 * This routine is called to explicitly arm the SLI4 device's completion and
5412 * event queues
5413 **/
5414static void
5415lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5416{
895427bd 5417 int qidx;
b71413dd 5418 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
da0436e9 5419
b71413dd
JS
5420 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5421 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5422 if (sli4_hba->nvmels_cq)
5423 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
895427bd
JS
5424 LPFC_QUEUE_REARM);
5425
b71413dd 5426 if (sli4_hba->fcp_cq)
895427bd 5427 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
b71413dd 5428 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
895427bd
JS
5429 LPFC_QUEUE_REARM);
5430
b71413dd 5431 if (sli4_hba->nvme_cq)
895427bd 5432 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
b71413dd 5433 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
895427bd 5434 LPFC_QUEUE_REARM);
1ba981fd 5435
f38fa0bb 5436 if (phba->cfg_fof)
b71413dd 5437 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
1ba981fd 5438
b71413dd 5439 if (sli4_hba->hba_eq)
895427bd 5440 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
b71413dd
JS
5441 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5442 LPFC_QUEUE_REARM);
1ba981fd 5443
2d7dbc4c
JS
5444 if (phba->nvmet_support) {
5445 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
b71413dd
JS
5446 sli4_hba->sli4_cq_release(
5447 sli4_hba->nvmet_cqset[qidx],
2d7dbc4c
JS
5448 LPFC_QUEUE_REARM);
5449 }
2e90f4b5 5450 }
1ba981fd
JS
5451
5452 if (phba->cfg_fof)
b71413dd 5453 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5454}
5455
6d368e53
JS
5456/**
5457 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5458 * @phba: Pointer to HBA context object.
5459 * @type: The resource extent type.
b76f2dc9
JS
5460 * @extnt_count: buffer to hold port available extent count.
5461 * @extnt_size: buffer to hold element count per extent.
6d368e53 5462 *
b76f2dc9
JS
5463 * This function calls the port and retrievs the number of available
5464 * extents and their size for a particular extent type.
5465 *
5466 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5467 **/
b76f2dc9 5468int
6d368e53
JS
5469lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5470 uint16_t *extnt_count, uint16_t *extnt_size)
5471{
5472 int rc = 0;
5473 uint32_t length;
5474 uint32_t mbox_tmo;
5475 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5476 LPFC_MBOXQ_t *mbox;
5477
5478 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5479 if (!mbox)
5480 return -ENOMEM;
5481
5482 /* Find out how many extents are available for this resource type */
5483 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5484 sizeof(struct lpfc_sli4_cfg_mhdr));
5485 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5486 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5487 length, LPFC_SLI4_MBX_EMBED);
5488
5489 /* Send an extents count of 0 - the GET doesn't use it. */
5490 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5491 LPFC_SLI4_MBX_EMBED);
5492 if (unlikely(rc)) {
5493 rc = -EIO;
5494 goto err_exit;
5495 }
5496
5497 if (!phba->sli4_hba.intr_enable)
5498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5499 else {
a183a15f 5500 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5501 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5502 }
5503 if (unlikely(rc)) {
5504 rc = -EIO;
5505 goto err_exit;
5506 }
5507
5508 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5509 if (bf_get(lpfc_mbox_hdr_status,
5510 &rsrc_info->header.cfg_shdr.response)) {
5511 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5512 "2930 Failed to get resource extents "
5513 "Status 0x%x Add'l Status 0x%x\n",
5514 bf_get(lpfc_mbox_hdr_status,
5515 &rsrc_info->header.cfg_shdr.response),
5516 bf_get(lpfc_mbox_hdr_add_status,
5517 &rsrc_info->header.cfg_shdr.response));
5518 rc = -EIO;
5519 goto err_exit;
5520 }
5521
5522 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5523 &rsrc_info->u.rsp);
5524 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5525 &rsrc_info->u.rsp);
8a9d2e80
JS
5526
5527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5528 "3162 Retrieved extents type-%d from port: count:%d, "
5529 "size:%d\n", type, *extnt_count, *extnt_size);
5530
5531err_exit:
6d368e53
JS
5532 mempool_free(mbox, phba->mbox_mem_pool);
5533 return rc;
5534}
5535
5536/**
5537 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5538 * @phba: Pointer to HBA context object.
5539 * @type: The extent type to check.
5540 *
5541 * This function reads the current available extents from the port and checks
5542 * if the extent count or extent size has changed since the last access.
5543 * Callers use this routine post port reset to understand if there is a
5544 * extent reprovisioning requirement.
5545 *
5546 * Returns:
5547 * -Error: error indicates problem.
5548 * 1: Extent count or size has changed.
5549 * 0: No changes.
5550 **/
5551static int
5552lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5553{
5554 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5555 uint16_t size_diff, rsrc_ext_size;
5556 int rc = 0;
5557 struct lpfc_rsrc_blks *rsrc_entry;
5558 struct list_head *rsrc_blk_list = NULL;
5559
5560 size_diff = 0;
5561 curr_ext_cnt = 0;
5562 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5563 &rsrc_ext_cnt,
5564 &rsrc_ext_size);
5565 if (unlikely(rc))
5566 return -EIO;
5567
5568 switch (type) {
5569 case LPFC_RSC_TYPE_FCOE_RPI:
5570 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5571 break;
5572 case LPFC_RSC_TYPE_FCOE_VPI:
5573 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5574 break;
5575 case LPFC_RSC_TYPE_FCOE_XRI:
5576 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5577 break;
5578 case LPFC_RSC_TYPE_FCOE_VFI:
5579 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5580 break;
5581 default:
5582 break;
5583 }
5584
5585 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5586 curr_ext_cnt++;
5587 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5588 size_diff++;
5589 }
5590
5591 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5592 rc = 1;
5593
5594 return rc;
5595}
5596
5597/**
5598 * lpfc_sli4_cfg_post_extnts -
5599 * @phba: Pointer to HBA context object.
5600 * @extnt_cnt - number of available extents.
5601 * @type - the extent type (rpi, xri, vfi, vpi).
5602 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5603 * @mbox - pointer to the caller's allocated mailbox structure.
5604 *
5605 * This function executes the extents allocation request. It also
5606 * takes care of the amount of memory needed to allocate or get the
5607 * allocated extents. It is the caller's responsibility to evaluate
5608 * the response.
5609 *
5610 * Returns:
5611 * -Error: Error value describes the condition found.
5612 * 0: if successful
5613 **/
5614static int
8a9d2e80 5615lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5616 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5617{
5618 int rc = 0;
5619 uint32_t req_len;
5620 uint32_t emb_len;
5621 uint32_t alloc_len, mbox_tmo;
5622
5623 /* Calculate the total requested length of the dma memory */
8a9d2e80 5624 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5625
5626 /*
5627 * Calculate the size of an embedded mailbox. The uint32_t
5628 * accounts for extents-specific word.
5629 */
5630 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5631 sizeof(uint32_t);
5632
5633 /*
5634 * Presume the allocation and response will fit into an embedded
5635 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5636 */
5637 *emb = LPFC_SLI4_MBX_EMBED;
5638 if (req_len > emb_len) {
8a9d2e80 5639 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5640 sizeof(union lpfc_sli4_cfg_shdr) +
5641 sizeof(uint32_t);
5642 *emb = LPFC_SLI4_MBX_NEMBED;
5643 }
5644
5645 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5646 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5647 req_len, *emb);
5648 if (alloc_len < req_len) {
5649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5650 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5651 "less than the requested DMA memory "
5652 "size (x%x)\n", alloc_len, req_len);
5653 return -ENOMEM;
5654 }
8a9d2e80 5655 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5656 if (unlikely(rc))
5657 return -EIO;
5658
5659 if (!phba->sli4_hba.intr_enable)
5660 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5661 else {
a183a15f 5662 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5663 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5664 }
5665
5666 if (unlikely(rc))
5667 rc = -EIO;
5668 return rc;
5669}
5670
5671/**
5672 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5673 * @phba: Pointer to HBA context object.
5674 * @type: The resource extent type to allocate.
5675 *
5676 * This function allocates the number of elements for the specified
5677 * resource type.
5678 **/
5679static int
5680lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5681{
5682 bool emb = false;
5683 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5684 uint16_t rsrc_id, rsrc_start, j, k;
5685 uint16_t *ids;
5686 int i, rc;
5687 unsigned long longs;
5688 unsigned long *bmask;
5689 struct lpfc_rsrc_blks *rsrc_blks;
5690 LPFC_MBOXQ_t *mbox;
5691 uint32_t length;
5692 struct lpfc_id_range *id_array = NULL;
5693 void *virtaddr = NULL;
5694 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5695 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5696 struct list_head *ext_blk_list;
5697
5698 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5699 &rsrc_cnt,
5700 &rsrc_size);
5701 if (unlikely(rc))
5702 return -EIO;
5703
5704 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5705 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5706 "3009 No available Resource Extents "
5707 "for resource type 0x%x: Count: 0x%x, "
5708 "Size 0x%x\n", type, rsrc_cnt,
5709 rsrc_size);
5710 return -ENOMEM;
5711 }
5712
8a9d2e80
JS
5713 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5714 "2903 Post resource extents type-0x%x: "
5715 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5716
5717 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5718 if (!mbox)
5719 return -ENOMEM;
5720
8a9d2e80 5721 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5722 if (unlikely(rc)) {
5723 rc = -EIO;
5724 goto err_exit;
5725 }
5726
5727 /*
5728 * Figure out where the response is located. Then get local pointers
5729 * to the response data. The port does not guarantee to respond to
5730 * all extents counts request so update the local variable with the
5731 * allocated count from the port.
5732 */
5733 if (emb == LPFC_SLI4_MBX_EMBED) {
5734 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5735 id_array = &rsrc_ext->u.rsp.id[0];
5736 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5737 } else {
5738 virtaddr = mbox->sge_array->addr[0];
5739 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5740 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5741 id_array = &n_rsrc->id;
5742 }
5743
5744 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5745 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5746
5747 /*
5748 * Based on the resource size and count, correct the base and max
5749 * resource values.
5750 */
5751 length = sizeof(struct lpfc_rsrc_blks);
5752 switch (type) {
5753 case LPFC_RSC_TYPE_FCOE_RPI:
5754 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5755 sizeof(unsigned long),
5756 GFP_KERNEL);
5757 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5758 rc = -ENOMEM;
5759 goto err_exit;
5760 }
5761 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5762 sizeof(uint16_t),
5763 GFP_KERNEL);
5764 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5765 kfree(phba->sli4_hba.rpi_bmask);
5766 rc = -ENOMEM;
5767 goto err_exit;
5768 }
5769
5770 /*
5771 * The next_rpi was initialized with the maximum available
5772 * count but the port may allocate a smaller number. Catch
5773 * that case and update the next_rpi.
5774 */
5775 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5776
5777 /* Initialize local ptrs for common extent processing later. */
5778 bmask = phba->sli4_hba.rpi_bmask;
5779 ids = phba->sli4_hba.rpi_ids;
5780 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5781 break;
5782 case LPFC_RSC_TYPE_FCOE_VPI:
5783 phba->vpi_bmask = kzalloc(longs *
5784 sizeof(unsigned long),
5785 GFP_KERNEL);
5786 if (unlikely(!phba->vpi_bmask)) {
5787 rc = -ENOMEM;
5788 goto err_exit;
5789 }
5790 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5791 sizeof(uint16_t),
5792 GFP_KERNEL);
5793 if (unlikely(!phba->vpi_ids)) {
5794 kfree(phba->vpi_bmask);
5795 rc = -ENOMEM;
5796 goto err_exit;
5797 }
5798
5799 /* Initialize local ptrs for common extent processing later. */
5800 bmask = phba->vpi_bmask;
5801 ids = phba->vpi_ids;
5802 ext_blk_list = &phba->lpfc_vpi_blk_list;
5803 break;
5804 case LPFC_RSC_TYPE_FCOE_XRI:
5805 phba->sli4_hba.xri_bmask = kzalloc(longs *
5806 sizeof(unsigned long),
5807 GFP_KERNEL);
5808 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5809 rc = -ENOMEM;
5810 goto err_exit;
5811 }
8a9d2e80 5812 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5813 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5814 sizeof(uint16_t),
5815 GFP_KERNEL);
5816 if (unlikely(!phba->sli4_hba.xri_ids)) {
5817 kfree(phba->sli4_hba.xri_bmask);
5818 rc = -ENOMEM;
5819 goto err_exit;
5820 }
5821
5822 /* Initialize local ptrs for common extent processing later. */
5823 bmask = phba->sli4_hba.xri_bmask;
5824 ids = phba->sli4_hba.xri_ids;
5825 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5826 break;
5827 case LPFC_RSC_TYPE_FCOE_VFI:
5828 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5829 sizeof(unsigned long),
5830 GFP_KERNEL);
5831 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5832 rc = -ENOMEM;
5833 goto err_exit;
5834 }
5835 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5836 sizeof(uint16_t),
5837 GFP_KERNEL);
5838 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5839 kfree(phba->sli4_hba.vfi_bmask);
5840 rc = -ENOMEM;
5841 goto err_exit;
5842 }
5843
5844 /* Initialize local ptrs for common extent processing later. */
5845 bmask = phba->sli4_hba.vfi_bmask;
5846 ids = phba->sli4_hba.vfi_ids;
5847 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5848 break;
5849 default:
5850 /* Unsupported Opcode. Fail call. */
5851 id_array = NULL;
5852 bmask = NULL;
5853 ids = NULL;
5854 ext_blk_list = NULL;
5855 goto err_exit;
5856 }
5857
5858 /*
5859 * Complete initializing the extent configuration with the
5860 * allocated ids assigned to this function. The bitmask serves
5861 * as an index into the array and manages the available ids. The
5862 * array just stores the ids communicated to the port via the wqes.
5863 */
5864 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5865 if ((i % 2) == 0)
5866 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5867 &id_array[k]);
5868 else
5869 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5870 &id_array[k]);
5871
5872 rsrc_blks = kzalloc(length, GFP_KERNEL);
5873 if (unlikely(!rsrc_blks)) {
5874 rc = -ENOMEM;
5875 kfree(bmask);
5876 kfree(ids);
5877 goto err_exit;
5878 }
5879 rsrc_blks->rsrc_start = rsrc_id;
5880 rsrc_blks->rsrc_size = rsrc_size;
5881 list_add_tail(&rsrc_blks->list, ext_blk_list);
5882 rsrc_start = rsrc_id;
895427bd 5883 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5884 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5885 lpfc_sli4_get_iocb_cnt(phba);
5886 phba->sli4_hba.nvme_xri_start =
5887 phba->sli4_hba.scsi_xri_start +
5888 phba->sli4_hba.scsi_xri_max;
5889 }
6d368e53
JS
5890
5891 while (rsrc_id < (rsrc_start + rsrc_size)) {
5892 ids[j] = rsrc_id;
5893 rsrc_id++;
5894 j++;
5895 }
5896 /* Entire word processed. Get next word.*/
5897 if ((i % 2) == 1)
5898 k++;
5899 }
5900 err_exit:
5901 lpfc_sli4_mbox_cmd_free(phba, mbox);
5902 return rc;
5903}
5904
895427bd
JS
5905
5906
6d368e53
JS
5907/**
5908 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5909 * @phba: Pointer to HBA context object.
5910 * @type: the extent's type.
5911 *
5912 * This function deallocates all extents of a particular resource type.
5913 * SLI4 does not allow for deallocating a particular extent range. It
5914 * is the caller's responsibility to release all kernel memory resources.
5915 **/
5916static int
5917lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5918{
5919 int rc;
5920 uint32_t length, mbox_tmo = 0;
5921 LPFC_MBOXQ_t *mbox;
5922 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5923 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5924
5925 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5926 if (!mbox)
5927 return -ENOMEM;
5928
5929 /*
5930 * This function sends an embedded mailbox because it only sends the
5931 * the resource type. All extents of this type are released by the
5932 * port.
5933 */
5934 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5935 sizeof(struct lpfc_sli4_cfg_mhdr));
5936 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5937 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5938 length, LPFC_SLI4_MBX_EMBED);
5939
5940 /* Send an extents count of 0 - the dealloc doesn't use it. */
5941 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5942 LPFC_SLI4_MBX_EMBED);
5943 if (unlikely(rc)) {
5944 rc = -EIO;
5945 goto out_free_mbox;
5946 }
5947 if (!phba->sli4_hba.intr_enable)
5948 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5949 else {
a183a15f 5950 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5951 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5952 }
5953 if (unlikely(rc)) {
5954 rc = -EIO;
5955 goto out_free_mbox;
5956 }
5957
5958 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5959 if (bf_get(lpfc_mbox_hdr_status,
5960 &dealloc_rsrc->header.cfg_shdr.response)) {
5961 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5962 "2919 Failed to release resource extents "
5963 "for type %d - Status 0x%x Add'l Status 0x%x. "
5964 "Resource memory not released.\n",
5965 type,
5966 bf_get(lpfc_mbox_hdr_status,
5967 &dealloc_rsrc->header.cfg_shdr.response),
5968 bf_get(lpfc_mbox_hdr_add_status,
5969 &dealloc_rsrc->header.cfg_shdr.response));
5970 rc = -EIO;
5971 goto out_free_mbox;
5972 }
5973
5974 /* Release kernel memory resources for the specific type. */
5975 switch (type) {
5976 case LPFC_RSC_TYPE_FCOE_VPI:
5977 kfree(phba->vpi_bmask);
5978 kfree(phba->vpi_ids);
5979 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5980 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5981 &phba->lpfc_vpi_blk_list, list) {
5982 list_del_init(&rsrc_blk->list);
5983 kfree(rsrc_blk);
5984 }
16a3a208 5985 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5986 break;
5987 case LPFC_RSC_TYPE_FCOE_XRI:
5988 kfree(phba->sli4_hba.xri_bmask);
5989 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5990 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5991 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5992 list_del_init(&rsrc_blk->list);
5993 kfree(rsrc_blk);
5994 }
5995 break;
5996 case LPFC_RSC_TYPE_FCOE_VFI:
5997 kfree(phba->sli4_hba.vfi_bmask);
5998 kfree(phba->sli4_hba.vfi_ids);
5999 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6000 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6001 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6002 list_del_init(&rsrc_blk->list);
6003 kfree(rsrc_blk);
6004 }
6005 break;
6006 case LPFC_RSC_TYPE_FCOE_RPI:
6007 /* RPI bitmask and physical id array are cleaned up earlier. */
6008 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6009 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6010 list_del_init(&rsrc_blk->list);
6011 kfree(rsrc_blk);
6012 }
6013 break;
6014 default:
6015 break;
6016 }
6017
6018 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6019
6020 out_free_mbox:
6021 mempool_free(mbox, phba->mbox_mem_pool);
6022 return rc;
6023}
6024
bd4b3e5c 6025static void
7bdedb34
JS
6026lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6027 uint32_t feature)
65791f1f 6028{
65791f1f 6029 uint32_t len;
65791f1f 6030
65791f1f
JS
6031 len = sizeof(struct lpfc_mbx_set_feature) -
6032 sizeof(struct lpfc_sli4_cfg_mhdr);
6033 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6034 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6035 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
6036
6037 switch (feature) {
6038 case LPFC_SET_UE_RECOVERY:
6039 bf_set(lpfc_mbx_set_feature_UER,
6040 &mbox->u.mqe.un.set_feature, 1);
6041 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6042 mbox->u.mqe.un.set_feature.param_len = 8;
6043 break;
6044 case LPFC_SET_MDS_DIAGS:
6045 bf_set(lpfc_mbx_set_feature_mds,
6046 &mbox->u.mqe.un.set_feature, 1);
6047 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 6048 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
6049 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6050 mbox->u.mqe.un.set_feature.param_len = 8;
6051 break;
65791f1f 6052 }
7bdedb34
JS
6053
6054 return;
65791f1f
JS
6055}
6056
6d368e53
JS
6057/**
6058 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6059 * @phba: Pointer to HBA context object.
6060 *
6061 * This function allocates all SLI4 resource identifiers.
6062 **/
6063int
6064lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6065{
6066 int i, rc, error = 0;
6067 uint16_t count, base;
6068 unsigned long longs;
6069
ff78d8f9
JS
6070 if (!phba->sli4_hba.rpi_hdrs_in_use)
6071 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
6072 if (phba->sli4_hba.extents_in_use) {
6073 /*
6074 * The port supports resource extents. The XRI, VPI, VFI, RPI
6075 * resource extent count must be read and allocated before
6076 * provisioning the resource id arrays.
6077 */
6078 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6079 LPFC_IDX_RSRC_RDY) {
6080 /*
6081 * Extent-based resources are set - the driver could
6082 * be in a port reset. Figure out if any corrective
6083 * actions need to be taken.
6084 */
6085 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6086 LPFC_RSC_TYPE_FCOE_VFI);
6087 if (rc != 0)
6088 error++;
6089 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6090 LPFC_RSC_TYPE_FCOE_VPI);
6091 if (rc != 0)
6092 error++;
6093 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6094 LPFC_RSC_TYPE_FCOE_XRI);
6095 if (rc != 0)
6096 error++;
6097 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6098 LPFC_RSC_TYPE_FCOE_RPI);
6099 if (rc != 0)
6100 error++;
6101
6102 /*
6103 * It's possible that the number of resources
6104 * provided to this port instance changed between
6105 * resets. Detect this condition and reallocate
6106 * resources. Otherwise, there is no action.
6107 */
6108 if (error) {
6109 lpfc_printf_log(phba, KERN_INFO,
6110 LOG_MBOX | LOG_INIT,
6111 "2931 Detected extent resource "
6112 "change. Reallocating all "
6113 "extents.\n");
6114 rc = lpfc_sli4_dealloc_extent(phba,
6115 LPFC_RSC_TYPE_FCOE_VFI);
6116 rc = lpfc_sli4_dealloc_extent(phba,
6117 LPFC_RSC_TYPE_FCOE_VPI);
6118 rc = lpfc_sli4_dealloc_extent(phba,
6119 LPFC_RSC_TYPE_FCOE_XRI);
6120 rc = lpfc_sli4_dealloc_extent(phba,
6121 LPFC_RSC_TYPE_FCOE_RPI);
6122 } else
6123 return 0;
6124 }
6125
6126 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6127 if (unlikely(rc))
6128 goto err_exit;
6129
6130 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6131 if (unlikely(rc))
6132 goto err_exit;
6133
6134 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6135 if (unlikely(rc))
6136 goto err_exit;
6137
6138 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6139 if (unlikely(rc))
6140 goto err_exit;
6141 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6142 LPFC_IDX_RSRC_RDY);
6143 return rc;
6144 } else {
6145 /*
6146 * The port does not support resource extents. The XRI, VPI,
6147 * VFI, RPI resource ids were determined from READ_CONFIG.
6148 * Just allocate the bitmasks and provision the resource id
6149 * arrays. If a port reset is active, the resources don't
6150 * need any action - just exit.
6151 */
6152 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6153 LPFC_IDX_RSRC_RDY) {
6154 lpfc_sli4_dealloc_resource_identifiers(phba);
6155 lpfc_sli4_remove_rpis(phba);
6156 }
6d368e53
JS
6157 /* RPIs. */
6158 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6159 if (count <= 0) {
6160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6161 "3279 Invalid provisioning of "
6162 "rpi:%d\n", count);
6163 rc = -EINVAL;
6164 goto err_exit;
6165 }
6d368e53
JS
6166 base = phba->sli4_hba.max_cfg_param.rpi_base;
6167 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6168 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6169 sizeof(unsigned long),
6170 GFP_KERNEL);
6171 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6172 rc = -ENOMEM;
6173 goto err_exit;
6174 }
6175 phba->sli4_hba.rpi_ids = kzalloc(count *
6176 sizeof(uint16_t),
6177 GFP_KERNEL);
6178 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6179 rc = -ENOMEM;
6180 goto free_rpi_bmask;
6181 }
6182
6183 for (i = 0; i < count; i++)
6184 phba->sli4_hba.rpi_ids[i] = base + i;
6185
6186 /* VPIs. */
6187 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6188 if (count <= 0) {
6189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6190 "3280 Invalid provisioning of "
6191 "vpi:%d\n", count);
6192 rc = -EINVAL;
6193 goto free_rpi_ids;
6194 }
6d368e53
JS
6195 base = phba->sli4_hba.max_cfg_param.vpi_base;
6196 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6197 phba->vpi_bmask = kzalloc(longs *
6198 sizeof(unsigned long),
6199 GFP_KERNEL);
6200 if (unlikely(!phba->vpi_bmask)) {
6201 rc = -ENOMEM;
6202 goto free_rpi_ids;
6203 }
6204 phba->vpi_ids = kzalloc(count *
6205 sizeof(uint16_t),
6206 GFP_KERNEL);
6207 if (unlikely(!phba->vpi_ids)) {
6208 rc = -ENOMEM;
6209 goto free_vpi_bmask;
6210 }
6211
6212 for (i = 0; i < count; i++)
6213 phba->vpi_ids[i] = base + i;
6214
6215 /* XRIs. */
6216 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6217 if (count <= 0) {
6218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6219 "3281 Invalid provisioning of "
6220 "xri:%d\n", count);
6221 rc = -EINVAL;
6222 goto free_vpi_ids;
6223 }
6d368e53
JS
6224 base = phba->sli4_hba.max_cfg_param.xri_base;
6225 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6226 phba->sli4_hba.xri_bmask = kzalloc(longs *
6227 sizeof(unsigned long),
6228 GFP_KERNEL);
6229 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6230 rc = -ENOMEM;
6231 goto free_vpi_ids;
6232 }
41899be7 6233 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6234 phba->sli4_hba.xri_ids = kzalloc(count *
6235 sizeof(uint16_t),
6236 GFP_KERNEL);
6237 if (unlikely(!phba->sli4_hba.xri_ids)) {
6238 rc = -ENOMEM;
6239 goto free_xri_bmask;
6240 }
6241
6242 for (i = 0; i < count; i++)
6243 phba->sli4_hba.xri_ids[i] = base + i;
6244
6245 /* VFIs. */
6246 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6247 if (count <= 0) {
6248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6249 "3282 Invalid provisioning of "
6250 "vfi:%d\n", count);
6251 rc = -EINVAL;
6252 goto free_xri_ids;
6253 }
6d368e53
JS
6254 base = phba->sli4_hba.max_cfg_param.vfi_base;
6255 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6256 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6257 sizeof(unsigned long),
6258 GFP_KERNEL);
6259 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6260 rc = -ENOMEM;
6261 goto free_xri_ids;
6262 }
6263 phba->sli4_hba.vfi_ids = kzalloc(count *
6264 sizeof(uint16_t),
6265 GFP_KERNEL);
6266 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6267 rc = -ENOMEM;
6268 goto free_vfi_bmask;
6269 }
6270
6271 for (i = 0; i < count; i++)
6272 phba->sli4_hba.vfi_ids[i] = base + i;
6273
6274 /*
6275 * Mark all resources ready. An HBA reset doesn't need
6276 * to reset the initialization.
6277 */
6278 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6279 LPFC_IDX_RSRC_RDY);
6280 return 0;
6281 }
6282
6283 free_vfi_bmask:
6284 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6285 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6286 free_xri_ids:
6287 kfree(phba->sli4_hba.xri_ids);
cd60be49 6288 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6289 free_xri_bmask:
6290 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6291 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6292 free_vpi_ids:
6293 kfree(phba->vpi_ids);
cd60be49 6294 phba->vpi_ids = NULL;
6d368e53
JS
6295 free_vpi_bmask:
6296 kfree(phba->vpi_bmask);
cd60be49 6297 phba->vpi_bmask = NULL;
6d368e53
JS
6298 free_rpi_ids:
6299 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6300 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6301 free_rpi_bmask:
6302 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6303 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6304 err_exit:
6305 return rc;
6306}
6307
6308/**
6309 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6310 * @phba: Pointer to HBA context object.
6311 *
6312 * This function allocates the number of elements for the specified
6313 * resource type.
6314 **/
6315int
6316lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6317{
6318 if (phba->sli4_hba.extents_in_use) {
6319 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6320 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6321 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6322 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6323 } else {
6324 kfree(phba->vpi_bmask);
16a3a208 6325 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6326 kfree(phba->vpi_ids);
6327 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6328 kfree(phba->sli4_hba.xri_bmask);
6329 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6330 kfree(phba->sli4_hba.vfi_bmask);
6331 kfree(phba->sli4_hba.vfi_ids);
6332 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6333 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6334 }
6335
6336 return 0;
6337}
6338
b76f2dc9
JS
6339/**
6340 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6341 * @phba: Pointer to HBA context object.
6342 * @type: The resource extent type.
6343 * @extnt_count: buffer to hold port extent count response
6344 * @extnt_size: buffer to hold port extent size response.
6345 *
6346 * This function calls the port to read the host allocated extents
6347 * for a particular type.
6348 **/
6349int
6350lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6351 uint16_t *extnt_cnt, uint16_t *extnt_size)
6352{
6353 bool emb;
6354 int rc = 0;
6355 uint16_t curr_blks = 0;
6356 uint32_t req_len, emb_len;
6357 uint32_t alloc_len, mbox_tmo;
6358 struct list_head *blk_list_head;
6359 struct lpfc_rsrc_blks *rsrc_blk;
6360 LPFC_MBOXQ_t *mbox;
6361 void *virtaddr = NULL;
6362 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6363 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6364 union lpfc_sli4_cfg_shdr *shdr;
6365
6366 switch (type) {
6367 case LPFC_RSC_TYPE_FCOE_VPI:
6368 blk_list_head = &phba->lpfc_vpi_blk_list;
6369 break;
6370 case LPFC_RSC_TYPE_FCOE_XRI:
6371 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6372 break;
6373 case LPFC_RSC_TYPE_FCOE_VFI:
6374 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6375 break;
6376 case LPFC_RSC_TYPE_FCOE_RPI:
6377 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6378 break;
6379 default:
6380 return -EIO;
6381 }
6382
6383 /* Count the number of extents currently allocatd for this type. */
6384 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6385 if (curr_blks == 0) {
6386 /*
6387 * The GET_ALLOCATED mailbox does not return the size,
6388 * just the count. The size should be just the size
6389 * stored in the current allocated block and all sizes
6390 * for an extent type are the same so set the return
6391 * value now.
6392 */
6393 *extnt_size = rsrc_blk->rsrc_size;
6394 }
6395 curr_blks++;
6396 }
6397
b76f2dc9
JS
6398 /*
6399 * Calculate the size of an embedded mailbox. The uint32_t
6400 * accounts for extents-specific word.
6401 */
6402 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6403 sizeof(uint32_t);
6404
6405 /*
6406 * Presume the allocation and response will fit into an embedded
6407 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6408 */
6409 emb = LPFC_SLI4_MBX_EMBED;
6410 req_len = emb_len;
6411 if (req_len > emb_len) {
6412 req_len = curr_blks * sizeof(uint16_t) +
6413 sizeof(union lpfc_sli4_cfg_shdr) +
6414 sizeof(uint32_t);
6415 emb = LPFC_SLI4_MBX_NEMBED;
6416 }
6417
6418 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6419 if (!mbox)
6420 return -ENOMEM;
6421 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6422
6423 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6424 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6425 req_len, emb);
6426 if (alloc_len < req_len) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6428 "2983 Allocated DMA memory size (x%x) is "
6429 "less than the requested DMA memory "
6430 "size (x%x)\n", alloc_len, req_len);
6431 rc = -ENOMEM;
6432 goto err_exit;
6433 }
6434 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6435 if (unlikely(rc)) {
6436 rc = -EIO;
6437 goto err_exit;
6438 }
6439
6440 if (!phba->sli4_hba.intr_enable)
6441 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6442 else {
a183a15f 6443 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6444 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6445 }
6446
6447 if (unlikely(rc)) {
6448 rc = -EIO;
6449 goto err_exit;
6450 }
6451
6452 /*
6453 * Figure out where the response is located. Then get local pointers
6454 * to the response data. The port does not guarantee to respond to
6455 * all extents counts request so update the local variable with the
6456 * allocated count from the port.
6457 */
6458 if (emb == LPFC_SLI4_MBX_EMBED) {
6459 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6460 shdr = &rsrc_ext->header.cfg_shdr;
6461 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6462 } else {
6463 virtaddr = mbox->sge_array->addr[0];
6464 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6465 shdr = &n_rsrc->cfg_shdr;
6466 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6467 }
6468
6469 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6470 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6471 "2984 Failed to read allocated resources "
6472 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6473 type,
6474 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6475 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6476 rc = -EIO;
6477 goto err_exit;
6478 }
6479 err_exit:
6480 lpfc_sli4_mbox_cmd_free(phba, mbox);
6481 return rc;
6482}
6483
8a9d2e80 6484/**
0ef69968 6485 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6486 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6487 * @pring: Pointer to driver SLI ring object.
6488 * @sgl_list: linked link of sgl buffers to post
6489 * @cnt: number of linked list buffers
8a9d2e80 6490 *
895427bd 6491 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6492 * repost them to the port by using SGL block post. This is needed after a
6493 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6494 * of buffer sgls which contains contiguous xris and uses the non-embedded
6495 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6496 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6497 * mailbox command for posting.
6498 *
6499 * Returns: 0 = success, non-zero failure.
6500 **/
6501static int
895427bd
JS
6502lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6503 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6504{
6505 struct lpfc_sglq *sglq_entry = NULL;
6506 struct lpfc_sglq *sglq_entry_next = NULL;
6507 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6508 int status, total_cnt;
6509 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6510 int last_xritag = NO_XRI;
6511 LIST_HEAD(prep_sgl_list);
6512 LIST_HEAD(blck_sgl_list);
6513 LIST_HEAD(allc_sgl_list);
6514 LIST_HEAD(post_sgl_list);
6515 LIST_HEAD(free_sgl_list);
6516
38c20673 6517 spin_lock_irq(&phba->hbalock);
895427bd
JS
6518 spin_lock(&phba->sli4_hba.sgl_list_lock);
6519 list_splice_init(sgl_list, &allc_sgl_list);
6520 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6521 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6522
895427bd 6523 total_cnt = cnt;
8a9d2e80
JS
6524 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6525 &allc_sgl_list, list) {
6526 list_del_init(&sglq_entry->list);
6527 block_cnt++;
6528 if ((last_xritag != NO_XRI) &&
6529 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6530 /* a hole in xri block, form a sgl posting block */
6531 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6532 post_cnt = block_cnt - 1;
6533 /* prepare list for next posting block */
6534 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6535 block_cnt = 1;
6536 } else {
6537 /* prepare list for next posting block */
6538 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6539 /* enough sgls for non-embed sgl mbox command */
6540 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6541 list_splice_init(&prep_sgl_list,
6542 &blck_sgl_list);
6543 post_cnt = block_cnt;
6544 block_cnt = 0;
6545 }
6546 }
6547 num_posted++;
6548
6549 /* keep track of last sgl's xritag */
6550 last_xritag = sglq_entry->sli4_xritag;
6551
895427bd
JS
6552 /* end of repost sgl list condition for buffers */
6553 if (num_posted == total_cnt) {
8a9d2e80
JS
6554 if (post_cnt == 0) {
6555 list_splice_init(&prep_sgl_list,
6556 &blck_sgl_list);
6557 post_cnt = block_cnt;
6558 } else if (block_cnt == 1) {
6559 status = lpfc_sli4_post_sgl(phba,
6560 sglq_entry->phys, 0,
6561 sglq_entry->sli4_xritag);
6562 if (!status) {
6563 /* successful, put sgl to posted list */
6564 list_add_tail(&sglq_entry->list,
6565 &post_sgl_list);
6566 } else {
6567 /* Failure, put sgl to free list */
6568 lpfc_printf_log(phba, KERN_WARNING,
6569 LOG_SLI,
895427bd 6570 "3159 Failed to post "
8a9d2e80
JS
6571 "sgl, xritag:x%x\n",
6572 sglq_entry->sli4_xritag);
6573 list_add_tail(&sglq_entry->list,
6574 &free_sgl_list);
711ea882 6575 total_cnt--;
8a9d2e80
JS
6576 }
6577 }
6578 }
6579
6580 /* continue until a nembed page worth of sgls */
6581 if (post_cnt == 0)
6582 continue;
6583
895427bd
JS
6584 /* post the buffer list sgls as a block */
6585 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6586 post_cnt);
8a9d2e80
JS
6587
6588 if (!status) {
6589 /* success, put sgl list to posted sgl list */
6590 list_splice_init(&blck_sgl_list, &post_sgl_list);
6591 } else {
6592 /* Failure, put sgl list to free sgl list */
6593 sglq_entry_first = list_first_entry(&blck_sgl_list,
6594 struct lpfc_sglq,
6595 list);
6596 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6597 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6598 "xritag:x%x-x%x\n",
6599 sglq_entry_first->sli4_xritag,
6600 (sglq_entry_first->sli4_xritag +
6601 post_cnt - 1));
6602 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6603 total_cnt -= post_cnt;
8a9d2e80
JS
6604 }
6605
6606 /* don't reset xirtag due to hole in xri block */
6607 if (block_cnt == 0)
6608 last_xritag = NO_XRI;
6609
895427bd 6610 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6611 post_cnt = 0;
6612 }
6613
895427bd 6614 /* free the sgls failed to post */
8a9d2e80
JS
6615 lpfc_free_sgl_list(phba, &free_sgl_list);
6616
895427bd 6617 /* push sgls posted to the available list */
8a9d2e80 6618 if (!list_empty(&post_sgl_list)) {
38c20673 6619 spin_lock_irq(&phba->hbalock);
895427bd
JS
6620 spin_lock(&phba->sli4_hba.sgl_list_lock);
6621 list_splice_init(&post_sgl_list, sgl_list);
6622 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6623 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6624 } else {
6625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6626 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6627 return -EIO;
6628 }
895427bd
JS
6629
6630 /* return the number of XRIs actually posted */
6631 return total_cnt;
8a9d2e80
JS
6632}
6633
61bda8f7
JS
6634void
6635lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6636{
6637 uint32_t len;
6638
6639 len = sizeof(struct lpfc_mbx_set_host_data) -
6640 sizeof(struct lpfc_sli4_cfg_mhdr);
6641 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6642 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6643 LPFC_SLI4_MBX_EMBED);
6644
6645 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6646 mbox->u.mqe.un.set_host_data.param_len =
6647 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6648 snprintf(mbox->u.mqe.un.set_host_data.data,
6649 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6650 "Linux %s v"LPFC_DRIVER_VERSION,
6651 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6652}
6653
a8cf5dfe 6654int
6c621a22 6655lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 6656 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
6657{
6658 int rc, i;
6659 struct lpfc_rqe hrqe;
6660 struct lpfc_rqe drqe;
6661 struct lpfc_rqb *rqbp;
411de511 6662 unsigned long flags;
6c621a22
JS
6663 struct rqb_dmabuf *rqb_buffer;
6664 LIST_HEAD(rqb_buf_list);
6665
411de511 6666 spin_lock_irqsave(&phba->hbalock, flags);
6c621a22
JS
6667 rqbp = hrq->rqbp;
6668 for (i = 0; i < count; i++) {
6669 /* IF RQ is already full, don't bother */
6670 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6671 break;
6672 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6673 if (!rqb_buffer)
6674 break;
6675 rqb_buffer->hrq = hrq;
6676 rqb_buffer->drq = drq;
a8cf5dfe 6677 rqb_buffer->idx = idx;
6c621a22
JS
6678 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6679 }
6680 while (!list_empty(&rqb_buf_list)) {
6681 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6682 hbuf.list);
6683
6684 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6685 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6686 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6687 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6688 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6689 if (rc < 0) {
411de511
JS
6690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6691 "6421 Cannot post to HRQ %d: %x %x %x "
6692 "DRQ %x %x\n",
6693 hrq->queue_id,
6694 hrq->host_index,
6695 hrq->hba_index,
6696 hrq->entry_count,
6697 drq->host_index,
6698 drq->hba_index);
6c621a22
JS
6699 rqbp->rqb_free_buffer(phba, rqb_buffer);
6700 } else {
6701 list_add_tail(&rqb_buffer->hbuf.list,
6702 &rqbp->rqb_buffer_list);
6703 rqbp->buffer_count++;
6704 }
6705 }
411de511 6706 spin_unlock_irqrestore(&phba->hbalock, flags);
6c621a22
JS
6707 return 1;
6708}
6709
da0436e9 6710/**
183b8021 6711 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
6712 * @phba: Pointer to HBA context object.
6713 *
183b8021
MY
6714 * This function is the main SLI4 device initialization PCI function. This
6715 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
6716 * HBA error attention handler code. Caller is not required to hold any
6717 * locks.
6718 **/
6719int
6720lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6721{
6c621a22 6722 int rc, i, cnt;
da0436e9
JS
6723 LPFC_MBOXQ_t *mboxq;
6724 struct lpfc_mqe *mqe;
6725 uint8_t *vpd;
6726 uint32_t vpd_size;
6727 uint32_t ftr_rsp = 0;
6728 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6729 struct lpfc_vport *vport = phba->pport;
6730 struct lpfc_dmabuf *mp;
2d7dbc4c 6731 struct lpfc_rqb *rqbp;
da0436e9
JS
6732
6733 /* Perform a PCI function reset to start from clean */
6734 rc = lpfc_pci_function_reset(phba);
6735 if (unlikely(rc))
6736 return -ENODEV;
6737
6738 /* Check the HBA Host Status Register for readyness */
6739 rc = lpfc_sli4_post_status_check(phba);
6740 if (unlikely(rc))
6741 return -ENODEV;
6742 else {
6743 spin_lock_irq(&phba->hbalock);
6744 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6745 spin_unlock_irq(&phba->hbalock);
6746 }
6747
6748 /*
6749 * Allocate a single mailbox container for initializing the
6750 * port.
6751 */
6752 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6753 if (!mboxq)
6754 return -ENOMEM;
6755
da0436e9 6756 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6757 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6758 vpd = kzalloc(vpd_size, GFP_KERNEL);
6759 if (!vpd) {
6760 rc = -ENOMEM;
6761 goto out_free_mbox;
6762 }
6763
6764 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6765 if (unlikely(rc)) {
6766 kfree(vpd);
6767 goto out_free_mbox;
6768 }
572709e2 6769
da0436e9 6770 mqe = &mboxq->u.mqe;
f1126688 6771 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6772 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6773 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6774 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6775 } else {
76a95d75 6776 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6777 }
45ed1190
JS
6778
6779 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6780 LPFC_DCBX_CEE_MODE)
6781 phba->hba_flag |= HBA_FIP_SUPPORT;
6782 else
6783 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6784
4f2e66c6
JS
6785 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6786
c31098ce 6787 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6789 "0376 READ_REV Error. SLI Level %d "
6790 "FCoE enabled %d\n",
76a95d75 6791 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6792 rc = -EIO;
76a95d75
JS
6793 kfree(vpd);
6794 goto out_free_mbox;
da0436e9 6795 }
cd1c8301 6796
ff78d8f9
JS
6797 /*
6798 * Continue initialization with default values even if driver failed
6799 * to read FCoE param config regions, only read parameters if the
6800 * board is FCoE
6801 */
6802 if (phba->hba_flag & HBA_FCOE_MODE &&
6803 lpfc_sli4_read_fcoe_params(phba))
6804 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6805 "2570 Failed to read FCoE parameters\n");
6806
cd1c8301
JS
6807 /*
6808 * Retrieve sli4 device physical port name, failure of doing it
6809 * is considered as non-fatal.
6810 */
6811 rc = lpfc_sli4_retrieve_pport_name(phba);
6812 if (!rc)
6813 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6814 "3080 Successful retrieving SLI4 device "
6815 "physical port name: %s.\n", phba->Port);
6816
da0436e9
JS
6817 /*
6818 * Evaluate the read rev and vpd data. Populate the driver
6819 * state with the results. If this routine fails, the failure
6820 * is not fatal as the driver will use generic values.
6821 */
6822 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6823 if (unlikely(!rc)) {
6824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6825 "0377 Error %d parsing vpd. "
6826 "Using defaults.\n", rc);
6827 rc = 0;
6828 }
76a95d75 6829 kfree(vpd);
da0436e9 6830
f1126688
JS
6831 /* Save information as VPD data */
6832 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6833 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6834 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6835 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6836 &mqe->un.read_rev);
6837 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6838 &mqe->un.read_rev);
6839 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6840 &mqe->un.read_rev);
6841 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6842 &mqe->un.read_rev);
6843 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6844 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6845 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6846 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6847 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6848 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6849 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6850 "(%d):0380 READ_REV Status x%x "
6851 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6852 mboxq->vport ? mboxq->vport->vpi : 0,
6853 bf_get(lpfc_mqe_status, mqe),
6854 phba->vpd.rev.opFwName,
6855 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6856 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6857
572709e2
JS
6858 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6859 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6860 if (phba->pport->cfg_lun_queue_depth > rc) {
6861 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6862 "3362 LUN queue depth changed from %d to %d\n",
6863 phba->pport->cfg_lun_queue_depth, rc);
6864 phba->pport->cfg_lun_queue_depth = rc;
6865 }
6866
65791f1f 6867 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6868 LPFC_SLI_INTF_IF_TYPE_0) {
6869 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6870 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6871 if (rc == MBX_SUCCESS) {
6872 phba->hba_flag |= HBA_RECOVERABLE_UE;
6873 /* Set 1Sec interval to detect UE */
6874 phba->eratt_poll_interval = 1;
6875 phba->sli4_hba.ue_to_sr = bf_get(
6876 lpfc_mbx_set_feature_UESR,
6877 &mboxq->u.mqe.un.set_feature);
6878 phba->sli4_hba.ue_to_rp = bf_get(
6879 lpfc_mbx_set_feature_UERP,
6880 &mboxq->u.mqe.un.set_feature);
6881 }
6882 }
6883
6884 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6885 /* Enable MDS Diagnostics only if the SLI Port supports it */
6886 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6887 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6888 if (rc != MBX_SUCCESS)
6889 phba->mds_diags_support = 0;
6890 }
572709e2 6891
da0436e9
JS
6892 /*
6893 * Discover the port's supported feature set and match it against the
6894 * hosts requests.
6895 */
6896 lpfc_request_features(phba, mboxq);
6897 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6898 if (unlikely(rc)) {
6899 rc = -EIO;
76a95d75 6900 goto out_free_mbox;
da0436e9
JS
6901 }
6902
6903 /*
6904 * The port must support FCP initiator mode as this is the
6905 * only mode running in the host.
6906 */
6907 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6908 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6909 "0378 No support for fcpi mode.\n");
6910 ftr_rsp++;
6911 }
fedd3b7b
JS
6912 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6913 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6914 else
6915 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6916 /*
6917 * If the port cannot support the host's requested features
6918 * then turn off the global config parameters to disable the
6919 * feature in the driver. This is not a fatal error.
6920 */
bf08611b
JS
6921 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6922 if (phba->cfg_enable_bg) {
6923 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6924 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6925 else
6926 ftr_rsp++;
6927 }
da0436e9
JS
6928
6929 if (phba->max_vpi && phba->cfg_enable_npiv &&
6930 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6931 ftr_rsp++;
6932
6933 if (ftr_rsp) {
6934 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6935 "0379 Feature Mismatch Data: x%08x %08x "
6936 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6937 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6938 phba->cfg_enable_npiv, phba->max_vpi);
6939 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6940 phba->cfg_enable_bg = 0;
6941 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6942 phba->cfg_enable_npiv = 0;
6943 }
6944
6945 /* These SLI3 features are assumed in SLI4 */
6946 spin_lock_irq(&phba->hbalock);
6947 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6948 spin_unlock_irq(&phba->hbalock);
6949
6d368e53
JS
6950 /*
6951 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6952 * calls depends on these resources to complete port setup.
6953 */
6954 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6955 if (rc) {
6956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6957 "2920 Failed to alloc Resource IDs "
6958 "rc = x%x\n", rc);
6959 goto out_free_mbox;
6960 }
6961
61bda8f7
JS
6962 lpfc_set_host_data(phba, mboxq);
6963
6964 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6965 if (rc) {
6966 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6967 "2134 Failed to set host os driver version %x",
6968 rc);
6969 }
6970
da0436e9 6971 /* Read the port's service parameters. */
9f1177a3
JS
6972 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6973 if (rc) {
6974 phba->link_state = LPFC_HBA_ERROR;
6975 rc = -ENOMEM;
76a95d75 6976 goto out_free_mbox;
9f1177a3
JS
6977 }
6978
da0436e9
JS
6979 mboxq->vport = vport;
6980 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6981 mp = (struct lpfc_dmabuf *) mboxq->context1;
6982 if (rc == MBX_SUCCESS) {
6983 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6984 rc = 0;
6985 }
6986
6987 /*
6988 * This memory was allocated by the lpfc_read_sparam routine. Release
6989 * it to the mbuf pool.
6990 */
6991 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6992 kfree(mp);
6993 mboxq->context1 = NULL;
6994 if (unlikely(rc)) {
6995 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6996 "0382 READ_SPARAM command failed "
6997 "status %d, mbxStatus x%x\n",
6998 rc, bf_get(lpfc_mqe_status, mqe));
6999 phba->link_state = LPFC_HBA_ERROR;
7000 rc = -EIO;
76a95d75 7001 goto out_free_mbox;
da0436e9
JS
7002 }
7003
0558056c 7004 lpfc_update_vport_wwn(vport);
da0436e9
JS
7005
7006 /* Update the fc_host data structures with new wwn. */
7007 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7008 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7009
895427bd
JS
7010 /* Create all the SLI4 queues */
7011 rc = lpfc_sli4_queue_create(phba);
7012 if (rc) {
7013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7014 "3089 Failed to allocate queues\n");
7015 rc = -ENODEV;
7016 goto out_free_mbox;
7017 }
7018 /* Set up all the queues to the device */
7019 rc = lpfc_sli4_queue_setup(phba);
7020 if (unlikely(rc)) {
7021 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7022 "0381 Error %d during queue setup.\n ", rc);
7023 goto out_stop_timers;
7024 }
7025 /* Initialize the driver internal SLI layer lists. */
7026 lpfc_sli4_setup(phba);
7027 lpfc_sli4_queue_init(phba);
7028
7029 /* update host els xri-sgl sizes and mappings */
7030 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
7031 if (unlikely(rc)) {
7032 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7033 "1400 Failed to update xri-sgl size and "
7034 "mapping: %d\n", rc);
895427bd 7035 goto out_destroy_queue;
da0436e9
JS
7036 }
7037
8a9d2e80 7038 /* register the els sgl pool to the port */
895427bd
JS
7039 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7040 phba->sli4_hba.els_xri_cnt);
7041 if (unlikely(rc < 0)) {
8a9d2e80
JS
7042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7043 "0582 Error %d during els sgl post "
7044 "operation\n", rc);
7045 rc = -ENODEV;
895427bd 7046 goto out_destroy_queue;
8a9d2e80 7047 }
895427bd 7048 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 7049
f358dd0c
JS
7050 if (phba->nvmet_support) {
7051 /* update host nvmet xri-sgl sizes and mappings */
7052 rc = lpfc_sli4_nvmet_sgl_update(phba);
7053 if (unlikely(rc)) {
7054 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7055 "6308 Failed to update nvmet-sgl size "
7056 "and mapping: %d\n", rc);
7057 goto out_destroy_queue;
7058 }
7059
7060 /* register the nvmet sgl pool to the port */
7061 rc = lpfc_sli4_repost_sgl_list(
7062 phba,
7063 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7064 phba->sli4_hba.nvmet_xri_cnt);
7065 if (unlikely(rc < 0)) {
7066 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7067 "3117 Error %d during nvmet "
7068 "sgl post\n", rc);
7069 rc = -ENODEV;
7070 goto out_destroy_queue;
7071 }
7072 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
7073
7074 cnt = phba->cfg_iocb_cnt * 1024;
7075 /* We need 1 iocbq for every SGL, for IO processing */
7076 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 7077 } else {
895427bd
JS
7078 /* update host scsi xri-sgl sizes and mappings */
7079 rc = lpfc_sli4_scsi_sgl_update(phba);
7080 if (unlikely(rc)) {
7081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7082 "6309 Failed to update scsi-sgl size "
7083 "and mapping: %d\n", rc);
7084 goto out_destroy_queue;
7085 }
7086
7087 /* update host nvme xri-sgl sizes and mappings */
7088 rc = lpfc_sli4_nvme_sgl_update(phba);
7089 if (unlikely(rc)) {
7090 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7091 "6082 Failed to update nvme-sgl size "
7092 "and mapping: %d\n", rc);
7093 goto out_destroy_queue;
7094 }
6c621a22
JS
7095
7096 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
7097 }
7098
7099 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
7100 /* Initialize and populate the iocb list per host */
7101 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 7102 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
7103 phba->cfg_iocb_cnt, cnt);
7104 rc = lpfc_init_iocb_list(phba, cnt);
7105 if (rc) {
7106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 7107 "1413 Failed to init iocb list.\n");
6c621a22
JS
7108 goto out_destroy_queue;
7109 }
895427bd
JS
7110 }
7111
11e644e2
JS
7112 if (phba->nvmet_support)
7113 lpfc_nvmet_create_targetport(phba);
7114
2d7dbc4c 7115 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
7116 /* Post initial buffers to all RQs created */
7117 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7118 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7119 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7120 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7121 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 7122 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
7123 rqbp->buffer_count = 0;
7124
2d7dbc4c
JS
7125 lpfc_post_rq_buffer(
7126 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7127 phba->sli4_hba.nvmet_mrq_data[i],
a8cf5dfe 7128 LPFC_NVMET_RQE_DEF_COUNT, i);
2d7dbc4c
JS
7129 }
7130 }
7131
895427bd
JS
7132 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7133 /* register the allocated scsi sgl pool to the port */
7134 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7135 if (unlikely(rc)) {
7136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7137 "0383 Error %d during scsi sgl post "
7138 "operation\n", rc);
7139 /* Some Scsi buffers were moved to abort scsi list */
7140 /* A pci function reset will repost them */
7141 rc = -ENODEV;
7142 goto out_destroy_queue;
7143 }
da0436e9
JS
7144 }
7145
01649561
JS
7146 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7147 (phba->nvmet_support == 0)) {
7148
7149 /* register the allocated nvme sgl pool to the port */
7150 rc = lpfc_repost_nvme_sgl_list(phba);
7151 if (unlikely(rc)) {
7152 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7153 "6116 Error %d during nvme sgl post "
7154 "operation\n", rc);
7155 /* Some NVME buffers were moved to abort nvme list */
7156 /* A pci function reset will repost them */
7157 rc = -ENODEV;
7158 goto out_destroy_queue;
7159 }
da0436e9
JS
7160 }
7161
7162 /* Post the rpi header region to the device. */
7163 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7164 if (unlikely(rc)) {
7165 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7166 "0393 Error %d during rpi post operation\n",
7167 rc);
7168 rc = -ENODEV;
895427bd 7169 goto out_destroy_queue;
da0436e9 7170 }
97f2ecf1 7171 lpfc_sli4_node_prep(phba);
da0436e9 7172
895427bd 7173 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7174 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7175 /*
7176 * The FC Port needs to register FCFI (index 0)
7177 */
7178 lpfc_reg_fcfi(phba, mboxq);
7179 mboxq->vport = phba->pport;
7180 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7181 if (rc != MBX_SUCCESS)
7182 goto out_unset_queue;
7183 rc = 0;
7184 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7185 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7186 } else {
7187 /* We are a NVME Target mode with MRQ > 1 */
7188
7189 /* First register the FCFI */
7190 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7191 mboxq->vport = phba->pport;
7192 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7193 if (rc != MBX_SUCCESS)
7194 goto out_unset_queue;
7195 rc = 0;
7196 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7197 &mboxq->u.mqe.un.reg_fcfi_mrq);
7198
7199 /* Next register the MRQs */
7200 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7201 mboxq->vport = phba->pport;
7202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7203 if (rc != MBX_SUCCESS)
7204 goto out_unset_queue;
7205 rc = 0;
895427bd
JS
7206 }
7207 /* Check if the port is configured to be disabled */
7208 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7209 }
7210
7211 /* Arm the CQs and then EQs on device */
7212 lpfc_sli4_arm_cqeq_intr(phba);
7213
7214 /* Indicate device interrupt mode */
7215 phba->sli4_hba.intr_enable = 1;
7216
7217 /* Allow asynchronous mailbox command to go through */
7218 spin_lock_irq(&phba->hbalock);
7219 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7220 spin_unlock_irq(&phba->hbalock);
7221
7222 /* Post receive buffers to the device */
7223 lpfc_sli4_rb_setup(phba);
7224
fc2b989b
JS
7225 /* Reset HBA FCF states after HBA reset */
7226 phba->fcf.fcf_flag = 0;
7227 phba->fcf.current_rec.flag = 0;
7228
da0436e9 7229 /* Start the ELS watchdog timer */
8fa38513 7230 mod_timer(&vport->els_tmofunc,
256ec0d0 7231 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7232
7233 /* Start heart beat timer */
7234 mod_timer(&phba->hb_tmofunc,
256ec0d0 7235 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7236 phba->hb_outstanding = 0;
7237 phba->last_completion_time = jiffies;
7238
7239 /* Start error attention (ERATT) polling timer */
256ec0d0 7240 mod_timer(&phba->eratt_poll,
65791f1f 7241 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7242
75baf696
JS
7243 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7244 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7245 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7246 if (!rc) {
7247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7248 "2829 This device supports "
7249 "Advanced Error Reporting (AER)\n");
7250 spin_lock_irq(&phba->hbalock);
7251 phba->hba_flag |= HBA_AER_ENABLED;
7252 spin_unlock_irq(&phba->hbalock);
7253 } else {
7254 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7255 "2830 This device does not support "
7256 "Advanced Error Reporting (AER)\n");
7257 phba->cfg_aer_support = 0;
7258 }
0a96e975 7259 rc = 0;
75baf696
JS
7260 }
7261
da0436e9
JS
7262 /*
7263 * The port is ready, set the host's link state to LINK_DOWN
7264 * in preparation for link interrupts.
7265 */
da0436e9
JS
7266 spin_lock_irq(&phba->hbalock);
7267 phba->link_state = LPFC_LINK_DOWN;
7268 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7269 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7270 (phba->hba_flag & LINK_DISABLED)) {
7271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7272 "3103 Adapter Link is disabled.\n");
7273 lpfc_down_link(phba, mboxq);
7274 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7275 if (rc != MBX_SUCCESS) {
7276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7277 "3104 Adapter failed to issue "
7278 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7279 goto out_unset_queue;
7280 }
7281 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7282 /* don't perform init_link on SLI4 FC port loopback test */
7283 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7284 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7285 if (rc)
7286 goto out_unset_queue;
7287 }
5350d872
JS
7288 }
7289 mempool_free(mboxq, phba->mbox_mem_pool);
7290 return rc;
76a95d75 7291out_unset_queue:
da0436e9 7292 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7293 lpfc_sli4_queue_unset(phba);
7294out_destroy_queue:
6c621a22 7295 lpfc_free_iocb_list(phba);
5350d872 7296 lpfc_sli4_queue_destroy(phba);
da0436e9 7297out_stop_timers:
5350d872 7298 lpfc_stop_hba_timers(phba);
da0436e9
JS
7299out_free_mbox:
7300 mempool_free(mboxq, phba->mbox_mem_pool);
7301 return rc;
7302}
7303
7304/**
7305 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7306 * @ptr: context object - pointer to hba structure.
7307 *
7308 * This is the callback function for mailbox timer. The mailbox
7309 * timer is armed when a new mailbox command is issued and the timer
7310 * is deleted when the mailbox complete. The function is called by
7311 * the kernel timer code when a mailbox does not complete within
7312 * expected time. This function wakes up the worker thread to
7313 * process the mailbox timeout and returns. All the processing is
7314 * done by the worker thread function lpfc_mbox_timeout_handler.
7315 **/
7316void
f22eb4d3 7317lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7318{
f22eb4d3 7319 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7320 unsigned long iflag;
7321 uint32_t tmo_posted;
7322
7323 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7324 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7325 if (!tmo_posted)
7326 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7327 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7328
7329 if (!tmo_posted)
7330 lpfc_worker_wake_up(phba);
7331 return;
7332}
7333
e8d3c3b1
JS
7334/**
7335 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7336 * are pending
7337 * @phba: Pointer to HBA context object.
7338 *
7339 * This function checks if any mailbox completions are present on the mailbox
7340 * completion queue.
7341 **/
3bb11fc5 7342static bool
e8d3c3b1
JS
7343lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7344{
7345
7346 uint32_t idx;
7347 struct lpfc_queue *mcq;
7348 struct lpfc_mcqe *mcqe;
7349 bool pending_completions = false;
7350
7351 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7352 return false;
7353
7354 /* Check for completions on mailbox completion queue */
7355
7356 mcq = phba->sli4_hba.mbx_cq;
7357 idx = mcq->hba_index;
7358 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7359 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7360 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7361 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7362 pending_completions = true;
7363 break;
7364 }
7365 idx = (idx + 1) % mcq->entry_count;
7366 if (mcq->hba_index == idx)
7367 break;
7368 }
7369 return pending_completions;
7370
7371}
7372
7373/**
7374 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7375 * that were missed.
7376 * @phba: Pointer to HBA context object.
7377 *
7378 * For sli4, it is possible to miss an interrupt. As such mbox completions
7379 * maybe missed causing erroneous mailbox timeouts to occur. This function
7380 * checks to see if mbox completions are on the mailbox completion queue
7381 * and will process all the completions associated with the eq for the
7382 * mailbox completion queue.
7383 **/
7384bool
7385lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7386{
b71413dd 7387 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
e8d3c3b1
JS
7388 uint32_t eqidx;
7389 struct lpfc_queue *fpeq = NULL;
7390 struct lpfc_eqe *eqe;
7391 bool mbox_pending;
7392
7393 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7394 return false;
7395
7396 /* Find the eq associated with the mcq */
7397
b71413dd 7398 if (sli4_hba->hba_eq)
895427bd 7399 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
b71413dd
JS
7400 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7401 sli4_hba->mbx_cq->assoc_qid) {
7402 fpeq = sli4_hba->hba_eq[eqidx];
e8d3c3b1
JS
7403 break;
7404 }
7405 if (!fpeq)
7406 return false;
7407
7408 /* Turn off interrupts from this EQ */
7409
b71413dd 7410 sli4_hba->sli4_eq_clr_intr(fpeq);
e8d3c3b1
JS
7411
7412 /* Check to see if a mbox completion is pending */
7413
7414 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7415
7416 /*
7417 * If a mbox completion is pending, process all the events on EQ
7418 * associated with the mbox completion queue (this could include
7419 * mailbox commands, async events, els commands, receive queue data
7420 * and fcp commands)
7421 */
7422
7423 if (mbox_pending)
7424 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7425 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7426 fpeq->EQ_processed++;
7427 }
7428
7429 /* Always clear and re-arm the EQ */
7430
b71413dd 7431 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
e8d3c3b1
JS
7432
7433 return mbox_pending;
7434
7435}
da0436e9
JS
7436
7437/**
7438 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7439 * @phba: Pointer to HBA context object.
7440 *
7441 * This function is called from worker thread when a mailbox command times out.
7442 * The caller is not required to hold any locks. This function will reset the
7443 * HBA and recover all the pending commands.
7444 **/
7445void
7446lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7447{
7448 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7449 MAILBOX_t *mb = NULL;
7450
da0436e9 7451 struct lpfc_sli *psli = &phba->sli;
da0436e9 7452
e8d3c3b1
JS
7453 /* If the mailbox completed, process the completion and return */
7454 if (lpfc_sli4_process_missed_mbox_completions(phba))
7455 return;
7456
eb016566
JS
7457 if (pmbox != NULL)
7458 mb = &pmbox->u.mb;
da0436e9
JS
7459 /* Check the pmbox pointer first. There is a race condition
7460 * between the mbox timeout handler getting executed in the
7461 * worklist and the mailbox actually completing. When this
7462 * race condition occurs, the mbox_active will be NULL.
7463 */
7464 spin_lock_irq(&phba->hbalock);
7465 if (pmbox == NULL) {
7466 lpfc_printf_log(phba, KERN_WARNING,
7467 LOG_MBOX | LOG_SLI,
7468 "0353 Active Mailbox cleared - mailbox timeout "
7469 "exiting\n");
7470 spin_unlock_irq(&phba->hbalock);
7471 return;
7472 }
7473
7474 /* Mbox cmd <mbxCommand> timeout */
7475 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7476 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7477 mb->mbxCommand,
7478 phba->pport->port_state,
7479 phba->sli.sli_flag,
7480 phba->sli.mbox_active);
7481 spin_unlock_irq(&phba->hbalock);
7482
7483 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7484 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7485 * it to fail all outstanding SCSI IO.
da0436e9
JS
7486 */
7487 spin_lock_irq(&phba->pport->work_port_lock);
7488 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7489 spin_unlock_irq(&phba->pport->work_port_lock);
7490 spin_lock_irq(&phba->hbalock);
7491 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7492 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7493 spin_unlock_irq(&phba->hbalock);
7494
db55fba8 7495 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7496
7497 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7498 "0345 Resetting board due to mailbox timeout\n");
7499
7500 /* Reset the HBA device */
7501 lpfc_reset_hba(phba);
7502}
7503
7504/**
7505 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7506 * @phba: Pointer to HBA context object.
7507 * @pmbox: Pointer to mailbox object.
7508 * @flag: Flag indicating how the mailbox need to be processed.
7509 *
7510 * This function is called by discovery code and HBA management code
7511 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7512 * function gets the hbalock to protect the data structures.
7513 * The mailbox command can be submitted in polling mode, in which case
7514 * this function will wait in a polling loop for the completion of the
7515 * mailbox.
7516 * If the mailbox is submitted in no_wait mode (not polling) the
7517 * function will submit the command and returns immediately without waiting
7518 * for the mailbox completion. The no_wait is supported only when HBA
7519 * is in SLI2/SLI3 mode - interrupts are enabled.
7520 * The SLI interface allows only one mailbox pending at a time. If the
7521 * mailbox is issued in polling mode and there is already a mailbox
7522 * pending, then the function will return an error. If the mailbox is issued
7523 * in NO_WAIT mode and there is a mailbox pending already, the function
7524 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7525 * The sli layer owns the mailbox object until the completion of mailbox
7526 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7527 * return codes the caller owns the mailbox command after the return of
7528 * the function.
e59058c4 7529 **/
3772a991
JS
7530static int
7531lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7532 uint32_t flag)
dea3101e 7533{
bf07bdea 7534 MAILBOX_t *mbx;
2e0fef85 7535 struct lpfc_sli *psli = &phba->sli;
dea3101e 7536 uint32_t status, evtctr;
9940b97b 7537 uint32_t ha_copy, hc_copy;
dea3101e 7538 int i;
09372820 7539 unsigned long timeout;
dea3101e 7540 unsigned long drvr_flag = 0;
34b02dcd 7541 uint32_t word0, ldata;
dea3101e 7542 void __iomem *to_slim;
58da1ffb
JS
7543 int processing_queue = 0;
7544
7545 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7546 if (!pmbox) {
8568a4d2 7547 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7548 /* processing mbox queue from intr_handler */
3772a991
JS
7549 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7550 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7551 return MBX_SUCCESS;
7552 }
58da1ffb 7553 processing_queue = 1;
58da1ffb
JS
7554 pmbox = lpfc_mbox_get(phba);
7555 if (!pmbox) {
7556 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7557 return MBX_SUCCESS;
7558 }
7559 }
dea3101e 7560
ed957684 7561 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7562 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7563 if(!pmbox->vport) {
58da1ffb 7564 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7565 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7566 LOG_MBOX | LOG_VPORT,
e8b62011 7567 "1806 Mbox x%x failed. No vport\n",
3772a991 7568 pmbox->u.mb.mbxCommand);
ed957684 7569 dump_stack();
58da1ffb 7570 goto out_not_finished;
ed957684
JS
7571 }
7572 }
7573
8d63f375 7574 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7575 if (unlikely(pci_channel_offline(phba->pcidev))) {
7576 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7577 goto out_not_finished;
7578 }
8d63f375 7579
a257bf90
JS
7580 /* If HBA has a deferred error attention, fail the iocb. */
7581 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7582 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7583 goto out_not_finished;
7584 }
7585
dea3101e 7586 psli = &phba->sli;
92d7f7b0 7587
bf07bdea 7588 mbx = &pmbox->u.mb;
dea3101e
JB
7589 status = MBX_SUCCESS;
7590
2e0fef85
JS
7591 if (phba->link_state == LPFC_HBA_ERROR) {
7592 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7593
7594 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7595 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7596 "(%d):0311 Mailbox command x%x cannot "
7597 "issue Data: x%x x%x\n",
7598 pmbox->vport ? pmbox->vport->vpi : 0,
7599 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7600 goto out_not_finished;
41415862
JW
7601 }
7602
bf07bdea 7603 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7604 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7605 !(hc_copy & HC_MBINT_ENA)) {
7606 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7607 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7608 "(%d):2528 Mailbox command x%x cannot "
7609 "issue Data: x%x x%x\n",
7610 pmbox->vport ? pmbox->vport->vpi : 0,
7611 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7612 goto out_not_finished;
7613 }
9290831f
JS
7614 }
7615
dea3101e
JB
7616 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7617 /* Polling for a mbox command when another one is already active
7618 * is not allowed in SLI. Also, the driver must have established
7619 * SLI2 mode to queue and process multiple mbox commands.
7620 */
7621
7622 if (flag & MBX_POLL) {
2e0fef85 7623 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7624
7625 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7626 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7627 "(%d):2529 Mailbox command x%x "
7628 "cannot issue Data: x%x x%x\n",
7629 pmbox->vport ? pmbox->vport->vpi : 0,
7630 pmbox->u.mb.mbxCommand,
7631 psli->sli_flag, flag);
58da1ffb 7632 goto out_not_finished;
dea3101e
JB
7633 }
7634
3772a991 7635 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7636 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7637 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7639 "(%d):2530 Mailbox command x%x "
7640 "cannot issue Data: x%x x%x\n",
7641 pmbox->vport ? pmbox->vport->vpi : 0,
7642 pmbox->u.mb.mbxCommand,
7643 psli->sli_flag, flag);
58da1ffb 7644 goto out_not_finished;
dea3101e
JB
7645 }
7646
dea3101e
JB
7647 /* Another mailbox command is still being processed, queue this
7648 * command to be processed later.
7649 */
7650 lpfc_mbox_put(phba, pmbox);
7651
7652 /* Mbox cmd issue - BUSY */
ed957684 7653 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7654 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7655 "x%x x%x x%x x%x\n",
92d7f7b0 7656 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
7657 mbx->mbxCommand,
7658 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7659 psli->sli_flag, flag);
dea3101e
JB
7660
7661 psli->slistat.mbox_busy++;
2e0fef85 7662 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7663
858c9f6c
JS
7664 if (pmbox->vport) {
7665 lpfc_debugfs_disc_trc(pmbox->vport,
7666 LPFC_DISC_TRC_MBOX_VPORT,
7667 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7668 (uint32_t)mbx->mbxCommand,
7669 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7670 }
7671 else {
7672 lpfc_debugfs_disc_trc(phba->pport,
7673 LPFC_DISC_TRC_MBOX,
7674 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7675 (uint32_t)mbx->mbxCommand,
7676 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7677 }
7678
2e0fef85 7679 return MBX_BUSY;
dea3101e
JB
7680 }
7681
dea3101e
JB
7682 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7683
7684 /* If we are not polling, we MUST be in SLI2 mode */
7685 if (flag != MBX_POLL) {
3772a991 7686 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7687 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7688 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7690 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7691 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7692 "(%d):2531 Mailbox command x%x "
7693 "cannot issue Data: x%x x%x\n",
7694 pmbox->vport ? pmbox->vport->vpi : 0,
7695 pmbox->u.mb.mbxCommand,
7696 psli->sli_flag, flag);
58da1ffb 7697 goto out_not_finished;
dea3101e
JB
7698 }
7699 /* timeout active mbox command */
256ec0d0
JS
7700 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7701 1000);
7702 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7703 }
7704
7705 /* Mailbox cmd <cmd> issue */
ed957684 7706 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7707 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7708 "x%x\n",
e8b62011 7709 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
7710 mbx->mbxCommand,
7711 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7712 psli->sli_flag, flag);
dea3101e 7713
bf07bdea 7714 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7715 if (pmbox->vport) {
7716 lpfc_debugfs_disc_trc(pmbox->vport,
7717 LPFC_DISC_TRC_MBOX_VPORT,
7718 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7719 (uint32_t)mbx->mbxCommand,
7720 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7721 }
7722 else {
7723 lpfc_debugfs_disc_trc(phba->pport,
7724 LPFC_DISC_TRC_MBOX,
7725 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7726 (uint32_t)mbx->mbxCommand,
7727 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7728 }
7729 }
7730
dea3101e
JB
7731 psli->slistat.mbox_cmd++;
7732 evtctr = psli->slistat.mbox_event;
7733
7734 /* next set own bit for the adapter and copy over command word */
bf07bdea 7735 mbx->mbxOwner = OWN_CHIP;
dea3101e 7736
3772a991 7737 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7738 /* Populate mbox extension offset word. */
7739 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7740 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7741 = (uint8_t *)phba->mbox_ext
7742 - (uint8_t *)phba->mbox;
7743 }
7744
7745 /* Copy the mailbox extension data */
7746 if (pmbox->in_ext_byte_len && pmbox->context2) {
7747 lpfc_sli_pcimem_bcopy(pmbox->context2,
7748 (uint8_t *)phba->mbox_ext,
7749 pmbox->in_ext_byte_len);
7750 }
7751 /* Copy command data to host SLIM area */
bf07bdea 7752 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7753 } else {
7a470277
JS
7754 /* Populate mbox extension offset word. */
7755 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7756 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7757 = MAILBOX_HBA_EXT_OFFSET;
7758
7759 /* Copy the mailbox extension data */
895427bd 7760 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7761 lpfc_memcpy_to_slim(phba->MBslimaddr +
7762 MAILBOX_HBA_EXT_OFFSET,
7763 pmbox->context2, pmbox->in_ext_byte_len);
7764
895427bd 7765 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7766 /* copy command data into host mbox for cmpl */
895427bd
JS
7767 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7768 MAILBOX_CMD_SIZE);
dea3101e
JB
7769
7770 /* First copy mbox command data to HBA SLIM, skip past first
7771 word */
7772 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7773 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7774 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7775
7776 /* Next copy over first word, with mbxOwner set */
bf07bdea 7777 ldata = *((uint32_t *)mbx);
dea3101e
JB
7778 to_slim = phba->MBslimaddr;
7779 writel(ldata, to_slim);
7780 readl(to_slim); /* flush */
7781
895427bd 7782 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7783 /* switch over to host mailbox */
3772a991 7784 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7785 }
7786
7787 wmb();
dea3101e
JB
7788
7789 switch (flag) {
7790 case MBX_NOWAIT:
09372820 7791 /* Set up reference to mailbox command */
dea3101e 7792 psli->mbox_active = pmbox;
09372820
JS
7793 /* Interrupt board to do it */
7794 writel(CA_MBATT, phba->CAregaddr);
7795 readl(phba->CAregaddr); /* flush */
7796 /* Don't wait for it to finish, just return */
dea3101e
JB
7797 break;
7798
7799 case MBX_POLL:
09372820 7800 /* Set up null reference to mailbox command */
dea3101e 7801 psli->mbox_active = NULL;
09372820
JS
7802 /* Interrupt board to do it */
7803 writel(CA_MBATT, phba->CAregaddr);
7804 readl(phba->CAregaddr); /* flush */
7805
3772a991 7806 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7807 /* First read mbox status word */
34b02dcd 7808 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7809 word0 = le32_to_cpu(word0);
7810 } else {
7811 /* First read mbox status word */
9940b97b
JS
7812 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7813 spin_unlock_irqrestore(&phba->hbalock,
7814 drvr_flag);
7815 goto out_not_finished;
7816 }
dea3101e
JB
7817 }
7818
7819 /* Read the HBA Host Attention Register */
9940b97b
JS
7820 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7821 spin_unlock_irqrestore(&phba->hbalock,
7822 drvr_flag);
7823 goto out_not_finished;
7824 }
a183a15f
JS
7825 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7826 1000) + jiffies;
09372820 7827 i = 0;
dea3101e 7828 /* Wait for command to complete */
41415862
JW
7829 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7830 (!(ha_copy & HA_MBATT) &&
2e0fef85 7831 (phba->link_state > LPFC_WARM_START))) {
09372820 7832 if (time_after(jiffies, timeout)) {
dea3101e 7833 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7834 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7835 drvr_flag);
58da1ffb 7836 goto out_not_finished;
dea3101e
JB
7837 }
7838
7839 /* Check if we took a mbox interrupt while we were
7840 polling */
7841 if (((word0 & OWN_CHIP) != OWN_CHIP)
7842 && (evtctr != psli->slistat.mbox_event))
7843 break;
7844
09372820
JS
7845 if (i++ > 10) {
7846 spin_unlock_irqrestore(&phba->hbalock,
7847 drvr_flag);
7848 msleep(1);
7849 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7850 }
dea3101e 7851
3772a991 7852 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7853 /* First copy command data */
34b02dcd 7854 word0 = *((uint32_t *)phba->mbox);
dea3101e 7855 word0 = le32_to_cpu(word0);
bf07bdea 7856 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7857 MAILBOX_t *slimmb;
34b02dcd 7858 uint32_t slimword0;
dea3101e
JB
7859 /* Check real SLIM for any errors */
7860 slimword0 = readl(phba->MBslimaddr);
7861 slimmb = (MAILBOX_t *) & slimword0;
7862 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7863 && slimmb->mbxStatus) {
7864 psli->sli_flag &=
3772a991 7865 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7866 word0 = slimword0;
7867 }
7868 }
7869 } else {
7870 /* First copy command data */
7871 word0 = readl(phba->MBslimaddr);
7872 }
7873 /* Read the HBA Host Attention Register */
9940b97b
JS
7874 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7875 spin_unlock_irqrestore(&phba->hbalock,
7876 drvr_flag);
7877 goto out_not_finished;
7878 }
dea3101e
JB
7879 }
7880
3772a991 7881 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7882 /* copy results back to user */
2ea259ee
JS
7883 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7884 MAILBOX_CMD_SIZE);
7a470277
JS
7885 /* Copy the mailbox extension data */
7886 if (pmbox->out_ext_byte_len && pmbox->context2) {
7887 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7888 pmbox->context2,
7889 pmbox->out_ext_byte_len);
7890 }
dea3101e
JB
7891 } else {
7892 /* First copy command data */
bf07bdea 7893 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 7894 MAILBOX_CMD_SIZE);
7a470277
JS
7895 /* Copy the mailbox extension data */
7896 if (pmbox->out_ext_byte_len && pmbox->context2) {
7897 lpfc_memcpy_from_slim(pmbox->context2,
7898 phba->MBslimaddr +
7899 MAILBOX_HBA_EXT_OFFSET,
7900 pmbox->out_ext_byte_len);
dea3101e
JB
7901 }
7902 }
7903
7904 writel(HA_MBATT, phba->HAregaddr);
7905 readl(phba->HAregaddr); /* flush */
7906
7907 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7908 status = mbx->mbxStatus;
dea3101e
JB
7909 }
7910
2e0fef85
JS
7911 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7912 return status;
58da1ffb
JS
7913
7914out_not_finished:
7915 if (processing_queue) {
da0436e9 7916 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7917 lpfc_mbox_cmpl_put(phba, pmbox);
7918 }
7919 return MBX_NOT_FINISHED;
dea3101e
JB
7920}
7921
f1126688
JS
7922/**
7923 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7924 * @phba: Pointer to HBA context object.
7925 *
7926 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7927 * the driver internal pending mailbox queue. It will then try to wait out the
7928 * possible outstanding mailbox command before return.
7929 *
7930 * Returns:
7931 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7932 * the outstanding mailbox command timed out.
7933 **/
7934static int
7935lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7936{
7937 struct lpfc_sli *psli = &phba->sli;
f1126688 7938 int rc = 0;
a183a15f 7939 unsigned long timeout = 0;
f1126688
JS
7940
7941 /* Mark the asynchronous mailbox command posting as blocked */
7942 spin_lock_irq(&phba->hbalock);
7943 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7944 /* Determine how long we might wait for the active mailbox
7945 * command to be gracefully completed by firmware.
7946 */
a183a15f
JS
7947 if (phba->sli.mbox_active)
7948 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7949 phba->sli.mbox_active) *
7950 1000) + jiffies;
7951 spin_unlock_irq(&phba->hbalock);
7952
e8d3c3b1
JS
7953 /* Make sure the mailbox is really active */
7954 if (timeout)
7955 lpfc_sli4_process_missed_mbox_completions(phba);
7956
f1126688
JS
7957 /* Wait for the outstnading mailbox command to complete */
7958 while (phba->sli.mbox_active) {
7959 /* Check active mailbox complete status every 2ms */
7960 msleep(2);
7961 if (time_after(jiffies, timeout)) {
7962 /* Timeout, marked the outstanding cmd not complete */
7963 rc = 1;
7964 break;
7965 }
7966 }
7967
7968 /* Can not cleanly block async mailbox command, fails it */
7969 if (rc) {
7970 spin_lock_irq(&phba->hbalock);
7971 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7972 spin_unlock_irq(&phba->hbalock);
7973 }
7974 return rc;
7975}
7976
7977/**
7978 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7979 * @phba: Pointer to HBA context object.
7980 *
7981 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7982 * commands from the driver internal pending mailbox queue. It makes sure
7983 * that there is no outstanding mailbox command before resuming posting
7984 * asynchronous mailbox commands. If, for any reason, there is outstanding
7985 * mailbox command, it will try to wait it out before resuming asynchronous
7986 * mailbox command posting.
7987 **/
7988static void
7989lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7990{
7991 struct lpfc_sli *psli = &phba->sli;
7992
7993 spin_lock_irq(&phba->hbalock);
7994 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7995 /* Asynchronous mailbox posting is not blocked, do nothing */
7996 spin_unlock_irq(&phba->hbalock);
7997 return;
7998 }
7999
8000 /* Outstanding synchronous mailbox command is guaranteed to be done,
8001 * successful or timeout, after timing-out the outstanding mailbox
8002 * command shall always be removed, so just unblock posting async
8003 * mailbox command and resume
8004 */
8005 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8006 spin_unlock_irq(&phba->hbalock);
8007
8008 /* wake up worker thread to post asynchronlous mailbox command */
8009 lpfc_worker_wake_up(phba);
8010}
8011
2d843edc
JS
8012/**
8013 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8014 * @phba: Pointer to HBA context object.
8015 * @mboxq: Pointer to mailbox object.
8016 *
8017 * The function waits for the bootstrap mailbox register ready bit from
8018 * port for twice the regular mailbox command timeout value.
8019 *
8020 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8021 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8022 **/
8023static int
8024lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8025{
8026 uint32_t db_ready;
8027 unsigned long timeout;
8028 struct lpfc_register bmbx_reg;
8029
8030 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8031 * 1000) + jiffies;
8032
8033 do {
8034 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8035 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8036 if (!db_ready)
8037 msleep(2);
8038
8039 if (time_after(jiffies, timeout))
8040 return MBXERR_ERROR;
8041 } while (!db_ready);
8042
8043 return 0;
8044}
8045
da0436e9
JS
8046/**
8047 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8048 * @phba: Pointer to HBA context object.
8049 * @mboxq: Pointer to mailbox object.
8050 *
8051 * The function posts a mailbox to the port. The mailbox is expected
8052 * to be comletely filled in and ready for the port to operate on it.
8053 * This routine executes a synchronous completion operation on the
8054 * mailbox by polling for its completion.
8055 *
8056 * The caller must not be holding any locks when calling this routine.
8057 *
8058 * Returns:
8059 * MBX_SUCCESS - mailbox posted successfully
8060 * Any of the MBX error values.
8061 **/
8062static int
8063lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8064{
8065 int rc = MBX_SUCCESS;
8066 unsigned long iflag;
da0436e9
JS
8067 uint32_t mcqe_status;
8068 uint32_t mbx_cmnd;
da0436e9
JS
8069 struct lpfc_sli *psli = &phba->sli;
8070 struct lpfc_mqe *mb = &mboxq->u.mqe;
8071 struct lpfc_bmbx_create *mbox_rgn;
8072 struct dma_address *dma_address;
da0436e9
JS
8073
8074 /*
8075 * Only one mailbox can be active to the bootstrap mailbox region
8076 * at a time and there is no queueing provided.
8077 */
8078 spin_lock_irqsave(&phba->hbalock, iflag);
8079 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8080 spin_unlock_irqrestore(&phba->hbalock, iflag);
8081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8082 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8083 "cannot issue Data: x%x x%x\n",
8084 mboxq->vport ? mboxq->vport->vpi : 0,
8085 mboxq->u.mb.mbxCommand,
a183a15f
JS
8086 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8087 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8088 psli->sli_flag, MBX_POLL);
8089 return MBXERR_ERROR;
8090 }
8091 /* The server grabs the token and owns it until release */
8092 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8093 phba->sli.mbox_active = mboxq;
8094 spin_unlock_irqrestore(&phba->hbalock, iflag);
8095
2d843edc
JS
8096 /* wait for bootstrap mbox register for readyness */
8097 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8098 if (rc)
8099 goto exit;
8100
da0436e9
JS
8101 /*
8102 * Initialize the bootstrap memory region to avoid stale data areas
8103 * in the mailbox post. Then copy the caller's mailbox contents to
8104 * the bmbx mailbox region.
8105 */
8106 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8107 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8108 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8109 sizeof(struct lpfc_mqe));
8110
8111 /* Post the high mailbox dma address to the port and wait for ready. */
8112 dma_address = &phba->sli4_hba.bmbx.dma_address;
8113 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8114
2d843edc
JS
8115 /* wait for bootstrap mbox register for hi-address write done */
8116 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8117 if (rc)
8118 goto exit;
da0436e9
JS
8119
8120 /* Post the low mailbox dma address to the port. */
8121 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 8122
2d843edc
JS
8123 /* wait for bootstrap mbox register for low address write done */
8124 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8125 if (rc)
8126 goto exit;
da0436e9
JS
8127
8128 /*
8129 * Read the CQ to ensure the mailbox has completed.
8130 * If so, update the mailbox status so that the upper layers
8131 * can complete the request normally.
8132 */
8133 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8134 sizeof(struct lpfc_mqe));
8135 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8136 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8137 sizeof(struct lpfc_mcqe));
8138 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
8139 /*
8140 * When the CQE status indicates a failure and the mailbox status
8141 * indicates success then copy the CQE status into the mailbox status
8142 * (and prefix it with x4000).
8143 */
da0436e9 8144 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8145 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8146 bf_set(lpfc_mqe_status, mb,
8147 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8148 rc = MBXERR_ERROR;
d7c47992
JS
8149 } else
8150 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8151
8152 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8153 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8154 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8155 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8156 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8157 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8158 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8159 bf_get(lpfc_mqe_status, mb),
8160 mb->un.mb_words[0], mb->un.mb_words[1],
8161 mb->un.mb_words[2], mb->un.mb_words[3],
8162 mb->un.mb_words[4], mb->un.mb_words[5],
8163 mb->un.mb_words[6], mb->un.mb_words[7],
8164 mb->un.mb_words[8], mb->un.mb_words[9],
8165 mb->un.mb_words[10], mb->un.mb_words[11],
8166 mb->un.mb_words[12], mboxq->mcqe.word0,
8167 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8168 mboxq->mcqe.trailer);
8169exit:
8170 /* We are holding the token, no needed for lock when release */
8171 spin_lock_irqsave(&phba->hbalock, iflag);
8172 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8173 phba->sli.mbox_active = NULL;
8174 spin_unlock_irqrestore(&phba->hbalock, iflag);
8175 return rc;
8176}
8177
8178/**
8179 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8180 * @phba: Pointer to HBA context object.
8181 * @pmbox: Pointer to mailbox object.
8182 * @flag: Flag indicating how the mailbox need to be processed.
8183 *
8184 * This function is called by discovery code and HBA management code to submit
8185 * a mailbox command to firmware with SLI-4 interface spec.
8186 *
8187 * Return codes the caller owns the mailbox command after the return of the
8188 * function.
8189 **/
8190static int
8191lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8192 uint32_t flag)
8193{
8194 struct lpfc_sli *psli = &phba->sli;
8195 unsigned long iflags;
8196 int rc;
8197
b76f2dc9
JS
8198 /* dump from issue mailbox command if setup */
8199 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8200
8fa38513
JS
8201 rc = lpfc_mbox_dev_check(phba);
8202 if (unlikely(rc)) {
8203 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8204 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8205 "cannot issue Data: x%x x%x\n",
8206 mboxq->vport ? mboxq->vport->vpi : 0,
8207 mboxq->u.mb.mbxCommand,
a183a15f
JS
8208 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8209 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8210 psli->sli_flag, flag);
8211 goto out_not_finished;
8212 }
8213
da0436e9
JS
8214 /* Detect polling mode and jump to a handler */
8215 if (!phba->sli4_hba.intr_enable) {
8216 if (flag == MBX_POLL)
8217 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8218 else
8219 rc = -EIO;
8220 if (rc != MBX_SUCCESS)
0558056c 8221 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8222 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8223 "(x%x/x%x) failure: "
8224 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8225 "Data: x%x x%x\n,",
da0436e9
JS
8226 mboxq->vport ? mboxq->vport->vpi : 0,
8227 mboxq->u.mb.mbxCommand,
a183a15f
JS
8228 lpfc_sli_config_mbox_subsys_get(phba,
8229 mboxq),
8230 lpfc_sli_config_mbox_opcode_get(phba,
8231 mboxq),
cc459f19
JS
8232 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8233 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8234 bf_get(lpfc_mcqe_ext_status,
8235 &mboxq->mcqe),
da0436e9
JS
8236 psli->sli_flag, flag);
8237 return rc;
8238 } else if (flag == MBX_POLL) {
f1126688
JS
8239 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8240 "(%d):2542 Try to issue mailbox command "
a183a15f 8241 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 8242 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8243 mboxq->vport ? mboxq->vport->vpi : 0,
8244 mboxq->u.mb.mbxCommand,
a183a15f
JS
8245 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8246 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8247 psli->sli_flag, flag);
f1126688
JS
8248 /* Try to block the asynchronous mailbox posting */
8249 rc = lpfc_sli4_async_mbox_block(phba);
8250 if (!rc) {
8251 /* Successfully blocked, now issue sync mbox cmd */
8252 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8253 if (rc != MBX_SUCCESS)
cc459f19 8254 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8255 LOG_MBOX | LOG_SLI,
cc459f19
JS
8256 "(%d):2597 Sync Mailbox command "
8257 "x%x (x%x/x%x) failure: "
8258 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8259 "Data: x%x x%x\n,",
8260 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8261 mboxq->u.mb.mbxCommand,
8262 lpfc_sli_config_mbox_subsys_get(phba,
8263 mboxq),
8264 lpfc_sli_config_mbox_opcode_get(phba,
8265 mboxq),
cc459f19
JS
8266 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8267 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8268 bf_get(lpfc_mcqe_ext_status,
8269 &mboxq->mcqe),
a183a15f 8270 psli->sli_flag, flag);
f1126688
JS
8271 /* Unblock the async mailbox posting afterward */
8272 lpfc_sli4_async_mbox_unblock(phba);
8273 }
8274 return rc;
da0436e9
JS
8275 }
8276
8277 /* Now, interrupt mode asynchrous mailbox command */
8278 rc = lpfc_mbox_cmd_check(phba, mboxq);
8279 if (rc) {
8280 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8281 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8282 "cannot issue Data: x%x x%x\n",
8283 mboxq->vport ? mboxq->vport->vpi : 0,
8284 mboxq->u.mb.mbxCommand,
a183a15f
JS
8285 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8286 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8287 psli->sli_flag, flag);
8288 goto out_not_finished;
8289 }
da0436e9
JS
8290
8291 /* Put the mailbox command to the driver internal FIFO */
8292 psli->slistat.mbox_busy++;
8293 spin_lock_irqsave(&phba->hbalock, iflags);
8294 lpfc_mbox_put(phba, mboxq);
8295 spin_unlock_irqrestore(&phba->hbalock, iflags);
8296 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8297 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8298 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8299 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8300 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8301 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8302 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8303 phba->pport->port_state,
8304 psli->sli_flag, MBX_NOWAIT);
8305 /* Wake up worker thread to transport mailbox command from head */
8306 lpfc_worker_wake_up(phba);
8307
8308 return MBX_BUSY;
8309
8310out_not_finished:
8311 return MBX_NOT_FINISHED;
8312}
8313
8314/**
8315 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8316 * @phba: Pointer to HBA context object.
8317 *
8318 * This function is called by worker thread to send a mailbox command to
8319 * SLI4 HBA firmware.
8320 *
8321 **/
8322int
8323lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8324{
8325 struct lpfc_sli *psli = &phba->sli;
8326 LPFC_MBOXQ_t *mboxq;
8327 int rc = MBX_SUCCESS;
8328 unsigned long iflags;
8329 struct lpfc_mqe *mqe;
8330 uint32_t mbx_cmnd;
8331
8332 /* Check interrupt mode before post async mailbox command */
8333 if (unlikely(!phba->sli4_hba.intr_enable))
8334 return MBX_NOT_FINISHED;
8335
8336 /* Check for mailbox command service token */
8337 spin_lock_irqsave(&phba->hbalock, iflags);
8338 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8339 spin_unlock_irqrestore(&phba->hbalock, iflags);
8340 return MBX_NOT_FINISHED;
8341 }
8342 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8343 spin_unlock_irqrestore(&phba->hbalock, iflags);
8344 return MBX_NOT_FINISHED;
8345 }
8346 if (unlikely(phba->sli.mbox_active)) {
8347 spin_unlock_irqrestore(&phba->hbalock, iflags);
8348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8349 "0384 There is pending active mailbox cmd\n");
8350 return MBX_NOT_FINISHED;
8351 }
8352 /* Take the mailbox command service token */
8353 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8354
8355 /* Get the next mailbox command from head of queue */
8356 mboxq = lpfc_mbox_get(phba);
8357
8358 /* If no more mailbox command waiting for post, we're done */
8359 if (!mboxq) {
8360 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8361 spin_unlock_irqrestore(&phba->hbalock, iflags);
8362 return MBX_SUCCESS;
8363 }
8364 phba->sli.mbox_active = mboxq;
8365 spin_unlock_irqrestore(&phba->hbalock, iflags);
8366
8367 /* Check device readiness for posting mailbox command */
8368 rc = lpfc_mbox_dev_check(phba);
8369 if (unlikely(rc))
8370 /* Driver clean routine will clean up pending mailbox */
8371 goto out_not_finished;
8372
8373 /* Prepare the mbox command to be posted */
8374 mqe = &mboxq->u.mqe;
8375 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8376
8377 /* Start timer for the mbox_tmo and log some mailbox post messages */
8378 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8379 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8380
8381 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8382 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8383 "x%x x%x\n",
8384 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8385 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8386 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8387 phba->pport->port_state, psli->sli_flag);
8388
8389 if (mbx_cmnd != MBX_HEARTBEAT) {
8390 if (mboxq->vport) {
8391 lpfc_debugfs_disc_trc(mboxq->vport,
8392 LPFC_DISC_TRC_MBOX_VPORT,
8393 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8394 mbx_cmnd, mqe->un.mb_words[0],
8395 mqe->un.mb_words[1]);
8396 } else {
8397 lpfc_debugfs_disc_trc(phba->pport,
8398 LPFC_DISC_TRC_MBOX,
8399 "MBOX Send: cmd:x%x mb:x%x x%x",
8400 mbx_cmnd, mqe->un.mb_words[0],
8401 mqe->un.mb_words[1]);
8402 }
8403 }
8404 psli->slistat.mbox_cmd++;
8405
8406 /* Post the mailbox command to the port */
8407 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8408 if (rc != MBX_SUCCESS) {
8409 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8410 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8411 "cannot issue Data: x%x x%x\n",
8412 mboxq->vport ? mboxq->vport->vpi : 0,
8413 mboxq->u.mb.mbxCommand,
a183a15f
JS
8414 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8415 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8416 psli->sli_flag, MBX_NOWAIT);
8417 goto out_not_finished;
8418 }
8419
8420 return rc;
8421
8422out_not_finished:
8423 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8424 if (phba->sli.mbox_active) {
8425 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8426 __lpfc_mbox_cmpl_put(phba, mboxq);
8427 /* Release the token */
8428 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8429 phba->sli.mbox_active = NULL;
8430 }
da0436e9
JS
8431 spin_unlock_irqrestore(&phba->hbalock, iflags);
8432
8433 return MBX_NOT_FINISHED;
8434}
8435
8436/**
8437 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8438 * @phba: Pointer to HBA context object.
8439 * @pmbox: Pointer to mailbox object.
8440 * @flag: Flag indicating how the mailbox need to be processed.
8441 *
8442 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8443 * the API jump table function pointer from the lpfc_hba struct.
8444 *
8445 * Return codes the caller owns the mailbox command after the return of the
8446 * function.
8447 **/
8448int
8449lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8450{
8451 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8452}
8453
8454/**
25985edc 8455 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8456 * @phba: The hba struct for which this call is being executed.
8457 * @dev_grp: The HBA PCI-Device group number.
8458 *
8459 * This routine sets up the mbox interface API function jump table in @phba
8460 * struct.
8461 * Returns: 0 - success, -ENODEV - failure.
8462 **/
8463int
8464lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8465{
8466
8467 switch (dev_grp) {
8468 case LPFC_PCI_DEV_LP:
8469 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8470 phba->lpfc_sli_handle_slow_ring_event =
8471 lpfc_sli_handle_slow_ring_event_s3;
8472 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8473 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8474 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8475 break;
8476 case LPFC_PCI_DEV_OC:
8477 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8478 phba->lpfc_sli_handle_slow_ring_event =
8479 lpfc_sli_handle_slow_ring_event_s4;
8480 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8481 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8482 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8483 break;
8484 default:
8485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8486 "1420 Invalid HBA PCI-device group: 0x%x\n",
8487 dev_grp);
8488 return -ENODEV;
8489 break;
8490 }
8491 return 0;
8492}
8493
e59058c4 8494/**
3621a710 8495 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8496 * @phba: Pointer to HBA context object.
8497 * @pring: Pointer to driver SLI ring object.
8498 * @piocb: Pointer to address of newly added command iocb.
8499 *
8500 * This function is called with hbalock held to add a command
8501 * iocb to the txq when SLI layer cannot submit the command iocb
8502 * to the ring.
8503 **/
2a9bf3d0 8504void
92d7f7b0 8505__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8506 struct lpfc_iocbq *piocb)
dea3101e 8507{
1c2ba475 8508 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8509 /* Insert the caller's iocb in the txq tail for later processing. */
8510 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8511}
8512
e59058c4 8513/**
3621a710 8514 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8515 * @phba: Pointer to HBA context object.
8516 * @pring: Pointer to driver SLI ring object.
8517 * @piocb: Pointer to address of newly added command iocb.
8518 *
8519 * This function is called with hbalock held before a new
8520 * iocb is submitted to the firmware. This function checks
8521 * txq to flush the iocbs in txq to Firmware before
8522 * submitting new iocbs to the Firmware.
8523 * If there are iocbs in the txq which need to be submitted
8524 * to firmware, lpfc_sli_next_iocb returns the first element
8525 * of the txq after dequeuing it from txq.
8526 * If there is no iocb in the txq then the function will return
8527 * *piocb and *piocb is set to NULL. Caller needs to check
8528 * *piocb to find if there are more commands in the txq.
8529 **/
dea3101e
JB
8530static struct lpfc_iocbq *
8531lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8532 struct lpfc_iocbq **piocb)
dea3101e
JB
8533{
8534 struct lpfc_iocbq * nextiocb;
8535
1c2ba475
JT
8536 lockdep_assert_held(&phba->hbalock);
8537
dea3101e
JB
8538 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8539 if (!nextiocb) {
8540 nextiocb = *piocb;
8541 *piocb = NULL;
8542 }
8543
8544 return nextiocb;
8545}
8546
e59058c4 8547/**
3772a991 8548 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8549 * @phba: Pointer to HBA context object.
3772a991 8550 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8551 * @piocb: Pointer to command iocb.
8552 * @flag: Flag indicating if this command can be put into txq.
8553 *
3772a991
JS
8554 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8555 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8556 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8557 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8558 * this function allows only iocbs for posting buffers. This function finds
8559 * next available slot in the command ring and posts the command to the
8560 * available slot and writes the port attention register to request HBA start
8561 * processing new iocb. If there is no slot available in the ring and
8562 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8563 * the function returns IOCB_BUSY.
e59058c4 8564 *
3772a991
JS
8565 * This function is called with hbalock held. The function will return success
8566 * after it successfully submit the iocb to firmware or after adding to the
8567 * txq.
e59058c4 8568 **/
98c9ea5c 8569static int
3772a991 8570__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8571 struct lpfc_iocbq *piocb, uint32_t flag)
8572{
8573 struct lpfc_iocbq *nextiocb;
8574 IOCB_t *iocb;
895427bd 8575 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8576
1c2ba475
JT
8577 lockdep_assert_held(&phba->hbalock);
8578
92d7f7b0
JS
8579 if (piocb->iocb_cmpl && (!piocb->vport) &&
8580 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8581 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8582 lpfc_printf_log(phba, KERN_ERR,
8583 LOG_SLI | LOG_VPORT,
e8b62011 8584 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8585 piocb->iocb.ulpCommand);
8586 dump_stack();
8587 return IOCB_ERROR;
8588 }
8589
8590
8d63f375
LV
8591 /* If the PCI channel is in offline state, do not post iocbs. */
8592 if (unlikely(pci_channel_offline(phba->pcidev)))
8593 return IOCB_ERROR;
8594
a257bf90
JS
8595 /* If HBA has a deferred error attention, fail the iocb. */
8596 if (unlikely(phba->hba_flag & DEFER_ERATT))
8597 return IOCB_ERROR;
8598
dea3101e
JB
8599 /*
8600 * We should never get an IOCB if we are in a < LINK_DOWN state
8601 */
2e0fef85 8602 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8603 return IOCB_ERROR;
8604
8605 /*
8606 * Check to see if we are blocking IOCB processing because of a
0b727fea 8607 * outstanding event.
dea3101e 8608 */
0b727fea 8609 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8610 goto iocb_busy;
8611
2e0fef85 8612 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8613 /*
2680eeaa 8614 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8615 * can be issued if the link is not up.
8616 */
8617 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8618 case CMD_GEN_REQUEST64_CR:
8619 case CMD_GEN_REQUEST64_CX:
8620 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8621 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8622 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8623 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8624 MENLO_TRANSPORT_TYPE))
8625
8626 goto iocb_busy;
8627 break;
dea3101e
JB
8628 case CMD_QUE_RING_BUF_CN:
8629 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8630 /*
8631 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8632 * completion, iocb_cmpl MUST be 0.
8633 */
8634 if (piocb->iocb_cmpl)
8635 piocb->iocb_cmpl = NULL;
8636 /*FALLTHROUGH*/
8637 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8638 case CMD_CLOSE_XRI_CN:
8639 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8640 break;
8641 default:
8642 goto iocb_busy;
8643 }
8644
8645 /*
8646 * For FCP commands, we must be in a state where we can process link
8647 * attention events.
8648 */
895427bd 8649 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8650 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8651 goto iocb_busy;
92d7f7b0 8652 }
dea3101e 8653
dea3101e
JB
8654 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8655 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8656 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8657
8658 if (iocb)
8659 lpfc_sli_update_ring(phba, pring);
8660 else
8661 lpfc_sli_update_full_ring(phba, pring);
8662
8663 if (!piocb)
8664 return IOCB_SUCCESS;
8665
8666 goto out_busy;
8667
8668 iocb_busy:
8669 pring->stats.iocb_cmd_delay++;
8670
8671 out_busy:
8672
8673 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8674 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8675 return IOCB_SUCCESS;
8676 }
8677
8678 return IOCB_BUSY;
8679}
8680
3772a991 8681/**
4f774513
JS
8682 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8683 * @phba: Pointer to HBA context object.
8684 * @piocb: Pointer to command iocb.
8685 * @sglq: Pointer to the scatter gather queue object.
8686 *
8687 * This routine converts the bpl or bde that is in the IOCB
8688 * to a sgl list for the sli4 hardware. The physical address
8689 * of the bpl/bde is converted back to a virtual address.
8690 * If the IOCB contains a BPL then the list of BDE's is
8691 * converted to sli4_sge's. If the IOCB contains a single
8692 * BDE then it is converted to a single sli_sge.
8693 * The IOCB is still in cpu endianess so the contents of
8694 * the bpl can be used without byte swapping.
8695 *
8696 * Returns valid XRI = Success, NO_XRI = Failure.
8697**/
8698static uint16_t
8699lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8700 struct lpfc_sglq *sglq)
3772a991 8701{
4f774513
JS
8702 uint16_t xritag = NO_XRI;
8703 struct ulp_bde64 *bpl = NULL;
8704 struct ulp_bde64 bde;
8705 struct sli4_sge *sgl = NULL;
1b51197d 8706 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8707 IOCB_t *icmd;
8708 int numBdes = 0;
8709 int i = 0;
63e801ce
JS
8710 uint32_t offset = 0; /* accumulated offset in the sg request list */
8711 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8712
4f774513
JS
8713 if (!piocbq || !sglq)
8714 return xritag;
8715
8716 sgl = (struct sli4_sge *)sglq->sgl;
8717 icmd = &piocbq->iocb;
6b5151fd
JS
8718 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8719 return sglq->sli4_xritag;
4f774513
JS
8720 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8721 numBdes = icmd->un.genreq64.bdl.bdeSize /
8722 sizeof(struct ulp_bde64);
8723 /* The addrHigh and addrLow fields within the IOCB
8724 * have not been byteswapped yet so there is no
8725 * need to swap them back.
8726 */
1b51197d
JS
8727 if (piocbq->context3)
8728 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8729 else
8730 return xritag;
4f774513 8731
1b51197d 8732 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8733 if (!bpl)
8734 return xritag;
8735
8736 for (i = 0; i < numBdes; i++) {
8737 /* Should already be byte swapped. */
28baac74
JS
8738 sgl->addr_hi = bpl->addrHigh;
8739 sgl->addr_lo = bpl->addrLow;
8740
0558056c 8741 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8742 if ((i+1) == numBdes)
8743 bf_set(lpfc_sli4_sge_last, sgl, 1);
8744 else
8745 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8746 /* swap the size field back to the cpu so we
8747 * can assign it to the sgl.
8748 */
8749 bde.tus.w = le32_to_cpu(bpl->tus.w);
8750 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8751 /* The offsets in the sgl need to be accumulated
8752 * separately for the request and reply lists.
8753 * The request is always first, the reply follows.
8754 */
8755 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8756 /* add up the reply sg entries */
8757 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8758 inbound++;
8759 /* first inbound? reset the offset */
8760 if (inbound == 1)
8761 offset = 0;
8762 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8763 bf_set(lpfc_sli4_sge_type, sgl,
8764 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8765 offset += bde.tus.f.bdeSize;
8766 }
546fc854 8767 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8768 bpl++;
8769 sgl++;
8770 }
8771 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8772 /* The addrHigh and addrLow fields of the BDE have not
8773 * been byteswapped yet so they need to be swapped
8774 * before putting them in the sgl.
8775 */
8776 sgl->addr_hi =
8777 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8778 sgl->addr_lo =
8779 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8780 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8781 bf_set(lpfc_sli4_sge_last, sgl, 1);
8782 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8783 sgl->sge_len =
8784 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8785 }
8786 return sglq->sli4_xritag;
3772a991 8787}
92d7f7b0 8788
e59058c4 8789/**
4f774513 8790 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8791 * @phba: Pointer to HBA context object.
4f774513
JS
8792 * @piocb: Pointer to command iocb.
8793 * @wqe: Pointer to the work queue entry.
e59058c4 8794 *
4f774513
JS
8795 * This routine converts the iocb command to its Work Queue Entry
8796 * equivalent. The wqe pointer should not have any fields set when
8797 * this routine is called because it will memcpy over them.
8798 * This routine does not set the CQ_ID or the WQEC bits in the
8799 * wqe.
e59058c4 8800 *
4f774513 8801 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8802 **/
cf5bf97e 8803static int
4f774513
JS
8804lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8805 union lpfc_wqe *wqe)
cf5bf97e 8806{
5ffc266e 8807 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8808 uint8_t ct = 0;
8809 uint32_t fip;
8810 uint32_t abort_tag;
8811 uint8_t command_type = ELS_COMMAND_NON_FIP;
8812 uint8_t cmnd;
8813 uint16_t xritag;
dcf2a4e0
JS
8814 uint16_t abrt_iotag;
8815 struct lpfc_iocbq *abrtiocbq;
4f774513 8816 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8817 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8818 int numBdes, i;
8819 struct ulp_bde64 bde;
c31098ce 8820 struct lpfc_nodelist *ndlp;
ff78d8f9 8821 uint32_t *pcmd;
1b51197d 8822 uint32_t if_type;
4f774513 8823
45ed1190 8824 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8825 /* The fcp commands will set command type */
0c287589 8826 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8827 command_type = FCP_COMMAND;
c868595d 8828 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8829 command_type = ELS_COMMAND_FIP;
8830 else
8831 command_type = ELS_COMMAND_NON_FIP;
8832
b5c53958
JS
8833 if (phba->fcp_embed_io)
8834 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8835 /* Some of the fields are in the right position already */
8836 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
8837 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8838 /* The ct field has moved so reset */
8839 wqe->generic.wqe_com.word7 = 0;
8840 wqe->generic.wqe_com.word10 = 0;
8841 }
b5c53958
JS
8842
8843 abort_tag = (uint32_t) iocbq->iotag;
8844 xritag = iocbq->sli4_xritag;
4f774513
JS
8845 /* words0-2 bpl convert bde */
8846 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8847 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8848 sizeof(struct ulp_bde64);
4f774513
JS
8849 bpl = (struct ulp_bde64 *)
8850 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8851 if (!bpl)
8852 return IOCB_ERROR;
cf5bf97e 8853
4f774513
JS
8854 /* Should already be byte swapped. */
8855 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8856 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8857 /* swap the size field back to the cpu so we
8858 * can assign it to the sgl.
8859 */
8860 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8861 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8862 total_len = 0;
8863 for (i = 0; i < numBdes; i++) {
8864 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8865 total_len += bde.tus.f.bdeSize;
8866 }
4f774513 8867 } else
5ffc266e 8868 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8869
4f774513
JS
8870 iocbq->iocb.ulpIoTag = iocbq->iotag;
8871 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8872
4f774513
JS
8873 switch (iocbq->iocb.ulpCommand) {
8874 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8875 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8876 ndlp = iocbq->context_un.ndlp;
8877 else
8878 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8879 if (!iocbq->iocb.ulpLe) {
8880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8881 "2007 Only Limited Edition cmd Format"
8882 " supported 0x%x\n",
8883 iocbq->iocb.ulpCommand);
8884 return IOCB_ERROR;
8885 }
ff78d8f9 8886
5ffc266e 8887 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8888 /* Els_reguest64 has a TMO */
8889 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8890 iocbq->iocb.ulpTimeout);
8891 /* Need a VF for word 4 set the vf bit*/
8892 bf_set(els_req64_vf, &wqe->els_req, 0);
8893 /* And a VFID for word 12 */
8894 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8895 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8896 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8897 iocbq->iocb.ulpContext);
8898 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8899 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8900 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8901 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8902 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8903 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8904 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8905 iocbq->context2)->virt);
1b51197d
JS
8906 if_type = bf_get(lpfc_sli_intf_if_type,
8907 &phba->sli4_hba.sli_intf);
27d6ac0a 8908 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8909 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8910 *pcmd == ELS_CMD_SCR ||
6b5151fd 8911 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8912 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8913 *pcmd == ELS_CMD_PLOGI)) {
8914 bf_set(els_req64_sp, &wqe->els_req, 1);
8915 bf_set(els_req64_sid, &wqe->els_req,
8916 iocbq->vport->fc_myDID);
939723a4
JS
8917 if ((*pcmd == ELS_CMD_FLOGI) &&
8918 !(phba->fc_topology ==
8919 LPFC_TOPOLOGY_LOOP))
8920 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8921 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8922 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8923 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8924 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8925 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8926 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8927 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8928 }
c868595d 8929 }
6d368e53
JS
8930 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8931 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8932 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8933 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8934 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8935 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8936 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8937 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8938 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8939 break;
5ffc266e 8940 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8941 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8942 iocbq->iocb.un.ulpWord[3]);
8943 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8944 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8945 /* The entire sequence is transmitted for this IOCB */
8946 xmit_len = total_len;
8947 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8948 if (phba->link_flag & LS_LOOPBACK_MODE)
8949 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8950 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8951 /* word3 iocb=io_tag32 wqe=reserved */
8952 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8953 /* word4 relative_offset memcpy */
8954 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8955 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8956 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8957 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8958 LPFC_WQE_IOD_WRITE);
8959 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8960 LPFC_WQE_LENLOC_WORD12);
8961 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8962 wqe->xmit_sequence.xmit_len = xmit_len;
8963 command_type = OTHER_COMMAND;
7851fe2c 8964 break;
4f774513 8965 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8966 /* word3 iocb=iotag32 wqe=seq_payload_len */
8967 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8968 /* word4 iocb=rsvd wqe=rsvd */
8969 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8970 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8971 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8972 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8973 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8974 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8975 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8976 LPFC_WQE_LENLOC_WORD3);
8977 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8978 break;
4f774513
JS
8979 case CMD_FCP_IWRITE64_CR:
8980 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8981 /* word3 iocb=iotag wqe=payload_offset_len */
8982 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8983 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8984 xmit_len + sizeof(struct fcp_rsp));
8985 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8986 0);
f0d9bccc
JS
8987 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8988 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8989 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8990 iocbq->iocb.ulpFCP2Rcvy);
8991 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8992 /* Always open the exchange */
f0d9bccc
JS
8993 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8994 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8995 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8996 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8997 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
8998 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8999 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
9000 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9001 if (iocbq->priority) {
9002 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9003 (iocbq->priority << 1));
9004 } else {
1ba981fd
JS
9005 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9006 (phba->cfg_XLanePriority << 1));
9007 }
9008 }
b5c53958
JS
9009 /* Note, word 10 is already initialized to 0 */
9010
9011 if (phba->fcp_embed_io) {
9012 struct lpfc_scsi_buf *lpfc_cmd;
9013 struct sli4_sge *sgl;
9014 union lpfc_wqe128 *wqe128;
9015 struct fcp_cmnd *fcp_cmnd;
9016 uint32_t *ptr;
9017
9018 /* 128 byte wqe support here */
9019 wqe128 = (union lpfc_wqe128 *)wqe;
9020
9021 lpfc_cmd = iocbq->context1;
9022 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9023 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9024
9025 /* Word 0-2 - FCP_CMND */
9026 wqe128->generic.bde.tus.f.bdeFlags =
9027 BUFF_TYPE_BDE_IMMED;
9028 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9029 wqe128->generic.bde.addrHigh = 0;
9030 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9031
9032 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
9033
9034 /* Word 22-29 FCP CMND Payload */
9035 ptr = &wqe128->words[22];
9036 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9037 }
7851fe2c 9038 break;
4f774513 9039 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
9040 /* word3 iocb=iotag wqe=payload_offset_len */
9041 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
9042 bf_set(payload_offset_len, &wqe->fcp_iread,
9043 xmit_len + sizeof(struct fcp_rsp));
9044 bf_set(cmd_buff_len, &wqe->fcp_iread,
9045 0);
f0d9bccc
JS
9046 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9047 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9048 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9049 iocbq->iocb.ulpFCP2Rcvy);
9050 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 9051 /* Always open the exchange */
f0d9bccc
JS
9052 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9053 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9054 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 9055 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 9056 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
9057 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9058 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
9059 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9060 if (iocbq->priority) {
9061 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9062 (iocbq->priority << 1));
9063 } else {
1ba981fd
JS
9064 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9065 (phba->cfg_XLanePriority << 1));
9066 }
9067 }
b5c53958
JS
9068 /* Note, word 10 is already initialized to 0 */
9069
9070 if (phba->fcp_embed_io) {
9071 struct lpfc_scsi_buf *lpfc_cmd;
9072 struct sli4_sge *sgl;
9073 union lpfc_wqe128 *wqe128;
9074 struct fcp_cmnd *fcp_cmnd;
9075 uint32_t *ptr;
9076
9077 /* 128 byte wqe support here */
9078 wqe128 = (union lpfc_wqe128 *)wqe;
9079
9080 lpfc_cmd = iocbq->context1;
9081 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9082 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9083
9084 /* Word 0-2 - FCP_CMND */
9085 wqe128->generic.bde.tus.f.bdeFlags =
9086 BUFF_TYPE_BDE_IMMED;
9087 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9088 wqe128->generic.bde.addrHigh = 0;
9089 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9090
9091 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
9092
9093 /* Word 22-29 FCP CMND Payload */
9094 ptr = &wqe128->words[22];
9095 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9096 }
7851fe2c 9097 break;
4f774513 9098 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
9099 /* word3 iocb=iotag wqe=payload_offset_len */
9100 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9101 bf_set(payload_offset_len, &wqe->fcp_icmd,
9102 xmit_len + sizeof(struct fcp_rsp));
9103 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9104 0);
f0d9bccc 9105 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 9106 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 9107 /* Always open the exchange */
f0d9bccc
JS
9108 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9109 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9110 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9111 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9112 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
9113 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9114 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
9115 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9116 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
9117 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9118 if (iocbq->priority) {
9119 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9120 (iocbq->priority << 1));
9121 } else {
1ba981fd
JS
9122 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9123 (phba->cfg_XLanePriority << 1));
9124 }
9125 }
b5c53958
JS
9126 /* Note, word 10 is already initialized to 0 */
9127
9128 if (phba->fcp_embed_io) {
9129 struct lpfc_scsi_buf *lpfc_cmd;
9130 struct sli4_sge *sgl;
9131 union lpfc_wqe128 *wqe128;
9132 struct fcp_cmnd *fcp_cmnd;
9133 uint32_t *ptr;
9134
9135 /* 128 byte wqe support here */
9136 wqe128 = (union lpfc_wqe128 *)wqe;
9137
9138 lpfc_cmd = iocbq->context1;
9139 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9140 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9141
9142 /* Word 0-2 - FCP_CMND */
9143 wqe128->generic.bde.tus.f.bdeFlags =
9144 BUFF_TYPE_BDE_IMMED;
9145 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9146 wqe128->generic.bde.addrHigh = 0;
9147 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9148
9149 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9150
9151 /* Word 22-29 FCP CMND Payload */
9152 ptr = &wqe128->words[22];
9153 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9154 }
7851fe2c 9155 break;
4f774513 9156 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9157 /* For this command calculate the xmit length of the
9158 * request bde.
9159 */
9160 xmit_len = 0;
9161 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9162 sizeof(struct ulp_bde64);
9163 for (i = 0; i < numBdes; i++) {
63e801ce 9164 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9165 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9166 break;
63e801ce
JS
9167 xmit_len += bde.tus.f.bdeSize;
9168 }
f0d9bccc
JS
9169 /* word3 iocb=IO_TAG wqe=request_payload_len */
9170 wqe->gen_req.request_payload_len = xmit_len;
9171 /* word4 iocb=parameter wqe=relative_offset memcpy */
9172 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9173 /* word6 context tag copied in memcpy */
9174 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9175 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9177 "2015 Invalid CT %x command 0x%x\n",
9178 ct, iocbq->iocb.ulpCommand);
9179 return IOCB_ERROR;
9180 }
f0d9bccc
JS
9181 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9182 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9183 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9184 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9185 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9186 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9187 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9188 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9189 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9190 command_type = OTHER_COMMAND;
7851fe2c 9191 break;
4f774513 9192 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9193 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9194 /* words0-2 BDE memcpy */
f0d9bccc
JS
9195 /* word3 iocb=iotag32 wqe=response_payload_len */
9196 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9197 /* word4 */
9198 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9199 /* word5 iocb=rsvd wge=did */
9200 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9201 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9202
9203 if_type = bf_get(lpfc_sli_intf_if_type,
9204 &phba->sli4_hba.sli_intf);
27d6ac0a 9205 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
939723a4
JS
9206 if (iocbq->vport->fc_flag & FC_PT2PT) {
9207 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9208 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9209 iocbq->vport->fc_myDID);
9210 if (iocbq->vport->fc_myDID == Fabric_DID) {
9211 bf_set(wqe_els_did,
9212 &wqe->xmit_els_rsp.wqe_dest, 0);
9213 }
9214 }
9215 }
f0d9bccc
JS
9216 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9217 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9218 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9219 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9220 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9221 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9222 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9223 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9224 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9225 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9226 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9227 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9228 LPFC_WQE_LENLOC_WORD3);
9229 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9230 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9231 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9232 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9233 iocbq->context2)->virt);
9234 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9235 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9236 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9237 iocbq->vport->fc_myDID);
939723a4
JS
9238 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9239 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9240 phba->vpi_ids[phba->pport->vpi]);
9241 }
4f774513 9242 command_type = OTHER_COMMAND;
7851fe2c 9243 break;
4f774513
JS
9244 case CMD_CLOSE_XRI_CN:
9245 case CMD_ABORT_XRI_CN:
9246 case CMD_ABORT_XRI_CX:
9247 /* words 0-2 memcpy should be 0 rserved */
9248 /* port will send abts */
dcf2a4e0
JS
9249 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9250 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9251 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9252 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9253 } else
9254 fip = 0;
9255
9256 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9257 /*
dcf2a4e0
JS
9258 * The link is down, or the command was ELS_FIP
9259 * so the fw does not need to send abts
4f774513
JS
9260 * on the wire.
9261 */
9262 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9263 else
9264 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9265 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9266 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9267 wqe->abort_cmd.rsrvd5 = 0;
9268 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9269 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9270 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9271 /*
9272 * The abort handler will send us CMD_ABORT_XRI_CN or
9273 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9274 */
f0d9bccc
JS
9275 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9276 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9277 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9278 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9279 cmnd = CMD_ABORT_XRI_CX;
9280 command_type = OTHER_COMMAND;
9281 xritag = 0;
7851fe2c 9282 break;
6669f9bb 9283 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9284 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9285 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9286 * we re-construct this WQE here based on information in
9287 * iocbq from scratch.
9288 */
9289 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9290 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9291 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9292 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9293 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9294 LPFC_ABTS_UNSOL_INT) {
9295 /* ABTS sent by initiator to CT exchange, the
9296 * RX_ID field will be filled with the newly
9297 * allocated responder XRI.
9298 */
9299 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9300 iocbq->sli4_xritag);
9301 } else {
9302 /* ABTS sent by responder to CT exchange, the
9303 * RX_ID field will be filled with the responder
9304 * RX_ID from ABTS.
9305 */
9306 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9307 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9308 }
6669f9bb
JS
9309 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9310 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9311
9312 /* Use CT=VPI */
9313 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9314 ndlp->nlp_DID);
9315 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9316 iocbq->iocb.ulpContext);
9317 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9318 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9319 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9320 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9321 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9322 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9323 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9324 command_type = OTHER_COMMAND;
546fc854
JS
9325 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9326 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9327 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9328 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9329 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9330 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9331 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9332 }
9333
7851fe2c 9334 break;
ae9e28f3
JS
9335 case CMD_SEND_FRAME:
9336 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9337 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9338 return 0;
4f774513
JS
9339 case CMD_XRI_ABORTED_CX:
9340 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9341 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9342 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9343 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9344 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9345 default:
9346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9347 "2014 Invalid command 0x%x\n",
9348 iocbq->iocb.ulpCommand);
9349 return IOCB_ERROR;
7851fe2c 9350 break;
4f774513 9351 }
6d368e53 9352
8012cc38
JS
9353 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9354 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9355 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9356 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9357 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9358 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9359 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9360 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9361 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9362 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9363 wqe->generic.wqe_com.abort_tag = abort_tag;
9364 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9365 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9366 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9367 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9368 return 0;
9369}
9370
9371/**
9372 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9373 * @phba: Pointer to HBA context object.
9374 * @ring_number: SLI ring number to issue iocb on.
9375 * @piocb: Pointer to command iocb.
9376 * @flag: Flag indicating if this command can be put into txq.
9377 *
9378 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9379 * an iocb command to an HBA with SLI-4 interface spec.
9380 *
9381 * This function is called with hbalock held. The function will return success
9382 * after it successfully submit the iocb to firmware or after adding to the
9383 * txq.
9384 **/
9385static int
9386__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9387 struct lpfc_iocbq *piocb, uint32_t flag)
9388{
9389 struct lpfc_sglq *sglq;
b5c53958
JS
9390 union lpfc_wqe *wqe;
9391 union lpfc_wqe128 wqe128;
1ba981fd 9392 struct lpfc_queue *wq;
895427bd 9393 struct lpfc_sli_ring *pring;
4f774513 9394
895427bd
JS
9395 /* Get the WQ */
9396 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9397 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9398 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9399 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9400 else
9401 wq = phba->sli4_hba.oas_wq;
9402 } else {
9403 wq = phba->sli4_hba.els_wq;
9404 }
9405
9406 /* Get corresponding ring */
9407 pring = wq->pring;
1c2ba475 9408
b5c53958
JS
9409 /*
9410 * The WQE can be either 64 or 128 bytes,
9411 * so allocate space on the stack assuming the largest.
9412 */
9413 wqe = (union lpfc_wqe *)&wqe128;
9414
895427bd
JS
9415 lockdep_assert_held(&phba->hbalock);
9416
4f774513
JS
9417 if (piocb->sli4_xritag == NO_XRI) {
9418 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9419 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9420 sglq = NULL;
9421 else {
0e9bb8d7 9422 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9423 if (!(flag & SLI_IOCB_RET_IOCB)) {
9424 __lpfc_sli_ringtx_put(phba,
9425 pring, piocb);
9426 return IOCB_SUCCESS;
9427 } else {
9428 return IOCB_BUSY;
9429 }
9430 } else {
895427bd 9431 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9432 if (!sglq) {
9433 if (!(flag & SLI_IOCB_RET_IOCB)) {
9434 __lpfc_sli_ringtx_put(phba,
9435 pring,
9436 piocb);
9437 return IOCB_SUCCESS;
9438 } else
9439 return IOCB_BUSY;
9440 }
9441 }
4f774513 9442 }
2ea259ee 9443 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9444 /* These IO's already have an XRI and a mapped sgl. */
9445 sglq = NULL;
2ea259ee 9446 else {
6d368e53
JS
9447 /*
9448 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9449 * sglq is on the active list
9450 */
edccdc17 9451 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9452 if (!sglq)
9453 return IOCB_ERROR;
9454 }
9455
9456 if (sglq) {
6d368e53 9457 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9458 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9459 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9460 return IOCB_ERROR;
9461 }
9462
b5c53958 9463 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
4f774513
JS
9464 return IOCB_ERROR;
9465
895427bd
JS
9466 if (lpfc_sli4_wq_put(wq, wqe))
9467 return IOCB_ERROR;
4f774513
JS
9468 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9469
9470 return 0;
9471}
9472
9473/**
9474 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9475 *
9476 * This routine wraps the actual lockless version for issusing IOCB function
9477 * pointer from the lpfc_hba struct.
9478 *
9479 * Return codes:
b5c53958
JS
9480 * IOCB_ERROR - Error
9481 * IOCB_SUCCESS - Success
9482 * IOCB_BUSY - Busy
4f774513 9483 **/
2a9bf3d0 9484int
4f774513
JS
9485__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9486 struct lpfc_iocbq *piocb, uint32_t flag)
9487{
9488 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9489}
9490
9491/**
25985edc 9492 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9493 * @phba: The hba struct for which this call is being executed.
9494 * @dev_grp: The HBA PCI-Device group number.
9495 *
9496 * This routine sets up the SLI interface API function jump table in @phba
9497 * struct.
9498 * Returns: 0 - success, -ENODEV - failure.
9499 **/
9500int
9501lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9502{
9503
9504 switch (dev_grp) {
9505 case LPFC_PCI_DEV_LP:
9506 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9507 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9508 break;
9509 case LPFC_PCI_DEV_OC:
9510 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9511 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9512 break;
9513 default:
9514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9515 "1419 Invalid HBA PCI-device group: 0x%x\n",
9516 dev_grp);
9517 return -ENODEV;
9518 break;
9519 }
9520 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9521 return 0;
9522}
9523
a1efe163 9524/**
895427bd 9525 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9526 * @phba: Pointer to HBA context object.
a1efe163
JS
9527 * @piocb: Pointer to command iocb.
9528 *
895427bd
JS
9529 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9530 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9531 * Since ABORTS must go on the same WQ of the command they are
895427bd 9532 * aborting, we use command's hba_wqidx.
a1efe163 9533 */
895427bd
JS
9534struct lpfc_sli_ring *
9535lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9536{
895427bd 9537 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9538 if (!(phba->cfg_fof) ||
895427bd 9539 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9540 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9541 return NULL;
8b0dff14 9542 /*
895427bd 9543 * for abort iocb hba_wqidx should already
8b0dff14
JS
9544 * be setup based on what work queue we used.
9545 */
8e036a94 9546 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9547 piocb->hba_wqidx =
8b0dff14
JS
9548 lpfc_sli4_scmd_to_wqidx_distr(phba,
9549 piocb->context1);
8e036a94
DK
9550 piocb->hba_wqidx = piocb->hba_wqidx %
9551 phba->cfg_fcp_io_channel;
9552 }
895427bd 9553 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9554 } else {
9555 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9556 return NULL;
9557 piocb->hba_wqidx = 0;
9558 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9559 }
895427bd
JS
9560 } else {
9561 if (unlikely(!phba->sli4_hba.els_wq))
9562 return NULL;
9563 piocb->hba_wqidx = 0;
9564 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9565 }
9bd2bff5
JS
9566}
9567
4f774513
JS
9568/**
9569 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9570 * @phba: Pointer to HBA context object.
9571 * @pring: Pointer to driver SLI ring object.
9572 * @piocb: Pointer to command iocb.
9573 * @flag: Flag indicating if this command can be put into txq.
9574 *
9575 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9576 * function. This function gets the hbalock and calls
9577 * __lpfc_sli_issue_iocb function and will return the error returned
9578 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9579 * functions which do not hold hbalock.
9580 **/
9581int
9582lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9583 struct lpfc_iocbq *piocb, uint32_t flag)
9584{
895427bd 9585 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9586 struct lpfc_sli_ring *pring;
ba20c853
JS
9587 struct lpfc_queue *fpeq;
9588 struct lpfc_eqe *eqe;
4f774513 9589 unsigned long iflags;
2a76a283 9590 int rc, idx;
4f774513 9591
7e56aa25 9592 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9593 pring = lpfc_sli4_calc_ring(phba, piocb);
9594 if (unlikely(pring == NULL))
9bd2bff5 9595 return IOCB_ERROR;
ba20c853 9596
9bd2bff5
JS
9597 spin_lock_irqsave(&pring->ring_lock, iflags);
9598 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9599 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9600
9bd2bff5 9601 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9602 idx = piocb->hba_wqidx;
9603 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9604
895427bd 9605 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9606
9bd2bff5
JS
9607 /* Get associated EQ with this index */
9608 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9609
9bd2bff5 9610 /* Turn off interrupts from this EQ */
b71413dd 9611 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 9612
9bd2bff5
JS
9613 /*
9614 * Process all the events on FCP EQ
9615 */
9616 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9617 lpfc_sli4_hba_handle_eqe(phba,
9618 eqe, idx);
9619 fpeq->EQ_processed++;
ba20c853 9620 }
ba20c853 9621
9bd2bff5 9622 /* Always clear and re-arm the EQ */
b71413dd 9623 phba->sli4_hba.sli4_eq_release(fpeq,
9bd2bff5
JS
9624 LPFC_QUEUE_REARM);
9625 }
895427bd 9626 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9627 }
7e56aa25
JS
9628 } else {
9629 /* For now, SLI2/3 will still use hbalock */
9630 spin_lock_irqsave(&phba->hbalock, iflags);
9631 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9632 spin_unlock_irqrestore(&phba->hbalock, iflags);
9633 }
4f774513
JS
9634 return rc;
9635}
9636
9637/**
9638 * lpfc_extra_ring_setup - Extra ring setup function
9639 * @phba: Pointer to HBA context object.
9640 *
9641 * This function is called while driver attaches with the
9642 * HBA to setup the extra ring. The extra ring is used
9643 * only when driver needs to support target mode functionality
9644 * or IP over FC functionalities.
9645 *
895427bd 9646 * This function is called with no lock held. SLI3 only.
4f774513
JS
9647 **/
9648static int
9649lpfc_extra_ring_setup( struct lpfc_hba *phba)
9650{
9651 struct lpfc_sli *psli;
9652 struct lpfc_sli_ring *pring;
9653
9654 psli = &phba->sli;
9655
9656 /* Adjust cmd/rsp ring iocb entries more evenly */
9657
9658 /* Take some away from the FCP ring */
895427bd 9659 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9660 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9661 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9662 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9663 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9664
a4bc3379 9665 /* and give them to the extra ring */
895427bd 9666 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9667
7e56aa25
JS
9668 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9669 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9670 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9671 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9672
9673 /* Setup default profile for this ring */
9674 pring->iotag_max = 4096;
9675 pring->num_mask = 1;
9676 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9677 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9678 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9679 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9680 return 0;
9681}
9682
cb69f7de
JS
9683/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9684 * @phba: Pointer to HBA context object.
9685 * @iocbq: Pointer to iocb object.
9686 *
9687 * The async_event handler calls this routine when it receives
9688 * an ASYNC_STATUS_CN event from the port. The port generates
9689 * this event when an Abort Sequence request to an rport fails
9690 * twice in succession. The abort could be originated by the
9691 * driver or by the port. The ABTS could have been for an ELS
9692 * or FCP IO. The port only generates this event when an ABTS
9693 * fails to complete after one retry.
9694 */
9695static void
9696lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9697 struct lpfc_iocbq *iocbq)
9698{
9699 struct lpfc_nodelist *ndlp = NULL;
9700 uint16_t rpi = 0, vpi = 0;
9701 struct lpfc_vport *vport = NULL;
9702
9703 /* The rpi in the ulpContext is vport-sensitive. */
9704 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9705 rpi = iocbq->iocb.ulpContext;
9706
9707 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9708 "3092 Port generated ABTS async event "
9709 "on vpi %d rpi %d status 0x%x\n",
9710 vpi, rpi, iocbq->iocb.ulpStatus);
9711
9712 vport = lpfc_find_vport_by_vpid(phba, vpi);
9713 if (!vport)
9714 goto err_exit;
9715 ndlp = lpfc_findnode_rpi(vport, rpi);
9716 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9717 goto err_exit;
9718
9719 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9720 lpfc_sli_abts_recover_port(vport, ndlp);
9721 return;
9722
9723 err_exit:
9724 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9725 "3095 Event Context not found, no "
9726 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9727 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9728 vpi, rpi);
9729}
9730
9731/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9732 * @phba: pointer to HBA context object.
9733 * @ndlp: nodelist pointer for the impacted rport.
9734 * @axri: pointer to the wcqe containing the failed exchange.
9735 *
9736 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9737 * port. The port generates this event when an abort exchange request to an
9738 * rport fails twice in succession with no reply. The abort could be originated
9739 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9740 */
9741void
9742lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9743 struct lpfc_nodelist *ndlp,
9744 struct sli4_wcqe_xri_aborted *axri)
9745{
9746 struct lpfc_vport *vport;
5c1db2ac 9747 uint32_t ext_status = 0;
cb69f7de 9748
6b5151fd 9749 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9750 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9751 "3115 Node Context not found, driver "
9752 "ignoring abts err event\n");
6b5151fd
JS
9753 return;
9754 }
9755
cb69f7de
JS
9756 vport = ndlp->vport;
9757 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9758 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9759 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9760 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9761 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9762 bf_get(lpfc_wcqe_xa_status, axri),
9763 axri->parameter);
cb69f7de 9764
5c1db2ac
JS
9765 /*
9766 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9767 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9768 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9769 */
e3d2b802 9770 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9771 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9772 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9773 lpfc_sli_abts_recover_port(vport, ndlp);
9774}
9775
e59058c4 9776/**
3621a710 9777 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9778 * @phba: Pointer to HBA context object.
9779 * @pring: Pointer to driver SLI ring object.
9780 * @iocbq: Pointer to iocb object.
9781 *
9782 * This function is called by the slow ring event handler
9783 * function when there is an ASYNC event iocb in the ring.
9784 * This function is called with no lock held.
9785 * Currently this function handles only temperature related
9786 * ASYNC events. The function decodes the temperature sensor
9787 * event message and posts events for the management applications.
9788 **/
98c9ea5c 9789static void
57127f15
JS
9790lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9791 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9792{
9793 IOCB_t *icmd;
9794 uint16_t evt_code;
57127f15
JS
9795 struct temp_event temp_event_data;
9796 struct Scsi_Host *shost;
a257bf90 9797 uint32_t *iocb_w;
57127f15
JS
9798
9799 icmd = &iocbq->iocb;
9800 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9801
cb69f7de
JS
9802 switch (evt_code) {
9803 case ASYNC_TEMP_WARN:
9804 case ASYNC_TEMP_SAFE:
9805 temp_event_data.data = (uint32_t) icmd->ulpContext;
9806 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9807 if (evt_code == ASYNC_TEMP_WARN) {
9808 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9809 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9810 "0347 Adapter is very hot, please take "
9811 "corrective action. temperature : %d Celsius\n",
9812 (uint32_t) icmd->ulpContext);
9813 } else {
9814 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9815 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9816 "0340 Adapter temperature is OK now. "
9817 "temperature : %d Celsius\n",
9818 (uint32_t) icmd->ulpContext);
9819 }
9820
9821 /* Send temperature change event to applications */
9822 shost = lpfc_shost_from_vport(phba->pport);
9823 fc_host_post_vendor_event(shost, fc_get_event_number(),
9824 sizeof(temp_event_data), (char *) &temp_event_data,
9825 LPFC_NL_VENDOR_ID);
9826 break;
9827 case ASYNC_STATUS_CN:
9828 lpfc_sli_abts_err_handler(phba, iocbq);
9829 break;
9830 default:
a257bf90 9831 iocb_w = (uint32_t *) icmd;
cb69f7de 9832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9833 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9834 " evt_code 0x%x\n"
a257bf90
JS
9835 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9836 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9837 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9838 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9839 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9840 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9841 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9842 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9843 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9844
cb69f7de 9845 break;
57127f15 9846 }
57127f15
JS
9847}
9848
9849
e59058c4 9850/**
895427bd 9851 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9852 * @phba: Pointer to HBA context object.
9853 *
9854 * lpfc_sli_setup sets up rings of the SLI interface with
9855 * number of iocbs per ring and iotags. This function is
9856 * called while driver attach to the HBA and before the
9857 * interrupts are enabled. So there is no need for locking.
9858 *
9859 * This function always returns 0.
9860 **/
dea3101e 9861int
895427bd
JS
9862lpfc_sli4_setup(struct lpfc_hba *phba)
9863{
9864 struct lpfc_sli_ring *pring;
9865
9866 pring = phba->sli4_hba.els_wq->pring;
9867 pring->num_mask = LPFC_MAX_RING_MASK;
9868 pring->prt[0].profile = 0; /* Mask 0 */
9869 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9870 pring->prt[0].type = FC_TYPE_ELS;
9871 pring->prt[0].lpfc_sli_rcv_unsol_event =
9872 lpfc_els_unsol_event;
9873 pring->prt[1].profile = 0; /* Mask 1 */
9874 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9875 pring->prt[1].type = FC_TYPE_ELS;
9876 pring->prt[1].lpfc_sli_rcv_unsol_event =
9877 lpfc_els_unsol_event;
9878 pring->prt[2].profile = 0; /* Mask 2 */
9879 /* NameServer Inquiry */
9880 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9881 /* NameServer */
9882 pring->prt[2].type = FC_TYPE_CT;
9883 pring->prt[2].lpfc_sli_rcv_unsol_event =
9884 lpfc_ct_unsol_event;
9885 pring->prt[3].profile = 0; /* Mask 3 */
9886 /* NameServer response */
9887 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9888 /* NameServer */
9889 pring->prt[3].type = FC_TYPE_CT;
9890 pring->prt[3].lpfc_sli_rcv_unsol_event =
9891 lpfc_ct_unsol_event;
9892 return 0;
9893}
9894
9895/**
9896 * lpfc_sli_setup - SLI ring setup function
9897 * @phba: Pointer to HBA context object.
9898 *
9899 * lpfc_sli_setup sets up rings of the SLI interface with
9900 * number of iocbs per ring and iotags. This function is
9901 * called while driver attach to the HBA and before the
9902 * interrupts are enabled. So there is no need for locking.
9903 *
9904 * This function always returns 0. SLI3 only.
9905 **/
9906int
dea3101e
JB
9907lpfc_sli_setup(struct lpfc_hba *phba)
9908{
ed957684 9909 int i, totiocbsize = 0;
dea3101e
JB
9910 struct lpfc_sli *psli = &phba->sli;
9911 struct lpfc_sli_ring *pring;
9912
2a76a283 9913 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 9914 psli->sli_flag = 0;
dea3101e 9915
604a3e30
JB
9916 psli->iocbq_lookup = NULL;
9917 psli->iocbq_lookup_len = 0;
9918 psli->last_iotag = 0;
9919
dea3101e 9920 for (i = 0; i < psli->num_rings; i++) {
895427bd 9921 pring = &psli->sli3_ring[i];
dea3101e
JB
9922 switch (i) {
9923 case LPFC_FCP_RING: /* ring 0 - FCP */
9924 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9925 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9926 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9927 pring->sli.sli3.numCiocb +=
9928 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9929 pring->sli.sli3.numRiocb +=
9930 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9931 pring->sli.sli3.numCiocb +=
9932 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9933 pring->sli.sli3.numRiocb +=
9934 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9935 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9936 SLI3_IOCB_CMD_SIZE :
9937 SLI2_IOCB_CMD_SIZE;
7e56aa25 9938 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9939 SLI3_IOCB_RSP_SIZE :
9940 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9941 pring->iotag_ctr = 0;
9942 pring->iotag_max =
92d7f7b0 9943 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
9944 pring->fast_iotag = pring->iotag_max;
9945 pring->num_mask = 0;
9946 break;
a4bc3379 9947 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 9948 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9949 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9950 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9951 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9952 SLI3_IOCB_CMD_SIZE :
9953 SLI2_IOCB_CMD_SIZE;
7e56aa25 9954 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9955 SLI3_IOCB_RSP_SIZE :
9956 SLI2_IOCB_RSP_SIZE;
2e0fef85 9957 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9958 pring->num_mask = 0;
9959 break;
9960 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9961 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9962 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9963 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9964 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9965 SLI3_IOCB_CMD_SIZE :
9966 SLI2_IOCB_CMD_SIZE;
7e56aa25 9967 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9968 SLI3_IOCB_RSP_SIZE :
9969 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9970 pring->fast_iotag = 0;
9971 pring->iotag_ctr = 0;
9972 pring->iotag_max = 4096;
57127f15
JS
9973 pring->lpfc_sli_rcv_async_status =
9974 lpfc_sli_async_event_handler;
6669f9bb 9975 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9976 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9977 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9978 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9979 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9980 lpfc_els_unsol_event;
dea3101e 9981 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9982 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9983 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9984 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9985 lpfc_els_unsol_event;
dea3101e
JB
9986 pring->prt[2].profile = 0; /* Mask 2 */
9987 /* NameServer Inquiry */
6a9c52cf 9988 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9989 /* NameServer */
6a9c52cf 9990 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9991 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9992 lpfc_ct_unsol_event;
dea3101e
JB
9993 pring->prt[3].profile = 0; /* Mask 3 */
9994 /* NameServer response */
6a9c52cf 9995 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9996 /* NameServer */
6a9c52cf 9997 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9998 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9999 lpfc_ct_unsol_event;
dea3101e
JB
10000 break;
10001 }
7e56aa25
JS
10002 totiocbsize += (pring->sli.sli3.numCiocb *
10003 pring->sli.sli3.sizeCiocb) +
10004 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 10005 }
ed957684 10006 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 10007 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
10008 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10009 "SLI2 SLIM Data: x%x x%lx\n",
10010 phba->brd_no, totiocbsize,
10011 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 10012 }
cf5bf97e
JW
10013 if (phba->cfg_multi_ring_support == 2)
10014 lpfc_extra_ring_setup(phba);
dea3101e
JB
10015
10016 return 0;
10017}
10018
e59058c4 10019/**
895427bd 10020 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
10021 * @phba: Pointer to HBA context object.
10022 *
895427bd 10023 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
10024 * ring. This function also initializes ring indices of each ring.
10025 * This function is called during the initialization of the SLI
10026 * interface of an HBA.
10027 * This function is called with no lock held and always returns
10028 * 1.
10029 **/
895427bd
JS
10030void
10031lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10032{
10033 struct lpfc_sli *psli;
10034 struct lpfc_sli_ring *pring;
604a3e30 10035 int i;
dea3101e
JB
10036
10037 psli = &phba->sli;
2e0fef85 10038 spin_lock_irq(&phba->hbalock);
dea3101e 10039 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10040 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 10041 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
10042 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10043 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 10044 pring->flag = 0;
895427bd 10045 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
10046 INIT_LIST_HEAD(&pring->txq);
10047 INIT_LIST_HEAD(&pring->txcmplq);
10048 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 10049 spin_lock_init(&pring->ring_lock);
dea3101e 10050 }
895427bd
JS
10051 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10052 pring = phba->sli4_hba.nvme_wq[i]->pring;
10053 pring->flag = 0;
10054 pring->ringno = LPFC_FCP_RING;
10055 INIT_LIST_HEAD(&pring->txq);
10056 INIT_LIST_HEAD(&pring->txcmplq);
10057 INIT_LIST_HEAD(&pring->iocb_continueq);
10058 spin_lock_init(&pring->ring_lock);
10059 }
10060 pring = phba->sli4_hba.els_wq->pring;
10061 pring->flag = 0;
10062 pring->ringno = LPFC_ELS_RING;
10063 INIT_LIST_HEAD(&pring->txq);
10064 INIT_LIST_HEAD(&pring->txcmplq);
10065 INIT_LIST_HEAD(&pring->iocb_continueq);
10066 spin_lock_init(&pring->ring_lock);
dea3101e 10067
895427bd
JS
10068 if (phba->cfg_nvme_io_channel) {
10069 pring = phba->sli4_hba.nvmels_wq->pring;
10070 pring->flag = 0;
10071 pring->ringno = LPFC_ELS_RING;
10072 INIT_LIST_HEAD(&pring->txq);
10073 INIT_LIST_HEAD(&pring->txcmplq);
10074 INIT_LIST_HEAD(&pring->iocb_continueq);
10075 spin_lock_init(&pring->ring_lock);
10076 }
10077
10078 if (phba->cfg_fof) {
10079 pring = phba->sli4_hba.oas_wq->pring;
10080 pring->flag = 0;
10081 pring->ringno = LPFC_FCP_RING;
10082 INIT_LIST_HEAD(&pring->txq);
10083 INIT_LIST_HEAD(&pring->txcmplq);
10084 INIT_LIST_HEAD(&pring->iocb_continueq);
10085 spin_lock_init(&pring->ring_lock);
10086 }
10087
10088 spin_unlock_irq(&phba->hbalock);
10089}
10090
10091/**
10092 * lpfc_sli_queue_init - Queue initialization function
10093 * @phba: Pointer to HBA context object.
10094 *
10095 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10096 * ring. This function also initializes ring indices of each ring.
10097 * This function is called during the initialization of the SLI
10098 * interface of an HBA.
10099 * This function is called with no lock held and always returns
10100 * 1.
10101 **/
10102void
10103lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e
JB
10104{
10105 struct lpfc_sli *psli;
10106 struct lpfc_sli_ring *pring;
604a3e30 10107 int i;
dea3101e
JB
10108
10109 psli = &phba->sli;
2e0fef85 10110 spin_lock_irq(&phba->hbalock);
dea3101e 10111 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 10112 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
10113 /* Initialize list headers for txq and txcmplq as double linked lists */
10114 for (i = 0; i < psli->num_rings; i++) {
895427bd 10115 pring = &psli->sli3_ring[i];
dea3101e 10116 pring->ringno = i;
7e56aa25
JS
10117 pring->sli.sli3.next_cmdidx = 0;
10118 pring->sli.sli3.local_getidx = 0;
10119 pring->sli.sli3.cmdidx = 0;
dea3101e 10120 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 10121 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 10122 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
10123 pring->flag = 0;
10124 INIT_LIST_HEAD(&pring->txq);
10125 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 10126 spin_lock_init(&pring->ring_lock);
dea3101e 10127 }
2e0fef85 10128 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10129}
10130
04c68496
JS
10131/**
10132 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10133 * @phba: Pointer to HBA context object.
10134 *
10135 * This routine flushes the mailbox command subsystem. It will unconditionally
10136 * flush all the mailbox commands in the three possible stages in the mailbox
10137 * command sub-system: pending mailbox command queue; the outstanding mailbox
10138 * command; and completed mailbox command queue. It is caller's responsibility
10139 * to make sure that the driver is in the proper state to flush the mailbox
10140 * command sub-system. Namely, the posting of mailbox commands into the
10141 * pending mailbox command queue from the various clients must be stopped;
10142 * either the HBA is in a state that it will never works on the outstanding
10143 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10144 * mailbox command has been completed.
10145 **/
10146static void
10147lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10148{
10149 LIST_HEAD(completions);
10150 struct lpfc_sli *psli = &phba->sli;
10151 LPFC_MBOXQ_t *pmb;
10152 unsigned long iflag;
10153
10154 /* Flush all the mailbox commands in the mbox system */
10155 spin_lock_irqsave(&phba->hbalock, iflag);
10156 /* The pending mailbox command queue */
10157 list_splice_init(&phba->sli.mboxq, &completions);
10158 /* The outstanding active mailbox command */
10159 if (psli->mbox_active) {
10160 list_add_tail(&psli->mbox_active->list, &completions);
10161 psli->mbox_active = NULL;
10162 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10163 }
10164 /* The completed mailbox command queue */
10165 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10166 spin_unlock_irqrestore(&phba->hbalock, iflag);
10167
10168 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10169 while (!list_empty(&completions)) {
10170 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10171 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10172 if (pmb->mbox_cmpl)
10173 pmb->mbox_cmpl(phba, pmb);
10174 }
10175}
10176
e59058c4 10177/**
3621a710 10178 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10179 * @vport: Pointer to virtual port object.
10180 *
10181 * lpfc_sli_host_down is called to clean up the resources
10182 * associated with a vport before destroying virtual
10183 * port data structures.
10184 * This function does following operations:
10185 * - Free discovery resources associated with this virtual
10186 * port.
10187 * - Free iocbs associated with this virtual port in
10188 * the txq.
10189 * - Send abort for all iocb commands associated with this
10190 * vport in txcmplq.
10191 *
10192 * This function is called with no lock held and always returns 1.
10193 **/
92d7f7b0
JS
10194int
10195lpfc_sli_host_down(struct lpfc_vport *vport)
10196{
858c9f6c 10197 LIST_HEAD(completions);
92d7f7b0
JS
10198 struct lpfc_hba *phba = vport->phba;
10199 struct lpfc_sli *psli = &phba->sli;
895427bd 10200 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10201 struct lpfc_sli_ring *pring;
10202 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10203 int i;
10204 unsigned long flags = 0;
10205 uint16_t prev_pring_flag;
10206
10207 lpfc_cleanup_discovery_resources(vport);
10208
10209 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10210
895427bd
JS
10211 /*
10212 * Error everything on the txq since these iocbs
10213 * have not been given to the FW yet.
10214 * Also issue ABTS for everything on the txcmplq
10215 */
10216 if (phba->sli_rev != LPFC_SLI_REV4) {
10217 for (i = 0; i < psli->num_rings; i++) {
10218 pring = &psli->sli3_ring[i];
10219 prev_pring_flag = pring->flag;
10220 /* Only slow rings */
10221 if (pring->ringno == LPFC_ELS_RING) {
10222 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10223 /* Set the lpfc data pending flag */
10224 set_bit(LPFC_DATA_READY, &phba->data_flags);
10225 }
10226 list_for_each_entry_safe(iocb, next_iocb,
10227 &pring->txq, list) {
10228 if (iocb->vport != vport)
10229 continue;
10230 list_move_tail(&iocb->list, &completions);
10231 }
10232 list_for_each_entry_safe(iocb, next_iocb,
10233 &pring->txcmplq, list) {
10234 if (iocb->vport != vport)
10235 continue;
10236 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10237 }
10238 pring->flag = prev_pring_flag;
10239 }
10240 } else {
10241 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10242 pring = qp->pring;
10243 if (!pring)
92d7f7b0 10244 continue;
895427bd
JS
10245 if (pring == phba->sli4_hba.els_wq->pring) {
10246 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10247 /* Set the lpfc data pending flag */
10248 set_bit(LPFC_DATA_READY, &phba->data_flags);
10249 }
10250 prev_pring_flag = pring->flag;
10251 spin_lock_irq(&pring->ring_lock);
10252 list_for_each_entry_safe(iocb, next_iocb,
10253 &pring->txq, list) {
10254 if (iocb->vport != vport)
10255 continue;
10256 list_move_tail(&iocb->list, &completions);
10257 }
10258 spin_unlock_irq(&pring->ring_lock);
10259 list_for_each_entry_safe(iocb, next_iocb,
10260 &pring->txcmplq, list) {
10261 if (iocb->vport != vport)
10262 continue;
10263 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10264 }
10265 pring->flag = prev_pring_flag;
92d7f7b0 10266 }
92d7f7b0 10267 }
92d7f7b0
JS
10268 spin_unlock_irqrestore(&phba->hbalock, flags);
10269
a257bf90
JS
10270 /* Cancel all the IOCBs from the completions list */
10271 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10272 IOERR_SLI_DOWN);
92d7f7b0
JS
10273 return 1;
10274}
10275
e59058c4 10276/**
3621a710 10277 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10278 * @phba: Pointer to HBA context object.
10279 *
10280 * This function cleans up all iocb, buffers, mailbox commands
10281 * while shutting down the HBA. This function is called with no
10282 * lock held and always returns 1.
10283 * This function does the following to cleanup driver resources:
10284 * - Free discovery resources for each virtual port
10285 * - Cleanup any pending fabric iocbs
10286 * - Iterate through the iocb txq and free each entry
10287 * in the list.
10288 * - Free up any buffer posted to the HBA
10289 * - Free mailbox commands in the mailbox queue.
10290 **/
dea3101e 10291int
2e0fef85 10292lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10293{
2534ba75 10294 LIST_HEAD(completions);
2e0fef85 10295 struct lpfc_sli *psli = &phba->sli;
895427bd 10296 struct lpfc_queue *qp = NULL;
dea3101e 10297 struct lpfc_sli_ring *pring;
0ff10d46 10298 struct lpfc_dmabuf *buf_ptr;
dea3101e 10299 unsigned long flags = 0;
04c68496
JS
10300 int i;
10301
10302 /* Shutdown the mailbox command sub-system */
618a5230 10303 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10304
dea3101e
JB
10305 lpfc_hba_down_prep(phba);
10306
92d7f7b0
JS
10307 lpfc_fabric_abort_hba(phba);
10308
2e0fef85 10309 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10310
895427bd
JS
10311 /*
10312 * Error everything on the txq since these iocbs
10313 * have not been given to the FW yet.
10314 */
10315 if (phba->sli_rev != LPFC_SLI_REV4) {
10316 for (i = 0; i < psli->num_rings; i++) {
10317 pring = &psli->sli3_ring[i];
10318 /* Only slow rings */
10319 if (pring->ringno == LPFC_ELS_RING) {
10320 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10321 /* Set the lpfc data pending flag */
10322 set_bit(LPFC_DATA_READY, &phba->data_flags);
10323 }
10324 list_splice_init(&pring->txq, &completions);
10325 }
10326 } else {
10327 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10328 pring = qp->pring;
10329 if (!pring)
10330 continue;
10331 spin_lock_irq(&pring->ring_lock);
10332 list_splice_init(&pring->txq, &completions);
10333 spin_unlock_irq(&pring->ring_lock);
10334 if (pring == phba->sli4_hba.els_wq->pring) {
10335 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10336 /* Set the lpfc data pending flag */
10337 set_bit(LPFC_DATA_READY, &phba->data_flags);
10338 }
10339 }
2534ba75 10340 }
2e0fef85 10341 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10342
a257bf90
JS
10343 /* Cancel all the IOCBs from the completions list */
10344 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10345 IOERR_SLI_DOWN);
dea3101e 10346
0ff10d46
JS
10347 spin_lock_irqsave(&phba->hbalock, flags);
10348 list_splice_init(&phba->elsbuf, &completions);
10349 phba->elsbuf_cnt = 0;
10350 phba->elsbuf_prev_cnt = 0;
10351 spin_unlock_irqrestore(&phba->hbalock, flags);
10352
10353 while (!list_empty(&completions)) {
10354 list_remove_head(&completions, buf_ptr,
10355 struct lpfc_dmabuf, list);
10356 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10357 kfree(buf_ptr);
10358 }
10359
dea3101e
JB
10360 /* Return any active mbox cmds */
10361 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10362
da0436e9 10363 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10364 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10365 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10366
da0436e9
JS
10367 return 1;
10368}
10369
e59058c4 10370/**
3621a710 10371 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10372 * @srcp: Source memory pointer.
10373 * @destp: Destination memory pointer.
10374 * @cnt: Number of words required to be copied.
10375 *
10376 * This function is used for copying data between driver memory
10377 * and the SLI memory. This function also changes the endianness
10378 * of each word if native endianness is different from SLI
10379 * endianness. This function can be called with or without
10380 * lock.
10381 **/
dea3101e
JB
10382void
10383lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10384{
10385 uint32_t *src = srcp;
10386 uint32_t *dest = destp;
10387 uint32_t ldata;
10388 int i;
10389
10390 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10391 ldata = *src;
10392 ldata = le32_to_cpu(ldata);
10393 *dest = ldata;
10394 src++;
10395 dest++;
10396 }
10397}
10398
e59058c4 10399
a0c87cbd
JS
10400/**
10401 * lpfc_sli_bemem_bcopy - SLI memory copy function
10402 * @srcp: Source memory pointer.
10403 * @destp: Destination memory pointer.
10404 * @cnt: Number of words required to be copied.
10405 *
10406 * This function is used for copying data between a data structure
10407 * with big endian representation to local endianness.
10408 * This function can be called with or without lock.
10409 **/
10410void
10411lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10412{
10413 uint32_t *src = srcp;
10414 uint32_t *dest = destp;
10415 uint32_t ldata;
10416 int i;
10417
10418 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10419 ldata = *src;
10420 ldata = be32_to_cpu(ldata);
10421 *dest = ldata;
10422 src++;
10423 dest++;
10424 }
10425}
10426
e59058c4 10427/**
3621a710 10428 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10429 * @phba: Pointer to HBA context object.
10430 * @pring: Pointer to driver SLI ring object.
10431 * @mp: Pointer to driver buffer object.
10432 *
10433 * This function is called with no lock held.
10434 * It always return zero after adding the buffer to the postbufq
10435 * buffer list.
10436 **/
dea3101e 10437int
2e0fef85
JS
10438lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10439 struct lpfc_dmabuf *mp)
dea3101e
JB
10440{
10441 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10442 later */
2e0fef85 10443 spin_lock_irq(&phba->hbalock);
dea3101e 10444 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10445 pring->postbufq_cnt++;
2e0fef85 10446 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10447 return 0;
10448}
10449
e59058c4 10450/**
3621a710 10451 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10452 * @phba: Pointer to HBA context object.
10453 *
10454 * When HBQ is enabled, buffers are searched based on tags. This function
10455 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10456 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10457 * does not conflict with tags of buffer posted for unsolicited events.
10458 * The function returns the allocated tag. The function is called with
10459 * no locks held.
10460 **/
76bb24ef
JS
10461uint32_t
10462lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10463{
10464 spin_lock_irq(&phba->hbalock);
10465 phba->buffer_tag_count++;
10466 /*
10467 * Always set the QUE_BUFTAG_BIT to distiguish between
10468 * a tag assigned by HBQ.
10469 */
10470 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10471 spin_unlock_irq(&phba->hbalock);
10472 return phba->buffer_tag_count;
10473}
10474
e59058c4 10475/**
3621a710 10476 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10477 * @phba: Pointer to HBA context object.
10478 * @pring: Pointer to driver SLI ring object.
10479 * @tag: Buffer tag.
10480 *
10481 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10482 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10483 * iocb is posted to the response ring with the tag of the buffer.
10484 * This function searches the pring->postbufq list using the tag
10485 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10486 * iocb. If the buffer is found then lpfc_dmabuf object of the
10487 * buffer is returned to the caller else NULL is returned.
10488 * This function is called with no lock held.
10489 **/
76bb24ef
JS
10490struct lpfc_dmabuf *
10491lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10492 uint32_t tag)
10493{
10494 struct lpfc_dmabuf *mp, *next_mp;
10495 struct list_head *slp = &pring->postbufq;
10496
25985edc 10497 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10498 spin_lock_irq(&phba->hbalock);
10499 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10500 if (mp->buffer_tag == tag) {
10501 list_del_init(&mp->list);
10502 pring->postbufq_cnt--;
10503 spin_unlock_irq(&phba->hbalock);
10504 return mp;
10505 }
10506 }
10507
10508 spin_unlock_irq(&phba->hbalock);
10509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10510 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10511 "ring %d Data x%lx x%p x%p x%x\n",
10512 pring->ringno, (unsigned long) tag,
10513 slp->next, slp->prev, pring->postbufq_cnt);
10514
10515 return NULL;
10516}
dea3101e 10517
e59058c4 10518/**
3621a710 10519 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10520 * @phba: Pointer to HBA context object.
10521 * @pring: Pointer to driver SLI ring object.
10522 * @phys: DMA address of the buffer.
10523 *
10524 * This function searches the buffer list using the dma_address
10525 * of unsolicited event to find the driver's lpfc_dmabuf object
10526 * corresponding to the dma_address. The function returns the
10527 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10528 * This function is called by the ct and els unsolicited event
10529 * handlers to get the buffer associated with the unsolicited
10530 * event.
10531 *
10532 * This function is called with no lock held.
10533 **/
dea3101e
JB
10534struct lpfc_dmabuf *
10535lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10536 dma_addr_t phys)
10537{
10538 struct lpfc_dmabuf *mp, *next_mp;
10539 struct list_head *slp = &pring->postbufq;
10540
25985edc 10541 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10542 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10543 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10544 if (mp->phys == phys) {
10545 list_del_init(&mp->list);
10546 pring->postbufq_cnt--;
2e0fef85 10547 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10548 return mp;
10549 }
10550 }
10551
2e0fef85 10552 spin_unlock_irq(&phba->hbalock);
dea3101e 10553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10554 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10555 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10556 pring->ringno, (unsigned long long)phys,
dea3101e
JB
10557 slp->next, slp->prev, pring->postbufq_cnt);
10558 return NULL;
10559}
10560
e59058c4 10561/**
3621a710 10562 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10563 * @phba: Pointer to HBA context object.
10564 * @cmdiocb: Pointer to driver command iocb object.
10565 * @rspiocb: Pointer to driver response iocb object.
10566 *
10567 * This function is the completion handler for the abort iocbs for
10568 * ELS commands. This function is called from the ELS ring event
10569 * handler with no lock held. This function frees memory resources
10570 * associated with the abort iocb.
10571 **/
dea3101e 10572static void
2e0fef85
JS
10573lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10574 struct lpfc_iocbq *rspiocb)
dea3101e 10575{
2e0fef85 10576 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10577 uint16_t abort_iotag, abort_context;
ff78d8f9 10578 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10579
10580 if (irsp->ulpStatus) {
ff78d8f9
JS
10581
10582 /*
10583 * Assume that the port already completed and returned, or
10584 * will return the iocb. Just Log the message.
10585 */
2680eeaa
JS
10586 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10587 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10588
2e0fef85 10589 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10590 if (phba->sli_rev < LPFC_SLI_REV4) {
10591 if (abort_iotag != 0 &&
10592 abort_iotag <= phba->sli.last_iotag)
10593 abort_iocb =
10594 phba->sli.iocbq_lookup[abort_iotag];
10595 } else
10596 /* For sli4 the abort_tag is the XRI,
10597 * so the abort routine puts the iotag of the iocb
10598 * being aborted in the context field of the abort
10599 * IOCB.
10600 */
10601 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10602
2a9bf3d0
JS
10603 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10604 "0327 Cannot abort els iocb %p "
10605 "with tag %x context %x, abort status %x, "
10606 "abort code %x\n",
10607 abort_iocb, abort_iotag, abort_context,
10608 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10609
ff78d8f9 10610 spin_unlock_irq(&phba->hbalock);
2680eeaa 10611 }
604a3e30 10612 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
10613 return;
10614}
10615
e59058c4 10616/**
3621a710 10617 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10618 * @phba: Pointer to HBA context object.
10619 * @cmdiocb: Pointer to driver command iocb object.
10620 * @rspiocb: Pointer to driver response iocb object.
10621 *
10622 * The function is called from SLI ring event handler with no
10623 * lock held. This function is the completion handler for ELS commands
10624 * which are aborted. The function frees memory resources used for
10625 * the aborted ELS commands.
10626 **/
92d7f7b0
JS
10627static void
10628lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10629 struct lpfc_iocbq *rspiocb)
10630{
10631 IOCB_t *irsp = &rspiocb->iocb;
10632
10633 /* ELS cmd tag <ulpIoTag> completes */
10634 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10635 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10636 "x%x x%x x%x\n",
e8b62011 10637 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10638 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10639 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10640 lpfc_ct_free_iocb(phba, cmdiocb);
10641 else
10642 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10643 return;
10644}
10645
e59058c4 10646/**
5af5eee7 10647 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10648 * @phba: Pointer to HBA context object.
10649 * @pring: Pointer to driver SLI ring object.
10650 * @cmdiocb: Pointer to driver command iocb object.
10651 *
5af5eee7
JS
10652 * This function issues an abort iocb for the provided command iocb down to
10653 * the port. Other than the case the outstanding command iocb is an abort
10654 * request, this function issues abort out unconditionally. This function is
10655 * called with hbalock held. The function returns 0 when it fails due to
10656 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10657 **/
5af5eee7
JS
10658static int
10659lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10660 struct lpfc_iocbq *cmdiocb)
dea3101e 10661{
2e0fef85 10662 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10663 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
10664 IOCB_t *icmd = NULL;
10665 IOCB_t *iabt = NULL;
5af5eee7 10666 int retval;
7e56aa25 10667 unsigned long iflags;
07951076 10668
1c2ba475
JT
10669 lockdep_assert_held(&phba->hbalock);
10670
92d7f7b0
JS
10671 /*
10672 * There are certain command types we don't want to abort. And we
10673 * don't want to abort commands that are already in the process of
10674 * being aborted.
07951076
JS
10675 */
10676 icmd = &cmdiocb->iocb;
2e0fef85 10677 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10678 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10679 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10680 return 0;
10681
dea3101e 10682 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10683 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
10684 if (abtsiocbp == NULL)
10685 return 0;
dea3101e 10686
07951076 10687 /* This signals the response to set the correct status
341af102 10688 * before calling the completion handler
07951076
JS
10689 */
10690 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10691
dea3101e 10692 iabt = &abtsiocbp->iocb;
07951076
JS
10693 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10694 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10695 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10696 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10697 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10698 }
da0436e9
JS
10699 else
10700 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10701 iabt->ulpLe = 1;
10702 iabt->ulpClass = icmd->ulpClass;
dea3101e 10703
5ffc266e 10704 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10705 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10706 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10707 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10708 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10709 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10710
2e0fef85 10711 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10712 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10713 else
10714 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10715
07951076 10716 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10717 abtsiocbp->vport = vport;
5b8bd0c9 10718
e8b62011
JS
10719 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10720 "0339 Abort xri x%x, original iotag x%x, "
10721 "abort cmd iotag x%x\n",
2a9bf3d0 10722 iabt->un.acxri.abortIoTag,
e8b62011 10723 iabt->un.acxri.abortContextTag,
2a9bf3d0 10724 abtsiocbp->iotag);
7e56aa25
JS
10725
10726 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10727 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10728 if (unlikely(pring == NULL))
9bd2bff5 10729 return 0;
7e56aa25
JS
10730 /* Note: both hbalock and ring_lock need to be set here */
10731 spin_lock_irqsave(&pring->ring_lock, iflags);
10732 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10733 abtsiocbp, 0);
10734 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10735 } else {
10736 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10737 abtsiocbp, 0);
10738 }
dea3101e 10739
d7c255b2
JS
10740 if (retval)
10741 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10742
10743 /*
10744 * Caller to this routine should check for IOCB_ERROR
10745 * and handle it properly. This routine no longer removes
10746 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10747 */
10748 return retval;
10749}
10750
10751/**
10752 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10753 * @phba: Pointer to HBA context object.
10754 * @pring: Pointer to driver SLI ring object.
10755 * @cmdiocb: Pointer to driver command iocb object.
10756 *
10757 * This function issues an abort iocb for the provided command iocb. In case
10758 * of unloading, the abort iocb will not be issued to commands on the ELS
10759 * ring. Instead, the callback function shall be changed to those commands
10760 * so that nothing happens when them finishes. This function is called with
10761 * hbalock held. The function returns 0 when the command iocb is an abort
10762 * request.
10763 **/
10764int
10765lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10766 struct lpfc_iocbq *cmdiocb)
10767{
10768 struct lpfc_vport *vport = cmdiocb->vport;
10769 int retval = IOCB_ERROR;
10770 IOCB_t *icmd = NULL;
10771
1c2ba475
JT
10772 lockdep_assert_held(&phba->hbalock);
10773
5af5eee7
JS
10774 /*
10775 * There are certain command types we don't want to abort. And we
10776 * don't want to abort commands that are already in the process of
10777 * being aborted.
10778 */
10779 icmd = &cmdiocb->iocb;
10780 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10781 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10782 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10783 return 0;
10784
1234a6d5
DK
10785 if (!pring) {
10786 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10787 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10788 else
10789 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10790 goto abort_iotag_exit;
10791 }
10792
5af5eee7
JS
10793 /*
10794 * If we're unloading, don't abort iocb on the ELS ring, but change
10795 * the callback so that nothing happens when it finishes.
10796 */
10797 if ((vport->load_flag & FC_UNLOADING) &&
10798 (pring->ringno == LPFC_ELS_RING)) {
10799 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10800 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10801 else
10802 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10803 goto abort_iotag_exit;
10804 }
10805
10806 /* Now, we try to issue the abort to the cmdiocb out */
10807 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10808
07951076 10809abort_iotag_exit:
2e0fef85
JS
10810 /*
10811 * Caller to this routine should check for IOCB_ERROR
10812 * and handle it properly. This routine no longer removes
10813 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10814 */
2e0fef85 10815 return retval;
dea3101e
JB
10816}
10817
895427bd
JS
10818/**
10819 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10820 * @phba: Pointer to HBA context object.
10821 * @pring: Pointer to driver SLI ring object.
10822 * @cmdiocb: Pointer to driver command iocb object.
10823 *
10824 * This function issues an abort iocb for the provided command iocb down to
10825 * the port. Other than the case the outstanding command iocb is an abort
10826 * request, this function issues abort out unconditionally. This function is
10827 * called with hbalock held. The function returns 0 when it fails due to
10828 * memory allocation failure or when the command iocb is an abort request.
10829 **/
10830static int
10831lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10832 struct lpfc_iocbq *cmdiocb)
10833{
10834 struct lpfc_vport *vport = cmdiocb->vport;
10835 struct lpfc_iocbq *abtsiocbp;
10836 union lpfc_wqe *abts_wqe;
10837 int retval;
10838
10839 /*
10840 * There are certain command types we don't want to abort. And we
10841 * don't want to abort commands that are already in the process of
10842 * being aborted.
10843 */
10844 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10845 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10846 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10847 return 0;
10848
10849 /* issue ABTS for this io based on iotag */
10850 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10851 if (abtsiocbp == NULL)
10852 return 0;
10853
10854 /* This signals the response to set the correct status
10855 * before calling the completion handler
10856 */
10857 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10858
10859 /* Complete prepping the abort wqe and issue to the FW. */
10860 abts_wqe = &abtsiocbp->wqe;
10861 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10862 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10863
10864 /* Explicitly set reserved fields to zero.*/
10865 abts_wqe->abort_cmd.rsrvd4 = 0;
10866 abts_wqe->abort_cmd.rsrvd5 = 0;
10867
10868 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10869 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10870 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10871
10872 /* word 7 */
10873 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10874 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10875 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10876 cmdiocb->iocb.ulpClass);
10877
10878 /* word 8 - tell the FW to abort the IO associated with this
10879 * outstanding exchange ID.
10880 */
10881 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10882
10883 /* word 9 - this is the iotag for the abts_wqe completion. */
10884 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10885 abtsiocbp->iotag);
10886
10887 /* word 10 */
10888 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10889 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10890 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10891
10892 /* word 11 */
10893 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10894 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10895 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10896
10897 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10898 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10899 abtsiocbp->vport = vport;
01649561 10900 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 10901 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 10902 if (retval) {
895427bd
JS
10903 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10904 "6147 Failed abts issue_wqe with status x%x "
10905 "for oxid x%x\n",
10906 retval, cmdiocb->sli4_xritag);
10907 lpfc_sli_release_iocbq(phba, abtsiocbp);
10908 return retval;
10909 }
10910
10911 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10912 "6148 Drv Abort NVME Request Issued for "
10913 "ox_id x%x on reqtag x%x\n",
10914 cmdiocb->sli4_xritag,
10915 abtsiocbp->iotag);
10916
10917 return retval;
10918}
10919
5af5eee7
JS
10920/**
10921 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10922 * @phba: pointer to lpfc HBA data structure.
10923 *
10924 * This routine will abort all pending and outstanding iocbs to an HBA.
10925 **/
10926void
10927lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10928{
10929 struct lpfc_sli *psli = &phba->sli;
10930 struct lpfc_sli_ring *pring;
895427bd 10931 struct lpfc_queue *qp = NULL;
5af5eee7
JS
10932 int i;
10933
895427bd
JS
10934 if (phba->sli_rev != LPFC_SLI_REV4) {
10935 for (i = 0; i < psli->num_rings; i++) {
10936 pring = &psli->sli3_ring[i];
10937 lpfc_sli_abort_iocb_ring(phba, pring);
10938 }
10939 return;
10940 }
10941 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10942 pring = qp->pring;
10943 if (!pring)
10944 continue;
db55fba8 10945 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
10946 }
10947}
10948
e59058c4 10949/**
3621a710 10950 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
10951 * @iocbq: Pointer to driver iocb object.
10952 * @vport: Pointer to driver virtual port object.
10953 * @tgt_id: SCSI ID of the target.
10954 * @lun_id: LUN ID of the scsi device.
10955 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10956 *
3621a710 10957 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
10958 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10959 * 0 if the filtering criteria is met for the given iocb and will return
10960 * 1 if the filtering criteria is not met.
10961 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10962 * given iocb is for the SCSI device specified by vport, tgt_id and
10963 * lun_id parameter.
10964 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10965 * given iocb is for the SCSI target specified by vport and tgt_id
10966 * parameters.
10967 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10968 * given iocb is for the SCSI host associated with the given vport.
10969 * This function is called with no locks held.
10970 **/
dea3101e 10971static int
51ef4c26
JS
10972lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10973 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 10974 lpfc_ctx_cmd ctx_cmd)
dea3101e 10975{
0bd4ca25 10976 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
10977 int rc = 1;
10978
0bd4ca25
JSEC
10979 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10980 return rc;
10981
51ef4c26
JS
10982 if (iocbq->vport != vport)
10983 return rc;
10984
0bd4ca25 10985 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 10986
495a714c 10987 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
10988 return rc;
10989
10990 switch (ctx_cmd) {
10991 case LPFC_CTX_LUN:
495a714c
JS
10992 if ((lpfc_cmd->rdata->pnode) &&
10993 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10994 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
10995 rc = 0;
10996 break;
10997 case LPFC_CTX_TGT:
495a714c
JS
10998 if ((lpfc_cmd->rdata->pnode) &&
10999 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
11000 rc = 0;
11001 break;
dea3101e
JB
11002 case LPFC_CTX_HOST:
11003 rc = 0;
11004 break;
11005 default:
11006 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 11007 __func__, ctx_cmd);
dea3101e
JB
11008 break;
11009 }
11010
11011 return rc;
11012}
11013
e59058c4 11014/**
3621a710 11015 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
11016 * @vport: Pointer to virtual port.
11017 * @tgt_id: SCSI ID of the target.
11018 * @lun_id: LUN ID of the scsi device.
11019 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11020 *
11021 * This function returns number of FCP commands pending for the vport.
11022 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11023 * commands pending on the vport associated with SCSI device specified
11024 * by tgt_id and lun_id parameters.
11025 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11026 * commands pending on the vport associated with SCSI target specified
11027 * by tgt_id parameter.
11028 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11029 * commands pending on the vport.
11030 * This function returns the number of iocbs which satisfy the filter.
11031 * This function is called without any lock held.
11032 **/
dea3101e 11033int
51ef4c26
JS
11034lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11035 lpfc_ctx_cmd ctx_cmd)
dea3101e 11036{
51ef4c26 11037 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11038 struct lpfc_iocbq *iocbq;
11039 int sum, i;
dea3101e 11040
31979008 11041 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
11042 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11043 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11044
51ef4c26
JS
11045 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11046 ctx_cmd) == 0)
0bd4ca25 11047 sum++;
dea3101e 11048 }
31979008 11049 spin_unlock_irq(&phba->hbalock);
0bd4ca25 11050
dea3101e
JB
11051 return sum;
11052}
11053
e59058c4 11054/**
3621a710 11055 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
11056 * @phba: Pointer to HBA context object
11057 * @cmdiocb: Pointer to command iocb object.
11058 * @rspiocb: Pointer to response iocb object.
11059 *
11060 * This function is called when an aborted FCP iocb completes. This
11061 * function is called by the ring event handler with no lock held.
11062 * This function frees the iocb.
11063 **/
5eb95af0 11064void
2e0fef85
JS
11065lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11066 struct lpfc_iocbq *rspiocb)
5eb95af0 11067{
cb69f7de 11068 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 11069 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
11070 "original iotag x%x, abort cmd iotag x%x "
11071 "status 0x%x, reason 0x%x\n",
11072 cmdiocb->iocb.un.acxri.abortContextTag,
11073 cmdiocb->iocb.un.acxri.abortIoTag,
11074 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11075 rspiocb->iocb.un.ulpWord[4]);
604a3e30 11076 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
11077 return;
11078}
11079
e59058c4 11080/**
3621a710 11081 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
11082 * @vport: Pointer to virtual port.
11083 * @pring: Pointer to driver SLI ring object.
11084 * @tgt_id: SCSI ID of the target.
11085 * @lun_id: LUN ID of the scsi device.
11086 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11087 *
11088 * This function sends an abort command for every SCSI command
11089 * associated with the given virtual port pending on the ring
11090 * filtered by lpfc_sli_validate_fcp_iocb function.
11091 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11092 * FCP iocbs associated with lun specified by tgt_id and lun_id
11093 * parameters
11094 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11095 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11096 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11097 * FCP iocbs associated with virtual port.
11098 * This function returns number of iocbs it failed to abort.
11099 * This function is called with no locks held.
11100 **/
dea3101e 11101int
51ef4c26
JS
11102lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11103 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 11104{
51ef4c26 11105 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
11106 struct lpfc_iocbq *iocbq;
11107 struct lpfc_iocbq *abtsiocb;
ecbb227e 11108 struct lpfc_sli_ring *pring_s4;
dea3101e 11109 IOCB_t *cmd = NULL;
dea3101e 11110 int errcnt = 0, ret_val = 0;
0bd4ca25 11111 int i;
dea3101e 11112
0bd4ca25
JSEC
11113 for (i = 1; i <= phba->sli.last_iotag; i++) {
11114 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 11115
51ef4c26 11116 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 11117 abort_cmd) != 0)
dea3101e
JB
11118 continue;
11119
afbd8d88
JS
11120 /*
11121 * If the iocbq is already being aborted, don't take a second
11122 * action, but do count it.
11123 */
11124 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11125 continue;
11126
dea3101e 11127 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 11128 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
11129 if (abtsiocb == NULL) {
11130 errcnt++;
11131 continue;
11132 }
dea3101e 11133
afbd8d88
JS
11134 /* indicate the IO is being aborted by the driver. */
11135 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11136
0bd4ca25 11137 cmd = &iocbq->iocb;
dea3101e
JB
11138 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11139 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11140 if (phba->sli_rev == LPFC_SLI_REV4)
11141 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11142 else
11143 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
11144 abtsiocb->iocb.ulpLe = 1;
11145 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11146 abtsiocb->vport = vport;
dea3101e 11147
5ffc266e 11148 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11149 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11150 if (iocbq->iocb_flag & LPFC_IO_FCP)
11151 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11152 if (iocbq->iocb_flag & LPFC_IO_FOF)
11153 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11154
2e0fef85 11155 if (lpfc_is_link_up(phba))
dea3101e
JB
11156 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11157 else
11158 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11159
5eb95af0
JSEC
11160 /* Setup callback routine and issue the command. */
11161 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11162 if (phba->sli_rev == LPFC_SLI_REV4) {
11163 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11164 if (!pring_s4)
11165 continue;
11166 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11167 abtsiocb, 0);
11168 } else
11169 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11170 abtsiocb, 0);
dea3101e 11171 if (ret_val == IOCB_ERROR) {
604a3e30 11172 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
11173 errcnt++;
11174 continue;
11175 }
11176 }
11177
11178 return errcnt;
11179}
11180
98912dda
JS
11181/**
11182 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11183 * @vport: Pointer to virtual port.
11184 * @pring: Pointer to driver SLI ring object.
11185 * @tgt_id: SCSI ID of the target.
11186 * @lun_id: LUN ID of the scsi device.
11187 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11188 *
11189 * This function sends an abort command for every SCSI command
11190 * associated with the given virtual port pending on the ring
11191 * filtered by lpfc_sli_validate_fcp_iocb function.
11192 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11193 * FCP iocbs associated with lun specified by tgt_id and lun_id
11194 * parameters
11195 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11196 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11197 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11198 * FCP iocbs associated with virtual port.
11199 * This function returns number of iocbs it aborted .
11200 * This function is called with no locks held right after a taskmgmt
11201 * command is sent.
11202 **/
11203int
11204lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11205 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11206{
11207 struct lpfc_hba *phba = vport->phba;
8c50d25c 11208 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11209 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11210 struct lpfc_nodelist *ndlp;
98912dda
JS
11211 struct lpfc_iocbq *iocbq;
11212 IOCB_t *icmd;
11213 int sum, i, ret_val;
11214 unsigned long iflags;
11215 struct lpfc_sli_ring *pring_s4;
98912dda
JS
11216
11217 spin_lock_irq(&phba->hbalock);
11218
11219 /* all I/Os are in process of being flushed */
11220 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11221 spin_unlock_irq(&phba->hbalock);
11222 return 0;
11223 }
11224 sum = 0;
11225
11226 for (i = 1; i <= phba->sli.last_iotag; i++) {
11227 iocbq = phba->sli.iocbq_lookup[i];
11228
11229 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11230 cmd) != 0)
11231 continue;
11232
11233 /*
11234 * If the iocbq is already being aborted, don't take a second
11235 * action, but do count it.
11236 */
11237 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11238 continue;
11239
11240 /* issue ABTS for this IOCB based on iotag */
11241 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11242 if (abtsiocbq == NULL)
11243 continue;
11244
11245 icmd = &iocbq->iocb;
11246 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11247 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11248 if (phba->sli_rev == LPFC_SLI_REV4)
11249 abtsiocbq->iocb.un.acxri.abortIoTag =
11250 iocbq->sli4_xritag;
11251 else
11252 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11253 abtsiocbq->iocb.ulpLe = 1;
11254 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11255 abtsiocbq->vport = vport;
11256
11257 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11258 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11259 if (iocbq->iocb_flag & LPFC_IO_FCP)
11260 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11261 if (iocbq->iocb_flag & LPFC_IO_FOF)
11262 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11263
8c50d25c
JS
11264 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11265 ndlp = lpfc_cmd->rdata->pnode;
11266
11267 if (lpfc_is_link_up(phba) &&
11268 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11269 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11270 else
11271 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11272
11273 /* Setup callback routine and issue the command. */
11274 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11275
11276 /*
11277 * Indicate the IO is being aborted by the driver and set
11278 * the caller's flag into the aborted IO.
11279 */
11280 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11281
11282 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11283 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11284 if (pring_s4 == NULL)
11285 continue;
98912dda
JS
11286 /* Note: both hbalock and ring_lock must be set here */
11287 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11288 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11289 abtsiocbq, 0);
11290 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11291 } else {
11292 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11293 abtsiocbq, 0);
11294 }
11295
11296
11297 if (ret_val == IOCB_ERROR)
11298 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11299 else
11300 sum++;
11301 }
11302 spin_unlock_irq(&phba->hbalock);
11303 return sum;
11304}
11305
e59058c4 11306/**
3621a710 11307 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11308 * @phba: Pointer to HBA context object.
11309 * @cmdiocbq: Pointer to command iocb.
11310 * @rspiocbq: Pointer to response iocb.
11311 *
11312 * This function is the completion handler for iocbs issued using
11313 * lpfc_sli_issue_iocb_wait function. This function is called by the
11314 * ring event handler function without any lock held. This function
11315 * can be called from both worker thread context and interrupt
11316 * context. This function also can be called from other thread which
11317 * cleans up the SLI layer objects.
11318 * This function copy the contents of the response iocb to the
11319 * response iocb memory object provided by the caller of
11320 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11321 * sleeps for the iocb completion.
11322 **/
68876920
JSEC
11323static void
11324lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11325 struct lpfc_iocbq *cmdiocbq,
11326 struct lpfc_iocbq *rspiocbq)
dea3101e 11327{
68876920
JSEC
11328 wait_queue_head_t *pdone_q;
11329 unsigned long iflags;
0f65ff68 11330 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11331
2e0fef85 11332 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11333 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11334
11335 /*
11336 * A time out has occurred for the iocb. If a time out
11337 * completion handler has been supplied, call it. Otherwise,
11338 * just free the iocbq.
11339 */
11340
11341 spin_unlock_irqrestore(&phba->hbalock, iflags);
11342 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11343 cmdiocbq->wait_iocb_cmpl = NULL;
11344 if (cmdiocbq->iocb_cmpl)
11345 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11346 else
11347 lpfc_sli_release_iocbq(phba, cmdiocbq);
11348 return;
11349 }
11350
68876920
JSEC
11351 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11352 if (cmdiocbq->context2 && rspiocbq)
11353 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11354 &rspiocbq->iocb, sizeof(IOCB_t));
11355
0f65ff68
JS
11356 /* Set the exchange busy flag for task management commands */
11357 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11358 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11359 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11360 cur_iocbq);
11361 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11362 }
11363
68876920 11364 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11365 if (pdone_q)
11366 wake_up(pdone_q);
858c9f6c 11367 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11368 return;
11369}
11370
d11e31dd
JS
11371/**
11372 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11373 * @phba: Pointer to HBA context object..
11374 * @piocbq: Pointer to command iocb.
11375 * @flag: Flag to test.
11376 *
11377 * This routine grabs the hbalock and then test the iocb_flag to
11378 * see if the passed in flag is set.
11379 * Returns:
11380 * 1 if flag is set.
11381 * 0 if flag is not set.
11382 **/
11383static int
11384lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11385 struct lpfc_iocbq *piocbq, uint32_t flag)
11386{
11387 unsigned long iflags;
11388 int ret;
11389
11390 spin_lock_irqsave(&phba->hbalock, iflags);
11391 ret = piocbq->iocb_flag & flag;
11392 spin_unlock_irqrestore(&phba->hbalock, iflags);
11393 return ret;
11394
11395}
11396
e59058c4 11397/**
3621a710 11398 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11399 * @phba: Pointer to HBA context object..
11400 * @pring: Pointer to sli ring.
11401 * @piocb: Pointer to command iocb.
11402 * @prspiocbq: Pointer to response iocb.
11403 * @timeout: Timeout in number of seconds.
11404 *
11405 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11406 * iocb to complete. The iocb_cmpl field of the shall be used
11407 * to handle iocbs which time out. If the field is NULL, the
11408 * function shall free the iocbq structure. If more clean up is
11409 * needed, the caller is expected to provide a completion function
11410 * that will provide the needed clean up. If the iocb command is
11411 * not completed within timeout seconds, the function will either
11412 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11413 * completion function set in the iocb_cmpl field and then return
11414 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11415 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11416 * The function waits for the iocb completion using an
11417 * non-interruptible wait.
11418 * This function will sleep while waiting for iocb completion.
11419 * So, this function should not be called from any context which
11420 * does not allow sleeping. Due to the same reason, this function
11421 * cannot be called with interrupt disabled.
11422 * This function assumes that the iocb completions occur while
11423 * this function sleep. So, this function cannot be called from
11424 * the thread which process iocb completion for this ring.
11425 * This function clears the iocb_flag of the iocb object before
11426 * issuing the iocb and the iocb completion handler sets this
11427 * flag and wakes this thread when the iocb completes.
11428 * The contents of the response iocb will be copied to prspiocbq
11429 * by the completion handler when the command completes.
11430 * This function returns IOCB_SUCCESS when success.
11431 * This function is called with no lock held.
11432 **/
dea3101e 11433int
2e0fef85 11434lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11435 uint32_t ring_number,
2e0fef85
JS
11436 struct lpfc_iocbq *piocb,
11437 struct lpfc_iocbq *prspiocbq,
68876920 11438 uint32_t timeout)
dea3101e 11439{
7259f0d0 11440 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11441 long timeleft, timeout_req = 0;
11442 int retval = IOCB_SUCCESS;
875fbdfe 11443 uint32_t creg_val;
0e9bb8d7
JS
11444 struct lpfc_iocbq *iocb;
11445 int txq_cnt = 0;
11446 int txcmplq_cnt = 0;
895427bd 11447 struct lpfc_sli_ring *pring;
5a0916b4
JS
11448 unsigned long iflags;
11449 bool iocb_completed = true;
11450
895427bd
JS
11451 if (phba->sli_rev >= LPFC_SLI_REV4)
11452 pring = lpfc_sli4_calc_ring(phba, piocb);
11453 else
11454 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11455 /*
68876920
JSEC
11456 * If the caller has provided a response iocbq buffer, then context2
11457 * is NULL or its an error.
dea3101e 11458 */
68876920
JSEC
11459 if (prspiocbq) {
11460 if (piocb->context2)
11461 return IOCB_ERROR;
11462 piocb->context2 = prspiocbq;
dea3101e
JB
11463 }
11464
5a0916b4 11465 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11466 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11467 piocb->context_un.wait_queue = &done_q;
5a0916b4 11468 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11469
875fbdfe 11470 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11471 if (lpfc_readl(phba->HCregaddr, &creg_val))
11472 return IOCB_ERROR;
875fbdfe
JSEC
11473 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11474 writel(creg_val, phba->HCregaddr);
11475 readl(phba->HCregaddr); /* flush */
11476 }
11477
2a9bf3d0
JS
11478 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11479 SLI_IOCB_RET_IOCB);
68876920 11480 if (retval == IOCB_SUCCESS) {
256ec0d0 11481 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11482 timeleft = wait_event_timeout(done_q,
d11e31dd 11483 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11484 timeout_req);
5a0916b4
JS
11485 spin_lock_irqsave(&phba->hbalock, iflags);
11486 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11487
11488 /*
11489 * IOCB timed out. Inform the wake iocb wait
11490 * completion function and set local status
11491 */
dea3101e 11492
5a0916b4
JS
11493 iocb_completed = false;
11494 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11495 }
11496 spin_unlock_irqrestore(&phba->hbalock, iflags);
11497 if (iocb_completed) {
7054a606 11498 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11499 "0331 IOCB wake signaled\n");
53151bbb
JS
11500 /* Note: we are not indicating if the IOCB has a success
11501 * status or not - that's for the caller to check.
11502 * IOCB_SUCCESS means just that the command was sent and
11503 * completed. Not that it completed successfully.
11504 * */
7054a606 11505 } else if (timeleft == 0) {
68876920 11506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11507 "0338 IOCB wait timeout error - no "
11508 "wake response Data x%x\n", timeout);
68876920 11509 retval = IOCB_TIMEDOUT;
7054a606 11510 } else {
68876920 11511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11512 "0330 IOCB wake NOT set, "
11513 "Data x%x x%lx\n",
68876920
JSEC
11514 timeout, (timeleft / jiffies));
11515 retval = IOCB_TIMEDOUT;
dea3101e 11516 }
2a9bf3d0 11517 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11518 if (phba->cfg_log_verbose & LOG_SLI) {
11519 list_for_each_entry(iocb, &pring->txq, list) {
11520 txq_cnt++;
11521 }
11522 list_for_each_entry(iocb, &pring->txcmplq, list) {
11523 txcmplq_cnt++;
11524 }
11525 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11526 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11527 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11528 }
2a9bf3d0 11529 return retval;
68876920
JSEC
11530 } else {
11531 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11532 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11533 retval);
68876920 11534 retval = IOCB_ERROR;
dea3101e
JB
11535 }
11536
875fbdfe 11537 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11538 if (lpfc_readl(phba->HCregaddr, &creg_val))
11539 return IOCB_ERROR;
875fbdfe
JSEC
11540 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11541 writel(creg_val, phba->HCregaddr);
11542 readl(phba->HCregaddr); /* flush */
11543 }
11544
68876920
JSEC
11545 if (prspiocbq)
11546 piocb->context2 = NULL;
11547
11548 piocb->context_un.wait_queue = NULL;
11549 piocb->iocb_cmpl = NULL;
dea3101e
JB
11550 return retval;
11551}
68876920 11552
e59058c4 11553/**
3621a710 11554 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11555 * @phba: Pointer to HBA context object.
11556 * @pmboxq: Pointer to driver mailbox object.
11557 * @timeout: Timeout in number of seconds.
11558 *
11559 * This function issues the mailbox to firmware and waits for the
11560 * mailbox command to complete. If the mailbox command is not
11561 * completed within timeout seconds, it returns MBX_TIMEOUT.
11562 * The function waits for the mailbox completion using an
11563 * interruptible wait. If the thread is woken up due to a
11564 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11565 * should not free the mailbox resources, if this function returns
11566 * MBX_TIMEOUT.
11567 * This function will sleep while waiting for mailbox completion.
11568 * So, this function should not be called from any context which
11569 * does not allow sleeping. Due to the same reason, this function
11570 * cannot be called with interrupt disabled.
11571 * This function assumes that the mailbox completion occurs while
11572 * this function sleep. So, this function cannot be called from
11573 * the worker thread which processes mailbox completion.
11574 * This function is called in the context of HBA management
11575 * applications.
11576 * This function returns MBX_SUCCESS when successful.
11577 * This function is called with no lock held.
11578 **/
dea3101e 11579int
2e0fef85 11580lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
11581 uint32_t timeout)
11582{
7259f0d0 11583 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 11584 MAILBOX_t *mb = NULL;
dea3101e 11585 int retval;
858c9f6c 11586 unsigned long flag;
dea3101e 11587
b230b8a2 11588 /* The caller might set context1 for extended buffer */
98c9ea5c 11589 if (pmboxq->context1)
b230b8a2 11590 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 11591
495a714c 11592 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11593 /* setup wake call as IOCB callback */
11594 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11595 /* setup context field to pass wait_queue pointer to wake function */
11596 pmboxq->context1 = &done_q;
11597
dea3101e
JB
11598 /* now issue the command */
11599 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11600 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
11601 wait_event_interruptible_timeout(done_q,
11602 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 11603 msecs_to_jiffies(timeout * 1000));
7054a606 11604
858c9f6c 11605 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
11606 /* restore the possible extended buffer for free resource */
11607 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
11608 /*
11609 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11610 * else do not free the resources.
11611 */
d7c47992 11612 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11613 retval = MBX_SUCCESS;
d7c47992 11614 } else {
7054a606 11615 retval = MBX_TIMEOUT;
858c9f6c
JS
11616 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11617 }
11618 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
11619 } else {
11620 /* restore the possible extended buffer for free resource */
11621 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
11622 }
11623
dea3101e
JB
11624 return retval;
11625}
11626
e59058c4 11627/**
3772a991 11628 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11629 * @phba: Pointer to HBA context.
11630 *
3772a991
JS
11631 * This function is called to shutdown the driver's mailbox sub-system.
11632 * It first marks the mailbox sub-system is in a block state to prevent
11633 * the asynchronous mailbox command from issued off the pending mailbox
11634 * command queue. If the mailbox command sub-system shutdown is due to
11635 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11636 * the mailbox sub-system flush routine to forcefully bring down the
11637 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11638 * as with offline or HBA function reset), this routine will wait for the
11639 * outstanding mailbox command to complete before invoking the mailbox
11640 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11641 **/
3772a991 11642void
618a5230 11643lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11644{
3772a991 11645 struct lpfc_sli *psli = &phba->sli;
3772a991 11646 unsigned long timeout;
b4c02652 11647
618a5230
JS
11648 if (mbx_action == LPFC_MBX_NO_WAIT) {
11649 /* delay 100ms for port state */
11650 msleep(100);
11651 lpfc_sli_mbox_sys_flush(phba);
11652 return;
11653 }
a183a15f 11654 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11655
3772a991
JS
11656 spin_lock_irq(&phba->hbalock);
11657 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11658
3772a991 11659 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11660 /* Determine how long we might wait for the active mailbox
11661 * command to be gracefully completed by firmware.
11662 */
a183a15f
JS
11663 if (phba->sli.mbox_active)
11664 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11665 phba->sli.mbox_active) *
11666 1000) + jiffies;
11667 spin_unlock_irq(&phba->hbalock);
11668
3772a991
JS
11669 while (phba->sli.mbox_active) {
11670 /* Check active mailbox complete status every 2ms */
11671 msleep(2);
11672 if (time_after(jiffies, timeout))
11673 /* Timeout, let the mailbox flush routine to
11674 * forcefully release active mailbox command
11675 */
11676 break;
11677 }
d7069f09
JS
11678 } else
11679 spin_unlock_irq(&phba->hbalock);
11680
3772a991
JS
11681 lpfc_sli_mbox_sys_flush(phba);
11682}
ed957684 11683
3772a991
JS
11684/**
11685 * lpfc_sli_eratt_read - read sli-3 error attention events
11686 * @phba: Pointer to HBA context.
11687 *
11688 * This function is called to read the SLI3 device error attention registers
11689 * for possible error attention events. The caller must hold the hostlock
11690 * with spin_lock_irq().
11691 *
25985edc 11692 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11693 * Register and returns 0 otherwise.
11694 **/
11695static int
11696lpfc_sli_eratt_read(struct lpfc_hba *phba)
11697{
11698 uint32_t ha_copy;
b4c02652 11699
3772a991 11700 /* Read chip Host Attention (HA) register */
9940b97b
JS
11701 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11702 goto unplug_err;
11703
3772a991
JS
11704 if (ha_copy & HA_ERATT) {
11705 /* Read host status register to retrieve error event */
9940b97b
JS
11706 if (lpfc_sli_read_hs(phba))
11707 goto unplug_err;
b4c02652 11708
3772a991
JS
11709 /* Check if there is a deferred error condition is active */
11710 if ((HS_FFER1 & phba->work_hs) &&
11711 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11712 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11713 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11714 /* Clear all interrupt enable conditions */
11715 writel(0, phba->HCregaddr);
11716 readl(phba->HCregaddr);
11717 }
11718
11719 /* Set the driver HA work bitmap */
3772a991
JS
11720 phba->work_ha |= HA_ERATT;
11721 /* Indicate polling handles this ERATT */
11722 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11723 return 1;
11724 }
11725 return 0;
9940b97b
JS
11726
11727unplug_err:
11728 /* Set the driver HS work bitmap */
11729 phba->work_hs |= UNPLUG_ERR;
11730 /* Set the driver HA work bitmap */
11731 phba->work_ha |= HA_ERATT;
11732 /* Indicate polling handles this ERATT */
11733 phba->hba_flag |= HBA_ERATT_HANDLED;
11734 return 1;
b4c02652
JS
11735}
11736
da0436e9
JS
11737/**
11738 * lpfc_sli4_eratt_read - read sli-4 error attention events
11739 * @phba: Pointer to HBA context.
11740 *
11741 * This function is called to read the SLI4 device error attention registers
11742 * for possible error attention events. The caller must hold the hostlock
11743 * with spin_lock_irq().
11744 *
25985edc 11745 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11746 * Register and returns 0 otherwise.
11747 **/
11748static int
11749lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11750{
11751 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11752 uint32_t if_type, portsmphr;
11753 struct lpfc_register portstat_reg;
da0436e9 11754
2fcee4bf
JS
11755 /*
11756 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11757 * registers for error attention. This can be changed later.
11758 */
2fcee4bf
JS
11759 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11760 switch (if_type) {
11761 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11762 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11763 &uerr_sta_lo) ||
11764 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11765 &uerr_sta_hi)) {
11766 phba->work_hs |= UNPLUG_ERR;
11767 phba->work_ha |= HA_ERATT;
11768 phba->hba_flag |= HBA_ERATT_HANDLED;
11769 return 1;
11770 }
2fcee4bf
JS
11771 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11772 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11774 "1423 HBA Unrecoverable error: "
11775 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11776 "ue_mask_lo_reg=0x%x, "
11777 "ue_mask_hi_reg=0x%x\n",
11778 uerr_sta_lo, uerr_sta_hi,
11779 phba->sli4_hba.ue_mask_lo,
11780 phba->sli4_hba.ue_mask_hi);
11781 phba->work_status[0] = uerr_sta_lo;
11782 phba->work_status[1] = uerr_sta_hi;
11783 phba->work_ha |= HA_ERATT;
11784 phba->hba_flag |= HBA_ERATT_HANDLED;
11785 return 1;
11786 }
11787 break;
11788 case LPFC_SLI_INTF_IF_TYPE_2:
27d6ac0a 11789 case LPFC_SLI_INTF_IF_TYPE_6:
9940b97b
JS
11790 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11791 &portstat_reg.word0) ||
11792 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11793 &portsmphr)){
11794 phba->work_hs |= UNPLUG_ERR;
11795 phba->work_ha |= HA_ERATT;
11796 phba->hba_flag |= HBA_ERATT_HANDLED;
11797 return 1;
11798 }
2fcee4bf
JS
11799 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11800 phba->work_status[0] =
11801 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11802 phba->work_status[1] =
11803 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11805 "2885 Port Status Event: "
2fcee4bf
JS
11806 "port status reg 0x%x, "
11807 "port smphr reg 0x%x, "
11808 "error 1=0x%x, error 2=0x%x\n",
11809 portstat_reg.word0,
11810 portsmphr,
11811 phba->work_status[0],
11812 phba->work_status[1]);
11813 phba->work_ha |= HA_ERATT;
11814 phba->hba_flag |= HBA_ERATT_HANDLED;
11815 return 1;
11816 }
11817 break;
11818 case LPFC_SLI_INTF_IF_TYPE_1:
11819 default:
a747c9ce 11820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11821 "2886 HBA Error Attention on unsupported "
11822 "if type %d.", if_type);
a747c9ce 11823 return 1;
da0436e9 11824 }
2fcee4bf 11825
da0436e9
JS
11826 return 0;
11827}
11828
e59058c4 11829/**
3621a710 11830 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11831 * @phba: Pointer to HBA context.
11832 *
3772a991 11833 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11834 * error attention register bit for error attention events.
11835 *
25985edc 11836 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11837 * Register and returns 0 otherwise.
11838 **/
11839int
11840lpfc_sli_check_eratt(struct lpfc_hba *phba)
11841{
11842 uint32_t ha_copy;
11843
11844 /* If somebody is waiting to handle an eratt, don't process it
11845 * here. The brdkill function will do this.
11846 */
11847 if (phba->link_flag & LS_IGNORE_ERATT)
11848 return 0;
11849
11850 /* Check if interrupt handler handles this ERATT */
11851 spin_lock_irq(&phba->hbalock);
11852 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11853 /* Interrupt handler has handled ERATT */
11854 spin_unlock_irq(&phba->hbalock);
11855 return 0;
11856 }
11857
a257bf90
JS
11858 /*
11859 * If there is deferred error attention, do not check for error
11860 * attention
11861 */
11862 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11863 spin_unlock_irq(&phba->hbalock);
11864 return 0;
11865 }
11866
3772a991
JS
11867 /* If PCI channel is offline, don't process it */
11868 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11869 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11870 return 0;
11871 }
11872
11873 switch (phba->sli_rev) {
11874 case LPFC_SLI_REV2:
11875 case LPFC_SLI_REV3:
11876 /* Read chip Host Attention (HA) register */
11877 ha_copy = lpfc_sli_eratt_read(phba);
11878 break;
da0436e9 11879 case LPFC_SLI_REV4:
2fcee4bf 11880 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11881 ha_copy = lpfc_sli4_eratt_read(phba);
11882 break;
3772a991
JS
11883 default:
11884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11885 "0299 Invalid SLI revision (%d)\n",
11886 phba->sli_rev);
11887 ha_copy = 0;
11888 break;
9399627f
JS
11889 }
11890 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11891
11892 return ha_copy;
11893}
11894
11895/**
11896 * lpfc_intr_state_check - Check device state for interrupt handling
11897 * @phba: Pointer to HBA context.
11898 *
11899 * This inline routine checks whether a device or its PCI slot is in a state
11900 * that the interrupt should be handled.
11901 *
11902 * This function returns 0 if the device or the PCI slot is in a state that
11903 * interrupt should be handled, otherwise -EIO.
11904 */
11905static inline int
11906lpfc_intr_state_check(struct lpfc_hba *phba)
11907{
11908 /* If the pci channel is offline, ignore all the interrupts */
11909 if (unlikely(pci_channel_offline(phba->pcidev)))
11910 return -EIO;
11911
11912 /* Update device level interrupt statistics */
11913 phba->sli.slistat.sli_intr++;
11914
11915 /* Ignore all interrupts during initialization. */
11916 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11917 return -EIO;
11918
9399627f
JS
11919 return 0;
11920}
11921
11922/**
3772a991 11923 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11924 * @irq: Interrupt number.
11925 * @dev_id: The device context pointer.
11926 *
9399627f 11927 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11928 * service routine when device with SLI-3 interface spec is enabled with
11929 * MSI-X multi-message interrupt mode and there are slow-path events in
11930 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11931 * interrupt mode, this function is called as part of the device-level
11932 * interrupt handler. When the PCI slot is in error recovery or the HBA
11933 * is undergoing initialization, the interrupt handler will not process
11934 * the interrupt. The link attention and ELS ring attention events are
11935 * handled by the worker thread. The interrupt handler signals the worker
11936 * thread and returns for these events. This function is called without
11937 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
11938 * structures.
11939 *
11940 * This function returns IRQ_HANDLED when interrupt is handled else it
11941 * returns IRQ_NONE.
e59058c4 11942 **/
dea3101e 11943irqreturn_t
3772a991 11944lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 11945{
2e0fef85 11946 struct lpfc_hba *phba;
a747c9ce 11947 uint32_t ha_copy, hc_copy;
dea3101e
JB
11948 uint32_t work_ha_copy;
11949 unsigned long status;
5b75da2f 11950 unsigned long iflag;
dea3101e
JB
11951 uint32_t control;
11952
92d7f7b0 11953 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
11954 struct lpfc_vport *vport;
11955 struct lpfc_nodelist *ndlp;
11956 struct lpfc_dmabuf *mp;
92d7f7b0
JS
11957 LPFC_MBOXQ_t *pmb;
11958 int rc;
11959
dea3101e
JB
11960 /*
11961 * Get the driver's phba structure from the dev_id and
11962 * assume the HBA is not interrupting.
11963 */
9399627f 11964 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
11965
11966 if (unlikely(!phba))
11967 return IRQ_NONE;
11968
dea3101e 11969 /*
9399627f
JS
11970 * Stuff needs to be attented to when this function is invoked as an
11971 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 11972 */
9399627f 11973 if (phba->intr_type == MSIX) {
3772a991
JS
11974 /* Check device state for handling interrupt */
11975 if (lpfc_intr_state_check(phba))
9399627f
JS
11976 return IRQ_NONE;
11977 /* Need to read HA REG for slow-path events */
5b75da2f 11978 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11979 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11980 goto unplug_error;
9399627f
JS
11981 /* If somebody is waiting to handle an eratt don't process it
11982 * here. The brdkill function will do this.
11983 */
11984 if (phba->link_flag & LS_IGNORE_ERATT)
11985 ha_copy &= ~HA_ERATT;
11986 /* Check the need for handling ERATT in interrupt handler */
11987 if (ha_copy & HA_ERATT) {
11988 if (phba->hba_flag & HBA_ERATT_HANDLED)
11989 /* ERATT polling has handled ERATT */
11990 ha_copy &= ~HA_ERATT;
11991 else
11992 /* Indicate interrupt handler handles ERATT */
11993 phba->hba_flag |= HBA_ERATT_HANDLED;
11994 }
a257bf90
JS
11995
11996 /*
11997 * If there is deferred error attention, do not check for any
11998 * interrupt.
11999 */
12000 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12001 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12002 return IRQ_NONE;
12003 }
12004
9399627f 12005 /* Clear up only attention source related to slow-path */
9940b97b
JS
12006 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12007 goto unplug_error;
12008
a747c9ce
JS
12009 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12010 HC_LAINT_ENA | HC_ERINT_ENA),
12011 phba->HCregaddr);
9399627f
JS
12012 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12013 phba->HAregaddr);
a747c9ce 12014 writel(hc_copy, phba->HCregaddr);
9399627f 12015 readl(phba->HAregaddr); /* flush */
5b75da2f 12016 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12017 } else
12018 ha_copy = phba->ha_copy;
dea3101e 12019
dea3101e
JB
12020 work_ha_copy = ha_copy & phba->work_ha_mask;
12021
9399627f 12022 if (work_ha_copy) {
dea3101e
JB
12023 if (work_ha_copy & HA_LATT) {
12024 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12025 /*
12026 * Turn off Link Attention interrupts
12027 * until CLEAR_LA done
12028 */
5b75da2f 12029 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12030 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
12031 if (lpfc_readl(phba->HCregaddr, &control))
12032 goto unplug_error;
dea3101e
JB
12033 control &= ~HC_LAINT_ENA;
12034 writel(control, phba->HCregaddr);
12035 readl(phba->HCregaddr); /* flush */
5b75da2f 12036 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12037 }
12038 else
12039 work_ha_copy &= ~HA_LATT;
12040 }
12041
9399627f 12042 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
12043 /*
12044 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12045 * the only slow ring.
12046 */
12047 status = (work_ha_copy &
12048 (HA_RXMASK << (4*LPFC_ELS_RING)));
12049 status >>= (4*LPFC_ELS_RING);
12050 if (status & HA_RXMASK) {
5b75da2f 12051 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
12052 if (lpfc_readl(phba->HCregaddr, &control))
12053 goto unplug_error;
a58cbd52
JS
12054
12055 lpfc_debugfs_slow_ring_trc(phba,
12056 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12057 control, status,
12058 (uint32_t)phba->sli.slistat.sli_intr);
12059
858c9f6c 12060 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
12061 lpfc_debugfs_slow_ring_trc(phba,
12062 "ISR Disable ring:"
12063 "pwork:x%x hawork:x%x wait:x%x",
12064 phba->work_ha, work_ha_copy,
12065 (uint32_t)((unsigned long)
5e9d9b82 12066 &phba->work_waitq));
a58cbd52 12067
858c9f6c
JS
12068 control &=
12069 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
12070 writel(control, phba->HCregaddr);
12071 readl(phba->HCregaddr); /* flush */
dea3101e 12072 }
a58cbd52
JS
12073 else {
12074 lpfc_debugfs_slow_ring_trc(phba,
12075 "ISR slow ring: pwork:"
12076 "x%x hawork:x%x wait:x%x",
12077 phba->work_ha, work_ha_copy,
12078 (uint32_t)((unsigned long)
5e9d9b82 12079 &phba->work_waitq));
a58cbd52 12080 }
5b75da2f 12081 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
12082 }
12083 }
5b75da2f 12084 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 12085 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
12086 if (lpfc_sli_read_hs(phba))
12087 goto unplug_error;
a257bf90
JS
12088 /*
12089 * Check if there is a deferred error condition
12090 * is active
12091 */
12092 if ((HS_FFER1 & phba->work_hs) &&
12093 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
12094 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12095 phba->work_hs)) {
a257bf90
JS
12096 phba->hba_flag |= DEFER_ERATT;
12097 /* Clear all interrupt enable conditions */
12098 writel(0, phba->HCregaddr);
12099 readl(phba->HCregaddr);
12100 }
12101 }
12102
9399627f 12103 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 12104 pmb = phba->sli.mbox_active;
04c68496 12105 pmbox = &pmb->u.mb;
34b02dcd 12106 mbox = phba->mbox;
858c9f6c 12107 vport = pmb->vport;
92d7f7b0
JS
12108
12109 /* First check out the status word */
12110 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12111 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 12112 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
12113 /*
12114 * Stray Mailbox Interrupt, mbxCommand <cmd>
12115 * mbxStatus <status>
12116 */
09372820 12117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 12118 LOG_SLI,
e8b62011 12119 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
12120 "Interrupt mbxCommand x%x "
12121 "mbxStatus x%x\n",
e8b62011 12122 (vport ? vport->vpi : 0),
92d7f7b0
JS
12123 pmbox->mbxCommand,
12124 pmbox->mbxStatus);
09372820
JS
12125 /* clear mailbox attention bit */
12126 work_ha_copy &= ~HA_MBATT;
12127 } else {
97eab634 12128 phba->sli.mbox_active = NULL;
5b75da2f 12129 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
12130 phba->last_completion_time = jiffies;
12131 del_timer(&phba->sli.mbox_tmo);
09372820
JS
12132 if (pmb->mbox_cmpl) {
12133 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12134 MAILBOX_CMD_SIZE);
7a470277
JS
12135 if (pmb->out_ext_byte_len &&
12136 pmb->context2)
12137 lpfc_sli_pcimem_bcopy(
12138 phba->mbox_ext,
12139 pmb->context2,
12140 pmb->out_ext_byte_len);
09372820
JS
12141 }
12142 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12143 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12144
12145 lpfc_debugfs_disc_trc(vport,
12146 LPFC_DISC_TRC_MBOX_VPORT,
12147 "MBOX dflt rpi: : "
12148 "status:x%x rpi:x%x",
12149 (uint32_t)pmbox->mbxStatus,
12150 pmbox->un.varWords[0], 0);
12151
12152 if (!pmbox->mbxStatus) {
12153 mp = (struct lpfc_dmabuf *)
12154 (pmb->context1);
12155 ndlp = (struct lpfc_nodelist *)
12156 pmb->context2;
12157
12158 /* Reg_LOGIN of dflt RPI was
12159 * successful. new lets get
12160 * rid of the RPI using the
12161 * same mbox buffer.
12162 */
12163 lpfc_unreg_login(phba,
12164 vport->vpi,
12165 pmbox->un.varWords[0],
12166 pmb);
12167 pmb->mbox_cmpl =
12168 lpfc_mbx_cmpl_dflt_rpi;
12169 pmb->context1 = mp;
12170 pmb->context2 = ndlp;
12171 pmb->vport = vport;
58da1ffb
JS
12172 rc = lpfc_sli_issue_mbox(phba,
12173 pmb,
12174 MBX_NOWAIT);
12175 if (rc != MBX_BUSY)
12176 lpfc_printf_log(phba,
12177 KERN_ERR,
12178 LOG_MBOX | LOG_SLI,
d7c255b2 12179 "0350 rc should have"
6a9c52cf 12180 "been MBX_BUSY\n");
3772a991
JS
12181 if (rc != MBX_NOT_FINISHED)
12182 goto send_current_mbox;
09372820 12183 }
858c9f6c 12184 }
5b75da2f
JS
12185 spin_lock_irqsave(
12186 &phba->pport->work_port_lock,
12187 iflag);
09372820
JS
12188 phba->pport->work_port_events &=
12189 ~WORKER_MBOX_TMO;
5b75da2f
JS
12190 spin_unlock_irqrestore(
12191 &phba->pport->work_port_lock,
12192 iflag);
09372820 12193 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12194 }
97eab634 12195 } else
5b75da2f 12196 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12197
92d7f7b0
JS
12198 if ((work_ha_copy & HA_MBATT) &&
12199 (phba->sli.mbox_active == NULL)) {
858c9f6c 12200send_current_mbox:
92d7f7b0 12201 /* Process next mailbox command if there is one */
58da1ffb
JS
12202 do {
12203 rc = lpfc_sli_issue_mbox(phba, NULL,
12204 MBX_NOWAIT);
12205 } while (rc == MBX_NOT_FINISHED);
12206 if (rc != MBX_SUCCESS)
12207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12208 LOG_SLI, "0349 rc should be "
6a9c52cf 12209 "MBX_SUCCESS\n");
92d7f7b0
JS
12210 }
12211
5b75da2f 12212 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12213 phba->work_ha |= work_ha_copy;
5b75da2f 12214 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12215 lpfc_worker_wake_up(phba);
dea3101e 12216 }
9399627f 12217 return IRQ_HANDLED;
9940b97b
JS
12218unplug_error:
12219 spin_unlock_irqrestore(&phba->hbalock, iflag);
12220 return IRQ_HANDLED;
dea3101e 12221
3772a991 12222} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12223
12224/**
3772a991 12225 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12226 * @irq: Interrupt number.
12227 * @dev_id: The device context pointer.
12228 *
12229 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12230 * service routine when device with SLI-3 interface spec is enabled with
12231 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12232 * ring event in the HBA. However, when the device is enabled with either
12233 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12234 * device-level interrupt handler. When the PCI slot is in error recovery
12235 * or the HBA is undergoing initialization, the interrupt handler will not
12236 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12237 * the intrrupt context. This function is called without any lock held.
12238 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12239 *
12240 * This function returns IRQ_HANDLED when interrupt is handled else it
12241 * returns IRQ_NONE.
12242 **/
12243irqreturn_t
3772a991 12244lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12245{
12246 struct lpfc_hba *phba;
12247 uint32_t ha_copy;
12248 unsigned long status;
5b75da2f 12249 unsigned long iflag;
895427bd 12250 struct lpfc_sli_ring *pring;
9399627f
JS
12251
12252 /* Get the driver's phba structure from the dev_id and
12253 * assume the HBA is not interrupting.
12254 */
12255 phba = (struct lpfc_hba *) dev_id;
12256
12257 if (unlikely(!phba))
12258 return IRQ_NONE;
12259
12260 /*
12261 * Stuff needs to be attented to when this function is invoked as an
12262 * individual interrupt handler in MSI-X multi-message interrupt mode
12263 */
12264 if (phba->intr_type == MSIX) {
3772a991
JS
12265 /* Check device state for handling interrupt */
12266 if (lpfc_intr_state_check(phba))
9399627f
JS
12267 return IRQ_NONE;
12268 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12269 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12270 return IRQ_HANDLED;
9399627f 12271 /* Clear up only attention source related to fast-path */
5b75da2f 12272 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12273 /*
12274 * If there is deferred error attention, do not check for
12275 * any interrupt.
12276 */
12277 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12278 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12279 return IRQ_NONE;
12280 }
9399627f
JS
12281 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12282 phba->HAregaddr);
12283 readl(phba->HAregaddr); /* flush */
5b75da2f 12284 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12285 } else
12286 ha_copy = phba->ha_copy;
dea3101e
JB
12287
12288 /*
9399627f 12289 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12290 */
9399627f
JS
12291 ha_copy &= ~(phba->work_ha_mask);
12292
12293 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12294 status >>= (4*LPFC_FCP_RING);
895427bd 12295 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12296 if (status & HA_RXMASK)
895427bd 12297 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12298
12299 if (phba->cfg_multi_ring_support == 2) {
12300 /*
9399627f
JS
12301 * Process all events on extra ring. Take the optimized path
12302 * for extra ring IO.
a4bc3379 12303 */
9399627f 12304 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12305 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12306 if (status & HA_RXMASK) {
a4bc3379 12307 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12308 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12309 status);
12310 }
12311 }
dea3101e 12312 return IRQ_HANDLED;
3772a991 12313} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12314
12315/**
3772a991 12316 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12317 * @irq: Interrupt number.
12318 * @dev_id: The device context pointer.
12319 *
3772a991
JS
12320 * This function is the HBA device-level interrupt handler to device with
12321 * SLI-3 interface spec, called from the PCI layer when either MSI or
12322 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12323 * requires driver attention. This function invokes the slow-path interrupt
12324 * attention handling function and fast-path interrupt attention handling
12325 * function in turn to process the relevant HBA attention events. This
12326 * function is called without any lock held. It gets the hbalock to access
12327 * and update SLI data structures.
9399627f
JS
12328 *
12329 * This function returns IRQ_HANDLED when interrupt is handled, else it
12330 * returns IRQ_NONE.
12331 **/
12332irqreturn_t
3772a991 12333lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12334{
12335 struct lpfc_hba *phba;
12336 irqreturn_t sp_irq_rc, fp_irq_rc;
12337 unsigned long status1, status2;
a747c9ce 12338 uint32_t hc_copy;
9399627f
JS
12339
12340 /*
12341 * Get the driver's phba structure from the dev_id and
12342 * assume the HBA is not interrupting.
12343 */
12344 phba = (struct lpfc_hba *) dev_id;
12345
12346 if (unlikely(!phba))
12347 return IRQ_NONE;
12348
3772a991
JS
12349 /* Check device state for handling interrupt */
12350 if (lpfc_intr_state_check(phba))
9399627f
JS
12351 return IRQ_NONE;
12352
12353 spin_lock(&phba->hbalock);
9940b97b
JS
12354 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12355 spin_unlock(&phba->hbalock);
12356 return IRQ_HANDLED;
12357 }
12358
9399627f
JS
12359 if (unlikely(!phba->ha_copy)) {
12360 spin_unlock(&phba->hbalock);
12361 return IRQ_NONE;
12362 } else if (phba->ha_copy & HA_ERATT) {
12363 if (phba->hba_flag & HBA_ERATT_HANDLED)
12364 /* ERATT polling has handled ERATT */
12365 phba->ha_copy &= ~HA_ERATT;
12366 else
12367 /* Indicate interrupt handler handles ERATT */
12368 phba->hba_flag |= HBA_ERATT_HANDLED;
12369 }
12370
a257bf90
JS
12371 /*
12372 * If there is deferred error attention, do not check for any interrupt.
12373 */
12374 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12375 spin_unlock(&phba->hbalock);
a257bf90
JS
12376 return IRQ_NONE;
12377 }
12378
9399627f 12379 /* Clear attention sources except link and error attentions */
9940b97b
JS
12380 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12381 spin_unlock(&phba->hbalock);
12382 return IRQ_HANDLED;
12383 }
a747c9ce
JS
12384 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12385 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12386 phba->HCregaddr);
9399627f 12387 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12388 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12389 readl(phba->HAregaddr); /* flush */
12390 spin_unlock(&phba->hbalock);
12391
12392 /*
12393 * Invokes slow-path host attention interrupt handling as appropriate.
12394 */
12395
12396 /* status of events with mailbox and link attention */
12397 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12398
12399 /* status of events with ELS ring */
12400 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12401 status2 >>= (4*LPFC_ELS_RING);
12402
12403 if (status1 || (status2 & HA_RXMASK))
3772a991 12404 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12405 else
12406 sp_irq_rc = IRQ_NONE;
12407
12408 /*
12409 * Invoke fast-path host attention interrupt handling as appropriate.
12410 */
12411
12412 /* status of events with FCP ring */
12413 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12414 status1 >>= (4*LPFC_FCP_RING);
12415
12416 /* status of events with extra ring */
12417 if (phba->cfg_multi_ring_support == 2) {
12418 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12419 status2 >>= (4*LPFC_EXTRA_RING);
12420 } else
12421 status2 = 0;
12422
12423 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12424 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12425 else
12426 fp_irq_rc = IRQ_NONE;
dea3101e 12427
9399627f
JS
12428 /* Return device-level interrupt handling status */
12429 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12430} /* lpfc_sli_intr_handler */
4f774513
JS
12431
12432/**
12433 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12434 * @phba: pointer to lpfc hba data structure.
12435 *
12436 * This routine is invoked by the worker thread to process all the pending
12437 * SLI4 FCP abort XRI events.
12438 **/
12439void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12440{
12441 struct lpfc_cq_event *cq_event;
12442
12443 /* First, declare the fcp xri abort event has been handled */
12444 spin_lock_irq(&phba->hbalock);
12445 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12446 spin_unlock_irq(&phba->hbalock);
12447 /* Now, handle all the fcp xri abort events */
12448 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12449 /* Get the first event from the head of the event queue */
12450 spin_lock_irq(&phba->hbalock);
12451 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12452 cq_event, struct lpfc_cq_event, list);
12453 spin_unlock_irq(&phba->hbalock);
12454 /* Notify aborted XRI for FCP work queue */
12455 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12456 /* Free the event processed back to the free pool */
12457 lpfc_sli4_cq_event_release(phba, cq_event);
12458 }
12459}
12460
12461/**
12462 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12463 * @phba: pointer to lpfc hba data structure.
12464 *
12465 * This routine is invoked by the worker thread to process all the pending
12466 * SLI4 els abort xri events.
12467 **/
12468void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12469{
12470 struct lpfc_cq_event *cq_event;
12471
12472 /* First, declare the els xri abort event has been handled */
12473 spin_lock_irq(&phba->hbalock);
12474 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12475 spin_unlock_irq(&phba->hbalock);
12476 /* Now, handle all the els xri abort events */
12477 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12478 /* Get the first event from the head of the event queue */
12479 spin_lock_irq(&phba->hbalock);
12480 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12481 cq_event, struct lpfc_cq_event, list);
12482 spin_unlock_irq(&phba->hbalock);
12483 /* Notify aborted XRI for ELS work queue */
12484 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12485 /* Free the event processed back to the free pool */
12486 lpfc_sli4_cq_event_release(phba, cq_event);
12487 }
12488}
12489
341af102
JS
12490/**
12491 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12492 * @phba: pointer to lpfc hba data structure
12493 * @pIocbIn: pointer to the rspiocbq
12494 * @pIocbOut: pointer to the cmdiocbq
12495 * @wcqe: pointer to the complete wcqe
12496 *
12497 * This routine transfers the fields of a command iocbq to a response iocbq
12498 * by copying all the IOCB fields from command iocbq and transferring the
12499 * completion status information from the complete wcqe.
12500 **/
4f774513 12501static void
341af102
JS
12502lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12503 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12504 struct lpfc_iocbq *pIocbOut,
12505 struct lpfc_wcqe_complete *wcqe)
12506{
af22741c 12507 int numBdes, i;
341af102 12508 unsigned long iflags;
af22741c
JS
12509 uint32_t status, max_response;
12510 struct lpfc_dmabuf *dmabuf;
12511 struct ulp_bde64 *bpl, bde;
4f774513
JS
12512 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12513
12514 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12515 sizeof(struct lpfc_iocbq) - offset);
4f774513 12516 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12517 status = bf_get(lpfc_wcqe_c_status, wcqe);
12518 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12519 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12520 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12521 pIocbIn->iocb.un.fcpi.fcpi_parm =
12522 pIocbOut->iocb.un.fcpi.fcpi_parm -
12523 wcqe->total_data_placed;
12524 else
12525 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12526 else {
4f774513 12527 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12528 switch (pIocbOut->iocb.ulpCommand) {
12529 case CMD_ELS_REQUEST64_CR:
12530 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12531 bpl = (struct ulp_bde64 *)dmabuf->virt;
12532 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12533 max_response = bde.tus.f.bdeSize;
12534 break;
12535 case CMD_GEN_REQUEST64_CR:
12536 max_response = 0;
12537 if (!pIocbOut->context3)
12538 break;
12539 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12540 sizeof(struct ulp_bde64);
12541 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12542 bpl = (struct ulp_bde64 *)dmabuf->virt;
12543 for (i = 0; i < numBdes; i++) {
12544 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12545 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12546 max_response += bde.tus.f.bdeSize;
12547 }
12548 break;
12549 default:
12550 max_response = wcqe->total_data_placed;
12551 break;
12552 }
12553 if (max_response < wcqe->total_data_placed)
12554 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12555 else
12556 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12557 wcqe->total_data_placed;
695a814e 12558 }
341af102 12559
acd6859b
JS
12560 /* Convert BG errors for completion status */
12561 if (status == CQE_STATUS_DI_ERROR) {
12562 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12563
12564 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12565 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12566 else
12567 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12568
12569 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12570 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12571 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12572 BGS_GUARD_ERR_MASK;
12573 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12574 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12575 BGS_APPTAG_ERR_MASK;
12576 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12577 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12578 BGS_REFTAG_ERR_MASK;
12579
12580 /* Check to see if there was any good data before the error */
12581 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12582 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12583 BGS_HI_WATER_MARK_PRESENT_MASK;
12584 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12585 wcqe->total_data_placed;
12586 }
12587
12588 /*
12589 * Set ALL the error bits to indicate we don't know what
12590 * type of error it is.
12591 */
12592 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12593 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12594 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12595 BGS_GUARD_ERR_MASK);
12596 }
12597
341af102
JS
12598 /* Pick up HBA exchange busy condition */
12599 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12600 spin_lock_irqsave(&phba->hbalock, iflags);
12601 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12602 spin_unlock_irqrestore(&phba->hbalock, iflags);
12603 }
4f774513
JS
12604}
12605
45ed1190
JS
12606/**
12607 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12608 * @phba: Pointer to HBA context object.
12609 * @wcqe: Pointer to work-queue completion queue entry.
12610 *
12611 * This routine handles an ELS work-queue completion event and construct
12612 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12613 * discovery engine to handle.
12614 *
12615 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12616 **/
12617static struct lpfc_iocbq *
12618lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12619 struct lpfc_iocbq *irspiocbq)
12620{
895427bd 12621 struct lpfc_sli_ring *pring;
45ed1190
JS
12622 struct lpfc_iocbq *cmdiocbq;
12623 struct lpfc_wcqe_complete *wcqe;
12624 unsigned long iflags;
12625
895427bd 12626 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12627 if (unlikely(!pring))
12628 return NULL;
895427bd 12629
45ed1190 12630 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12631 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12632 pring->stats.iocb_event++;
12633 /* Look up the ELS command IOCB and create pseudo response IOCB */
12634 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12635 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12636 if (unlikely(!cmdiocbq)) {
401bb416 12637 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12638 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12639 "0386 ELS complete with no corresponding "
401bb416
DK
12640 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12641 wcqe->word0, wcqe->total_data_placed,
12642 wcqe->parameter, wcqe->word3);
45ed1190
JS
12643 lpfc_sli_release_iocbq(phba, irspiocbq);
12644 return NULL;
12645 }
12646
401bb416
DK
12647 /* Put the iocb back on the txcmplq */
12648 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12649 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12650
45ed1190 12651 /* Fake the irspiocbq and copy necessary response information */
341af102 12652 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12653
12654 return irspiocbq;
12655}
12656
8a5ca109
JS
12657inline struct lpfc_cq_event *
12658lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12659{
12660 struct lpfc_cq_event *cq_event;
12661
12662 /* Allocate a new internal CQ_EVENT entry */
12663 cq_event = lpfc_sli4_cq_event_alloc(phba);
12664 if (!cq_event) {
12665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12666 "0602 Failed to alloc CQ_EVENT entry\n");
12667 return NULL;
12668 }
12669
12670 /* Move the CQE into the event */
12671 memcpy(&cq_event->cqe, entry, size);
12672 return cq_event;
12673}
12674
04c68496
JS
12675/**
12676 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12677 * @phba: Pointer to HBA context object.
12678 * @cqe: Pointer to mailbox completion queue entry.
12679 *
12680 * This routine process a mailbox completion queue entry with asynchrous
12681 * event.
12682 *
12683 * Return: true if work posted to worker thread, otherwise false.
12684 **/
12685static bool
12686lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12687{
12688 struct lpfc_cq_event *cq_event;
12689 unsigned long iflags;
12690
12691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12692 "0392 Async Event: word0:x%x, word1:x%x, "
12693 "word2:x%x, word3:x%x\n", mcqe->word0,
12694 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12695
8a5ca109
JS
12696 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12697 if (!cq_event)
04c68496 12698 return false;
04c68496
JS
12699 spin_lock_irqsave(&phba->hbalock, iflags);
12700 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12701 /* Set the async event flag */
12702 phba->hba_flag |= ASYNC_EVENT;
12703 spin_unlock_irqrestore(&phba->hbalock, iflags);
12704
12705 return true;
12706}
12707
12708/**
12709 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12710 * @phba: Pointer to HBA context object.
12711 * @cqe: Pointer to mailbox completion queue entry.
12712 *
12713 * This routine process a mailbox completion queue entry with mailbox
12714 * completion event.
12715 *
12716 * Return: true if work posted to worker thread, otherwise false.
12717 **/
12718static bool
12719lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12720{
12721 uint32_t mcqe_status;
12722 MAILBOX_t *mbox, *pmbox;
12723 struct lpfc_mqe *mqe;
12724 struct lpfc_vport *vport;
12725 struct lpfc_nodelist *ndlp;
12726 struct lpfc_dmabuf *mp;
12727 unsigned long iflags;
12728 LPFC_MBOXQ_t *pmb;
12729 bool workposted = false;
12730 int rc;
12731
12732 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12733 if (!bf_get(lpfc_trailer_completed, mcqe))
12734 goto out_no_mqe_complete;
12735
12736 /* Get the reference to the active mbox command */
12737 spin_lock_irqsave(&phba->hbalock, iflags);
12738 pmb = phba->sli.mbox_active;
12739 if (unlikely(!pmb)) {
12740 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12741 "1832 No pending MBOX command to handle\n");
12742 spin_unlock_irqrestore(&phba->hbalock, iflags);
12743 goto out_no_mqe_complete;
12744 }
12745 spin_unlock_irqrestore(&phba->hbalock, iflags);
12746 mqe = &pmb->u.mqe;
12747 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12748 mbox = phba->mbox;
12749 vport = pmb->vport;
12750
12751 /* Reset heartbeat timer */
12752 phba->last_completion_time = jiffies;
12753 del_timer(&phba->sli.mbox_tmo);
12754
12755 /* Move mbox data to caller's mailbox region, do endian swapping */
12756 if (pmb->mbox_cmpl && mbox)
12757 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12758
73d91e50
JS
12759 /*
12760 * For mcqe errors, conditionally move a modified error code to
12761 * the mbox so that the error will not be missed.
12762 */
12763 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12764 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12765 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12766 bf_set(lpfc_mqe_status, mqe,
12767 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12768 }
04c68496
JS
12769 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12770 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12771 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12772 "MBOX dflt rpi: status:x%x rpi:x%x",
12773 mcqe_status,
12774 pmbox->un.varWords[0], 0);
12775 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12776 mp = (struct lpfc_dmabuf *)(pmb->context1);
12777 ndlp = (struct lpfc_nodelist *)pmb->context2;
12778 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12779 * RID of the PPI using the same mbox buffer.
12780 */
12781 lpfc_unreg_login(phba, vport->vpi,
12782 pmbox->un.varWords[0], pmb);
12783 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12784 pmb->context1 = mp;
12785 pmb->context2 = ndlp;
12786 pmb->vport = vport;
12787 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12788 if (rc != MBX_BUSY)
12789 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12790 LOG_SLI, "0385 rc should "
12791 "have been MBX_BUSY\n");
12792 if (rc != MBX_NOT_FINISHED)
12793 goto send_current_mbox;
12794 }
12795 }
12796 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12797 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12798 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12799
12800 /* There is mailbox completion work to do */
12801 spin_lock_irqsave(&phba->hbalock, iflags);
12802 __lpfc_mbox_cmpl_put(phba, pmb);
12803 phba->work_ha |= HA_MBATT;
12804 spin_unlock_irqrestore(&phba->hbalock, iflags);
12805 workposted = true;
12806
12807send_current_mbox:
12808 spin_lock_irqsave(&phba->hbalock, iflags);
12809 /* Release the mailbox command posting token */
12810 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12811 /* Setting active mailbox pointer need to be in sync to flag clear */
12812 phba->sli.mbox_active = NULL;
12813 spin_unlock_irqrestore(&phba->hbalock, iflags);
12814 /* Wake up worker thread to post the next pending mailbox command */
12815 lpfc_worker_wake_up(phba);
12816out_no_mqe_complete:
12817 if (bf_get(lpfc_trailer_consumed, mcqe))
12818 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12819 return workposted;
12820}
12821
12822/**
12823 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12824 * @phba: Pointer to HBA context object.
12825 * @cqe: Pointer to mailbox completion queue entry.
12826 *
12827 * This routine process a mailbox completion queue entry, it invokes the
12828 * proper mailbox complete handling or asynchrous event handling routine
12829 * according to the MCQE's async bit.
12830 *
12831 * Return: true if work posted to worker thread, otherwise false.
12832 **/
12833static bool
12834lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12835{
12836 struct lpfc_mcqe mcqe;
12837 bool workposted;
12838
12839 /* Copy the mailbox MCQE and convert endian order as needed */
12840 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12841
12842 /* Invoke the proper event handling routine */
12843 if (!bf_get(lpfc_trailer_async, &mcqe))
12844 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12845 else
12846 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12847 return workposted;
12848}
12849
4f774513
JS
12850/**
12851 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12852 * @phba: Pointer to HBA context object.
2a76a283 12853 * @cq: Pointer to associated CQ
4f774513
JS
12854 * @wcqe: Pointer to work-queue completion queue entry.
12855 *
12856 * This routine handles an ELS work-queue completion event.
12857 *
12858 * Return: true if work posted to worker thread, otherwise false.
12859 **/
12860static bool
2a76a283 12861lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12862 struct lpfc_wcqe_complete *wcqe)
12863{
4f774513
JS
12864 struct lpfc_iocbq *irspiocbq;
12865 unsigned long iflags;
2a76a283 12866 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12867 int txq_cnt = 0;
12868 int txcmplq_cnt = 0;
12869 int fcp_txcmplq_cnt = 0;
4f774513 12870
45ed1190 12871 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12872 irspiocbq = lpfc_sli_get_iocbq(phba);
12873 if (!irspiocbq) {
0e9bb8d7
JS
12874 if (!list_empty(&pring->txq))
12875 txq_cnt++;
12876 if (!list_empty(&pring->txcmplq))
12877 txcmplq_cnt++;
4f774513 12878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12879 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12880 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12881 txq_cnt, phba->iocb_cnt,
12882 fcp_txcmplq_cnt,
12883 txcmplq_cnt);
45ed1190 12884 return false;
4f774513 12885 }
4f774513 12886
45ed1190
JS
12887 /* Save off the slow-path queue event for work thread to process */
12888 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12889 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12890 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12891 &phba->sli4_hba.sp_queue_event);
12892 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12893 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12894
45ed1190 12895 return true;
4f774513
JS
12896}
12897
12898/**
12899 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12900 * @phba: Pointer to HBA context object.
12901 * @wcqe: Pointer to work-queue completion queue entry.
12902 *
3f8b6fb7 12903 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
12904 * proper WQ release routine to the slow-path WQ.
12905 **/
12906static void
12907lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12908 struct lpfc_wcqe_release *wcqe)
12909{
2e90f4b5
JS
12910 /* sanity check on queue memory */
12911 if (unlikely(!phba->sli4_hba.els_wq))
12912 return;
4f774513
JS
12913 /* Check for the slow-path ELS work queue */
12914 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12915 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12916 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12917 else
12918 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12919 "2579 Slow-path wqe consume event carries "
12920 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12921 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12922 phba->sli4_hba.els_wq->queue_id);
12923}
12924
12925/**
12926 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12927 * @phba: Pointer to HBA context object.
12928 * @cq: Pointer to a WQ completion queue.
12929 * @wcqe: Pointer to work-queue completion queue entry.
12930 *
12931 * This routine handles an XRI abort event.
12932 *
12933 * Return: true if work posted to worker thread, otherwise false.
12934 **/
12935static bool
12936lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12937 struct lpfc_queue *cq,
12938 struct sli4_wcqe_xri_aborted *wcqe)
12939{
12940 bool workposted = false;
12941 struct lpfc_cq_event *cq_event;
12942 unsigned long iflags;
12943
4f774513
JS
12944 switch (cq->subtype) {
12945 case LPFC_FCP:
8a5ca109
JS
12946 cq_event = lpfc_cq_event_setup(
12947 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12948 if (!cq_event)
12949 return false;
4f774513
JS
12950 spin_lock_irqsave(&phba->hbalock, iflags);
12951 list_add_tail(&cq_event->list,
12952 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12953 /* Set the fcp xri abort event flag */
12954 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12955 spin_unlock_irqrestore(&phba->hbalock, iflags);
12956 workposted = true;
12957 break;
422c4cb7 12958 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 12959 case LPFC_ELS:
8a5ca109
JS
12960 cq_event = lpfc_cq_event_setup(
12961 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12962 if (!cq_event)
12963 return false;
4f774513
JS
12964 spin_lock_irqsave(&phba->hbalock, iflags);
12965 list_add_tail(&cq_event->list,
12966 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12967 /* Set the els xri abort event flag */
12968 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12969 spin_unlock_irqrestore(&phba->hbalock, iflags);
12970 workposted = true;
12971 break;
318083ad 12972 case LPFC_NVME:
8a5ca109
JS
12973 /* Notify aborted XRI for NVME work queue */
12974 if (phba->nvmet_support)
12975 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12976 else
12977 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12978
12979 workposted = false;
318083ad 12980 break;
4f774513
JS
12981 default:
12982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
12983 "0603 Invalid CQ subtype %d: "
12984 "%08x %08x %08x %08x\n",
12985 cq->subtype, wcqe->word0, wcqe->parameter,
12986 wcqe->word2, wcqe->word3);
4f774513
JS
12987 workposted = false;
12988 break;
12989 }
12990 return workposted;
12991}
12992
4f774513
JS
12993/**
12994 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12995 * @phba: Pointer to HBA context object.
12996 * @rcqe: Pointer to receive-queue completion queue entry.
12997 *
12998 * This routine process a receive-queue completion queue entry.
12999 *
13000 * Return: true if work posted to worker thread, otherwise false.
13001 **/
13002static bool
4d9ab994 13003lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 13004{
4f774513 13005 bool workposted = false;
895427bd 13006 struct fc_frame_header *fc_hdr;
4f774513
JS
13007 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13008 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 13009 struct lpfc_nvmet_tgtport *tgtp;
4f774513 13010 struct hbq_dmabuf *dma_buf;
7851fe2c 13011 uint32_t status, rq_id;
4f774513
JS
13012 unsigned long iflags;
13013
2e90f4b5
JS
13014 /* sanity check on queue memory */
13015 if (unlikely(!hrq) || unlikely(!drq))
13016 return workposted;
13017
7851fe2c
JS
13018 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13019 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13020 else
13021 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13022 if (rq_id != hrq->queue_id)
4f774513
JS
13023 goto out;
13024
4d9ab994 13025 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
13026 switch (status) {
13027 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13029 "2537 Receive Frame Truncated!!\n");
13030 case FC_STATUS_RQ_SUCCESS:
13031 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13032 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
13033 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13034 if (!dma_buf) {
b84daac9 13035 hrq->RQ_no_buf_found++;
4f774513
JS
13036 spin_unlock_irqrestore(&phba->hbalock, iflags);
13037 goto out;
13038 }
b84daac9 13039 hrq->RQ_rcv_buf++;
547077a4 13040 hrq->RQ_buf_posted--;
4d9ab994 13041 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
13042
13043 /* If a NVME LS event (type 0x28), treat it as Fast path */
13044 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13045
4f774513 13046 /* save off the frame for the word thread to process */
4d9ab994 13047 list_add_tail(&dma_buf->cq_event.list,
45ed1190 13048 &phba->sli4_hba.sp_queue_event);
4f774513 13049 /* Frame received */
45ed1190 13050 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
13051 spin_unlock_irqrestore(&phba->hbalock, iflags);
13052 workposted = true;
13053 break;
4f774513 13054 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13055 if (phba->nvmet_support) {
13056 tgtp = phba->targetport->private;
13057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13058 "6402 RQE Error x%x, posted %d err_cnt "
13059 "%d: %x %x %x\n",
13060 status, hrq->RQ_buf_posted,
13061 hrq->RQ_no_posted_buf,
13062 atomic_read(&tgtp->rcv_fcp_cmd_in),
13063 atomic_read(&tgtp->rcv_fcp_cmd_out),
13064 atomic_read(&tgtp->xmt_fcp_release));
13065 }
13066 /* fallthrough */
13067
13068 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 13069 hrq->RQ_no_posted_buf++;
4f774513
JS
13070 /* Post more buffers if possible */
13071 spin_lock_irqsave(&phba->hbalock, iflags);
13072 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13073 spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 workposted = true;
13075 break;
13076 }
13077out:
13078 return workposted;
4f774513
JS
13079}
13080
4d9ab994
JS
13081/**
13082 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13083 * @phba: Pointer to HBA context object.
13084 * @cq: Pointer to the completion queue.
13085 * @wcqe: Pointer to a completion queue entry.
13086 *
25985edc 13087 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
13088 * entry.
13089 *
13090 * Return: true if work posted to worker thread, otherwise false.
13091 **/
13092static bool
13093lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13094 struct lpfc_cqe *cqe)
13095{
45ed1190 13096 struct lpfc_cqe cqevt;
4d9ab994
JS
13097 bool workposted = false;
13098
13099 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 13100 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
13101
13102 /* Check and process for different type of WCQE and dispatch */
45ed1190 13103 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 13104 case CQE_CODE_COMPL_WQE:
45ed1190 13105 /* Process the WQ/RQ complete event */
bc73905a 13106 phba->last_completion_time = jiffies;
2a76a283 13107 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 13108 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
13109 break;
13110 case CQE_CODE_RELEASE_WQE:
13111 /* Process the WQ release event */
13112 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 13113 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
13114 break;
13115 case CQE_CODE_XRI_ABORTED:
13116 /* Process the WQ XRI abort event */
bc73905a 13117 phba->last_completion_time = jiffies;
4d9ab994 13118 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 13119 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
13120 break;
13121 case CQE_CODE_RECEIVE:
7851fe2c 13122 case CQE_CODE_RECEIVE_V1:
4d9ab994 13123 /* Process the RQ event */
bc73905a 13124 phba->last_completion_time = jiffies;
4d9ab994 13125 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 13126 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
13127 break;
13128 default:
13129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13130 "0388 Not a valid WCQE code: x%x\n",
45ed1190 13131 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
13132 break;
13133 }
13134 return workposted;
13135}
13136
4f774513
JS
13137/**
13138 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13139 * @phba: Pointer to HBA context object.
13140 * @eqe: Pointer to fast-path event queue entry.
13141 *
13142 * This routine process a event queue entry from the slow-path event queue.
13143 * It will check the MajorCode and MinorCode to determine this is for a
13144 * completion event on a completion queue, if not, an error shall be logged
13145 * and just return. Otherwise, it will get to the corresponding completion
13146 * queue and process all the entries on that completion queue, rearm the
13147 * completion queue, and then return.
13148 *
13149 **/
f485c18d 13150static void
67d12733
JS
13151lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13152 struct lpfc_queue *speq)
4f774513 13153{
67d12733 13154 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13155 uint16_t cqid;
13156
4f774513 13157 /* Get the reference to the corresponding CQ */
cb5172ea 13158 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13159
4f774513
JS
13160 list_for_each_entry(childq, &speq->child_list, list) {
13161 if (childq->queue_id == cqid) {
13162 cq = childq;
13163 break;
13164 }
13165 }
13166 if (unlikely(!cq)) {
75baf696
JS
13167 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13169 "0365 Slow-path CQ identifier "
13170 "(%d) does not exist\n", cqid);
f485c18d 13171 return;
4f774513
JS
13172 }
13173
895427bd
JS
13174 /* Save EQ associated with this CQ */
13175 cq->assoc_qp = speq;
13176
f485c18d
DK
13177 if (!queue_work(phba->wq, &cq->spwork))
13178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13179 "0390 Cannot schedule soft IRQ "
13180 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13181 cqid, cq->queue_id, smp_processor_id());
13182}
13183
13184/**
13185 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13186 * @phba: Pointer to HBA context object.
13187 *
13188 * This routine process a event queue entry from the slow-path event queue.
13189 * It will check the MajorCode and MinorCode to determine this is for a
13190 * completion event on a completion queue, if not, an error shall be logged
13191 * and just return. Otherwise, it will get to the corresponding completion
13192 * queue and process all the entries on that completion queue, rearm the
13193 * completion queue, and then return.
13194 *
13195 **/
13196static void
13197lpfc_sli4_sp_process_cq(struct work_struct *work)
13198{
13199 struct lpfc_queue *cq =
13200 container_of(work, struct lpfc_queue, spwork);
13201 struct lpfc_hba *phba = cq->phba;
13202 struct lpfc_cqe *cqe;
13203 bool workposted = false;
13204 int ccount = 0;
13205
4f774513
JS
13206 /* Process all the entries to the CQ */
13207 switch (cq->type) {
13208 case LPFC_MCQ:
13209 while ((cqe = lpfc_sli4_cq_get(cq))) {
13210 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13211 if (!(++ccount % cq->entry_repost))
7869da18 13212 break;
b84daac9 13213 cq->CQ_mbox++;
4f774513
JS
13214 }
13215 break;
13216 case LPFC_WCQ:
13217 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13218 if (cq->subtype == LPFC_FCP ||
13219 cq->subtype == LPFC_NVME) {
13220#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13221 if (phba->ktime_on)
13222 cq->isr_timestamp = ktime_get_ns();
13223 else
13224 cq->isr_timestamp = 0;
13225#endif
895427bd 13226 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13227 cqe);
c8a4ce0b 13228 } else {
0558056c
JS
13229 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13230 cqe);
c8a4ce0b 13231 }
f485c18d 13232 if (!(++ccount % cq->entry_repost))
7869da18 13233 break;
4f774513 13234 }
b84daac9
JS
13235
13236 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13237 if (ccount > cq->CQ_max_cqe)
13238 cq->CQ_max_cqe = ccount;
4f774513
JS
13239 break;
13240 default:
13241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13242 "0370 Invalid completion queue type (%d)\n",
13243 cq->type);
f485c18d 13244 return;
4f774513
JS
13245 }
13246
13247 /* Catch the no cq entry condition, log an error */
f485c18d 13248 if (unlikely(ccount == 0))
4f774513
JS
13249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13250 "0371 No entry from the CQ: identifier "
13251 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13252
13253 /* In any case, flash and re-arm the RCQ */
b71413dd 13254 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13255
13256 /* wake up worker thread if there are works to be done */
13257 if (workposted)
13258 lpfc_worker_wake_up(phba);
13259}
13260
13261/**
13262 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13263 * @phba: Pointer to HBA context object.
13264 * @cq: Pointer to associated CQ
13265 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13266 *
13267 * This routine process a fast-path work queue completion entry from fast-path
13268 * event queue for FCP command response completion.
13269 **/
13270static void
2a76a283 13271lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13272 struct lpfc_wcqe_complete *wcqe)
13273{
2a76a283 13274 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13275 struct lpfc_iocbq *cmdiocbq;
13276 struct lpfc_iocbq irspiocbq;
13277 unsigned long iflags;
13278
4f774513
JS
13279 /* Check for response status */
13280 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13281 /* If resource errors reported from HBA, reduce queue
13282 * depth of the SCSI device.
13283 */
e3d2b802
JS
13284 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13285 IOSTAT_LOCAL_REJECT)) &&
13286 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13287 IOERR_NO_RESOURCES))
4f774513 13288 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13289
4f774513
JS
13290 /* Log the error status */
13291 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13292 "0373 FCP complete error: status=x%x, "
13293 "hw_status=x%x, total_data_specified=%d, "
13294 "parameter=x%x, word3=x%x\n",
13295 bf_get(lpfc_wcqe_c_status, wcqe),
13296 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13297 wcqe->total_data_placed, wcqe->parameter,
13298 wcqe->word3);
13299 }
13300
13301 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13302 spin_lock_irqsave(&pring->ring_lock, iflags);
13303 pring->stats.iocb_event++;
4f774513
JS
13304 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13305 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13306 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13307 if (unlikely(!cmdiocbq)) {
13308 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13309 "0374 FCP complete with no corresponding "
13310 "cmdiocb: iotag (%d)\n",
13311 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13312 return;
13313 }
c8a4ce0b
DK
13314#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13315 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13316#endif
895427bd
JS
13317 if (cmdiocbq->iocb_cmpl == NULL) {
13318 if (cmdiocbq->wqe_cmpl) {
13319 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13320 spin_lock_irqsave(&phba->hbalock, iflags);
13321 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13322 spin_unlock_irqrestore(&phba->hbalock, iflags);
13323 }
13324
13325 /* Pass the cmd_iocb and the wcqe to the upper layer */
13326 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13327 return;
13328 }
4f774513
JS
13329 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13330 "0375 FCP cmdiocb not callback function "
13331 "iotag: (%d)\n",
13332 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13333 return;
13334 }
13335
13336 /* Fake the irspiocb and copy necessary response information */
341af102 13337 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13338
0f65ff68
JS
13339 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13340 spin_lock_irqsave(&phba->hbalock, iflags);
13341 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13342 spin_unlock_irqrestore(&phba->hbalock, iflags);
13343 }
13344
4f774513
JS
13345 /* Pass the cmd_iocb and the rsp state to the upper layer */
13346 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13347}
13348
13349/**
13350 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13351 * @phba: Pointer to HBA context object.
13352 * @cq: Pointer to completion queue.
13353 * @wcqe: Pointer to work-queue completion queue entry.
13354 *
3f8b6fb7 13355 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13356 * proper WQ release routine to the slow-path WQ.
13357 **/
13358static void
13359lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13360 struct lpfc_wcqe_release *wcqe)
13361{
13362 struct lpfc_queue *childwq;
13363 bool wqid_matched = false;
895427bd 13364 uint16_t hba_wqid;
4f774513
JS
13365
13366 /* Check for fast-path FCP work queue release */
895427bd 13367 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13368 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13369 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13370 lpfc_sli4_wq_release(childwq,
13371 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
6e8e1c14
JS
13372 if (childwq->q_flag & HBA_NVMET_WQFULL)
13373 lpfc_nvmet_wqfull_process(phba, childwq);
4f774513
JS
13374 wqid_matched = true;
13375 break;
13376 }
13377 }
13378 /* Report warning log message if no match found */
13379 if (wqid_matched != true)
13380 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13381 "2580 Fast-path wqe consume event carries "
895427bd 13382 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13383}
13384
13385/**
2d7dbc4c
JS
13386 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13387 * @phba: Pointer to HBA context object.
13388 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13389 *
2d7dbc4c
JS
13390 * This routine process a receive-queue completion queue entry.
13391 *
13392 * Return: true if work posted to worker thread, otherwise false.
13393 **/
13394static bool
13395lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13396 struct lpfc_rcqe *rcqe)
13397{
13398 bool workposted = false;
13399 struct lpfc_queue *hrq;
13400 struct lpfc_queue *drq;
13401 struct rqb_dmabuf *dma_buf;
13402 struct fc_frame_header *fc_hdr;
547077a4 13403 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13404 uint32_t status, rq_id;
13405 unsigned long iflags;
13406 uint32_t fctl, idx;
13407
13408 if ((phba->nvmet_support == 0) ||
13409 (phba->sli4_hba.nvmet_cqset == NULL))
13410 return workposted;
13411
13412 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13413 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13414 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13415
13416 /* sanity check on queue memory */
13417 if (unlikely(!hrq) || unlikely(!drq))
13418 return workposted;
13419
13420 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13421 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13422 else
13423 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13424
13425 if ((phba->nvmet_support == 0) ||
13426 (rq_id != hrq->queue_id))
13427 return workposted;
13428
13429 status = bf_get(lpfc_rcqe_status, rcqe);
13430 switch (status) {
13431 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13433 "6126 Receive Frame Truncated!!\n");
78e1d200 13434 /* Drop thru */
2d7dbc4c 13435 case FC_STATUS_RQ_SUCCESS:
2d7dbc4c 13436 spin_lock_irqsave(&phba->hbalock, iflags);
cbc5de1b 13437 lpfc_sli4_rq_release(hrq, drq);
2d7dbc4c
JS
13438 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13439 if (!dma_buf) {
13440 hrq->RQ_no_buf_found++;
13441 spin_unlock_irqrestore(&phba->hbalock, iflags);
13442 goto out;
13443 }
13444 spin_unlock_irqrestore(&phba->hbalock, iflags);
13445 hrq->RQ_rcv_buf++;
547077a4 13446 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13447 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13448
13449 /* Just some basic sanity checks on FCP Command frame */
13450 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13451 fc_hdr->fh_f_ctl[1] << 8 |
13452 fc_hdr->fh_f_ctl[2]);
13453 if (((fctl &
13454 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13455 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13456 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13457 goto drop;
13458
13459 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13460 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13461 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13462 phba, idx, dma_buf,
c8a4ce0b 13463 cq->isr_timestamp);
2d7dbc4c
JS
13464 return false;
13465 }
13466drop:
13467 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13468 break;
2d7dbc4c 13469 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13470 if (phba->nvmet_support) {
13471 tgtp = phba->targetport->private;
13472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13473 "6401 RQE Error x%x, posted %d err_cnt "
13474 "%d: %x %x %x\n",
13475 status, hrq->RQ_buf_posted,
13476 hrq->RQ_no_posted_buf,
13477 atomic_read(&tgtp->rcv_fcp_cmd_in),
13478 atomic_read(&tgtp->rcv_fcp_cmd_out),
13479 atomic_read(&tgtp->xmt_fcp_release));
13480 }
13481 /* fallthrough */
13482
13483 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13484 hrq->RQ_no_posted_buf++;
13485 /* Post more buffers if possible */
2d7dbc4c
JS
13486 break;
13487 }
13488out:
13489 return workposted;
13490}
13491
4f774513 13492/**
895427bd 13493 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13494 * @cq: Pointer to the completion queue.
13495 * @eqe: Pointer to fast-path completion queue entry.
13496 *
13497 * This routine process a fast-path work queue completion entry from fast-path
13498 * event queue for FCP command response completion.
13499 **/
13500static int
895427bd 13501lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13502 struct lpfc_cqe *cqe)
13503{
13504 struct lpfc_wcqe_release wcqe;
13505 bool workposted = false;
13506
13507 /* Copy the work queue CQE and convert endian order if needed */
13508 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13509
13510 /* Check and process for different type of WCQE and dispatch */
13511 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13512 case CQE_CODE_COMPL_WQE:
895427bd 13513 case CQE_CODE_NVME_ERSP:
b84daac9 13514 cq->CQ_wq++;
4f774513 13515 /* Process the WQ complete event */
98fc5dd9 13516 phba->last_completion_time = jiffies;
895427bd
JS
13517 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13518 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13519 (struct lpfc_wcqe_complete *)&wcqe);
13520 if (cq->subtype == LPFC_NVME_LS)
13521 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13522 (struct lpfc_wcqe_complete *)&wcqe);
13523 break;
13524 case CQE_CODE_RELEASE_WQE:
b84daac9 13525 cq->CQ_release_wqe++;
4f774513
JS
13526 /* Process the WQ release event */
13527 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13528 (struct lpfc_wcqe_release *)&wcqe);
13529 break;
13530 case CQE_CODE_XRI_ABORTED:
b84daac9 13531 cq->CQ_xri_aborted++;
4f774513 13532 /* Process the WQ XRI abort event */
bc73905a 13533 phba->last_completion_time = jiffies;
4f774513
JS
13534 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13535 (struct sli4_wcqe_xri_aborted *)&wcqe);
13536 break;
895427bd
JS
13537 case CQE_CODE_RECEIVE_V1:
13538 case CQE_CODE_RECEIVE:
13539 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13540 if (cq->subtype == LPFC_NVMET) {
13541 workposted = lpfc_sli4_nvmet_handle_rcqe(
13542 phba, cq, (struct lpfc_rcqe *)&wcqe);
13543 }
895427bd 13544 break;
4f774513
JS
13545 default:
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13547 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13548 bf_get(lpfc_wcqe_c_code, &wcqe));
13549 break;
13550 }
13551 return workposted;
13552}
13553
13554/**
67d12733 13555 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13556 * @phba: Pointer to HBA context object.
13557 * @eqe: Pointer to fast-path event queue entry.
13558 *
13559 * This routine process a event queue entry from the fast-path event queue.
13560 * It will check the MajorCode and MinorCode to determine this is for a
13561 * completion event on a completion queue, if not, an error shall be logged
13562 * and just return. Otherwise, it will get to the corresponding completion
13563 * queue and process all the entries on the completion queue, rearm the
13564 * completion queue, and then return.
13565 **/
f485c18d 13566static void
67d12733
JS
13567lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13568 uint32_t qidx)
4f774513 13569{
895427bd 13570 struct lpfc_queue *cq = NULL;
2d7dbc4c 13571 uint16_t cqid, id;
4f774513 13572
cb5172ea 13573 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13575 "0366 Not a valid completion "
4f774513 13576 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13577 bf_get_le32(lpfc_eqe_major_code, eqe),
13578 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13579 return;
4f774513
JS
13580 }
13581
67d12733
JS
13582 /* Get the reference to the corresponding CQ */
13583 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13584
2d7dbc4c
JS
13585 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13586 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13587 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13588 /* Process NVMET unsol rcv */
13589 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13590 goto process_cq;
13591 }
67d12733
JS
13592 }
13593
895427bd
JS
13594 if (phba->sli4_hba.nvme_cq_map &&
13595 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13596 /* Process NVME / NVMET command completion */
895427bd
JS
13597 cq = phba->sli4_hba.nvme_cq[qidx];
13598 goto process_cq;
2e90f4b5 13599 }
67d12733 13600
895427bd
JS
13601 if (phba->sli4_hba.fcp_cq_map &&
13602 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13603 /* Process FCP command completion */
13604 cq = phba->sli4_hba.fcp_cq[qidx];
13605 goto process_cq;
2e90f4b5 13606 }
895427bd
JS
13607
13608 if (phba->sli4_hba.nvmels_cq &&
13609 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13610 /* Process NVME unsol rcv */
13611 cq = phba->sli4_hba.nvmels_cq;
13612 }
13613
13614 /* Otherwise this is a Slow path event */
13615 if (cq == NULL) {
f485c18d
DK
13616 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13617 return;
4f774513
JS
13618 }
13619
895427bd 13620process_cq:
4f774513
JS
13621 if (unlikely(cqid != cq->queue_id)) {
13622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13623 "0368 Miss-matched fast-path completion "
13624 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13625 cqid, cq->queue_id);
f485c18d 13626 return;
4f774513
JS
13627 }
13628
895427bd
JS
13629 /* Save EQ associated with this CQ */
13630 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13631
f485c18d
DK
13632 if (!queue_work(phba->wq, &cq->irqwork))
13633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13634 "0363 Cannot schedule soft IRQ "
13635 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13636 cqid, cq->queue_id, smp_processor_id());
13637}
13638
13639/**
13640 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13641 * @phba: Pointer to HBA context object.
13642 * @eqe: Pointer to fast-path event queue entry.
13643 *
13644 * This routine process a event queue entry from the fast-path event queue.
13645 * It will check the MajorCode and MinorCode to determine this is for a
13646 * completion event on a completion queue, if not, an error shall be logged
13647 * and just return. Otherwise, it will get to the corresponding completion
13648 * queue and process all the entries on the completion queue, rearm the
13649 * completion queue, and then return.
13650 **/
13651static void
13652lpfc_sli4_hba_process_cq(struct work_struct *work)
13653{
13654 struct lpfc_queue *cq =
13655 container_of(work, struct lpfc_queue, irqwork);
13656 struct lpfc_hba *phba = cq->phba;
13657 struct lpfc_cqe *cqe;
13658 bool workposted = false;
13659 int ccount = 0;
13660
4f774513
JS
13661 /* Process all the entries to the CQ */
13662 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13663#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13664 if (phba->ktime_on)
13665 cq->isr_timestamp = ktime_get_ns();
13666 else
13667 cq->isr_timestamp = 0;
13668#endif
895427bd 13669 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13670 if (!(++ccount % cq->entry_repost))
7869da18 13671 break;
4f774513
JS
13672 }
13673
b84daac9 13674 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13675 if (ccount > cq->CQ_max_cqe)
13676 cq->CQ_max_cqe = ccount;
13677 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13678
4f774513 13679 /* Catch the no cq entry condition */
f485c18d 13680 if (unlikely(ccount == 0))
4f774513
JS
13681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13682 "0369 No entry from fast-path completion "
13683 "queue fcpcqid=%d\n", cq->queue_id);
13684
13685 /* In any case, flash and re-arm the CQ */
b71413dd 13686 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
4f774513
JS
13687
13688 /* wake up worker thread if there are works to be done */
13689 if (workposted)
13690 lpfc_worker_wake_up(phba);
13691}
13692
13693static void
13694lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13695{
13696 struct lpfc_eqe *eqe;
13697
13698 /* walk all the EQ entries and drop on the floor */
13699 while ((eqe = lpfc_sli4_eq_get(eq)))
13700 ;
13701
13702 /* Clear and re-arm the EQ */
b71413dd 13703 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
4f774513
JS
13704}
13705
1ba981fd
JS
13706
13707/**
13708 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13709 * entry
13710 * @phba: Pointer to HBA context object.
13711 * @eqe: Pointer to fast-path event queue entry.
13712 *
13713 * This routine process a event queue entry from the Flash Optimized Fabric
13714 * event queue. It will check the MajorCode and MinorCode to determine this
13715 * is for a completion event on a completion queue, if not, an error shall be
13716 * logged and just return. Otherwise, it will get to the corresponding
13717 * completion queue and process all the entries on the completion queue, rearm
13718 * the completion queue, and then return.
13719 **/
13720static void
13721lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13722{
13723 struct lpfc_queue *cq;
1ba981fd 13724 uint16_t cqid;
1ba981fd
JS
13725
13726 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13728 "9147 Not a valid completion "
13729 "event: majorcode=x%x, minorcode=x%x\n",
13730 bf_get_le32(lpfc_eqe_major_code, eqe),
13731 bf_get_le32(lpfc_eqe_minor_code, eqe));
13732 return;
13733 }
13734
13735 /* Get the reference to the corresponding CQ */
13736 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13737
13738 /* Next check for OAS */
13739 cq = phba->sli4_hba.oas_cq;
13740 if (unlikely(!cq)) {
13741 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13742 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13743 "9148 OAS completion queue "
13744 "does not exist\n");
13745 return;
13746 }
13747
13748 if (unlikely(cqid != cq->queue_id)) {
13749 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13750 "9149 Miss-matched fast-path compl "
13751 "queue id: eqcqid=%d, fcpcqid=%d\n",
13752 cqid, cq->queue_id);
13753 return;
13754 }
13755
d41b65bc
JS
13756 /* Save EQ associated with this CQ */
13757 cq->assoc_qp = phba->sli4_hba.fof_eq;
13758
f485c18d
DK
13759 /* CQ work will be processed on CPU affinitized to this IRQ */
13760 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13762 "0367 Cannot schedule soft IRQ "
13763 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13764 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13765}
13766
13767/**
13768 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13769 * @irq: Interrupt number.
13770 * @dev_id: The device context pointer.
13771 *
13772 * This function is directly called from the PCI layer as an interrupt
13773 * service routine when device with SLI-4 interface spec is enabled with
13774 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13775 * IOCB ring event in the HBA. However, when the device is enabled with either
13776 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13777 * device-level interrupt handler. When the PCI slot is in error recovery
13778 * or the HBA is undergoing initialization, the interrupt handler will not
13779 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13780 * the intrrupt context. This function is called without any lock held.
13781 * It gets the hbalock to access and update SLI data structures. Note that,
13782 * the EQ to CQ are one-to-one map such that the EQ index is
13783 * equal to that of CQ index.
13784 *
13785 * This function returns IRQ_HANDLED when interrupt is handled else it
13786 * returns IRQ_NONE.
13787 **/
13788irqreturn_t
13789lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13790{
13791 struct lpfc_hba *phba;
895427bd 13792 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13793 struct lpfc_queue *eq;
13794 struct lpfc_eqe *eqe;
13795 unsigned long iflag;
13796 int ecount = 0;
1ba981fd
JS
13797
13798 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13799 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13800 phba = hba_eq_hdl->phba;
1ba981fd
JS
13801
13802 if (unlikely(!phba))
13803 return IRQ_NONE;
13804
13805 /* Get to the EQ struct associated with this vector */
13806 eq = phba->sli4_hba.fof_eq;
13807 if (unlikely(!eq))
13808 return IRQ_NONE;
13809
13810 /* Check device state for handling interrupt */
13811 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13812 /* Check again for link_state with lock held */
13813 spin_lock_irqsave(&phba->hbalock, iflag);
13814 if (phba->link_state < LPFC_LINK_DOWN)
13815 /* Flush, clear interrupt, and rearm the EQ */
13816 lpfc_sli4_eq_flush(phba, eq);
13817 spin_unlock_irqrestore(&phba->hbalock, iflag);
13818 return IRQ_NONE;
13819 }
13820
13821 /*
13822 * Process all the event on FCP fast-path EQ
13823 */
13824 while ((eqe = lpfc_sli4_eq_get(eq))) {
13825 lpfc_sli4_fof_handle_eqe(phba, eqe);
13826 if (!(++ecount % eq->entry_repost))
7869da18 13827 break;
1ba981fd
JS
13828 eq->EQ_processed++;
13829 }
13830
13831 /* Track the max number of EQEs processed in 1 intr */
13832 if (ecount > eq->EQ_max_eqe)
13833 eq->EQ_max_eqe = ecount;
13834
13835
13836 if (unlikely(ecount == 0)) {
13837 eq->EQ_no_entry++;
13838
13839 if (phba->intr_type == MSIX)
13840 /* MSI-X treated interrupt served as no EQ share INT */
13841 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13842 "9145 MSI-X interrupt with no EQE\n");
13843 else {
13844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13845 "9146 ISR interrupt with no EQE\n");
13846 /* Non MSI-X treated on interrupt as EQ share INT */
13847 return IRQ_NONE;
13848 }
13849 }
13850 /* Always clear and re-arm the fast-path EQ */
b71413dd 13851 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
1ba981fd
JS
13852 return IRQ_HANDLED;
13853}
13854
4f774513 13855/**
67d12733 13856 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13857 * @irq: Interrupt number.
13858 * @dev_id: The device context pointer.
13859 *
13860 * This function is directly called from the PCI layer as an interrupt
13861 * service routine when device with SLI-4 interface spec is enabled with
13862 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13863 * ring event in the HBA. However, when the device is enabled with either
13864 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13865 * device-level interrupt handler. When the PCI slot is in error recovery
13866 * or the HBA is undergoing initialization, the interrupt handler will not
13867 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13868 * the intrrupt context. This function is called without any lock held.
13869 * It gets the hbalock to access and update SLI data structures. Note that,
13870 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13871 * equal to that of FCP CQ index.
13872 *
67d12733
JS
13873 * The link attention and ELS ring attention events are handled
13874 * by the worker thread. The interrupt handler signals the worker thread
13875 * and returns for these events. This function is called without any lock
13876 * held. It gets the hbalock to access and update SLI data structures.
13877 *
4f774513
JS
13878 * This function returns IRQ_HANDLED when interrupt is handled else it
13879 * returns IRQ_NONE.
13880 **/
13881irqreturn_t
67d12733 13882lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13883{
13884 struct lpfc_hba *phba;
895427bd 13885 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13886 struct lpfc_queue *fpeq;
13887 struct lpfc_eqe *eqe;
13888 unsigned long iflag;
13889 int ecount = 0;
895427bd 13890 int hba_eqidx;
4f774513
JS
13891
13892 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13893 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13894 phba = hba_eq_hdl->phba;
13895 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13896
13897 if (unlikely(!phba))
13898 return IRQ_NONE;
67d12733 13899 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13900 return IRQ_NONE;
4f774513
JS
13901
13902 /* Get to the EQ struct associated with this vector */
895427bd 13903 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13904 if (unlikely(!fpeq))
13905 return IRQ_NONE;
4f774513 13906
ba20c853 13907 if (lpfc_fcp_look_ahead) {
895427bd 13908 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
b71413dd 13909 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
ba20c853 13910 else {
895427bd 13911 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13912 return IRQ_NONE;
13913 }
13914 }
13915
4f774513
JS
13916 /* Check device state for handling interrupt */
13917 if (unlikely(lpfc_intr_state_check(phba))) {
13918 /* Check again for link_state with lock held */
13919 spin_lock_irqsave(&phba->hbalock, iflag);
13920 if (phba->link_state < LPFC_LINK_DOWN)
13921 /* Flush, clear interrupt, and rearm the EQ */
13922 lpfc_sli4_eq_flush(phba, fpeq);
13923 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13924 if (lpfc_fcp_look_ahead)
895427bd 13925 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
13926 return IRQ_NONE;
13927 }
13928
13929 /*
13930 * Process all the event on FCP fast-path EQ
13931 */
13932 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
13933 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13934 if (!(++ecount % fpeq->entry_repost))
7869da18 13935 break;
b84daac9 13936 fpeq->EQ_processed++;
4f774513
JS
13937 }
13938
b84daac9
JS
13939 /* Track the max number of EQEs processed in 1 intr */
13940 if (ecount > fpeq->EQ_max_eqe)
13941 fpeq->EQ_max_eqe = ecount;
13942
4f774513 13943 /* Always clear and re-arm the fast-path EQ */
b71413dd 13944 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
4f774513
JS
13945
13946 if (unlikely(ecount == 0)) {
b84daac9 13947 fpeq->EQ_no_entry++;
ba20c853
JS
13948
13949 if (lpfc_fcp_look_ahead) {
895427bd 13950 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13951 return IRQ_NONE;
13952 }
13953
4f774513
JS
13954 if (phba->intr_type == MSIX)
13955 /* MSI-X treated interrupt served as no EQ share INT */
13956 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13957 "0358 MSI-X interrupt with no EQE\n");
13958 else
13959 /* Non MSI-X treated on interrupt as EQ share INT */
13960 return IRQ_NONE;
13961 }
13962
ba20c853 13963 if (lpfc_fcp_look_ahead)
895427bd
JS
13964 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13965
4f774513
JS
13966 return IRQ_HANDLED;
13967} /* lpfc_sli4_fp_intr_handler */
13968
13969/**
13970 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13971 * @irq: Interrupt number.
13972 * @dev_id: The device context pointer.
13973 *
13974 * This function is the device-level interrupt handler to device with SLI-4
13975 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13976 * interrupt mode is enabled and there is an event in the HBA which requires
13977 * driver attention. This function invokes the slow-path interrupt attention
13978 * handling function and fast-path interrupt attention handling function in
13979 * turn to process the relevant HBA attention events. This function is called
13980 * without any lock held. It gets the hbalock to access and update SLI data
13981 * structures.
13982 *
13983 * This function returns IRQ_HANDLED when interrupt is handled, else it
13984 * returns IRQ_NONE.
13985 **/
13986irqreturn_t
13987lpfc_sli4_intr_handler(int irq, void *dev_id)
13988{
13989 struct lpfc_hba *phba;
67d12733
JS
13990 irqreturn_t hba_irq_rc;
13991 bool hba_handled = false;
895427bd 13992 int qidx;
4f774513
JS
13993
13994 /* Get the driver's phba structure from the dev_id */
13995 phba = (struct lpfc_hba *)dev_id;
13996
13997 if (unlikely(!phba))
13998 return IRQ_NONE;
13999
4f774513
JS
14000 /*
14001 * Invoke fast-path host attention interrupt handling as appropriate.
14002 */
895427bd 14003 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 14004 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 14005 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
14006 if (hba_irq_rc == IRQ_HANDLED)
14007 hba_handled |= true;
4f774513
JS
14008 }
14009
1ba981fd
JS
14010 if (phba->cfg_fof) {
14011 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 14012 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
14013 if (hba_irq_rc == IRQ_HANDLED)
14014 hba_handled |= true;
14015 }
14016
67d12733 14017 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
14018} /* lpfc_sli4_intr_handler */
14019
14020/**
14021 * lpfc_sli4_queue_free - free a queue structure and associated memory
14022 * @queue: The queue structure to free.
14023 *
b595076a 14024 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
14025 * the host resident queue. This function must be called after destroying the
14026 * queue on the HBA.
14027 **/
14028void
14029lpfc_sli4_queue_free(struct lpfc_queue *queue)
14030{
14031 struct lpfc_dmabuf *dmabuf;
14032
14033 if (!queue)
14034 return;
14035
14036 while (!list_empty(&queue->page_list)) {
14037 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14038 list);
81b96eda 14039 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
14040 dmabuf->virt, dmabuf->phys);
14041 kfree(dmabuf);
14042 }
895427bd
JS
14043 if (queue->rqbp) {
14044 lpfc_free_rq_buffer(queue->phba, queue);
14045 kfree(queue->rqbp);
14046 }
d1f525aa
JS
14047
14048 if (!list_empty(&queue->wq_list))
14049 list_del(&queue->wq_list);
14050
4f774513
JS
14051 kfree(queue);
14052 return;
14053}
14054
14055/**
14056 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14057 * @phba: The HBA that this queue is being created on.
81b96eda 14058 * @page_size: The size of a queue page
4f774513
JS
14059 * @entry_size: The size of each queue entry for this queue.
14060 * @entry count: The number of entries that this queue will handle.
14061 *
14062 * This function allocates a queue structure and the DMAable memory used for
14063 * the host resident queue. This function must be called before creating the
14064 * queue on the HBA.
14065 **/
14066struct lpfc_queue *
81b96eda
JS
14067lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14068 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
14069{
14070 struct lpfc_queue *queue;
14071 struct lpfc_dmabuf *dmabuf;
14072 int x, total_qe_count;
14073 void *dma_pointer;
cb5172ea 14074 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 14075
cb5172ea 14076 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14077 hw_page_size = page_size;
cb5172ea 14078
4f774513
JS
14079 queue = kzalloc(sizeof(struct lpfc_queue) +
14080 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14081 if (!queue)
14082 return NULL;
cb5172ea
JS
14083 queue->page_count = (ALIGN(entry_size * entry_count,
14084 hw_page_size))/hw_page_size;
895427bd
JS
14085
14086 /* If needed, Adjust page count to match the max the adapter supports */
14087 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14088 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14089
4f774513 14090 INIT_LIST_HEAD(&queue->list);
895427bd 14091 INIT_LIST_HEAD(&queue->wq_list);
6e8e1c14 14092 INIT_LIST_HEAD(&queue->wqfull_list);
4f774513
JS
14093 INIT_LIST_HEAD(&queue->page_list);
14094 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
14095
14096 /* Set queue parameters now. If the system cannot provide memory
14097 * resources, the free routine needs to know what was allocated.
14098 */
14099 queue->entry_size = entry_size;
14100 queue->entry_count = entry_count;
14101 queue->page_size = hw_page_size;
14102 queue->phba = phba;
14103
4f774513
JS
14104 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14105 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14106 if (!dmabuf)
14107 goto out_fail;
1aee383d
JP
14108 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14109 hw_page_size, &dmabuf->phys,
14110 GFP_KERNEL);
4f774513
JS
14111 if (!dmabuf->virt) {
14112 kfree(dmabuf);
14113 goto out_fail;
14114 }
14115 dmabuf->buffer_tag = x;
14116 list_add_tail(&dmabuf->list, &queue->page_list);
14117 /* initialize queue's entry array */
14118 dma_pointer = dmabuf->virt;
14119 for (; total_qe_count < entry_count &&
cb5172ea 14120 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
14121 total_qe_count++, dma_pointer += entry_size) {
14122 queue->qe[total_qe_count].address = dma_pointer;
14123 }
14124 }
f485c18d
DK
14125 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14126 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 14127
64eb4dcb
JS
14128 /* entry_repost will be set during q creation */
14129
4f774513
JS
14130 return queue;
14131out_fail:
14132 lpfc_sli4_queue_free(queue);
14133 return NULL;
14134}
14135
962bc51b
JS
14136/**
14137 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14138 * @phba: HBA structure that indicates port to create a queue on.
14139 * @pci_barset: PCI BAR set flag.
14140 *
14141 * This function shall perform iomap of the specified PCI BAR address to host
14142 * memory address if not already done so and return it. The returned host
14143 * memory address can be NULL.
14144 */
14145static void __iomem *
14146lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14147{
962bc51b
JS
14148 if (!phba->pcidev)
14149 return NULL;
962bc51b
JS
14150
14151 switch (pci_barset) {
14152 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14153 return phba->pci_bar0_memmap_p;
14154 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14155 return phba->pci_bar2_memmap_p;
14156 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14157 return phba->pci_bar4_memmap_p;
14158 default:
14159 break;
14160 }
14161 return NULL;
14162}
14163
173edbb2 14164/**
895427bd 14165 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14166 * @phba: HBA structure that indicates port to create a queue on.
14167 * @startq: The starting FCP EQ to modify
14168 *
14169 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14170 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14171 * updated in one mailbox command.
173edbb2
JS
14172 *
14173 * The @phba struct is used to send mailbox command to HBA. The @startq
14174 * is used to get the starting FCP EQ to change.
14175 * This function is asynchronous and will wait for the mailbox
14176 * command to finish before continuing.
14177 *
14178 * On success this function will return a zero. If unable to allocate enough
14179 * memory this function will return -ENOMEM. If the queue create mailbox command
14180 * fails this function will return -ENXIO.
14181 **/
a2fc4aef 14182int
0cf07f84
JS
14183lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14184 uint32_t numq, uint32_t imax)
173edbb2
JS
14185{
14186 struct lpfc_mbx_modify_eq_delay *eq_delay;
14187 LPFC_MBOXQ_t *mbox;
14188 struct lpfc_queue *eq;
14189 int cnt, rc, length, status = 0;
14190 uint32_t shdr_status, shdr_add_status;
0cf07f84 14191 uint32_t result, val;
895427bd 14192 int qidx;
173edbb2
JS
14193 union lpfc_sli4_cfg_shdr *shdr;
14194 uint16_t dmult;
14195
895427bd 14196 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14197 return 0;
14198
14199 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14200 if (!mbox)
14201 return -ENOMEM;
14202 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14203 sizeof(struct lpfc_sli4_cfg_mhdr));
14204 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14205 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14206 length, LPFC_SLI4_MBX_EMBED);
14207 eq_delay = &mbox->u.mqe.un.eq_delay;
14208
14209 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14210 result = imax / phba->io_channel_irqs;
895427bd 14211 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14212 dmult = 0;
14213 else
14214 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14215 if (dmult > LPFC_DMULT_MAX)
14216 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14217
14218 cnt = 0;
895427bd
JS
14219 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14220 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14221 if (!eq)
14222 continue;
0cf07f84 14223 eq->q_mode = imax;
173edbb2
JS
14224 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14225 eq_delay->u.request.eq[cnt].phase = 0;
14226 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14227 cnt++;
0cf07f84
JS
14228
14229 /* q_mode is only used for auto_imax */
14230 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14231 /* Use EQ Delay Register method for q_mode */
14232
14233 /* Convert for EQ Delay register */
14234 val = phba->cfg_fcp_imax;
14235 if (val) {
14236 /* First, interrupts per sec per EQ */
14237 val = phba->cfg_fcp_imax /
14238 phba->io_channel_irqs;
14239
14240 /* us delay between each interrupt */
14241 val = LPFC_SEC_TO_USEC / val;
14242 }
14243 eq->q_mode = val;
14244 } else {
14245 eq->q_mode = imax;
14246 }
14247
14248 if (cnt >= numq)
173edbb2
JS
14249 break;
14250 }
14251 eq_delay->u.request.num_eq = cnt;
14252
14253 mbox->vport = phba->pport;
14254 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14255 mbox->context1 = NULL;
14256 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14257 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14258 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14259 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14260 if (shdr_status || shdr_add_status || rc) {
14261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14262 "2512 MODIFY_EQ_DELAY mailbox failed with "
14263 "status x%x add_status x%x, mbx status x%x\n",
14264 shdr_status, shdr_add_status, rc);
14265 status = -ENXIO;
14266 }
14267 mempool_free(mbox, phba->mbox_mem_pool);
14268 return status;
14269}
14270
4f774513
JS
14271/**
14272 * lpfc_eq_create - Create an Event Queue on the HBA
14273 * @phba: HBA structure that indicates port to create a queue on.
14274 * @eq: The queue structure to use to create the event queue.
14275 * @imax: The maximum interrupt per second limit.
14276 *
14277 * This function creates an event queue, as detailed in @eq, on a port,
14278 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14279 *
14280 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14281 * is used to get the entry count and entry size that are necessary to
14282 * determine the number of pages to allocate and use for this queue. This
14283 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14284 * event queue. This function is asynchronous and will wait for the mailbox
14285 * command to finish before continuing.
14286 *
14287 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14288 * memory this function will return -ENOMEM. If the queue create mailbox command
14289 * fails this function will return -ENXIO.
4f774513 14290 **/
a2fc4aef 14291int
ee02006b 14292lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14293{
14294 struct lpfc_mbx_eq_create *eq_create;
14295 LPFC_MBOXQ_t *mbox;
14296 int rc, length, status = 0;
14297 struct lpfc_dmabuf *dmabuf;
14298 uint32_t shdr_status, shdr_add_status;
14299 union lpfc_sli4_cfg_shdr *shdr;
14300 uint16_t dmult;
49198b37
JS
14301 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14302
2e90f4b5
JS
14303 /* sanity check on queue memory */
14304 if (!eq)
14305 return -ENODEV;
49198b37
JS
14306 if (!phba->sli4_hba.pc_sli4_params.supported)
14307 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14308
14309 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14310 if (!mbox)
14311 return -ENOMEM;
14312 length = (sizeof(struct lpfc_mbx_eq_create) -
14313 sizeof(struct lpfc_sli4_cfg_mhdr));
14314 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14315 LPFC_MBOX_OPCODE_EQ_CREATE,
14316 length, LPFC_SLI4_MBX_EMBED);
14317 eq_create = &mbox->u.mqe.un.eq_create;
14318 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14319 eq->page_count);
14320 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14321 LPFC_EQE_SIZE);
14322 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
14323 /* don't setup delay multiplier using EQ_CREATE */
14324 dmult = 0;
4f774513
JS
14325 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14326 dmult);
14327 switch (eq->entry_count) {
14328 default:
14329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14330 "0360 Unsupported EQ count. (%d)\n",
14331 eq->entry_count);
14332 if (eq->entry_count < 256)
14333 return -EINVAL;
14334 /* otherwise default to smallest count (drop through) */
14335 case 256:
14336 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14337 LPFC_EQ_CNT_256);
14338 break;
14339 case 512:
14340 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14341 LPFC_EQ_CNT_512);
14342 break;
14343 case 1024:
14344 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14345 LPFC_EQ_CNT_1024);
14346 break;
14347 case 2048:
14348 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14349 LPFC_EQ_CNT_2048);
14350 break;
14351 case 4096:
14352 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14353 LPFC_EQ_CNT_4096);
14354 break;
14355 }
14356 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14357 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14358 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14359 putPaddrLow(dmabuf->phys);
14360 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14361 putPaddrHigh(dmabuf->phys);
14362 }
14363 mbox->vport = phba->pport;
14364 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14365 mbox->context1 = NULL;
14366 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14367 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14368 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14369 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14370 if (shdr_status || shdr_add_status || rc) {
14371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14372 "2500 EQ_CREATE mailbox failed with "
14373 "status x%x add_status x%x, mbx status x%x\n",
14374 shdr_status, shdr_add_status, rc);
14375 status = -ENXIO;
14376 }
14377 eq->type = LPFC_EQ;
14378 eq->subtype = LPFC_NONE;
14379 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14380 if (eq->queue_id == 0xFFFF)
14381 status = -ENXIO;
14382 eq->host_index = 0;
14383 eq->hba_index = 0;
64eb4dcb 14384 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14385
8fa38513 14386 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14387 return status;
14388}
14389
14390/**
14391 * lpfc_cq_create - Create a Completion Queue on the HBA
14392 * @phba: HBA structure that indicates port to create a queue on.
14393 * @cq: The queue structure to use to create the completion queue.
14394 * @eq: The event queue to bind this completion queue to.
14395 *
14396 * This function creates a completion queue, as detailed in @wq, on a port,
14397 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14398 *
14399 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14400 * is used to get the entry count and entry size that are necessary to
14401 * determine the number of pages to allocate and use for this queue. The @eq
14402 * is used to indicate which event queue to bind this completion queue to. This
14403 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14404 * completion queue. This function is asynchronous and will wait for the mailbox
14405 * command to finish before continuing.
14406 *
14407 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14408 * memory this function will return -ENOMEM. If the queue create mailbox command
14409 * fails this function will return -ENXIO.
4f774513 14410 **/
a2fc4aef 14411int
4f774513
JS
14412lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14413 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14414{
14415 struct lpfc_mbx_cq_create *cq_create;
14416 struct lpfc_dmabuf *dmabuf;
14417 LPFC_MBOXQ_t *mbox;
14418 int rc, length, status = 0;
14419 uint32_t shdr_status, shdr_add_status;
14420 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14421 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14422
2e90f4b5
JS
14423 /* sanity check on queue memory */
14424 if (!cq || !eq)
14425 return -ENODEV;
49198b37 14426 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14427 hw_page_size = cq->page_size;
49198b37 14428
4f774513
JS
14429 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14430 if (!mbox)
14431 return -ENOMEM;
14432 length = (sizeof(struct lpfc_mbx_cq_create) -
14433 sizeof(struct lpfc_sli4_cfg_mhdr));
14434 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14435 LPFC_MBOX_OPCODE_CQ_CREATE,
14436 length, LPFC_SLI4_MBX_EMBED);
14437 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14438 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14439 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14440 cq->page_count);
14441 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14442 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14443 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14444 phba->sli4_hba.pc_sli4_params.cqv);
14445 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14446 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14447 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14448 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14449 eq->queue_id);
14450 } else {
14451 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14452 eq->queue_id);
14453 }
4f774513 14454 switch (cq->entry_count) {
81b96eda
JS
14455 case 2048:
14456 case 4096:
14457 if (phba->sli4_hba.pc_sli4_params.cqv ==
14458 LPFC_Q_CREATE_VERSION_2) {
14459 cq_create->u.request.context.lpfc_cq_context_count =
14460 cq->entry_count;
14461 bf_set(lpfc_cq_context_count,
14462 &cq_create->u.request.context,
14463 LPFC_CQ_CNT_WORD7);
14464 break;
14465 }
14466 /* Fall Thru */
4f774513
JS
14467 default:
14468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14469 "0361 Unsupported CQ count: "
64eb4dcb 14470 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14471 cq->entry_count, cq->entry_size,
64eb4dcb 14472 cq->page_count);
4f4c1863
JS
14473 if (cq->entry_count < 256) {
14474 status = -EINVAL;
14475 goto out;
14476 }
4f774513
JS
14477 /* otherwise default to smallest count (drop through) */
14478 case 256:
14479 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14480 LPFC_CQ_CNT_256);
14481 break;
14482 case 512:
14483 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14484 LPFC_CQ_CNT_512);
14485 break;
14486 case 1024:
14487 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14488 LPFC_CQ_CNT_1024);
14489 break;
14490 }
14491 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14492 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14493 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14494 putPaddrLow(dmabuf->phys);
14495 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14496 putPaddrHigh(dmabuf->phys);
14497 }
14498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14499
14500 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14503 if (shdr_status || shdr_add_status || rc) {
14504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14505 "2501 CQ_CREATE mailbox failed with "
14506 "status x%x add_status x%x, mbx status x%x\n",
14507 shdr_status, shdr_add_status, rc);
14508 status = -ENXIO;
14509 goto out;
14510 }
14511 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14512 if (cq->queue_id == 0xFFFF) {
14513 status = -ENXIO;
14514 goto out;
14515 }
14516 /* link the cq onto the parent eq child list */
14517 list_add_tail(&cq->list, &eq->child_list);
14518 /* Set up completion queue's type and subtype */
14519 cq->type = type;
14520 cq->subtype = subtype;
14521 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14522 cq->assoc_qid = eq->queue_id;
4f774513
JS
14523 cq->host_index = 0;
14524 cq->hba_index = 0;
64eb4dcb 14525 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14526
8fa38513
JS
14527out:
14528 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14529 return status;
14530}
14531
2d7dbc4c
JS
14532/**
14533 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14534 * @phba: HBA structure that indicates port to create a queue on.
14535 * @cqp: The queue structure array to use to create the completion queues.
14536 * @eqp: The event queue array to bind these completion queues to.
14537 *
14538 * This function creates a set of completion queue, s to support MRQ
14539 * as detailed in @cqp, on a port,
14540 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14541 *
14542 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14543 * is used to get the entry count and entry size that are necessary to
14544 * determine the number of pages to allocate and use for this queue. The @eq
14545 * is used to indicate which event queue to bind this completion queue to. This
14546 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14547 * completion queue. This function is asynchronous and will wait for the mailbox
14548 * command to finish before continuing.
14549 *
14550 * On success this function will return a zero. If unable to allocate enough
14551 * memory this function will return -ENOMEM. If the queue create mailbox command
14552 * fails this function will return -ENXIO.
14553 **/
14554int
14555lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14556 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14557{
14558 struct lpfc_queue *cq;
14559 struct lpfc_queue *eq;
14560 struct lpfc_mbx_cq_create_set *cq_set;
14561 struct lpfc_dmabuf *dmabuf;
14562 LPFC_MBOXQ_t *mbox;
14563 int rc, length, alloclen, status = 0;
14564 int cnt, idx, numcq, page_idx = 0;
14565 uint32_t shdr_status, shdr_add_status;
14566 union lpfc_sli4_cfg_shdr *shdr;
14567 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14568
14569 /* sanity check on queue memory */
14570 numcq = phba->cfg_nvmet_mrq;
14571 if (!cqp || !eqp || !numcq)
14572 return -ENODEV;
2d7dbc4c
JS
14573
14574 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14575 if (!mbox)
14576 return -ENOMEM;
14577
14578 length = sizeof(struct lpfc_mbx_cq_create_set);
14579 length += ((numcq * cqp[0]->page_count) *
14580 sizeof(struct dma_address));
14581 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14582 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14583 LPFC_SLI4_MBX_NEMBED);
14584 if (alloclen < length) {
14585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14586 "3098 Allocated DMA memory size (%d) is "
14587 "less than the requested DMA memory size "
14588 "(%d)\n", alloclen, length);
14589 status = -ENOMEM;
14590 goto out;
14591 }
14592 cq_set = mbox->sge_array->addr[0];
14593 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14594 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14595
14596 for (idx = 0; idx < numcq; idx++) {
14597 cq = cqp[idx];
14598 eq = eqp[idx];
14599 if (!cq || !eq) {
14600 status = -ENOMEM;
14601 goto out;
14602 }
81b96eda
JS
14603 if (!phba->sli4_hba.pc_sli4_params.supported)
14604 hw_page_size = cq->page_size;
2d7dbc4c
JS
14605
14606 switch (idx) {
14607 case 0:
14608 bf_set(lpfc_mbx_cq_create_set_page_size,
14609 &cq_set->u.request,
14610 (hw_page_size / SLI4_PAGE_SIZE));
14611 bf_set(lpfc_mbx_cq_create_set_num_pages,
14612 &cq_set->u.request, cq->page_count);
14613 bf_set(lpfc_mbx_cq_create_set_evt,
14614 &cq_set->u.request, 1);
14615 bf_set(lpfc_mbx_cq_create_set_valid,
14616 &cq_set->u.request, 1);
14617 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14618 &cq_set->u.request, 0);
14619 bf_set(lpfc_mbx_cq_create_set_num_cq,
14620 &cq_set->u.request, numcq);
14621 switch (cq->entry_count) {
81b96eda
JS
14622 case 2048:
14623 case 4096:
14624 if (phba->sli4_hba.pc_sli4_params.cqv ==
14625 LPFC_Q_CREATE_VERSION_2) {
14626 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14627 &cq_set->u.request,
14628 cq->entry_count);
14629 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14630 &cq_set->u.request,
14631 LPFC_CQ_CNT_WORD7);
14632 break;
14633 }
14634 /* Fall Thru */
2d7dbc4c
JS
14635 default:
14636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14637 "3118 Bad CQ count. (%d)\n",
14638 cq->entry_count);
14639 if (cq->entry_count < 256) {
14640 status = -EINVAL;
14641 goto out;
14642 }
14643 /* otherwise default to smallest (drop thru) */
14644 case 256:
14645 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14646 &cq_set->u.request, LPFC_CQ_CNT_256);
14647 break;
14648 case 512:
14649 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14650 &cq_set->u.request, LPFC_CQ_CNT_512);
14651 break;
14652 case 1024:
14653 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14654 &cq_set->u.request, LPFC_CQ_CNT_1024);
14655 break;
14656 }
14657 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14658 &cq_set->u.request, eq->queue_id);
14659 break;
14660 case 1:
14661 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14662 &cq_set->u.request, eq->queue_id);
14663 break;
14664 case 2:
14665 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14666 &cq_set->u.request, eq->queue_id);
14667 break;
14668 case 3:
14669 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14670 &cq_set->u.request, eq->queue_id);
14671 break;
14672 case 4:
14673 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14674 &cq_set->u.request, eq->queue_id);
14675 break;
14676 case 5:
14677 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14678 &cq_set->u.request, eq->queue_id);
14679 break;
14680 case 6:
14681 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14682 &cq_set->u.request, eq->queue_id);
14683 break;
14684 case 7:
14685 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14686 &cq_set->u.request, eq->queue_id);
14687 break;
14688 case 8:
14689 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14690 &cq_set->u.request, eq->queue_id);
14691 break;
14692 case 9:
14693 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14694 &cq_set->u.request, eq->queue_id);
14695 break;
14696 case 10:
14697 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14698 &cq_set->u.request, eq->queue_id);
14699 break;
14700 case 11:
14701 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14702 &cq_set->u.request, eq->queue_id);
14703 break;
14704 case 12:
14705 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14706 &cq_set->u.request, eq->queue_id);
14707 break;
14708 case 13:
14709 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14710 &cq_set->u.request, eq->queue_id);
14711 break;
14712 case 14:
14713 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14714 &cq_set->u.request, eq->queue_id);
14715 break;
14716 case 15:
14717 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14718 &cq_set->u.request, eq->queue_id);
14719 break;
14720 }
14721
14722 /* link the cq onto the parent eq child list */
14723 list_add_tail(&cq->list, &eq->child_list);
14724 /* Set up completion queue's type and subtype */
14725 cq->type = type;
14726 cq->subtype = subtype;
14727 cq->assoc_qid = eq->queue_id;
14728 cq->host_index = 0;
14729 cq->hba_index = 0;
64eb4dcb 14730 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14731 cq->chann = idx;
2d7dbc4c
JS
14732
14733 rc = 0;
14734 list_for_each_entry(dmabuf, &cq->page_list, list) {
14735 memset(dmabuf->virt, 0, hw_page_size);
14736 cnt = page_idx + dmabuf->buffer_tag;
14737 cq_set->u.request.page[cnt].addr_lo =
14738 putPaddrLow(dmabuf->phys);
14739 cq_set->u.request.page[cnt].addr_hi =
14740 putPaddrHigh(dmabuf->phys);
14741 rc++;
14742 }
14743 page_idx += rc;
14744 }
14745
14746 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14747
14748 /* The IOCTL status is embedded in the mailbox subheader. */
14749 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14750 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14751 if (shdr_status || shdr_add_status || rc) {
14752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14753 "3119 CQ_CREATE_SET mailbox failed with "
14754 "status x%x add_status x%x, mbx status x%x\n",
14755 shdr_status, shdr_add_status, rc);
14756 status = -ENXIO;
14757 goto out;
14758 }
14759 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14760 if (rc == 0xFFFF) {
14761 status = -ENXIO;
14762 goto out;
14763 }
14764
14765 for (idx = 0; idx < numcq; idx++) {
14766 cq = cqp[idx];
14767 cq->queue_id = rc + idx;
14768 }
14769
14770out:
14771 lpfc_sli4_mbox_cmd_free(phba, mbox);
14772 return status;
14773}
14774
b19a061a
JS
14775/**
14776 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14777 * @phba: HBA structure that indicates port to create a queue on.
14778 * @mq: The queue structure to use to create the mailbox queue.
14779 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14780 * @cq: The completion queue to associate with this cq.
14781 *
14782 * This function provides failback (fb) functionality when the
14783 * mq_create_ext fails on older FW generations. It's purpose is identical
14784 * to mq_create_ext otherwise.
14785 *
14786 * This routine cannot fail as all attributes were previously accessed and
14787 * initialized in mq_create_ext.
14788 **/
14789static void
14790lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14791 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14792{
14793 struct lpfc_mbx_mq_create *mq_create;
14794 struct lpfc_dmabuf *dmabuf;
14795 int length;
14796
14797 length = (sizeof(struct lpfc_mbx_mq_create) -
14798 sizeof(struct lpfc_sli4_cfg_mhdr));
14799 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14800 LPFC_MBOX_OPCODE_MQ_CREATE,
14801 length, LPFC_SLI4_MBX_EMBED);
14802 mq_create = &mbox->u.mqe.un.mq_create;
14803 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14804 mq->page_count);
14805 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14806 cq->queue_id);
14807 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14808 switch (mq->entry_count) {
14809 case 16:
5a6f133e
JS
14810 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14811 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14812 break;
14813 case 32:
5a6f133e
JS
14814 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14815 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14816 break;
14817 case 64:
5a6f133e
JS
14818 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14819 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14820 break;
14821 case 128:
5a6f133e
JS
14822 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14823 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14824 break;
14825 }
14826 list_for_each_entry(dmabuf, &mq->page_list, list) {
14827 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14828 putPaddrLow(dmabuf->phys);
14829 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14830 putPaddrHigh(dmabuf->phys);
14831 }
14832}
14833
04c68496
JS
14834/**
14835 * lpfc_mq_create - Create a mailbox Queue on the HBA
14836 * @phba: HBA structure that indicates port to create a queue on.
14837 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14838 * @cq: The completion queue to associate with this cq.
14839 * @subtype: The queue's subtype.
04c68496
JS
14840 *
14841 * This function creates a mailbox queue, as detailed in @mq, on a port,
14842 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14843 *
14844 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14845 * is used to get the entry count and entry size that are necessary to
14846 * determine the number of pages to allocate and use for this queue. This
14847 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14848 * mailbox queue. This function is asynchronous and will wait for the mailbox
14849 * command to finish before continuing.
14850 *
14851 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14852 * memory this function will return -ENOMEM. If the queue create mailbox command
14853 * fails this function will return -ENXIO.
04c68496 14854 **/
b19a061a 14855int32_t
04c68496
JS
14856lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14857 struct lpfc_queue *cq, uint32_t subtype)
14858{
14859 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14860 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14861 struct lpfc_dmabuf *dmabuf;
14862 LPFC_MBOXQ_t *mbox;
14863 int rc, length, status = 0;
14864 uint32_t shdr_status, shdr_add_status;
14865 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14866 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14867
2e90f4b5
JS
14868 /* sanity check on queue memory */
14869 if (!mq || !cq)
14870 return -ENODEV;
49198b37
JS
14871 if (!phba->sli4_hba.pc_sli4_params.supported)
14872 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14873
04c68496
JS
14874 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14875 if (!mbox)
14876 return -ENOMEM;
b19a061a 14877 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14878 sizeof(struct lpfc_sli4_cfg_mhdr));
14879 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14880 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14881 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14882
14883 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14884 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14885 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14886 &mq_create_ext->u.request, mq->page_count);
14887 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14888 &mq_create_ext->u.request, 1);
14889 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14890 &mq_create_ext->u.request, 1);
14891 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14892 &mq_create_ext->u.request, 1);
70f3c073
JS
14893 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14894 &mq_create_ext->u.request, 1);
14895 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14896 &mq_create_ext->u.request, 1);
b19a061a 14897 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14898 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14899 phba->sli4_hba.pc_sli4_params.mqv);
14900 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14901 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14902 cq->queue_id);
14903 else
14904 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14905 cq->queue_id);
04c68496
JS
14906 switch (mq->entry_count) {
14907 default:
14908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14909 "0362 Unsupported MQ count. (%d)\n",
14910 mq->entry_count);
4f4c1863
JS
14911 if (mq->entry_count < 16) {
14912 status = -EINVAL;
14913 goto out;
14914 }
04c68496
JS
14915 /* otherwise default to smallest count (drop through) */
14916 case 16:
5a6f133e
JS
14917 bf_set(lpfc_mq_context_ring_size,
14918 &mq_create_ext->u.request.context,
14919 LPFC_MQ_RING_SIZE_16);
04c68496
JS
14920 break;
14921 case 32:
5a6f133e
JS
14922 bf_set(lpfc_mq_context_ring_size,
14923 &mq_create_ext->u.request.context,
14924 LPFC_MQ_RING_SIZE_32);
04c68496
JS
14925 break;
14926 case 64:
5a6f133e
JS
14927 bf_set(lpfc_mq_context_ring_size,
14928 &mq_create_ext->u.request.context,
14929 LPFC_MQ_RING_SIZE_64);
04c68496
JS
14930 break;
14931 case 128:
5a6f133e
JS
14932 bf_set(lpfc_mq_context_ring_size,
14933 &mq_create_ext->u.request.context,
14934 LPFC_MQ_RING_SIZE_128);
04c68496
JS
14935 break;
14936 }
14937 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 14938 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 14939 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 14940 putPaddrLow(dmabuf->phys);
b19a061a 14941 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
14942 putPaddrHigh(dmabuf->phys);
14943 }
14944 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
14945 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14946 &mq_create_ext->u.response);
14947 if (rc != MBX_SUCCESS) {
14948 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14949 "2795 MQ_CREATE_EXT failed with "
14950 "status x%x. Failback to MQ_CREATE.\n",
14951 rc);
14952 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14953 mq_create = &mbox->u.mqe.un.mq_create;
14954 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14955 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14956 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14957 &mq_create->u.response);
14958 }
14959
04c68496 14960 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
14961 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14962 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14963 if (shdr_status || shdr_add_status || rc) {
14964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14965 "2502 MQ_CREATE mailbox failed with "
14966 "status x%x add_status x%x, mbx status x%x\n",
14967 shdr_status, shdr_add_status, rc);
14968 status = -ENXIO;
14969 goto out;
14970 }
04c68496
JS
14971 if (mq->queue_id == 0xFFFF) {
14972 status = -ENXIO;
14973 goto out;
14974 }
14975 mq->type = LPFC_MQ;
2a622bfb 14976 mq->assoc_qid = cq->queue_id;
04c68496
JS
14977 mq->subtype = subtype;
14978 mq->host_index = 0;
14979 mq->hba_index = 0;
64eb4dcb 14980 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
14981
14982 /* link the mq onto the parent cq child list */
14983 list_add_tail(&mq->list, &cq->child_list);
14984out:
8fa38513 14985 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
14986 return status;
14987}
14988
4f774513
JS
14989/**
14990 * lpfc_wq_create - Create a Work Queue on the HBA
14991 * @phba: HBA structure that indicates port to create a queue on.
14992 * @wq: The queue structure to use to create the work queue.
14993 * @cq: The completion queue to bind this work queue to.
14994 * @subtype: The subtype of the work queue indicating its functionality.
14995 *
14996 * This function creates a work queue, as detailed in @wq, on a port, described
14997 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14998 *
14999 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15000 * is used to get the entry count and entry size that are necessary to
15001 * determine the number of pages to allocate and use for this queue. The @cq
15002 * is used to indicate which completion queue to bind this work queue to. This
15003 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15004 * work queue. This function is asynchronous and will wait for the mailbox
15005 * command to finish before continuing.
15006 *
15007 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15008 * memory this function will return -ENOMEM. If the queue create mailbox command
15009 * fails this function will return -ENXIO.
4f774513 15010 **/
a2fc4aef 15011int
4f774513
JS
15012lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15013 struct lpfc_queue *cq, uint32_t subtype)
15014{
15015 struct lpfc_mbx_wq_create *wq_create;
15016 struct lpfc_dmabuf *dmabuf;
15017 LPFC_MBOXQ_t *mbox;
15018 int rc, length, status = 0;
15019 uint32_t shdr_status, shdr_add_status;
15020 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15021 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 15022 struct dma_address *page;
962bc51b
JS
15023 void __iomem *bar_memmap_p;
15024 uint32_t db_offset;
15025 uint16_t pci_barset;
81b96eda 15026 uint8_t wq_create_version;
49198b37 15027
2e90f4b5
JS
15028 /* sanity check on queue memory */
15029 if (!wq || !cq)
15030 return -ENODEV;
49198b37 15031 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 15032 hw_page_size = wq->page_size;
4f774513
JS
15033
15034 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15035 if (!mbox)
15036 return -ENOMEM;
15037 length = (sizeof(struct lpfc_mbx_wq_create) -
15038 sizeof(struct lpfc_sli4_cfg_mhdr));
15039 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15040 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15041 length, LPFC_SLI4_MBX_EMBED);
15042 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 15043 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
15044 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15045 wq->page_count);
15046 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15047 cq->queue_id);
0c651878
JS
15048
15049 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
15050 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15051 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 15052
c176ffa0
JS
15053 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15054 (wq->page_size > SLI4_PAGE_SIZE))
81b96eda
JS
15055 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15056 else
15057 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15058
15059 switch (wq_create_version) {
0c651878
JS
15060 case LPFC_Q_CREATE_VERSION_0:
15061 switch (wq->entry_size) {
15062 default:
15063 case 64:
15064 /* Nothing to do, version 0 ONLY supports 64 byte */
15065 page = wq_create->u.request.page;
15066 break;
15067 case 128:
15068 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
15069 LPFC_WQ_SZ128_SUPPORT)) {
15070 status = -ERANGE;
15071 goto out;
15072 }
15073 /* If we get here the HBA MUST also support V1 and
15074 * we MUST use it
15075 */
15076 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15077 LPFC_Q_CREATE_VERSION_1);
15078
15079 bf_set(lpfc_mbx_wq_create_wqe_count,
15080 &wq_create->u.request_1, wq->entry_count);
15081 bf_set(lpfc_mbx_wq_create_wqe_size,
15082 &wq_create->u.request_1,
15083 LPFC_WQ_WQE_SIZE_128);
15084 bf_set(lpfc_mbx_wq_create_page_size,
15085 &wq_create->u.request_1,
8ea73db4 15086 LPFC_WQ_PAGE_SIZE_4096);
0c651878
JS
15087 page = wq_create->u.request_1.page;
15088 break;
15089 }
15090 break;
15091 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
15092 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15093 wq->entry_count);
3f247de7
JS
15094 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15095 LPFC_Q_CREATE_VERSION_1);
15096
5a6f133e
JS
15097 switch (wq->entry_size) {
15098 default:
15099 case 64:
15100 bf_set(lpfc_mbx_wq_create_wqe_size,
15101 &wq_create->u.request_1,
15102 LPFC_WQ_WQE_SIZE_64);
15103 break;
15104 case 128:
0c651878
JS
15105 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
15106 LPFC_WQ_SZ128_SUPPORT)) {
15107 status = -ERANGE;
15108 goto out;
15109 }
5a6f133e
JS
15110 bf_set(lpfc_mbx_wq_create_wqe_size,
15111 &wq_create->u.request_1,
15112 LPFC_WQ_WQE_SIZE_128);
15113 break;
15114 }
8ea73db4
JS
15115 bf_set(lpfc_mbx_wq_create_page_size,
15116 &wq_create->u.request_1,
81b96eda 15117 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 15118 page = wq_create->u.request_1.page;
0c651878
JS
15119 break;
15120 default:
15121 status = -ERANGE;
15122 goto out;
5a6f133e 15123 }
0c651878 15124
4f774513 15125 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 15126 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
15127 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15128 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 15129 }
962bc51b
JS
15130
15131 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15132 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15133
4f774513
JS
15134 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15135 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15136 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15138 if (shdr_status || shdr_add_status || rc) {
15139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15140 "2503 WQ_CREATE mailbox failed with "
15141 "status x%x add_status x%x, mbx status x%x\n",
15142 shdr_status, shdr_add_status, rc);
15143 status = -ENXIO;
15144 goto out;
15145 }
15146 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15147 if (wq->queue_id == 0xFFFF) {
15148 status = -ENXIO;
15149 goto out;
15150 }
962bc51b
JS
15151 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15152 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15153 &wq_create->u.response);
15154 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15155 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15157 "3265 WQ[%d] doorbell format not "
15158 "supported: x%x\n", wq->queue_id,
15159 wq->db_format);
15160 status = -EINVAL;
15161 goto out;
15162 }
15163 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15164 &wq_create->u.response);
15165 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15166 if (!bar_memmap_p) {
15167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15168 "3263 WQ[%d] failed to memmap pci "
15169 "barset:x%x\n", wq->queue_id,
15170 pci_barset);
15171 status = -ENOMEM;
15172 goto out;
15173 }
15174 db_offset = wq_create->u.response.doorbell_offset;
15175 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15176 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15178 "3252 WQ[%d] doorbell offset not "
15179 "supported: x%x\n", wq->queue_id,
15180 db_offset);
15181 status = -EINVAL;
15182 goto out;
15183 }
15184 wq->db_regaddr = bar_memmap_p + db_offset;
15185 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15186 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15187 "format:x%x\n", wq->queue_id, pci_barset,
15188 db_offset, wq->db_format);
962bc51b
JS
15189 } else {
15190 wq->db_format = LPFC_DB_LIST_FORMAT;
15191 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15192 }
895427bd
JS
15193 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15194 if (wq->pring == NULL) {
15195 status = -ENOMEM;
15196 goto out;
15197 }
4f774513 15198 wq->type = LPFC_WQ;
2a622bfb 15199 wq->assoc_qid = cq->queue_id;
4f774513
JS
15200 wq->subtype = subtype;
15201 wq->host_index = 0;
15202 wq->hba_index = 0;
ff78d8f9 15203 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15204
15205 /* link the wq onto the parent cq child list */
15206 list_add_tail(&wq->list, &cq->child_list);
15207out:
8fa38513 15208 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15209 return status;
15210}
15211
15212/**
15213 * lpfc_rq_create - Create a Receive Queue on the HBA
15214 * @phba: HBA structure that indicates port to create a queue on.
15215 * @hrq: The queue structure to use to create the header receive queue.
15216 * @drq: The queue structure to use to create the data receive queue.
15217 * @cq: The completion queue to bind this work queue to.
15218 *
15219 * This function creates a receive buffer queue pair , as detailed in @hrq and
15220 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15221 * to the HBA.
15222 *
15223 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15224 * struct is used to get the entry count that is necessary to determine the
15225 * number of pages to use for this queue. The @cq is used to indicate which
15226 * completion queue to bind received buffers that are posted to these queues to.
15227 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15228 * receive queue pair. This function is asynchronous and will wait for the
15229 * mailbox command to finish before continuing.
15230 *
15231 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15232 * memory this function will return -ENOMEM. If the queue create mailbox command
15233 * fails this function will return -ENXIO.
4f774513 15234 **/
a2fc4aef 15235int
4f774513
JS
15236lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15237 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15238{
15239 struct lpfc_mbx_rq_create *rq_create;
15240 struct lpfc_dmabuf *dmabuf;
15241 LPFC_MBOXQ_t *mbox;
15242 int rc, length, status = 0;
15243 uint32_t shdr_status, shdr_add_status;
15244 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15245 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15246 void __iomem *bar_memmap_p;
15247 uint32_t db_offset;
15248 uint16_t pci_barset;
49198b37 15249
2e90f4b5
JS
15250 /* sanity check on queue memory */
15251 if (!hrq || !drq || !cq)
15252 return -ENODEV;
49198b37
JS
15253 if (!phba->sli4_hba.pc_sli4_params.supported)
15254 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15255
15256 if (hrq->entry_count != drq->entry_count)
15257 return -EINVAL;
15258 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15259 if (!mbox)
15260 return -ENOMEM;
15261 length = (sizeof(struct lpfc_mbx_rq_create) -
15262 sizeof(struct lpfc_sli4_cfg_mhdr));
15263 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15264 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15265 length, LPFC_SLI4_MBX_EMBED);
15266 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15267 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15268 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15269 phba->sli4_hba.pc_sli4_params.rqv);
15270 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15271 bf_set(lpfc_rq_context_rqe_count_1,
15272 &rq_create->u.request.context,
15273 hrq->entry_count);
15274 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15275 bf_set(lpfc_rq_context_rqe_size,
15276 &rq_create->u.request.context,
15277 LPFC_RQE_SIZE_8);
15278 bf_set(lpfc_rq_context_page_size,
15279 &rq_create->u.request.context,
8ea73db4 15280 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15281 } else {
15282 switch (hrq->entry_count) {
15283 default:
15284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15285 "2535 Unsupported RQ count. (%d)\n",
15286 hrq->entry_count);
4f4c1863
JS
15287 if (hrq->entry_count < 512) {
15288 status = -EINVAL;
15289 goto out;
15290 }
5a6f133e
JS
15291 /* otherwise default to smallest count (drop through) */
15292 case 512:
15293 bf_set(lpfc_rq_context_rqe_count,
15294 &rq_create->u.request.context,
15295 LPFC_RQ_RING_SIZE_512);
15296 break;
15297 case 1024:
15298 bf_set(lpfc_rq_context_rqe_count,
15299 &rq_create->u.request.context,
15300 LPFC_RQ_RING_SIZE_1024);
15301 break;
15302 case 2048:
15303 bf_set(lpfc_rq_context_rqe_count,
15304 &rq_create->u.request.context,
15305 LPFC_RQ_RING_SIZE_2048);
15306 break;
15307 case 4096:
15308 bf_set(lpfc_rq_context_rqe_count,
15309 &rq_create->u.request.context,
15310 LPFC_RQ_RING_SIZE_4096);
15311 break;
15312 }
15313 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15314 LPFC_HDR_BUF_SIZE);
4f774513
JS
15315 }
15316 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15317 cq->queue_id);
15318 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15319 hrq->page_count);
4f774513 15320 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15321 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15322 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15323 putPaddrLow(dmabuf->phys);
15324 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15325 putPaddrHigh(dmabuf->phys);
15326 }
962bc51b
JS
15327 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15328 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15329
4f774513
JS
15330 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15331 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15332 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15333 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15334 if (shdr_status || shdr_add_status || rc) {
15335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15336 "2504 RQ_CREATE mailbox failed with "
15337 "status x%x add_status x%x, mbx status x%x\n",
15338 shdr_status, shdr_add_status, rc);
15339 status = -ENXIO;
15340 goto out;
15341 }
15342 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15343 if (hrq->queue_id == 0xFFFF) {
15344 status = -ENXIO;
15345 goto out;
15346 }
962bc51b
JS
15347
15348 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15349 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15350 &rq_create->u.response);
15351 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15352 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15354 "3262 RQ [%d] doorbell format not "
15355 "supported: x%x\n", hrq->queue_id,
15356 hrq->db_format);
15357 status = -EINVAL;
15358 goto out;
15359 }
15360
15361 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15362 &rq_create->u.response);
15363 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15364 if (!bar_memmap_p) {
15365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15366 "3269 RQ[%d] failed to memmap pci "
15367 "barset:x%x\n", hrq->queue_id,
15368 pci_barset);
15369 status = -ENOMEM;
15370 goto out;
15371 }
15372
15373 db_offset = rq_create->u.response.doorbell_offset;
15374 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15375 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15377 "3270 RQ[%d] doorbell offset not "
15378 "supported: x%x\n", hrq->queue_id,
15379 db_offset);
15380 status = -EINVAL;
15381 goto out;
15382 }
15383 hrq->db_regaddr = bar_memmap_p + db_offset;
15384 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15385 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15386 "format:x%x\n", hrq->queue_id, pci_barset,
15387 db_offset, hrq->db_format);
962bc51b
JS
15388 } else {
15389 hrq->db_format = LPFC_DB_RING_FORMAT;
15390 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15391 }
4f774513 15392 hrq->type = LPFC_HRQ;
2a622bfb 15393 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15394 hrq->subtype = subtype;
15395 hrq->host_index = 0;
15396 hrq->hba_index = 0;
61f3d4bf 15397 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15398
15399 /* now create the data queue */
15400 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15401 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15402 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15403 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15404 phba->sli4_hba.pc_sli4_params.rqv);
15405 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15406 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15407 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15408 if (subtype == LPFC_NVMET)
15409 rq_create->u.request.context.buffer_size =
15410 LPFC_NVMET_DATA_BUF_SIZE;
15411 else
15412 rq_create->u.request.context.buffer_size =
15413 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15414 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15415 LPFC_RQE_SIZE_8);
15416 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15417 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15418 } else {
15419 switch (drq->entry_count) {
15420 default:
15421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15422 "2536 Unsupported RQ count. (%d)\n",
15423 drq->entry_count);
4f4c1863
JS
15424 if (drq->entry_count < 512) {
15425 status = -EINVAL;
15426 goto out;
15427 }
5a6f133e
JS
15428 /* otherwise default to smallest count (drop through) */
15429 case 512:
15430 bf_set(lpfc_rq_context_rqe_count,
15431 &rq_create->u.request.context,
15432 LPFC_RQ_RING_SIZE_512);
15433 break;
15434 case 1024:
15435 bf_set(lpfc_rq_context_rqe_count,
15436 &rq_create->u.request.context,
15437 LPFC_RQ_RING_SIZE_1024);
15438 break;
15439 case 2048:
15440 bf_set(lpfc_rq_context_rqe_count,
15441 &rq_create->u.request.context,
15442 LPFC_RQ_RING_SIZE_2048);
15443 break;
15444 case 4096:
15445 bf_set(lpfc_rq_context_rqe_count,
15446 &rq_create->u.request.context,
15447 LPFC_RQ_RING_SIZE_4096);
15448 break;
15449 }
3c603be9
JS
15450 if (subtype == LPFC_NVMET)
15451 bf_set(lpfc_rq_context_buf_size,
15452 &rq_create->u.request.context,
15453 LPFC_NVMET_DATA_BUF_SIZE);
15454 else
15455 bf_set(lpfc_rq_context_buf_size,
15456 &rq_create->u.request.context,
15457 LPFC_DATA_BUF_SIZE);
4f774513
JS
15458 }
15459 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15460 cq->queue_id);
15461 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15462 drq->page_count);
4f774513
JS
15463 list_for_each_entry(dmabuf, &drq->page_list, list) {
15464 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15465 putPaddrLow(dmabuf->phys);
15466 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15467 putPaddrHigh(dmabuf->phys);
15468 }
962bc51b
JS
15469 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15470 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15471 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15472 /* The IOCTL status is embedded in the mailbox subheader. */
15473 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15474 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15475 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15476 if (shdr_status || shdr_add_status || rc) {
15477 status = -ENXIO;
15478 goto out;
15479 }
15480 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15481 if (drq->queue_id == 0xFFFF) {
15482 status = -ENXIO;
15483 goto out;
15484 }
15485 drq->type = LPFC_DRQ;
2a622bfb 15486 drq->assoc_qid = cq->queue_id;
4f774513
JS
15487 drq->subtype = subtype;
15488 drq->host_index = 0;
15489 drq->hba_index = 0;
61f3d4bf 15490 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15491
15492 /* link the header and data RQs onto the parent cq child list */
15493 list_add_tail(&hrq->list, &cq->child_list);
15494 list_add_tail(&drq->list, &cq->child_list);
15495
15496out:
8fa38513 15497 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15498 return status;
15499}
15500
2d7dbc4c
JS
15501/**
15502 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15503 * @phba: HBA structure that indicates port to create a queue on.
15504 * @hrqp: The queue structure array to use to create the header receive queues.
15505 * @drqp: The queue structure array to use to create the data receive queues.
15506 * @cqp: The completion queue array to bind these receive queues to.
15507 *
15508 * This function creates a receive buffer queue pair , as detailed in @hrq and
15509 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15510 * to the HBA.
15511 *
15512 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15513 * struct is used to get the entry count that is necessary to determine the
15514 * number of pages to use for this queue. The @cq is used to indicate which
15515 * completion queue to bind received buffers that are posted to these queues to.
15516 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15517 * receive queue pair. This function is asynchronous and will wait for the
15518 * mailbox command to finish before continuing.
15519 *
15520 * On success this function will return a zero. If unable to allocate enough
15521 * memory this function will return -ENOMEM. If the queue create mailbox command
15522 * fails this function will return -ENXIO.
15523 **/
15524int
15525lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15526 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15527 uint32_t subtype)
15528{
15529 struct lpfc_queue *hrq, *drq, *cq;
15530 struct lpfc_mbx_rq_create_v2 *rq_create;
15531 struct lpfc_dmabuf *dmabuf;
15532 LPFC_MBOXQ_t *mbox;
15533 int rc, length, alloclen, status = 0;
15534 int cnt, idx, numrq, page_idx = 0;
15535 uint32_t shdr_status, shdr_add_status;
15536 union lpfc_sli4_cfg_shdr *shdr;
15537 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15538
15539 numrq = phba->cfg_nvmet_mrq;
15540 /* sanity check on array memory */
15541 if (!hrqp || !drqp || !cqp || !numrq)
15542 return -ENODEV;
15543 if (!phba->sli4_hba.pc_sli4_params.supported)
15544 hw_page_size = SLI4_PAGE_SIZE;
15545
15546 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15547 if (!mbox)
15548 return -ENOMEM;
15549
15550 length = sizeof(struct lpfc_mbx_rq_create_v2);
15551 length += ((2 * numrq * hrqp[0]->page_count) *
15552 sizeof(struct dma_address));
15553
15554 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15555 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15556 LPFC_SLI4_MBX_NEMBED);
15557 if (alloclen < length) {
15558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15559 "3099 Allocated DMA memory size (%d) is "
15560 "less than the requested DMA memory size "
15561 "(%d)\n", alloclen, length);
15562 status = -ENOMEM;
15563 goto out;
15564 }
15565
15566
15567
15568 rq_create = mbox->sge_array->addr[0];
15569 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15570
15571 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15572 cnt = 0;
15573
15574 for (idx = 0; idx < numrq; idx++) {
15575 hrq = hrqp[idx];
15576 drq = drqp[idx];
15577 cq = cqp[idx];
15578
2d7dbc4c
JS
15579 /* sanity check on queue memory */
15580 if (!hrq || !drq || !cq) {
15581 status = -ENODEV;
15582 goto out;
15583 }
15584
7aabe84b
JS
15585 if (hrq->entry_count != drq->entry_count) {
15586 status = -EINVAL;
15587 goto out;
15588 }
15589
2d7dbc4c
JS
15590 if (idx == 0) {
15591 bf_set(lpfc_mbx_rq_create_num_pages,
15592 &rq_create->u.request,
15593 hrq->page_count);
15594 bf_set(lpfc_mbx_rq_create_rq_cnt,
15595 &rq_create->u.request, (numrq * 2));
15596 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15597 1);
15598 bf_set(lpfc_rq_context_base_cq,
15599 &rq_create->u.request.context,
15600 cq->queue_id);
15601 bf_set(lpfc_rq_context_data_size,
15602 &rq_create->u.request.context,
3c603be9 15603 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15604 bf_set(lpfc_rq_context_hdr_size,
15605 &rq_create->u.request.context,
15606 LPFC_HDR_BUF_SIZE);
15607 bf_set(lpfc_rq_context_rqe_count_1,
15608 &rq_create->u.request.context,
15609 hrq->entry_count);
15610 bf_set(lpfc_rq_context_rqe_size,
15611 &rq_create->u.request.context,
15612 LPFC_RQE_SIZE_8);
15613 bf_set(lpfc_rq_context_page_size,
15614 &rq_create->u.request.context,
15615 (PAGE_SIZE/SLI4_PAGE_SIZE));
15616 }
15617 rc = 0;
15618 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15619 memset(dmabuf->virt, 0, hw_page_size);
15620 cnt = page_idx + dmabuf->buffer_tag;
15621 rq_create->u.request.page[cnt].addr_lo =
15622 putPaddrLow(dmabuf->phys);
15623 rq_create->u.request.page[cnt].addr_hi =
15624 putPaddrHigh(dmabuf->phys);
15625 rc++;
15626 }
15627 page_idx += rc;
15628
15629 rc = 0;
15630 list_for_each_entry(dmabuf, &drq->page_list, list) {
15631 memset(dmabuf->virt, 0, hw_page_size);
15632 cnt = page_idx + dmabuf->buffer_tag;
15633 rq_create->u.request.page[cnt].addr_lo =
15634 putPaddrLow(dmabuf->phys);
15635 rq_create->u.request.page[cnt].addr_hi =
15636 putPaddrHigh(dmabuf->phys);
15637 rc++;
15638 }
15639 page_idx += rc;
15640
15641 hrq->db_format = LPFC_DB_RING_FORMAT;
15642 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15643 hrq->type = LPFC_HRQ;
15644 hrq->assoc_qid = cq->queue_id;
15645 hrq->subtype = subtype;
15646 hrq->host_index = 0;
15647 hrq->hba_index = 0;
61f3d4bf 15648 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15649
15650 drq->db_format = LPFC_DB_RING_FORMAT;
15651 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15652 drq->type = LPFC_DRQ;
15653 drq->assoc_qid = cq->queue_id;
15654 drq->subtype = subtype;
15655 drq->host_index = 0;
15656 drq->hba_index = 0;
61f3d4bf 15657 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15658
15659 list_add_tail(&hrq->list, &cq->child_list);
15660 list_add_tail(&drq->list, &cq->child_list);
15661 }
15662
15663 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15664 /* The IOCTL status is embedded in the mailbox subheader. */
15665 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15666 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15667 if (shdr_status || shdr_add_status || rc) {
15668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15669 "3120 RQ_CREATE mailbox failed with "
15670 "status x%x add_status x%x, mbx status x%x\n",
15671 shdr_status, shdr_add_status, rc);
15672 status = -ENXIO;
15673 goto out;
15674 }
15675 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15676 if (rc == 0xFFFF) {
15677 status = -ENXIO;
15678 goto out;
15679 }
15680
15681 /* Initialize all RQs with associated queue id */
15682 for (idx = 0; idx < numrq; idx++) {
15683 hrq = hrqp[idx];
15684 hrq->queue_id = rc + (2 * idx);
15685 drq = drqp[idx];
15686 drq->queue_id = rc + (2 * idx) + 1;
15687 }
15688
15689out:
15690 lpfc_sli4_mbox_cmd_free(phba, mbox);
15691 return status;
15692}
15693
4f774513
JS
15694/**
15695 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15696 * @eq: The queue structure associated with the queue to destroy.
15697 *
15698 * This function destroys a queue, as detailed in @eq by sending an mailbox
15699 * command, specific to the type of queue, to the HBA.
15700 *
15701 * The @eq struct is used to get the queue ID of the queue to destroy.
15702 *
15703 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15704 * command fails this function will return -ENXIO.
4f774513 15705 **/
a2fc4aef 15706int
4f774513
JS
15707lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15708{
15709 LPFC_MBOXQ_t *mbox;
15710 int rc, length, status = 0;
15711 uint32_t shdr_status, shdr_add_status;
15712 union lpfc_sli4_cfg_shdr *shdr;
15713
2e90f4b5 15714 /* sanity check on queue memory */
4f774513
JS
15715 if (!eq)
15716 return -ENODEV;
15717 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15718 if (!mbox)
15719 return -ENOMEM;
15720 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15721 sizeof(struct lpfc_sli4_cfg_mhdr));
15722 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15723 LPFC_MBOX_OPCODE_EQ_DESTROY,
15724 length, LPFC_SLI4_MBX_EMBED);
15725 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15726 eq->queue_id);
15727 mbox->vport = eq->phba->pport;
15728 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15729
15730 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15731 /* The IOCTL status is embedded in the mailbox subheader. */
15732 shdr = (union lpfc_sli4_cfg_shdr *)
15733 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15734 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15735 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15736 if (shdr_status || shdr_add_status || rc) {
15737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15738 "2505 EQ_DESTROY mailbox failed with "
15739 "status x%x add_status x%x, mbx status x%x\n",
15740 shdr_status, shdr_add_status, rc);
15741 status = -ENXIO;
15742 }
15743
15744 /* Remove eq from any list */
15745 list_del_init(&eq->list);
8fa38513 15746 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15747 return status;
15748}
15749
15750/**
15751 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15752 * @cq: The queue structure associated with the queue to destroy.
15753 *
15754 * This function destroys a queue, as detailed in @cq by sending an mailbox
15755 * command, specific to the type of queue, to the HBA.
15756 *
15757 * The @cq struct is used to get the queue ID of the queue to destroy.
15758 *
15759 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15760 * command fails this function will return -ENXIO.
4f774513 15761 **/
a2fc4aef 15762int
4f774513
JS
15763lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15764{
15765 LPFC_MBOXQ_t *mbox;
15766 int rc, length, status = 0;
15767 uint32_t shdr_status, shdr_add_status;
15768 union lpfc_sli4_cfg_shdr *shdr;
15769
2e90f4b5 15770 /* sanity check on queue memory */
4f774513
JS
15771 if (!cq)
15772 return -ENODEV;
15773 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15774 if (!mbox)
15775 return -ENOMEM;
15776 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15777 sizeof(struct lpfc_sli4_cfg_mhdr));
15778 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15779 LPFC_MBOX_OPCODE_CQ_DESTROY,
15780 length, LPFC_SLI4_MBX_EMBED);
15781 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15782 cq->queue_id);
15783 mbox->vport = cq->phba->pport;
15784 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15785 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15786 /* The IOCTL status is embedded in the mailbox subheader. */
15787 shdr = (union lpfc_sli4_cfg_shdr *)
15788 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15789 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15790 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15791 if (shdr_status || shdr_add_status || rc) {
15792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15793 "2506 CQ_DESTROY mailbox failed with "
15794 "status x%x add_status x%x, mbx status x%x\n",
15795 shdr_status, shdr_add_status, rc);
15796 status = -ENXIO;
15797 }
15798 /* Remove cq from any list */
15799 list_del_init(&cq->list);
8fa38513 15800 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15801 return status;
15802}
15803
04c68496
JS
15804/**
15805 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15806 * @qm: The queue structure associated with the queue to destroy.
15807 *
15808 * This function destroys a queue, as detailed in @mq by sending an mailbox
15809 * command, specific to the type of queue, to the HBA.
15810 *
15811 * The @mq struct is used to get the queue ID of the queue to destroy.
15812 *
15813 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15814 * command fails this function will return -ENXIO.
04c68496 15815 **/
a2fc4aef 15816int
04c68496
JS
15817lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15818{
15819 LPFC_MBOXQ_t *mbox;
15820 int rc, length, status = 0;
15821 uint32_t shdr_status, shdr_add_status;
15822 union lpfc_sli4_cfg_shdr *shdr;
15823
2e90f4b5 15824 /* sanity check on queue memory */
04c68496
JS
15825 if (!mq)
15826 return -ENODEV;
15827 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15828 if (!mbox)
15829 return -ENOMEM;
15830 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15831 sizeof(struct lpfc_sli4_cfg_mhdr));
15832 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15833 LPFC_MBOX_OPCODE_MQ_DESTROY,
15834 length, LPFC_SLI4_MBX_EMBED);
15835 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15836 mq->queue_id);
15837 mbox->vport = mq->phba->pport;
15838 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15839 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15840 /* The IOCTL status is embedded in the mailbox subheader. */
15841 shdr = (union lpfc_sli4_cfg_shdr *)
15842 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15843 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15844 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15845 if (shdr_status || shdr_add_status || rc) {
15846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15847 "2507 MQ_DESTROY mailbox failed with "
15848 "status x%x add_status x%x, mbx status x%x\n",
15849 shdr_status, shdr_add_status, rc);
15850 status = -ENXIO;
15851 }
15852 /* Remove mq from any list */
15853 list_del_init(&mq->list);
8fa38513 15854 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15855 return status;
15856}
15857
4f774513
JS
15858/**
15859 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15860 * @wq: The queue structure associated with the queue to destroy.
15861 *
15862 * This function destroys a queue, as detailed in @wq by sending an mailbox
15863 * command, specific to the type of queue, to the HBA.
15864 *
15865 * The @wq struct is used to get the queue ID of the queue to destroy.
15866 *
15867 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15868 * command fails this function will return -ENXIO.
4f774513 15869 **/
a2fc4aef 15870int
4f774513
JS
15871lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15872{
15873 LPFC_MBOXQ_t *mbox;
15874 int rc, length, status = 0;
15875 uint32_t shdr_status, shdr_add_status;
15876 union lpfc_sli4_cfg_shdr *shdr;
15877
2e90f4b5 15878 /* sanity check on queue memory */
4f774513
JS
15879 if (!wq)
15880 return -ENODEV;
15881 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15882 if (!mbox)
15883 return -ENOMEM;
15884 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15885 sizeof(struct lpfc_sli4_cfg_mhdr));
15886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15887 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15888 length, LPFC_SLI4_MBX_EMBED);
15889 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15890 wq->queue_id);
15891 mbox->vport = wq->phba->pport;
15892 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15893 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15894 shdr = (union lpfc_sli4_cfg_shdr *)
15895 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15896 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15898 if (shdr_status || shdr_add_status || rc) {
15899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15900 "2508 WQ_DESTROY mailbox failed with "
15901 "status x%x add_status x%x, mbx status x%x\n",
15902 shdr_status, shdr_add_status, rc);
15903 status = -ENXIO;
15904 }
15905 /* Remove wq from any list */
15906 list_del_init(&wq->list);
d1f525aa
JS
15907 kfree(wq->pring);
15908 wq->pring = NULL;
8fa38513 15909 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
15910 return status;
15911}
15912
15913/**
15914 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15915 * @rq: The queue structure associated with the queue to destroy.
15916 *
15917 * This function destroys a queue, as detailed in @rq by sending an mailbox
15918 * command, specific to the type of queue, to the HBA.
15919 *
15920 * The @rq struct is used to get the queue ID of the queue to destroy.
15921 *
15922 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15923 * command fails this function will return -ENXIO.
4f774513 15924 **/
a2fc4aef 15925int
4f774513
JS
15926lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15927 struct lpfc_queue *drq)
15928{
15929 LPFC_MBOXQ_t *mbox;
15930 int rc, length, status = 0;
15931 uint32_t shdr_status, shdr_add_status;
15932 union lpfc_sli4_cfg_shdr *shdr;
15933
2e90f4b5 15934 /* sanity check on queue memory */
4f774513
JS
15935 if (!hrq || !drq)
15936 return -ENODEV;
15937 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15938 if (!mbox)
15939 return -ENOMEM;
15940 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 15941 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
15942 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15943 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15944 length, LPFC_SLI4_MBX_EMBED);
15945 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15946 hrq->queue_id);
15947 mbox->vport = hrq->phba->pport;
15948 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15949 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15950 /* The IOCTL status is embedded in the mailbox subheader. */
15951 shdr = (union lpfc_sli4_cfg_shdr *)
15952 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15953 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15954 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15955 if (shdr_status || shdr_add_status || rc) {
15956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15957 "2509 RQ_DESTROY mailbox failed with "
15958 "status x%x add_status x%x, mbx status x%x\n",
15959 shdr_status, shdr_add_status, rc);
15960 if (rc != MBX_TIMEOUT)
15961 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15962 return -ENXIO;
15963 }
15964 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15965 drq->queue_id);
15966 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15967 shdr = (union lpfc_sli4_cfg_shdr *)
15968 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15969 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15970 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15971 if (shdr_status || shdr_add_status || rc) {
15972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15973 "2510 RQ_DESTROY mailbox failed with "
15974 "status x%x add_status x%x, mbx status x%x\n",
15975 shdr_status, shdr_add_status, rc);
15976 status = -ENXIO;
15977 }
15978 list_del_init(&hrq->list);
15979 list_del_init(&drq->list);
8fa38513 15980 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
15981 return status;
15982}
15983
15984/**
15985 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15986 * @phba: The virtual port for which this call being executed.
15987 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15988 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15989 * @xritag: the xritag that ties this io to the SGL pages.
15990 *
15991 * This routine will post the sgl pages for the IO that has the xritag
15992 * that is in the iocbq structure. The xritag is assigned during iocbq
15993 * creation and persists for as long as the driver is loaded.
15994 * if the caller has fewer than 256 scatter gather segments to map then
15995 * pdma_phys_addr1 should be 0.
15996 * If the caller needs to map more than 256 scatter gather segment then
15997 * pdma_phys_addr1 should be a valid physical address.
15998 * physical address for SGLs must be 64 byte aligned.
15999 * If you are going to map 2 SGL's then the first one must have 256 entries
16000 * the second sgl can have between 1 and 256 entries.
16001 *
16002 * Return codes:
16003 * 0 - Success
16004 * -ENXIO, -ENOMEM - Failure
16005 **/
16006int
16007lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16008 dma_addr_t pdma_phys_addr0,
16009 dma_addr_t pdma_phys_addr1,
16010 uint16_t xritag)
16011{
16012 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16013 LPFC_MBOXQ_t *mbox;
16014 int rc;
16015 uint32_t shdr_status, shdr_add_status;
6d368e53 16016 uint32_t mbox_tmo;
4f774513
JS
16017 union lpfc_sli4_cfg_shdr *shdr;
16018
16019 if (xritag == NO_XRI) {
16020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16021 "0364 Invalid param:\n");
16022 return -EINVAL;
16023 }
16024
16025 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16026 if (!mbox)
16027 return -ENOMEM;
16028
16029 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16030 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16031 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 16032 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
16033
16034 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16035 &mbox->u.mqe.un.post_sgl_pages;
16036 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16037 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16038
16039 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16040 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16041 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16042 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16043
16044 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16045 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16046 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16047 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16048 if (!phba->sli4_hba.intr_enable)
16049 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 16050 else {
a183a15f 16051 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
16052 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16053 }
4f774513
JS
16054 /* The IOCTL status is embedded in the mailbox subheader. */
16055 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16056 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16057 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16058 if (rc != MBX_TIMEOUT)
16059 mempool_free(mbox, phba->mbox_mem_pool);
16060 if (shdr_status || shdr_add_status || rc) {
16061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16062 "2511 POST_SGL mailbox failed with "
16063 "status x%x add_status x%x, mbx status x%x\n",
16064 shdr_status, shdr_add_status, rc);
4f774513
JS
16065 }
16066 return 0;
16067}
4f774513 16068
6d368e53 16069/**
88a2cfbb 16070 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
16071 * @phba: pointer to lpfc hba data structure.
16072 *
16073 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
16074 * HBA consistent with the SLI-4 interface spec. This routine
16075 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16076 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 16077 *
88a2cfbb
JS
16078 * Returns
16079 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16080 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16081 **/
5d8b8167 16082static uint16_t
6d368e53
JS
16083lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16084{
16085 unsigned long xri;
16086
16087 /*
16088 * Fetch the next logical xri. Because this index is logical,
16089 * the driver starts at 0 each time.
16090 */
16091 spin_lock_irq(&phba->hbalock);
16092 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16093 phba->sli4_hba.max_cfg_param.max_xri, 0);
16094 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16095 spin_unlock_irq(&phba->hbalock);
16096 return NO_XRI;
16097 } else {
16098 set_bit(xri, phba->sli4_hba.xri_bmask);
16099 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 16100 }
6d368e53
JS
16101 spin_unlock_irq(&phba->hbalock);
16102 return xri;
16103}
16104
16105/**
16106 * lpfc_sli4_free_xri - Release an xri for reuse.
16107 * @phba: pointer to lpfc hba data structure.
16108 *
16109 * This routine is invoked to release an xri to the pool of
16110 * available rpis maintained by the driver.
16111 **/
5d8b8167 16112static void
6d368e53
JS
16113__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16114{
16115 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
16116 phba->sli4_hba.max_cfg_param.xri_used--;
16117 }
16118}
16119
16120/**
16121 * lpfc_sli4_free_xri - Release an xri for reuse.
16122 * @phba: pointer to lpfc hba data structure.
16123 *
16124 * This routine is invoked to release an xri to the pool of
16125 * available rpis maintained by the driver.
16126 **/
16127void
16128lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16129{
16130 spin_lock_irq(&phba->hbalock);
16131 __lpfc_sli4_free_xri(phba, xri);
16132 spin_unlock_irq(&phba->hbalock);
16133}
16134
4f774513
JS
16135/**
16136 * lpfc_sli4_next_xritag - Get an xritag for the io
16137 * @phba: Pointer to HBA context object.
16138 *
16139 * This function gets an xritag for the iocb. If there is no unused xritag
16140 * it will return 0xffff.
16141 * The function returns the allocated xritag if successful, else returns zero.
16142 * Zero is not a valid xritag.
16143 * The caller is not required to hold any lock.
16144 **/
16145uint16_t
16146lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16147{
6d368e53 16148 uint16_t xri_index;
4f774513 16149
6d368e53 16150 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16151 if (xri_index == NO_XRI)
16152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16153 "2004 Failed to allocate XRI.last XRITAG is %d"
16154 " Max XRI is %d, Used XRI is %d\n",
16155 xri_index,
16156 phba->sli4_hba.max_cfg_param.max_xri,
16157 phba->sli4_hba.max_cfg_param.xri_used);
16158 return xri_index;
4f774513
JS
16159}
16160
16161/**
895427bd 16162 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16163 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16164 * @post_sgl_list: pointer to els sgl entry list.
16165 * @count: number of els sgl entries on the list.
4f774513
JS
16166 *
16167 * This routine is invoked to post a block of driver's sgl pages to the
16168 * HBA using non-embedded mailbox command. No Lock is held. This routine
16169 * is only called when the driver is loading and after all IO has been
16170 * stopped.
16171 **/
8a9d2e80 16172static int
895427bd 16173lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16174 struct list_head *post_sgl_list,
16175 int post_cnt)
4f774513 16176{
8a9d2e80 16177 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16178 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16179 struct sgl_page_pairs *sgl_pg_pairs;
16180 void *viraddr;
16181 LPFC_MBOXQ_t *mbox;
16182 uint32_t reqlen, alloclen, pg_pairs;
16183 uint32_t mbox_tmo;
8a9d2e80
JS
16184 uint16_t xritag_start = 0;
16185 int rc = 0;
4f774513
JS
16186 uint32_t shdr_status, shdr_add_status;
16187 union lpfc_sli4_cfg_shdr *shdr;
16188
895427bd 16189 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16190 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16191 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16193 "2559 Block sgl registration required DMA "
16194 "size (%d) great than a page\n", reqlen);
16195 return -ENOMEM;
16196 }
895427bd 16197
4f774513 16198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16199 if (!mbox)
4f774513 16200 return -ENOMEM;
4f774513
JS
16201
16202 /* Allocate DMA memory and set up the non-embedded mailbox command */
16203 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16204 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16205 LPFC_SLI4_MBX_NEMBED);
16206
16207 if (alloclen < reqlen) {
16208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16209 "0285 Allocated DMA memory size (%d) is "
16210 "less than the requested DMA memory "
16211 "size (%d)\n", alloclen, reqlen);
16212 lpfc_sli4_mbox_cmd_free(phba, mbox);
16213 return -ENOMEM;
16214 }
4f774513 16215 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16216 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16217 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16218 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16219
8a9d2e80
JS
16220 pg_pairs = 0;
16221 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16222 /* Set up the sge entry */
16223 sgl_pg_pairs->sgl_pg0_addr_lo =
16224 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16225 sgl_pg_pairs->sgl_pg0_addr_hi =
16226 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16227 sgl_pg_pairs->sgl_pg1_addr_lo =
16228 cpu_to_le32(putPaddrLow(0));
16229 sgl_pg_pairs->sgl_pg1_addr_hi =
16230 cpu_to_le32(putPaddrHigh(0));
6d368e53 16231
4f774513
JS
16232 /* Keep the first xritag on the list */
16233 if (pg_pairs == 0)
16234 xritag_start = sglq_entry->sli4_xritag;
16235 sgl_pg_pairs++;
8a9d2e80 16236 pg_pairs++;
4f774513 16237 }
6d368e53
JS
16238
16239 /* Complete initialization and perform endian conversion. */
4f774513 16240 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16241 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16242 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16243
4f774513
JS
16244 if (!phba->sli4_hba.intr_enable)
16245 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16246 else {
a183a15f 16247 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16248 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16249 }
16250 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16251 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16252 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16253 if (rc != MBX_TIMEOUT)
16254 lpfc_sli4_mbox_cmd_free(phba, mbox);
16255 if (shdr_status || shdr_add_status || rc) {
16256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16257 "2513 POST_SGL_BLOCK mailbox command failed "
16258 "status x%x add_status x%x mbx status x%x\n",
16259 shdr_status, shdr_add_status, rc);
16260 rc = -ENXIO;
16261 }
16262 return rc;
16263}
16264
16265/**
16266 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16267 * @phba: pointer to lpfc hba data structure.
16268 * @sblist: pointer to scsi buffer list.
16269 * @count: number of scsi buffers on the list.
16270 *
16271 * This routine is invoked to post a block of @count scsi sgl pages from a
16272 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16273 * No Lock is held.
16274 *
16275 **/
16276int
8a9d2e80
JS
16277lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16278 struct list_head *sblist,
16279 int count)
4f774513
JS
16280{
16281 struct lpfc_scsi_buf *psb;
16282 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16283 struct sgl_page_pairs *sgl_pg_pairs;
16284 void *viraddr;
16285 LPFC_MBOXQ_t *mbox;
16286 uint32_t reqlen, alloclen, pg_pairs;
16287 uint32_t mbox_tmo;
16288 uint16_t xritag_start = 0;
16289 int rc = 0;
16290 uint32_t shdr_status, shdr_add_status;
16291 dma_addr_t pdma_phys_bpl1;
16292 union lpfc_sli4_cfg_shdr *shdr;
16293
16294 /* Calculate the requested length of the dma memory */
8a9d2e80 16295 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16296 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16297 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16298 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16299 "0217 Block sgl registration required DMA "
16300 "size (%d) great than a page\n", reqlen);
16301 return -ENOMEM;
16302 }
16303 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16304 if (!mbox) {
16305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16306 "0283 Failed to allocate mbox cmd memory\n");
16307 return -ENOMEM;
16308 }
16309
16310 /* Allocate DMA memory and set up the non-embedded mailbox command */
16311 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16312 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16313 LPFC_SLI4_MBX_NEMBED);
16314
16315 if (alloclen < reqlen) {
16316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16317 "2561 Allocated DMA memory size (%d) is "
16318 "less than the requested DMA memory "
16319 "size (%d)\n", alloclen, reqlen);
16320 lpfc_sli4_mbox_cmd_free(phba, mbox);
16321 return -ENOMEM;
16322 }
6d368e53 16323
4f774513 16324 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16325 viraddr = mbox->sge_array->addr[0];
16326
16327 /* Set up the SGL pages in the non-embedded DMA pages */
16328 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16329 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16330
16331 pg_pairs = 0;
16332 list_for_each_entry(psb, sblist, list) {
16333 /* Set up the sge entry */
16334 sgl_pg_pairs->sgl_pg0_addr_lo =
16335 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16336 sgl_pg_pairs->sgl_pg0_addr_hi =
16337 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16338 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16339 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16340 else
16341 pdma_phys_bpl1 = 0;
16342 sgl_pg_pairs->sgl_pg1_addr_lo =
16343 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16344 sgl_pg_pairs->sgl_pg1_addr_hi =
16345 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16346 /* Keep the first xritag on the list */
16347 if (pg_pairs == 0)
16348 xritag_start = psb->cur_iocbq.sli4_xritag;
16349 sgl_pg_pairs++;
16350 pg_pairs++;
16351 }
16352 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16353 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16354 /* Perform endian conversion if necessary */
16355 sgl->word0 = cpu_to_le32(sgl->word0);
16356
16357 if (!phba->sli4_hba.intr_enable)
16358 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16359 else {
a183a15f 16360 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16361 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16362 }
16363 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16366 if (rc != MBX_TIMEOUT)
16367 lpfc_sli4_mbox_cmd_free(phba, mbox);
16368 if (shdr_status || shdr_add_status || rc) {
16369 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16370 "2564 POST_SGL_BLOCK mailbox command failed "
16371 "status x%x add_status x%x mbx status x%x\n",
16372 shdr_status, shdr_add_status, rc);
16373 rc = -ENXIO;
16374 }
16375 return rc;
16376}
16377
16378/**
16379 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16380 * @phba: pointer to lpfc_hba struct that the frame was received on
16381 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16382 *
16383 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16384 * valid type of frame that the LPFC driver will handle. This function will
16385 * return a zero if the frame is a valid frame or a non zero value when the
16386 * frame does not pass the check.
16387 **/
16388static int
16389lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16390{
474ffb74 16391 /* make rctl_names static to save stack space */
4f774513 16392 struct fc_vft_header *fc_vft_hdr;
546fc854 16393 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16394
ae9e28f3
JS
16395#define FC_RCTL_MDS_DIAGS 0xF4
16396
4f774513
JS
16397 switch (fc_hdr->fh_r_ctl) {
16398 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16399 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16400 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16401 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16402 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16403 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16404 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16405 case FC_RCTL_DD_CMD_STATUS: /* command status */
16406 case FC_RCTL_ELS_REQ: /* extended link services request */
16407 case FC_RCTL_ELS_REP: /* extended link services reply */
16408 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16409 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16410 case FC_RCTL_BA_NOP: /* basic link service NOP */
16411 case FC_RCTL_BA_ABTS: /* basic link service abort */
16412 case FC_RCTL_BA_RMC: /* remove connection */
16413 case FC_RCTL_BA_ACC: /* basic accept */
16414 case FC_RCTL_BA_RJT: /* basic reject */
16415 case FC_RCTL_BA_PRMT:
16416 case FC_RCTL_ACK_1: /* acknowledge_1 */
16417 case FC_RCTL_ACK_0: /* acknowledge_0 */
16418 case FC_RCTL_P_RJT: /* port reject */
16419 case FC_RCTL_F_RJT: /* fabric reject */
16420 case FC_RCTL_P_BSY: /* port busy */
16421 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16422 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16423 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16424 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16425 case FC_RCTL_END: /* end */
16426 break;
16427 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16428 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16429 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16430 return lpfc_fc_frame_check(phba, fc_hdr);
16431 default:
16432 goto drop;
16433 }
ae9e28f3
JS
16434
16435#define FC_TYPE_VENDOR_UNIQUE 0xFF
16436
4f774513
JS
16437 switch (fc_hdr->fh_type) {
16438 case FC_TYPE_BLS:
16439 case FC_TYPE_ELS:
16440 case FC_TYPE_FCP:
16441 case FC_TYPE_CT:
895427bd 16442 case FC_TYPE_NVME:
ae9e28f3 16443 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16444 break;
16445 case FC_TYPE_IP:
16446 case FC_TYPE_ILS:
16447 default:
16448 goto drop;
16449 }
546fc854 16450
4f774513 16451 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16452 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16453 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16454 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16455 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16456 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16457 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16458 be32_to_cpu(header[6]));
4f774513
JS
16459 return 0;
16460drop:
16461 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16462 "2539 Dropped frame rctl:x%x type:x%x\n",
16463 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16464 return 1;
16465}
16466
16467/**
16468 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16469 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16470 *
16471 * This function processes the FC header to retrieve the VFI from the VF
16472 * header, if one exists. This function will return the VFI if one exists
16473 * or 0 if no VSAN Header exists.
16474 **/
16475static uint32_t
16476lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16477{
16478 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16479
16480 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16481 return 0;
16482 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16483}
16484
16485/**
16486 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16487 * @phba: Pointer to the HBA structure to search for the vport on
16488 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16489 * @fcfi: The FC Fabric ID that the frame came from
16490 *
16491 * This function searches the @phba for a vport that matches the content of the
16492 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16493 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16494 * returns the matching vport pointer or NULL if unable to match frame to a
16495 * vport.
16496 **/
16497static struct lpfc_vport *
16498lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16499 uint16_t fcfi, uint32_t did)
4f774513
JS
16500{
16501 struct lpfc_vport **vports;
16502 struct lpfc_vport *vport = NULL;
16503 int i;
939723a4 16504
bf08611b
JS
16505 if (did == Fabric_DID)
16506 return phba->pport;
939723a4
JS
16507 if ((phba->pport->fc_flag & FC_PT2PT) &&
16508 !(phba->link_state == LPFC_HBA_READY))
16509 return phba->pport;
16510
4f774513 16511 vports = lpfc_create_vport_work_array(phba);
895427bd 16512 if (vports != NULL) {
4f774513
JS
16513 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16514 if (phba->fcf.fcfi == fcfi &&
16515 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16516 vports[i]->fc_myDID == did) {
16517 vport = vports[i];
16518 break;
16519 }
16520 }
895427bd 16521 }
4f774513
JS
16522 lpfc_destroy_vport_work_array(phba, vports);
16523 return vport;
16524}
16525
45ed1190
JS
16526/**
16527 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16528 * @vport: The vport to work on.
16529 *
16530 * This function updates the receive sequence time stamp for this vport. The
16531 * receive sequence time stamp indicates the time that the last frame of the
16532 * the sequence that has been idle for the longest amount of time was received.
16533 * the driver uses this time stamp to indicate if any received sequences have
16534 * timed out.
16535 **/
5d8b8167 16536static void
45ed1190
JS
16537lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16538{
16539 struct lpfc_dmabuf *h_buf;
16540 struct hbq_dmabuf *dmabuf = NULL;
16541
16542 /* get the oldest sequence on the rcv list */
16543 h_buf = list_get_first(&vport->rcv_buffer_list,
16544 struct lpfc_dmabuf, list);
16545 if (!h_buf)
16546 return;
16547 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16548 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16549}
16550
16551/**
16552 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16553 * @vport: The vport that the received sequences were sent to.
16554 *
16555 * This function cleans up all outstanding received sequences. This is called
16556 * by the driver when a link event or user action invalidates all the received
16557 * sequences.
16558 **/
16559void
16560lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16561{
16562 struct lpfc_dmabuf *h_buf, *hnext;
16563 struct lpfc_dmabuf *d_buf, *dnext;
16564 struct hbq_dmabuf *dmabuf = NULL;
16565
16566 /* start with the oldest sequence on the rcv list */
16567 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16568 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16569 list_del_init(&dmabuf->hbuf.list);
16570 list_for_each_entry_safe(d_buf, dnext,
16571 &dmabuf->dbuf.list, list) {
16572 list_del_init(&d_buf->list);
16573 lpfc_in_buf_free(vport->phba, d_buf);
16574 }
16575 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16576 }
16577}
16578
16579/**
16580 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16581 * @vport: The vport that the received sequences were sent to.
16582 *
16583 * This function determines whether any received sequences have timed out by
16584 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16585 * indicates that there is at least one timed out sequence this routine will
16586 * go through the received sequences one at a time from most inactive to most
16587 * active to determine which ones need to be cleaned up. Once it has determined
16588 * that a sequence needs to be cleaned up it will simply free up the resources
16589 * without sending an abort.
16590 **/
16591void
16592lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16593{
16594 struct lpfc_dmabuf *h_buf, *hnext;
16595 struct lpfc_dmabuf *d_buf, *dnext;
16596 struct hbq_dmabuf *dmabuf = NULL;
16597 unsigned long timeout;
16598 int abort_count = 0;
16599
16600 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16601 vport->rcv_buffer_time_stamp);
16602 if (list_empty(&vport->rcv_buffer_list) ||
16603 time_before(jiffies, timeout))
16604 return;
16605 /* start with the oldest sequence on the rcv list */
16606 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16607 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16608 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16609 dmabuf->time_stamp);
16610 if (time_before(jiffies, timeout))
16611 break;
16612 abort_count++;
16613 list_del_init(&dmabuf->hbuf.list);
16614 list_for_each_entry_safe(d_buf, dnext,
16615 &dmabuf->dbuf.list, list) {
16616 list_del_init(&d_buf->list);
16617 lpfc_in_buf_free(vport->phba, d_buf);
16618 }
16619 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16620 }
16621 if (abort_count)
16622 lpfc_update_rcv_time_stamp(vport);
16623}
16624
4f774513
JS
16625/**
16626 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16627 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16628 *
16629 * This function searches through the existing incomplete sequences that have
16630 * been sent to this @vport. If the frame matches one of the incomplete
16631 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16632 * make up that sequence. If no sequence is found that matches this frame then
16633 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16634 * This function returns a pointer to the first dmabuf in the sequence list that
16635 * the frame was linked to.
16636 **/
16637static struct hbq_dmabuf *
16638lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16639{
16640 struct fc_frame_header *new_hdr;
16641 struct fc_frame_header *temp_hdr;
16642 struct lpfc_dmabuf *d_buf;
16643 struct lpfc_dmabuf *h_buf;
16644 struct hbq_dmabuf *seq_dmabuf = NULL;
16645 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16646 uint8_t found = 0;
4f774513 16647
4d9ab994 16648 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16649 dmabuf->time_stamp = jiffies;
4f774513 16650 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16651
4f774513
JS
16652 /* Use the hdr_buf to find the sequence that this frame belongs to */
16653 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16654 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16655 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16656 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16657 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16658 continue;
16659 /* found a pending sequence that matches this frame */
16660 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16661 break;
16662 }
16663 if (!seq_dmabuf) {
16664 /*
16665 * This indicates first frame received for this sequence.
16666 * Queue the buffer on the vport's rcv_buffer_list.
16667 */
16668 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16669 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16670 return dmabuf;
16671 }
16672 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16673 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16674 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16675 list_del_init(&seq_dmabuf->hbuf.list);
16676 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16677 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16678 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16679 return dmabuf;
16680 }
45ed1190
JS
16681 /* move this sequence to the tail to indicate a young sequence */
16682 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16683 seq_dmabuf->time_stamp = jiffies;
16684 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16685 if (list_empty(&seq_dmabuf->dbuf.list)) {
16686 temp_hdr = dmabuf->hbuf.virt;
16687 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16688 return seq_dmabuf;
16689 }
4f774513 16690 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16691 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16692 while (!found) {
4f774513
JS
16693 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16694 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16695 /*
16696 * If the frame's sequence count is greater than the frame on
16697 * the list then insert the frame right after this frame
16698 */
eeead811
JS
16699 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16700 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16701 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16702 found = 1;
16703 break;
4f774513 16704 }
4360ca9c
JS
16705
16706 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16707 break;
16708 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16709 }
4360ca9c
JS
16710
16711 if (found)
16712 return seq_dmabuf;
4f774513
JS
16713 return NULL;
16714}
16715
6669f9bb
JS
16716/**
16717 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16718 * @vport: pointer to a vitural port
16719 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16720 *
16721 * This function tries to abort from the partially assembed sequence, described
16722 * by the information from basic abbort @dmabuf. It checks to see whether such
16723 * partially assembled sequence held by the driver. If so, it shall free up all
16724 * the frames from the partially assembled sequence.
16725 *
16726 * Return
16727 * true -- if there is matching partially assembled sequence present and all
16728 * the frames freed with the sequence;
16729 * false -- if there is no matching partially assembled sequence present so
16730 * nothing got aborted in the lower layer driver
16731 **/
16732static bool
16733lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16734 struct hbq_dmabuf *dmabuf)
16735{
16736 struct fc_frame_header *new_hdr;
16737 struct fc_frame_header *temp_hdr;
16738 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16739 struct hbq_dmabuf *seq_dmabuf = NULL;
16740
16741 /* Use the hdr_buf to find the sequence that matches this frame */
16742 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16743 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16744 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16745 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16746 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16747 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16748 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16749 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16750 continue;
16751 /* found a pending sequence that matches this frame */
16752 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16753 break;
16754 }
16755
16756 /* Free up all the frames from the partially assembled sequence */
16757 if (seq_dmabuf) {
16758 list_for_each_entry_safe(d_buf, n_buf,
16759 &seq_dmabuf->dbuf.list, list) {
16760 list_del_init(&d_buf->list);
16761 lpfc_in_buf_free(vport->phba, d_buf);
16762 }
16763 return true;
16764 }
16765 return false;
16766}
16767
6dd9e31c
JS
16768/**
16769 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16770 * @vport: pointer to a vitural port
16771 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16772 *
16773 * This function tries to abort from the assembed sequence from upper level
16774 * protocol, described by the information from basic abbort @dmabuf. It
16775 * checks to see whether such pending context exists at upper level protocol.
16776 * If so, it shall clean up the pending context.
16777 *
16778 * Return
16779 * true -- if there is matching pending context of the sequence cleaned
16780 * at ulp;
16781 * false -- if there is no matching pending context of the sequence present
16782 * at ulp.
16783 **/
16784static bool
16785lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16786{
16787 struct lpfc_hba *phba = vport->phba;
16788 int handled;
16789
16790 /* Accepting abort at ulp with SLI4 only */
16791 if (phba->sli_rev < LPFC_SLI_REV4)
16792 return false;
16793
16794 /* Register all caring upper level protocols to attend abort */
16795 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16796 if (handled)
16797 return true;
16798
16799 return false;
16800}
16801
6669f9bb 16802/**
546fc854 16803 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16804 * @phba: Pointer to HBA context object.
16805 * @cmd_iocbq: pointer to the command iocbq structure.
16806 * @rsp_iocbq: pointer to the response iocbq structure.
16807 *
546fc854 16808 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16809 * event. It properly releases the memory allocated to the sequence abort
16810 * accept iocb.
16811 **/
16812static void
546fc854 16813lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16814 struct lpfc_iocbq *cmd_iocbq,
16815 struct lpfc_iocbq *rsp_iocbq)
16816{
6dd9e31c
JS
16817 struct lpfc_nodelist *ndlp;
16818
16819 if (cmd_iocbq) {
16820 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16821 lpfc_nlp_put(ndlp);
16822 lpfc_nlp_not_used(ndlp);
6669f9bb 16823 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16824 }
6b5151fd
JS
16825
16826 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16827 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16829 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16830 rsp_iocbq->iocb.ulpStatus,
16831 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16832}
16833
6d368e53
JS
16834/**
16835 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16836 * @phba: Pointer to HBA context object.
16837 * @xri: xri id in transaction.
16838 *
16839 * This function validates the xri maps to the known range of XRIs allocated an
16840 * used by the driver.
16841 **/
7851fe2c 16842uint16_t
6d368e53
JS
16843lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16844 uint16_t xri)
16845{
a2fc4aef 16846 uint16_t i;
6d368e53
JS
16847
16848 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16849 if (xri == phba->sli4_hba.xri_ids[i])
16850 return i;
16851 }
16852 return NO_XRI;
16853}
16854
6669f9bb 16855/**
546fc854 16856 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16857 * @phba: Pointer to HBA context object.
16858 * @fc_hdr: pointer to a FC frame header.
16859 *
546fc854 16860 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16861 * event after aborting the sequence handling.
16862 **/
86c67379 16863void
6dd9e31c
JS
16864lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16865 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16866{
6dd9e31c 16867 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
16868 struct lpfc_iocbq *ctiocb = NULL;
16869 struct lpfc_nodelist *ndlp;
ee0f4fe1 16870 uint16_t oxid, rxid, xri, lxri;
5ffc266e 16871 uint32_t sid, fctl;
6669f9bb 16872 IOCB_t *icmd;
546fc854 16873 int rc;
6669f9bb
JS
16874
16875 if (!lpfc_is_link_up(phba))
16876 return;
16877
16878 sid = sli4_sid_from_fc_hdr(fc_hdr);
16879 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 16880 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 16881
6dd9e31c 16882 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 16883 if (!ndlp) {
9d3d340d 16884 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
16885 if (!ndlp) {
16886 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16887 "1268 Failed to allocate ndlp for "
16888 "oxid:x%x SID:x%x\n", oxid, sid);
16889 return;
16890 }
6dd9e31c
JS
16891 /* Put ndlp onto pport node list */
16892 lpfc_enqueue_node(vport, ndlp);
16893 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16894 /* re-setup ndlp without removing from node list */
16895 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16896 if (!ndlp) {
16897 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16898 "3275 Failed to active ndlp found "
16899 "for oxid:x%x SID:x%x\n", oxid, sid);
16900 return;
16901 }
6669f9bb
JS
16902 }
16903
546fc854 16904 /* Allocate buffer for rsp iocb */
6669f9bb
JS
16905 ctiocb = lpfc_sli_get_iocbq(phba);
16906 if (!ctiocb)
16907 return;
16908
5ffc266e
JS
16909 /* Extract the F_CTL field from FC_HDR */
16910 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16911
6669f9bb 16912 icmd = &ctiocb->iocb;
6669f9bb 16913 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 16914 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
16915 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16916 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16917 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16918
16919 /* Fill in the rest of iocb fields */
16920 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16921 icmd->ulpBdeCount = 0;
16922 icmd->ulpLe = 1;
16923 icmd->ulpClass = CLASS3;
6d368e53 16924 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 16925 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 16926
6669f9bb
JS
16927 ctiocb->iocb_cmpl = NULL;
16928 ctiocb->vport = phba->pport;
546fc854 16929 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 16930 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
16931 ctiocb->sli4_xritag = NO_XRI;
16932
ee0f4fe1
JS
16933 if (fctl & FC_FC_EX_CTX)
16934 /* Exchange responder sent the abort so we
16935 * own the oxid.
16936 */
16937 xri = oxid;
16938 else
16939 xri = rxid;
16940 lxri = lpfc_sli4_xri_inrange(phba, xri);
16941 if (lxri != NO_XRI)
16942 lpfc_set_rrq_active(phba, ndlp, lxri,
16943 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
16944 /* For BA_ABTS from exchange responder, if the logical xri with
16945 * the oxid maps to the FCP XRI range, the port no longer has
16946 * that exchange context, send a BLS_RJT. Override the IOCB for
16947 * a BA_RJT.
16948 */
16949 if ((fctl & FC_FC_EX_CTX) &&
895427bd 16950 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
16951 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16952 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16953 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16954 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16955 }
16956
16957 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16958 * the driver no longer has that exchange, send a BLS_RJT. Override
16959 * the IOCB for a BA_RJT.
546fc854 16960 */
6dd9e31c 16961 if (aborted == false) {
546fc854
JS
16962 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16963 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16964 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16965 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16966 }
6669f9bb 16967
5ffc266e
JS
16968 if (fctl & FC_FC_EX_CTX) {
16969 /* ABTS sent by responder to CT exchange, construction
16970 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16971 * field and RX_ID from ABTS for RX_ID field.
16972 */
546fc854 16973 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
16974 } else {
16975 /* ABTS sent by initiator to CT exchange, construction
16976 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 16977 * XRI_TAG field.
5ffc266e 16978 */
546fc854 16979 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 16980 }
f09c3acc 16981 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 16982 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 16983
546fc854 16984 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
16985 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16986 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16987 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
16988
16989 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16990 if (rc == IOCB_ERROR) {
6dd9e31c
JS
16991 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16992 "2925 Failed to issue CT ABTS RSP x%x on "
16993 "xri x%x, Data x%x\n",
16994 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16995 phba->link_state);
16996 lpfc_nlp_put(ndlp);
16997 ctiocb->context1 = NULL;
546fc854
JS
16998 lpfc_sli_release_iocbq(phba, ctiocb);
16999 }
6669f9bb
JS
17000}
17001
17002/**
17003 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17004 * @vport: Pointer to the vport on which this sequence was received
17005 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17006 *
17007 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17008 * receive sequence is only partially assembed by the driver, it shall abort
17009 * the partially assembled frames for the sequence. Otherwise, if the
17010 * unsolicited receive sequence has been completely assembled and passed to
17011 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17012 * unsolicited sequence has been aborted. After that, it will issue a basic
17013 * accept to accept the abort.
17014 **/
5d8b8167 17015static void
6669f9bb
JS
17016lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17017 struct hbq_dmabuf *dmabuf)
17018{
17019 struct lpfc_hba *phba = vport->phba;
17020 struct fc_frame_header fc_hdr;
5ffc266e 17021 uint32_t fctl;
6dd9e31c 17022 bool aborted;
6669f9bb 17023
6669f9bb
JS
17024 /* Make a copy of fc_hdr before the dmabuf being released */
17025 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 17026 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 17027
5ffc266e 17028 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
17029 /* ABTS by responder to exchange, no cleanup needed */
17030 aborted = true;
5ffc266e 17031 } else {
6dd9e31c
JS
17032 /* ABTS by initiator to exchange, need to do cleanup */
17033 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17034 if (aborted == false)
17035 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 17036 }
6dd9e31c
JS
17037 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17038
86c67379
JS
17039 if (phba->nvmet_support) {
17040 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17041 return;
17042 }
17043
6dd9e31c
JS
17044 /* Respond with BA_ACC or BA_RJT accordingly */
17045 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
17046}
17047
4f774513
JS
17048/**
17049 * lpfc_seq_complete - Indicates if a sequence is complete
17050 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17051 *
17052 * This function checks the sequence, starting with the frame described by
17053 * @dmabuf, to see if all the frames associated with this sequence are present.
17054 * the frames associated with this sequence are linked to the @dmabuf using the
17055 * dbuf list. This function looks for two major things. 1) That the first frame
17056 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17057 * set. 3) That there are no holes in the sequence count. The function will
17058 * return 1 when the sequence is complete, otherwise it will return 0.
17059 **/
17060static int
17061lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17062{
17063 struct fc_frame_header *hdr;
17064 struct lpfc_dmabuf *d_buf;
17065 struct hbq_dmabuf *seq_dmabuf;
17066 uint32_t fctl;
17067 int seq_count = 0;
17068
17069 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17070 /* make sure first fame of sequence has a sequence count of zero */
17071 if (hdr->fh_seq_cnt != seq_count)
17072 return 0;
17073 fctl = (hdr->fh_f_ctl[0] << 16 |
17074 hdr->fh_f_ctl[1] << 8 |
17075 hdr->fh_f_ctl[2]);
17076 /* If last frame of sequence we can return success. */
17077 if (fctl & FC_FC_END_SEQ)
17078 return 1;
17079 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17080 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17081 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17082 /* If there is a hole in the sequence count then fail. */
eeead811 17083 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
17084 return 0;
17085 fctl = (hdr->fh_f_ctl[0] << 16 |
17086 hdr->fh_f_ctl[1] << 8 |
17087 hdr->fh_f_ctl[2]);
17088 /* If last frame of sequence we can return success. */
17089 if (fctl & FC_FC_END_SEQ)
17090 return 1;
17091 }
17092 return 0;
17093}
17094
17095/**
17096 * lpfc_prep_seq - Prep sequence for ULP processing
17097 * @vport: Pointer to the vport on which this sequence was received
17098 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17099 *
17100 * This function takes a sequence, described by a list of frames, and creates
17101 * a list of iocbq structures to describe the sequence. This iocbq list will be
17102 * used to issue to the generic unsolicited sequence handler. This routine
17103 * returns a pointer to the first iocbq in the list. If the function is unable
17104 * to allocate an iocbq then it throw out the received frames that were not
17105 * able to be described and return a pointer to the first iocbq. If unable to
17106 * allocate any iocbqs (including the first) this function will return NULL.
17107 **/
17108static struct lpfc_iocbq *
17109lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17110{
7851fe2c 17111 struct hbq_dmabuf *hbq_buf;
4f774513
JS
17112 struct lpfc_dmabuf *d_buf, *n_buf;
17113 struct lpfc_iocbq *first_iocbq, *iocbq;
17114 struct fc_frame_header *fc_hdr;
17115 uint32_t sid;
7851fe2c 17116 uint32_t len, tot_len;
eeead811 17117 struct ulp_bde64 *pbde;
4f774513
JS
17118
17119 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17120 /* remove from receive buffer list */
17121 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 17122 lpfc_update_rcv_time_stamp(vport);
4f774513 17123 /* get the Remote Port's SID */
6669f9bb 17124 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 17125 tot_len = 0;
4f774513
JS
17126 /* Get an iocbq struct to fill in. */
17127 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17128 if (first_iocbq) {
17129 /* Initialize the first IOCB. */
8fa38513 17130 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 17131 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 17132 first_iocbq->vport = vport;
939723a4
JS
17133
17134 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17135 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17136 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17137 first_iocbq->iocb.un.rcvels.parmRo =
17138 sli4_did_from_fc_hdr(fc_hdr);
17139 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17140 } else
17141 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
17142 first_iocbq->iocb.ulpContext = NO_XRI;
17143 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17144 be16_to_cpu(fc_hdr->fh_ox_id);
17145 /* iocbq is prepped for internal consumption. Physical vpi. */
17146 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17147 vport->phba->vpi_ids[vport->vpi];
4f774513 17148 /* put the first buffer into the first IOCBq */
48a5a664
JS
17149 tot_len = bf_get(lpfc_rcqe_length,
17150 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17151
4f774513
JS
17152 first_iocbq->context2 = &seq_dmabuf->dbuf;
17153 first_iocbq->context3 = NULL;
17154 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17155 if (tot_len > LPFC_DATA_BUF_SIZE)
17156 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17157 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17158 else
17159 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17160
4f774513 17161 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17162
7851fe2c 17163 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17164 }
17165 iocbq = first_iocbq;
17166 /*
17167 * Each IOCBq can have two Buffers assigned, so go through the list
17168 * of buffers for this sequence and save two buffers in each IOCBq
17169 */
17170 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17171 if (!iocbq) {
17172 lpfc_in_buf_free(vport->phba, d_buf);
17173 continue;
17174 }
17175 if (!iocbq->context3) {
17176 iocbq->context3 = d_buf;
17177 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17178 /* We need to get the size out of the right CQE */
17179 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17180 len = bf_get(lpfc_rcqe_length,
17181 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17182 pbde = (struct ulp_bde64 *)
17183 &iocbq->iocb.unsli3.sli3Words[4];
17184 if (len > LPFC_DATA_BUF_SIZE)
17185 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17186 else
17187 pbde->tus.f.bdeSize = len;
17188
7851fe2c
JS
17189 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17190 tot_len += len;
4f774513
JS
17191 } else {
17192 iocbq = lpfc_sli_get_iocbq(vport->phba);
17193 if (!iocbq) {
17194 if (first_iocbq) {
17195 first_iocbq->iocb.ulpStatus =
17196 IOSTAT_FCP_RSP_ERROR;
17197 first_iocbq->iocb.un.ulpWord[4] =
17198 IOERR_NO_RESOURCES;
17199 }
17200 lpfc_in_buf_free(vport->phba, d_buf);
17201 continue;
17202 }
48a5a664
JS
17203 /* We need to get the size out of the right CQE */
17204 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17205 len = bf_get(lpfc_rcqe_length,
17206 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17207 iocbq->context2 = d_buf;
17208 iocbq->context3 = NULL;
17209 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17210 if (len > LPFC_DATA_BUF_SIZE)
17211 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17212 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17213 else
17214 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17215
7851fe2c
JS
17216 tot_len += len;
17217 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17218
4f774513
JS
17219 iocbq->iocb.un.rcvels.remoteID = sid;
17220 list_add_tail(&iocbq->list, &first_iocbq->list);
17221 }
17222 }
17223 return first_iocbq;
17224}
17225
6669f9bb
JS
17226static void
17227lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17228 struct hbq_dmabuf *seq_dmabuf)
17229{
17230 struct fc_frame_header *fc_hdr;
17231 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17232 struct lpfc_hba *phba = vport->phba;
17233
17234 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17235 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17236 if (!iocbq) {
17237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17238 "2707 Ring %d handler: Failed to allocate "
17239 "iocb Rctl x%x Type x%x received\n",
17240 LPFC_ELS_RING,
17241 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17242 return;
17243 }
17244 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17245 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17246 iocbq, fc_hdr->fh_r_ctl,
17247 fc_hdr->fh_type))
6d368e53 17248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17249 "2540 Ring %d handler: unexpected Rctl "
17250 "x%x Type x%x received\n",
17251 LPFC_ELS_RING,
17252 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17253
17254 /* Free iocb created in lpfc_prep_seq */
17255 list_for_each_entry_safe(curr_iocb, next_iocb,
17256 &iocbq->list, list) {
17257 list_del_init(&curr_iocb->list);
17258 lpfc_sli_release_iocbq(phba, curr_iocb);
17259 }
17260 lpfc_sli_release_iocbq(phba, iocbq);
17261}
17262
ae9e28f3
JS
17263static void
17264lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17265 struct lpfc_iocbq *rspiocb)
17266{
17267 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17268
17269 if (pcmd && pcmd->virt)
771db5c0 17270 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17271 kfree(pcmd);
17272 lpfc_sli_release_iocbq(phba, cmdiocb);
17273}
17274
17275static void
17276lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17277 struct hbq_dmabuf *dmabuf)
17278{
17279 struct fc_frame_header *fc_hdr;
17280 struct lpfc_hba *phba = vport->phba;
17281 struct lpfc_iocbq *iocbq = NULL;
17282 union lpfc_wqe *wqe;
17283 struct lpfc_dmabuf *pcmd = NULL;
17284 uint32_t frame_len;
17285 int rc;
17286
17287 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17288 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17289
17290 /* Send the received frame back */
17291 iocbq = lpfc_sli_get_iocbq(phba);
17292 if (!iocbq)
17293 goto exit;
17294
17295 /* Allocate buffer for command payload */
17296 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17297 if (pcmd)
771db5c0 17298 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17299 &pcmd->phys);
17300 if (!pcmd || !pcmd->virt)
17301 goto exit;
17302
17303 INIT_LIST_HEAD(&pcmd->list);
17304
17305 /* copyin the payload */
17306 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17307
17308 /* fill in BDE's for command */
17309 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17310 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17311 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17312 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17313
17314 iocbq->context2 = pcmd;
17315 iocbq->vport = vport;
17316 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17317 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17318
17319 /*
17320 * Setup rest of the iocb as though it were a WQE
17321 * Build the SEND_FRAME WQE
17322 */
17323 wqe = (union lpfc_wqe *)&iocbq->iocb;
17324
17325 wqe->send_frame.frame_len = frame_len;
17326 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17327 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17328 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17329 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17330 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17331 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17332
17333 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17334 iocbq->iocb.ulpLe = 1;
17335 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17336 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17337 if (rc == IOCB_ERROR)
17338 goto exit;
17339
17340 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17341 return;
17342
17343exit:
17344 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17345 "2023 Unable to process MDS loopback frame\n");
17346 if (pcmd && pcmd->virt)
771db5c0 17347 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17348 kfree(pcmd);
401bb416
DK
17349 if (iocbq)
17350 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17351 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17352}
17353
4f774513
JS
17354/**
17355 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17356 * @phba: Pointer to HBA context object.
17357 *
17358 * This function is called with no lock held. This function processes all
17359 * the received buffers and gives it to upper layers when a received buffer
17360 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17361 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17362 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17363 * appropriate receive function when the final frame in a sequence is received.
17364 **/
4d9ab994
JS
17365void
17366lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17367 struct hbq_dmabuf *dmabuf)
4f774513 17368{
4d9ab994 17369 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17370 struct fc_frame_header *fc_hdr;
17371 struct lpfc_vport *vport;
17372 uint32_t fcfi;
939723a4 17373 uint32_t did;
4f774513 17374
4f774513 17375 /* Process each received buffer */
4d9ab994 17376 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17377
4d9ab994
JS
17378 /* check to see if this a valid type of frame */
17379 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17380 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17381 return;
17382 }
2ea259ee 17383
7851fe2c
JS
17384 if ((bf_get(lpfc_cqe_code,
17385 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17386 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17387 &dmabuf->cq_event.cqe.rcqe_cmpl);
17388 else
17389 fcfi = bf_get(lpfc_rcqe_fcf_id,
17390 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17391
ae9e28f3
JS
17392 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17393 vport = phba->pport;
17394 /* Handle MDS Loopback frames */
17395 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17396 return;
17397 }
17398
895427bd
JS
17399 /* d_id this frame is directed to */
17400 did = sli4_did_from_fc_hdr(fc_hdr);
17401
17402 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17403 if (!vport) {
4d9ab994
JS
17404 /* throw out the frame */
17405 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17406 return;
17407 }
939723a4 17408
939723a4
JS
17409 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17410 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17411 (did != Fabric_DID)) {
17412 /*
17413 * Throw out the frame if we are not pt2pt.
17414 * The pt2pt protocol allows for discovery frames
17415 * to be received without a registered VPI.
17416 */
17417 if (!(vport->fc_flag & FC_PT2PT) ||
17418 (phba->link_state == LPFC_HBA_READY)) {
17419 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17420 return;
17421 }
17422 }
17423
6669f9bb
JS
17424 /* Handle the basic abort sequence (BA_ABTS) event */
17425 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17426 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17427 return;
17428 }
17429
4d9ab994
JS
17430 /* Link this frame */
17431 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17432 if (!seq_dmabuf) {
17433 /* unable to add frame to vport - throw it out */
17434 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17435 return;
17436 }
17437 /* If not last frame in sequence continue processing frames. */
def9c7a9 17438 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17439 return;
def9c7a9 17440
6669f9bb
JS
17441 /* Send the complete sequence to the upper layer protocol */
17442 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17443}
6fb120a7
JS
17444
17445/**
17446 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17447 * @phba: pointer to lpfc hba data structure.
17448 *
17449 * This routine is invoked to post rpi header templates to the
17450 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17451 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17452 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17453 *
17454 * This routine does not require any locks. It's usage is expected
17455 * to be driver load or reset recovery when the driver is
17456 * sequential.
17457 *
17458 * Return codes
af901ca1 17459 * 0 - successful
d439d286 17460 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17461 * When this error occurs, the driver is not guaranteed
17462 * to have any rpi regions posted to the device and
17463 * must either attempt to repost the regions or take a
17464 * fatal error.
17465 **/
17466int
17467lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17468{
17469 struct lpfc_rpi_hdr *rpi_page;
17470 uint32_t rc = 0;
6d368e53
JS
17471 uint16_t lrpi = 0;
17472
17473 /* SLI4 ports that support extents do not require RPI headers. */
17474 if (!phba->sli4_hba.rpi_hdrs_in_use)
17475 goto exit;
17476 if (phba->sli4_hba.extents_in_use)
17477 return -EIO;
6fb120a7 17478
6fb120a7 17479 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17480 /*
17481 * Assign the rpi headers a physical rpi only if the driver
17482 * has not initialized those resources. A port reset only
17483 * needs the headers posted.
17484 */
17485 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17486 LPFC_RPI_RSRC_RDY)
17487 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17488
6fb120a7
JS
17489 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17490 if (rc != MBX_SUCCESS) {
17491 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17492 "2008 Error %d posting all rpi "
17493 "headers\n", rc);
17494 rc = -EIO;
17495 break;
17496 }
17497 }
17498
6d368e53
JS
17499 exit:
17500 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17501 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17502 return rc;
17503}
17504
17505/**
17506 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17507 * @phba: pointer to lpfc hba data structure.
17508 * @rpi_page: pointer to the rpi memory region.
17509 *
17510 * This routine is invoked to post a single rpi header to the
17511 * HBA consistent with the SLI-4 interface spec. This memory region
17512 * maps up to 64 rpi context regions.
17513 *
17514 * Return codes
af901ca1 17515 * 0 - successful
d439d286
JS
17516 * -ENOMEM - No available memory
17517 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17518 **/
17519int
17520lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17521{
17522 LPFC_MBOXQ_t *mboxq;
17523 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17524 uint32_t rc = 0;
6fb120a7
JS
17525 uint32_t shdr_status, shdr_add_status;
17526 union lpfc_sli4_cfg_shdr *shdr;
17527
6d368e53
JS
17528 /* SLI4 ports that support extents do not require RPI headers. */
17529 if (!phba->sli4_hba.rpi_hdrs_in_use)
17530 return rc;
17531 if (phba->sli4_hba.extents_in_use)
17532 return -EIO;
17533
6fb120a7
JS
17534 /* The port is notified of the header region via a mailbox command. */
17535 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17536 if (!mboxq) {
17537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17538 "2001 Unable to allocate memory for issuing "
17539 "SLI_CONFIG_SPECIAL mailbox command\n");
17540 return -ENOMEM;
17541 }
17542
17543 /* Post all rpi memory regions to the port. */
17544 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17545 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17546 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17547 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17548 sizeof(struct lpfc_sli4_cfg_mhdr),
17549 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17550
17551
17552 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17553 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17554 rpi_page->start_rpi);
6d368e53
JS
17555 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17556 hdr_tmpl, rpi_page->page_count);
17557
6fb120a7
JS
17558 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17559 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17560 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17561 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17562 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17563 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17564 if (rc != MBX_TIMEOUT)
17565 mempool_free(mboxq, phba->mbox_mem_pool);
17566 if (shdr_status || shdr_add_status || rc) {
17567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17568 "2514 POST_RPI_HDR mailbox failed with "
17569 "status x%x add_status x%x, mbx status x%x\n",
17570 shdr_status, shdr_add_status, rc);
17571 rc = -ENXIO;
845d9e8d
JS
17572 } else {
17573 /*
17574 * The next_rpi stores the next logical module-64 rpi value used
17575 * to post physical rpis in subsequent rpi postings.
17576 */
17577 spin_lock_irq(&phba->hbalock);
17578 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17579 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17580 }
17581 return rc;
17582}
17583
17584/**
17585 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17586 * @phba: pointer to lpfc hba data structure.
17587 *
17588 * This routine is invoked to post rpi header templates to the
17589 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17590 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17591 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17592 *
17593 * Returns
af901ca1 17594 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17595 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17596 **/
17597int
17598lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17599{
6d368e53
JS
17600 unsigned long rpi;
17601 uint16_t max_rpi, rpi_limit;
17602 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17603 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17604 unsigned long iflag;
6fb120a7 17605
6fb120a7 17606 /*
6d368e53
JS
17607 * Fetch the next logical rpi. Because this index is logical,
17608 * the driver starts at 0 each time.
6fb120a7 17609 */
4902b381 17610 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17611 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17612 rpi_limit = phba->sli4_hba.next_rpi;
17613
6d368e53
JS
17614 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17615 if (rpi >= rpi_limit)
6fb120a7
JS
17616 rpi = LPFC_RPI_ALLOC_ERROR;
17617 else {
17618 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17619 phba->sli4_hba.max_cfg_param.rpi_used++;
17620 phba->sli4_hba.rpi_count++;
17621 }
be6bb941
JS
17622 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17623 "0001 rpi:%x max:%x lim:%x\n",
17624 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17625
17626 /*
17627 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17628 * has been exhausted.
6fb120a7
JS
17629 */
17630 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17631 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17632 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17633 return rpi;
17634 }
17635
6d368e53
JS
17636 /*
17637 * RPI header postings are not required for SLI4 ports capable of
17638 * extents.
17639 */
17640 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17641 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17642 return rpi;
17643 }
17644
6fb120a7
JS
17645 /*
17646 * If the driver is running low on rpi resources, allocate another
17647 * page now. Note that the next_rpi value is used because
17648 * it represents how many are actually in use whereas max_rpi notes
17649 * how many are supported max by the device.
17650 */
6d368e53 17651 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17652 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17653 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17654 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17655 if (!rpi_hdr) {
17656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17657 "2002 Error Could not grow rpi "
17658 "count\n");
17659 } else {
6d368e53
JS
17660 lrpi = rpi_hdr->start_rpi;
17661 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17662 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17663 }
17664 }
17665
17666 return rpi;
17667}
17668
d7c47992
JS
17669/**
17670 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17671 * @phba: pointer to lpfc hba data structure.
17672 *
17673 * This routine is invoked to release an rpi to the pool of
17674 * available rpis maintained by the driver.
17675 **/
5d8b8167 17676static void
d7c47992
JS
17677__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17678{
17679 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17680 phba->sli4_hba.rpi_count--;
17681 phba->sli4_hba.max_cfg_param.rpi_used--;
17682 }
17683}
17684
6fb120a7
JS
17685/**
17686 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17687 * @phba: pointer to lpfc hba data structure.
17688 *
17689 * This routine is invoked to release an rpi to the pool of
17690 * available rpis maintained by the driver.
17691 **/
17692void
17693lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17694{
17695 spin_lock_irq(&phba->hbalock);
d7c47992 17696 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17697 spin_unlock_irq(&phba->hbalock);
17698}
17699
17700/**
17701 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17702 * @phba: pointer to lpfc hba data structure.
17703 *
17704 * This routine is invoked to remove the memory region that
17705 * provided rpi via a bitmask.
17706 **/
17707void
17708lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17709{
17710 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17711 kfree(phba->sli4_hba.rpi_ids);
17712 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17713}
17714
17715/**
17716 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17717 * @phba: pointer to lpfc hba data structure.
17718 *
17719 * This routine is invoked to remove the memory region that
17720 * provided rpi via a bitmask.
17721 **/
17722int
6b5151fd
JS
17723lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17724 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17725{
17726 LPFC_MBOXQ_t *mboxq;
17727 struct lpfc_hba *phba = ndlp->phba;
17728 int rc;
17729
17730 /* The port is notified of the header region via a mailbox command. */
17731 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17732 if (!mboxq)
17733 return -ENOMEM;
17734
17735 /* Post all rpi memory regions to the port. */
17736 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17737 if (cmpl) {
17738 mboxq->mbox_cmpl = cmpl;
17739 mboxq->context1 = arg;
17740 mboxq->context2 = ndlp;
72859909
JS
17741 } else
17742 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17743 mboxq->vport = ndlp->vport;
6fb120a7
JS
17744 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17745 if (rc == MBX_NOT_FINISHED) {
17746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17747 "2010 Resume RPI Mailbox failed "
17748 "status %d, mbxStatus x%x\n", rc,
17749 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17750 mempool_free(mboxq, phba->mbox_mem_pool);
17751 return -EIO;
17752 }
17753 return 0;
17754}
17755
17756/**
17757 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17758 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17759 *
76a95d75 17760 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17761 *
17762 * Returns:
17763 * 0 success
17764 * -Evalue otherwise
17765 **/
17766int
76a95d75 17767lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17768{
17769 LPFC_MBOXQ_t *mboxq;
17770 int rc = 0;
6a9c52cf 17771 int retval = MBX_SUCCESS;
6fb120a7 17772 uint32_t mbox_tmo;
76a95d75 17773 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17774 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17775 if (!mboxq)
17776 return -ENOMEM;
76a95d75 17777 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17778 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17779 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17780 if (rc != MBX_SUCCESS) {
76a95d75 17781 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17782 "2022 INIT VPI Mailbox failed "
17783 "status %d, mbxStatus x%x\n", rc,
17784 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17785 retval = -EIO;
6fb120a7 17786 }
6a9c52cf 17787 if (rc != MBX_TIMEOUT)
76a95d75 17788 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17789
17790 return retval;
6fb120a7
JS
17791}
17792
17793/**
17794 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17795 * @phba: pointer to lpfc hba data structure.
17796 * @mboxq: Pointer to mailbox object.
17797 *
17798 * This routine is invoked to manually add a single FCF record. The caller
17799 * must pass a completely initialized FCF_Record. This routine takes
17800 * care of the nonembedded mailbox operations.
17801 **/
17802static void
17803lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17804{
17805 void *virt_addr;
17806 union lpfc_sli4_cfg_shdr *shdr;
17807 uint32_t shdr_status, shdr_add_status;
17808
17809 virt_addr = mboxq->sge_array->addr[0];
17810 /* The IOCTL status is embedded in the mailbox subheader. */
17811 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17812 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17813 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17814
17815 if ((shdr_status || shdr_add_status) &&
17816 (shdr_status != STATUS_FCF_IN_USE))
17817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17818 "2558 ADD_FCF_RECORD mailbox failed with "
17819 "status x%x add_status x%x\n",
17820 shdr_status, shdr_add_status);
17821
17822 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17823}
17824
17825/**
17826 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17827 * @phba: pointer to lpfc hba data structure.
17828 * @fcf_record: pointer to the initialized fcf record to add.
17829 *
17830 * This routine is invoked to manually add a single FCF record. The caller
17831 * must pass a completely initialized FCF_Record. This routine takes
17832 * care of the nonembedded mailbox operations.
17833 **/
17834int
17835lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17836{
17837 int rc = 0;
17838 LPFC_MBOXQ_t *mboxq;
17839 uint8_t *bytep;
17840 void *virt_addr;
6fb120a7
JS
17841 struct lpfc_mbx_sge sge;
17842 uint32_t alloc_len, req_len;
17843 uint32_t fcfindex;
17844
17845 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17846 if (!mboxq) {
17847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17848 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17849 return -ENOMEM;
17850 }
17851
17852 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17853 sizeof(uint32_t);
17854
17855 /* Allocate DMA memory and set up the non-embedded mailbox command */
17856 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17857 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17858 req_len, LPFC_SLI4_MBX_NEMBED);
17859 if (alloc_len < req_len) {
17860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17861 "2523 Allocated DMA memory size (x%x) is "
17862 "less than the requested DMA memory "
17863 "size (x%x)\n", alloc_len, req_len);
17864 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17865 return -ENOMEM;
17866 }
17867
17868 /*
17869 * Get the first SGE entry from the non-embedded DMA memory. This
17870 * routine only uses a single SGE.
17871 */
17872 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
17873 virt_addr = mboxq->sge_array->addr[0];
17874 /*
17875 * Configure the FCF record for FCFI 0. This is the driver's
17876 * hardcoded default and gets used in nonFIP mode.
17877 */
17878 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17879 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17880 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17881
17882 /*
17883 * Copy the fcf_index and the FCF Record Data. The data starts after
17884 * the FCoE header plus word10. The data copy needs to be endian
17885 * correct.
17886 */
17887 bytep += sizeof(uint32_t);
17888 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17889 mboxq->vport = phba->pport;
17890 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17891 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17892 if (rc == MBX_NOT_FINISHED) {
17893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17894 "2515 ADD_FCF_RECORD mailbox failed with "
17895 "status 0x%x\n", rc);
17896 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17897 rc = -EIO;
17898 } else
17899 rc = 0;
17900
17901 return rc;
17902}
17903
17904/**
17905 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17906 * @phba: pointer to lpfc hba data structure.
17907 * @fcf_record: pointer to the fcf record to write the default data.
17908 * @fcf_index: FCF table entry index.
17909 *
17910 * This routine is invoked to build the driver's default FCF record. The
17911 * values used are hardcoded. This routine handles memory initialization.
17912 *
17913 **/
17914void
17915lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17916 struct fcf_record *fcf_record,
17917 uint16_t fcf_index)
17918{
17919 memset(fcf_record, 0, sizeof(struct fcf_record));
17920 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17921 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17922 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17923 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17924 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17925 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17926 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17927 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17928 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17929 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17930 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17931 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17932 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 17933 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
17934 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17935 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17936 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17937 /* Set the VLAN bit map */
17938 if (phba->valid_vlan) {
17939 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17940 = 1 << (phba->vlan_id % 8);
17941 }
17942}
17943
17944/**
0c9ab6f5 17945 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
17946 * @phba: pointer to lpfc hba data structure.
17947 * @fcf_index: FCF table entry offset.
17948 *
0c9ab6f5
JS
17949 * This routine is invoked to scan the entire FCF table by reading FCF
17950 * record and processing it one at a time starting from the @fcf_index
17951 * for initial FCF discovery or fast FCF failover rediscovery.
17952 *
25985edc 17953 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 17954 * otherwise.
6fb120a7
JS
17955 **/
17956int
0c9ab6f5 17957lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
17958{
17959 int rc = 0, error;
17960 LPFC_MBOXQ_t *mboxq;
6fb120a7 17961
32b9793f 17962 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 17963 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
17964 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17965 if (!mboxq) {
17966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17967 "2000 Failed to allocate mbox for "
17968 "READ_FCF cmd\n");
4d9ab994 17969 error = -ENOMEM;
0c9ab6f5 17970 goto fail_fcf_scan;
6fb120a7 17971 }
ecfd03c6 17972 /* Construct the read FCF record mailbox command */
0c9ab6f5 17973 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
17974 if (rc) {
17975 error = -EINVAL;
0c9ab6f5 17976 goto fail_fcf_scan;
6fb120a7 17977 }
ecfd03c6 17978 /* Issue the mailbox command asynchronously */
6fb120a7 17979 mboxq->vport = phba->pport;
0c9ab6f5 17980 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
17981
17982 spin_lock_irq(&phba->hbalock);
17983 phba->hba_flag |= FCF_TS_INPROG;
17984 spin_unlock_irq(&phba->hbalock);
17985
6fb120a7 17986 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 17987 if (rc == MBX_NOT_FINISHED)
6fb120a7 17988 error = -EIO;
ecfd03c6 17989 else {
38b92ef8
JS
17990 /* Reset eligible FCF count for new scan */
17991 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 17992 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 17993 error = 0;
32b9793f 17994 }
0c9ab6f5 17995fail_fcf_scan:
4d9ab994
JS
17996 if (error) {
17997 if (mboxq)
17998 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 17999 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 18000 spin_lock_irq(&phba->hbalock);
a93ff37a 18001 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
18002 spin_unlock_irq(&phba->hbalock);
18003 }
6fb120a7
JS
18004 return error;
18005}
a0c87cbd 18006
0c9ab6f5 18007/**
a93ff37a 18008 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
18009 * @phba: pointer to lpfc hba data structure.
18010 * @fcf_index: FCF table entry offset.
18011 *
18012 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 18013 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 18014 *
25985edc 18015 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18016 * otherwise.
18017 **/
18018int
18019lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18020{
18021 int rc = 0, error;
18022 LPFC_MBOXQ_t *mboxq;
18023
18024 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18025 if (!mboxq) {
18026 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18027 "2763 Failed to allocate mbox for "
18028 "READ_FCF cmd\n");
18029 error = -ENOMEM;
18030 goto fail_fcf_read;
18031 }
18032 /* Construct the read FCF record mailbox command */
18033 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18034 if (rc) {
18035 error = -EINVAL;
18036 goto fail_fcf_read;
18037 }
18038 /* Issue the mailbox command asynchronously */
18039 mboxq->vport = phba->pport;
18040 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18041 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18042 if (rc == MBX_NOT_FINISHED)
18043 error = -EIO;
18044 else
18045 error = 0;
18046
18047fail_fcf_read:
18048 if (error && mboxq)
18049 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18050 return error;
18051}
18052
18053/**
18054 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18055 * @phba: pointer to lpfc hba data structure.
18056 * @fcf_index: FCF table entry offset.
18057 *
18058 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 18059 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 18060 *
25985edc 18061 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
18062 * otherwise.
18063 **/
18064int
18065lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18066{
18067 int rc = 0, error;
18068 LPFC_MBOXQ_t *mboxq;
18069
18070 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18071 if (!mboxq) {
18072 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18073 "2758 Failed to allocate mbox for "
18074 "READ_FCF cmd\n");
18075 error = -ENOMEM;
18076 goto fail_fcf_read;
18077 }
18078 /* Construct the read FCF record mailbox command */
18079 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18080 if (rc) {
18081 error = -EINVAL;
18082 goto fail_fcf_read;
18083 }
18084 /* Issue the mailbox command asynchronously */
18085 mboxq->vport = phba->pport;
18086 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18087 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18088 if (rc == MBX_NOT_FINISHED)
18089 error = -EIO;
18090 else
18091 error = 0;
18092
18093fail_fcf_read:
18094 if (error && mboxq)
18095 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18096 return error;
18097}
18098
7d791df7 18099/**
f5cb5304 18100 * lpfc_check_next_fcf_pri_level
7d791df7
JS
18101 * phba pointer to the lpfc_hba struct for this port.
18102 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18103 * routine when the rr_bmask is empty. The FCF indecies are put into the
18104 * rr_bmask based on their priority level. Starting from the highest priority
18105 * to the lowest. The most likely FCF candidate will be in the highest
18106 * priority group. When this routine is called it searches the fcf_pri list for
18107 * next lowest priority group and repopulates the rr_bmask with only those
18108 * fcf_indexes.
18109 * returns:
18110 * 1=success 0=failure
18111 **/
5d8b8167 18112static int
7d791df7
JS
18113lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18114{
18115 uint16_t next_fcf_pri;
18116 uint16_t last_index;
18117 struct lpfc_fcf_pri *fcf_pri;
18118 int rc;
18119 int ret = 0;
18120
18121 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18122 LPFC_SLI4_FCF_TBL_INDX_MAX);
18123 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18124 "3060 Last IDX %d\n", last_index);
2562669c
JS
18125
18126 /* Verify the priority list has 2 or more entries */
18127 spin_lock_irq(&phba->hbalock);
18128 if (list_empty(&phba->fcf.fcf_pri_list) ||
18129 list_is_singular(&phba->fcf.fcf_pri_list)) {
18130 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
18131 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18132 "3061 Last IDX %d\n", last_index);
18133 return 0; /* Empty rr list */
18134 }
2562669c
JS
18135 spin_unlock_irq(&phba->hbalock);
18136
7d791df7
JS
18137 next_fcf_pri = 0;
18138 /*
18139 * Clear the rr_bmask and set all of the bits that are at this
18140 * priority.
18141 */
18142 memset(phba->fcf.fcf_rr_bmask, 0,
18143 sizeof(*phba->fcf.fcf_rr_bmask));
18144 spin_lock_irq(&phba->hbalock);
18145 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18146 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18147 continue;
18148 /*
18149 * the 1st priority that has not FLOGI failed
18150 * will be the highest.
18151 */
18152 if (!next_fcf_pri)
18153 next_fcf_pri = fcf_pri->fcf_rec.priority;
18154 spin_unlock_irq(&phba->hbalock);
18155 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18156 rc = lpfc_sli4_fcf_rr_index_set(phba,
18157 fcf_pri->fcf_rec.fcf_index);
18158 if (rc)
18159 return 0;
18160 }
18161 spin_lock_irq(&phba->hbalock);
18162 }
18163 /*
18164 * if next_fcf_pri was not set above and the list is not empty then
18165 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18166 * and start at the beginning.
7d791df7
JS
18167 */
18168 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18169 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18170 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18171 /*
18172 * the 1st priority that has not FLOGI failed
18173 * will be the highest.
18174 */
18175 if (!next_fcf_pri)
18176 next_fcf_pri = fcf_pri->fcf_rec.priority;
18177 spin_unlock_irq(&phba->hbalock);
18178 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18179 rc = lpfc_sli4_fcf_rr_index_set(phba,
18180 fcf_pri->fcf_rec.fcf_index);
18181 if (rc)
18182 return 0;
18183 }
18184 spin_lock_irq(&phba->hbalock);
18185 }
18186 } else
18187 ret = 1;
18188 spin_unlock_irq(&phba->hbalock);
18189
18190 return ret;
18191}
0c9ab6f5
JS
18192/**
18193 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18194 * @phba: pointer to lpfc hba data structure.
18195 *
18196 * This routine is to get the next eligible FCF record index in a round
18197 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18198 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18199 * shall be returned, otherwise, the next eligible FCF record's index
18200 * shall be returned.
18201 **/
18202uint16_t
18203lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18204{
18205 uint16_t next_fcf_index;
18206
421c6622 18207initial_priority:
3804dc84 18208 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18209 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18210
7d791df7 18211next_priority:
421c6622
JS
18212 /* Determine the next fcf index to check */
18213 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18214 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18215 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18216 next_fcf_index);
18217
0c9ab6f5 18218 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18219 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18220 /*
18221 * If we have wrapped then we need to clear the bits that
18222 * have been tested so that we can detect when we should
18223 * change the priority level.
18224 */
0c9ab6f5
JS
18225 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18226 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18227 }
18228
3804dc84
JS
18229
18230 /* Check roundrobin failover list empty condition */
7d791df7
JS
18231 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18232 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18233 /*
18234 * If next fcf index is not found check if there are lower
18235 * Priority level fcf's in the fcf_priority list.
18236 * Set up the rr_bmask with all of the avaiable fcf bits
18237 * at that level and continue the selection process.
18238 */
18239 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18240 goto initial_priority;
3804dc84
JS
18241 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18242 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18243 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18244 return LPFC_FCOE_FCF_NEXT_NONE;
18245 else {
18246 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18247 "3063 Only FCF available idx %d, flag %x\n",
18248 next_fcf_index,
18249 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18250 return next_fcf_index;
18251 }
3804dc84
JS
18252 }
18253
7d791df7
JS
18254 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18255 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18256 LPFC_FCF_FLOGI_FAILED) {
18257 if (list_is_singular(&phba->fcf.fcf_pri_list))
18258 return LPFC_FCOE_FCF_NEXT_NONE;
18259
7d791df7 18260 goto next_priority;
f5cb5304 18261 }
7d791df7 18262
3804dc84 18263 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18264 "2845 Get next roundrobin failover FCF (x%x)\n",
18265 next_fcf_index);
18266
0c9ab6f5
JS
18267 return next_fcf_index;
18268}
18269
18270/**
18271 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18272 * @phba: pointer to lpfc hba data structure.
18273 *
18274 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18275 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18276 * does not go beyond the range of the driver allocated bmask dimension
18277 * before setting the bit.
18278 *
18279 * Returns 0 if the index bit successfully set, otherwise, it returns
18280 * -EINVAL.
18281 **/
18282int
18283lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18284{
18285 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18286 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18287 "2610 FCF (x%x) reached driver's book "
18288 "keeping dimension:x%x\n",
0c9ab6f5
JS
18289 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18290 return -EINVAL;
18291 }
18292 /* Set the eligible FCF record index bmask */
18293 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18294
3804dc84 18295 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18296 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18297 "bmask\n", fcf_index);
18298
0c9ab6f5
JS
18299 return 0;
18300}
18301
18302/**
3804dc84 18303 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18304 * @phba: pointer to lpfc hba data structure.
18305 *
18306 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18307 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18308 * does not go beyond the range of the driver allocated bmask dimension
18309 * before clearing the bit.
18310 **/
18311void
18312lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18313{
9a803a74 18314 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18315 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18316 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18317 "2762 FCF (x%x) reached driver's book "
18318 "keeping dimension:x%x\n",
0c9ab6f5
JS
18319 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18320 return;
18321 }
18322 /* Clear the eligible FCF record index bmask */
7d791df7 18323 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18324 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18325 list) {
7d791df7
JS
18326 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18327 list_del_init(&fcf_pri->list);
18328 break;
18329 }
18330 }
18331 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18332 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18333
18334 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18335 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18336 "bmask\n", fcf_index);
0c9ab6f5
JS
18337}
18338
ecfd03c6
JS
18339/**
18340 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18341 * @phba: pointer to lpfc hba data structure.
18342 *
18343 * This routine is the completion routine for the rediscover FCF table mailbox
18344 * command. If the mailbox command returned failure, it will try to stop the
18345 * FCF rediscover wait timer.
18346 **/
5d8b8167 18347static void
ecfd03c6
JS
18348lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18349{
18350 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18351 uint32_t shdr_status, shdr_add_status;
18352
18353 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18354
18355 shdr_status = bf_get(lpfc_mbox_hdr_status,
18356 &redisc_fcf->header.cfg_shdr.response);
18357 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18358 &redisc_fcf->header.cfg_shdr.response);
18359 if (shdr_status || shdr_add_status) {
0c9ab6f5 18360 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18361 "2746 Requesting for FCF rediscovery failed "
18362 "status x%x add_status x%x\n",
18363 shdr_status, shdr_add_status);
0c9ab6f5 18364 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18365 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18366 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18367 spin_unlock_irq(&phba->hbalock);
18368 /*
18369 * CVL event triggered FCF rediscover request failed,
18370 * last resort to re-try current registered FCF entry.
18371 */
18372 lpfc_retry_pport_discovery(phba);
18373 } else {
18374 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18375 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18376 spin_unlock_irq(&phba->hbalock);
18377 /*
18378 * DEAD FCF event triggered FCF rediscover request
18379 * failed, last resort to fail over as a link down
18380 * to FCF registration.
18381 */
18382 lpfc_sli4_fcf_dead_failthrough(phba);
18383 }
0c9ab6f5
JS
18384 } else {
18385 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18386 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18387 /*
18388 * Start FCF rediscovery wait timer for pending FCF
18389 * before rescan FCF record table.
18390 */
18391 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18392 }
ecfd03c6
JS
18393
18394 mempool_free(mbox, phba->mbox_mem_pool);
18395}
18396
18397/**
3804dc84 18398 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18399 * @phba: pointer to lpfc hba data structure.
18400 *
18401 * This routine is invoked to request for rediscovery of the entire FCF table
18402 * by the port.
18403 **/
18404int
18405lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18406{
18407 LPFC_MBOXQ_t *mbox;
18408 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18409 int rc, length;
18410
0c9ab6f5
JS
18411 /* Cancel retry delay timers to all vports before FCF rediscover */
18412 lpfc_cancel_all_vport_retry_delay_timer(phba);
18413
ecfd03c6
JS
18414 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18415 if (!mbox) {
18416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18417 "2745 Failed to allocate mbox for "
18418 "requesting FCF rediscover.\n");
18419 return -ENOMEM;
18420 }
18421
18422 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18423 sizeof(struct lpfc_sli4_cfg_mhdr));
18424 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18425 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18426 length, LPFC_SLI4_MBX_EMBED);
18427
18428 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18429 /* Set count to 0 for invalidating the entire FCF database */
18430 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18431
18432 /* Issue the mailbox command asynchronously */
18433 mbox->vport = phba->pport;
18434 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18435 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18436
18437 if (rc == MBX_NOT_FINISHED) {
18438 mempool_free(mbox, phba->mbox_mem_pool);
18439 return -EIO;
18440 }
18441 return 0;
18442}
18443
fc2b989b
JS
18444/**
18445 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18446 * @phba: pointer to lpfc hba data structure.
18447 *
18448 * This function is the failover routine as a last resort to the FCF DEAD
18449 * event when driver failed to perform fast FCF failover.
18450 **/
18451void
18452lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18453{
18454 uint32_t link_state;
18455
18456 /*
18457 * Last resort as FCF DEAD event failover will treat this as
18458 * a link down, but save the link state because we don't want
18459 * it to be changed to Link Down unless it is already down.
18460 */
18461 link_state = phba->link_state;
18462 lpfc_linkdown(phba);
18463 phba->link_state = link_state;
18464
18465 /* Unregister FCF if no devices connected to it */
18466 lpfc_unregister_unused_fcf(phba);
18467}
18468
a0c87cbd 18469/**
026abb87 18470 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18471 * @phba: pointer to lpfc hba data structure.
026abb87 18472 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18473 *
026abb87
JS
18474 * This function gets SLI3 port configure region 23 data through memory dump
18475 * mailbox command. When it successfully retrieves data, the size of the data
18476 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18477 **/
026abb87
JS
18478static uint32_t
18479lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18480{
18481 LPFC_MBOXQ_t *pmb = NULL;
18482 MAILBOX_t *mb;
026abb87 18483 uint32_t offset = 0;
a0c87cbd
JS
18484 int rc;
18485
026abb87
JS
18486 if (!rgn23_data)
18487 return 0;
18488
a0c87cbd
JS
18489 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18490 if (!pmb) {
18491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18492 "2600 failed to allocate mailbox memory\n");
18493 return 0;
a0c87cbd
JS
18494 }
18495 mb = &pmb->u.mb;
18496
a0c87cbd
JS
18497 do {
18498 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18499 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18500
18501 if (rc != MBX_SUCCESS) {
18502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18503 "2601 failed to read config "
18504 "region 23, rc 0x%x Status 0x%x\n",
18505 rc, mb->mbxStatus);
a0c87cbd
JS
18506 mb->un.varDmp.word_cnt = 0;
18507 }
18508 /*
18509 * dump mem may return a zero when finished or we got a
18510 * mailbox error, either way we are done.
18511 */
18512 if (mb->un.varDmp.word_cnt == 0)
18513 break;
18514 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18515 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18516
18517 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18518 rgn23_data + offset,
18519 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18520 offset += mb->un.varDmp.word_cnt;
18521 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18522
026abb87
JS
18523 mempool_free(pmb, phba->mbox_mem_pool);
18524 return offset;
18525}
18526
18527/**
18528 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18529 * @phba: pointer to lpfc hba data structure.
18530 * @rgn23_data: pointer to configure region 23 data.
18531 *
18532 * This function gets SLI4 port configure region 23 data through memory dump
18533 * mailbox command. When it successfully retrieves data, the size of the data
18534 * will be returned, otherwise, 0 will be returned.
18535 **/
18536static uint32_t
18537lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18538{
18539 LPFC_MBOXQ_t *mboxq = NULL;
18540 struct lpfc_dmabuf *mp = NULL;
18541 struct lpfc_mqe *mqe;
18542 uint32_t data_length = 0;
18543 int rc;
18544
18545 if (!rgn23_data)
18546 return 0;
18547
18548 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18549 if (!mboxq) {
18550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18551 "3105 failed to allocate mailbox memory\n");
18552 return 0;
18553 }
18554
18555 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18556 goto out;
18557 mqe = &mboxq->u.mqe;
18558 mp = (struct lpfc_dmabuf *) mboxq->context1;
18559 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18560 if (rc)
18561 goto out;
18562 data_length = mqe->un.mb_words[5];
18563 if (data_length == 0)
18564 goto out;
18565 if (data_length > DMP_RGN23_SIZE) {
18566 data_length = 0;
18567 goto out;
18568 }
18569 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18570out:
18571 mempool_free(mboxq, phba->mbox_mem_pool);
18572 if (mp) {
18573 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18574 kfree(mp);
18575 }
18576 return data_length;
18577}
18578
18579/**
18580 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18581 * @phba: pointer to lpfc hba data structure.
18582 *
18583 * This function read region 23 and parse TLV for port status to
18584 * decide if the user disaled the port. If the TLV indicates the
18585 * port is disabled, the hba_flag is set accordingly.
18586 **/
18587void
18588lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18589{
18590 uint8_t *rgn23_data = NULL;
18591 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18592 uint32_t offset = 0;
18593
18594 /* Get adapter Region 23 data */
18595 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18596 if (!rgn23_data)
18597 goto out;
18598
18599 if (phba->sli_rev < LPFC_SLI_REV4)
18600 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18601 else {
18602 if_type = bf_get(lpfc_sli_intf_if_type,
18603 &phba->sli4_hba.sli_intf);
18604 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18605 goto out;
18606 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18607 }
a0c87cbd
JS
18608
18609 if (!data_size)
18610 goto out;
18611
18612 /* Check the region signature first */
18613 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18615 "2619 Config region 23 has bad signature\n");
18616 goto out;
18617 }
18618 offset += 4;
18619
18620 /* Check the data structure version */
18621 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18623 "2620 Config region 23 has bad version\n");
18624 goto out;
18625 }
18626 offset += 4;
18627
18628 /* Parse TLV entries in the region */
18629 while (offset < data_size) {
18630 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18631 break;
18632 /*
18633 * If the TLV is not driver specific TLV or driver id is
18634 * not linux driver id, skip the record.
18635 */
18636 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18637 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18638 (rgn23_data[offset + 3] != 0)) {
18639 offset += rgn23_data[offset + 1] * 4 + 4;
18640 continue;
18641 }
18642
18643 /* Driver found a driver specific TLV in the config region */
18644 sub_tlv_len = rgn23_data[offset + 1] * 4;
18645 offset += 4;
18646 tlv_offset = 0;
18647
18648 /*
18649 * Search for configured port state sub-TLV.
18650 */
18651 while ((offset < data_size) &&
18652 (tlv_offset < sub_tlv_len)) {
18653 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18654 offset += 4;
18655 tlv_offset += 4;
18656 break;
18657 }
18658 if (rgn23_data[offset] != PORT_STE_TYPE) {
18659 offset += rgn23_data[offset + 1] * 4 + 4;
18660 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18661 continue;
18662 }
18663
18664 /* This HBA contains PORT_STE configured */
18665 if (!rgn23_data[offset + 2])
18666 phba->hba_flag |= LINK_DISABLED;
18667
18668 goto out;
18669 }
18670 }
026abb87 18671
a0c87cbd 18672out:
a0c87cbd
JS
18673 kfree(rgn23_data);
18674 return;
18675}
695a814e 18676
52d52440
JS
18677/**
18678 * lpfc_wr_object - write an object to the firmware
18679 * @phba: HBA structure that indicates port to create a queue on.
18680 * @dmabuf_list: list of dmabufs to write to the port.
18681 * @size: the total byte value of the objects to write to the port.
18682 * @offset: the current offset to be used to start the transfer.
18683 *
18684 * This routine will create a wr_object mailbox command to send to the port.
18685 * the mailbox command will be constructed using the dma buffers described in
18686 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18687 * BDEs that the imbedded mailbox can support. The @offset variable will be
18688 * used to indicate the starting offset of the transfer and will also return
18689 * the offset after the write object mailbox has completed. @size is used to
18690 * determine the end of the object and whether the eof bit should be set.
18691 *
18692 * Return 0 is successful and offset will contain the the new offset to use
18693 * for the next write.
18694 * Return negative value for error cases.
18695 **/
18696int
18697lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18698 uint32_t size, uint32_t *offset)
18699{
18700 struct lpfc_mbx_wr_object *wr_object;
18701 LPFC_MBOXQ_t *mbox;
18702 int rc = 0, i = 0;
18703 uint32_t shdr_status, shdr_add_status;
18704 uint32_t mbox_tmo;
18705 union lpfc_sli4_cfg_shdr *shdr;
18706 struct lpfc_dmabuf *dmabuf;
18707 uint32_t written = 0;
18708
18709 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18710 if (!mbox)
18711 return -ENOMEM;
18712
18713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18714 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18715 sizeof(struct lpfc_mbx_wr_object) -
18716 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18717
18718 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18719 wr_object->u.request.write_offset = *offset;
18720 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18721 wr_object->u.request.object_name[0] =
18722 cpu_to_le32(wr_object->u.request.object_name[0]);
18723 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18724 list_for_each_entry(dmabuf, dmabuf_list, list) {
18725 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18726 break;
18727 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18728 wr_object->u.request.bde[i].addrHigh =
18729 putPaddrHigh(dmabuf->phys);
18730 if (written + SLI4_PAGE_SIZE >= size) {
18731 wr_object->u.request.bde[i].tus.f.bdeSize =
18732 (size - written);
18733 written += (size - written);
18734 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18735 } else {
18736 wr_object->u.request.bde[i].tus.f.bdeSize =
18737 SLI4_PAGE_SIZE;
18738 written += SLI4_PAGE_SIZE;
18739 }
18740 i++;
18741 }
18742 wr_object->u.request.bde_count = i;
18743 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18744 if (!phba->sli4_hba.intr_enable)
18745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18746 else {
a183a15f 18747 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18748 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18749 }
18750 /* The IOCTL status is embedded in the mailbox subheader. */
18751 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18752 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18753 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18754 if (rc != MBX_TIMEOUT)
18755 mempool_free(mbox, phba->mbox_mem_pool);
18756 if (shdr_status || shdr_add_status || rc) {
18757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18758 "3025 Write Object mailbox failed with "
18759 "status x%x add_status x%x, mbx status x%x\n",
18760 shdr_status, shdr_add_status, rc);
18761 rc = -ENXIO;
18762 } else
18763 *offset += wr_object->u.response.actual_write_length;
18764 return rc;
18765}
18766
695a814e
JS
18767/**
18768 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18769 * @vport: pointer to vport data structure.
18770 *
18771 * This function iterate through the mailboxq and clean up all REG_LOGIN
18772 * and REG_VPI mailbox commands associated with the vport. This function
18773 * is called when driver want to restart discovery of the vport due to
18774 * a Clear Virtual Link event.
18775 **/
18776void
18777lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18778{
18779 struct lpfc_hba *phba = vport->phba;
18780 LPFC_MBOXQ_t *mb, *nextmb;
18781 struct lpfc_dmabuf *mp;
78730cfe 18782 struct lpfc_nodelist *ndlp;
d439d286 18783 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18784 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18785 LIST_HEAD(mbox_cmd_list);
63e801ce 18786 uint8_t restart_loop;
695a814e 18787
d439d286 18788 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18789 spin_lock_irq(&phba->hbalock);
18790 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18791 if (mb->vport != vport)
18792 continue;
18793
18794 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18795 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18796 continue;
18797
d439d286
JS
18798 list_del(&mb->list);
18799 list_add_tail(&mb->list, &mbox_cmd_list);
18800 }
18801 /* Clean up active mailbox command with the vport */
18802 mb = phba->sli.mbox_active;
18803 if (mb && (mb->vport == vport)) {
18804 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18805 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18806 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18807 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18808 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18809 /* Put reference count for delayed processing */
18810 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18811 /* Unregister the RPI when mailbox complete */
18812 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18813 }
18814 }
63e801ce
JS
18815 /* Cleanup any mailbox completions which are not yet processed */
18816 do {
18817 restart_loop = 0;
18818 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18819 /*
18820 * If this mailox is already processed or it is
18821 * for another vport ignore it.
18822 */
18823 if ((mb->vport != vport) ||
18824 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18825 continue;
18826
18827 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18828 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18829 continue;
18830
18831 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18832 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18833 ndlp = (struct lpfc_nodelist *)mb->context2;
18834 /* Unregister the RPI when mailbox complete */
18835 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18836 restart_loop = 1;
18837 spin_unlock_irq(&phba->hbalock);
18838 spin_lock(shost->host_lock);
18839 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18840 spin_unlock(shost->host_lock);
18841 spin_lock_irq(&phba->hbalock);
18842 break;
18843 }
18844 }
18845 } while (restart_loop);
18846
d439d286
JS
18847 spin_unlock_irq(&phba->hbalock);
18848
18849 /* Release the cleaned-up mailbox commands */
18850 while (!list_empty(&mbox_cmd_list)) {
18851 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18852 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18853 mp = (struct lpfc_dmabuf *) (mb->context1);
18854 if (mp) {
18855 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18856 kfree(mp);
18857 }
78730cfe 18858 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18859 mb->context2 = NULL;
78730cfe 18860 if (ndlp) {
ec21b3b0 18861 spin_lock(shost->host_lock);
589a52d6 18862 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18863 spin_unlock(shost->host_lock);
78730cfe 18864 lpfc_nlp_put(ndlp);
78730cfe 18865 }
695a814e 18866 }
695a814e
JS
18867 mempool_free(mb, phba->mbox_mem_pool);
18868 }
d439d286
JS
18869
18870 /* Release the ndlp with the cleaned-up active mailbox command */
18871 if (act_mbx_ndlp) {
18872 spin_lock(shost->host_lock);
18873 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18874 spin_unlock(shost->host_lock);
18875 lpfc_nlp_put(act_mbx_ndlp);
695a814e 18876 }
695a814e
JS
18877}
18878
2a9bf3d0
JS
18879/**
18880 * lpfc_drain_txq - Drain the txq
18881 * @phba: Pointer to HBA context object.
18882 *
18883 * This function attempt to submit IOCBs on the txq
18884 * to the adapter. For SLI4 adapters, the txq contains
18885 * ELS IOCBs that have been deferred because the there
18886 * are no SGLs. This congestion can occur with large
18887 * vport counts during node discovery.
18888 **/
18889
18890uint32_t
18891lpfc_drain_txq(struct lpfc_hba *phba)
18892{
18893 LIST_HEAD(completions);
895427bd 18894 struct lpfc_sli_ring *pring;
2e706377 18895 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
18896 unsigned long iflags = 0;
18897 char *fail_msg = NULL;
18898 struct lpfc_sglq *sglq;
2f07784f
JS
18899 union lpfc_wqe128 wqe128;
18900 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 18901 uint32_t txq_cnt = 0;
2a9bf3d0 18902
895427bd 18903 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
18904 if (unlikely(!pring))
18905 return 0;
895427bd 18906
398d81c9 18907 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
18908 list_for_each_entry(piocbq, &pring->txq, list) {
18909 txq_cnt++;
18910 }
18911
18912 if (txq_cnt > pring->txq_max)
18913 pring->txq_max = txq_cnt;
2a9bf3d0 18914
398d81c9 18915 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18916
0e9bb8d7 18917 while (!list_empty(&pring->txq)) {
398d81c9 18918 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 18919
19ca7609 18920 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 18921 if (!piocbq) {
398d81c9 18922 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
18923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18924 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 18925 txq_cnt);
a629852a
JS
18926 break;
18927 }
895427bd 18928 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 18929 if (!sglq) {
19ca7609 18930 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 18931 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18932 break;
2a9bf3d0 18933 }
0e9bb8d7 18934 txq_cnt--;
2a9bf3d0
JS
18935
18936 /* The xri and iocb resources secured,
18937 * attempt to issue request
18938 */
6d368e53 18939 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
18940 piocbq->sli4_xritag = sglq->sli4_xritag;
18941 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18942 fail_msg = "to convert bpl to sgl";
2f07784f 18943 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 18944 fail_msg = "to convert iocb to wqe";
2f07784f 18945 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
18946 fail_msg = " - Wq is full";
18947 else
18948 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18949
18950 if (fail_msg) {
18951 /* Failed means we can't issue and need to cancel */
18952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18953 "2822 IOCB failed %s iotag 0x%x "
18954 "xri 0x%x\n",
18955 fail_msg,
18956 piocbq->iotag, piocbq->sli4_xritag);
18957 list_add_tail(&piocbq->list, &completions);
18958 }
398d81c9 18959 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
18960 }
18961
2a9bf3d0
JS
18962 /* Cancel all the IOCBs that cannot be issued */
18963 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18964 IOERR_SLI_ABORTED);
18965
0e9bb8d7 18966 return txq_cnt;
2a9bf3d0 18967}
895427bd
JS
18968
18969/**
18970 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18971 * @phba: Pointer to HBA context object.
18972 * @pwqe: Pointer to command WQE.
18973 * @sglq: Pointer to the scatter gather queue object.
18974 *
18975 * This routine converts the bpl or bde that is in the WQE
18976 * to a sgl list for the sli4 hardware. The physical address
18977 * of the bpl/bde is converted back to a virtual address.
18978 * If the WQE contains a BPL then the list of BDE's is
18979 * converted to sli4_sge's. If the WQE contains a single
18980 * BDE then it is converted to a single sli_sge.
18981 * The WQE is still in cpu endianness so the contents of
18982 * the bpl can be used without byte swapping.
18983 *
18984 * Returns valid XRI = Success, NO_XRI = Failure.
18985 */
18986static uint16_t
18987lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18988 struct lpfc_sglq *sglq)
18989{
18990 uint16_t xritag = NO_XRI;
18991 struct ulp_bde64 *bpl = NULL;
18992 struct ulp_bde64 bde;
18993 struct sli4_sge *sgl = NULL;
18994 struct lpfc_dmabuf *dmabuf;
18995 union lpfc_wqe *wqe;
18996 int numBdes = 0;
18997 int i = 0;
18998 uint32_t offset = 0; /* accumulated offset in the sg request list */
18999 int inbound = 0; /* number of sg reply entries inbound from firmware */
19000 uint32_t cmd;
19001
19002 if (!pwqeq || !sglq)
19003 return xritag;
19004
19005 sgl = (struct sli4_sge *)sglq->sgl;
19006 wqe = &pwqeq->wqe;
19007 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19008
19009 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19010 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19011 return sglq->sli4_xritag;
19012 numBdes = pwqeq->rsvd2;
19013 if (numBdes) {
19014 /* The addrHigh and addrLow fields within the WQE
19015 * have not been byteswapped yet so there is no
19016 * need to swap them back.
19017 */
19018 if (pwqeq->context3)
19019 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19020 else
19021 return xritag;
19022
19023 bpl = (struct ulp_bde64 *)dmabuf->virt;
19024 if (!bpl)
19025 return xritag;
19026
19027 for (i = 0; i < numBdes; i++) {
19028 /* Should already be byte swapped. */
19029 sgl->addr_hi = bpl->addrHigh;
19030 sgl->addr_lo = bpl->addrLow;
19031
19032 sgl->word2 = le32_to_cpu(sgl->word2);
19033 if ((i+1) == numBdes)
19034 bf_set(lpfc_sli4_sge_last, sgl, 1);
19035 else
19036 bf_set(lpfc_sli4_sge_last, sgl, 0);
19037 /* swap the size field back to the cpu so we
19038 * can assign it to the sgl.
19039 */
19040 bde.tus.w = le32_to_cpu(bpl->tus.w);
19041 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19042 /* The offsets in the sgl need to be accumulated
19043 * separately for the request and reply lists.
19044 * The request is always first, the reply follows.
19045 */
19046 switch (cmd) {
19047 case CMD_GEN_REQUEST64_WQE:
19048 /* add up the reply sg entries */
19049 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19050 inbound++;
19051 /* first inbound? reset the offset */
19052 if (inbound == 1)
19053 offset = 0;
19054 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19055 bf_set(lpfc_sli4_sge_type, sgl,
19056 LPFC_SGE_TYPE_DATA);
19057 offset += bde.tus.f.bdeSize;
19058 break;
19059 case CMD_FCP_TRSP64_WQE:
19060 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19061 bf_set(lpfc_sli4_sge_type, sgl,
19062 LPFC_SGE_TYPE_DATA);
19063 break;
19064 case CMD_FCP_TSEND64_WQE:
19065 case CMD_FCP_TRECEIVE64_WQE:
19066 bf_set(lpfc_sli4_sge_type, sgl,
19067 bpl->tus.f.bdeFlags);
19068 if (i < 3)
19069 offset = 0;
19070 else
19071 offset += bde.tus.f.bdeSize;
19072 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19073 break;
19074 }
19075 sgl->word2 = cpu_to_le32(sgl->word2);
19076 bpl++;
19077 sgl++;
19078 }
19079 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19080 /* The addrHigh and addrLow fields of the BDE have not
19081 * been byteswapped yet so they need to be swapped
19082 * before putting them in the sgl.
19083 */
19084 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19085 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19086 sgl->word2 = le32_to_cpu(sgl->word2);
19087 bf_set(lpfc_sli4_sge_last, sgl, 1);
19088 sgl->word2 = cpu_to_le32(sgl->word2);
19089 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19090 }
19091 return sglq->sli4_xritag;
19092}
19093
19094/**
19095 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19096 * @phba: Pointer to HBA context object.
19097 * @ring_number: Base sli ring number
19098 * @pwqe: Pointer to command WQE.
19099 **/
19100int
19101lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19102 struct lpfc_iocbq *pwqe)
19103{
19104 union lpfc_wqe *wqe = &pwqe->wqe;
f358dd0c 19105 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
19106 struct lpfc_queue *wq;
19107 struct lpfc_sglq *sglq;
19108 struct lpfc_sli_ring *pring;
19109 unsigned long iflags;
cd22d605 19110 uint32_t ret = 0;
895427bd
JS
19111
19112 /* NVME_LS and NVME_LS ABTS requests. */
19113 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19114 pring = phba->sli4_hba.nvmels_wq->pring;
19115 spin_lock_irqsave(&pring->ring_lock, iflags);
19116 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19117 if (!sglq) {
19118 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19119 return WQE_BUSY;
19120 }
19121 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19122 pwqe->sli4_xritag = sglq->sli4_xritag;
19123 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19124 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19125 return WQE_ERROR;
19126 }
19127 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19128 pwqe->sli4_xritag);
cd22d605
DK
19129 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19130 if (ret) {
895427bd 19131 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19132 return ret;
895427bd 19133 }
cd22d605 19134
895427bd
JS
19135 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19136 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19137 return 0;
19138 }
19139
19140 /* NVME_FCREQ and NVME_ABTS requests */
19141 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19142 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19143 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19144
19145 spin_lock_irqsave(&pring->ring_lock, iflags);
19146 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19147 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19148 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19149 ret = lpfc_sli4_wq_put(wq, wqe);
19150 if (ret) {
895427bd 19151 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19152 return ret;
895427bd
JS
19153 }
19154 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19155 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19156 return 0;
19157 }
19158
f358dd0c
JS
19159 /* NVMET requests */
19160 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19161 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19162 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19163
19164 spin_lock_irqsave(&pring->ring_lock, iflags);
19165 ctxp = pwqe->context2;
6c621a22 19166 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19167 if (pwqe->sli4_xritag == NO_XRI) {
19168 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19169 pwqe->sli4_xritag = sglq->sli4_xritag;
19170 }
19171 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19172 pwqe->sli4_xritag);
19173 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19174 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19175 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19176 ret = lpfc_sli4_wq_put(wq, wqe);
19177 if (ret) {
f358dd0c 19178 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19179 return ret;
f358dd0c
JS
19180 }
19181 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19182 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19183 return 0;
19184 }
895427bd
JS
19185 return WQE_ERROR;
19186}