]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: Fix random heartbeat timeouts during heavy IO
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
43140ca6 1
dea3101e
JB
2/*******************************************************************
3 * This file is part of the Emulex Linux Device Driver for *
c44ce173 4 * Fibre Channel Host Bus Adapters. *
d080abe0
JS
5 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
6 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
50611577 7 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 8 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 9 * www.broadcom.com *
c44ce173 10 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
11 * *
12 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
13 * modify it under the terms of version 2 of the GNU General *
14 * Public License as published by the Free Software Foundation. *
15 * This program is distributed in the hope that it will be useful. *
16 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
17 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
18 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
19 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20 * TO BE LEGALLY INVALID. See the GNU General Public License for *
21 * more details, a copy of which can be found in the file COPYING *
22 * included with this package. *
dea3101e
JB
23 *******************************************************************/
24
dea3101e
JB
25#include <linux/blkdev.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
5a0e3ad6 29#include <linux/slab.h>
1c2ba475 30#include <linux/lockdep.h>
dea3101e 31
91886523 32#include <scsi/scsi.h>
dea3101e
JB
33#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
f888ba3c 36#include <scsi/scsi_transport_fc.h>
da0436e9 37#include <scsi/fc/fc_fs.h>
0d878419 38#include <linux/aer.h>
dea3101e 39
895427bd
JS
40#include <linux/nvme-fc-driver.h>
41
da0436e9 42#include "lpfc_hw4.h"
dea3101e
JB
43#include "lpfc_hw.h"
44#include "lpfc_sli.h"
da0436e9 45#include "lpfc_sli4.h"
ea2151b4 46#include "lpfc_nl.h"
dea3101e 47#include "lpfc_disc.h"
dea3101e 48#include "lpfc.h"
895427bd
JS
49#include "lpfc_scsi.h"
50#include "lpfc_nvme.h"
f358dd0c 51#include "lpfc_nvmet.h"
dea3101e
JB
52#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
858c9f6c 55#include "lpfc_debugfs.h"
04c68496 56#include "lpfc_vport.h"
61bda8f7 57#include "lpfc_version.h"
dea3101e
JB
58
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
4f774513
JS
67
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
72 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
6669f9bb
JS
75static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
ae9e28f3
JS
77static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
895427bd 79static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
0558056c 80 struct lpfc_cqe *);
895427bd 81static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
8a9d2e80 82 int);
f485c18d
DK
83static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
84 struct lpfc_eqe *eqe, uint32_t qidx);
e8d3c3b1
JS
85static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
86static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
895427bd
JS
87static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
88 struct lpfc_sli_ring *pring,
89 struct lpfc_iocbq *cmdiocb);
0558056c 90
4f774513
JS
91static IOCB_t *
92lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
93{
94 return &iocbq->iocb;
95}
96
97/**
98 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
99 * @q: The Work Queue to operate on.
100 * @wqe: The work Queue Entry to put on the Work queue.
101 *
102 * This routine will copy the contents of @wqe to the next available entry on
103 * the @q. This function will then ring the Work Queue Doorbell to signal the
104 * HBA to start processing the Work Queue Entry. This function returns 0 if
105 * successful. If no entries are available on @q then this function will return
106 * -ENOMEM.
107 * The caller is expected to hold the hbalock when calling this routine.
108 **/
cd22d605 109static int
4f774513
JS
110lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
111{
2e90f4b5 112 union lpfc_wqe *temp_wqe;
4f774513
JS
113 struct lpfc_register doorbell;
114 uint32_t host_index;
027140ea 115 uint32_t idx;
4f774513 116
2e90f4b5
JS
117 /* sanity check on queue memory */
118 if (unlikely(!q))
119 return -ENOMEM;
120 temp_wqe = q->qe[q->host_index].wqe;
121
4f774513 122 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
123 idx = ((q->host_index + 1) % q->entry_count);
124 if (idx == q->hba_index) {
b84daac9 125 q->WQ_overflow++;
cd22d605 126 return -EBUSY;
b84daac9
JS
127 }
128 q->WQ_posted++;
4f774513 129 /* set consumption flag every once in a while */
ff78d8f9 130 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 131 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
132 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
133 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513 134 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
6b3b3bdb
JS
135 /* ensure WQE bcopy flushed before doorbell write */
136 wmb();
4f774513
JS
137
138 /* Update the host index before invoking device */
139 host_index = q->host_index;
027140ea
JS
140
141 q->host_index = idx;
4f774513
JS
142
143 /* Ring Doorbell */
144 doorbell.word0 = 0;
962bc51b
JS
145 if (q->db_format == LPFC_DB_LIST_FORMAT) {
146 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
147 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
148 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
149 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
150 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
151 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
152 } else {
153 return -EINVAL;
154 }
155 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
156
157 return 0;
158}
159
160/**
161 * lpfc_sli4_wq_release - Updates internal hba index for WQ
162 * @q: The Work Queue to operate on.
163 * @index: The index to advance the hba index to.
164 *
165 * This routine will update the HBA index of a queue to reflect consumption of
166 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
167 * an entry the host calls this function to update the queue's internal
168 * pointers. This routine returns the number of entries that were consumed by
169 * the HBA.
170 **/
171static uint32_t
172lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
173{
174 uint32_t released = 0;
175
2e90f4b5
JS
176 /* sanity check on queue memory */
177 if (unlikely(!q))
178 return 0;
179
4f774513
JS
180 if (q->hba_index == index)
181 return 0;
182 do {
183 q->hba_index = ((q->hba_index + 1) % q->entry_count);
184 released++;
185 } while (q->hba_index != index);
186 return released;
187}
188
189/**
190 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
191 * @q: The Mailbox Queue to operate on.
192 * @wqe: The Mailbox Queue Entry to put on the Work queue.
193 *
194 * This routine will copy the contents of @mqe to the next available entry on
195 * the @q. This function will then ring the Work Queue Doorbell to signal the
196 * HBA to start processing the Work Queue Entry. This function returns 0 if
197 * successful. If no entries are available on @q then this function will return
198 * -ENOMEM.
199 * The caller is expected to hold the hbalock when calling this routine.
200 **/
201static uint32_t
202lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
203{
2e90f4b5 204 struct lpfc_mqe *temp_mqe;
4f774513 205 struct lpfc_register doorbell;
4f774513 206
2e90f4b5
JS
207 /* sanity check on queue memory */
208 if (unlikely(!q))
209 return -ENOMEM;
210 temp_mqe = q->qe[q->host_index].mqe;
211
4f774513
JS
212 /* If the host has not yet processed the next entry then we are done */
213 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
214 return -ENOMEM;
215 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
216 /* Save off the mailbox pointer for completion */
217 q->phba->mbox = (MAILBOX_t *)temp_mqe;
218
219 /* Update the host index before invoking device */
4f774513
JS
220 q->host_index = ((q->host_index + 1) % q->entry_count);
221
222 /* Ring Doorbell */
223 doorbell.word0 = 0;
224 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
225 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
226 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
227 return 0;
228}
229
230/**
231 * lpfc_sli4_mq_release - Updates internal hba index for MQ
232 * @q: The Mailbox Queue to operate on.
233 *
234 * This routine will update the HBA index of a queue to reflect consumption of
235 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
236 * an entry the host calls this function to update the queue's internal
237 * pointers. This routine returns the number of entries that were consumed by
238 * the HBA.
239 **/
240static uint32_t
241lpfc_sli4_mq_release(struct lpfc_queue *q)
242{
2e90f4b5
JS
243 /* sanity check on queue memory */
244 if (unlikely(!q))
245 return 0;
246
4f774513
JS
247 /* Clear the mailbox pointer for completion */
248 q->phba->mbox = NULL;
249 q->hba_index = ((q->hba_index + 1) % q->entry_count);
250 return 1;
251}
252
253/**
254 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
255 * @q: The Event Queue to get the first valid EQE from
256 *
257 * This routine will get the first valid Event Queue Entry from @q, update
258 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
259 * the Queue (no more work to do), or the Queue is full of EQEs that have been
260 * processed, but not popped back to the HBA then this routine will return NULL.
261 **/
262static struct lpfc_eqe *
263lpfc_sli4_eq_get(struct lpfc_queue *q)
264{
2e90f4b5 265 struct lpfc_eqe *eqe;
027140ea 266 uint32_t idx;
2e90f4b5
JS
267
268 /* sanity check on queue memory */
269 if (unlikely(!q))
270 return NULL;
271 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
272
273 /* If the next EQE is not valid then we are done */
cb5172ea 274 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
275 return NULL;
276 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
277 idx = ((q->hba_index + 1) % q->entry_count);
278 if (idx == q->host_index)
4f774513
JS
279 return NULL;
280
027140ea 281 q->hba_index = idx;
27f344eb
JS
282
283 /*
284 * insert barrier for instruction interlock : data from the hardware
285 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
286 * upon. Speculative instructions were allowing a bcopy at the start
287 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
288 * after our return, to copy data before the valid bit check above
289 * was done. As such, some of the copied data was stale. The barrier
290 * ensures the check is before any data is copied.
27f344eb
JS
291 */
292 mb();
4f774513
JS
293 return eqe;
294}
295
ba20c853
JS
296/**
297 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
298 * @q: The Event Queue to disable interrupts
299 *
300 **/
301static inline void
302lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
303{
304 struct lpfc_register doorbell;
305
306 doorbell.word0 = 0;
307 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
308 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
309 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
310 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
311 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
312 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
313}
314
4f774513
JS
315/**
316 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
317 * @q: The Event Queue that the host has completed processing for.
318 * @arm: Indicates whether the host wants to arms this CQ.
319 *
320 * This routine will mark all Event Queue Entries on @q, from the last
321 * known completed entry to the last entry that was processed, as completed
322 * by clearing the valid bit for each completion queue entry. Then it will
323 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
324 * The internal host index in the @q will be updated by this routine to indicate
325 * that the host has finished processing the entries. The @arm parameter
326 * indicates that the queue should be rearmed when ringing the doorbell.
327 *
328 * This function will return the number of EQEs that were popped.
329 **/
330uint32_t
331lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
332{
333 uint32_t released = 0;
334 struct lpfc_eqe *temp_eqe;
335 struct lpfc_register doorbell;
336
2e90f4b5
JS
337 /* sanity check on queue memory */
338 if (unlikely(!q))
339 return 0;
340
4f774513
JS
341 /* while there are valid entries */
342 while (q->hba_index != q->host_index) {
343 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 344 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
345 released++;
346 q->host_index = ((q->host_index + 1) % q->entry_count);
347 }
348 if (unlikely(released == 0 && !arm))
349 return 0;
350
351 /* ring doorbell for number popped */
352 doorbell.word0 = 0;
353 if (arm) {
354 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
355 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
356 }
357 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
358 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
359 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
360 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
361 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 362 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
363 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
364 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
365 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
366 return released;
367}
368
369/**
370 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
371 * @q: The Completion Queue to get the first valid CQE from
372 *
373 * This routine will get the first valid Completion Queue Entry from @q, update
374 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
375 * the Queue (no more work to do), or the Queue is full of CQEs that have been
376 * processed, but not popped back to the HBA then this routine will return NULL.
377 **/
378static struct lpfc_cqe *
379lpfc_sli4_cq_get(struct lpfc_queue *q)
380{
381 struct lpfc_cqe *cqe;
027140ea 382 uint32_t idx;
4f774513 383
2e90f4b5
JS
384 /* sanity check on queue memory */
385 if (unlikely(!q))
386 return NULL;
387
4f774513 388 /* If the next CQE is not valid then we are done */
cb5172ea 389 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
390 return NULL;
391 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
392 idx = ((q->hba_index + 1) % q->entry_count);
393 if (idx == q->host_index)
4f774513
JS
394 return NULL;
395
396 cqe = q->qe[q->hba_index].cqe;
027140ea 397 q->hba_index = idx;
27f344eb
JS
398
399 /*
400 * insert barrier for instruction interlock : data from the hardware
401 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
402 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
403 * instructions allowing action on content before valid bit checked,
404 * add barrier here as well. May not be needed as "content" is a
405 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
406 */
407 mb();
4f774513
JS
408 return cqe;
409}
410
411/**
412 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
413 * @q: The Completion Queue that the host has completed processing for.
414 * @arm: Indicates whether the host wants to arms this CQ.
415 *
416 * This routine will mark all Completion queue entries on @q, from the last
417 * known completed entry to the last entry that was processed, as completed
418 * by clearing the valid bit for each completion queue entry. Then it will
419 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
420 * The internal host index in the @q will be updated by this routine to indicate
421 * that the host has finished processing the entries. The @arm parameter
422 * indicates that the queue should be rearmed when ringing the doorbell.
423 *
424 * This function will return the number of CQEs that were released.
425 **/
426uint32_t
427lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
428{
429 uint32_t released = 0;
430 struct lpfc_cqe *temp_qe;
431 struct lpfc_register doorbell;
432
2e90f4b5
JS
433 /* sanity check on queue memory */
434 if (unlikely(!q))
435 return 0;
4f774513
JS
436 /* while there are valid entries */
437 while (q->hba_index != q->host_index) {
438 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 439 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
440 released++;
441 q->host_index = ((q->host_index + 1) % q->entry_count);
442 }
443 if (unlikely(released == 0 && !arm))
444 return 0;
445
446 /* ring doorbell for number popped */
447 doorbell.word0 = 0;
448 if (arm)
449 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
450 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
451 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
452 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
453 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
454 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
455 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
456 return released;
457}
458
459/**
460 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
461 * @q: The Header Receive Queue to operate on.
462 * @wqe: The Receive Queue Entry to put on the Receive queue.
463 *
464 * This routine will copy the contents of @wqe to the next available entry on
465 * the @q. This function will then ring the Receive Queue Doorbell to signal the
466 * HBA to start processing the Receive Queue Entry. This function returns the
467 * index that the rqe was copied to if successful. If no entries are available
468 * on @q then this function will return -ENOMEM.
469 * The caller is expected to hold the hbalock when calling this routine.
470 **/
895427bd 471int
4f774513
JS
472lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
473 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
474{
2e90f4b5
JS
475 struct lpfc_rqe *temp_hrqe;
476 struct lpfc_rqe *temp_drqe;
4f774513 477 struct lpfc_register doorbell;
5a25bf36 478 int put_index;
4f774513 479
2e90f4b5
JS
480 /* sanity check on queue memory */
481 if (unlikely(!hq) || unlikely(!dq))
482 return -ENOMEM;
5a25bf36 483 put_index = hq->host_index;
61f3d4bf 484 temp_hrqe = hq->qe[put_index].rqe;
2e90f4b5
JS
485 temp_drqe = dq->qe[dq->host_index].rqe;
486
4f774513
JS
487 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
488 return -EINVAL;
61f3d4bf 489 if (put_index != dq->host_index)
4f774513
JS
490 return -EINVAL;
491 /* If the host has not yet processed the next entry then we are done */
61f3d4bf 492 if (((put_index + 1) % hq->entry_count) == hq->hba_index)
4f774513
JS
493 return -EBUSY;
494 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
495 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
496
497 /* Update the host index to point to the next slot */
61f3d4bf 498 hq->host_index = ((put_index + 1) % hq->entry_count);
4f774513 499 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
61f3d4bf 500 hq->RQ_buf_posted++;
4f774513
JS
501
502 /* Ring The Header Receive Queue Doorbell */
73d91e50 503 if (!(hq->host_index % hq->entry_repost)) {
4f774513 504 doorbell.word0 = 0;
962bc51b
JS
505 if (hq->db_format == LPFC_DB_RING_FORMAT) {
506 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
507 hq->entry_repost);
508 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
509 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
510 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
511 hq->entry_repost);
512 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
513 hq->host_index);
514 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
515 } else {
516 return -EINVAL;
517 }
518 writel(doorbell.word0, hq->db_regaddr);
4f774513
JS
519 }
520 return put_index;
521}
522
523/**
524 * lpfc_sli4_rq_release - Updates internal hba index for RQ
525 * @q: The Header Receive Queue to operate on.
526 *
527 * This routine will update the HBA index of a queue to reflect consumption of
528 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
529 * consumed an entry the host calls this function to update the queue's
530 * internal pointers. This routine returns the number of entries that were
531 * consumed by the HBA.
532 **/
533static uint32_t
534lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
535{
2e90f4b5
JS
536 /* sanity check on queue memory */
537 if (unlikely(!hq) || unlikely(!dq))
538 return 0;
539
4f774513
JS
540 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
541 return 0;
542 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
543 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
544 return 1;
545}
546
e59058c4 547/**
3621a710 548 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
549 * @phba: Pointer to HBA context object.
550 * @pring: Pointer to driver SLI ring object.
551 *
552 * This function returns pointer to next command iocb entry
553 * in the command ring. The caller must hold hbalock to prevent
554 * other threads consume the next command iocb.
555 * SLI-2/SLI-3 provide different sized iocbs.
556 **/
ed957684
JS
557static inline IOCB_t *
558lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
559{
7e56aa25
JS
560 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
561 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
562}
563
e59058c4 564/**
3621a710 565 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
566 * @phba: Pointer to HBA context object.
567 * @pring: Pointer to driver SLI ring object.
568 *
569 * This function returns pointer to next response iocb entry
570 * in the response ring. The caller must hold hbalock to make sure
571 * that no other thread consume the next response iocb.
572 * SLI-2/SLI-3 provide different sized iocbs.
573 **/
ed957684
JS
574static inline IOCB_t *
575lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
576{
7e56aa25
JS
577 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
578 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
579}
580
e59058c4 581/**
3621a710 582 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
583 * @phba: Pointer to HBA context object.
584 *
585 * This function is called with hbalock held. This function
586 * allocates a new driver iocb object from the iocb pool. If the
587 * allocation is successful, it returns pointer to the newly
588 * allocated iocb object else it returns NULL.
589 **/
4f2e66c6 590struct lpfc_iocbq *
2e0fef85 591__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
592{
593 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
594 struct lpfc_iocbq * iocbq = NULL;
595
1c2ba475
JT
596 lockdep_assert_held(&phba->hbalock);
597
0bd4ca25 598 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
599 if (iocbq)
600 phba->iocb_cnt++;
601 if (phba->iocb_cnt > phba->iocb_max)
602 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
603 return iocbq;
604}
605
da0436e9
JS
606/**
607 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
608 * @phba: Pointer to HBA context object.
609 * @xritag: XRI value.
610 *
611 * This function clears the sglq pointer from the array of acive
612 * sglq's. The xritag that is passed in is used to index into the
613 * array. Before the xritag can be used it needs to be adjusted
614 * by subtracting the xribase.
615 *
616 * Returns sglq ponter = success, NULL = Failure.
617 **/
895427bd 618struct lpfc_sglq *
da0436e9
JS
619__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
620{
da0436e9 621 struct lpfc_sglq *sglq;
6d368e53
JS
622
623 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
624 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
625 return sglq;
626}
627
628/**
629 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
630 * @phba: Pointer to HBA context object.
631 * @xritag: XRI value.
632 *
633 * This function returns the sglq pointer from the array of acive
634 * sglq's. The xritag that is passed in is used to index into the
635 * array. Before the xritag can be used it needs to be adjusted
636 * by subtracting the xribase.
637 *
638 * Returns sglq ponter = success, NULL = Failure.
639 **/
0f65ff68 640struct lpfc_sglq *
da0436e9
JS
641__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
642{
da0436e9 643 struct lpfc_sglq *sglq;
6d368e53
JS
644
645 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
646 return sglq;
647}
648
19ca7609 649/**
1151e3ec 650 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
651 * @phba: Pointer to HBA context object.
652 * @xritag: xri used in this exchange.
653 * @rrq: The RRQ to be cleared.
654 *
19ca7609 655 **/
1151e3ec
JS
656void
657lpfc_clr_rrq_active(struct lpfc_hba *phba,
658 uint16_t xritag,
659 struct lpfc_node_rrq *rrq)
19ca7609 660{
1151e3ec 661 struct lpfc_nodelist *ndlp = NULL;
19ca7609 662
1151e3ec
JS
663 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
664 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
665
666 /* The target DID could have been swapped (cable swap)
667 * we should use the ndlp from the findnode if it is
668 * available.
669 */
1151e3ec 670 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
671 ndlp = rrq->ndlp;
672
1151e3ec
JS
673 if (!ndlp)
674 goto out;
675
cff261f6 676 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
677 rrq->send_rrq = 0;
678 rrq->xritag = 0;
679 rrq->rrq_stop_time = 0;
680 }
1151e3ec 681out:
19ca7609
JS
682 mempool_free(rrq, phba->rrq_pool);
683}
684
685/**
686 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
687 * @phba: Pointer to HBA context object.
688 *
689 * This function is called with hbalock held. This function
690 * Checks if stop_time (ratov from setting rrq active) has
691 * been reached, if it has and the send_rrq flag is set then
692 * it will call lpfc_send_rrq. If the send_rrq flag is not set
693 * then it will just call the routine to clear the rrq and
694 * free the rrq resource.
695 * The timer is set to the next rrq that is going to expire before
696 * leaving the routine.
697 *
698 **/
699void
700lpfc_handle_rrq_active(struct lpfc_hba *phba)
701{
702 struct lpfc_node_rrq *rrq;
703 struct lpfc_node_rrq *nextrrq;
704 unsigned long next_time;
705 unsigned long iflags;
1151e3ec 706 LIST_HEAD(send_rrq);
19ca7609
JS
707
708 spin_lock_irqsave(&phba->hbalock, iflags);
709 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 710 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 711 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
712 &phba->active_rrq_list, list) {
713 if (time_after(jiffies, rrq->rrq_stop_time))
714 list_move(&rrq->list, &send_rrq);
715 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
716 next_time = rrq->rrq_stop_time;
717 }
718 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
719 if ((!list_empty(&phba->active_rrq_list)) &&
720 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 721 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
722 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
723 list_del(&rrq->list);
724 if (!rrq->send_rrq)
725 /* this call will free the rrq */
726 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
727 else if (lpfc_send_rrq(phba, rrq)) {
728 /* if we send the rrq then the completion handler
729 * will clear the bit in the xribitmap.
730 */
731 lpfc_clr_rrq_active(phba, rrq->xritag,
732 rrq);
733 }
734 }
19ca7609
JS
735}
736
737/**
738 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
739 * @vport: Pointer to vport context object.
740 * @xri: The xri used in the exchange.
741 * @did: The targets DID for this exchange.
742 *
743 * returns NULL = rrq not found in the phba->active_rrq_list.
744 * rrq = rrq for this xri and target.
745 **/
746struct lpfc_node_rrq *
747lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
748{
749 struct lpfc_hba *phba = vport->phba;
750 struct lpfc_node_rrq *rrq;
751 struct lpfc_node_rrq *nextrrq;
752 unsigned long iflags;
753
754 if (phba->sli_rev != LPFC_SLI_REV4)
755 return NULL;
756 spin_lock_irqsave(&phba->hbalock, iflags);
757 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
758 if (rrq->vport == vport && rrq->xritag == xri &&
759 rrq->nlp_DID == did){
760 list_del(&rrq->list);
761 spin_unlock_irqrestore(&phba->hbalock, iflags);
762 return rrq;
763 }
764 }
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766 return NULL;
767}
768
769/**
770 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
771 * @vport: Pointer to vport context object.
1151e3ec
JS
772 * @ndlp: Pointer to the lpfc_node_list structure.
773 * If ndlp is NULL Remove all active RRQs for this vport from the
774 * phba->active_rrq_list and clear the rrq.
775 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
776 **/
777void
1151e3ec 778lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
779
780{
781 struct lpfc_hba *phba = vport->phba;
782 struct lpfc_node_rrq *rrq;
783 struct lpfc_node_rrq *nextrrq;
784 unsigned long iflags;
1151e3ec 785 LIST_HEAD(rrq_list);
19ca7609
JS
786
787 if (phba->sli_rev != LPFC_SLI_REV4)
788 return;
1151e3ec
JS
789 if (!ndlp) {
790 lpfc_sli4_vport_delete_els_xri_aborted(vport);
791 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 792 }
1151e3ec
JS
793 spin_lock_irqsave(&phba->hbalock, iflags);
794 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
795 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
796 list_move(&rrq->list, &rrq_list);
19ca7609 797 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
798
799 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
800 list_del(&rrq->list);
801 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
802 }
19ca7609
JS
803}
804
19ca7609 805/**
1151e3ec 806 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
807 * @phba: Pointer to HBA context object.
808 * @ndlp: Targets nodelist pointer for this exchange.
809 * @xritag the xri in the bitmap to test.
810 *
811 * This function is called with hbalock held. This function
812 * returns 0 = rrq not active for this xri
813 * 1 = rrq is valid for this xri.
814 **/
1151e3ec
JS
815int
816lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
817 uint16_t xritag)
818{
1c2ba475 819 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
820 if (!ndlp)
821 return 0;
cff261f6
JS
822 if (!ndlp->active_rrqs_xri_bitmap)
823 return 0;
824 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
825 return 1;
826 else
827 return 0;
828}
829
830/**
831 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
832 * @phba: Pointer to HBA context object.
833 * @ndlp: nodelist pointer for this target.
834 * @xritag: xri used in this exchange.
835 * @rxid: Remote Exchange ID.
836 * @send_rrq: Flag used to determine if we should send rrq els cmd.
837 *
838 * This function takes the hbalock.
839 * The active bit is always set in the active rrq xri_bitmap even
840 * if there is no slot avaiable for the other rrq information.
841 *
842 * returns 0 rrq actived for this xri
843 * < 0 No memory or invalid ndlp.
844 **/
845int
846lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 847 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 848{
19ca7609 849 unsigned long iflags;
b42c07c8
JS
850 struct lpfc_node_rrq *rrq;
851 int empty;
852
853 if (!ndlp)
854 return -EINVAL;
855
856 if (!phba->cfg_enable_rrq)
857 return -EINVAL;
19ca7609
JS
858
859 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
860 if (phba->pport->load_flag & FC_UNLOADING) {
861 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
862 goto out;
863 }
864
865 /*
866 * set the active bit even if there is no mem available.
867 */
868 if (NLP_CHK_FREE_REQ(ndlp))
869 goto out;
870
871 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
872 goto out;
873
cff261f6
JS
874 if (!ndlp->active_rrqs_xri_bitmap)
875 goto out;
876
877 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
878 goto out;
879
19ca7609 880 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
881 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
882 if (!rrq) {
883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
884 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
885 " DID:0x%x Send:%d\n",
886 xritag, rxid, ndlp->nlp_DID, send_rrq);
887 return -EINVAL;
888 }
e5771b4d
JS
889 if (phba->cfg_enable_rrq == 1)
890 rrq->send_rrq = send_rrq;
891 else
892 rrq->send_rrq = 0;
b42c07c8 893 rrq->xritag = xritag;
256ec0d0
JS
894 rrq->rrq_stop_time = jiffies +
895 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
896 rrq->ndlp = ndlp;
897 rrq->nlp_DID = ndlp->nlp_DID;
898 rrq->vport = ndlp->vport;
899 rrq->rxid = rxid;
b42c07c8
JS
900 spin_lock_irqsave(&phba->hbalock, iflags);
901 empty = list_empty(&phba->active_rrq_list);
902 list_add_tail(&rrq->list, &phba->active_rrq_list);
903 phba->hba_flag |= HBA_RRQ_ACTIVE;
904 if (empty)
905 lpfc_worker_wake_up(phba);
906 spin_unlock_irqrestore(&phba->hbalock, iflags);
907 return 0;
908out:
909 spin_unlock_irqrestore(&phba->hbalock, iflags);
910 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
911 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
912 " DID:0x%x Send:%d\n",
913 xritag, rxid, ndlp->nlp_DID, send_rrq);
914 return -EINVAL;
19ca7609
JS
915}
916
da0436e9 917/**
895427bd 918 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 919 * @phba: Pointer to HBA context object.
19ca7609 920 * @piocb: Pointer to the iocbq.
da0436e9 921 *
dafe8cea 922 * This function is called with the ring lock held. This function
6d368e53 923 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
924 * list is not empty then it is successful, it returns pointer to the newly
925 * allocated sglq object else it returns NULL.
926 **/
927static struct lpfc_sglq *
895427bd 928__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 929{
895427bd 930 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 931 struct lpfc_sglq *sglq = NULL;
19ca7609 932 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
933 struct lpfc_scsi_buf *lpfc_cmd;
934 struct lpfc_nodelist *ndlp;
935 int found = 0;
936
1c2ba475
JT
937 lockdep_assert_held(&phba->hbalock);
938
19ca7609
JS
939 if (piocbq->iocb_flag & LPFC_IO_FCP) {
940 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
941 ndlp = lpfc_cmd->rdata->pnode;
be858b65 942 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 943 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 944 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
945 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
946 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
947 ndlp = NULL;
948 else
949 ndlp = piocbq->context_un.ndlp;
950 } else {
19ca7609 951 ndlp = piocbq->context1;
6c7cf486 952 }
19ca7609 953
895427bd
JS
954 spin_lock(&phba->sli4_hba.sgl_list_lock);
955 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
956 start_sglq = sglq;
957 while (!found) {
958 if (!sglq)
d11f54b7 959 break;
895427bd
JS
960 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
961 test_bit(sglq->sli4_lxritag,
962 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
963 /* This xri has an rrq outstanding for this DID.
964 * put it back in the list and get another xri.
965 */
895427bd 966 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 967 sglq = NULL;
895427bd 968 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
969 struct lpfc_sglq, list);
970 if (sglq == start_sglq) {
14041bd1 971 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609
JS
972 sglq = NULL;
973 break;
974 } else
975 continue;
976 }
977 sglq->ndlp = ndlp;
978 found = 1;
6d368e53 979 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
980 sglq->state = SGL_ALLOCATED;
981 }
895427bd 982 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
983 return sglq;
984}
985
f358dd0c
JS
986/**
987 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
988 * @phba: Pointer to HBA context object.
989 * @piocb: Pointer to the iocbq.
990 *
991 * This function is called with the sgl_list lock held. This function
992 * gets a new driver sglq object from the sglq list. If the
993 * list is not empty then it is successful, it returns pointer to the newly
994 * allocated sglq object else it returns NULL.
995 **/
996struct lpfc_sglq *
997__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
998{
999 struct list_head *lpfc_nvmet_sgl_list;
1000 struct lpfc_sglq *sglq = NULL;
1001
1002 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1003
1004 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1005
1006 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1007 if (!sglq)
1008 return NULL;
1009 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1010 sglq->state = SGL_ALLOCATED;
da0436e9
JS
1011 return sglq;
1012}
1013
e59058c4 1014/**
3621a710 1015 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1016 * @phba: Pointer to HBA context object.
1017 *
1018 * This function is called with no lock held. This function
1019 * allocates a new driver iocb object from the iocb pool. If the
1020 * allocation is successful, it returns pointer to the newly
1021 * allocated iocb object else it returns NULL.
1022 **/
2e0fef85
JS
1023struct lpfc_iocbq *
1024lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1025{
1026 struct lpfc_iocbq * iocbq = NULL;
1027 unsigned long iflags;
1028
1029 spin_lock_irqsave(&phba->hbalock, iflags);
1030 iocbq = __lpfc_sli_get_iocbq(phba);
1031 spin_unlock_irqrestore(&phba->hbalock, iflags);
1032 return iocbq;
1033}
1034
4f774513
JS
1035/**
1036 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1037 * @phba: Pointer to HBA context object.
1038 * @iocbq: Pointer to driver iocb object.
1039 *
1040 * This function is called with hbalock held to release driver
1041 * iocb object to the iocb pool. The iotag in the iocb object
1042 * does not change for each use of the iocb object. This function
1043 * clears all other fields of the iocb object when it is freed.
1044 * The sqlq structure that holds the xritag and phys and virtual
1045 * mappings for the scatter gather list is retrieved from the
1046 * active array of sglq. The get of the sglq pointer also clears
1047 * the entry in the array. If the status of the IO indiactes that
1048 * this IO was aborted then the sglq entry it put on the
1049 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1050 * IO has good status or fails for any other reason then the sglq
895427bd 1051 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1052 **/
1053static void
1054__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1055{
1056 struct lpfc_sglq *sglq;
1057 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1058 unsigned long iflag = 0;
895427bd 1059 struct lpfc_sli_ring *pring;
4f774513 1060
1c2ba475
JT
1061 lockdep_assert_held(&phba->hbalock);
1062
4f774513
JS
1063 if (iocbq->sli4_xritag == NO_XRI)
1064 sglq = NULL;
1065 else
6d368e53
JS
1066 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1067
0e9bb8d7 1068
4f774513 1069 if (sglq) {
f358dd0c
JS
1070 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1071 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1072 iflag);
1073 sglq->state = SGL_FREED;
1074 sglq->ndlp = NULL;
1075 list_add_tail(&sglq->list,
1076 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1077 spin_unlock_irqrestore(
1078 &phba->sli4_hba.sgl_list_lock, iflag);
1079 goto out;
1080 }
1081
895427bd 1082 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1083 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1084 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1085 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1086 iflag);
4f774513 1087 list_add(&sglq->list,
895427bd 1088 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1089 spin_unlock_irqrestore(
895427bd 1090 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1091 } else {
895427bd
JS
1092 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1093 iflag);
0f65ff68 1094 sglq->state = SGL_FREED;
19ca7609 1095 sglq->ndlp = NULL;
fedd3b7b 1096 list_add_tail(&sglq->list,
895427bd
JS
1097 &phba->sli4_hba.lpfc_els_sgl_list);
1098 spin_unlock_irqrestore(
1099 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1100
1101 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1102 if (!list_empty(&pring->txq))
2a9bf3d0 1103 lpfc_worker_wake_up(phba);
0f65ff68 1104 }
4f774513
JS
1105 }
1106
f358dd0c 1107out:
4f774513
JS
1108 /*
1109 * Clean all volatile data fields, preserve iotag and node struct.
1110 */
1111 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1112 iocbq->sli4_lxritag = NO_XRI;
4f774513 1113 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1114 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1115 LPFC_IO_NVME_LS);
4f774513
JS
1116 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1117}
1118
2a9bf3d0 1119
e59058c4 1120/**
3772a991 1121 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1122 * @phba: Pointer to HBA context object.
1123 * @iocbq: Pointer to driver iocb object.
1124 *
1125 * This function is called with hbalock held to release driver
1126 * iocb object to the iocb pool. The iotag in the iocb object
1127 * does not change for each use of the iocb object. This function
1128 * clears all other fields of the iocb object when it is freed.
1129 **/
a6ababd2 1130static void
3772a991 1131__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1132{
2e0fef85 1133 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1134
1c2ba475 1135 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1136
604a3e30
JB
1137 /*
1138 * Clean all volatile data fields, preserve iotag and node struct.
1139 */
1140 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1141 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1142 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1143}
1144
3772a991
JS
1145/**
1146 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1147 * @phba: Pointer to HBA context object.
1148 * @iocbq: Pointer to driver iocb object.
1149 *
1150 * This function is called with hbalock held to release driver
1151 * iocb object to the iocb pool. The iotag in the iocb object
1152 * does not change for each use of the iocb object. This function
1153 * clears all other fields of the iocb object when it is freed.
1154 **/
1155static void
1156__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1157{
1c2ba475
JT
1158 lockdep_assert_held(&phba->hbalock);
1159
3772a991 1160 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1161 phba->iocb_cnt--;
3772a991
JS
1162}
1163
e59058c4 1164/**
3621a710 1165 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1166 * @phba: Pointer to HBA context object.
1167 * @iocbq: Pointer to driver iocb object.
1168 *
1169 * This function is called with no lock held to release the iocb to
1170 * iocb pool.
1171 **/
2e0fef85
JS
1172void
1173lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1174{
1175 unsigned long iflags;
1176
1177 /*
1178 * Clean all volatile data fields, preserve iotag and node struct.
1179 */
1180 spin_lock_irqsave(&phba->hbalock, iflags);
1181 __lpfc_sli_release_iocbq(phba, iocbq);
1182 spin_unlock_irqrestore(&phba->hbalock, iflags);
1183}
1184
a257bf90
JS
1185/**
1186 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1187 * @phba: Pointer to HBA context object.
1188 * @iocblist: List of IOCBs.
1189 * @ulpstatus: ULP status in IOCB command field.
1190 * @ulpWord4: ULP word-4 in IOCB command field.
1191 *
1192 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1193 * on the list by invoking the complete callback function associated with the
1194 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1195 * fields.
1196 **/
1197void
1198lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1199 uint32_t ulpstatus, uint32_t ulpWord4)
1200{
1201 struct lpfc_iocbq *piocb;
1202
1203 while (!list_empty(iocblist)) {
1204 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1205 if (!piocb->iocb_cmpl)
1206 lpfc_sli_release_iocbq(phba, piocb);
1207 else {
1208 piocb->iocb.ulpStatus = ulpstatus;
1209 piocb->iocb.un.ulpWord[4] = ulpWord4;
1210 (piocb->iocb_cmpl) (phba, piocb, piocb);
1211 }
1212 }
1213 return;
1214}
1215
e59058c4 1216/**
3621a710
JS
1217 * lpfc_sli_iocb_cmd_type - Get the iocb type
1218 * @iocb_cmnd: iocb command code.
e59058c4
JS
1219 *
1220 * This function is called by ring event handler function to get the iocb type.
1221 * This function translates the iocb command to an iocb command type used to
1222 * decide the final disposition of each completed IOCB.
1223 * The function returns
1224 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1225 * LPFC_SOL_IOCB if it is a solicited iocb completion
1226 * LPFC_ABORT_IOCB if it is an abort iocb
1227 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1228 *
1229 * The caller is not required to hold any lock.
1230 **/
dea3101e
JB
1231static lpfc_iocb_type
1232lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1233{
1234 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1235
1236 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1237 return 0;
1238
1239 switch (iocb_cmnd) {
1240 case CMD_XMIT_SEQUENCE_CR:
1241 case CMD_XMIT_SEQUENCE_CX:
1242 case CMD_XMIT_BCAST_CN:
1243 case CMD_XMIT_BCAST_CX:
1244 case CMD_ELS_REQUEST_CR:
1245 case CMD_ELS_REQUEST_CX:
1246 case CMD_CREATE_XRI_CR:
1247 case CMD_CREATE_XRI_CX:
1248 case CMD_GET_RPI_CN:
1249 case CMD_XMIT_ELS_RSP_CX:
1250 case CMD_GET_RPI_CR:
1251 case CMD_FCP_IWRITE_CR:
1252 case CMD_FCP_IWRITE_CX:
1253 case CMD_FCP_IREAD_CR:
1254 case CMD_FCP_IREAD_CX:
1255 case CMD_FCP_ICMND_CR:
1256 case CMD_FCP_ICMND_CX:
f5603511
JS
1257 case CMD_FCP_TSEND_CX:
1258 case CMD_FCP_TRSP_CX:
1259 case CMD_FCP_TRECEIVE_CX:
1260 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1261 case CMD_ADAPTER_MSG:
1262 case CMD_ADAPTER_DUMP:
1263 case CMD_XMIT_SEQUENCE64_CR:
1264 case CMD_XMIT_SEQUENCE64_CX:
1265 case CMD_XMIT_BCAST64_CN:
1266 case CMD_XMIT_BCAST64_CX:
1267 case CMD_ELS_REQUEST64_CR:
1268 case CMD_ELS_REQUEST64_CX:
1269 case CMD_FCP_IWRITE64_CR:
1270 case CMD_FCP_IWRITE64_CX:
1271 case CMD_FCP_IREAD64_CR:
1272 case CMD_FCP_IREAD64_CX:
1273 case CMD_FCP_ICMND64_CR:
1274 case CMD_FCP_ICMND64_CX:
f5603511
JS
1275 case CMD_FCP_TSEND64_CX:
1276 case CMD_FCP_TRSP64_CX:
1277 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1278 case CMD_GEN_REQUEST64_CR:
1279 case CMD_GEN_REQUEST64_CX:
1280 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1281 case DSSCMD_IWRITE64_CR:
1282 case DSSCMD_IWRITE64_CX:
1283 case DSSCMD_IREAD64_CR:
1284 case DSSCMD_IREAD64_CX:
dea3101e
JB
1285 type = LPFC_SOL_IOCB;
1286 break;
1287 case CMD_ABORT_XRI_CN:
1288 case CMD_ABORT_XRI_CX:
1289 case CMD_CLOSE_XRI_CN:
1290 case CMD_CLOSE_XRI_CX:
1291 case CMD_XRI_ABORTED_CX:
1292 case CMD_ABORT_MXRI64_CN:
6669f9bb 1293 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1294 type = LPFC_ABORT_IOCB;
1295 break;
1296 case CMD_RCV_SEQUENCE_CX:
1297 case CMD_RCV_ELS_REQ_CX:
1298 case CMD_RCV_SEQUENCE64_CX:
1299 case CMD_RCV_ELS_REQ64_CX:
57127f15 1300 case CMD_ASYNC_STATUS:
ed957684
JS
1301 case CMD_IOCB_RCV_SEQ64_CX:
1302 case CMD_IOCB_RCV_ELS64_CX:
1303 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1304 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1305 type = LPFC_UNSOL_IOCB;
1306 break;
3163f725
JS
1307 case CMD_IOCB_XMIT_MSEQ64_CR:
1308 case CMD_IOCB_XMIT_MSEQ64_CX:
1309 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1310 case CMD_IOCB_RCV_ELS_LIST64_CX:
1311 case CMD_IOCB_CLOSE_EXTENDED_CN:
1312 case CMD_IOCB_ABORT_EXTENDED_CN:
1313 case CMD_IOCB_RET_HBQE64_CN:
1314 case CMD_IOCB_FCP_IBIDIR64_CR:
1315 case CMD_IOCB_FCP_IBIDIR64_CX:
1316 case CMD_IOCB_FCP_ITASKMGT64_CX:
1317 case CMD_IOCB_LOGENTRY_CN:
1318 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1319 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1320 __func__, iocb_cmnd);
3163f725
JS
1321 type = LPFC_UNKNOWN_IOCB;
1322 break;
dea3101e
JB
1323 default:
1324 type = LPFC_UNKNOWN_IOCB;
1325 break;
1326 }
1327
1328 return type;
1329}
1330
e59058c4 1331/**
3621a710 1332 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1333 * @phba: Pointer to HBA context object.
1334 *
1335 * This function is called from SLI initialization code
1336 * to configure every ring of the HBA's SLI interface. The
1337 * caller is not required to hold any lock. This function issues
1338 * a config_ring mailbox command for each ring.
1339 * This function returns zero if successful else returns a negative
1340 * error code.
1341 **/
dea3101e 1342static int
ed957684 1343lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1344{
1345 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1346 LPFC_MBOXQ_t *pmb;
1347 MAILBOX_t *pmbox;
1348 int i, rc, ret = 0;
dea3101e 1349
ed957684
JS
1350 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1351 if (!pmb)
1352 return -ENOMEM;
04c68496 1353 pmbox = &pmb->u.mb;
ed957684 1354 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1355 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1356 lpfc_config_ring(phba, i, pmb);
1357 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1358 if (rc != MBX_SUCCESS) {
92d7f7b0 1359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1360 "0446 Adapter failed to init (%d), "
dea3101e
JB
1361 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1362 "ring %d\n",
e8b62011
JS
1363 rc, pmbox->mbxCommand,
1364 pmbox->mbxStatus, i);
2e0fef85 1365 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1366 ret = -ENXIO;
1367 break;
dea3101e
JB
1368 }
1369 }
ed957684
JS
1370 mempool_free(pmb, phba->mbox_mem_pool);
1371 return ret;
dea3101e
JB
1372}
1373
e59058c4 1374/**
3621a710 1375 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1376 * @phba: Pointer to HBA context object.
1377 * @pring: Pointer to driver SLI ring object.
1378 * @piocb: Pointer to the driver iocb object.
1379 *
1380 * This function is called with hbalock held. The function adds the
1381 * new iocb to txcmplq of the given ring. This function always returns
1382 * 0. If this function is called for ELS ring, this function checks if
1383 * there is a vport associated with the ELS command. This function also
1384 * starts els_tmofunc timer if this is an ELS command.
1385 **/
dea3101e 1386static int
2e0fef85
JS
1387lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1388 struct lpfc_iocbq *piocb)
dea3101e 1389{
1c2ba475
JT
1390 lockdep_assert_held(&phba->hbalock);
1391
2319f847 1392 BUG_ON(!piocb);
22466da5 1393
dea3101e 1394 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1395 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1396
92d7f7b0
JS
1397 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1398 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1399 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1400 BUG_ON(!piocb->vport);
1401 if (!(piocb->vport->load_flag & FC_UNLOADING))
1402 mod_timer(&piocb->vport->els_tmofunc,
1403 jiffies +
1404 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1405 }
dea3101e 1406
2e0fef85 1407 return 0;
dea3101e
JB
1408}
1409
e59058c4 1410/**
3621a710 1411 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1412 * @phba: Pointer to HBA context object.
1413 * @pring: Pointer to driver SLI ring object.
1414 *
1415 * This function is called with hbalock held to get next
1416 * iocb in txq of the given ring. If there is any iocb in
1417 * the txq, the function returns first iocb in the list after
1418 * removing the iocb from the list, else it returns NULL.
1419 **/
2a9bf3d0 1420struct lpfc_iocbq *
2e0fef85 1421lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1422{
dea3101e
JB
1423 struct lpfc_iocbq *cmd_iocb;
1424
1c2ba475
JT
1425 lockdep_assert_held(&phba->hbalock);
1426
858c9f6c 1427 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1428 return cmd_iocb;
dea3101e
JB
1429}
1430
e59058c4 1431/**
3621a710 1432 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1433 * @phba: Pointer to HBA context object.
1434 * @pring: Pointer to driver SLI ring object.
1435 *
1436 * This function is called with hbalock held and the caller must post the
1437 * iocb without releasing the lock. If the caller releases the lock,
1438 * iocb slot returned by the function is not guaranteed to be available.
1439 * The function returns pointer to the next available iocb slot if there
1440 * is available slot in the ring, else it returns NULL.
1441 * If the get index of the ring is ahead of the put index, the function
1442 * will post an error attention event to the worker thread to take the
1443 * HBA to offline state.
1444 **/
dea3101e
JB
1445static IOCB_t *
1446lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1447{
34b02dcd 1448 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1449 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1450
1451 lockdep_assert_held(&phba->hbalock);
1452
7e56aa25
JS
1453 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1454 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1455 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1456
7e56aa25
JS
1457 if (unlikely(pring->sli.sli3.local_getidx ==
1458 pring->sli.sli3.next_cmdidx)) {
dea3101e 1459
7e56aa25 1460 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1461
7e56aa25 1462 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1464 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1465 "is bigger than cmd ring %d\n",
e8b62011 1466 pring->ringno,
7e56aa25
JS
1467 pring->sli.sli3.local_getidx,
1468 max_cmd_idx);
dea3101e 1469
2e0fef85 1470 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1471 /*
1472 * All error attention handlers are posted to
1473 * worker thread
1474 */
1475 phba->work_ha |= HA_ERATT;
1476 phba->work_hs = HS_FFER3;
92d7f7b0 1477
5e9d9b82 1478 lpfc_worker_wake_up(phba);
dea3101e
JB
1479
1480 return NULL;
1481 }
1482
7e56aa25 1483 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1484 return NULL;
1485 }
1486
ed957684 1487 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1488}
1489
e59058c4 1490/**
3621a710 1491 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1492 * @phba: Pointer to HBA context object.
1493 * @iocbq: Pointer to driver iocb object.
1494 *
1495 * This function gets an iotag for the iocb. If there is no unused iotag and
1496 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1497 * array and assigns a new iotag.
1498 * The function returns the allocated iotag if successful, else returns zero.
1499 * Zero is not a valid iotag.
1500 * The caller is not required to hold any lock.
1501 **/
604a3e30 1502uint16_t
2e0fef85 1503lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1504{
2e0fef85
JS
1505 struct lpfc_iocbq **new_arr;
1506 struct lpfc_iocbq **old_arr;
604a3e30
JB
1507 size_t new_len;
1508 struct lpfc_sli *psli = &phba->sli;
1509 uint16_t iotag;
dea3101e 1510
2e0fef85 1511 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1512 iotag = psli->last_iotag;
1513 if(++iotag < psli->iocbq_lookup_len) {
1514 psli->last_iotag = iotag;
1515 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1516 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1517 iocbq->iotag = iotag;
1518 return iotag;
2e0fef85 1519 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1520 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1521 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1522 spin_unlock_irq(&phba->hbalock);
1523 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1524 GFP_KERNEL);
1525 if (new_arr) {
2e0fef85 1526 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1527 old_arr = psli->iocbq_lookup;
1528 if (new_len <= psli->iocbq_lookup_len) {
1529 /* highly unprobable case */
1530 kfree(new_arr);
1531 iotag = psli->last_iotag;
1532 if(++iotag < psli->iocbq_lookup_len) {
1533 psli->last_iotag = iotag;
1534 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1535 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1536 iocbq->iotag = iotag;
1537 return iotag;
1538 }
2e0fef85 1539 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1540 return 0;
1541 }
1542 if (psli->iocbq_lookup)
1543 memcpy(new_arr, old_arr,
1544 ((psli->last_iotag + 1) *
311464ec 1545 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1546 psli->iocbq_lookup = new_arr;
1547 psli->iocbq_lookup_len = new_len;
1548 psli->last_iotag = iotag;
1549 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1550 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1551 iocbq->iotag = iotag;
1552 kfree(old_arr);
1553 return iotag;
1554 }
8f6d98d2 1555 } else
2e0fef85 1556 spin_unlock_irq(&phba->hbalock);
dea3101e 1557
bc73905a 1558 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1559 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1560 psli->last_iotag);
dea3101e 1561
604a3e30 1562 return 0;
dea3101e
JB
1563}
1564
e59058c4 1565/**
3621a710 1566 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1567 * @phba: Pointer to HBA context object.
1568 * @pring: Pointer to driver SLI ring object.
1569 * @iocb: Pointer to iocb slot in the ring.
1570 * @nextiocb: Pointer to driver iocb object which need to be
1571 * posted to firmware.
1572 *
1573 * This function is called with hbalock held to post a new iocb to
1574 * the firmware. This function copies the new iocb to ring iocb slot and
1575 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1576 * a completion call back for this iocb else the function will free the
1577 * iocb object.
1578 **/
dea3101e
JB
1579static void
1580lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1581 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1582{
1c2ba475 1583 lockdep_assert_held(&phba->hbalock);
dea3101e 1584 /*
604a3e30 1585 * Set up an iotag
dea3101e 1586 */
604a3e30 1587 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1588
e2a0a9d6 1589
a58cbd52
JS
1590 if (pring->ringno == LPFC_ELS_RING) {
1591 lpfc_debugfs_slow_ring_trc(phba,
1592 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1593 *(((uint32_t *) &nextiocb->iocb) + 4),
1594 *(((uint32_t *) &nextiocb->iocb) + 6),
1595 *(((uint32_t *) &nextiocb->iocb) + 7));
1596 }
1597
dea3101e
JB
1598 /*
1599 * Issue iocb command to adapter
1600 */
92d7f7b0 1601 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1602 wmb();
1603 pring->stats.iocb_cmd++;
1604
1605 /*
1606 * If there is no completion routine to call, we can release the
1607 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1608 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1609 */
1610 if (nextiocb->iocb_cmpl)
1611 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1612 else
2e0fef85 1613 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1614
1615 /*
1616 * Let the HBA know what IOCB slot will be the next one the
1617 * driver will put a command into.
1618 */
7e56aa25
JS
1619 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1620 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1621}
1622
e59058c4 1623/**
3621a710 1624 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1625 * @phba: Pointer to HBA context object.
1626 * @pring: Pointer to driver SLI ring object.
1627 *
1628 * The caller is not required to hold any lock for calling this function.
1629 * This function updates the chip attention bits for the ring to inform firmware
1630 * that there are pending work to be done for this ring and requests an
1631 * interrupt when there is space available in the ring. This function is
1632 * called when the driver is unable to post more iocbs to the ring due
1633 * to unavailability of space in the ring.
1634 **/
dea3101e 1635static void
2e0fef85 1636lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1637{
1638 int ringno = pring->ringno;
1639
1640 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1641
1642 wmb();
1643
1644 /*
1645 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1646 * The HBA will tell us when an IOCB entry is available.
1647 */
1648 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1649 readl(phba->CAregaddr); /* flush */
1650
1651 pring->stats.iocb_cmd_full++;
1652}
1653
e59058c4 1654/**
3621a710 1655 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1656 * @phba: Pointer to HBA context object.
1657 * @pring: Pointer to driver SLI ring object.
1658 *
1659 * This function updates the chip attention register bit for the
1660 * given ring to inform HBA that there is more work to be done
1661 * in this ring. The caller is not required to hold any lock.
1662 **/
dea3101e 1663static void
2e0fef85 1664lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1665{
1666 int ringno = pring->ringno;
1667
1668 /*
1669 * Tell the HBA that there is work to do in this ring.
1670 */
34b02dcd
JS
1671 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1672 wmb();
1673 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1674 readl(phba->CAregaddr); /* flush */
1675 }
dea3101e
JB
1676}
1677
e59058c4 1678/**
3621a710 1679 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1680 * @phba: Pointer to HBA context object.
1681 * @pring: Pointer to driver SLI ring object.
1682 *
1683 * This function is called with hbalock held to post pending iocbs
1684 * in the txq to the firmware. This function is called when driver
1685 * detects space available in the ring.
1686 **/
dea3101e 1687static void
2e0fef85 1688lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1689{
1690 IOCB_t *iocb;
1691 struct lpfc_iocbq *nextiocb;
1692
1c2ba475
JT
1693 lockdep_assert_held(&phba->hbalock);
1694
dea3101e
JB
1695 /*
1696 * Check to see if:
1697 * (a) there is anything on the txq to send
1698 * (b) link is up
1699 * (c) link attention events can be processed (fcp ring only)
1700 * (d) IOCB processing is not blocked by the outstanding mbox command.
1701 */
0e9bb8d7
JS
1702
1703 if (lpfc_is_link_up(phba) &&
1704 (!list_empty(&pring->txq)) &&
895427bd 1705 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1706 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1707
1708 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1709 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1710 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1711
1712 if (iocb)
1713 lpfc_sli_update_ring(phba, pring);
1714 else
1715 lpfc_sli_update_full_ring(phba, pring);
1716 }
1717
1718 return;
1719}
1720
e59058c4 1721/**
3621a710 1722 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1723 * @phba: Pointer to HBA context object.
1724 * @hbqno: HBQ number.
1725 *
1726 * This function is called with hbalock held to get the next
1727 * available slot for the given HBQ. If there is free slot
1728 * available for the HBQ it will return pointer to the next available
1729 * HBQ entry else it will return NULL.
1730 **/
a6ababd2 1731static struct lpfc_hbq_entry *
ed957684
JS
1732lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1733{
1734 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1735
1c2ba475
JT
1736 lockdep_assert_held(&phba->hbalock);
1737
ed957684
JS
1738 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1739 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1740 hbqp->next_hbqPutIdx = 0;
1741
1742 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1743 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1744 uint32_t getidx = le32_to_cpu(raw_index);
1745
1746 hbqp->local_hbqGetIdx = getidx;
1747
1748 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1749 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1750 LOG_SLI | LOG_VPORT,
e8b62011 1751 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1752 "%u is > than hbqp->entry_count %u\n",
e8b62011 1753 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1754 hbqp->entry_count);
1755
1756 phba->link_state = LPFC_HBA_ERROR;
1757 return NULL;
1758 }
1759
1760 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1761 return NULL;
1762 }
1763
51ef4c26
JS
1764 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1765 hbqp->hbqPutIdx;
ed957684
JS
1766}
1767
e59058c4 1768/**
3621a710 1769 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1770 * @phba: Pointer to HBA context object.
1771 *
1772 * This function is called with no lock held to free all the
1773 * hbq buffers while uninitializing the SLI interface. It also
1774 * frees the HBQ buffers returned by the firmware but not yet
1775 * processed by the upper layers.
1776 **/
ed957684
JS
1777void
1778lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1779{
92d7f7b0
JS
1780 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1781 struct hbq_dmabuf *hbq_buf;
3163f725 1782 unsigned long flags;
51ef4c26 1783 int i, hbq_count;
ed957684 1784
51ef4c26 1785 hbq_count = lpfc_sli_hbq_count();
ed957684 1786 /* Return all memory used by all HBQs */
3163f725 1787 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1788 for (i = 0; i < hbq_count; ++i) {
1789 list_for_each_entry_safe(dmabuf, next_dmabuf,
1790 &phba->hbqs[i].hbq_buffer_list, list) {
1791 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1792 list_del(&hbq_buf->dbuf.list);
1793 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1794 }
a8adb832 1795 phba->hbqs[i].buffer_count = 0;
ed957684 1796 }
3163f725
JS
1797
1798 /* Mark the HBQs not in use */
1799 phba->hbq_in_use = 0;
1800 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1801}
1802
e59058c4 1803/**
3621a710 1804 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1805 * @phba: Pointer to HBA context object.
1806 * @hbqno: HBQ number.
1807 * @hbq_buf: Pointer to HBQ buffer.
1808 *
1809 * This function is called with the hbalock held to post a
1810 * hbq buffer to the firmware. If the function finds an empty
1811 * slot in the HBQ, it will post the buffer. The function will return
1812 * pointer to the hbq entry if it successfully post the buffer
1813 * else it will return NULL.
1814 **/
3772a991 1815static int
ed957684 1816lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1817 struct hbq_dmabuf *hbq_buf)
3772a991 1818{
1c2ba475 1819 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1820 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1821}
1822
1823/**
1824 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1825 * @phba: Pointer to HBA context object.
1826 * @hbqno: HBQ number.
1827 * @hbq_buf: Pointer to HBQ buffer.
1828 *
1829 * This function is called with the hbalock held to post a hbq buffer to the
1830 * firmware. If the function finds an empty slot in the HBQ, it will post the
1831 * buffer and place it on the hbq_buffer_list. The function will return zero if
1832 * it successfully post the buffer else it will return an error.
1833 **/
1834static int
1835lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1836 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1837{
1838 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1839 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 1840
1c2ba475 1841 lockdep_assert_held(&phba->hbalock);
ed957684
JS
1842 /* Get next HBQ entry slot to use */
1843 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1844 if (hbqe) {
1845 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1846
92d7f7b0
JS
1847 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1848 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 1849 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 1850 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1851 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1852 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1853 /* Sync SLIM */
ed957684
JS
1854 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1855 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1856 /* flush */
ed957684 1857 readl(phba->hbq_put + hbqno);
51ef4c26 1858 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1859 return 0;
1860 } else
1861 return -ENOMEM;
ed957684
JS
1862}
1863
4f774513
JS
1864/**
1865 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1866 * @phba: Pointer to HBA context object.
1867 * @hbqno: HBQ number.
1868 * @hbq_buf: Pointer to HBQ buffer.
1869 *
1870 * This function is called with the hbalock held to post an RQE to the SLI4
1871 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1872 * the hbq_buffer_list and return zero, otherwise it will return an error.
1873 **/
1874static int
1875lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1876 struct hbq_dmabuf *hbq_buf)
1877{
1878 int rc;
1879 struct lpfc_rqe hrqe;
1880 struct lpfc_rqe drqe;
895427bd
JS
1881 struct lpfc_queue *hrq;
1882 struct lpfc_queue *drq;
1883
1884 if (hbqno != LPFC_ELS_HBQ)
1885 return 1;
1886 hrq = phba->sli4_hba.hdr_rq;
1887 drq = phba->sli4_hba.dat_rq;
4f774513 1888
1c2ba475 1889 lockdep_assert_held(&phba->hbalock);
4f774513
JS
1890 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1891 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1892 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1893 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 1894 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
1895 if (rc < 0)
1896 return rc;
895427bd 1897 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
1898 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1899 return 0;
1900}
1901
e59058c4 1902/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1903static struct lpfc_hbq_init lpfc_els_hbq = {
1904 .rn = 1,
def9c7a9 1905 .entry_count = 256,
92d7f7b0
JS
1906 .mask_count = 0,
1907 .profile = 0,
51ef4c26 1908 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1909 .buffer_count = 0,
a257bf90
JS
1910 .init_count = 40,
1911 .add_count = 40,
92d7f7b0 1912};
ed957684 1913
e59058c4 1914/* Array of HBQs */
78b2d852 1915struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
1916 &lpfc_els_hbq,
1917};
ed957684 1918
e59058c4 1919/**
3621a710 1920 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1921 * @phba: Pointer to HBA context object.
1922 * @hbqno: HBQ number.
1923 * @count: Number of HBQ buffers to be posted.
1924 *
d7c255b2
JS
1925 * This function is called with no lock held to post more hbq buffers to the
1926 * given HBQ. The function returns the number of HBQ buffers successfully
1927 * posted.
e59058c4 1928 **/
311464ec 1929static int
92d7f7b0 1930lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1931{
d7c255b2 1932 uint32_t i, posted = 0;
3163f725 1933 unsigned long flags;
92d7f7b0 1934 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1935 LIST_HEAD(hbq_buf_list);
eafe1df9 1936 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1937 return 0;
51ef4c26 1938
d7c255b2
JS
1939 if ((phba->hbqs[hbqno].buffer_count + count) >
1940 lpfc_hbq_defs[hbqno]->entry_count)
1941 count = lpfc_hbq_defs[hbqno]->entry_count -
1942 phba->hbqs[hbqno].buffer_count;
1943 if (!count)
1944 return 0;
1945 /* Allocate HBQ entries */
1946 for (i = 0; i < count; i++) {
1947 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1948 if (!hbq_buffer)
1949 break;
1950 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1951 }
3163f725
JS
1952 /* Check whether HBQ is still in use */
1953 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1954 if (!phba->hbq_in_use)
d7c255b2
JS
1955 goto err;
1956 while (!list_empty(&hbq_buf_list)) {
1957 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1958 dbuf.list);
1959 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1960 (hbqno << 16));
3772a991 1961 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1962 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1963 posted++;
1964 } else
51ef4c26 1965 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1966 }
3163f725 1967 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1968 return posted;
1969err:
eafe1df9 1970 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1971 while (!list_empty(&hbq_buf_list)) {
1972 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1973 dbuf.list);
1974 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1975 }
1976 return 0;
ed957684
JS
1977}
1978
e59058c4 1979/**
3621a710 1980 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1981 * @phba: Pointer to HBA context object.
1982 * @qno: HBQ number.
1983 *
1984 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1985 * is called with no lock held. The function returns the number of HBQ entries
1986 * successfully allocated.
e59058c4 1987 **/
92d7f7b0
JS
1988int
1989lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1990{
def9c7a9
JS
1991 if (phba->sli_rev == LPFC_SLI_REV4)
1992 return 0;
1993 else
1994 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1995 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1996}
ed957684 1997
e59058c4 1998/**
3621a710 1999 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
2000 * @phba: Pointer to HBA context object.
2001 * @qno: HBQ queue number.
2002 *
2003 * This function is called from SLI initialization code path with
2004 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 2005 * function returns the number of HBQ entries successfully allocated.
e59058c4 2006 **/
a6ababd2 2007static int
92d7f7b0
JS
2008lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2009{
def9c7a9
JS
2010 if (phba->sli_rev == LPFC_SLI_REV4)
2011 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2012 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2013 else
2014 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2015 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2016}
2017
3772a991
JS
2018/**
2019 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2020 * @phba: Pointer to HBA context object.
2021 * @hbqno: HBQ number.
2022 *
2023 * This function removes the first hbq buffer on an hbq list and returns a
2024 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2025 **/
2026static struct hbq_dmabuf *
2027lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2028{
2029 struct lpfc_dmabuf *d_buf;
2030
2031 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2032 if (!d_buf)
2033 return NULL;
2034 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2035}
2036
2d7dbc4c
JS
2037/**
2038 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2039 * @phba: Pointer to HBA context object.
2040 * @hbqno: HBQ number.
2041 *
2042 * This function removes the first RQ buffer on an RQ buffer list and returns a
2043 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2044 **/
2045static struct rqb_dmabuf *
2046lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2047{
2048 struct lpfc_dmabuf *h_buf;
2049 struct lpfc_rqb *rqbp;
2050
2051 rqbp = hrq->rqbp;
2052 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2053 struct lpfc_dmabuf, list);
2054 if (!h_buf)
2055 return NULL;
2056 rqbp->buffer_count--;
2057 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2058}
2059
e59058c4 2060/**
3621a710 2061 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2062 * @phba: Pointer to HBA context object.
2063 * @tag: Tag of the hbq buffer.
2064 *
71892418
SH
2065 * This function searches for the hbq buffer associated with the given tag in
2066 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2067 * otherwise it returns NULL.
e59058c4 2068 **/
a6ababd2 2069static struct hbq_dmabuf *
92d7f7b0 2070lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2071{
92d7f7b0
JS
2072 struct lpfc_dmabuf *d_buf;
2073 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2074 uint32_t hbqno;
2075
2076 hbqno = tag >> 16;
a0a74e45 2077 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2078 return NULL;
ed957684 2079
3772a991 2080 spin_lock_irq(&phba->hbalock);
51ef4c26 2081 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2082 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2083 if (hbq_buf->tag == tag) {
3772a991 2084 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2085 return hbq_buf;
ed957684
JS
2086 }
2087 }
3772a991 2088 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2090 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2091 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2092 return NULL;
ed957684
JS
2093}
2094
e59058c4 2095/**
3621a710 2096 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2097 * @phba: Pointer to HBA context object.
2098 * @hbq_buffer: Pointer to HBQ buffer.
2099 *
2100 * This function is called with hbalock. This function gives back
2101 * the hbq buffer to firmware. If the HBQ does not have space to
2102 * post the buffer, it will free the buffer.
2103 **/
ed957684 2104void
51ef4c26 2105lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2106{
2107 uint32_t hbqno;
2108
51ef4c26
JS
2109 if (hbq_buffer) {
2110 hbqno = hbq_buffer->tag >> 16;
3772a991 2111 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2112 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2113 }
2114}
2115
e59058c4 2116/**
3621a710 2117 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2118 * @mbxCommand: mailbox command code.
2119 *
2120 * This function is called by the mailbox event handler function to verify
2121 * that the completed mailbox command is a legitimate mailbox command. If the
2122 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2123 * and the mailbox event handler will take the HBA offline.
2124 **/
dea3101e
JB
2125static int
2126lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2127{
2128 uint8_t ret;
2129
2130 switch (mbxCommand) {
2131 case MBX_LOAD_SM:
2132 case MBX_READ_NV:
2133 case MBX_WRITE_NV:
a8adb832 2134 case MBX_WRITE_VPARMS:
dea3101e
JB
2135 case MBX_RUN_BIU_DIAG:
2136 case MBX_INIT_LINK:
2137 case MBX_DOWN_LINK:
2138 case MBX_CONFIG_LINK:
2139 case MBX_CONFIG_RING:
2140 case MBX_RESET_RING:
2141 case MBX_READ_CONFIG:
2142 case MBX_READ_RCONFIG:
2143 case MBX_READ_SPARM:
2144 case MBX_READ_STATUS:
2145 case MBX_READ_RPI:
2146 case MBX_READ_XRI:
2147 case MBX_READ_REV:
2148 case MBX_READ_LNK_STAT:
2149 case MBX_REG_LOGIN:
2150 case MBX_UNREG_LOGIN:
dea3101e
JB
2151 case MBX_CLEAR_LA:
2152 case MBX_DUMP_MEMORY:
2153 case MBX_DUMP_CONTEXT:
2154 case MBX_RUN_DIAGS:
2155 case MBX_RESTART:
2156 case MBX_UPDATE_CFG:
2157 case MBX_DOWN_LOAD:
2158 case MBX_DEL_LD_ENTRY:
2159 case MBX_RUN_PROGRAM:
2160 case MBX_SET_MASK:
09372820 2161 case MBX_SET_VARIABLE:
dea3101e 2162 case MBX_UNREG_D_ID:
41415862 2163 case MBX_KILL_BOARD:
dea3101e 2164 case MBX_CONFIG_FARP:
41415862 2165 case MBX_BEACON:
dea3101e
JB
2166 case MBX_LOAD_AREA:
2167 case MBX_RUN_BIU_DIAG64:
2168 case MBX_CONFIG_PORT:
2169 case MBX_READ_SPARM64:
2170 case MBX_READ_RPI64:
2171 case MBX_REG_LOGIN64:
76a95d75 2172 case MBX_READ_TOPOLOGY:
09372820 2173 case MBX_WRITE_WWN:
dea3101e
JB
2174 case MBX_SET_DEBUG:
2175 case MBX_LOAD_EXP_ROM:
57127f15 2176 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2177 case MBX_REG_VPI:
2178 case MBX_UNREG_VPI:
858c9f6c 2179 case MBX_HEARTBEAT:
84774a4d
JS
2180 case MBX_PORT_CAPABILITIES:
2181 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2182 case MBX_SLI4_CONFIG:
2183 case MBX_SLI4_REQ_FTRS:
2184 case MBX_REG_FCFI:
2185 case MBX_UNREG_FCFI:
2186 case MBX_REG_VFI:
2187 case MBX_UNREG_VFI:
2188 case MBX_INIT_VPI:
2189 case MBX_INIT_VFI:
2190 case MBX_RESUME_RPI:
c7495937
JS
2191 case MBX_READ_EVENT_LOG_STATUS:
2192 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2193 case MBX_SECURITY_MGMT:
2194 case MBX_AUTH_PORT:
940eb687 2195 case MBX_ACCESS_VDATA:
dea3101e
JB
2196 ret = mbxCommand;
2197 break;
2198 default:
2199 ret = MBX_SHUTDOWN;
2200 break;
2201 }
2e0fef85 2202 return ret;
dea3101e 2203}
e59058c4
JS
2204
2205/**
3621a710 2206 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2207 * @phba: Pointer to HBA context object.
2208 * @pmboxq: Pointer to mailbox command.
2209 *
2210 * This is completion handler function for mailbox commands issued from
2211 * lpfc_sli_issue_mbox_wait function. This function is called by the
2212 * mailbox event handler function with no lock held. This function
2213 * will wake up thread waiting on the wait queue pointed by context1
2214 * of the mailbox.
2215 **/
04c68496 2216void
2e0fef85 2217lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2218{
2219 wait_queue_head_t *pdone_q;
858c9f6c 2220 unsigned long drvr_flag;
dea3101e
JB
2221
2222 /*
2223 * If pdone_q is empty, the driver thread gave up waiting and
2224 * continued running.
2225 */
7054a606 2226 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2227 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2228 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2229 if (pdone_q)
2230 wake_up_interruptible(pdone_q);
858c9f6c 2231 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2232 return;
2233}
2234
e59058c4
JS
2235
2236/**
3621a710 2237 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2238 * @phba: Pointer to HBA context object.
2239 * @pmb: Pointer to mailbox object.
2240 *
2241 * This function is the default mailbox completion handler. It
2242 * frees the memory resources associated with the completed mailbox
2243 * command. If the completed command is a REG_LOGIN mailbox command,
2244 * this function will issue a UREG_LOGIN to re-claim the RPI.
2245 **/
dea3101e 2246void
2e0fef85 2247lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2248{
d439d286 2249 struct lpfc_vport *vport = pmb->vport;
dea3101e 2250 struct lpfc_dmabuf *mp;
d439d286 2251 struct lpfc_nodelist *ndlp;
5af5eee7 2252 struct Scsi_Host *shost;
04c68496 2253 uint16_t rpi, vpi;
7054a606
JS
2254 int rc;
2255
dea3101e 2256 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2257
dea3101e
JB
2258 if (mp) {
2259 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2260 kfree(mp);
2261 }
7054a606
JS
2262
2263 /*
2264 * If a REG_LOGIN succeeded after node is destroyed or node
2265 * is in re-discovery driver need to cleanup the RPI.
2266 */
2e0fef85 2267 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2268 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2269 !pmb->u.mb.mbxStatus) {
2270 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2271 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2272 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2273 pmb->vport = vport;
92d7f7b0 2274 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2275 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2276 if (rc != MBX_NOT_FINISHED)
2277 return;
2278 }
2279
695a814e
JS
2280 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2281 !(phba->pport->load_flag & FC_UNLOADING) &&
2282 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2283 shost = lpfc_shost_from_vport(vport);
2284 spin_lock_irq(shost->host_lock);
2285 vport->vpi_state |= LPFC_VPI_REGISTERED;
2286 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2287 spin_unlock_irq(shost->host_lock);
695a814e
JS
2288 }
2289
d439d286
JS
2290 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2291 ndlp = (struct lpfc_nodelist *)pmb->context2;
2292 lpfc_nlp_put(ndlp);
2293 pmb->context2 = NULL;
2294 }
2295
dcf2a4e0
JS
2296 /* Check security permission status on INIT_LINK mailbox command */
2297 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2298 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2299 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2300 "2860 SLI authentication is required "
2301 "for INIT_LINK but has not done yet\n");
2302
04c68496
JS
2303 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2304 lpfc_sli4_mbox_cmd_free(phba, pmb);
2305 else
2306 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2307}
be6bb941
JS
2308 /**
2309 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2310 * @phba: Pointer to HBA context object.
2311 * @pmb: Pointer to mailbox object.
2312 *
2313 * This function is the unreg rpi mailbox completion handler. It
2314 * frees the memory resources associated with the completed mailbox
2315 * command. An additional refrenece is put on the ndlp to prevent
2316 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2317 * the unreg mailbox command completes, this routine puts the
2318 * reference back.
2319 *
2320 **/
2321void
2322lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2323{
2324 struct lpfc_vport *vport = pmb->vport;
2325 struct lpfc_nodelist *ndlp;
2326
2327 ndlp = pmb->context1;
2328 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2329 if (phba->sli_rev == LPFC_SLI_REV4 &&
2330 (bf_get(lpfc_sli_intf_if_type,
2331 &phba->sli4_hba.sli_intf) ==
2332 LPFC_SLI_INTF_IF_TYPE_2)) {
2333 if (ndlp) {
2334 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2335 "0010 UNREG_LOGIN vpi:%x "
2336 "rpi:%x DID:%x map:%x %p\n",
2337 vport->vpi, ndlp->nlp_rpi,
2338 ndlp->nlp_DID,
2339 ndlp->nlp_usg_map, ndlp);
7c5e518c 2340 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2341 lpfc_nlp_put(ndlp);
2342 }
2343 }
2344 }
2345
2346 mempool_free(pmb, phba->mbox_mem_pool);
2347}
dea3101e 2348
e59058c4 2349/**
3621a710 2350 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2351 * @phba: Pointer to HBA context object.
2352 *
2353 * This function is called with no lock held. This function processes all
2354 * the completed mailbox commands and gives it to upper layers. The interrupt
2355 * service routine processes mailbox completion interrupt and adds completed
2356 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2357 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2358 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2359 * function returns the mailbox commands to the upper layer by calling the
2360 * completion handler function of each mailbox.
2361 **/
dea3101e 2362int
2e0fef85 2363lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2364{
92d7f7b0 2365 MAILBOX_t *pmbox;
dea3101e 2366 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2367 int rc;
2368 LIST_HEAD(cmplq);
dea3101e
JB
2369
2370 phba->sli.slistat.mbox_event++;
2371
92d7f7b0
JS
2372 /* Get all completed mailboxe buffers into the cmplq */
2373 spin_lock_irq(&phba->hbalock);
2374 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2375 spin_unlock_irq(&phba->hbalock);
dea3101e 2376
92d7f7b0
JS
2377 /* Get a Mailbox buffer to setup mailbox commands for callback */
2378 do {
2379 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2380 if (pmb == NULL)
2381 break;
2e0fef85 2382
04c68496 2383 pmbox = &pmb->u.mb;
dea3101e 2384
858c9f6c
JS
2385 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2386 if (pmb->vport) {
2387 lpfc_debugfs_disc_trc(pmb->vport,
2388 LPFC_DISC_TRC_MBOX_VPORT,
2389 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2390 (uint32_t)pmbox->mbxCommand,
2391 pmbox->un.varWords[0],
2392 pmbox->un.varWords[1]);
2393 }
2394 else {
2395 lpfc_debugfs_disc_trc(phba->pport,
2396 LPFC_DISC_TRC_MBOX,
2397 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2398 (uint32_t)pmbox->mbxCommand,
2399 pmbox->un.varWords[0],
2400 pmbox->un.varWords[1]);
2401 }
2402 }
2403
dea3101e
JB
2404 /*
2405 * It is a fatal error if unknown mbox command completion.
2406 */
2407 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2408 MBX_SHUTDOWN) {
af901ca1 2409 /* Unknown mailbox command compl */
92d7f7b0 2410 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2411 "(%d):0323 Unknown Mailbox command "
a183a15f 2412 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2413 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2414 pmbox->mbxCommand,
a183a15f
JS
2415 lpfc_sli_config_mbox_subsys_get(phba,
2416 pmb),
2417 lpfc_sli_config_mbox_opcode_get(phba,
2418 pmb));
2e0fef85 2419 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2420 phba->work_hs = HS_FFER3;
2421 lpfc_handle_eratt(phba);
92d7f7b0 2422 continue;
dea3101e
JB
2423 }
2424
dea3101e
JB
2425 if (pmbox->mbxStatus) {
2426 phba->sli.slistat.mbox_stat_err++;
2427 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2428 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2429 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2430 LOG_MBOX | LOG_SLI,
2431 "(%d):0305 Mbox cmd cmpl "
2432 "error - RETRYing Data: x%x "
2433 "(x%x/x%x) x%x x%x x%x\n",
2434 pmb->vport ? pmb->vport->vpi : 0,
2435 pmbox->mbxCommand,
2436 lpfc_sli_config_mbox_subsys_get(phba,
2437 pmb),
2438 lpfc_sli_config_mbox_opcode_get(phba,
2439 pmb),
2440 pmbox->mbxStatus,
2441 pmbox->un.varWords[0],
2442 pmb->vport->port_state);
dea3101e
JB
2443 pmbox->mbxStatus = 0;
2444 pmbox->mbxOwner = OWN_HOST;
dea3101e 2445 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2446 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2447 continue;
dea3101e
JB
2448 }
2449 }
2450
2451 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2452 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2453 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2454 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2455 "x%x x%x x%x\n",
92d7f7b0 2456 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2457 pmbox->mbxCommand,
a183a15f
JS
2458 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2459 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2460 pmb->mbox_cmpl,
2461 *((uint32_t *) pmbox),
2462 pmbox->un.varWords[0],
2463 pmbox->un.varWords[1],
2464 pmbox->un.varWords[2],
2465 pmbox->un.varWords[3],
2466 pmbox->un.varWords[4],
2467 pmbox->un.varWords[5],
2468 pmbox->un.varWords[6],
e74c03c8
JS
2469 pmbox->un.varWords[7],
2470 pmbox->un.varWords[8],
2471 pmbox->un.varWords[9],
2472 pmbox->un.varWords[10]);
dea3101e 2473
92d7f7b0 2474 if (pmb->mbox_cmpl)
dea3101e 2475 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2476 } while (1);
2477 return 0;
2478}
dea3101e 2479
e59058c4 2480/**
3621a710 2481 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2482 * @phba: Pointer to HBA context object.
2483 * @pring: Pointer to driver SLI ring object.
2484 * @tag: buffer tag.
2485 *
2486 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2487 * is set in the tag the buffer is posted for a particular exchange,
2488 * the function will return the buffer without replacing the buffer.
2489 * If the buffer is for unsolicited ELS or CT traffic, this function
2490 * returns the buffer and also posts another buffer to the firmware.
2491 **/
76bb24ef
JS
2492static struct lpfc_dmabuf *
2493lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2494 struct lpfc_sli_ring *pring,
2495 uint32_t tag)
76bb24ef 2496{
9f1e1b50
JS
2497 struct hbq_dmabuf *hbq_entry;
2498
76bb24ef
JS
2499 if (tag & QUE_BUFTAG_BIT)
2500 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2501 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2502 if (!hbq_entry)
2503 return NULL;
2504 return &hbq_entry->dbuf;
76bb24ef 2505}
57127f15 2506
3772a991
JS
2507/**
2508 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2509 * @phba: Pointer to HBA context object.
2510 * @pring: Pointer to driver SLI ring object.
2511 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2512 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2513 * @fch_type: the type for the first frame of the sequence.
2514 *
2515 * This function is called with no lock held. This function uses the r_ctl and
2516 * type of the received sequence to find the correct callback function to call
2517 * to process the sequence.
2518 **/
2519static int
2520lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2521 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2522 uint32_t fch_type)
2523{
2524 int i;
2525
f358dd0c
JS
2526 switch (fch_type) {
2527 case FC_TYPE_NVME:
d613b6a7 2528 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2529 return 1;
2530 default:
2531 break;
2532 }
2533
3772a991
JS
2534 /* unSolicited Responses */
2535 if (pring->prt[0].profile) {
2536 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2537 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2538 saveq);
2539 return 1;
2540 }
2541 /* We must search, based on rctl / type
2542 for the right routine */
2543 for (i = 0; i < pring->num_mask; i++) {
2544 if ((pring->prt[i].rctl == fch_r_ctl) &&
2545 (pring->prt[i].type == fch_type)) {
2546 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2547 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2548 (phba, pring, saveq);
2549 return 1;
2550 }
2551 }
2552 return 0;
2553}
e59058c4
JS
2554
2555/**
3621a710 2556 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2557 * @phba: Pointer to HBA context object.
2558 * @pring: Pointer to driver SLI ring object.
2559 * @saveq: Pointer to the unsolicited iocb.
2560 *
2561 * This function is called with no lock held by the ring event handler
2562 * when there is an unsolicited iocb posted to the response ring by the
2563 * firmware. This function gets the buffer associated with the iocbs
2564 * and calls the event handler for the ring. This function handles both
2565 * qring buffers and hbq buffers.
2566 * When the function returns 1 the caller can free the iocb object otherwise
2567 * upper layer functions will free the iocb objects.
2568 **/
dea3101e
JB
2569static int
2570lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2571 struct lpfc_iocbq *saveq)
2572{
2573 IOCB_t * irsp;
2574 WORD5 * w5p;
2575 uint32_t Rctl, Type;
76bb24ef 2576 struct lpfc_iocbq *iocbq;
3163f725 2577 struct lpfc_dmabuf *dmzbuf;
dea3101e 2578
dea3101e 2579 irsp = &(saveq->iocb);
57127f15
JS
2580
2581 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2582 if (pring->lpfc_sli_rcv_async_status)
2583 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2584 else
2585 lpfc_printf_log(phba,
2586 KERN_WARNING,
2587 LOG_SLI,
2588 "0316 Ring %d handler: unexpected "
2589 "ASYNC_STATUS iocb received evt_code "
2590 "0x%x\n",
2591 pring->ringno,
2592 irsp->un.asyncstat.evt_code);
2593 return 1;
2594 }
2595
3163f725
JS
2596 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2597 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2598 if (irsp->ulpBdeCount > 0) {
2599 dmzbuf = lpfc_sli_get_buff(phba, pring,
2600 irsp->un.ulpWord[3]);
2601 lpfc_in_buf_free(phba, dmzbuf);
2602 }
2603
2604 if (irsp->ulpBdeCount > 1) {
2605 dmzbuf = lpfc_sli_get_buff(phba, pring,
2606 irsp->unsli3.sli3Words[3]);
2607 lpfc_in_buf_free(phba, dmzbuf);
2608 }
2609
2610 if (irsp->ulpBdeCount > 2) {
2611 dmzbuf = lpfc_sli_get_buff(phba, pring,
2612 irsp->unsli3.sli3Words[7]);
2613 lpfc_in_buf_free(phba, dmzbuf);
2614 }
2615
2616 return 1;
2617 }
2618
92d7f7b0 2619 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2620 if (irsp->ulpBdeCount != 0) {
2621 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2622 irsp->un.ulpWord[3]);
2623 if (!saveq->context2)
2624 lpfc_printf_log(phba,
2625 KERN_ERR,
2626 LOG_SLI,
2627 "0341 Ring %d Cannot find buffer for "
2628 "an unsolicited iocb. tag 0x%x\n",
2629 pring->ringno,
2630 irsp->un.ulpWord[3]);
76bb24ef
JS
2631 }
2632 if (irsp->ulpBdeCount == 2) {
2633 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2634 irsp->unsli3.sli3Words[7]);
2635 if (!saveq->context3)
2636 lpfc_printf_log(phba,
2637 KERN_ERR,
2638 LOG_SLI,
2639 "0342 Ring %d Cannot find buffer for an"
2640 " unsolicited iocb. tag 0x%x\n",
2641 pring->ringno,
2642 irsp->unsli3.sli3Words[7]);
2643 }
2644 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2645 irsp = &(iocbq->iocb);
76bb24ef
JS
2646 if (irsp->ulpBdeCount != 0) {
2647 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2648 irsp->un.ulpWord[3]);
9c2face6 2649 if (!iocbq->context2)
76bb24ef
JS
2650 lpfc_printf_log(phba,
2651 KERN_ERR,
2652 LOG_SLI,
2653 "0343 Ring %d Cannot find "
2654 "buffer for an unsolicited iocb"
2655 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2656 irsp->un.ulpWord[3]);
76bb24ef
JS
2657 }
2658 if (irsp->ulpBdeCount == 2) {
2659 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2660 irsp->unsli3.sli3Words[7]);
9c2face6 2661 if (!iocbq->context3)
76bb24ef
JS
2662 lpfc_printf_log(phba,
2663 KERN_ERR,
2664 LOG_SLI,
2665 "0344 Ring %d Cannot find "
2666 "buffer for an unsolicited "
2667 "iocb. tag 0x%x\n",
2668 pring->ringno,
2669 irsp->unsli3.sli3Words[7]);
2670 }
2671 }
92d7f7b0 2672 }
9c2face6
JS
2673 if (irsp->ulpBdeCount != 0 &&
2674 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2675 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2676 int found = 0;
2677
2678 /* search continue save q for same XRI */
2679 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2680 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2681 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2682 list_add_tail(&saveq->list, &iocbq->list);
2683 found = 1;
2684 break;
2685 }
2686 }
2687 if (!found)
2688 list_add_tail(&saveq->clist,
2689 &pring->iocb_continue_saveq);
2690 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2691 list_del_init(&iocbq->clist);
2692 saveq = iocbq;
2693 irsp = &(saveq->iocb);
2694 } else
2695 return 0;
2696 }
2697 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2698 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2699 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2700 Rctl = FC_RCTL_ELS_REQ;
2701 Type = FC_TYPE_ELS;
9c2face6
JS
2702 } else {
2703 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2704 Rctl = w5p->hcsw.Rctl;
2705 Type = w5p->hcsw.Type;
2706
2707 /* Firmware Workaround */
2708 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2709 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2710 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2711 Rctl = FC_RCTL_ELS_REQ;
2712 Type = FC_TYPE_ELS;
9c2face6
JS
2713 w5p->hcsw.Rctl = Rctl;
2714 w5p->hcsw.Type = Type;
2715 }
2716 }
92d7f7b0 2717
3772a991 2718 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2719 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2720 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2721 "Type x%x received\n",
e8b62011 2722 pring->ringno, Rctl, Type);
3772a991 2723
92d7f7b0 2724 return 1;
dea3101e
JB
2725}
2726
e59058c4 2727/**
3621a710 2728 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2729 * @phba: Pointer to HBA context object.
2730 * @pring: Pointer to driver SLI ring object.
2731 * @prspiocb: Pointer to response iocb object.
2732 *
2733 * This function looks up the iocb_lookup table to get the command iocb
2734 * corresponding to the given response iocb using the iotag of the
341b2aa8
DK
2735 * response iocb. This function is called with the hbalock held
2736 * for sli3 devices or the ring_lock for sli4 devices.
e59058c4
JS
2737 * This function returns the command iocb object if it finds the command
2738 * iocb else returns NULL.
2739 **/
dea3101e 2740static struct lpfc_iocbq *
2e0fef85
JS
2741lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2742 struct lpfc_sli_ring *pring,
2743 struct lpfc_iocbq *prspiocb)
dea3101e 2744{
dea3101e
JB
2745 struct lpfc_iocbq *cmd_iocb = NULL;
2746 uint16_t iotag;
1c2ba475 2747 lockdep_assert_held(&phba->hbalock);
dea3101e 2748
604a3e30
JB
2749 iotag = prspiocb->iocb.ulpIoTag;
2750
2751 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2752 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2753 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2754 /* remove from txcmpl queue list */
2755 list_del_init(&cmd_iocb->list);
4f2e66c6 2756 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2757 return cmd_iocb;
2a9bf3d0 2758 }
dea3101e
JB
2759 }
2760
dea3101e 2761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2762 "0317 iotag x%x is out of "
604a3e30 2763 "range: max iotag x%x wd0 x%x\n",
e8b62011 2764 iotag, phba->sli.last_iotag,
604a3e30 2765 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2766 return NULL;
2767}
2768
3772a991
JS
2769/**
2770 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2771 * @phba: Pointer to HBA context object.
2772 * @pring: Pointer to driver SLI ring object.
2773 * @iotag: IOCB tag.
2774 *
2775 * This function looks up the iocb_lookup table to get the command iocb
2776 * corresponding to the given iotag. This function is called with the
2777 * hbalock held.
2778 * This function returns the command iocb object if it finds the command
2779 * iocb else returns NULL.
2780 **/
2781static struct lpfc_iocbq *
2782lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2783 struct lpfc_sli_ring *pring, uint16_t iotag)
2784{
895427bd 2785 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2786
1c2ba475 2787 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2788 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2789 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2790 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2791 /* remove from txcmpl queue list */
2792 list_del_init(&cmd_iocb->list);
2793 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2794 return cmd_iocb;
2a9bf3d0 2795 }
3772a991 2796 }
89533e9b 2797
3772a991 2798 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2799 "0372 iotag x%x lookup error: max iotag (x%x) "
2800 "iocb_flag x%x\n",
2801 iotag, phba->sli.last_iotag,
2802 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
2803 return NULL;
2804}
2805
e59058c4 2806/**
3621a710 2807 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2808 * @phba: Pointer to HBA context object.
2809 * @pring: Pointer to driver SLI ring object.
2810 * @saveq: Pointer to the response iocb to be processed.
2811 *
2812 * This function is called by the ring event handler for non-fcp
2813 * rings when there is a new response iocb in the response ring.
2814 * The caller is not required to hold any locks. This function
2815 * gets the command iocb associated with the response iocb and
2816 * calls the completion handler for the command iocb. If there
2817 * is no completion handler, the function will free the resources
2818 * associated with command iocb. If the response iocb is for
2819 * an already aborted command iocb, the status of the completion
2820 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2821 * This function always returns 1.
2822 **/
dea3101e 2823static int
2e0fef85 2824lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2825 struct lpfc_iocbq *saveq)
2826{
2e0fef85 2827 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2828 int rc = 1;
2829 unsigned long iflag;
2830
2831 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
341b2aa8
DK
2832 if (phba->sli_rev == LPFC_SLI_REV4)
2833 spin_lock_irqsave(&pring->ring_lock, iflag);
2834 else
2835 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2836 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
341b2aa8
DK
2837 if (phba->sli_rev == LPFC_SLI_REV4)
2838 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2839 else
2840 spin_unlock_irqrestore(&phba->hbalock, iflag);
2e0fef85 2841
dea3101e
JB
2842 if (cmdiocbp) {
2843 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2844 /*
2845 * If an ELS command failed send an event to mgmt
2846 * application.
2847 */
2848 if (saveq->iocb.ulpStatus &&
2849 (pring->ringno == LPFC_ELS_RING) &&
2850 (cmdiocbp->iocb.ulpCommand ==
2851 CMD_ELS_REQUEST64_CR))
2852 lpfc_send_els_failure_event(phba,
2853 cmdiocbp, saveq);
2854
dea3101e
JB
2855 /*
2856 * Post all ELS completions to the worker thread.
2857 * All other are passed to the completion callback.
2858 */
2859 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2860 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2861 (cmdiocbp->iocb_flag &
2862 LPFC_DRIVER_ABORTED)) {
2863 spin_lock_irqsave(&phba->hbalock,
2864 iflag);
07951076
JS
2865 cmdiocbp->iocb_flag &=
2866 ~LPFC_DRIVER_ABORTED;
341af102
JS
2867 spin_unlock_irqrestore(&phba->hbalock,
2868 iflag);
07951076
JS
2869 saveq->iocb.ulpStatus =
2870 IOSTAT_LOCAL_REJECT;
2871 saveq->iocb.un.ulpWord[4] =
2872 IOERR_SLI_ABORTED;
0ff10d46
JS
2873
2874 /* Firmware could still be in progress
2875 * of DMAing payload, so don't free data
2876 * buffer till after a hbeat.
2877 */
341af102
JS
2878 spin_lock_irqsave(&phba->hbalock,
2879 iflag);
0ff10d46 2880 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2881 spin_unlock_irqrestore(&phba->hbalock,
2882 iflag);
2883 }
0f65ff68
JS
2884 if (phba->sli_rev == LPFC_SLI_REV4) {
2885 if (saveq->iocb_flag &
2886 LPFC_EXCHANGE_BUSY) {
2887 /* Set cmdiocb flag for the
2888 * exchange busy so sgl (xri)
2889 * will not be released until
2890 * the abort xri is received
2891 * from hba.
2892 */
2893 spin_lock_irqsave(
2894 &phba->hbalock, iflag);
2895 cmdiocbp->iocb_flag |=
2896 LPFC_EXCHANGE_BUSY;
2897 spin_unlock_irqrestore(
2898 &phba->hbalock, iflag);
2899 }
2900 if (cmdiocbp->iocb_flag &
2901 LPFC_DRIVER_ABORTED) {
2902 /*
2903 * Clear LPFC_DRIVER_ABORTED
2904 * bit in case it was driver
2905 * initiated abort.
2906 */
2907 spin_lock_irqsave(
2908 &phba->hbalock, iflag);
2909 cmdiocbp->iocb_flag &=
2910 ~LPFC_DRIVER_ABORTED;
2911 spin_unlock_irqrestore(
2912 &phba->hbalock, iflag);
2913 cmdiocbp->iocb.ulpStatus =
2914 IOSTAT_LOCAL_REJECT;
2915 cmdiocbp->iocb.un.ulpWord[4] =
2916 IOERR_ABORT_REQUESTED;
2917 /*
2918 * For SLI4, irsiocb contains
2919 * NO_XRI in sli_xritag, it
2920 * shall not affect releasing
2921 * sgl (xri) process.
2922 */
2923 saveq->iocb.ulpStatus =
2924 IOSTAT_LOCAL_REJECT;
2925 saveq->iocb.un.ulpWord[4] =
2926 IOERR_SLI_ABORTED;
2927 spin_lock_irqsave(
2928 &phba->hbalock, iflag);
2929 saveq->iocb_flag |=
2930 LPFC_DELAY_MEM_FREE;
2931 spin_unlock_irqrestore(
2932 &phba->hbalock, iflag);
2933 }
07951076 2934 }
dea3101e 2935 }
2e0fef85 2936 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2937 } else
2938 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2939 } else {
2940 /*
2941 * Unknown initiating command based on the response iotag.
2942 * This could be the case on the ELS ring because of
2943 * lpfc_els_abort().
2944 */
2945 if (pring->ringno != LPFC_ELS_RING) {
2946 /*
2947 * Ring <ringno> handler: unexpected completion IoTag
2948 * <IoTag>
2949 */
a257bf90 2950 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2951 "0322 Ring %d handler: "
2952 "unexpected completion IoTag x%x "
2953 "Data: x%x x%x x%x x%x\n",
2954 pring->ringno,
2955 saveq->iocb.ulpIoTag,
2956 saveq->iocb.ulpStatus,
2957 saveq->iocb.un.ulpWord[4],
2958 saveq->iocb.ulpCommand,
2959 saveq->iocb.ulpContext);
dea3101e
JB
2960 }
2961 }
68876920 2962
dea3101e
JB
2963 return rc;
2964}
2965
e59058c4 2966/**
3621a710 2967 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2968 * @phba: Pointer to HBA context object.
2969 * @pring: Pointer to driver SLI ring object.
2970 *
2971 * This function is called from the iocb ring event handlers when
2972 * put pointer is ahead of the get pointer for a ring. This function signal
2973 * an error attention condition to the worker thread and the worker
2974 * thread will transition the HBA to offline state.
2975 **/
2e0fef85
JS
2976static void
2977lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2978{
34b02dcd 2979 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2980 /*
025dfdaf 2981 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2982 * rsp ring <portRspMax>
2983 */
2984 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2985 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2986 "is bigger than rsp ring %d\n",
e8b62011 2987 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2988 pring->sli.sli3.numRiocb);
875fbdfe 2989
2e0fef85 2990 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2991
2992 /*
2993 * All error attention handlers are posted to
2994 * worker thread
2995 */
2996 phba->work_ha |= HA_ERATT;
2997 phba->work_hs = HS_FFER3;
92d7f7b0 2998
5e9d9b82 2999 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
3000
3001 return;
3002}
3003
9399627f 3004/**
3621a710 3005 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
3006 * @ptr: Pointer to address of HBA context object.
3007 *
3008 * This function is invoked by the Error Attention polling timer when the
3009 * timer times out. It will check the SLI Error Attention register for
3010 * possible attention events. If so, it will post an Error Attention event
3011 * and wake up worker thread to process it. Otherwise, it will set up the
3012 * Error Attention polling timer for the next poll.
3013 **/
f22eb4d3 3014void lpfc_poll_eratt(struct timer_list *t)
9399627f
JS
3015{
3016 struct lpfc_hba *phba;
eb016566 3017 uint32_t eratt = 0;
aa6fbb75 3018 uint64_t sli_intr, cnt;
9399627f 3019
f22eb4d3 3020 phba = from_timer(phba, t, eratt_poll);
9399627f 3021
aa6fbb75
JS
3022 /* Here we will also keep track of interrupts per sec of the hba */
3023 sli_intr = phba->sli.slistat.sli_intr;
3024
3025 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3026 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3027 sli_intr);
3028 else
3029 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3030
65791f1f
JS
3031 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3032 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3033 phba->sli.slistat.sli_ips = cnt;
3034
3035 phba->sli.slistat.sli_prev_intr = sli_intr;
3036
9399627f
JS
3037 /* Check chip HA register for error event */
3038 eratt = lpfc_sli_check_eratt(phba);
3039
3040 if (eratt)
3041 /* Tell the worker thread there is work to do */
3042 lpfc_worker_wake_up(phba);
3043 else
3044 /* Restart the timer for next eratt poll */
256ec0d0
JS
3045 mod_timer(&phba->eratt_poll,
3046 jiffies +
65791f1f 3047 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3048 return;
3049}
3050
875fbdfe 3051
e59058c4 3052/**
3621a710 3053 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3054 * @phba: Pointer to HBA context object.
3055 * @pring: Pointer to driver SLI ring object.
3056 * @mask: Host attention register mask for this ring.
3057 *
3058 * This function is called from the interrupt context when there is a ring
3059 * event for the fcp ring. The caller does not hold any lock.
3060 * The function processes each response iocb in the response ring until it
25985edc 3061 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3062 * LE bit set. The function will call the completion handler of the command iocb
3063 * if the response iocb indicates a completion for a command iocb or it is
3064 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3065 * function if this is an unsolicited iocb.
dea3101e 3066 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3067 * to check it explicitly.
3068 */
3069int
2e0fef85
JS
3070lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3071 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3072{
34b02dcd 3073 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3074 IOCB_t *irsp = NULL;
87f6eaff 3075 IOCB_t *entry = NULL;
dea3101e
JB
3076 struct lpfc_iocbq *cmdiocbq = NULL;
3077 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3078 uint32_t status;
3079 uint32_t portRspPut, portRspMax;
3080 int rc = 1;
3081 lpfc_iocb_type type;
3082 unsigned long iflag;
3083 uint32_t rsp_cmpl = 0;
dea3101e 3084
2e0fef85 3085 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3086 pring->stats.iocb_event++;
3087
dea3101e
JB
3088 /*
3089 * The next available response entry should never exceed the maximum
3090 * entries. If it does, treat it as an adapter hardware error.
3091 */
7e56aa25 3092 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3093 portRspPut = le32_to_cpu(pgp->rspPutInx);
3094 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3095 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3096 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3097 return 1;
3098 }
45ed1190
JS
3099 if (phba->fcp_ring_in_use) {
3100 spin_unlock_irqrestore(&phba->hbalock, iflag);
3101 return 1;
3102 } else
3103 phba->fcp_ring_in_use = 1;
dea3101e
JB
3104
3105 rmb();
7e56aa25 3106 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3107 /*
3108 * Fetch an entry off the ring and copy it into a local data
3109 * structure. The copy involves a byte-swap since the
3110 * network byte order and pci byte orders are different.
3111 */
ed957684 3112 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3113 phba->last_completion_time = jiffies;
875fbdfe 3114
7e56aa25
JS
3115 if (++pring->sli.sli3.rspidx >= portRspMax)
3116 pring->sli.sli3.rspidx = 0;
875fbdfe 3117
87f6eaff
JSEC
3118 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3119 (uint32_t *) &rspiocbq.iocb,
ed957684 3120 phba->iocb_rsp_size);
a4bc3379 3121 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3122 irsp = &rspiocbq.iocb;
3123
dea3101e
JB
3124 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3125 pring->stats.iocb_rsp++;
3126 rsp_cmpl++;
3127
3128 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3129 /*
3130 * If resource errors reported from HBA, reduce
3131 * queuedepths of the SCSI device.
3132 */
3133 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3134 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3135 IOERR_NO_RESOURCES)) {
92d7f7b0 3136 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3137 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3138 spin_lock_irqsave(&phba->hbalock, iflag);
3139 }
3140
dea3101e
JB
3141 /* Rsp ring <ringno> error: IOCB */
3142 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3143 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3144 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3145 pring->ringno,
92d7f7b0
JS
3146 irsp->un.ulpWord[0],
3147 irsp->un.ulpWord[1],
3148 irsp->un.ulpWord[2],
3149 irsp->un.ulpWord[3],
3150 irsp->un.ulpWord[4],
3151 irsp->un.ulpWord[5],
d7c255b2
JS
3152 *(uint32_t *)&irsp->un1,
3153 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3154 }
3155
3156 switch (type) {
3157 case LPFC_ABORT_IOCB:
3158 case LPFC_SOL_IOCB:
3159 /*
3160 * Idle exchange closed via ABTS from port. No iocb
3161 * resources need to be recovered.
3162 */
3163 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3164 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3165 "0333 IOCB cmd 0x%x"
dca9479b 3166 " processed. Skipping"
92d7f7b0 3167 " completion\n",
dca9479b 3168 irsp->ulpCommand);
dea3101e
JB
3169 break;
3170 }
3171
604a3e30
JB
3172 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3173 &rspiocbq);
0f65ff68
JS
3174 if (unlikely(!cmdiocbq))
3175 break;
3176 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3177 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3178 if (cmdiocbq->iocb_cmpl) {
3179 spin_unlock_irqrestore(&phba->hbalock, iflag);
3180 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3181 &rspiocbq);
3182 spin_lock_irqsave(&phba->hbalock, iflag);
3183 }
dea3101e 3184 break;
a4bc3379 3185 case LPFC_UNSOL_IOCB:
2e0fef85 3186 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3187 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3188 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3189 break;
dea3101e
JB
3190 default:
3191 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3192 char adaptermsg[LPFC_MAX_ADPTMSG];
3193 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3194 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3195 MAX_MSG_DATA);
898eb71c
JP
3196 dev_warn(&((phba->pcidev)->dev),
3197 "lpfc%d: %s\n",
dea3101e
JB
3198 phba->brd_no, adaptermsg);
3199 } else {
3200 /* Unknown IOCB command */
3201 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3202 "0334 Unknown IOCB command "
92d7f7b0 3203 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3204 type, irsp->ulpCommand,
92d7f7b0
JS
3205 irsp->ulpStatus,
3206 irsp->ulpIoTag,
3207 irsp->ulpContext);
dea3101e
JB
3208 }
3209 break;
3210 }
3211
3212 /*
3213 * The response IOCB has been processed. Update the ring
3214 * pointer in SLIM. If the port response put pointer has not
3215 * been updated, sync the pgp->rspPutInx and fetch the new port
3216 * response put pointer.
3217 */
7e56aa25
JS
3218 writel(pring->sli.sli3.rspidx,
3219 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3220
7e56aa25 3221 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3222 portRspPut = le32_to_cpu(pgp->rspPutInx);
3223 }
3224
3225 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3226 pring->stats.iocb_rsp_full++;
3227 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3228 writel(status, phba->CAregaddr);
3229 readl(phba->CAregaddr);
3230 }
3231 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3232 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3233 pring->stats.iocb_cmd_empty++;
3234
3235 /* Force update of the local copy of cmdGetInx */
7e56aa25 3236 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3237 lpfc_sli_resume_iocb(phba, pring);
3238
3239 if ((pring->lpfc_sli_cmd_available))
3240 (pring->lpfc_sli_cmd_available) (phba, pring);
3241
3242 }
3243
45ed1190 3244 phba->fcp_ring_in_use = 0;
2e0fef85 3245 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3246 return rc;
3247}
3248
e59058c4 3249/**
3772a991
JS
3250 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3251 * @phba: Pointer to HBA context object.
3252 * @pring: Pointer to driver SLI ring object.
3253 * @rspiocbp: Pointer to driver response IOCB object.
3254 *
3255 * This function is called from the worker thread when there is a slow-path
3256 * response IOCB to process. This function chains all the response iocbs until
3257 * seeing the iocb with the LE bit set. The function will call
3258 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3259 * completion of a command iocb. The function will call the
3260 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3261 * The function frees the resources or calls the completion handler if this
3262 * iocb is an abort completion. The function returns NULL when the response
3263 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3264 * this function shall chain the iocb on to the iocb_continueq and return the
3265 * response iocb passed in.
3266 **/
3267static struct lpfc_iocbq *
3268lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3269 struct lpfc_iocbq *rspiocbp)
3270{
3271 struct lpfc_iocbq *saveq;
3272 struct lpfc_iocbq *cmdiocbp;
3273 struct lpfc_iocbq *next_iocb;
3274 IOCB_t *irsp = NULL;
3275 uint32_t free_saveq;
3276 uint8_t iocb_cmd_type;
3277 lpfc_iocb_type type;
3278 unsigned long iflag;
3279 int rc;
3280
3281 spin_lock_irqsave(&phba->hbalock, iflag);
3282 /* First add the response iocb to the countinueq list */
3283 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3284 pring->iocb_continueq_cnt++;
3285
70f23fd6 3286 /* Now, determine whether the list is completed for processing */
3772a991
JS
3287 irsp = &rspiocbp->iocb;
3288 if (irsp->ulpLe) {
3289 /*
3290 * By default, the driver expects to free all resources
3291 * associated with this iocb completion.
3292 */
3293 free_saveq = 1;
3294 saveq = list_get_first(&pring->iocb_continueq,
3295 struct lpfc_iocbq, list);
3296 irsp = &(saveq->iocb);
3297 list_del_init(&pring->iocb_continueq);
3298 pring->iocb_continueq_cnt = 0;
3299
3300 pring->stats.iocb_rsp++;
3301
3302 /*
3303 * If resource errors reported from HBA, reduce
3304 * queuedepths of the SCSI device.
3305 */
3306 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3307 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3308 IOERR_NO_RESOURCES)) {
3772a991
JS
3309 spin_unlock_irqrestore(&phba->hbalock, iflag);
3310 phba->lpfc_rampdown_queue_depth(phba);
3311 spin_lock_irqsave(&phba->hbalock, iflag);
3312 }
3313
3314 if (irsp->ulpStatus) {
3315 /* Rsp ring <ringno> error: IOCB */
3316 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3317 "0328 Rsp Ring %d error: "
3318 "IOCB Data: "
3319 "x%x x%x x%x x%x "
3320 "x%x x%x x%x x%x "
3321 "x%x x%x x%x x%x "
3322 "x%x x%x x%x x%x\n",
3323 pring->ringno,
3324 irsp->un.ulpWord[0],
3325 irsp->un.ulpWord[1],
3326 irsp->un.ulpWord[2],
3327 irsp->un.ulpWord[3],
3328 irsp->un.ulpWord[4],
3329 irsp->un.ulpWord[5],
3330 *(((uint32_t *) irsp) + 6),
3331 *(((uint32_t *) irsp) + 7),
3332 *(((uint32_t *) irsp) + 8),
3333 *(((uint32_t *) irsp) + 9),
3334 *(((uint32_t *) irsp) + 10),
3335 *(((uint32_t *) irsp) + 11),
3336 *(((uint32_t *) irsp) + 12),
3337 *(((uint32_t *) irsp) + 13),
3338 *(((uint32_t *) irsp) + 14),
3339 *(((uint32_t *) irsp) + 15));
3340 }
3341
3342 /*
3343 * Fetch the IOCB command type and call the correct completion
3344 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3345 * get freed back to the lpfc_iocb_list by the discovery
3346 * kernel thread.
3347 */
3348 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3349 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3350 switch (type) {
3351 case LPFC_SOL_IOCB:
3352 spin_unlock_irqrestore(&phba->hbalock, iflag);
3353 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3354 spin_lock_irqsave(&phba->hbalock, iflag);
3355 break;
3356
3357 case LPFC_UNSOL_IOCB:
3358 spin_unlock_irqrestore(&phba->hbalock, iflag);
3359 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3360 spin_lock_irqsave(&phba->hbalock, iflag);
3361 if (!rc)
3362 free_saveq = 0;
3363 break;
3364
3365 case LPFC_ABORT_IOCB:
3366 cmdiocbp = NULL;
3367 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3368 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3369 saveq);
3370 if (cmdiocbp) {
3371 /* Call the specified completion routine */
3372 if (cmdiocbp->iocb_cmpl) {
3373 spin_unlock_irqrestore(&phba->hbalock,
3374 iflag);
3375 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3376 saveq);
3377 spin_lock_irqsave(&phba->hbalock,
3378 iflag);
3379 } else
3380 __lpfc_sli_release_iocbq(phba,
3381 cmdiocbp);
3382 }
3383 break;
3384
3385 case LPFC_UNKNOWN_IOCB:
3386 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3387 char adaptermsg[LPFC_MAX_ADPTMSG];
3388 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3389 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3390 MAX_MSG_DATA);
3391 dev_warn(&((phba->pcidev)->dev),
3392 "lpfc%d: %s\n",
3393 phba->brd_no, adaptermsg);
3394 } else {
3395 /* Unknown IOCB command */
3396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3397 "0335 Unknown IOCB "
3398 "command Data: x%x "
3399 "x%x x%x x%x\n",
3400 irsp->ulpCommand,
3401 irsp->ulpStatus,
3402 irsp->ulpIoTag,
3403 irsp->ulpContext);
3404 }
3405 break;
3406 }
3407
3408 if (free_saveq) {
3409 list_for_each_entry_safe(rspiocbp, next_iocb,
3410 &saveq->list, list) {
61f35bff 3411 list_del_init(&rspiocbp->list);
3772a991
JS
3412 __lpfc_sli_release_iocbq(phba, rspiocbp);
3413 }
3414 __lpfc_sli_release_iocbq(phba, saveq);
3415 }
3416 rspiocbp = NULL;
3417 }
3418 spin_unlock_irqrestore(&phba->hbalock, iflag);
3419 return rspiocbp;
3420}
3421
3422/**
3423 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3424 * @phba: Pointer to HBA context object.
3425 * @pring: Pointer to driver SLI ring object.
3426 * @mask: Host attention register mask for this ring.
3427 *
3772a991
JS
3428 * This routine wraps the actual slow_ring event process routine from the
3429 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3430 **/
3772a991 3431void
2e0fef85
JS
3432lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3433 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3434{
3435 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3436}
3437
3438/**
3439 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3440 * @phba: Pointer to HBA context object.
3441 * @pring: Pointer to driver SLI ring object.
3442 * @mask: Host attention register mask for this ring.
3443 *
3444 * This function is called from the worker thread when there is a ring event
3445 * for non-fcp rings. The caller does not hold any lock. The function will
3446 * remove each response iocb in the response ring and calls the handle
3447 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3448 **/
3449static void
3450lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3451 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3452{
34b02dcd 3453 struct lpfc_pgp *pgp;
dea3101e
JB
3454 IOCB_t *entry;
3455 IOCB_t *irsp = NULL;
3456 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3457 uint32_t portRspPut, portRspMax;
dea3101e 3458 unsigned long iflag;
3772a991 3459 uint32_t status;
dea3101e 3460
34b02dcd 3461 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3462 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3463 pring->stats.iocb_event++;
3464
dea3101e
JB
3465 /*
3466 * The next available response entry should never exceed the maximum
3467 * entries. If it does, treat it as an adapter hardware error.
3468 */
7e56aa25 3469 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3470 portRspPut = le32_to_cpu(pgp->rspPutInx);
3471 if (portRspPut >= portRspMax) {
3472 /*
025dfdaf 3473 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3474 * rsp ring <portRspMax>
3475 */
ed957684 3476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3477 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3478 "is bigger than rsp ring %d\n",
e8b62011 3479 pring->ringno, portRspPut, portRspMax);
dea3101e 3480
2e0fef85
JS
3481 phba->link_state = LPFC_HBA_ERROR;
3482 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3483
3484 phba->work_hs = HS_FFER3;
3485 lpfc_handle_eratt(phba);
3486
3772a991 3487 return;
dea3101e
JB
3488 }
3489
3490 rmb();
7e56aa25 3491 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3492 /*
3493 * Build a completion list and call the appropriate handler.
3494 * The process is to get the next available response iocb, get
3495 * a free iocb from the list, copy the response data into the
3496 * free iocb, insert to the continuation list, and update the
3497 * next response index to slim. This process makes response
3498 * iocb's in the ring available to DMA as fast as possible but
3499 * pays a penalty for a copy operation. Since the iocb is
3500 * only 32 bytes, this penalty is considered small relative to
3501 * the PCI reads for register values and a slim write. When
3502 * the ulpLe field is set, the entire Command has been
3503 * received.
3504 */
ed957684
JS
3505 entry = lpfc_resp_iocb(phba, pring);
3506
858c9f6c 3507 phba->last_completion_time = jiffies;
2e0fef85 3508 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3509 if (rspiocbp == NULL) {
3510 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3511 "completion.\n", __func__);
dea3101e
JB
3512 break;
3513 }
3514
ed957684
JS
3515 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3516 phba->iocb_rsp_size);
dea3101e
JB
3517 irsp = &rspiocbp->iocb;
3518
7e56aa25
JS
3519 if (++pring->sli.sli3.rspidx >= portRspMax)
3520 pring->sli.sli3.rspidx = 0;
dea3101e 3521
a58cbd52
JS
3522 if (pring->ringno == LPFC_ELS_RING) {
3523 lpfc_debugfs_slow_ring_trc(phba,
3524 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3525 *(((uint32_t *) irsp) + 4),
3526 *(((uint32_t *) irsp) + 6),
3527 *(((uint32_t *) irsp) + 7));
3528 }
3529
7e56aa25
JS
3530 writel(pring->sli.sli3.rspidx,
3531 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3532
3772a991
JS
3533 spin_unlock_irqrestore(&phba->hbalock, iflag);
3534 /* Handle the response IOCB */
3535 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3536 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3537
3538 /*
3539 * If the port response put pointer has not been updated, sync
3540 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3541 * response put pointer.
3542 */
7e56aa25 3543 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3544 portRspPut = le32_to_cpu(pgp->rspPutInx);
3545 }
7e56aa25 3546 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3547
92d7f7b0 3548 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3549 /* At least one response entry has been freed */
3550 pring->stats.iocb_rsp_full++;
3551 /* SET RxRE_RSP in Chip Att register */
3552 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3553 writel(status, phba->CAregaddr);
3554 readl(phba->CAregaddr); /* flush */
3555 }
3556 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3557 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3558 pring->stats.iocb_cmd_empty++;
3559
3560 /* Force update of the local copy of cmdGetInx */
7e56aa25 3561 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3562 lpfc_sli_resume_iocb(phba, pring);
3563
3564 if ((pring->lpfc_sli_cmd_available))
3565 (pring->lpfc_sli_cmd_available) (phba, pring);
3566
3567 }
3568
2e0fef85 3569 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3570 return;
dea3101e
JB
3571}
3572
4f774513
JS
3573/**
3574 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3575 * @phba: Pointer to HBA context object.
3576 * @pring: Pointer to driver SLI ring object.
3577 * @mask: Host attention register mask for this ring.
3578 *
3579 * This function is called from the worker thread when there is a pending
3580 * ELS response iocb on the driver internal slow-path response iocb worker
3581 * queue. The caller does not hold any lock. The function will remove each
3582 * response iocb from the response worker queue and calls the handle
3583 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3584 **/
3585static void
3586lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3587 struct lpfc_sli_ring *pring, uint32_t mask)
3588{
3589 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3590 struct hbq_dmabuf *dmabuf;
3591 struct lpfc_cq_event *cq_event;
4f774513
JS
3592 unsigned long iflag;
3593
45ed1190
JS
3594 spin_lock_irqsave(&phba->hbalock, iflag);
3595 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3596 spin_unlock_irqrestore(&phba->hbalock, iflag);
3597 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3598 /* Get the response iocb from the head of work queue */
3599 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3600 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3601 cq_event, struct lpfc_cq_event, list);
4f774513 3602 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3603
3604 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3605 case CQE_CODE_COMPL_WQE:
3606 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3607 cq_event);
45ed1190
JS
3608 /* Translate ELS WCQE to response IOCBQ */
3609 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3610 irspiocbq);
3611 if (irspiocbq)
3612 lpfc_sli_sp_handle_rspiocb(phba, pring,
3613 irspiocbq);
4d9ab994
JS
3614 break;
3615 case CQE_CODE_RECEIVE:
7851fe2c 3616 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3617 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3618 cq_event);
3619 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3620 break;
3621 default:
3622 break;
3623 }
4f774513
JS
3624 }
3625}
3626
e59058c4 3627/**
3621a710 3628 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3629 * @phba: Pointer to HBA context object.
3630 * @pring: Pointer to driver SLI ring object.
3631 *
3632 * This function aborts all iocbs in the given ring and frees all the iocb
3633 * objects in txq. This function issues an abort iocb for all the iocb commands
3634 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3635 * the return of this function. The caller is not required to hold any locks.
3636 **/
2e0fef85 3637void
dea3101e
JB
3638lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3639{
2534ba75 3640 LIST_HEAD(completions);
dea3101e 3641 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3642
92d7f7b0
JS
3643 if (pring->ringno == LPFC_ELS_RING) {
3644 lpfc_fabric_abort_hba(phba);
3645 }
3646
dea3101e
JB
3647 /* Error everything on txq and txcmplq
3648 * First do the txq.
3649 */
db55fba8
JS
3650 if (phba->sli_rev >= LPFC_SLI_REV4) {
3651 spin_lock_irq(&pring->ring_lock);
3652 list_splice_init(&pring->txq, &completions);
3653 pring->txq_cnt = 0;
3654 spin_unlock_irq(&pring->ring_lock);
dea3101e 3655
db55fba8
JS
3656 spin_lock_irq(&phba->hbalock);
3657 /* Next issue ABTS for everything on the txcmplq */
3658 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3659 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3660 spin_unlock_irq(&phba->hbalock);
3661 } else {
3662 spin_lock_irq(&phba->hbalock);
3663 list_splice_init(&pring->txq, &completions);
3664 pring->txq_cnt = 0;
dea3101e 3665
db55fba8
JS
3666 /* Next issue ABTS for everything on the txcmplq */
3667 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3668 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3669 spin_unlock_irq(&phba->hbalock);
3670 }
dea3101e 3671
a257bf90
JS
3672 /* Cancel all the IOCBs from the completions list */
3673 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3674 IOERR_SLI_ABORTED);
dea3101e
JB
3675}
3676
895427bd
JS
3677/**
3678 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3679 * @phba: Pointer to HBA context object.
3680 * @pring: Pointer to driver SLI ring object.
3681 *
3682 * This function aborts all iocbs in the given ring and frees all the iocb
3683 * objects in txq. This function issues an abort iocb for all the iocb commands
3684 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3685 * the return of this function. The caller is not required to hold any locks.
3686 **/
3687void
3688lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3689{
3690 LIST_HEAD(completions);
3691 struct lpfc_iocbq *iocb, *next_iocb;
3692
3693 if (pring->ringno == LPFC_ELS_RING)
3694 lpfc_fabric_abort_hba(phba);
3695
3696 spin_lock_irq(&phba->hbalock);
3697 /* Next issue ABTS for everything on the txcmplq */
3698 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3699 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3700 spin_unlock_irq(&phba->hbalock);
3701}
3702
3703
db55fba8
JS
3704/**
3705 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3706 * @phba: Pointer to HBA context object.
3707 * @pring: Pointer to driver SLI ring object.
3708 *
3709 * This function aborts all iocbs in FCP rings and frees all the iocb
3710 * objects in txq. This function issues an abort iocb for all the iocb commands
3711 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3712 * the return of this function. The caller is not required to hold any locks.
3713 **/
3714void
3715lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3716{
3717 struct lpfc_sli *psli = &phba->sli;
3718 struct lpfc_sli_ring *pring;
3719 uint32_t i;
3720
3721 /* Look on all the FCP Rings for the iotag */
3722 if (phba->sli_rev >= LPFC_SLI_REV4) {
3723 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3724 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3725 lpfc_sli_abort_iocb_ring(phba, pring);
3726 }
3727 } else {
895427bd 3728 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3729 lpfc_sli_abort_iocb_ring(phba, pring);
3730 }
3731}
3732
895427bd
JS
3733/**
3734 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3735 * @phba: Pointer to HBA context object.
3736 *
3737 * This function aborts all wqes in NVME rings. This function issues an
3738 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3739 * the txcmplq is not guaranteed to complete before the return of this
3740 * function. The caller is not required to hold any locks.
3741 **/
3742void
3743lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3744{
3745 struct lpfc_sli_ring *pring;
3746 uint32_t i;
3747
3748 if (phba->sli_rev < LPFC_SLI_REV4)
3749 return;
3750
3751 /* Abort all IO on each NVME ring. */
3752 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3753 pring = phba->sli4_hba.nvme_wq[i]->pring;
3754 lpfc_sli_abort_wqe_ring(phba, pring);
3755 }
3756}
3757
db55fba8 3758
a8e497d5 3759/**
3621a710 3760 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3761 * @phba: Pointer to HBA context object.
3762 *
3763 * This function flushes all iocbs in the fcp ring and frees all the iocb
3764 * objects in txq and txcmplq. This function will not issue abort iocbs
3765 * for all the iocb commands in txcmplq, they will just be returned with
3766 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3767 * slot has been permanently disabled.
3768 **/
3769void
3770lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3771{
3772 LIST_HEAD(txq);
3773 LIST_HEAD(txcmplq);
a8e497d5
JS
3774 struct lpfc_sli *psli = &phba->sli;
3775 struct lpfc_sli_ring *pring;
db55fba8 3776 uint32_t i;
a8e497d5
JS
3777
3778 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3779 /* Indicate the I/O queues are flushed */
3780 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3781 spin_unlock_irq(&phba->hbalock);
3782
db55fba8
JS
3783 /* Look on all the FCP Rings for the iotag */
3784 if (phba->sli_rev >= LPFC_SLI_REV4) {
3785 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3786 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3787
3788 spin_lock_irq(&pring->ring_lock);
3789 /* Retrieve everything on txq */
3790 list_splice_init(&pring->txq, &txq);
3791 /* Retrieve everything on the txcmplq */
3792 list_splice_init(&pring->txcmplq, &txcmplq);
3793 pring->txq_cnt = 0;
3794 pring->txcmplq_cnt = 0;
3795 spin_unlock_irq(&pring->ring_lock);
3796
3797 /* Flush the txq */
3798 lpfc_sli_cancel_iocbs(phba, &txq,
3799 IOSTAT_LOCAL_REJECT,
3800 IOERR_SLI_DOWN);
3801 /* Flush the txcmpq */
3802 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3803 IOSTAT_LOCAL_REJECT,
3804 IOERR_SLI_DOWN);
3805 }
3806 } else {
895427bd 3807 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 3808
db55fba8
JS
3809 spin_lock_irq(&phba->hbalock);
3810 /* Retrieve everything on txq */
3811 list_splice_init(&pring->txq, &txq);
3812 /* Retrieve everything on the txcmplq */
3813 list_splice_init(&pring->txcmplq, &txcmplq);
3814 pring->txq_cnt = 0;
3815 pring->txcmplq_cnt = 0;
3816 spin_unlock_irq(&phba->hbalock);
3817
3818 /* Flush the txq */
3819 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3820 IOERR_SLI_DOWN);
3821 /* Flush the txcmpq */
3822 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3823 IOERR_SLI_DOWN);
3824 }
a8e497d5
JS
3825}
3826
895427bd
JS
3827/**
3828 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3829 * @phba: Pointer to HBA context object.
3830 *
3831 * This function flushes all wqes in the nvme rings and frees all resources
3832 * in the txcmplq. This function does not issue abort wqes for the IO
3833 * commands in txcmplq, they will just be returned with
3834 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3835 * slot has been permanently disabled.
3836 **/
3837void
3838lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3839{
3840 LIST_HEAD(txcmplq);
3841 struct lpfc_sli_ring *pring;
3842 uint32_t i;
3843
3844 if (phba->sli_rev < LPFC_SLI_REV4)
3845 return;
3846
3847 /* Hint to other driver operations that a flush is in progress. */
3848 spin_lock_irq(&phba->hbalock);
3849 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3850 spin_unlock_irq(&phba->hbalock);
3851
3852 /* Cycle through all NVME rings and complete each IO with
3853 * a local driver reason code. This is a flush so no
3854 * abort exchange to FW.
3855 */
3856 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3857 pring = phba->sli4_hba.nvme_wq[i]->pring;
3858
3859 /* Retrieve everything on the txcmplq */
3860 spin_lock_irq(&pring->ring_lock);
3861 list_splice_init(&pring->txcmplq, &txcmplq);
3862 pring->txcmplq_cnt = 0;
3863 spin_unlock_irq(&pring->ring_lock);
3864
3865 /* Flush the txcmpq &&&PAE */
3866 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3867 IOSTAT_LOCAL_REJECT,
3868 IOERR_SLI_DOWN);
3869 }
3870}
3871
e59058c4 3872/**
3772a991 3873 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3874 * @phba: Pointer to HBA context object.
3875 * @mask: Bit mask to be checked.
3876 *
3877 * This function reads the host status register and compares
3878 * with the provided bit mask to check if HBA completed
3879 * the restart. This function will wait in a loop for the
3880 * HBA to complete restart. If the HBA does not restart within
3881 * 15 iterations, the function will reset the HBA again. The
3882 * function returns 1 when HBA fail to restart otherwise returns
3883 * zero.
3884 **/
3772a991
JS
3885static int
3886lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3887{
41415862
JW
3888 uint32_t status;
3889 int i = 0;
3890 int retval = 0;
dea3101e 3891
41415862 3892 /* Read the HBA Host Status Register */
9940b97b
JS
3893 if (lpfc_readl(phba->HSregaddr, &status))
3894 return 1;
dea3101e 3895
41415862
JW
3896 /*
3897 * Check status register every 100ms for 5 retries, then every
3898 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3899 * every 2.5 sec for 4.
3900 * Break our of the loop if errors occurred during init.
3901 */
3902 while (((status & mask) != mask) &&
3903 !(status & HS_FFERM) &&
3904 i++ < 20) {
dea3101e 3905
41415862
JW
3906 if (i <= 5)
3907 msleep(10);
3908 else if (i <= 10)
3909 msleep(500);
3910 else
3911 msleep(2500);
dea3101e 3912
41415862 3913 if (i == 15) {
2e0fef85 3914 /* Do post */
92d7f7b0 3915 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3916 lpfc_sli_brdrestart(phba);
3917 }
3918 /* Read the HBA Host Status Register */
9940b97b
JS
3919 if (lpfc_readl(phba->HSregaddr, &status)) {
3920 retval = 1;
3921 break;
3922 }
41415862 3923 }
dea3101e 3924
41415862
JW
3925 /* Check to see if any errors occurred during init */
3926 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3928 "2751 Adapter failed to restart, "
3929 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3930 status,
3931 readl(phba->MBslimaddr + 0xa8),
3932 readl(phba->MBslimaddr + 0xac));
2e0fef85 3933 phba->link_state = LPFC_HBA_ERROR;
41415862 3934 retval = 1;
dea3101e 3935 }
dea3101e 3936
41415862
JW
3937 return retval;
3938}
dea3101e 3939
da0436e9
JS
3940/**
3941 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3942 * @phba: Pointer to HBA context object.
3943 * @mask: Bit mask to be checked.
3944 *
3945 * This function checks the host status register to check if HBA is
3946 * ready. This function will wait in a loop for the HBA to be ready
3947 * If the HBA is not ready , the function will will reset the HBA PCI
3948 * function again. The function returns 1 when HBA fail to be ready
3949 * otherwise returns zero.
3950 **/
3951static int
3952lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3953{
3954 uint32_t status;
3955 int retval = 0;
3956
3957 /* Read the HBA Host Status Register */
3958 status = lpfc_sli4_post_status_check(phba);
3959
3960 if (status) {
3961 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3962 lpfc_sli_brdrestart(phba);
3963 status = lpfc_sli4_post_status_check(phba);
3964 }
3965
3966 /* Check to see if any errors occurred during init */
3967 if (status) {
3968 phba->link_state = LPFC_HBA_ERROR;
3969 retval = 1;
3970 } else
3971 phba->sli4_hba.intr_enable = 0;
3972
3973 return retval;
3974}
3975
3976/**
3977 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3978 * @phba: Pointer to HBA context object.
3979 * @mask: Bit mask to be checked.
3980 *
3981 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3982 * from the API jump table function pointer from the lpfc_hba struct.
3983 **/
3984int
3985lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3986{
3987 return phba->lpfc_sli_brdready(phba, mask);
3988}
3989
9290831f
JS
3990#define BARRIER_TEST_PATTERN (0xdeadbeef)
3991
e59058c4 3992/**
3621a710 3993 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3994 * @phba: Pointer to HBA context object.
3995 *
1b51197d
JS
3996 * This function is called before resetting an HBA. This function is called
3997 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 3998 **/
2e0fef85 3999void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 4000{
65a29c16
JS
4001 uint32_t __iomem *resp_buf;
4002 uint32_t __iomem *mbox_buf;
9290831f 4003 volatile uint32_t mbox;
9940b97b 4004 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
4005 int i;
4006 uint8_t hdrtype;
4007
1c2ba475
JT
4008 lockdep_assert_held(&phba->hbalock);
4009
9290831f
JS
4010 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4011 if (hdrtype != 0x80 ||
4012 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4013 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4014 return;
4015
4016 /*
4017 * Tell the other part of the chip to suspend temporarily all
4018 * its DMA activity.
4019 */
65a29c16 4020 resp_buf = phba->MBslimaddr;
9290831f
JS
4021
4022 /* Disable the error attention */
9940b97b
JS
4023 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4024 return;
9290831f
JS
4025 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4026 readl(phba->HCregaddr); /* flush */
2e0fef85 4027 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4028
9940b97b
JS
4029 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4030 return;
4031 if (ha_copy & HA_ERATT) {
9290831f
JS
4032 /* Clear Chip error bit */
4033 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4034 phba->pport->stopped = 1;
9290831f
JS
4035 }
4036
4037 mbox = 0;
4038 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4039 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4040
4041 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4042 mbox_buf = phba->MBslimaddr;
9290831f
JS
4043 writel(mbox, mbox_buf);
4044
9940b97b
JS
4045 for (i = 0; i < 50; i++) {
4046 if (lpfc_readl((resp_buf + 1), &resp_data))
4047 return;
4048 if (resp_data != ~(BARRIER_TEST_PATTERN))
4049 mdelay(1);
4050 else
4051 break;
4052 }
4053 resp_data = 0;
4054 if (lpfc_readl((resp_buf + 1), &resp_data))
4055 return;
4056 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4057 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4058 phba->pport->stopped)
9290831f
JS
4059 goto restore_hc;
4060 else
4061 goto clear_errat;
4062 }
4063
4064 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4065 resp_data = 0;
4066 for (i = 0; i < 500; i++) {
4067 if (lpfc_readl(resp_buf, &resp_data))
4068 return;
4069 if (resp_data != mbox)
4070 mdelay(1);
4071 else
4072 break;
4073 }
9290831f
JS
4074
4075clear_errat:
4076
9940b97b
JS
4077 while (++i < 500) {
4078 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4079 return;
4080 if (!(ha_copy & HA_ERATT))
4081 mdelay(1);
4082 else
4083 break;
4084 }
9290831f
JS
4085
4086 if (readl(phba->HAregaddr) & HA_ERATT) {
4087 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4088 phba->pport->stopped = 1;
9290831f
JS
4089 }
4090
4091restore_hc:
2e0fef85 4092 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4093 writel(hc_copy, phba->HCregaddr);
4094 readl(phba->HCregaddr); /* flush */
4095}
4096
e59058c4 4097/**
3621a710 4098 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4099 * @phba: Pointer to HBA context object.
4100 *
4101 * This function issues a kill_board mailbox command and waits for
4102 * the error attention interrupt. This function is called for stopping
4103 * the firmware processing. The caller is not required to hold any
4104 * locks. This function calls lpfc_hba_down_post function to free
4105 * any pending commands after the kill. The function will return 1 when it
4106 * fails to kill the board else will return 0.
4107 **/
41415862 4108int
2e0fef85 4109lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4110{
4111 struct lpfc_sli *psli;
4112 LPFC_MBOXQ_t *pmb;
4113 uint32_t status;
4114 uint32_t ha_copy;
4115 int retval;
4116 int i = 0;
dea3101e 4117
41415862 4118 psli = &phba->sli;
dea3101e 4119
41415862 4120 /* Kill HBA */
ed957684 4121 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4122 "0329 Kill HBA Data: x%x x%x\n",
4123 phba->pport->port_state, psli->sli_flag);
41415862 4124
98c9ea5c
JS
4125 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4126 if (!pmb)
41415862 4127 return 1;
41415862
JW
4128
4129 /* Disable the error attention */
2e0fef85 4130 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4131 if (lpfc_readl(phba->HCregaddr, &status)) {
4132 spin_unlock_irq(&phba->hbalock);
4133 mempool_free(pmb, phba->mbox_mem_pool);
4134 return 1;
4135 }
41415862
JW
4136 status &= ~HC_ERINT_ENA;
4137 writel(status, phba->HCregaddr);
4138 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4139 phba->link_flag |= LS_IGNORE_ERATT;
4140 spin_unlock_irq(&phba->hbalock);
41415862
JW
4141
4142 lpfc_kill_board(phba, pmb);
4143 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4144 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4145
4146 if (retval != MBX_SUCCESS) {
4147 if (retval != MBX_BUSY)
4148 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4150 "2752 KILL_BOARD command failed retval %d\n",
4151 retval);
2e0fef85
JS
4152 spin_lock_irq(&phba->hbalock);
4153 phba->link_flag &= ~LS_IGNORE_ERATT;
4154 spin_unlock_irq(&phba->hbalock);
41415862
JW
4155 return 1;
4156 }
4157
f4b4c68f
JS
4158 spin_lock_irq(&phba->hbalock);
4159 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4160 spin_unlock_irq(&phba->hbalock);
9290831f 4161
41415862
JW
4162 mempool_free(pmb, phba->mbox_mem_pool);
4163
4164 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4165 * attention every 100ms for 3 seconds. If we don't get ERATT after
4166 * 3 seconds we still set HBA_ERROR state because the status of the
4167 * board is now undefined.
4168 */
9940b97b
JS
4169 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4170 return 1;
41415862
JW
4171 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4172 mdelay(100);
9940b97b
JS
4173 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4174 return 1;
41415862
JW
4175 }
4176
4177 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4178 if (ha_copy & HA_ERATT) {
4179 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4180 phba->pport->stopped = 1;
9290831f 4181 }
2e0fef85 4182 spin_lock_irq(&phba->hbalock);
41415862 4183 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4184 psli->mbox_active = NULL;
2e0fef85
JS
4185 phba->link_flag &= ~LS_IGNORE_ERATT;
4186 spin_unlock_irq(&phba->hbalock);
41415862 4187
41415862 4188 lpfc_hba_down_post(phba);
2e0fef85 4189 phba->link_state = LPFC_HBA_ERROR;
41415862 4190
2e0fef85 4191 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4192}
4193
e59058c4 4194/**
3772a991 4195 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4196 * @phba: Pointer to HBA context object.
4197 *
4198 * This function resets the HBA by writing HC_INITFF to the control
4199 * register. After the HBA resets, this function resets all the iocb ring
4200 * indices. This function disables PCI layer parity checking during
4201 * the reset.
4202 * This function returns 0 always.
4203 * The caller is not required to hold any locks.
4204 **/
41415862 4205int
2e0fef85 4206lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4207{
41415862 4208 struct lpfc_sli *psli;
dea3101e 4209 struct lpfc_sli_ring *pring;
41415862 4210 uint16_t cfg_value;
dea3101e 4211 int i;
dea3101e 4212
41415862 4213 psli = &phba->sli;
dea3101e 4214
41415862
JW
4215 /* Reset HBA */
4216 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4217 "0325 Reset HBA Data: x%x x%x\n",
4492b739
JS
4218 (phba->pport) ? phba->pport->port_state : 0,
4219 psli->sli_flag);
dea3101e
JB
4220
4221 /* perform board reset */
4222 phba->fc_eventTag = 0;
4d9ab994 4223 phba->link_events = 0;
4492b739
JS
4224 if (phba->pport) {
4225 phba->pport->fc_myDID = 0;
4226 phba->pport->fc_prevDID = 0;
4227 }
dea3101e 4228
41415862
JW
4229 /* Turn off parity checking and serr during the physical reset */
4230 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4231 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4232 (cfg_value &
4233 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4234
3772a991
JS
4235 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4236
41415862
JW
4237 /* Now toggle INITFF bit in the Host Control Register */
4238 writel(HC_INITFF, phba->HCregaddr);
4239 mdelay(1);
4240 readl(phba->HCregaddr); /* flush */
4241 writel(0, phba->HCregaddr);
4242 readl(phba->HCregaddr); /* flush */
4243
4244 /* Restore PCI cmd register */
4245 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4246
4247 /* Initialize relevant SLI info */
41415862 4248 for (i = 0; i < psli->num_rings; i++) {
895427bd 4249 pring = &psli->sli3_ring[i];
dea3101e 4250 pring->flag = 0;
7e56aa25
JS
4251 pring->sli.sli3.rspidx = 0;
4252 pring->sli.sli3.next_cmdidx = 0;
4253 pring->sli.sli3.local_getidx = 0;
4254 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4255 pring->missbufcnt = 0;
4256 }
dea3101e 4257
2e0fef85 4258 phba->link_state = LPFC_WARM_START;
41415862
JW
4259 return 0;
4260}
4261
e59058c4 4262/**
da0436e9
JS
4263 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4264 * @phba: Pointer to HBA context object.
4265 *
4266 * This function resets a SLI4 HBA. This function disables PCI layer parity
4267 * checking during resets the device. The caller is not required to hold
4268 * any locks.
4269 *
4270 * This function returns 0 always.
4271 **/
4272int
4273lpfc_sli4_brdreset(struct lpfc_hba *phba)
4274{
4275 struct lpfc_sli *psli = &phba->sli;
4276 uint16_t cfg_value;
0293635e 4277 int rc = 0;
da0436e9
JS
4278
4279 /* Reset HBA */
4280 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4281 "0295 Reset HBA Data: x%x x%x x%x\n",
4282 phba->pport->port_state, psli->sli_flag,
4283 phba->hba_flag);
da0436e9
JS
4284
4285 /* perform board reset */
4286 phba->fc_eventTag = 0;
4d9ab994 4287 phba->link_events = 0;
da0436e9
JS
4288 phba->pport->fc_myDID = 0;
4289 phba->pport->fc_prevDID = 0;
4290
da0436e9
JS
4291 spin_lock_irq(&phba->hbalock);
4292 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4293 phba->fcf.fcf_flag = 0;
da0436e9
JS
4294 spin_unlock_irq(&phba->hbalock);
4295
0293635e
JS
4296 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4297 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4298 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4299 return rc;
4300 }
4301
da0436e9
JS
4302 /* Now physically reset the device */
4303 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4304 "0389 Performing PCI function reset!\n");
be858b65
JS
4305
4306 /* Turn off parity checking and serr during the physical reset */
4307 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4308 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4309 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4310
88318816 4311 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4312 rc = lpfc_pci_function_reset(phba);
da0436e9 4313
be858b65
JS
4314 /* Restore PCI cmd register */
4315 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4316
27b01b82 4317 return rc;
da0436e9
JS
4318}
4319
4320/**
4321 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4322 * @phba: Pointer to HBA context object.
4323 *
4324 * This function is called in the SLI initialization code path to
4325 * restart the HBA. The caller is not required to hold any lock.
4326 * This function writes MBX_RESTART mailbox command to the SLIM and
4327 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4328 * function to free any pending commands. The function enables
4329 * POST only during the first initialization. The function returns zero.
4330 * The function does not guarantee completion of MBX_RESTART mailbox
4331 * command before the return of this function.
4332 **/
da0436e9
JS
4333static int
4334lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4335{
4336 MAILBOX_t *mb;
4337 struct lpfc_sli *psli;
41415862
JW
4338 volatile uint32_t word0;
4339 void __iomem *to_slim;
0d878419 4340 uint32_t hba_aer_enabled;
41415862 4341
2e0fef85 4342 spin_lock_irq(&phba->hbalock);
41415862 4343
0d878419
JS
4344 /* Take PCIe device Advanced Error Reporting (AER) state */
4345 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4346
41415862
JW
4347 psli = &phba->sli;
4348
4349 /* Restart HBA */
4350 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4351 "0337 Restart HBA Data: x%x x%x\n",
4492b739
JS
4352 (phba->pport) ? phba->pport->port_state : 0,
4353 psli->sli_flag);
41415862
JW
4354
4355 word0 = 0;
4356 mb = (MAILBOX_t *) &word0;
4357 mb->mbxCommand = MBX_RESTART;
4358 mb->mbxHc = 1;
4359
9290831f
JS
4360 lpfc_reset_barrier(phba);
4361
41415862
JW
4362 to_slim = phba->MBslimaddr;
4363 writel(*(uint32_t *) mb, to_slim);
4364 readl(to_slim); /* flush */
4365
4366 /* Only skip post after fc_ffinit is completed */
4492b739 4367 if (phba->pport && phba->pport->port_state)
41415862 4368 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4369 else
41415862 4370 word0 = 0; /* This is really setting up word1 */
65a29c16 4371 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4372 writel(*(uint32_t *) mb, to_slim);
4373 readl(to_slim); /* flush */
dea3101e 4374
41415862 4375 lpfc_sli_brdreset(phba);
4492b739
JS
4376 if (phba->pport)
4377 phba->pport->stopped = 0;
2e0fef85 4378 phba->link_state = LPFC_INIT_START;
da0436e9 4379 phba->hba_flag = 0;
2e0fef85 4380 spin_unlock_irq(&phba->hbalock);
41415862 4381
64ba8818
JS
4382 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4383 psli->stats_start = get_seconds();
4384
eaf15d5b
JS
4385 /* Give the INITFF and Post time to settle. */
4386 mdelay(100);
41415862 4387
0d878419
JS
4388 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4389 if (hba_aer_enabled)
4390 pci_disable_pcie_error_reporting(phba->pcidev);
4391
41415862 4392 lpfc_hba_down_post(phba);
dea3101e
JB
4393
4394 return 0;
4395}
4396
da0436e9
JS
4397/**
4398 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4399 * @phba: Pointer to HBA context object.
4400 *
4401 * This function is called in the SLI initialization code path to restart
4402 * a SLI4 HBA. The caller is not required to hold any lock.
4403 * At the end of the function, it calls lpfc_hba_down_post function to
4404 * free any pending commands.
4405 **/
4406static int
4407lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4408{
4409 struct lpfc_sli *psli = &phba->sli;
75baf696 4410 uint32_t hba_aer_enabled;
27b01b82 4411 int rc;
da0436e9
JS
4412
4413 /* Restart HBA */
4414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4415 "0296 Restart HBA Data: x%x x%x\n",
4416 phba->pport->port_state, psli->sli_flag);
4417
75baf696
JS
4418 /* Take PCIe device Advanced Error Reporting (AER) state */
4419 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4420
27b01b82 4421 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4422
4423 spin_lock_irq(&phba->hbalock);
4424 phba->pport->stopped = 0;
4425 phba->link_state = LPFC_INIT_START;
4426 phba->hba_flag = 0;
4427 spin_unlock_irq(&phba->hbalock);
4428
4429 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4430 psli->stats_start = get_seconds();
4431
75baf696
JS
4432 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4433 if (hba_aer_enabled)
4434 pci_disable_pcie_error_reporting(phba->pcidev);
4435
da0436e9 4436 lpfc_hba_down_post(phba);
569dbe84 4437 lpfc_sli4_queue_destroy(phba);
da0436e9 4438
27b01b82 4439 return rc;
da0436e9
JS
4440}
4441
4442/**
4443 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4444 * @phba: Pointer to HBA context object.
4445 *
4446 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4447 * API jump table function pointer from the lpfc_hba struct.
4448**/
4449int
4450lpfc_sli_brdrestart(struct lpfc_hba *phba)
4451{
4452 return phba->lpfc_sli_brdrestart(phba);
4453}
4454
e59058c4 4455/**
3621a710 4456 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4457 * @phba: Pointer to HBA context object.
4458 *
4459 * This function is called after a HBA restart to wait for successful
4460 * restart of the HBA. Successful restart of the HBA is indicated by
4461 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4462 * iteration, the function will restart the HBA again. The function returns
4463 * zero if HBA successfully restarted else returns negative error code.
4464 **/
4492b739 4465int
dea3101e
JB
4466lpfc_sli_chipset_init(struct lpfc_hba *phba)
4467{
4468 uint32_t status, i = 0;
4469
4470 /* Read the HBA Host Status Register */
9940b97b
JS
4471 if (lpfc_readl(phba->HSregaddr, &status))
4472 return -EIO;
dea3101e
JB
4473
4474 /* Check status register to see what current state is */
4475 i = 0;
4476 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4477
dcf2a4e0
JS
4478 /* Check every 10ms for 10 retries, then every 100ms for 90
4479 * retries, then every 1 sec for 50 retires for a total of
4480 * ~60 seconds before reset the board again and check every
4481 * 1 sec for 50 retries. The up to 60 seconds before the
4482 * board ready is required by the Falcon FIPS zeroization
4483 * complete, and any reset the board in between shall cause
4484 * restart of zeroization, further delay the board ready.
dea3101e 4485 */
dcf2a4e0 4486 if (i++ >= 200) {
dea3101e
JB
4487 /* Adapter failed to init, timeout, status reg
4488 <status> */
ed957684 4489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4490 "0436 Adapter failed to init, "
09372820
JS
4491 "timeout, status reg x%x, "
4492 "FW Data: A8 x%x AC x%x\n", status,
4493 readl(phba->MBslimaddr + 0xa8),
4494 readl(phba->MBslimaddr + 0xac));
2e0fef85 4495 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4496 return -ETIMEDOUT;
4497 }
4498
4499 /* Check to see if any errors occurred during init */
4500 if (status & HS_FFERM) {
4501 /* ERROR: During chipset initialization */
4502 /* Adapter failed to init, chipset, status reg
4503 <status> */
ed957684 4504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4505 "0437 Adapter failed to init, "
09372820
JS
4506 "chipset, status reg x%x, "
4507 "FW Data: A8 x%x AC x%x\n", status,
4508 readl(phba->MBslimaddr + 0xa8),
4509 readl(phba->MBslimaddr + 0xac));
2e0fef85 4510 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4511 return -EIO;
4512 }
4513
dcf2a4e0 4514 if (i <= 10)
dea3101e 4515 msleep(10);
dcf2a4e0
JS
4516 else if (i <= 100)
4517 msleep(100);
4518 else
4519 msleep(1000);
dea3101e 4520
dcf2a4e0
JS
4521 if (i == 150) {
4522 /* Do post */
92d7f7b0 4523 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4524 lpfc_sli_brdrestart(phba);
dea3101e
JB
4525 }
4526 /* Read the HBA Host Status Register */
9940b97b
JS
4527 if (lpfc_readl(phba->HSregaddr, &status))
4528 return -EIO;
dea3101e
JB
4529 }
4530
4531 /* Check to see if any errors occurred during init */
4532 if (status & HS_FFERM) {
4533 /* ERROR: During chipset initialization */
4534 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4536 "0438 Adapter failed to init, chipset, "
09372820
JS
4537 "status reg x%x, "
4538 "FW Data: A8 x%x AC x%x\n", status,
4539 readl(phba->MBslimaddr + 0xa8),
4540 readl(phba->MBslimaddr + 0xac));
2e0fef85 4541 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4542 return -EIO;
4543 }
4544
4545 /* Clear all interrupt enable conditions */
4546 writel(0, phba->HCregaddr);
4547 readl(phba->HCregaddr); /* flush */
4548
4549 /* setup host attn register */
4550 writel(0xffffffff, phba->HAregaddr);
4551 readl(phba->HAregaddr); /* flush */
4552 return 0;
4553}
4554
e59058c4 4555/**
3621a710 4556 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4557 *
4558 * This function calculates and returns the number of HBQs required to be
4559 * configured.
4560 **/
78b2d852 4561int
ed957684
JS
4562lpfc_sli_hbq_count(void)
4563{
92d7f7b0 4564 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4565}
4566
e59058c4 4567/**
3621a710 4568 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4569 *
4570 * This function adds the number of hbq entries in every HBQ to get
4571 * the total number of hbq entries required for the HBA and returns
4572 * the total count.
4573 **/
ed957684
JS
4574static int
4575lpfc_sli_hbq_entry_count(void)
4576{
4577 int hbq_count = lpfc_sli_hbq_count();
4578 int count = 0;
4579 int i;
4580
4581 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4582 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4583 return count;
4584}
4585
e59058c4 4586/**
3621a710 4587 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4588 *
4589 * This function calculates amount of memory required for all hbq entries
4590 * to be configured and returns the total memory required.
4591 **/
dea3101e 4592int
ed957684
JS
4593lpfc_sli_hbq_size(void)
4594{
4595 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4596}
4597
e59058c4 4598/**
3621a710 4599 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4600 * @phba: Pointer to HBA context object.
4601 *
4602 * This function is called during the SLI initialization to configure
4603 * all the HBQs and post buffers to the HBQ. The caller is not
4604 * required to hold any locks. This function will return zero if successful
4605 * else it will return negative error code.
4606 **/
ed957684
JS
4607static int
4608lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4609{
4610 int hbq_count = lpfc_sli_hbq_count();
4611 LPFC_MBOXQ_t *pmb;
4612 MAILBOX_t *pmbox;
4613 uint32_t hbqno;
4614 uint32_t hbq_entry_index;
ed957684 4615
92d7f7b0
JS
4616 /* Get a Mailbox buffer to setup mailbox
4617 * commands for HBA initialization
4618 */
ed957684
JS
4619 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4620
4621 if (!pmb)
4622 return -ENOMEM;
4623
04c68496 4624 pmbox = &pmb->u.mb;
ed957684
JS
4625
4626 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4627 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4628 phba->hbq_in_use = 1;
ed957684
JS
4629
4630 hbq_entry_index = 0;
4631 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4632 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4633 phba->hbqs[hbqno].hbqPutIdx = 0;
4634 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4635 phba->hbqs[hbqno].entry_count =
92d7f7b0 4636 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4637 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4638 hbq_entry_index, pmb);
ed957684
JS
4639 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4640
4641 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4642 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4643 mbxStatus <status>, ring <num> */
4644
4645 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4646 LOG_SLI | LOG_VPORT,
e8b62011 4647 "1805 Adapter failed to init. "
ed957684 4648 "Data: x%x x%x x%x\n",
e8b62011 4649 pmbox->mbxCommand,
ed957684
JS
4650 pmbox->mbxStatus, hbqno);
4651
4652 phba->link_state = LPFC_HBA_ERROR;
4653 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4654 return -ENXIO;
ed957684
JS
4655 }
4656 }
4657 phba->hbq_count = hbq_count;
4658
ed957684
JS
4659 mempool_free(pmb, phba->mbox_mem_pool);
4660
92d7f7b0 4661 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4662 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4663 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4664 return 0;
4665}
4666
4f774513
JS
4667/**
4668 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4669 * @phba: Pointer to HBA context object.
4670 *
4671 * This function is called during the SLI initialization to configure
4672 * all the HBQs and post buffers to the HBQ. The caller is not
4673 * required to hold any locks. This function will return zero if successful
4674 * else it will return negative error code.
4675 **/
4676static int
4677lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4678{
4679 phba->hbq_in_use = 1;
895427bd
JS
4680 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4681 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4682 phba->hbq_count = 1;
895427bd 4683 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4684 /* Initially populate or replenish the HBQs */
4f774513
JS
4685 return 0;
4686}
4687
e59058c4 4688/**
3621a710 4689 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4690 * @phba: Pointer to HBA context object.
4691 * @sli_mode: sli mode - 2/3
4692 *
183b8021 4693 * This function is called by the sli initialization code path
e59058c4
JS
4694 * to issue config_port mailbox command. This function restarts the
4695 * HBA firmware and issues a config_port mailbox command to configure
4696 * the SLI interface in the sli mode specified by sli_mode
4697 * variable. The caller is not required to hold any locks.
4698 * The function returns 0 if successful, else returns negative error
4699 * code.
4700 **/
9399627f
JS
4701int
4702lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4703{
4704 LPFC_MBOXQ_t *pmb;
4705 uint32_t resetcount = 0, rc = 0, done = 0;
4706
4707 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4708 if (!pmb) {
2e0fef85 4709 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4710 return -ENOMEM;
4711 }
4712
ed957684 4713 phba->sli_rev = sli_mode;
dea3101e 4714 while (resetcount < 2 && !done) {
2e0fef85 4715 spin_lock_irq(&phba->hbalock);
1c067a42 4716 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4717 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4718 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4719 lpfc_sli_brdrestart(phba);
dea3101e
JB
4720 rc = lpfc_sli_chipset_init(phba);
4721 if (rc)
4722 break;
4723
2e0fef85 4724 spin_lock_irq(&phba->hbalock);
1c067a42 4725 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4726 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4727 resetcount++;
4728
ed957684
JS
4729 /* Call pre CONFIG_PORT mailbox command initialization. A
4730 * value of 0 means the call was successful. Any other
4731 * nonzero value is a failure, but if ERESTART is returned,
4732 * the driver may reset the HBA and try again.
4733 */
dea3101e
JB
4734 rc = lpfc_config_port_prep(phba);
4735 if (rc == -ERESTART) {
ed957684 4736 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4737 continue;
34b02dcd 4738 } else if (rc)
dea3101e 4739 break;
6d368e53 4740
2e0fef85 4741 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4742 lpfc_config_port(phba, pmb);
4743 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4744 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4745 LPFC_SLI3_HBQ_ENABLED |
4746 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4747 LPFC_SLI3_BG_ENABLED |
4748 LPFC_SLI3_DSS_ENABLED);
ed957684 4749 if (rc != MBX_SUCCESS) {
dea3101e 4750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4751 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4752 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4753 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4754 spin_lock_irq(&phba->hbalock);
04c68496 4755 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4756 spin_unlock_irq(&phba->hbalock);
4757 rc = -ENXIO;
04c68496
JS
4758 } else {
4759 /* Allow asynchronous mailbox command to go through */
4760 spin_lock_irq(&phba->hbalock);
4761 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4762 spin_unlock_irq(&phba->hbalock);
ed957684 4763 done = 1;
cb69f7de
JS
4764
4765 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4766 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4767 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4768 "3110 Port did not grant ASABT\n");
04c68496 4769 }
dea3101e 4770 }
ed957684
JS
4771 if (!done) {
4772 rc = -EINVAL;
4773 goto do_prep_failed;
4774 }
04c68496
JS
4775 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4776 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4777 rc = -ENXIO;
4778 goto do_prep_failed;
4779 }
04c68496 4780 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4781 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4782 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4783 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4784 phba->max_vpi : phba->max_vports;
4785
34b02dcd
JS
4786 } else
4787 phba->max_vpi = 0;
bc73905a
JS
4788 phba->fips_level = 0;
4789 phba->fips_spec_rev = 0;
4790 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4791 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4792 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4793 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4794 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4795 "2850 Security Crypto Active. FIPS x%d "
4796 "(Spec Rev: x%d)",
4797 phba->fips_level, phba->fips_spec_rev);
4798 }
4799 if (pmb->u.mb.un.varCfgPort.sec_err) {
4800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4801 "2856 Config Port Security Crypto "
4802 "Error: x%x ",
4803 pmb->u.mb.un.varCfgPort.sec_err);
4804 }
04c68496 4805 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4806 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4807 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4808 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4809
4810 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4811 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4812
4813 if (phba->cfg_enable_bg) {
04c68496 4814 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4815 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4816 else
4817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4818 "0443 Adapter did not grant "
4819 "BlockGuard\n");
4820 }
34b02dcd 4821 } else {
8f34f4ce 4822 phba->hbq_get = NULL;
34b02dcd 4823 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4824 phba->max_vpi = 0;
ed957684 4825 }
92d7f7b0 4826do_prep_failed:
ed957684
JS
4827 mempool_free(pmb, phba->mbox_mem_pool);
4828 return rc;
4829}
4830
e59058c4
JS
4831
4832/**
183b8021 4833 * lpfc_sli_hba_setup - SLI initialization function
e59058c4
JS
4834 * @phba: Pointer to HBA context object.
4835 *
183b8021
MY
4836 * This function is the main SLI initialization function. This function
4837 * is called by the HBA initialization code, HBA reset code and HBA
e59058c4
JS
4838 * error attention handler code. Caller is not required to hold any
4839 * locks. This function issues config_port mailbox command to configure
4840 * the SLI, setup iocb rings and HBQ rings. In the end the function
4841 * calls the config_port_post function to issue init_link mailbox
4842 * command and to start the discovery. The function will return zero
4843 * if successful, else it will return negative error code.
4844 **/
ed957684
JS
4845int
4846lpfc_sli_hba_setup(struct lpfc_hba *phba)
4847{
4848 uint32_t rc;
6d368e53
JS
4849 int mode = 3, i;
4850 int longs;
ed957684 4851
12247e81 4852 switch (phba->cfg_sli_mode) {
ed957684 4853 case 2:
78b2d852 4854 if (phba->cfg_enable_npiv) {
92d7f7b0 4855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 4856 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 4857 "parameter (%d) to auto (0).\n",
12247e81 4858 phba->cfg_sli_mode);
92d7f7b0
JS
4859 break;
4860 }
ed957684
JS
4861 mode = 2;
4862 break;
4863 case 0:
4864 case 3:
4865 break;
4866 default:
92d7f7b0 4867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
4868 "1819 Unrecognized sli_mode parameter: %d.\n",
4869 phba->cfg_sli_mode);
ed957684
JS
4870
4871 break;
4872 }
b5c53958 4873 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 4874
9399627f
JS
4875 rc = lpfc_sli_config_port(phba, mode);
4876
12247e81 4877 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 4878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4879 "1820 Unable to select SLI-3. "
4880 "Not supported by adapter.\n");
ed957684 4881 if (rc && mode != 2)
9399627f 4882 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
4883 else if (rc && mode == 2)
4884 rc = lpfc_sli_config_port(phba, 3);
ed957684 4885 if (rc)
dea3101e
JB
4886 goto lpfc_sli_hba_setup_error;
4887
0d878419
JS
4888 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4889 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4890 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4891 if (!rc) {
4892 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4893 "2709 This device supports "
4894 "Advanced Error Reporting (AER)\n");
4895 spin_lock_irq(&phba->hbalock);
4896 phba->hba_flag |= HBA_AER_ENABLED;
4897 spin_unlock_irq(&phba->hbalock);
4898 } else {
4899 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4900 "2708 This device does not support "
b069d7eb
JS
4901 "Advanced Error Reporting (AER): %d\n",
4902 rc);
0d878419
JS
4903 phba->cfg_aer_support = 0;
4904 }
4905 }
4906
ed957684
JS
4907 if (phba->sli_rev == 3) {
4908 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4909 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4910 } else {
4911 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4912 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4913 phba->sli3_options = 0;
ed957684
JS
4914 }
4915
4916 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4917 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4918 phba->sli_rev, phba->max_vpi);
ed957684 4919 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4920
4921 if (rc)
4922 goto lpfc_sli_hba_setup_error;
4923
6d368e53
JS
4924 /* Initialize VPIs. */
4925 if (phba->sli_rev == LPFC_SLI_REV3) {
4926 /*
4927 * The VPI bitmask and physical ID array are allocated
4928 * and initialized once only - at driver load. A port
4929 * reset doesn't need to reinitialize this memory.
4930 */
4931 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4932 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4933 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4934 GFP_KERNEL);
4935 if (!phba->vpi_bmask) {
4936 rc = -ENOMEM;
4937 goto lpfc_sli_hba_setup_error;
4938 }
4939
4940 phba->vpi_ids = kzalloc(
4941 (phba->max_vpi+1) * sizeof(uint16_t),
4942 GFP_KERNEL);
4943 if (!phba->vpi_ids) {
4944 kfree(phba->vpi_bmask);
4945 rc = -ENOMEM;
4946 goto lpfc_sli_hba_setup_error;
4947 }
4948 for (i = 0; i < phba->max_vpi; i++)
4949 phba->vpi_ids[i] = i;
4950 }
4951 }
4952
9399627f 4953 /* Init HBQs */
ed957684
JS
4954 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4955 rc = lpfc_sli_hbq_setup(phba);
4956 if (rc)
4957 goto lpfc_sli_hba_setup_error;
4958 }
04c68496 4959 spin_lock_irq(&phba->hbalock);
dea3101e 4960 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4961 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4962
4963 rc = lpfc_config_port_post(phba);
4964 if (rc)
4965 goto lpfc_sli_hba_setup_error;
4966
ed957684
JS
4967 return rc;
4968
92d7f7b0 4969lpfc_sli_hba_setup_error:
2e0fef85 4970 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4972 "0445 Firmware initialization failed\n");
dea3101e
JB
4973 return rc;
4974}
4975
e59058c4 4976/**
da0436e9
JS
4977 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4978 * @phba: Pointer to HBA context object.
4979 * @mboxq: mailbox pointer.
4980 * This function issue a dump mailbox command to read config region
4981 * 23 and parse the records in the region and populate driver
4982 * data structure.
e59058c4 4983 **/
da0436e9 4984static int
ff78d8f9 4985lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4986{
ff78d8f9 4987 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4988 struct lpfc_dmabuf *mp;
4989 struct lpfc_mqe *mqe;
4990 uint32_t data_length;
4991 int rc;
dea3101e 4992
da0436e9
JS
4993 /* Program the default value of vlan_id and fc_map */
4994 phba->valid_vlan = 0;
4995 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4996 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4997 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4998
ff78d8f9
JS
4999 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5000 if (!mboxq)
da0436e9
JS
5001 return -ENOMEM;
5002
ff78d8f9
JS
5003 mqe = &mboxq->u.mqe;
5004 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5005 rc = -ENOMEM;
5006 goto out_free_mboxq;
5007 }
5008
da0436e9
JS
5009 mp = (struct lpfc_dmabuf *) mboxq->context1;
5010 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5011
5012 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5013 "(%d):2571 Mailbox cmd x%x Status x%x "
5014 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5015 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5016 "CQ: x%x x%x x%x x%x\n",
5017 mboxq->vport ? mboxq->vport->vpi : 0,
5018 bf_get(lpfc_mqe_command, mqe),
5019 bf_get(lpfc_mqe_status, mqe),
5020 mqe->un.mb_words[0], mqe->un.mb_words[1],
5021 mqe->un.mb_words[2], mqe->un.mb_words[3],
5022 mqe->un.mb_words[4], mqe->un.mb_words[5],
5023 mqe->un.mb_words[6], mqe->un.mb_words[7],
5024 mqe->un.mb_words[8], mqe->un.mb_words[9],
5025 mqe->un.mb_words[10], mqe->un.mb_words[11],
5026 mqe->un.mb_words[12], mqe->un.mb_words[13],
5027 mqe->un.mb_words[14], mqe->un.mb_words[15],
5028 mqe->un.mb_words[16], mqe->un.mb_words[50],
5029 mboxq->mcqe.word0,
5030 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5031 mboxq->mcqe.trailer);
5032
5033 if (rc) {
5034 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5035 kfree(mp);
ff78d8f9
JS
5036 rc = -EIO;
5037 goto out_free_mboxq;
da0436e9
JS
5038 }
5039 data_length = mqe->un.mb_words[5];
a0c87cbd 5040 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5041 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5042 kfree(mp);
ff78d8f9
JS
5043 rc = -EIO;
5044 goto out_free_mboxq;
d11e31dd 5045 }
dea3101e 5046
da0436e9
JS
5047 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5048 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5049 kfree(mp);
ff78d8f9
JS
5050 rc = 0;
5051
5052out_free_mboxq:
5053 mempool_free(mboxq, phba->mbox_mem_pool);
5054 return rc;
da0436e9 5055}
e59058c4
JS
5056
5057/**
da0436e9
JS
5058 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5059 * @phba: pointer to lpfc hba data structure.
5060 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5061 * @vpd: pointer to the memory to hold resulting port vpd data.
5062 * @vpd_size: On input, the number of bytes allocated to @vpd.
5063 * On output, the number of data bytes in @vpd.
e59058c4 5064 *
da0436e9
JS
5065 * This routine executes a READ_REV SLI4 mailbox command. In
5066 * addition, this routine gets the port vpd data.
5067 *
5068 * Return codes
af901ca1 5069 * 0 - successful
d439d286 5070 * -ENOMEM - could not allocated memory.
e59058c4 5071 **/
da0436e9
JS
5072static int
5073lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5074 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5075{
da0436e9
JS
5076 int rc = 0;
5077 uint32_t dma_size;
5078 struct lpfc_dmabuf *dmabuf;
5079 struct lpfc_mqe *mqe;
dea3101e 5080
da0436e9
JS
5081 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5082 if (!dmabuf)
5083 return -ENOMEM;
5084
5085 /*
5086 * Get a DMA buffer for the vpd data resulting from the READ_REV
5087 * mailbox command.
a257bf90 5088 */
da0436e9 5089 dma_size = *vpd_size;
1aee383d
JP
5090 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5091 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5092 if (!dmabuf->virt) {
5093 kfree(dmabuf);
5094 return -ENOMEM;
a257bf90
JS
5095 }
5096
da0436e9
JS
5097 /*
5098 * The SLI4 implementation of READ_REV conflicts at word1,
5099 * bits 31:16 and SLI4 adds vpd functionality not present
5100 * in SLI3. This code corrects the conflicts.
1dcb58e5 5101 */
da0436e9
JS
5102 lpfc_read_rev(phba, mboxq);
5103 mqe = &mboxq->u.mqe;
5104 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5105 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5106 mqe->un.read_rev.word1 &= 0x0000FFFF;
5107 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5108 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5109
5110 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5111 if (rc) {
5112 dma_free_coherent(&phba->pcidev->dev, dma_size,
5113 dmabuf->virt, dmabuf->phys);
def9c7a9 5114 kfree(dmabuf);
da0436e9
JS
5115 return -EIO;
5116 }
1dcb58e5 5117
da0436e9
JS
5118 /*
5119 * The available vpd length cannot be bigger than the
5120 * DMA buffer passed to the port. Catch the less than
5121 * case and update the caller's size.
5122 */
5123 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5124 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5125
d7c47992
JS
5126 memcpy(vpd, dmabuf->virt, *vpd_size);
5127
da0436e9
JS
5128 dma_free_coherent(&phba->pcidev->dev, dma_size,
5129 dmabuf->virt, dmabuf->phys);
5130 kfree(dmabuf);
5131 return 0;
dea3101e
JB
5132}
5133
cd1c8301
JS
5134/**
5135 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5136 * @phba: pointer to lpfc hba data structure.
5137 *
5138 * This routine retrieves SLI4 device physical port name this PCI function
5139 * is attached to.
5140 *
5141 * Return codes
4907cb7b 5142 * 0 - successful
cd1c8301
JS
5143 * otherwise - failed to retrieve physical port name
5144 **/
5145static int
5146lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5147{
5148 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5149 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5150 struct lpfc_controller_attribute *cntl_attr;
5151 struct lpfc_mbx_get_port_name *get_port_name;
5152 void *virtaddr = NULL;
5153 uint32_t alloclen, reqlen;
5154 uint32_t shdr_status, shdr_add_status;
5155 union lpfc_sli4_cfg_shdr *shdr;
5156 char cport_name = 0;
5157 int rc;
5158
5159 /* We assume nothing at this point */
5160 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5161 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5162
5163 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5164 if (!mboxq)
5165 return -ENOMEM;
cd1c8301 5166 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5167 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5168 lpfc_sli4_read_config(phba);
5169 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5170 goto retrieve_ppname;
cd1c8301
JS
5171
5172 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5173 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5174 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5175 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5176 LPFC_SLI4_MBX_NEMBED);
5177 if (alloclen < reqlen) {
5178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5179 "3084 Allocated DMA memory size (%d) is "
5180 "less than the requested DMA memory size "
5181 "(%d)\n", alloclen, reqlen);
5182 rc = -ENOMEM;
5183 goto out_free_mboxq;
5184 }
5185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5186 virtaddr = mboxq->sge_array->addr[0];
5187 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5188 shdr = &mbx_cntl_attr->cfg_shdr;
5189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5191 if (shdr_status || shdr_add_status || rc) {
5192 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5193 "3085 Mailbox x%x (x%x/x%x) failed, "
5194 "rc:x%x, status:x%x, add_status:x%x\n",
5195 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5196 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5197 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5198 rc, shdr_status, shdr_add_status);
5199 rc = -ENXIO;
5200 goto out_free_mboxq;
5201 }
5202 cntl_attr = &mbx_cntl_attr->cntl_attr;
5203 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5204 phba->sli4_hba.lnk_info.lnk_tp =
5205 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5206 phba->sli4_hba.lnk_info.lnk_no =
5207 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5208 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5209 "3086 lnk_type:%d, lnk_numb:%d\n",
5210 phba->sli4_hba.lnk_info.lnk_tp,
5211 phba->sli4_hba.lnk_info.lnk_no);
5212
5213retrieve_ppname:
5214 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5215 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5216 sizeof(struct lpfc_mbx_get_port_name) -
5217 sizeof(struct lpfc_sli4_cfg_mhdr),
5218 LPFC_SLI4_MBX_EMBED);
5219 get_port_name = &mboxq->u.mqe.un.get_port_name;
5220 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5221 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5222 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5223 phba->sli4_hba.lnk_info.lnk_tp);
5224 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5225 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5226 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5227 if (shdr_status || shdr_add_status || rc) {
5228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5229 "3087 Mailbox x%x (x%x/x%x) failed: "
5230 "rc:x%x, status:x%x, add_status:x%x\n",
5231 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5232 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5233 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5234 rc, shdr_status, shdr_add_status);
5235 rc = -ENXIO;
5236 goto out_free_mboxq;
5237 }
5238 switch (phba->sli4_hba.lnk_info.lnk_no) {
5239 case LPFC_LINK_NUMBER_0:
5240 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5241 &get_port_name->u.response);
5242 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5243 break;
5244 case LPFC_LINK_NUMBER_1:
5245 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5246 &get_port_name->u.response);
5247 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5248 break;
5249 case LPFC_LINK_NUMBER_2:
5250 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5251 &get_port_name->u.response);
5252 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5253 break;
5254 case LPFC_LINK_NUMBER_3:
5255 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5256 &get_port_name->u.response);
5257 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5258 break;
5259 default:
5260 break;
5261 }
5262
5263 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5264 phba->Port[0] = cport_name;
5265 phba->Port[1] = '\0';
5266 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5267 "3091 SLI get port name: %s\n", phba->Port);
5268 }
5269
5270out_free_mboxq:
5271 if (rc != MBX_TIMEOUT) {
5272 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5273 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5274 else
5275 mempool_free(mboxq, phba->mbox_mem_pool);
5276 }
5277 return rc;
5278}
5279
e59058c4 5280/**
da0436e9
JS
5281 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5282 * @phba: pointer to lpfc hba data structure.
e59058c4 5283 *
da0436e9
JS
5284 * This routine is called to explicitly arm the SLI4 device's completion and
5285 * event queues
5286 **/
5287static void
5288lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5289{
895427bd 5290 int qidx;
da0436e9
JS
5291
5292 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5293 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
895427bd
JS
5294 if (phba->sli4_hba.nvmels_cq)
5295 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5296 LPFC_QUEUE_REARM);
5297
5298 if (phba->sli4_hba.fcp_cq)
5299 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5300 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5301 LPFC_QUEUE_REARM);
5302
5303 if (phba->sli4_hba.nvme_cq)
5304 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5305 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5306 LPFC_QUEUE_REARM);
1ba981fd 5307
f38fa0bb 5308 if (phba->cfg_fof)
1ba981fd
JS
5309 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5310
895427bd
JS
5311 if (phba->sli4_hba.hba_eq)
5312 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5313 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5314 LPFC_QUEUE_REARM);
1ba981fd 5315
2d7dbc4c
JS
5316 if (phba->nvmet_support) {
5317 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5318 lpfc_sli4_cq_release(
5319 phba->sli4_hba.nvmet_cqset[qidx],
5320 LPFC_QUEUE_REARM);
5321 }
2e90f4b5 5322 }
1ba981fd
JS
5323
5324 if (phba->cfg_fof)
5325 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5326}
5327
6d368e53
JS
5328/**
5329 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5330 * @phba: Pointer to HBA context object.
5331 * @type: The resource extent type.
b76f2dc9
JS
5332 * @extnt_count: buffer to hold port available extent count.
5333 * @extnt_size: buffer to hold element count per extent.
6d368e53 5334 *
b76f2dc9
JS
5335 * This function calls the port and retrievs the number of available
5336 * extents and their size for a particular extent type.
5337 *
5338 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5339 **/
b76f2dc9 5340int
6d368e53
JS
5341lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5342 uint16_t *extnt_count, uint16_t *extnt_size)
5343{
5344 int rc = 0;
5345 uint32_t length;
5346 uint32_t mbox_tmo;
5347 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5348 LPFC_MBOXQ_t *mbox;
5349
5350 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5351 if (!mbox)
5352 return -ENOMEM;
5353
5354 /* Find out how many extents are available for this resource type */
5355 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5356 sizeof(struct lpfc_sli4_cfg_mhdr));
5357 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5358 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5359 length, LPFC_SLI4_MBX_EMBED);
5360
5361 /* Send an extents count of 0 - the GET doesn't use it. */
5362 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5363 LPFC_SLI4_MBX_EMBED);
5364 if (unlikely(rc)) {
5365 rc = -EIO;
5366 goto err_exit;
5367 }
5368
5369 if (!phba->sli4_hba.intr_enable)
5370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5371 else {
a183a15f 5372 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5373 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5374 }
5375 if (unlikely(rc)) {
5376 rc = -EIO;
5377 goto err_exit;
5378 }
5379
5380 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5381 if (bf_get(lpfc_mbox_hdr_status,
5382 &rsrc_info->header.cfg_shdr.response)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "2930 Failed to get resource extents "
5385 "Status 0x%x Add'l Status 0x%x\n",
5386 bf_get(lpfc_mbox_hdr_status,
5387 &rsrc_info->header.cfg_shdr.response),
5388 bf_get(lpfc_mbox_hdr_add_status,
5389 &rsrc_info->header.cfg_shdr.response));
5390 rc = -EIO;
5391 goto err_exit;
5392 }
5393
5394 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5395 &rsrc_info->u.rsp);
5396 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5397 &rsrc_info->u.rsp);
8a9d2e80
JS
5398
5399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5400 "3162 Retrieved extents type-%d from port: count:%d, "
5401 "size:%d\n", type, *extnt_count, *extnt_size);
5402
5403err_exit:
6d368e53
JS
5404 mempool_free(mbox, phba->mbox_mem_pool);
5405 return rc;
5406}
5407
5408/**
5409 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5410 * @phba: Pointer to HBA context object.
5411 * @type: The extent type to check.
5412 *
5413 * This function reads the current available extents from the port and checks
5414 * if the extent count or extent size has changed since the last access.
5415 * Callers use this routine post port reset to understand if there is a
5416 * extent reprovisioning requirement.
5417 *
5418 * Returns:
5419 * -Error: error indicates problem.
5420 * 1: Extent count or size has changed.
5421 * 0: No changes.
5422 **/
5423static int
5424lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5425{
5426 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5427 uint16_t size_diff, rsrc_ext_size;
5428 int rc = 0;
5429 struct lpfc_rsrc_blks *rsrc_entry;
5430 struct list_head *rsrc_blk_list = NULL;
5431
5432 size_diff = 0;
5433 curr_ext_cnt = 0;
5434 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5435 &rsrc_ext_cnt,
5436 &rsrc_ext_size);
5437 if (unlikely(rc))
5438 return -EIO;
5439
5440 switch (type) {
5441 case LPFC_RSC_TYPE_FCOE_RPI:
5442 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5443 break;
5444 case LPFC_RSC_TYPE_FCOE_VPI:
5445 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5446 break;
5447 case LPFC_RSC_TYPE_FCOE_XRI:
5448 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5449 break;
5450 case LPFC_RSC_TYPE_FCOE_VFI:
5451 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5452 break;
5453 default:
5454 break;
5455 }
5456
5457 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5458 curr_ext_cnt++;
5459 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5460 size_diff++;
5461 }
5462
5463 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5464 rc = 1;
5465
5466 return rc;
5467}
5468
5469/**
5470 * lpfc_sli4_cfg_post_extnts -
5471 * @phba: Pointer to HBA context object.
5472 * @extnt_cnt - number of available extents.
5473 * @type - the extent type (rpi, xri, vfi, vpi).
5474 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5475 * @mbox - pointer to the caller's allocated mailbox structure.
5476 *
5477 * This function executes the extents allocation request. It also
5478 * takes care of the amount of memory needed to allocate or get the
5479 * allocated extents. It is the caller's responsibility to evaluate
5480 * the response.
5481 *
5482 * Returns:
5483 * -Error: Error value describes the condition found.
5484 * 0: if successful
5485 **/
5486static int
8a9d2e80 5487lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5488 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5489{
5490 int rc = 0;
5491 uint32_t req_len;
5492 uint32_t emb_len;
5493 uint32_t alloc_len, mbox_tmo;
5494
5495 /* Calculate the total requested length of the dma memory */
8a9d2e80 5496 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5497
5498 /*
5499 * Calculate the size of an embedded mailbox. The uint32_t
5500 * accounts for extents-specific word.
5501 */
5502 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5503 sizeof(uint32_t);
5504
5505 /*
5506 * Presume the allocation and response will fit into an embedded
5507 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5508 */
5509 *emb = LPFC_SLI4_MBX_EMBED;
5510 if (req_len > emb_len) {
8a9d2e80 5511 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5512 sizeof(union lpfc_sli4_cfg_shdr) +
5513 sizeof(uint32_t);
5514 *emb = LPFC_SLI4_MBX_NEMBED;
5515 }
5516
5517 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5518 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5519 req_len, *emb);
5520 if (alloc_len < req_len) {
5521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5522 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5523 "less than the requested DMA memory "
5524 "size (x%x)\n", alloc_len, req_len);
5525 return -ENOMEM;
5526 }
8a9d2e80 5527 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5528 if (unlikely(rc))
5529 return -EIO;
5530
5531 if (!phba->sli4_hba.intr_enable)
5532 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5533 else {
a183a15f 5534 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5535 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5536 }
5537
5538 if (unlikely(rc))
5539 rc = -EIO;
5540 return rc;
5541}
5542
5543/**
5544 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5545 * @phba: Pointer to HBA context object.
5546 * @type: The resource extent type to allocate.
5547 *
5548 * This function allocates the number of elements for the specified
5549 * resource type.
5550 **/
5551static int
5552lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5553{
5554 bool emb = false;
5555 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5556 uint16_t rsrc_id, rsrc_start, j, k;
5557 uint16_t *ids;
5558 int i, rc;
5559 unsigned long longs;
5560 unsigned long *bmask;
5561 struct lpfc_rsrc_blks *rsrc_blks;
5562 LPFC_MBOXQ_t *mbox;
5563 uint32_t length;
5564 struct lpfc_id_range *id_array = NULL;
5565 void *virtaddr = NULL;
5566 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5567 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5568 struct list_head *ext_blk_list;
5569
5570 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5571 &rsrc_cnt,
5572 &rsrc_size);
5573 if (unlikely(rc))
5574 return -EIO;
5575
5576 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5578 "3009 No available Resource Extents "
5579 "for resource type 0x%x: Count: 0x%x, "
5580 "Size 0x%x\n", type, rsrc_cnt,
5581 rsrc_size);
5582 return -ENOMEM;
5583 }
5584
8a9d2e80
JS
5585 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5586 "2903 Post resource extents type-0x%x: "
5587 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5588
5589 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5590 if (!mbox)
5591 return -ENOMEM;
5592
8a9d2e80 5593 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5594 if (unlikely(rc)) {
5595 rc = -EIO;
5596 goto err_exit;
5597 }
5598
5599 /*
5600 * Figure out where the response is located. Then get local pointers
5601 * to the response data. The port does not guarantee to respond to
5602 * all extents counts request so update the local variable with the
5603 * allocated count from the port.
5604 */
5605 if (emb == LPFC_SLI4_MBX_EMBED) {
5606 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5607 id_array = &rsrc_ext->u.rsp.id[0];
5608 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5609 } else {
5610 virtaddr = mbox->sge_array->addr[0];
5611 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5612 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5613 id_array = &n_rsrc->id;
5614 }
5615
5616 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5617 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5618
5619 /*
5620 * Based on the resource size and count, correct the base and max
5621 * resource values.
5622 */
5623 length = sizeof(struct lpfc_rsrc_blks);
5624 switch (type) {
5625 case LPFC_RSC_TYPE_FCOE_RPI:
5626 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5627 sizeof(unsigned long),
5628 GFP_KERNEL);
5629 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5630 rc = -ENOMEM;
5631 goto err_exit;
5632 }
5633 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5634 sizeof(uint16_t),
5635 GFP_KERNEL);
5636 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5637 kfree(phba->sli4_hba.rpi_bmask);
5638 rc = -ENOMEM;
5639 goto err_exit;
5640 }
5641
5642 /*
5643 * The next_rpi was initialized with the maximum available
5644 * count but the port may allocate a smaller number. Catch
5645 * that case and update the next_rpi.
5646 */
5647 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5648
5649 /* Initialize local ptrs for common extent processing later. */
5650 bmask = phba->sli4_hba.rpi_bmask;
5651 ids = phba->sli4_hba.rpi_ids;
5652 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5653 break;
5654 case LPFC_RSC_TYPE_FCOE_VPI:
5655 phba->vpi_bmask = kzalloc(longs *
5656 sizeof(unsigned long),
5657 GFP_KERNEL);
5658 if (unlikely(!phba->vpi_bmask)) {
5659 rc = -ENOMEM;
5660 goto err_exit;
5661 }
5662 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5663 sizeof(uint16_t),
5664 GFP_KERNEL);
5665 if (unlikely(!phba->vpi_ids)) {
5666 kfree(phba->vpi_bmask);
5667 rc = -ENOMEM;
5668 goto err_exit;
5669 }
5670
5671 /* Initialize local ptrs for common extent processing later. */
5672 bmask = phba->vpi_bmask;
5673 ids = phba->vpi_ids;
5674 ext_blk_list = &phba->lpfc_vpi_blk_list;
5675 break;
5676 case LPFC_RSC_TYPE_FCOE_XRI:
5677 phba->sli4_hba.xri_bmask = kzalloc(longs *
5678 sizeof(unsigned long),
5679 GFP_KERNEL);
5680 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5681 rc = -ENOMEM;
5682 goto err_exit;
5683 }
8a9d2e80 5684 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5685 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5686 sizeof(uint16_t),
5687 GFP_KERNEL);
5688 if (unlikely(!phba->sli4_hba.xri_ids)) {
5689 kfree(phba->sli4_hba.xri_bmask);
5690 rc = -ENOMEM;
5691 goto err_exit;
5692 }
5693
5694 /* Initialize local ptrs for common extent processing later. */
5695 bmask = phba->sli4_hba.xri_bmask;
5696 ids = phba->sli4_hba.xri_ids;
5697 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5698 break;
5699 case LPFC_RSC_TYPE_FCOE_VFI:
5700 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5701 sizeof(unsigned long),
5702 GFP_KERNEL);
5703 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5704 rc = -ENOMEM;
5705 goto err_exit;
5706 }
5707 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5708 sizeof(uint16_t),
5709 GFP_KERNEL);
5710 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5711 kfree(phba->sli4_hba.vfi_bmask);
5712 rc = -ENOMEM;
5713 goto err_exit;
5714 }
5715
5716 /* Initialize local ptrs for common extent processing later. */
5717 bmask = phba->sli4_hba.vfi_bmask;
5718 ids = phba->sli4_hba.vfi_ids;
5719 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5720 break;
5721 default:
5722 /* Unsupported Opcode. Fail call. */
5723 id_array = NULL;
5724 bmask = NULL;
5725 ids = NULL;
5726 ext_blk_list = NULL;
5727 goto err_exit;
5728 }
5729
5730 /*
5731 * Complete initializing the extent configuration with the
5732 * allocated ids assigned to this function. The bitmask serves
5733 * as an index into the array and manages the available ids. The
5734 * array just stores the ids communicated to the port via the wqes.
5735 */
5736 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5737 if ((i % 2) == 0)
5738 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5739 &id_array[k]);
5740 else
5741 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5742 &id_array[k]);
5743
5744 rsrc_blks = kzalloc(length, GFP_KERNEL);
5745 if (unlikely(!rsrc_blks)) {
5746 rc = -ENOMEM;
5747 kfree(bmask);
5748 kfree(ids);
5749 goto err_exit;
5750 }
5751 rsrc_blks->rsrc_start = rsrc_id;
5752 rsrc_blks->rsrc_size = rsrc_size;
5753 list_add_tail(&rsrc_blks->list, ext_blk_list);
5754 rsrc_start = rsrc_id;
895427bd 5755 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5756 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5757 lpfc_sli4_get_iocb_cnt(phba);
5758 phba->sli4_hba.nvme_xri_start =
5759 phba->sli4_hba.scsi_xri_start +
5760 phba->sli4_hba.scsi_xri_max;
5761 }
6d368e53
JS
5762
5763 while (rsrc_id < (rsrc_start + rsrc_size)) {
5764 ids[j] = rsrc_id;
5765 rsrc_id++;
5766 j++;
5767 }
5768 /* Entire word processed. Get next word.*/
5769 if ((i % 2) == 1)
5770 k++;
5771 }
5772 err_exit:
5773 lpfc_sli4_mbox_cmd_free(phba, mbox);
5774 return rc;
5775}
5776
895427bd
JS
5777
5778
6d368e53
JS
5779/**
5780 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5781 * @phba: Pointer to HBA context object.
5782 * @type: the extent's type.
5783 *
5784 * This function deallocates all extents of a particular resource type.
5785 * SLI4 does not allow for deallocating a particular extent range. It
5786 * is the caller's responsibility to release all kernel memory resources.
5787 **/
5788static int
5789lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5790{
5791 int rc;
5792 uint32_t length, mbox_tmo = 0;
5793 LPFC_MBOXQ_t *mbox;
5794 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5795 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5796
5797 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5798 if (!mbox)
5799 return -ENOMEM;
5800
5801 /*
5802 * This function sends an embedded mailbox because it only sends the
5803 * the resource type. All extents of this type are released by the
5804 * port.
5805 */
5806 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5807 sizeof(struct lpfc_sli4_cfg_mhdr));
5808 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5809 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5810 length, LPFC_SLI4_MBX_EMBED);
5811
5812 /* Send an extents count of 0 - the dealloc doesn't use it. */
5813 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5814 LPFC_SLI4_MBX_EMBED);
5815 if (unlikely(rc)) {
5816 rc = -EIO;
5817 goto out_free_mbox;
5818 }
5819 if (!phba->sli4_hba.intr_enable)
5820 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5821 else {
a183a15f 5822 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5823 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5824 }
5825 if (unlikely(rc)) {
5826 rc = -EIO;
5827 goto out_free_mbox;
5828 }
5829
5830 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5831 if (bf_get(lpfc_mbox_hdr_status,
5832 &dealloc_rsrc->header.cfg_shdr.response)) {
5833 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5834 "2919 Failed to release resource extents "
5835 "for type %d - Status 0x%x Add'l Status 0x%x. "
5836 "Resource memory not released.\n",
5837 type,
5838 bf_get(lpfc_mbox_hdr_status,
5839 &dealloc_rsrc->header.cfg_shdr.response),
5840 bf_get(lpfc_mbox_hdr_add_status,
5841 &dealloc_rsrc->header.cfg_shdr.response));
5842 rc = -EIO;
5843 goto out_free_mbox;
5844 }
5845
5846 /* Release kernel memory resources for the specific type. */
5847 switch (type) {
5848 case LPFC_RSC_TYPE_FCOE_VPI:
5849 kfree(phba->vpi_bmask);
5850 kfree(phba->vpi_ids);
5851 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5852 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5853 &phba->lpfc_vpi_blk_list, list) {
5854 list_del_init(&rsrc_blk->list);
5855 kfree(rsrc_blk);
5856 }
16a3a208 5857 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5858 break;
5859 case LPFC_RSC_TYPE_FCOE_XRI:
5860 kfree(phba->sli4_hba.xri_bmask);
5861 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5862 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5863 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5864 list_del_init(&rsrc_blk->list);
5865 kfree(rsrc_blk);
5866 }
5867 break;
5868 case LPFC_RSC_TYPE_FCOE_VFI:
5869 kfree(phba->sli4_hba.vfi_bmask);
5870 kfree(phba->sli4_hba.vfi_ids);
5871 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5872 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5873 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5874 list_del_init(&rsrc_blk->list);
5875 kfree(rsrc_blk);
5876 }
5877 break;
5878 case LPFC_RSC_TYPE_FCOE_RPI:
5879 /* RPI bitmask and physical id array are cleaned up earlier. */
5880 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5881 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5882 list_del_init(&rsrc_blk->list);
5883 kfree(rsrc_blk);
5884 }
5885 break;
5886 default:
5887 break;
5888 }
5889
5890 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5891
5892 out_free_mbox:
5893 mempool_free(mbox, phba->mbox_mem_pool);
5894 return rc;
5895}
5896
bd4b3e5c 5897static void
7bdedb34
JS
5898lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5899 uint32_t feature)
65791f1f 5900{
65791f1f 5901 uint32_t len;
65791f1f 5902
65791f1f
JS
5903 len = sizeof(struct lpfc_mbx_set_feature) -
5904 sizeof(struct lpfc_sli4_cfg_mhdr);
5905 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5906 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5907 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
5908
5909 switch (feature) {
5910 case LPFC_SET_UE_RECOVERY:
5911 bf_set(lpfc_mbx_set_feature_UER,
5912 &mbox->u.mqe.un.set_feature, 1);
5913 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5914 mbox->u.mqe.un.set_feature.param_len = 8;
5915 break;
5916 case LPFC_SET_MDS_DIAGS:
5917 bf_set(lpfc_mbx_set_feature_mds,
5918 &mbox->u.mqe.un.set_feature, 1);
5919 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
ae9e28f3 5920 &mbox->u.mqe.un.set_feature, 1);
7bdedb34
JS
5921 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5922 mbox->u.mqe.un.set_feature.param_len = 8;
5923 break;
65791f1f 5924 }
7bdedb34
JS
5925
5926 return;
65791f1f
JS
5927}
5928
6d368e53
JS
5929/**
5930 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5931 * @phba: Pointer to HBA context object.
5932 *
5933 * This function allocates all SLI4 resource identifiers.
5934 **/
5935int
5936lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5937{
5938 int i, rc, error = 0;
5939 uint16_t count, base;
5940 unsigned long longs;
5941
ff78d8f9
JS
5942 if (!phba->sli4_hba.rpi_hdrs_in_use)
5943 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5944 if (phba->sli4_hba.extents_in_use) {
5945 /*
5946 * The port supports resource extents. The XRI, VPI, VFI, RPI
5947 * resource extent count must be read and allocated before
5948 * provisioning the resource id arrays.
5949 */
5950 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5951 LPFC_IDX_RSRC_RDY) {
5952 /*
5953 * Extent-based resources are set - the driver could
5954 * be in a port reset. Figure out if any corrective
5955 * actions need to be taken.
5956 */
5957 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5958 LPFC_RSC_TYPE_FCOE_VFI);
5959 if (rc != 0)
5960 error++;
5961 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5962 LPFC_RSC_TYPE_FCOE_VPI);
5963 if (rc != 0)
5964 error++;
5965 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5966 LPFC_RSC_TYPE_FCOE_XRI);
5967 if (rc != 0)
5968 error++;
5969 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5970 LPFC_RSC_TYPE_FCOE_RPI);
5971 if (rc != 0)
5972 error++;
5973
5974 /*
5975 * It's possible that the number of resources
5976 * provided to this port instance changed between
5977 * resets. Detect this condition and reallocate
5978 * resources. Otherwise, there is no action.
5979 */
5980 if (error) {
5981 lpfc_printf_log(phba, KERN_INFO,
5982 LOG_MBOX | LOG_INIT,
5983 "2931 Detected extent resource "
5984 "change. Reallocating all "
5985 "extents.\n");
5986 rc = lpfc_sli4_dealloc_extent(phba,
5987 LPFC_RSC_TYPE_FCOE_VFI);
5988 rc = lpfc_sli4_dealloc_extent(phba,
5989 LPFC_RSC_TYPE_FCOE_VPI);
5990 rc = lpfc_sli4_dealloc_extent(phba,
5991 LPFC_RSC_TYPE_FCOE_XRI);
5992 rc = lpfc_sli4_dealloc_extent(phba,
5993 LPFC_RSC_TYPE_FCOE_RPI);
5994 } else
5995 return 0;
5996 }
5997
5998 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5999 if (unlikely(rc))
6000 goto err_exit;
6001
6002 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6003 if (unlikely(rc))
6004 goto err_exit;
6005
6006 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6007 if (unlikely(rc))
6008 goto err_exit;
6009
6010 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6011 if (unlikely(rc))
6012 goto err_exit;
6013 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6014 LPFC_IDX_RSRC_RDY);
6015 return rc;
6016 } else {
6017 /*
6018 * The port does not support resource extents. The XRI, VPI,
6019 * VFI, RPI resource ids were determined from READ_CONFIG.
6020 * Just allocate the bitmasks and provision the resource id
6021 * arrays. If a port reset is active, the resources don't
6022 * need any action - just exit.
6023 */
6024 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6025 LPFC_IDX_RSRC_RDY) {
6026 lpfc_sli4_dealloc_resource_identifiers(phba);
6027 lpfc_sli4_remove_rpis(phba);
6028 }
6d368e53
JS
6029 /* RPIs. */
6030 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6031 if (count <= 0) {
6032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6033 "3279 Invalid provisioning of "
6034 "rpi:%d\n", count);
6035 rc = -EINVAL;
6036 goto err_exit;
6037 }
6d368e53
JS
6038 base = phba->sli4_hba.max_cfg_param.rpi_base;
6039 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6040 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6041 sizeof(unsigned long),
6042 GFP_KERNEL);
6043 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6044 rc = -ENOMEM;
6045 goto err_exit;
6046 }
6047 phba->sli4_hba.rpi_ids = kzalloc(count *
6048 sizeof(uint16_t),
6049 GFP_KERNEL);
6050 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6051 rc = -ENOMEM;
6052 goto free_rpi_bmask;
6053 }
6054
6055 for (i = 0; i < count; i++)
6056 phba->sli4_hba.rpi_ids[i] = base + i;
6057
6058 /* VPIs. */
6059 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6060 if (count <= 0) {
6061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6062 "3280 Invalid provisioning of "
6063 "vpi:%d\n", count);
6064 rc = -EINVAL;
6065 goto free_rpi_ids;
6066 }
6d368e53
JS
6067 base = phba->sli4_hba.max_cfg_param.vpi_base;
6068 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6069 phba->vpi_bmask = kzalloc(longs *
6070 sizeof(unsigned long),
6071 GFP_KERNEL);
6072 if (unlikely(!phba->vpi_bmask)) {
6073 rc = -ENOMEM;
6074 goto free_rpi_ids;
6075 }
6076 phba->vpi_ids = kzalloc(count *
6077 sizeof(uint16_t),
6078 GFP_KERNEL);
6079 if (unlikely(!phba->vpi_ids)) {
6080 rc = -ENOMEM;
6081 goto free_vpi_bmask;
6082 }
6083
6084 for (i = 0; i < count; i++)
6085 phba->vpi_ids[i] = base + i;
6086
6087 /* XRIs. */
6088 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6089 if (count <= 0) {
6090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6091 "3281 Invalid provisioning of "
6092 "xri:%d\n", count);
6093 rc = -EINVAL;
6094 goto free_vpi_ids;
6095 }
6d368e53
JS
6096 base = phba->sli4_hba.max_cfg_param.xri_base;
6097 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6098 phba->sli4_hba.xri_bmask = kzalloc(longs *
6099 sizeof(unsigned long),
6100 GFP_KERNEL);
6101 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6102 rc = -ENOMEM;
6103 goto free_vpi_ids;
6104 }
41899be7 6105 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6106 phba->sli4_hba.xri_ids = kzalloc(count *
6107 sizeof(uint16_t),
6108 GFP_KERNEL);
6109 if (unlikely(!phba->sli4_hba.xri_ids)) {
6110 rc = -ENOMEM;
6111 goto free_xri_bmask;
6112 }
6113
6114 for (i = 0; i < count; i++)
6115 phba->sli4_hba.xri_ids[i] = base + i;
6116
6117 /* VFIs. */
6118 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6119 if (count <= 0) {
6120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6121 "3282 Invalid provisioning of "
6122 "vfi:%d\n", count);
6123 rc = -EINVAL;
6124 goto free_xri_ids;
6125 }
6d368e53
JS
6126 base = phba->sli4_hba.max_cfg_param.vfi_base;
6127 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6128 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6129 sizeof(unsigned long),
6130 GFP_KERNEL);
6131 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6132 rc = -ENOMEM;
6133 goto free_xri_ids;
6134 }
6135 phba->sli4_hba.vfi_ids = kzalloc(count *
6136 sizeof(uint16_t),
6137 GFP_KERNEL);
6138 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6139 rc = -ENOMEM;
6140 goto free_vfi_bmask;
6141 }
6142
6143 for (i = 0; i < count; i++)
6144 phba->sli4_hba.vfi_ids[i] = base + i;
6145
6146 /*
6147 * Mark all resources ready. An HBA reset doesn't need
6148 * to reset the initialization.
6149 */
6150 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6151 LPFC_IDX_RSRC_RDY);
6152 return 0;
6153 }
6154
6155 free_vfi_bmask:
6156 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6157 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6158 free_xri_ids:
6159 kfree(phba->sli4_hba.xri_ids);
cd60be49 6160 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6161 free_xri_bmask:
6162 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6163 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6164 free_vpi_ids:
6165 kfree(phba->vpi_ids);
cd60be49 6166 phba->vpi_ids = NULL;
6d368e53
JS
6167 free_vpi_bmask:
6168 kfree(phba->vpi_bmask);
cd60be49 6169 phba->vpi_bmask = NULL;
6d368e53
JS
6170 free_rpi_ids:
6171 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6172 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6173 free_rpi_bmask:
6174 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6175 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6176 err_exit:
6177 return rc;
6178}
6179
6180/**
6181 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6182 * @phba: Pointer to HBA context object.
6183 *
6184 * This function allocates the number of elements for the specified
6185 * resource type.
6186 **/
6187int
6188lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6189{
6190 if (phba->sli4_hba.extents_in_use) {
6191 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6192 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6193 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6194 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6195 } else {
6196 kfree(phba->vpi_bmask);
16a3a208 6197 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6198 kfree(phba->vpi_ids);
6199 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6200 kfree(phba->sli4_hba.xri_bmask);
6201 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6202 kfree(phba->sli4_hba.vfi_bmask);
6203 kfree(phba->sli4_hba.vfi_ids);
6204 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6205 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6206 }
6207
6208 return 0;
6209}
6210
b76f2dc9
JS
6211/**
6212 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6213 * @phba: Pointer to HBA context object.
6214 * @type: The resource extent type.
6215 * @extnt_count: buffer to hold port extent count response
6216 * @extnt_size: buffer to hold port extent size response.
6217 *
6218 * This function calls the port to read the host allocated extents
6219 * for a particular type.
6220 **/
6221int
6222lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6223 uint16_t *extnt_cnt, uint16_t *extnt_size)
6224{
6225 bool emb;
6226 int rc = 0;
6227 uint16_t curr_blks = 0;
6228 uint32_t req_len, emb_len;
6229 uint32_t alloc_len, mbox_tmo;
6230 struct list_head *blk_list_head;
6231 struct lpfc_rsrc_blks *rsrc_blk;
6232 LPFC_MBOXQ_t *mbox;
6233 void *virtaddr = NULL;
6234 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6235 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6236 union lpfc_sli4_cfg_shdr *shdr;
6237
6238 switch (type) {
6239 case LPFC_RSC_TYPE_FCOE_VPI:
6240 blk_list_head = &phba->lpfc_vpi_blk_list;
6241 break;
6242 case LPFC_RSC_TYPE_FCOE_XRI:
6243 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6244 break;
6245 case LPFC_RSC_TYPE_FCOE_VFI:
6246 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6247 break;
6248 case LPFC_RSC_TYPE_FCOE_RPI:
6249 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6250 break;
6251 default:
6252 return -EIO;
6253 }
6254
6255 /* Count the number of extents currently allocatd for this type. */
6256 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6257 if (curr_blks == 0) {
6258 /*
6259 * The GET_ALLOCATED mailbox does not return the size,
6260 * just the count. The size should be just the size
6261 * stored in the current allocated block and all sizes
6262 * for an extent type are the same so set the return
6263 * value now.
6264 */
6265 *extnt_size = rsrc_blk->rsrc_size;
6266 }
6267 curr_blks++;
6268 }
6269
b76f2dc9
JS
6270 /*
6271 * Calculate the size of an embedded mailbox. The uint32_t
6272 * accounts for extents-specific word.
6273 */
6274 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6275 sizeof(uint32_t);
6276
6277 /*
6278 * Presume the allocation and response will fit into an embedded
6279 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6280 */
6281 emb = LPFC_SLI4_MBX_EMBED;
6282 req_len = emb_len;
6283 if (req_len > emb_len) {
6284 req_len = curr_blks * sizeof(uint16_t) +
6285 sizeof(union lpfc_sli4_cfg_shdr) +
6286 sizeof(uint32_t);
6287 emb = LPFC_SLI4_MBX_NEMBED;
6288 }
6289
6290 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6291 if (!mbox)
6292 return -ENOMEM;
6293 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6294
6295 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6296 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6297 req_len, emb);
6298 if (alloc_len < req_len) {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6300 "2983 Allocated DMA memory size (x%x) is "
6301 "less than the requested DMA memory "
6302 "size (x%x)\n", alloc_len, req_len);
6303 rc = -ENOMEM;
6304 goto err_exit;
6305 }
6306 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6307 if (unlikely(rc)) {
6308 rc = -EIO;
6309 goto err_exit;
6310 }
6311
6312 if (!phba->sli4_hba.intr_enable)
6313 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6314 else {
a183a15f 6315 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6316 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6317 }
6318
6319 if (unlikely(rc)) {
6320 rc = -EIO;
6321 goto err_exit;
6322 }
6323
6324 /*
6325 * Figure out where the response is located. Then get local pointers
6326 * to the response data. The port does not guarantee to respond to
6327 * all extents counts request so update the local variable with the
6328 * allocated count from the port.
6329 */
6330 if (emb == LPFC_SLI4_MBX_EMBED) {
6331 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6332 shdr = &rsrc_ext->header.cfg_shdr;
6333 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6334 } else {
6335 virtaddr = mbox->sge_array->addr[0];
6336 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6337 shdr = &n_rsrc->cfg_shdr;
6338 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6339 }
6340
6341 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6342 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6343 "2984 Failed to read allocated resources "
6344 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6345 type,
6346 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6347 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6348 rc = -EIO;
6349 goto err_exit;
6350 }
6351 err_exit:
6352 lpfc_sli4_mbox_cmd_free(phba, mbox);
6353 return rc;
6354}
6355
8a9d2e80 6356/**
0ef69968 6357 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
8a9d2e80 6358 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6359 * @pring: Pointer to driver SLI ring object.
6360 * @sgl_list: linked link of sgl buffers to post
6361 * @cnt: number of linked list buffers
8a9d2e80 6362 *
895427bd 6363 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6364 * repost them to the port by using SGL block post. This is needed after a
6365 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6366 * of buffer sgls which contains contiguous xris and uses the non-embedded
6367 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6368 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6369 * mailbox command for posting.
6370 *
6371 * Returns: 0 = success, non-zero failure.
6372 **/
6373static int
895427bd
JS
6374lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6375 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6376{
6377 struct lpfc_sglq *sglq_entry = NULL;
6378 struct lpfc_sglq *sglq_entry_next = NULL;
6379 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6380 int status, total_cnt;
6381 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6382 int last_xritag = NO_XRI;
6383 LIST_HEAD(prep_sgl_list);
6384 LIST_HEAD(blck_sgl_list);
6385 LIST_HEAD(allc_sgl_list);
6386 LIST_HEAD(post_sgl_list);
6387 LIST_HEAD(free_sgl_list);
6388
38c20673 6389 spin_lock_irq(&phba->hbalock);
895427bd
JS
6390 spin_lock(&phba->sli4_hba.sgl_list_lock);
6391 list_splice_init(sgl_list, &allc_sgl_list);
6392 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6393 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6394
895427bd 6395 total_cnt = cnt;
8a9d2e80
JS
6396 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6397 &allc_sgl_list, list) {
6398 list_del_init(&sglq_entry->list);
6399 block_cnt++;
6400 if ((last_xritag != NO_XRI) &&
6401 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6402 /* a hole in xri block, form a sgl posting block */
6403 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6404 post_cnt = block_cnt - 1;
6405 /* prepare list for next posting block */
6406 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6407 block_cnt = 1;
6408 } else {
6409 /* prepare list for next posting block */
6410 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6411 /* enough sgls for non-embed sgl mbox command */
6412 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6413 list_splice_init(&prep_sgl_list,
6414 &blck_sgl_list);
6415 post_cnt = block_cnt;
6416 block_cnt = 0;
6417 }
6418 }
6419 num_posted++;
6420
6421 /* keep track of last sgl's xritag */
6422 last_xritag = sglq_entry->sli4_xritag;
6423
895427bd
JS
6424 /* end of repost sgl list condition for buffers */
6425 if (num_posted == total_cnt) {
8a9d2e80
JS
6426 if (post_cnt == 0) {
6427 list_splice_init(&prep_sgl_list,
6428 &blck_sgl_list);
6429 post_cnt = block_cnt;
6430 } else if (block_cnt == 1) {
6431 status = lpfc_sli4_post_sgl(phba,
6432 sglq_entry->phys, 0,
6433 sglq_entry->sli4_xritag);
6434 if (!status) {
6435 /* successful, put sgl to posted list */
6436 list_add_tail(&sglq_entry->list,
6437 &post_sgl_list);
6438 } else {
6439 /* Failure, put sgl to free list */
6440 lpfc_printf_log(phba, KERN_WARNING,
6441 LOG_SLI,
895427bd 6442 "3159 Failed to post "
8a9d2e80
JS
6443 "sgl, xritag:x%x\n",
6444 sglq_entry->sli4_xritag);
6445 list_add_tail(&sglq_entry->list,
6446 &free_sgl_list);
711ea882 6447 total_cnt--;
8a9d2e80
JS
6448 }
6449 }
6450 }
6451
6452 /* continue until a nembed page worth of sgls */
6453 if (post_cnt == 0)
6454 continue;
6455
895427bd
JS
6456 /* post the buffer list sgls as a block */
6457 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6458 post_cnt);
8a9d2e80
JS
6459
6460 if (!status) {
6461 /* success, put sgl list to posted sgl list */
6462 list_splice_init(&blck_sgl_list, &post_sgl_list);
6463 } else {
6464 /* Failure, put sgl list to free sgl list */
6465 sglq_entry_first = list_first_entry(&blck_sgl_list,
6466 struct lpfc_sglq,
6467 list);
6468 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6469 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6470 "xritag:x%x-x%x\n",
6471 sglq_entry_first->sli4_xritag,
6472 (sglq_entry_first->sli4_xritag +
6473 post_cnt - 1));
6474 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6475 total_cnt -= post_cnt;
8a9d2e80
JS
6476 }
6477
6478 /* don't reset xirtag due to hole in xri block */
6479 if (block_cnt == 0)
6480 last_xritag = NO_XRI;
6481
895427bd 6482 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6483 post_cnt = 0;
6484 }
6485
895427bd 6486 /* free the sgls failed to post */
8a9d2e80
JS
6487 lpfc_free_sgl_list(phba, &free_sgl_list);
6488
895427bd 6489 /* push sgls posted to the available list */
8a9d2e80 6490 if (!list_empty(&post_sgl_list)) {
38c20673 6491 spin_lock_irq(&phba->hbalock);
895427bd
JS
6492 spin_lock(&phba->sli4_hba.sgl_list_lock);
6493 list_splice_init(&post_sgl_list, sgl_list);
6494 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6495 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6496 } else {
6497 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6498 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6499 return -EIO;
6500 }
895427bd
JS
6501
6502 /* return the number of XRIs actually posted */
6503 return total_cnt;
8a9d2e80
JS
6504}
6505
61bda8f7
JS
6506void
6507lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6508{
6509 uint32_t len;
6510
6511 len = sizeof(struct lpfc_mbx_set_host_data) -
6512 sizeof(struct lpfc_sli4_cfg_mhdr);
6513 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6514 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6515 LPFC_SLI4_MBX_EMBED);
6516
6517 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6518 mbox->u.mqe.un.set_host_data.param_len =
6519 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6520 snprintf(mbox->u.mqe.un.set_host_data.data,
6521 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6522 "Linux %s v"LPFC_DRIVER_VERSION,
6523 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6524}
6525
a8cf5dfe 6526int
6c621a22 6527lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
a8cf5dfe 6528 struct lpfc_queue *drq, int count, int idx)
6c621a22
JS
6529{
6530 int rc, i;
6531 struct lpfc_rqe hrqe;
6532 struct lpfc_rqe drqe;
6533 struct lpfc_rqb *rqbp;
6534 struct rqb_dmabuf *rqb_buffer;
6535 LIST_HEAD(rqb_buf_list);
6536
6537 rqbp = hrq->rqbp;
6538 for (i = 0; i < count; i++) {
6539 /* IF RQ is already full, don't bother */
6540 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6541 break;
6542 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6543 if (!rqb_buffer)
6544 break;
6545 rqb_buffer->hrq = hrq;
6546 rqb_buffer->drq = drq;
a8cf5dfe 6547 rqb_buffer->idx = idx;
6c621a22
JS
6548 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6549 }
6550 while (!list_empty(&rqb_buf_list)) {
6551 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6552 hbuf.list);
6553
6554 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6555 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6556 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6557 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6558 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6559 if (rc < 0) {
6560 rqbp->rqb_free_buffer(phba, rqb_buffer);
6561 } else {
6562 list_add_tail(&rqb_buffer->hbuf.list,
6563 &rqbp->rqb_buffer_list);
6564 rqbp->buffer_count++;
6565 }
6566 }
6567 return 1;
6568}
6569
da0436e9 6570/**
183b8021 6571 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
da0436e9
JS
6572 * @phba: Pointer to HBA context object.
6573 *
183b8021
MY
6574 * This function is the main SLI4 device initialization PCI function. This
6575 * function is called by the HBA initialization code, HBA reset code and
da0436e9
JS
6576 * HBA error attention handler code. Caller is not required to hold any
6577 * locks.
6578 **/
6579int
6580lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6581{
6c621a22 6582 int rc, i, cnt;
da0436e9
JS
6583 LPFC_MBOXQ_t *mboxq;
6584 struct lpfc_mqe *mqe;
6585 uint8_t *vpd;
6586 uint32_t vpd_size;
6587 uint32_t ftr_rsp = 0;
6588 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6589 struct lpfc_vport *vport = phba->pport;
6590 struct lpfc_dmabuf *mp;
2d7dbc4c 6591 struct lpfc_rqb *rqbp;
da0436e9
JS
6592
6593 /* Perform a PCI function reset to start from clean */
6594 rc = lpfc_pci_function_reset(phba);
6595 if (unlikely(rc))
6596 return -ENODEV;
6597
6598 /* Check the HBA Host Status Register for readyness */
6599 rc = lpfc_sli4_post_status_check(phba);
6600 if (unlikely(rc))
6601 return -ENODEV;
6602 else {
6603 spin_lock_irq(&phba->hbalock);
6604 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6605 spin_unlock_irq(&phba->hbalock);
6606 }
6607
6608 /*
6609 * Allocate a single mailbox container for initializing the
6610 * port.
6611 */
6612 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6613 if (!mboxq)
6614 return -ENOMEM;
6615
da0436e9 6616 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6617 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6618 vpd = kzalloc(vpd_size, GFP_KERNEL);
6619 if (!vpd) {
6620 rc = -ENOMEM;
6621 goto out_free_mbox;
6622 }
6623
6624 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6625 if (unlikely(rc)) {
6626 kfree(vpd);
6627 goto out_free_mbox;
6628 }
572709e2 6629
da0436e9 6630 mqe = &mboxq->u.mqe;
f1126688 6631 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6632 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6633 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6634 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6635 } else {
76a95d75 6636 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6637 }
45ed1190
JS
6638
6639 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6640 LPFC_DCBX_CEE_MODE)
6641 phba->hba_flag |= HBA_FIP_SUPPORT;
6642 else
6643 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6644
4f2e66c6
JS
6645 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6646
c31098ce 6647 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6648 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6649 "0376 READ_REV Error. SLI Level %d "
6650 "FCoE enabled %d\n",
76a95d75 6651 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6652 rc = -EIO;
76a95d75
JS
6653 kfree(vpd);
6654 goto out_free_mbox;
da0436e9 6655 }
cd1c8301 6656
ff78d8f9
JS
6657 /*
6658 * Continue initialization with default values even if driver failed
6659 * to read FCoE param config regions, only read parameters if the
6660 * board is FCoE
6661 */
6662 if (phba->hba_flag & HBA_FCOE_MODE &&
6663 lpfc_sli4_read_fcoe_params(phba))
6664 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6665 "2570 Failed to read FCoE parameters\n");
6666
cd1c8301
JS
6667 /*
6668 * Retrieve sli4 device physical port name, failure of doing it
6669 * is considered as non-fatal.
6670 */
6671 rc = lpfc_sli4_retrieve_pport_name(phba);
6672 if (!rc)
6673 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6674 "3080 Successful retrieving SLI4 device "
6675 "physical port name: %s.\n", phba->Port);
6676
da0436e9
JS
6677 /*
6678 * Evaluate the read rev and vpd data. Populate the driver
6679 * state with the results. If this routine fails, the failure
6680 * is not fatal as the driver will use generic values.
6681 */
6682 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6683 if (unlikely(!rc)) {
6684 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6685 "0377 Error %d parsing vpd. "
6686 "Using defaults.\n", rc);
6687 rc = 0;
6688 }
76a95d75 6689 kfree(vpd);
da0436e9 6690
f1126688
JS
6691 /* Save information as VPD data */
6692 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6693 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6694 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6695 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6696 &mqe->un.read_rev);
6697 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6698 &mqe->un.read_rev);
6699 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6700 &mqe->un.read_rev);
6701 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6702 &mqe->un.read_rev);
6703 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6704 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6705 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6706 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6707 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6708 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6709 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6710 "(%d):0380 READ_REV Status x%x "
6711 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6712 mboxq->vport ? mboxq->vport->vpi : 0,
6713 bf_get(lpfc_mqe_status, mqe),
6714 phba->vpd.rev.opFwName,
6715 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6716 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6717
572709e2
JS
6718 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6719 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6720 if (phba->pport->cfg_lun_queue_depth > rc) {
6721 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6722 "3362 LUN queue depth changed from %d to %d\n",
6723 phba->pport->cfg_lun_queue_depth, rc);
6724 phba->pport->cfg_lun_queue_depth = rc;
6725 }
6726
65791f1f 6727 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6728 LPFC_SLI_INTF_IF_TYPE_0) {
6729 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6730 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6731 if (rc == MBX_SUCCESS) {
6732 phba->hba_flag |= HBA_RECOVERABLE_UE;
6733 /* Set 1Sec interval to detect UE */
6734 phba->eratt_poll_interval = 1;
6735 phba->sli4_hba.ue_to_sr = bf_get(
6736 lpfc_mbx_set_feature_UESR,
6737 &mboxq->u.mqe.un.set_feature);
6738 phba->sli4_hba.ue_to_rp = bf_get(
6739 lpfc_mbx_set_feature_UERP,
6740 &mboxq->u.mqe.un.set_feature);
6741 }
6742 }
6743
6744 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6745 /* Enable MDS Diagnostics only if the SLI Port supports it */
6746 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6748 if (rc != MBX_SUCCESS)
6749 phba->mds_diags_support = 0;
6750 }
572709e2 6751
da0436e9
JS
6752 /*
6753 * Discover the port's supported feature set and match it against the
6754 * hosts requests.
6755 */
6756 lpfc_request_features(phba, mboxq);
6757 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6758 if (unlikely(rc)) {
6759 rc = -EIO;
76a95d75 6760 goto out_free_mbox;
da0436e9
JS
6761 }
6762
6763 /*
6764 * The port must support FCP initiator mode as this is the
6765 * only mode running in the host.
6766 */
6767 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6768 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6769 "0378 No support for fcpi mode.\n");
6770 ftr_rsp++;
6771 }
fedd3b7b
JS
6772 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6773 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6774 else
6775 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6776 /*
6777 * If the port cannot support the host's requested features
6778 * then turn off the global config parameters to disable the
6779 * feature in the driver. This is not a fatal error.
6780 */
bf08611b
JS
6781 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6782 if (phba->cfg_enable_bg) {
6783 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6784 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6785 else
6786 ftr_rsp++;
6787 }
da0436e9
JS
6788
6789 if (phba->max_vpi && phba->cfg_enable_npiv &&
6790 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6791 ftr_rsp++;
6792
6793 if (ftr_rsp) {
6794 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6795 "0379 Feature Mismatch Data: x%08x %08x "
6796 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6797 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6798 phba->cfg_enable_npiv, phba->max_vpi);
6799 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6800 phba->cfg_enable_bg = 0;
6801 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6802 phba->cfg_enable_npiv = 0;
6803 }
6804
6805 /* These SLI3 features are assumed in SLI4 */
6806 spin_lock_irq(&phba->hbalock);
6807 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6808 spin_unlock_irq(&phba->hbalock);
6809
6d368e53
JS
6810 /*
6811 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6812 * calls depends on these resources to complete port setup.
6813 */
6814 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6815 if (rc) {
6816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6817 "2920 Failed to alloc Resource IDs "
6818 "rc = x%x\n", rc);
6819 goto out_free_mbox;
6820 }
6821
61bda8f7
JS
6822 lpfc_set_host_data(phba, mboxq);
6823
6824 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6825 if (rc) {
6826 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6827 "2134 Failed to set host os driver version %x",
6828 rc);
6829 }
6830
da0436e9 6831 /* Read the port's service parameters. */
9f1177a3
JS
6832 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6833 if (rc) {
6834 phba->link_state = LPFC_HBA_ERROR;
6835 rc = -ENOMEM;
76a95d75 6836 goto out_free_mbox;
9f1177a3
JS
6837 }
6838
da0436e9
JS
6839 mboxq->vport = vport;
6840 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6841 mp = (struct lpfc_dmabuf *) mboxq->context1;
6842 if (rc == MBX_SUCCESS) {
6843 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6844 rc = 0;
6845 }
6846
6847 /*
6848 * This memory was allocated by the lpfc_read_sparam routine. Release
6849 * it to the mbuf pool.
6850 */
6851 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6852 kfree(mp);
6853 mboxq->context1 = NULL;
6854 if (unlikely(rc)) {
6855 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6856 "0382 READ_SPARAM command failed "
6857 "status %d, mbxStatus x%x\n",
6858 rc, bf_get(lpfc_mqe_status, mqe));
6859 phba->link_state = LPFC_HBA_ERROR;
6860 rc = -EIO;
76a95d75 6861 goto out_free_mbox;
da0436e9
JS
6862 }
6863
0558056c 6864 lpfc_update_vport_wwn(vport);
da0436e9
JS
6865
6866 /* Update the fc_host data structures with new wwn. */
6867 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6868 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6869
895427bd
JS
6870 /* Create all the SLI4 queues */
6871 rc = lpfc_sli4_queue_create(phba);
6872 if (rc) {
6873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6874 "3089 Failed to allocate queues\n");
6875 rc = -ENODEV;
6876 goto out_free_mbox;
6877 }
6878 /* Set up all the queues to the device */
6879 rc = lpfc_sli4_queue_setup(phba);
6880 if (unlikely(rc)) {
6881 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6882 "0381 Error %d during queue setup.\n ", rc);
6883 goto out_stop_timers;
6884 }
6885 /* Initialize the driver internal SLI layer lists. */
6886 lpfc_sli4_setup(phba);
6887 lpfc_sli4_queue_init(phba);
6888
6889 /* update host els xri-sgl sizes and mappings */
6890 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
6891 if (unlikely(rc)) {
6892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6893 "1400 Failed to update xri-sgl size and "
6894 "mapping: %d\n", rc);
895427bd 6895 goto out_destroy_queue;
da0436e9
JS
6896 }
6897
8a9d2e80 6898 /* register the els sgl pool to the port */
895427bd
JS
6899 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6900 phba->sli4_hba.els_xri_cnt);
6901 if (unlikely(rc < 0)) {
8a9d2e80
JS
6902 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6903 "0582 Error %d during els sgl post "
6904 "operation\n", rc);
6905 rc = -ENODEV;
895427bd 6906 goto out_destroy_queue;
8a9d2e80 6907 }
895427bd 6908 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 6909
f358dd0c
JS
6910 if (phba->nvmet_support) {
6911 /* update host nvmet xri-sgl sizes and mappings */
6912 rc = lpfc_sli4_nvmet_sgl_update(phba);
6913 if (unlikely(rc)) {
6914 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6915 "6308 Failed to update nvmet-sgl size "
6916 "and mapping: %d\n", rc);
6917 goto out_destroy_queue;
6918 }
6919
6920 /* register the nvmet sgl pool to the port */
6921 rc = lpfc_sli4_repost_sgl_list(
6922 phba,
6923 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6924 phba->sli4_hba.nvmet_xri_cnt);
6925 if (unlikely(rc < 0)) {
6926 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6927 "3117 Error %d during nvmet "
6928 "sgl post\n", rc);
6929 rc = -ENODEV;
6930 goto out_destroy_queue;
6931 }
6932 phba->sli4_hba.nvmet_xri_cnt = rc;
6c621a22
JS
6933
6934 cnt = phba->cfg_iocb_cnt * 1024;
6935 /* We need 1 iocbq for every SGL, for IO processing */
6936 cnt += phba->sli4_hba.nvmet_xri_cnt;
f358dd0c 6937 } else {
895427bd
JS
6938 /* update host scsi xri-sgl sizes and mappings */
6939 rc = lpfc_sli4_scsi_sgl_update(phba);
6940 if (unlikely(rc)) {
6941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6942 "6309 Failed to update scsi-sgl size "
6943 "and mapping: %d\n", rc);
6944 goto out_destroy_queue;
6945 }
6946
6947 /* update host nvme xri-sgl sizes and mappings */
6948 rc = lpfc_sli4_nvme_sgl_update(phba);
6949 if (unlikely(rc)) {
6950 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6951 "6082 Failed to update nvme-sgl size "
6952 "and mapping: %d\n", rc);
6953 goto out_destroy_queue;
6954 }
6c621a22
JS
6955
6956 cnt = phba->cfg_iocb_cnt * 1024;
11e644e2
JS
6957 }
6958
6959 if (!phba->sli.iocbq_lookup) {
6c621a22
JS
6960 /* Initialize and populate the iocb list per host */
6961 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11e644e2 6962 "2821 initialize iocb list %d total %d\n",
6c621a22
JS
6963 phba->cfg_iocb_cnt, cnt);
6964 rc = lpfc_init_iocb_list(phba, cnt);
6965 if (rc) {
6966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11e644e2 6967 "1413 Failed to init iocb list.\n");
6c621a22
JS
6968 goto out_destroy_queue;
6969 }
895427bd
JS
6970 }
6971
11e644e2
JS
6972 if (phba->nvmet_support)
6973 lpfc_nvmet_create_targetport(phba);
6974
2d7dbc4c 6975 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
2d7dbc4c
JS
6976 /* Post initial buffers to all RQs created */
6977 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6978 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6979 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6980 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6981 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
61f3d4bf 6982 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
2d7dbc4c
JS
6983 rqbp->buffer_count = 0;
6984
2d7dbc4c
JS
6985 lpfc_post_rq_buffer(
6986 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6987 phba->sli4_hba.nvmet_mrq_data[i],
a8cf5dfe 6988 LPFC_NVMET_RQE_DEF_COUNT, i);
2d7dbc4c
JS
6989 }
6990 }
6991
895427bd
JS
6992 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6993 /* register the allocated scsi sgl pool to the port */
6994 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6995 if (unlikely(rc)) {
6996 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6997 "0383 Error %d during scsi sgl post "
6998 "operation\n", rc);
6999 /* Some Scsi buffers were moved to abort scsi list */
7000 /* A pci function reset will repost them */
7001 rc = -ENODEV;
7002 goto out_destroy_queue;
7003 }
da0436e9
JS
7004 }
7005
01649561
JS
7006 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7007 (phba->nvmet_support == 0)) {
7008
7009 /* register the allocated nvme sgl pool to the port */
7010 rc = lpfc_repost_nvme_sgl_list(phba);
7011 if (unlikely(rc)) {
7012 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7013 "6116 Error %d during nvme sgl post "
7014 "operation\n", rc);
7015 /* Some NVME buffers were moved to abort nvme list */
7016 /* A pci function reset will repost them */
7017 rc = -ENODEV;
7018 goto out_destroy_queue;
7019 }
da0436e9
JS
7020 }
7021
7022 /* Post the rpi header region to the device. */
7023 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7024 if (unlikely(rc)) {
7025 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7026 "0393 Error %d during rpi post operation\n",
7027 rc);
7028 rc = -ENODEV;
895427bd 7029 goto out_destroy_queue;
da0436e9 7030 }
97f2ecf1 7031 lpfc_sli4_node_prep(phba);
da0436e9 7032
895427bd 7033 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 7034 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
7035 /*
7036 * The FC Port needs to register FCFI (index 0)
7037 */
7038 lpfc_reg_fcfi(phba, mboxq);
7039 mboxq->vport = phba->pport;
7040 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7041 if (rc != MBX_SUCCESS)
7042 goto out_unset_queue;
7043 rc = 0;
7044 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7045 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
7046 } else {
7047 /* We are a NVME Target mode with MRQ > 1 */
7048
7049 /* First register the FCFI */
7050 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7051 mboxq->vport = phba->pport;
7052 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7053 if (rc != MBX_SUCCESS)
7054 goto out_unset_queue;
7055 rc = 0;
7056 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7057 &mboxq->u.mqe.un.reg_fcfi_mrq);
7058
7059 /* Next register the MRQs */
7060 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7061 mboxq->vport = phba->pport;
7062 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7063 if (rc != MBX_SUCCESS)
7064 goto out_unset_queue;
7065 rc = 0;
895427bd
JS
7066 }
7067 /* Check if the port is configured to be disabled */
7068 lpfc_sli_read_link_ste(phba);
da0436e9
JS
7069 }
7070
7071 /* Arm the CQs and then EQs on device */
7072 lpfc_sli4_arm_cqeq_intr(phba);
7073
7074 /* Indicate device interrupt mode */
7075 phba->sli4_hba.intr_enable = 1;
7076
7077 /* Allow asynchronous mailbox command to go through */
7078 spin_lock_irq(&phba->hbalock);
7079 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7080 spin_unlock_irq(&phba->hbalock);
7081
7082 /* Post receive buffers to the device */
7083 lpfc_sli4_rb_setup(phba);
7084
fc2b989b
JS
7085 /* Reset HBA FCF states after HBA reset */
7086 phba->fcf.fcf_flag = 0;
7087 phba->fcf.current_rec.flag = 0;
7088
da0436e9 7089 /* Start the ELS watchdog timer */
8fa38513 7090 mod_timer(&vport->els_tmofunc,
256ec0d0 7091 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7092
7093 /* Start heart beat timer */
7094 mod_timer(&phba->hb_tmofunc,
256ec0d0 7095 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7096 phba->hb_outstanding = 0;
7097 phba->last_completion_time = jiffies;
7098
7099 /* Start error attention (ERATT) polling timer */
256ec0d0 7100 mod_timer(&phba->eratt_poll,
65791f1f 7101 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7102
75baf696
JS
7103 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7104 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7105 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7106 if (!rc) {
7107 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7108 "2829 This device supports "
7109 "Advanced Error Reporting (AER)\n");
7110 spin_lock_irq(&phba->hbalock);
7111 phba->hba_flag |= HBA_AER_ENABLED;
7112 spin_unlock_irq(&phba->hbalock);
7113 } else {
7114 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7115 "2830 This device does not support "
7116 "Advanced Error Reporting (AER)\n");
7117 phba->cfg_aer_support = 0;
7118 }
0a96e975 7119 rc = 0;
75baf696
JS
7120 }
7121
da0436e9
JS
7122 /*
7123 * The port is ready, set the host's link state to LINK_DOWN
7124 * in preparation for link interrupts.
7125 */
da0436e9
JS
7126 spin_lock_irq(&phba->hbalock);
7127 phba->link_state = LPFC_LINK_DOWN;
7128 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7129 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7130 (phba->hba_flag & LINK_DISABLED)) {
7131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7132 "3103 Adapter Link is disabled.\n");
7133 lpfc_down_link(phba, mboxq);
7134 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7135 if (rc != MBX_SUCCESS) {
7136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7137 "3104 Adapter failed to issue "
7138 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7139 goto out_unset_queue;
7140 }
7141 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7142 /* don't perform init_link on SLI4 FC port loopback test */
7143 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7144 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7145 if (rc)
7146 goto out_unset_queue;
7147 }
5350d872
JS
7148 }
7149 mempool_free(mboxq, phba->mbox_mem_pool);
7150 return rc;
76a95d75 7151out_unset_queue:
da0436e9 7152 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7153 lpfc_sli4_queue_unset(phba);
7154out_destroy_queue:
6c621a22 7155 lpfc_free_iocb_list(phba);
5350d872 7156 lpfc_sli4_queue_destroy(phba);
da0436e9 7157out_stop_timers:
5350d872 7158 lpfc_stop_hba_timers(phba);
da0436e9
JS
7159out_free_mbox:
7160 mempool_free(mboxq, phba->mbox_mem_pool);
7161 return rc;
7162}
7163
7164/**
7165 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7166 * @ptr: context object - pointer to hba structure.
7167 *
7168 * This is the callback function for mailbox timer. The mailbox
7169 * timer is armed when a new mailbox command is issued and the timer
7170 * is deleted when the mailbox complete. The function is called by
7171 * the kernel timer code when a mailbox does not complete within
7172 * expected time. This function wakes up the worker thread to
7173 * process the mailbox timeout and returns. All the processing is
7174 * done by the worker thread function lpfc_mbox_timeout_handler.
7175 **/
7176void
f22eb4d3 7177lpfc_mbox_timeout(struct timer_list *t)
da0436e9 7178{
f22eb4d3 7179 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
da0436e9
JS
7180 unsigned long iflag;
7181 uint32_t tmo_posted;
7182
7183 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7184 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7185 if (!tmo_posted)
7186 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7187 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7188
7189 if (!tmo_posted)
7190 lpfc_worker_wake_up(phba);
7191 return;
7192}
7193
e8d3c3b1
JS
7194/**
7195 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7196 * are pending
7197 * @phba: Pointer to HBA context object.
7198 *
7199 * This function checks if any mailbox completions are present on the mailbox
7200 * completion queue.
7201 **/
3bb11fc5 7202static bool
e8d3c3b1
JS
7203lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7204{
7205
7206 uint32_t idx;
7207 struct lpfc_queue *mcq;
7208 struct lpfc_mcqe *mcqe;
7209 bool pending_completions = false;
7210
7211 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7212 return false;
7213
7214 /* Check for completions on mailbox completion queue */
7215
7216 mcq = phba->sli4_hba.mbx_cq;
7217 idx = mcq->hba_index;
7218 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7219 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7220 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7221 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7222 pending_completions = true;
7223 break;
7224 }
7225 idx = (idx + 1) % mcq->entry_count;
7226 if (mcq->hba_index == idx)
7227 break;
7228 }
7229 return pending_completions;
7230
7231}
7232
7233/**
7234 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7235 * that were missed.
7236 * @phba: Pointer to HBA context object.
7237 *
7238 * For sli4, it is possible to miss an interrupt. As such mbox completions
7239 * maybe missed causing erroneous mailbox timeouts to occur. This function
7240 * checks to see if mbox completions are on the mailbox completion queue
7241 * and will process all the completions associated with the eq for the
7242 * mailbox completion queue.
7243 **/
7244bool
7245lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7246{
7247
7248 uint32_t eqidx;
7249 struct lpfc_queue *fpeq = NULL;
7250 struct lpfc_eqe *eqe;
7251 bool mbox_pending;
7252
7253 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7254 return false;
7255
7256 /* Find the eq associated with the mcq */
7257
7258 if (phba->sli4_hba.hba_eq)
895427bd 7259 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
e8d3c3b1
JS
7260 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7261 phba->sli4_hba.mbx_cq->assoc_qid) {
7262 fpeq = phba->sli4_hba.hba_eq[eqidx];
7263 break;
7264 }
7265 if (!fpeq)
7266 return false;
7267
7268 /* Turn off interrupts from this EQ */
7269
7270 lpfc_sli4_eq_clr_intr(fpeq);
7271
7272 /* Check to see if a mbox completion is pending */
7273
7274 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7275
7276 /*
7277 * If a mbox completion is pending, process all the events on EQ
7278 * associated with the mbox completion queue (this could include
7279 * mailbox commands, async events, els commands, receive queue data
7280 * and fcp commands)
7281 */
7282
7283 if (mbox_pending)
7284 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7285 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7286 fpeq->EQ_processed++;
7287 }
7288
7289 /* Always clear and re-arm the EQ */
7290
7291 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7292
7293 return mbox_pending;
7294
7295}
da0436e9
JS
7296
7297/**
7298 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7299 * @phba: Pointer to HBA context object.
7300 *
7301 * This function is called from worker thread when a mailbox command times out.
7302 * The caller is not required to hold any locks. This function will reset the
7303 * HBA and recover all the pending commands.
7304 **/
7305void
7306lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7307{
7308 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7309 MAILBOX_t *mb = NULL;
7310
da0436e9 7311 struct lpfc_sli *psli = &phba->sli;
da0436e9 7312
e8d3c3b1
JS
7313 /* If the mailbox completed, process the completion and return */
7314 if (lpfc_sli4_process_missed_mbox_completions(phba))
7315 return;
7316
eb016566
JS
7317 if (pmbox != NULL)
7318 mb = &pmbox->u.mb;
da0436e9
JS
7319 /* Check the pmbox pointer first. There is a race condition
7320 * between the mbox timeout handler getting executed in the
7321 * worklist and the mailbox actually completing. When this
7322 * race condition occurs, the mbox_active will be NULL.
7323 */
7324 spin_lock_irq(&phba->hbalock);
7325 if (pmbox == NULL) {
7326 lpfc_printf_log(phba, KERN_WARNING,
7327 LOG_MBOX | LOG_SLI,
7328 "0353 Active Mailbox cleared - mailbox timeout "
7329 "exiting\n");
7330 spin_unlock_irq(&phba->hbalock);
7331 return;
7332 }
7333
7334 /* Mbox cmd <mbxCommand> timeout */
7335 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7336 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7337 mb->mbxCommand,
7338 phba->pport->port_state,
7339 phba->sli.sli_flag,
7340 phba->sli.mbox_active);
7341 spin_unlock_irq(&phba->hbalock);
7342
7343 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7344 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7345 * it to fail all outstanding SCSI IO.
da0436e9
JS
7346 */
7347 spin_lock_irq(&phba->pport->work_port_lock);
7348 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7349 spin_unlock_irq(&phba->pport->work_port_lock);
7350 spin_lock_irq(&phba->hbalock);
7351 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7352 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7353 spin_unlock_irq(&phba->hbalock);
7354
db55fba8 7355 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7356
7357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7358 "0345 Resetting board due to mailbox timeout\n");
7359
7360 /* Reset the HBA device */
7361 lpfc_reset_hba(phba);
7362}
7363
7364/**
7365 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7366 * @phba: Pointer to HBA context object.
7367 * @pmbox: Pointer to mailbox object.
7368 * @flag: Flag indicating how the mailbox need to be processed.
7369 *
7370 * This function is called by discovery code and HBA management code
7371 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7372 * function gets the hbalock to protect the data structures.
7373 * The mailbox command can be submitted in polling mode, in which case
7374 * this function will wait in a polling loop for the completion of the
7375 * mailbox.
7376 * If the mailbox is submitted in no_wait mode (not polling) the
7377 * function will submit the command and returns immediately without waiting
7378 * for the mailbox completion. The no_wait is supported only when HBA
7379 * is in SLI2/SLI3 mode - interrupts are enabled.
7380 * The SLI interface allows only one mailbox pending at a time. If the
7381 * mailbox is issued in polling mode and there is already a mailbox
7382 * pending, then the function will return an error. If the mailbox is issued
7383 * in NO_WAIT mode and there is a mailbox pending already, the function
7384 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7385 * The sli layer owns the mailbox object until the completion of mailbox
7386 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7387 * return codes the caller owns the mailbox command after the return of
7388 * the function.
e59058c4 7389 **/
3772a991
JS
7390static int
7391lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7392 uint32_t flag)
dea3101e 7393{
bf07bdea 7394 MAILBOX_t *mbx;
2e0fef85 7395 struct lpfc_sli *psli = &phba->sli;
dea3101e 7396 uint32_t status, evtctr;
9940b97b 7397 uint32_t ha_copy, hc_copy;
dea3101e 7398 int i;
09372820 7399 unsigned long timeout;
dea3101e 7400 unsigned long drvr_flag = 0;
34b02dcd 7401 uint32_t word0, ldata;
dea3101e 7402 void __iomem *to_slim;
58da1ffb
JS
7403 int processing_queue = 0;
7404
7405 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7406 if (!pmbox) {
8568a4d2 7407 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7408 /* processing mbox queue from intr_handler */
3772a991
JS
7409 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7410 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7411 return MBX_SUCCESS;
7412 }
58da1ffb 7413 processing_queue = 1;
58da1ffb
JS
7414 pmbox = lpfc_mbox_get(phba);
7415 if (!pmbox) {
7416 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7417 return MBX_SUCCESS;
7418 }
7419 }
dea3101e 7420
ed957684 7421 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7422 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7423 if(!pmbox->vport) {
58da1ffb 7424 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7425 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7426 LOG_MBOX | LOG_VPORT,
e8b62011 7427 "1806 Mbox x%x failed. No vport\n",
3772a991 7428 pmbox->u.mb.mbxCommand);
ed957684 7429 dump_stack();
58da1ffb 7430 goto out_not_finished;
ed957684
JS
7431 }
7432 }
7433
8d63f375 7434 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7435 if (unlikely(pci_channel_offline(phba->pcidev))) {
7436 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7437 goto out_not_finished;
7438 }
8d63f375 7439
a257bf90
JS
7440 /* If HBA has a deferred error attention, fail the iocb. */
7441 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7442 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7443 goto out_not_finished;
7444 }
7445
dea3101e 7446 psli = &phba->sli;
92d7f7b0 7447
bf07bdea 7448 mbx = &pmbox->u.mb;
dea3101e
JB
7449 status = MBX_SUCCESS;
7450
2e0fef85
JS
7451 if (phba->link_state == LPFC_HBA_ERROR) {
7452 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7453
7454 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7455 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7456 "(%d):0311 Mailbox command x%x cannot "
7457 "issue Data: x%x x%x\n",
7458 pmbox->vport ? pmbox->vport->vpi : 0,
7459 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7460 goto out_not_finished;
41415862
JW
7461 }
7462
bf07bdea 7463 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7464 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7465 !(hc_copy & HC_MBINT_ENA)) {
7466 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7467 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7468 "(%d):2528 Mailbox command x%x cannot "
7469 "issue Data: x%x x%x\n",
7470 pmbox->vport ? pmbox->vport->vpi : 0,
7471 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7472 goto out_not_finished;
7473 }
9290831f
JS
7474 }
7475
dea3101e
JB
7476 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7477 /* Polling for a mbox command when another one is already active
7478 * is not allowed in SLI. Also, the driver must have established
7479 * SLI2 mode to queue and process multiple mbox commands.
7480 */
7481
7482 if (flag & MBX_POLL) {
2e0fef85 7483 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7484
7485 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7487 "(%d):2529 Mailbox command x%x "
7488 "cannot issue Data: x%x x%x\n",
7489 pmbox->vport ? pmbox->vport->vpi : 0,
7490 pmbox->u.mb.mbxCommand,
7491 psli->sli_flag, flag);
58da1ffb 7492 goto out_not_finished;
dea3101e
JB
7493 }
7494
3772a991 7495 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7496 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7497 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7498 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7499 "(%d):2530 Mailbox command x%x "
7500 "cannot issue Data: x%x x%x\n",
7501 pmbox->vport ? pmbox->vport->vpi : 0,
7502 pmbox->u.mb.mbxCommand,
7503 psli->sli_flag, flag);
58da1ffb 7504 goto out_not_finished;
dea3101e
JB
7505 }
7506
dea3101e
JB
7507 /* Another mailbox command is still being processed, queue this
7508 * command to be processed later.
7509 */
7510 lpfc_mbox_put(phba, pmbox);
7511
7512 /* Mbox cmd issue - BUSY */
ed957684 7513 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7514 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7515 "x%x x%x x%x x%x\n",
92d7f7b0 7516 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
e92974f6
JS
7517 mbx->mbxCommand,
7518 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7519 psli->sli_flag, flag);
dea3101e
JB
7520
7521 psli->slistat.mbox_busy++;
2e0fef85 7522 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7523
858c9f6c
JS
7524 if (pmbox->vport) {
7525 lpfc_debugfs_disc_trc(pmbox->vport,
7526 LPFC_DISC_TRC_MBOX_VPORT,
7527 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7528 (uint32_t)mbx->mbxCommand,
7529 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7530 }
7531 else {
7532 lpfc_debugfs_disc_trc(phba->pport,
7533 LPFC_DISC_TRC_MBOX,
7534 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7535 (uint32_t)mbx->mbxCommand,
7536 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7537 }
7538
2e0fef85 7539 return MBX_BUSY;
dea3101e
JB
7540 }
7541
dea3101e
JB
7542 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7543
7544 /* If we are not polling, we MUST be in SLI2 mode */
7545 if (flag != MBX_POLL) {
3772a991 7546 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7547 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7548 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7549 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7550 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7551 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7552 "(%d):2531 Mailbox command x%x "
7553 "cannot issue Data: x%x x%x\n",
7554 pmbox->vport ? pmbox->vport->vpi : 0,
7555 pmbox->u.mb.mbxCommand,
7556 psli->sli_flag, flag);
58da1ffb 7557 goto out_not_finished;
dea3101e
JB
7558 }
7559 /* timeout active mbox command */
256ec0d0
JS
7560 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7561 1000);
7562 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7563 }
7564
7565 /* Mailbox cmd <cmd> issue */
ed957684 7566 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7567 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7568 "x%x\n",
e8b62011 7569 pmbox->vport ? pmbox->vport->vpi : 0,
e92974f6
JS
7570 mbx->mbxCommand,
7571 phba->pport ? phba->pport->port_state : 0xff,
92d7f7b0 7572 psli->sli_flag, flag);
dea3101e 7573
bf07bdea 7574 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7575 if (pmbox->vport) {
7576 lpfc_debugfs_disc_trc(pmbox->vport,
7577 LPFC_DISC_TRC_MBOX_VPORT,
7578 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7579 (uint32_t)mbx->mbxCommand,
7580 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7581 }
7582 else {
7583 lpfc_debugfs_disc_trc(phba->pport,
7584 LPFC_DISC_TRC_MBOX,
7585 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7586 (uint32_t)mbx->mbxCommand,
7587 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7588 }
7589 }
7590
dea3101e
JB
7591 psli->slistat.mbox_cmd++;
7592 evtctr = psli->slistat.mbox_event;
7593
7594 /* next set own bit for the adapter and copy over command word */
bf07bdea 7595 mbx->mbxOwner = OWN_CHIP;
dea3101e 7596
3772a991 7597 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7598 /* Populate mbox extension offset word. */
7599 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7600 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7601 = (uint8_t *)phba->mbox_ext
7602 - (uint8_t *)phba->mbox;
7603 }
7604
7605 /* Copy the mailbox extension data */
7606 if (pmbox->in_ext_byte_len && pmbox->context2) {
7607 lpfc_sli_pcimem_bcopy(pmbox->context2,
7608 (uint8_t *)phba->mbox_ext,
7609 pmbox->in_ext_byte_len);
7610 }
7611 /* Copy command data to host SLIM area */
bf07bdea 7612 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7613 } else {
7a470277
JS
7614 /* Populate mbox extension offset word. */
7615 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7616 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7617 = MAILBOX_HBA_EXT_OFFSET;
7618
7619 /* Copy the mailbox extension data */
895427bd 7620 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7621 lpfc_memcpy_to_slim(phba->MBslimaddr +
7622 MAILBOX_HBA_EXT_OFFSET,
7623 pmbox->context2, pmbox->in_ext_byte_len);
7624
895427bd 7625 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7626 /* copy command data into host mbox for cmpl */
895427bd
JS
7627 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7628 MAILBOX_CMD_SIZE);
dea3101e
JB
7629
7630 /* First copy mbox command data to HBA SLIM, skip past first
7631 word */
7632 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7633 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7634 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7635
7636 /* Next copy over first word, with mbxOwner set */
bf07bdea 7637 ldata = *((uint32_t *)mbx);
dea3101e
JB
7638 to_slim = phba->MBslimaddr;
7639 writel(ldata, to_slim);
7640 readl(to_slim); /* flush */
7641
895427bd 7642 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7643 /* switch over to host mailbox */
3772a991 7644 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7645 }
7646
7647 wmb();
dea3101e
JB
7648
7649 switch (flag) {
7650 case MBX_NOWAIT:
09372820 7651 /* Set up reference to mailbox command */
dea3101e 7652 psli->mbox_active = pmbox;
09372820
JS
7653 /* Interrupt board to do it */
7654 writel(CA_MBATT, phba->CAregaddr);
7655 readl(phba->CAregaddr); /* flush */
7656 /* Don't wait for it to finish, just return */
dea3101e
JB
7657 break;
7658
7659 case MBX_POLL:
09372820 7660 /* Set up null reference to mailbox command */
dea3101e 7661 psli->mbox_active = NULL;
09372820
JS
7662 /* Interrupt board to do it */
7663 writel(CA_MBATT, phba->CAregaddr);
7664 readl(phba->CAregaddr); /* flush */
7665
3772a991 7666 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7667 /* First read mbox status word */
34b02dcd 7668 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7669 word0 = le32_to_cpu(word0);
7670 } else {
7671 /* First read mbox status word */
9940b97b
JS
7672 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7673 spin_unlock_irqrestore(&phba->hbalock,
7674 drvr_flag);
7675 goto out_not_finished;
7676 }
dea3101e
JB
7677 }
7678
7679 /* Read the HBA Host Attention Register */
9940b97b
JS
7680 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7681 spin_unlock_irqrestore(&phba->hbalock,
7682 drvr_flag);
7683 goto out_not_finished;
7684 }
a183a15f
JS
7685 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7686 1000) + jiffies;
09372820 7687 i = 0;
dea3101e 7688 /* Wait for command to complete */
41415862
JW
7689 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7690 (!(ha_copy & HA_MBATT) &&
2e0fef85 7691 (phba->link_state > LPFC_WARM_START))) {
09372820 7692 if (time_after(jiffies, timeout)) {
dea3101e 7693 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7694 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7695 drvr_flag);
58da1ffb 7696 goto out_not_finished;
dea3101e
JB
7697 }
7698
7699 /* Check if we took a mbox interrupt while we were
7700 polling */
7701 if (((word0 & OWN_CHIP) != OWN_CHIP)
7702 && (evtctr != psli->slistat.mbox_event))
7703 break;
7704
09372820
JS
7705 if (i++ > 10) {
7706 spin_unlock_irqrestore(&phba->hbalock,
7707 drvr_flag);
7708 msleep(1);
7709 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7710 }
dea3101e 7711
3772a991 7712 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7713 /* First copy command data */
34b02dcd 7714 word0 = *((uint32_t *)phba->mbox);
dea3101e 7715 word0 = le32_to_cpu(word0);
bf07bdea 7716 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7717 MAILBOX_t *slimmb;
34b02dcd 7718 uint32_t slimword0;
dea3101e
JB
7719 /* Check real SLIM for any errors */
7720 slimword0 = readl(phba->MBslimaddr);
7721 slimmb = (MAILBOX_t *) & slimword0;
7722 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7723 && slimmb->mbxStatus) {
7724 psli->sli_flag &=
3772a991 7725 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7726 word0 = slimword0;
7727 }
7728 }
7729 } else {
7730 /* First copy command data */
7731 word0 = readl(phba->MBslimaddr);
7732 }
7733 /* Read the HBA Host Attention Register */
9940b97b
JS
7734 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7735 spin_unlock_irqrestore(&phba->hbalock,
7736 drvr_flag);
7737 goto out_not_finished;
7738 }
dea3101e
JB
7739 }
7740
3772a991 7741 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7742 /* copy results back to user */
2ea259ee
JS
7743 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7744 MAILBOX_CMD_SIZE);
7a470277
JS
7745 /* Copy the mailbox extension data */
7746 if (pmbox->out_ext_byte_len && pmbox->context2) {
7747 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7748 pmbox->context2,
7749 pmbox->out_ext_byte_len);
7750 }
dea3101e
JB
7751 } else {
7752 /* First copy command data */
bf07bdea 7753 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 7754 MAILBOX_CMD_SIZE);
7a470277
JS
7755 /* Copy the mailbox extension data */
7756 if (pmbox->out_ext_byte_len && pmbox->context2) {
7757 lpfc_memcpy_from_slim(pmbox->context2,
7758 phba->MBslimaddr +
7759 MAILBOX_HBA_EXT_OFFSET,
7760 pmbox->out_ext_byte_len);
dea3101e
JB
7761 }
7762 }
7763
7764 writel(HA_MBATT, phba->HAregaddr);
7765 readl(phba->HAregaddr); /* flush */
7766
7767 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7768 status = mbx->mbxStatus;
dea3101e
JB
7769 }
7770
2e0fef85
JS
7771 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7772 return status;
58da1ffb
JS
7773
7774out_not_finished:
7775 if (processing_queue) {
da0436e9 7776 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7777 lpfc_mbox_cmpl_put(phba, pmbox);
7778 }
7779 return MBX_NOT_FINISHED;
dea3101e
JB
7780}
7781
f1126688
JS
7782/**
7783 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7784 * @phba: Pointer to HBA context object.
7785 *
7786 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7787 * the driver internal pending mailbox queue. It will then try to wait out the
7788 * possible outstanding mailbox command before return.
7789 *
7790 * Returns:
7791 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7792 * the outstanding mailbox command timed out.
7793 **/
7794static int
7795lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7796{
7797 struct lpfc_sli *psli = &phba->sli;
f1126688 7798 int rc = 0;
a183a15f 7799 unsigned long timeout = 0;
f1126688
JS
7800
7801 /* Mark the asynchronous mailbox command posting as blocked */
7802 spin_lock_irq(&phba->hbalock);
7803 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7804 /* Determine how long we might wait for the active mailbox
7805 * command to be gracefully completed by firmware.
7806 */
a183a15f
JS
7807 if (phba->sli.mbox_active)
7808 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7809 phba->sli.mbox_active) *
7810 1000) + jiffies;
7811 spin_unlock_irq(&phba->hbalock);
7812
e8d3c3b1
JS
7813 /* Make sure the mailbox is really active */
7814 if (timeout)
7815 lpfc_sli4_process_missed_mbox_completions(phba);
7816
f1126688
JS
7817 /* Wait for the outstnading mailbox command to complete */
7818 while (phba->sli.mbox_active) {
7819 /* Check active mailbox complete status every 2ms */
7820 msleep(2);
7821 if (time_after(jiffies, timeout)) {
7822 /* Timeout, marked the outstanding cmd not complete */
7823 rc = 1;
7824 break;
7825 }
7826 }
7827
7828 /* Can not cleanly block async mailbox command, fails it */
7829 if (rc) {
7830 spin_lock_irq(&phba->hbalock);
7831 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7832 spin_unlock_irq(&phba->hbalock);
7833 }
7834 return rc;
7835}
7836
7837/**
7838 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7839 * @phba: Pointer to HBA context object.
7840 *
7841 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7842 * commands from the driver internal pending mailbox queue. It makes sure
7843 * that there is no outstanding mailbox command before resuming posting
7844 * asynchronous mailbox commands. If, for any reason, there is outstanding
7845 * mailbox command, it will try to wait it out before resuming asynchronous
7846 * mailbox command posting.
7847 **/
7848static void
7849lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7850{
7851 struct lpfc_sli *psli = &phba->sli;
7852
7853 spin_lock_irq(&phba->hbalock);
7854 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7855 /* Asynchronous mailbox posting is not blocked, do nothing */
7856 spin_unlock_irq(&phba->hbalock);
7857 return;
7858 }
7859
7860 /* Outstanding synchronous mailbox command is guaranteed to be done,
7861 * successful or timeout, after timing-out the outstanding mailbox
7862 * command shall always be removed, so just unblock posting async
7863 * mailbox command and resume
7864 */
7865 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7866 spin_unlock_irq(&phba->hbalock);
7867
7868 /* wake up worker thread to post asynchronlous mailbox command */
7869 lpfc_worker_wake_up(phba);
7870}
7871
2d843edc
JS
7872/**
7873 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7874 * @phba: Pointer to HBA context object.
7875 * @mboxq: Pointer to mailbox object.
7876 *
7877 * The function waits for the bootstrap mailbox register ready bit from
7878 * port for twice the regular mailbox command timeout value.
7879 *
7880 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7881 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7882 **/
7883static int
7884lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7885{
7886 uint32_t db_ready;
7887 unsigned long timeout;
7888 struct lpfc_register bmbx_reg;
7889
7890 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7891 * 1000) + jiffies;
7892
7893 do {
7894 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7895 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7896 if (!db_ready)
7897 msleep(2);
7898
7899 if (time_after(jiffies, timeout))
7900 return MBXERR_ERROR;
7901 } while (!db_ready);
7902
7903 return 0;
7904}
7905
da0436e9
JS
7906/**
7907 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7908 * @phba: Pointer to HBA context object.
7909 * @mboxq: Pointer to mailbox object.
7910 *
7911 * The function posts a mailbox to the port. The mailbox is expected
7912 * to be comletely filled in and ready for the port to operate on it.
7913 * This routine executes a synchronous completion operation on the
7914 * mailbox by polling for its completion.
7915 *
7916 * The caller must not be holding any locks when calling this routine.
7917 *
7918 * Returns:
7919 * MBX_SUCCESS - mailbox posted successfully
7920 * Any of the MBX error values.
7921 **/
7922static int
7923lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7924{
7925 int rc = MBX_SUCCESS;
7926 unsigned long iflag;
da0436e9
JS
7927 uint32_t mcqe_status;
7928 uint32_t mbx_cmnd;
da0436e9
JS
7929 struct lpfc_sli *psli = &phba->sli;
7930 struct lpfc_mqe *mb = &mboxq->u.mqe;
7931 struct lpfc_bmbx_create *mbox_rgn;
7932 struct dma_address *dma_address;
da0436e9
JS
7933
7934 /*
7935 * Only one mailbox can be active to the bootstrap mailbox region
7936 * at a time and there is no queueing provided.
7937 */
7938 spin_lock_irqsave(&phba->hbalock, iflag);
7939 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7940 spin_unlock_irqrestore(&phba->hbalock, iflag);
7941 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7942 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7943 "cannot issue Data: x%x x%x\n",
7944 mboxq->vport ? mboxq->vport->vpi : 0,
7945 mboxq->u.mb.mbxCommand,
a183a15f
JS
7946 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7947 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7948 psli->sli_flag, MBX_POLL);
7949 return MBXERR_ERROR;
7950 }
7951 /* The server grabs the token and owns it until release */
7952 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7953 phba->sli.mbox_active = mboxq;
7954 spin_unlock_irqrestore(&phba->hbalock, iflag);
7955
2d843edc
JS
7956 /* wait for bootstrap mbox register for readyness */
7957 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7958 if (rc)
7959 goto exit;
7960
da0436e9
JS
7961 /*
7962 * Initialize the bootstrap memory region to avoid stale data areas
7963 * in the mailbox post. Then copy the caller's mailbox contents to
7964 * the bmbx mailbox region.
7965 */
7966 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7967 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7968 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7969 sizeof(struct lpfc_mqe));
7970
7971 /* Post the high mailbox dma address to the port and wait for ready. */
7972 dma_address = &phba->sli4_hba.bmbx.dma_address;
7973 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7974
2d843edc
JS
7975 /* wait for bootstrap mbox register for hi-address write done */
7976 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7977 if (rc)
7978 goto exit;
da0436e9
JS
7979
7980 /* Post the low mailbox dma address to the port. */
7981 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7982
2d843edc
JS
7983 /* wait for bootstrap mbox register for low address write done */
7984 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7985 if (rc)
7986 goto exit;
da0436e9
JS
7987
7988 /*
7989 * Read the CQ to ensure the mailbox has completed.
7990 * If so, update the mailbox status so that the upper layers
7991 * can complete the request normally.
7992 */
7993 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7994 sizeof(struct lpfc_mqe));
7995 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7996 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7997 sizeof(struct lpfc_mcqe));
7998 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
7999 /*
8000 * When the CQE status indicates a failure and the mailbox status
8001 * indicates success then copy the CQE status into the mailbox status
8002 * (and prefix it with x4000).
8003 */
da0436e9 8004 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
8005 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8006 bf_set(lpfc_mqe_status, mb,
8007 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 8008 rc = MBXERR_ERROR;
d7c47992
JS
8009 } else
8010 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
8011
8012 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8013 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
8014 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8015 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
8016 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8017 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8018 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8019 bf_get(lpfc_mqe_status, mb),
8020 mb->un.mb_words[0], mb->un.mb_words[1],
8021 mb->un.mb_words[2], mb->un.mb_words[3],
8022 mb->un.mb_words[4], mb->un.mb_words[5],
8023 mb->un.mb_words[6], mb->un.mb_words[7],
8024 mb->un.mb_words[8], mb->un.mb_words[9],
8025 mb->un.mb_words[10], mb->un.mb_words[11],
8026 mb->un.mb_words[12], mboxq->mcqe.word0,
8027 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8028 mboxq->mcqe.trailer);
8029exit:
8030 /* We are holding the token, no needed for lock when release */
8031 spin_lock_irqsave(&phba->hbalock, iflag);
8032 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8033 phba->sli.mbox_active = NULL;
8034 spin_unlock_irqrestore(&phba->hbalock, iflag);
8035 return rc;
8036}
8037
8038/**
8039 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8040 * @phba: Pointer to HBA context object.
8041 * @pmbox: Pointer to mailbox object.
8042 * @flag: Flag indicating how the mailbox need to be processed.
8043 *
8044 * This function is called by discovery code and HBA management code to submit
8045 * a mailbox command to firmware with SLI-4 interface spec.
8046 *
8047 * Return codes the caller owns the mailbox command after the return of the
8048 * function.
8049 **/
8050static int
8051lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8052 uint32_t flag)
8053{
8054 struct lpfc_sli *psli = &phba->sli;
8055 unsigned long iflags;
8056 int rc;
8057
b76f2dc9
JS
8058 /* dump from issue mailbox command if setup */
8059 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8060
8fa38513
JS
8061 rc = lpfc_mbox_dev_check(phba);
8062 if (unlikely(rc)) {
8063 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8064 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
8065 "cannot issue Data: x%x x%x\n",
8066 mboxq->vport ? mboxq->vport->vpi : 0,
8067 mboxq->u.mb.mbxCommand,
a183a15f
JS
8068 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8069 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
8070 psli->sli_flag, flag);
8071 goto out_not_finished;
8072 }
8073
da0436e9
JS
8074 /* Detect polling mode and jump to a handler */
8075 if (!phba->sli4_hba.intr_enable) {
8076 if (flag == MBX_POLL)
8077 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8078 else
8079 rc = -EIO;
8080 if (rc != MBX_SUCCESS)
0558056c 8081 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8082 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8083 "(x%x/x%x) failure: "
8084 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8085 "Data: x%x x%x\n,",
da0436e9
JS
8086 mboxq->vport ? mboxq->vport->vpi : 0,
8087 mboxq->u.mb.mbxCommand,
a183a15f
JS
8088 lpfc_sli_config_mbox_subsys_get(phba,
8089 mboxq),
8090 lpfc_sli_config_mbox_opcode_get(phba,
8091 mboxq),
cc459f19
JS
8092 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8093 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8094 bf_get(lpfc_mcqe_ext_status,
8095 &mboxq->mcqe),
da0436e9
JS
8096 psli->sli_flag, flag);
8097 return rc;
8098 } else if (flag == MBX_POLL) {
f1126688
JS
8099 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8100 "(%d):2542 Try to issue mailbox command "
a183a15f 8101 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 8102 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8103 mboxq->vport ? mboxq->vport->vpi : 0,
8104 mboxq->u.mb.mbxCommand,
a183a15f
JS
8105 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8106 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8107 psli->sli_flag, flag);
f1126688
JS
8108 /* Try to block the asynchronous mailbox posting */
8109 rc = lpfc_sli4_async_mbox_block(phba);
8110 if (!rc) {
8111 /* Successfully blocked, now issue sync mbox cmd */
8112 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8113 if (rc != MBX_SUCCESS)
cc459f19 8114 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8115 LOG_MBOX | LOG_SLI,
cc459f19
JS
8116 "(%d):2597 Sync Mailbox command "
8117 "x%x (x%x/x%x) failure: "
8118 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8119 "Data: x%x x%x\n,",
8120 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8121 mboxq->u.mb.mbxCommand,
8122 lpfc_sli_config_mbox_subsys_get(phba,
8123 mboxq),
8124 lpfc_sli_config_mbox_opcode_get(phba,
8125 mboxq),
cc459f19
JS
8126 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8127 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8128 bf_get(lpfc_mcqe_ext_status,
8129 &mboxq->mcqe),
a183a15f 8130 psli->sli_flag, flag);
f1126688
JS
8131 /* Unblock the async mailbox posting afterward */
8132 lpfc_sli4_async_mbox_unblock(phba);
8133 }
8134 return rc;
da0436e9
JS
8135 }
8136
8137 /* Now, interrupt mode asynchrous mailbox command */
8138 rc = lpfc_mbox_cmd_check(phba, mboxq);
8139 if (rc) {
8140 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8141 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8142 "cannot issue Data: x%x x%x\n",
8143 mboxq->vport ? mboxq->vport->vpi : 0,
8144 mboxq->u.mb.mbxCommand,
a183a15f
JS
8145 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8146 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8147 psli->sli_flag, flag);
8148 goto out_not_finished;
8149 }
da0436e9
JS
8150
8151 /* Put the mailbox command to the driver internal FIFO */
8152 psli->slistat.mbox_busy++;
8153 spin_lock_irqsave(&phba->hbalock, iflags);
8154 lpfc_mbox_put(phba, mboxq);
8155 spin_unlock_irqrestore(&phba->hbalock, iflags);
8156 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8157 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8158 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8159 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8160 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8161 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8162 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8163 phba->pport->port_state,
8164 psli->sli_flag, MBX_NOWAIT);
8165 /* Wake up worker thread to transport mailbox command from head */
8166 lpfc_worker_wake_up(phba);
8167
8168 return MBX_BUSY;
8169
8170out_not_finished:
8171 return MBX_NOT_FINISHED;
8172}
8173
8174/**
8175 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8176 * @phba: Pointer to HBA context object.
8177 *
8178 * This function is called by worker thread to send a mailbox command to
8179 * SLI4 HBA firmware.
8180 *
8181 **/
8182int
8183lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8184{
8185 struct lpfc_sli *psli = &phba->sli;
8186 LPFC_MBOXQ_t *mboxq;
8187 int rc = MBX_SUCCESS;
8188 unsigned long iflags;
8189 struct lpfc_mqe *mqe;
8190 uint32_t mbx_cmnd;
8191
8192 /* Check interrupt mode before post async mailbox command */
8193 if (unlikely(!phba->sli4_hba.intr_enable))
8194 return MBX_NOT_FINISHED;
8195
8196 /* Check for mailbox command service token */
8197 spin_lock_irqsave(&phba->hbalock, iflags);
8198 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8199 spin_unlock_irqrestore(&phba->hbalock, iflags);
8200 return MBX_NOT_FINISHED;
8201 }
8202 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8203 spin_unlock_irqrestore(&phba->hbalock, iflags);
8204 return MBX_NOT_FINISHED;
8205 }
8206 if (unlikely(phba->sli.mbox_active)) {
8207 spin_unlock_irqrestore(&phba->hbalock, iflags);
8208 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8209 "0384 There is pending active mailbox cmd\n");
8210 return MBX_NOT_FINISHED;
8211 }
8212 /* Take the mailbox command service token */
8213 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8214
8215 /* Get the next mailbox command from head of queue */
8216 mboxq = lpfc_mbox_get(phba);
8217
8218 /* If no more mailbox command waiting for post, we're done */
8219 if (!mboxq) {
8220 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8221 spin_unlock_irqrestore(&phba->hbalock, iflags);
8222 return MBX_SUCCESS;
8223 }
8224 phba->sli.mbox_active = mboxq;
8225 spin_unlock_irqrestore(&phba->hbalock, iflags);
8226
8227 /* Check device readiness for posting mailbox command */
8228 rc = lpfc_mbox_dev_check(phba);
8229 if (unlikely(rc))
8230 /* Driver clean routine will clean up pending mailbox */
8231 goto out_not_finished;
8232
8233 /* Prepare the mbox command to be posted */
8234 mqe = &mboxq->u.mqe;
8235 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8236
8237 /* Start timer for the mbox_tmo and log some mailbox post messages */
8238 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8239 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8240
8241 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8242 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8243 "x%x x%x\n",
8244 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8245 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8246 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8247 phba->pport->port_state, psli->sli_flag);
8248
8249 if (mbx_cmnd != MBX_HEARTBEAT) {
8250 if (mboxq->vport) {
8251 lpfc_debugfs_disc_trc(mboxq->vport,
8252 LPFC_DISC_TRC_MBOX_VPORT,
8253 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8254 mbx_cmnd, mqe->un.mb_words[0],
8255 mqe->un.mb_words[1]);
8256 } else {
8257 lpfc_debugfs_disc_trc(phba->pport,
8258 LPFC_DISC_TRC_MBOX,
8259 "MBOX Send: cmd:x%x mb:x%x x%x",
8260 mbx_cmnd, mqe->un.mb_words[0],
8261 mqe->un.mb_words[1]);
8262 }
8263 }
8264 psli->slistat.mbox_cmd++;
8265
8266 /* Post the mailbox command to the port */
8267 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8268 if (rc != MBX_SUCCESS) {
8269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8270 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8271 "cannot issue Data: x%x x%x\n",
8272 mboxq->vport ? mboxq->vport->vpi : 0,
8273 mboxq->u.mb.mbxCommand,
a183a15f
JS
8274 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8275 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8276 psli->sli_flag, MBX_NOWAIT);
8277 goto out_not_finished;
8278 }
8279
8280 return rc;
8281
8282out_not_finished:
8283 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8284 if (phba->sli.mbox_active) {
8285 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8286 __lpfc_mbox_cmpl_put(phba, mboxq);
8287 /* Release the token */
8288 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8289 phba->sli.mbox_active = NULL;
8290 }
da0436e9
JS
8291 spin_unlock_irqrestore(&phba->hbalock, iflags);
8292
8293 return MBX_NOT_FINISHED;
8294}
8295
8296/**
8297 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8298 * @phba: Pointer to HBA context object.
8299 * @pmbox: Pointer to mailbox object.
8300 * @flag: Flag indicating how the mailbox need to be processed.
8301 *
8302 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8303 * the API jump table function pointer from the lpfc_hba struct.
8304 *
8305 * Return codes the caller owns the mailbox command after the return of the
8306 * function.
8307 **/
8308int
8309lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8310{
8311 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8312}
8313
8314/**
25985edc 8315 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8316 * @phba: The hba struct for which this call is being executed.
8317 * @dev_grp: The HBA PCI-Device group number.
8318 *
8319 * This routine sets up the mbox interface API function jump table in @phba
8320 * struct.
8321 * Returns: 0 - success, -ENODEV - failure.
8322 **/
8323int
8324lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8325{
8326
8327 switch (dev_grp) {
8328 case LPFC_PCI_DEV_LP:
8329 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8330 phba->lpfc_sli_handle_slow_ring_event =
8331 lpfc_sli_handle_slow_ring_event_s3;
8332 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8333 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8334 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8335 break;
8336 case LPFC_PCI_DEV_OC:
8337 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8338 phba->lpfc_sli_handle_slow_ring_event =
8339 lpfc_sli_handle_slow_ring_event_s4;
8340 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8341 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8342 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8343 break;
8344 default:
8345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8346 "1420 Invalid HBA PCI-device group: 0x%x\n",
8347 dev_grp);
8348 return -ENODEV;
8349 break;
8350 }
8351 return 0;
8352}
8353
e59058c4 8354/**
3621a710 8355 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8356 * @phba: Pointer to HBA context object.
8357 * @pring: Pointer to driver SLI ring object.
8358 * @piocb: Pointer to address of newly added command iocb.
8359 *
8360 * This function is called with hbalock held to add a command
8361 * iocb to the txq when SLI layer cannot submit the command iocb
8362 * to the ring.
8363 **/
2a9bf3d0 8364void
92d7f7b0 8365__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8366 struct lpfc_iocbq *piocb)
dea3101e 8367{
1c2ba475 8368 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8369 /* Insert the caller's iocb in the txq tail for later processing. */
8370 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8371}
8372
e59058c4 8373/**
3621a710 8374 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8375 * @phba: Pointer to HBA context object.
8376 * @pring: Pointer to driver SLI ring object.
8377 * @piocb: Pointer to address of newly added command iocb.
8378 *
8379 * This function is called with hbalock held before a new
8380 * iocb is submitted to the firmware. This function checks
8381 * txq to flush the iocbs in txq to Firmware before
8382 * submitting new iocbs to the Firmware.
8383 * If there are iocbs in the txq which need to be submitted
8384 * to firmware, lpfc_sli_next_iocb returns the first element
8385 * of the txq after dequeuing it from txq.
8386 * If there is no iocb in the txq then the function will return
8387 * *piocb and *piocb is set to NULL. Caller needs to check
8388 * *piocb to find if there are more commands in the txq.
8389 **/
dea3101e
JB
8390static struct lpfc_iocbq *
8391lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8392 struct lpfc_iocbq **piocb)
dea3101e
JB
8393{
8394 struct lpfc_iocbq * nextiocb;
8395
1c2ba475
JT
8396 lockdep_assert_held(&phba->hbalock);
8397
dea3101e
JB
8398 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8399 if (!nextiocb) {
8400 nextiocb = *piocb;
8401 *piocb = NULL;
8402 }
8403
8404 return nextiocb;
8405}
8406
e59058c4 8407/**
3772a991 8408 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8409 * @phba: Pointer to HBA context object.
3772a991 8410 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8411 * @piocb: Pointer to command iocb.
8412 * @flag: Flag indicating if this command can be put into txq.
8413 *
3772a991
JS
8414 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8415 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8416 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8417 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8418 * this function allows only iocbs for posting buffers. This function finds
8419 * next available slot in the command ring and posts the command to the
8420 * available slot and writes the port attention register to request HBA start
8421 * processing new iocb. If there is no slot available in the ring and
8422 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8423 * the function returns IOCB_BUSY.
e59058c4 8424 *
3772a991
JS
8425 * This function is called with hbalock held. The function will return success
8426 * after it successfully submit the iocb to firmware or after adding to the
8427 * txq.
e59058c4 8428 **/
98c9ea5c 8429static int
3772a991 8430__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8431 struct lpfc_iocbq *piocb, uint32_t flag)
8432{
8433 struct lpfc_iocbq *nextiocb;
8434 IOCB_t *iocb;
895427bd 8435 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8436
1c2ba475
JT
8437 lockdep_assert_held(&phba->hbalock);
8438
92d7f7b0
JS
8439 if (piocb->iocb_cmpl && (!piocb->vport) &&
8440 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8441 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8442 lpfc_printf_log(phba, KERN_ERR,
8443 LOG_SLI | LOG_VPORT,
e8b62011 8444 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8445 piocb->iocb.ulpCommand);
8446 dump_stack();
8447 return IOCB_ERROR;
8448 }
8449
8450
8d63f375
LV
8451 /* If the PCI channel is in offline state, do not post iocbs. */
8452 if (unlikely(pci_channel_offline(phba->pcidev)))
8453 return IOCB_ERROR;
8454
a257bf90
JS
8455 /* If HBA has a deferred error attention, fail the iocb. */
8456 if (unlikely(phba->hba_flag & DEFER_ERATT))
8457 return IOCB_ERROR;
8458
dea3101e
JB
8459 /*
8460 * We should never get an IOCB if we are in a < LINK_DOWN state
8461 */
2e0fef85 8462 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8463 return IOCB_ERROR;
8464
8465 /*
8466 * Check to see if we are blocking IOCB processing because of a
0b727fea 8467 * outstanding event.
dea3101e 8468 */
0b727fea 8469 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8470 goto iocb_busy;
8471
2e0fef85 8472 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8473 /*
2680eeaa 8474 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8475 * can be issued if the link is not up.
8476 */
8477 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8478 case CMD_GEN_REQUEST64_CR:
8479 case CMD_GEN_REQUEST64_CX:
8480 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8481 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8482 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8483 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8484 MENLO_TRANSPORT_TYPE))
8485
8486 goto iocb_busy;
8487 break;
dea3101e
JB
8488 case CMD_QUE_RING_BUF_CN:
8489 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8490 /*
8491 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8492 * completion, iocb_cmpl MUST be 0.
8493 */
8494 if (piocb->iocb_cmpl)
8495 piocb->iocb_cmpl = NULL;
8496 /*FALLTHROUGH*/
8497 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8498 case CMD_CLOSE_XRI_CN:
8499 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8500 break;
8501 default:
8502 goto iocb_busy;
8503 }
8504
8505 /*
8506 * For FCP commands, we must be in a state where we can process link
8507 * attention events.
8508 */
895427bd 8509 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8510 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8511 goto iocb_busy;
92d7f7b0 8512 }
dea3101e 8513
dea3101e
JB
8514 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8515 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8516 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8517
8518 if (iocb)
8519 lpfc_sli_update_ring(phba, pring);
8520 else
8521 lpfc_sli_update_full_ring(phba, pring);
8522
8523 if (!piocb)
8524 return IOCB_SUCCESS;
8525
8526 goto out_busy;
8527
8528 iocb_busy:
8529 pring->stats.iocb_cmd_delay++;
8530
8531 out_busy:
8532
8533 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8534 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8535 return IOCB_SUCCESS;
8536 }
8537
8538 return IOCB_BUSY;
8539}
8540
3772a991 8541/**
4f774513
JS
8542 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8543 * @phba: Pointer to HBA context object.
8544 * @piocb: Pointer to command iocb.
8545 * @sglq: Pointer to the scatter gather queue object.
8546 *
8547 * This routine converts the bpl or bde that is in the IOCB
8548 * to a sgl list for the sli4 hardware. The physical address
8549 * of the bpl/bde is converted back to a virtual address.
8550 * If the IOCB contains a BPL then the list of BDE's is
8551 * converted to sli4_sge's. If the IOCB contains a single
8552 * BDE then it is converted to a single sli_sge.
8553 * The IOCB is still in cpu endianess so the contents of
8554 * the bpl can be used without byte swapping.
8555 *
8556 * Returns valid XRI = Success, NO_XRI = Failure.
8557**/
8558static uint16_t
8559lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8560 struct lpfc_sglq *sglq)
3772a991 8561{
4f774513
JS
8562 uint16_t xritag = NO_XRI;
8563 struct ulp_bde64 *bpl = NULL;
8564 struct ulp_bde64 bde;
8565 struct sli4_sge *sgl = NULL;
1b51197d 8566 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8567 IOCB_t *icmd;
8568 int numBdes = 0;
8569 int i = 0;
63e801ce
JS
8570 uint32_t offset = 0; /* accumulated offset in the sg request list */
8571 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8572
4f774513
JS
8573 if (!piocbq || !sglq)
8574 return xritag;
8575
8576 sgl = (struct sli4_sge *)sglq->sgl;
8577 icmd = &piocbq->iocb;
6b5151fd
JS
8578 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8579 return sglq->sli4_xritag;
4f774513
JS
8580 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8581 numBdes = icmd->un.genreq64.bdl.bdeSize /
8582 sizeof(struct ulp_bde64);
8583 /* The addrHigh and addrLow fields within the IOCB
8584 * have not been byteswapped yet so there is no
8585 * need to swap them back.
8586 */
1b51197d
JS
8587 if (piocbq->context3)
8588 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8589 else
8590 return xritag;
4f774513 8591
1b51197d 8592 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8593 if (!bpl)
8594 return xritag;
8595
8596 for (i = 0; i < numBdes; i++) {
8597 /* Should already be byte swapped. */
28baac74
JS
8598 sgl->addr_hi = bpl->addrHigh;
8599 sgl->addr_lo = bpl->addrLow;
8600
0558056c 8601 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8602 if ((i+1) == numBdes)
8603 bf_set(lpfc_sli4_sge_last, sgl, 1);
8604 else
8605 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8606 /* swap the size field back to the cpu so we
8607 * can assign it to the sgl.
8608 */
8609 bde.tus.w = le32_to_cpu(bpl->tus.w);
8610 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8611 /* The offsets in the sgl need to be accumulated
8612 * separately for the request and reply lists.
8613 * The request is always first, the reply follows.
8614 */
8615 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8616 /* add up the reply sg entries */
8617 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8618 inbound++;
8619 /* first inbound? reset the offset */
8620 if (inbound == 1)
8621 offset = 0;
8622 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8623 bf_set(lpfc_sli4_sge_type, sgl,
8624 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8625 offset += bde.tus.f.bdeSize;
8626 }
546fc854 8627 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8628 bpl++;
8629 sgl++;
8630 }
8631 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8632 /* The addrHigh and addrLow fields of the BDE have not
8633 * been byteswapped yet so they need to be swapped
8634 * before putting them in the sgl.
8635 */
8636 sgl->addr_hi =
8637 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8638 sgl->addr_lo =
8639 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8640 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8641 bf_set(lpfc_sli4_sge_last, sgl, 1);
8642 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8643 sgl->sge_len =
8644 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8645 }
8646 return sglq->sli4_xritag;
3772a991 8647}
92d7f7b0 8648
e59058c4 8649/**
4f774513 8650 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8651 * @phba: Pointer to HBA context object.
4f774513
JS
8652 * @piocb: Pointer to command iocb.
8653 * @wqe: Pointer to the work queue entry.
e59058c4 8654 *
4f774513
JS
8655 * This routine converts the iocb command to its Work Queue Entry
8656 * equivalent. The wqe pointer should not have any fields set when
8657 * this routine is called because it will memcpy over them.
8658 * This routine does not set the CQ_ID or the WQEC bits in the
8659 * wqe.
e59058c4 8660 *
4f774513 8661 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8662 **/
cf5bf97e 8663static int
4f774513
JS
8664lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8665 union lpfc_wqe *wqe)
cf5bf97e 8666{
5ffc266e 8667 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8668 uint8_t ct = 0;
8669 uint32_t fip;
8670 uint32_t abort_tag;
8671 uint8_t command_type = ELS_COMMAND_NON_FIP;
8672 uint8_t cmnd;
8673 uint16_t xritag;
dcf2a4e0
JS
8674 uint16_t abrt_iotag;
8675 struct lpfc_iocbq *abrtiocbq;
4f774513 8676 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8677 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8678 int numBdes, i;
8679 struct ulp_bde64 bde;
c31098ce 8680 struct lpfc_nodelist *ndlp;
ff78d8f9 8681 uint32_t *pcmd;
1b51197d 8682 uint32_t if_type;
4f774513 8683
45ed1190 8684 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8685 /* The fcp commands will set command type */
0c287589 8686 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8687 command_type = FCP_COMMAND;
c868595d 8688 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8689 command_type = ELS_COMMAND_FIP;
8690 else
8691 command_type = ELS_COMMAND_NON_FIP;
8692
b5c53958
JS
8693 if (phba->fcp_embed_io)
8694 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8695 /* Some of the fields are in the right position already */
8696 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
ae9e28f3
JS
8697 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8698 /* The ct field has moved so reset */
8699 wqe->generic.wqe_com.word7 = 0;
8700 wqe->generic.wqe_com.word10 = 0;
8701 }
b5c53958
JS
8702
8703 abort_tag = (uint32_t) iocbq->iotag;
8704 xritag = iocbq->sli4_xritag;
4f774513
JS
8705 /* words0-2 bpl convert bde */
8706 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8707 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8708 sizeof(struct ulp_bde64);
4f774513
JS
8709 bpl = (struct ulp_bde64 *)
8710 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8711 if (!bpl)
8712 return IOCB_ERROR;
cf5bf97e 8713
4f774513
JS
8714 /* Should already be byte swapped. */
8715 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8716 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8717 /* swap the size field back to the cpu so we
8718 * can assign it to the sgl.
8719 */
8720 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8721 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8722 total_len = 0;
8723 for (i = 0; i < numBdes; i++) {
8724 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8725 total_len += bde.tus.f.bdeSize;
8726 }
4f774513 8727 } else
5ffc266e 8728 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8729
4f774513
JS
8730 iocbq->iocb.ulpIoTag = iocbq->iotag;
8731 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8732
4f774513
JS
8733 switch (iocbq->iocb.ulpCommand) {
8734 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8735 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8736 ndlp = iocbq->context_un.ndlp;
8737 else
8738 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8739 if (!iocbq->iocb.ulpLe) {
8740 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8741 "2007 Only Limited Edition cmd Format"
8742 " supported 0x%x\n",
8743 iocbq->iocb.ulpCommand);
8744 return IOCB_ERROR;
8745 }
ff78d8f9 8746
5ffc266e 8747 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8748 /* Els_reguest64 has a TMO */
8749 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8750 iocbq->iocb.ulpTimeout);
8751 /* Need a VF for word 4 set the vf bit*/
8752 bf_set(els_req64_vf, &wqe->els_req, 0);
8753 /* And a VFID for word 12 */
8754 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8755 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8756 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8757 iocbq->iocb.ulpContext);
8758 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8759 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8760 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8761 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8762 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8763 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8764 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8765 iocbq->context2)->virt);
1b51197d
JS
8766 if_type = bf_get(lpfc_sli_intf_if_type,
8767 &phba->sli4_hba.sli_intf);
8768 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8769 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8770 *pcmd == ELS_CMD_SCR ||
6b5151fd 8771 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8772 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8773 *pcmd == ELS_CMD_PLOGI)) {
8774 bf_set(els_req64_sp, &wqe->els_req, 1);
8775 bf_set(els_req64_sid, &wqe->els_req,
8776 iocbq->vport->fc_myDID);
939723a4
JS
8777 if ((*pcmd == ELS_CMD_FLOGI) &&
8778 !(phba->fc_topology ==
8779 LPFC_TOPOLOGY_LOOP))
8780 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8781 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8782 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8783 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8784 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8785 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8786 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8787 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8788 }
c868595d 8789 }
6d368e53
JS
8790 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8791 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8792 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8793 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8794 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8795 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8796 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8797 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8798 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8799 break;
5ffc266e 8800 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8801 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8802 iocbq->iocb.un.ulpWord[3]);
8803 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8804 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8805 /* The entire sequence is transmitted for this IOCB */
8806 xmit_len = total_len;
8807 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8808 if (phba->link_flag & LS_LOOPBACK_MODE)
8809 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8810 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8811 /* word3 iocb=io_tag32 wqe=reserved */
8812 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8813 /* word4 relative_offset memcpy */
8814 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8815 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8816 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8817 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8818 LPFC_WQE_IOD_WRITE);
8819 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8820 LPFC_WQE_LENLOC_WORD12);
8821 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8822 wqe->xmit_sequence.xmit_len = xmit_len;
8823 command_type = OTHER_COMMAND;
7851fe2c 8824 break;
4f774513 8825 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8826 /* word3 iocb=iotag32 wqe=seq_payload_len */
8827 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8828 /* word4 iocb=rsvd wqe=rsvd */
8829 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8830 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8831 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8832 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8833 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8834 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8835 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8836 LPFC_WQE_LENLOC_WORD3);
8837 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8838 break;
4f774513
JS
8839 case CMD_FCP_IWRITE64_CR:
8840 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8841 /* word3 iocb=iotag wqe=payload_offset_len */
8842 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8843 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8844 xmit_len + sizeof(struct fcp_rsp));
8845 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8846 0);
f0d9bccc
JS
8847 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8848 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8849 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8850 iocbq->iocb.ulpFCP2Rcvy);
8851 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8852 /* Always open the exchange */
f0d9bccc
JS
8853 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8854 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8855 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8856 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8857 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
8858 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8859 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
8860 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8861 if (iocbq->priority) {
8862 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8863 (iocbq->priority << 1));
8864 } else {
1ba981fd
JS
8865 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8866 (phba->cfg_XLanePriority << 1));
8867 }
8868 }
b5c53958
JS
8869 /* Note, word 10 is already initialized to 0 */
8870
8871 if (phba->fcp_embed_io) {
8872 struct lpfc_scsi_buf *lpfc_cmd;
8873 struct sli4_sge *sgl;
8874 union lpfc_wqe128 *wqe128;
8875 struct fcp_cmnd *fcp_cmnd;
8876 uint32_t *ptr;
8877
8878 /* 128 byte wqe support here */
8879 wqe128 = (union lpfc_wqe128 *)wqe;
8880
8881 lpfc_cmd = iocbq->context1;
8882 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8883 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8884
8885 /* Word 0-2 - FCP_CMND */
8886 wqe128->generic.bde.tus.f.bdeFlags =
8887 BUFF_TYPE_BDE_IMMED;
8888 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8889 wqe128->generic.bde.addrHigh = 0;
8890 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8891
8892 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8893
8894 /* Word 22-29 FCP CMND Payload */
8895 ptr = &wqe128->words[22];
8896 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8897 }
7851fe2c 8898 break;
4f774513 8899 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8900 /* word3 iocb=iotag wqe=payload_offset_len */
8901 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8902 bf_set(payload_offset_len, &wqe->fcp_iread,
8903 xmit_len + sizeof(struct fcp_rsp));
8904 bf_set(cmd_buff_len, &wqe->fcp_iread,
8905 0);
f0d9bccc
JS
8906 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8907 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8908 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8909 iocbq->iocb.ulpFCP2Rcvy);
8910 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 8911 /* Always open the exchange */
f0d9bccc
JS
8912 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8913 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8914 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8915 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8916 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
8917 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8918 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
8919 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8920 if (iocbq->priority) {
8921 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8922 (iocbq->priority << 1));
8923 } else {
1ba981fd
JS
8924 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8925 (phba->cfg_XLanePriority << 1));
8926 }
8927 }
b5c53958
JS
8928 /* Note, word 10 is already initialized to 0 */
8929
8930 if (phba->fcp_embed_io) {
8931 struct lpfc_scsi_buf *lpfc_cmd;
8932 struct sli4_sge *sgl;
8933 union lpfc_wqe128 *wqe128;
8934 struct fcp_cmnd *fcp_cmnd;
8935 uint32_t *ptr;
8936
8937 /* 128 byte wqe support here */
8938 wqe128 = (union lpfc_wqe128 *)wqe;
8939
8940 lpfc_cmd = iocbq->context1;
8941 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8942 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8943
8944 /* Word 0-2 - FCP_CMND */
8945 wqe128->generic.bde.tus.f.bdeFlags =
8946 BUFF_TYPE_BDE_IMMED;
8947 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8948 wqe128->generic.bde.addrHigh = 0;
8949 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8950
8951 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8952
8953 /* Word 22-29 FCP CMND Payload */
8954 ptr = &wqe128->words[22];
8955 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8956 }
7851fe2c 8957 break;
4f774513 8958 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
8959 /* word3 iocb=iotag wqe=payload_offset_len */
8960 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8961 bf_set(payload_offset_len, &wqe->fcp_icmd,
8962 xmit_len + sizeof(struct fcp_rsp));
8963 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8964 0);
f0d9bccc 8965 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 8966 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8967 /* Always open the exchange */
f0d9bccc
JS
8968 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8969 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8970 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8971 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8972 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
8973 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8974 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
8975 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8976 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
8977 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8978 if (iocbq->priority) {
8979 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8980 (iocbq->priority << 1));
8981 } else {
1ba981fd
JS
8982 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8983 (phba->cfg_XLanePriority << 1));
8984 }
8985 }
b5c53958
JS
8986 /* Note, word 10 is already initialized to 0 */
8987
8988 if (phba->fcp_embed_io) {
8989 struct lpfc_scsi_buf *lpfc_cmd;
8990 struct sli4_sge *sgl;
8991 union lpfc_wqe128 *wqe128;
8992 struct fcp_cmnd *fcp_cmnd;
8993 uint32_t *ptr;
8994
8995 /* 128 byte wqe support here */
8996 wqe128 = (union lpfc_wqe128 *)wqe;
8997
8998 lpfc_cmd = iocbq->context1;
8999 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9000 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9001
9002 /* Word 0-2 - FCP_CMND */
9003 wqe128->generic.bde.tus.f.bdeFlags =
9004 BUFF_TYPE_BDE_IMMED;
9005 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9006 wqe128->generic.bde.addrHigh = 0;
9007 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9008
9009 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9010
9011 /* Word 22-29 FCP CMND Payload */
9012 ptr = &wqe128->words[22];
9013 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9014 }
7851fe2c 9015 break;
4f774513 9016 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
9017 /* For this command calculate the xmit length of the
9018 * request bde.
9019 */
9020 xmit_len = 0;
9021 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9022 sizeof(struct ulp_bde64);
9023 for (i = 0; i < numBdes; i++) {
63e801ce 9024 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
9025 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9026 break;
63e801ce
JS
9027 xmit_len += bde.tus.f.bdeSize;
9028 }
f0d9bccc
JS
9029 /* word3 iocb=IO_TAG wqe=request_payload_len */
9030 wqe->gen_req.request_payload_len = xmit_len;
9031 /* word4 iocb=parameter wqe=relative_offset memcpy */
9032 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
9033 /* word6 context tag copied in memcpy */
9034 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9035 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9037 "2015 Invalid CT %x command 0x%x\n",
9038 ct, iocbq->iocb.ulpCommand);
9039 return IOCB_ERROR;
9040 }
f0d9bccc
JS
9041 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9042 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9043 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9044 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9045 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9046 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9047 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9048 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 9049 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 9050 command_type = OTHER_COMMAND;
7851fe2c 9051 break;
4f774513 9052 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 9053 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 9054 /* words0-2 BDE memcpy */
f0d9bccc
JS
9055 /* word3 iocb=iotag32 wqe=response_payload_len */
9056 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
9057 /* word4 */
9058 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
9059 /* word5 iocb=rsvd wge=did */
9060 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
9061 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9062
9063 if_type = bf_get(lpfc_sli_intf_if_type,
9064 &phba->sli4_hba.sli_intf);
9065 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9066 if (iocbq->vport->fc_flag & FC_PT2PT) {
9067 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9068 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9069 iocbq->vport->fc_myDID);
9070 if (iocbq->vport->fc_myDID == Fabric_DID) {
9071 bf_set(wqe_els_did,
9072 &wqe->xmit_els_rsp.wqe_dest, 0);
9073 }
9074 }
9075 }
f0d9bccc
JS
9076 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9077 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9078 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9079 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 9080 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 9081 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 9082 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 9083 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9084 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9085 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9086 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9087 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9088 LPFC_WQE_LENLOC_WORD3);
9089 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9090 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9091 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9092 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9093 iocbq->context2)->virt);
9094 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9095 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9096 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9097 iocbq->vport->fc_myDID);
939723a4
JS
9098 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9099 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9100 phba->vpi_ids[phba->pport->vpi]);
9101 }
4f774513 9102 command_type = OTHER_COMMAND;
7851fe2c 9103 break;
4f774513
JS
9104 case CMD_CLOSE_XRI_CN:
9105 case CMD_ABORT_XRI_CN:
9106 case CMD_ABORT_XRI_CX:
9107 /* words 0-2 memcpy should be 0 rserved */
9108 /* port will send abts */
dcf2a4e0
JS
9109 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9110 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9111 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9112 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9113 } else
9114 fip = 0;
9115
9116 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9117 /*
dcf2a4e0
JS
9118 * The link is down, or the command was ELS_FIP
9119 * so the fw does not need to send abts
4f774513
JS
9120 * on the wire.
9121 */
9122 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9123 else
9124 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9125 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9126 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9127 wqe->abort_cmd.rsrvd5 = 0;
9128 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9129 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9130 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9131 /*
9132 * The abort handler will send us CMD_ABORT_XRI_CN or
9133 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9134 */
f0d9bccc
JS
9135 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9136 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9137 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9138 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9139 cmnd = CMD_ABORT_XRI_CX;
9140 command_type = OTHER_COMMAND;
9141 xritag = 0;
7851fe2c 9142 break;
6669f9bb 9143 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9144 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9145 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9146 * we re-construct this WQE here based on information in
9147 * iocbq from scratch.
9148 */
9149 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9150 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9151 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9152 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9153 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9154 LPFC_ABTS_UNSOL_INT) {
9155 /* ABTS sent by initiator to CT exchange, the
9156 * RX_ID field will be filled with the newly
9157 * allocated responder XRI.
9158 */
9159 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9160 iocbq->sli4_xritag);
9161 } else {
9162 /* ABTS sent by responder to CT exchange, the
9163 * RX_ID field will be filled with the responder
9164 * RX_ID from ABTS.
9165 */
9166 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9167 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9168 }
6669f9bb
JS
9169 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9170 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9171
9172 /* Use CT=VPI */
9173 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9174 ndlp->nlp_DID);
9175 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9176 iocbq->iocb.ulpContext);
9177 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9178 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9179 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9180 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9181 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9182 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9183 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9184 command_type = OTHER_COMMAND;
546fc854
JS
9185 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9186 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9187 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9188 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9189 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9190 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9191 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9192 }
9193
7851fe2c 9194 break;
ae9e28f3
JS
9195 case CMD_SEND_FRAME:
9196 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9197 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9198 return 0;
4f774513
JS
9199 case CMD_XRI_ABORTED_CX:
9200 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9201 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9202 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9203 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9204 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9205 default:
9206 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9207 "2014 Invalid command 0x%x\n",
9208 iocbq->iocb.ulpCommand);
9209 return IOCB_ERROR;
7851fe2c 9210 break;
4f774513 9211 }
6d368e53 9212
8012cc38
JS
9213 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9214 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9215 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9216 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9217 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9218 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9219 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9220 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9221 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9222 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9223 wqe->generic.wqe_com.abort_tag = abort_tag;
9224 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9225 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9226 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9227 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9228 return 0;
9229}
9230
9231/**
9232 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9233 * @phba: Pointer to HBA context object.
9234 * @ring_number: SLI ring number to issue iocb on.
9235 * @piocb: Pointer to command iocb.
9236 * @flag: Flag indicating if this command can be put into txq.
9237 *
9238 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9239 * an iocb command to an HBA with SLI-4 interface spec.
9240 *
9241 * This function is called with hbalock held. The function will return success
9242 * after it successfully submit the iocb to firmware or after adding to the
9243 * txq.
9244 **/
9245static int
9246__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9247 struct lpfc_iocbq *piocb, uint32_t flag)
9248{
9249 struct lpfc_sglq *sglq;
b5c53958
JS
9250 union lpfc_wqe *wqe;
9251 union lpfc_wqe128 wqe128;
1ba981fd 9252 struct lpfc_queue *wq;
895427bd 9253 struct lpfc_sli_ring *pring;
4f774513 9254
895427bd
JS
9255 /* Get the WQ */
9256 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9257 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9258 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9259 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9260 else
9261 wq = phba->sli4_hba.oas_wq;
9262 } else {
9263 wq = phba->sli4_hba.els_wq;
9264 }
9265
9266 /* Get corresponding ring */
9267 pring = wq->pring;
1c2ba475 9268
b5c53958
JS
9269 /*
9270 * The WQE can be either 64 or 128 bytes,
9271 * so allocate space on the stack assuming the largest.
9272 */
9273 wqe = (union lpfc_wqe *)&wqe128;
9274
895427bd
JS
9275 lockdep_assert_held(&phba->hbalock);
9276
4f774513
JS
9277 if (piocb->sli4_xritag == NO_XRI) {
9278 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9279 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9280 sglq = NULL;
9281 else {
0e9bb8d7 9282 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9283 if (!(flag & SLI_IOCB_RET_IOCB)) {
9284 __lpfc_sli_ringtx_put(phba,
9285 pring, piocb);
9286 return IOCB_SUCCESS;
9287 } else {
9288 return IOCB_BUSY;
9289 }
9290 } else {
895427bd 9291 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9292 if (!sglq) {
9293 if (!(flag & SLI_IOCB_RET_IOCB)) {
9294 __lpfc_sli_ringtx_put(phba,
9295 pring,
9296 piocb);
9297 return IOCB_SUCCESS;
9298 } else
9299 return IOCB_BUSY;
9300 }
9301 }
4f774513 9302 }
2ea259ee 9303 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9304 /* These IO's already have an XRI and a mapped sgl. */
9305 sglq = NULL;
2ea259ee 9306 else {
6d368e53
JS
9307 /*
9308 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9309 * sglq is on the active list
9310 */
edccdc17 9311 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9312 if (!sglq)
9313 return IOCB_ERROR;
9314 }
9315
9316 if (sglq) {
6d368e53 9317 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9318 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9319 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9320 return IOCB_ERROR;
9321 }
9322
b5c53958 9323 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
4f774513
JS
9324 return IOCB_ERROR;
9325
895427bd
JS
9326 if (lpfc_sli4_wq_put(wq, wqe))
9327 return IOCB_ERROR;
4f774513
JS
9328 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9329
9330 return 0;
9331}
9332
9333/**
9334 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9335 *
9336 * This routine wraps the actual lockless version for issusing IOCB function
9337 * pointer from the lpfc_hba struct.
9338 *
9339 * Return codes:
b5c53958
JS
9340 * IOCB_ERROR - Error
9341 * IOCB_SUCCESS - Success
9342 * IOCB_BUSY - Busy
4f774513 9343 **/
2a9bf3d0 9344int
4f774513
JS
9345__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9346 struct lpfc_iocbq *piocb, uint32_t flag)
9347{
9348 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9349}
9350
9351/**
25985edc 9352 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9353 * @phba: The hba struct for which this call is being executed.
9354 * @dev_grp: The HBA PCI-Device group number.
9355 *
9356 * This routine sets up the SLI interface API function jump table in @phba
9357 * struct.
9358 * Returns: 0 - success, -ENODEV - failure.
9359 **/
9360int
9361lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9362{
9363
9364 switch (dev_grp) {
9365 case LPFC_PCI_DEV_LP:
9366 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9367 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9368 break;
9369 case LPFC_PCI_DEV_OC:
9370 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9371 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9372 break;
9373 default:
9374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9375 "1419 Invalid HBA PCI-device group: 0x%x\n",
9376 dev_grp);
9377 return -ENODEV;
9378 break;
9379 }
9380 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9381 return 0;
9382}
9383
a1efe163 9384/**
895427bd 9385 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9386 * @phba: Pointer to HBA context object.
a1efe163
JS
9387 * @piocb: Pointer to command iocb.
9388 *
895427bd
JS
9389 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9390 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9391 * Since ABORTS must go on the same WQ of the command they are
895427bd 9392 * aborting, we use command's hba_wqidx.
a1efe163 9393 */
895427bd
JS
9394struct lpfc_sli_ring *
9395lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9396{
895427bd 9397 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9398 if (!(phba->cfg_fof) ||
895427bd 9399 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9400 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9401 return NULL;
8b0dff14 9402 /*
895427bd 9403 * for abort iocb hba_wqidx should already
8b0dff14
JS
9404 * be setup based on what work queue we used.
9405 */
8e036a94 9406 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
895427bd 9407 piocb->hba_wqidx =
8b0dff14
JS
9408 lpfc_sli4_scmd_to_wqidx_distr(phba,
9409 piocb->context1);
8e036a94
DK
9410 piocb->hba_wqidx = piocb->hba_wqidx %
9411 phba->cfg_fcp_io_channel;
9412 }
895427bd 9413 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9414 } else {
9415 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9416 return NULL;
9417 piocb->hba_wqidx = 0;
9418 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9419 }
895427bd
JS
9420 } else {
9421 if (unlikely(!phba->sli4_hba.els_wq))
9422 return NULL;
9423 piocb->hba_wqidx = 0;
9424 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9425 }
9bd2bff5
JS
9426}
9427
4f774513
JS
9428/**
9429 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9430 * @phba: Pointer to HBA context object.
9431 * @pring: Pointer to driver SLI ring object.
9432 * @piocb: Pointer to command iocb.
9433 * @flag: Flag indicating if this command can be put into txq.
9434 *
9435 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9436 * function. This function gets the hbalock and calls
9437 * __lpfc_sli_issue_iocb function and will return the error returned
9438 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9439 * functions which do not hold hbalock.
9440 **/
9441int
9442lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9443 struct lpfc_iocbq *piocb, uint32_t flag)
9444{
895427bd 9445 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9446 struct lpfc_sli_ring *pring;
ba20c853
JS
9447 struct lpfc_queue *fpeq;
9448 struct lpfc_eqe *eqe;
4f774513 9449 unsigned long iflags;
2a76a283 9450 int rc, idx;
4f774513 9451
7e56aa25 9452 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9453 pring = lpfc_sli4_calc_ring(phba, piocb);
9454 if (unlikely(pring == NULL))
9bd2bff5 9455 return IOCB_ERROR;
ba20c853 9456
9bd2bff5
JS
9457 spin_lock_irqsave(&pring->ring_lock, iflags);
9458 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9459 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9460
9bd2bff5 9461 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9462 idx = piocb->hba_wqidx;
9463 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9464
895427bd 9465 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9466
9bd2bff5
JS
9467 /* Get associated EQ with this index */
9468 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9469
9bd2bff5
JS
9470 /* Turn off interrupts from this EQ */
9471 lpfc_sli4_eq_clr_intr(fpeq);
ba20c853 9472
9bd2bff5
JS
9473 /*
9474 * Process all the events on FCP EQ
9475 */
9476 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9477 lpfc_sli4_hba_handle_eqe(phba,
9478 eqe, idx);
9479 fpeq->EQ_processed++;
ba20c853 9480 }
ba20c853 9481
9bd2bff5
JS
9482 /* Always clear and re-arm the EQ */
9483 lpfc_sli4_eq_release(fpeq,
9484 LPFC_QUEUE_REARM);
9485 }
895427bd 9486 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9487 }
7e56aa25
JS
9488 } else {
9489 /* For now, SLI2/3 will still use hbalock */
9490 spin_lock_irqsave(&phba->hbalock, iflags);
9491 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9492 spin_unlock_irqrestore(&phba->hbalock, iflags);
9493 }
4f774513
JS
9494 return rc;
9495}
9496
9497/**
9498 * lpfc_extra_ring_setup - Extra ring setup function
9499 * @phba: Pointer to HBA context object.
9500 *
9501 * This function is called while driver attaches with the
9502 * HBA to setup the extra ring. The extra ring is used
9503 * only when driver needs to support target mode functionality
9504 * or IP over FC functionalities.
9505 *
895427bd 9506 * This function is called with no lock held. SLI3 only.
4f774513
JS
9507 **/
9508static int
9509lpfc_extra_ring_setup( struct lpfc_hba *phba)
9510{
9511 struct lpfc_sli *psli;
9512 struct lpfc_sli_ring *pring;
9513
9514 psli = &phba->sli;
9515
9516 /* Adjust cmd/rsp ring iocb entries more evenly */
9517
9518 /* Take some away from the FCP ring */
895427bd 9519 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9520 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9521 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9522 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9523 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9524
a4bc3379 9525 /* and give them to the extra ring */
895427bd 9526 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9527
7e56aa25
JS
9528 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9529 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9530 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9531 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9532
9533 /* Setup default profile for this ring */
9534 pring->iotag_max = 4096;
9535 pring->num_mask = 1;
9536 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9537 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9538 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9539 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9540 return 0;
9541}
9542
cb69f7de
JS
9543/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9544 * @phba: Pointer to HBA context object.
9545 * @iocbq: Pointer to iocb object.
9546 *
9547 * The async_event handler calls this routine when it receives
9548 * an ASYNC_STATUS_CN event from the port. The port generates
9549 * this event when an Abort Sequence request to an rport fails
9550 * twice in succession. The abort could be originated by the
9551 * driver or by the port. The ABTS could have been for an ELS
9552 * or FCP IO. The port only generates this event when an ABTS
9553 * fails to complete after one retry.
9554 */
9555static void
9556lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9557 struct lpfc_iocbq *iocbq)
9558{
9559 struct lpfc_nodelist *ndlp = NULL;
9560 uint16_t rpi = 0, vpi = 0;
9561 struct lpfc_vport *vport = NULL;
9562
9563 /* The rpi in the ulpContext is vport-sensitive. */
9564 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9565 rpi = iocbq->iocb.ulpContext;
9566
9567 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9568 "3092 Port generated ABTS async event "
9569 "on vpi %d rpi %d status 0x%x\n",
9570 vpi, rpi, iocbq->iocb.ulpStatus);
9571
9572 vport = lpfc_find_vport_by_vpid(phba, vpi);
9573 if (!vport)
9574 goto err_exit;
9575 ndlp = lpfc_findnode_rpi(vport, rpi);
9576 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9577 goto err_exit;
9578
9579 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9580 lpfc_sli_abts_recover_port(vport, ndlp);
9581 return;
9582
9583 err_exit:
9584 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9585 "3095 Event Context not found, no "
9586 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9587 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9588 vpi, rpi);
9589}
9590
9591/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9592 * @phba: pointer to HBA context object.
9593 * @ndlp: nodelist pointer for the impacted rport.
9594 * @axri: pointer to the wcqe containing the failed exchange.
9595 *
9596 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9597 * port. The port generates this event when an abort exchange request to an
9598 * rport fails twice in succession with no reply. The abort could be originated
9599 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9600 */
9601void
9602lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9603 struct lpfc_nodelist *ndlp,
9604 struct sli4_wcqe_xri_aborted *axri)
9605{
9606 struct lpfc_vport *vport;
5c1db2ac 9607 uint32_t ext_status = 0;
cb69f7de 9608
6b5151fd 9609 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9611 "3115 Node Context not found, driver "
9612 "ignoring abts err event\n");
6b5151fd
JS
9613 return;
9614 }
9615
cb69f7de
JS
9616 vport = ndlp->vport;
9617 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9618 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9619 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9620 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9621 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9622 bf_get(lpfc_wcqe_xa_status, axri),
9623 axri->parameter);
cb69f7de 9624
5c1db2ac
JS
9625 /*
9626 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9627 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9628 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9629 */
e3d2b802 9630 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9631 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9632 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9633 lpfc_sli_abts_recover_port(vport, ndlp);
9634}
9635
e59058c4 9636/**
3621a710 9637 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9638 * @phba: Pointer to HBA context object.
9639 * @pring: Pointer to driver SLI ring object.
9640 * @iocbq: Pointer to iocb object.
9641 *
9642 * This function is called by the slow ring event handler
9643 * function when there is an ASYNC event iocb in the ring.
9644 * This function is called with no lock held.
9645 * Currently this function handles only temperature related
9646 * ASYNC events. The function decodes the temperature sensor
9647 * event message and posts events for the management applications.
9648 **/
98c9ea5c 9649static void
57127f15
JS
9650lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9651 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9652{
9653 IOCB_t *icmd;
9654 uint16_t evt_code;
57127f15
JS
9655 struct temp_event temp_event_data;
9656 struct Scsi_Host *shost;
a257bf90 9657 uint32_t *iocb_w;
57127f15
JS
9658
9659 icmd = &iocbq->iocb;
9660 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9661
cb69f7de
JS
9662 switch (evt_code) {
9663 case ASYNC_TEMP_WARN:
9664 case ASYNC_TEMP_SAFE:
9665 temp_event_data.data = (uint32_t) icmd->ulpContext;
9666 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9667 if (evt_code == ASYNC_TEMP_WARN) {
9668 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9669 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9670 "0347 Adapter is very hot, please take "
9671 "corrective action. temperature : %d Celsius\n",
9672 (uint32_t) icmd->ulpContext);
9673 } else {
9674 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9675 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9676 "0340 Adapter temperature is OK now. "
9677 "temperature : %d Celsius\n",
9678 (uint32_t) icmd->ulpContext);
9679 }
9680
9681 /* Send temperature change event to applications */
9682 shost = lpfc_shost_from_vport(phba->pport);
9683 fc_host_post_vendor_event(shost, fc_get_event_number(),
9684 sizeof(temp_event_data), (char *) &temp_event_data,
9685 LPFC_NL_VENDOR_ID);
9686 break;
9687 case ASYNC_STATUS_CN:
9688 lpfc_sli_abts_err_handler(phba, iocbq);
9689 break;
9690 default:
a257bf90 9691 iocb_w = (uint32_t *) icmd;
cb69f7de 9692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9693 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9694 " evt_code 0x%x\n"
a257bf90
JS
9695 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9696 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9697 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9698 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9699 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9700 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9701 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9702 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9703 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9704
cb69f7de 9705 break;
57127f15 9706 }
57127f15
JS
9707}
9708
9709
e59058c4 9710/**
895427bd 9711 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9712 * @phba: Pointer to HBA context object.
9713 *
9714 * lpfc_sli_setup sets up rings of the SLI interface with
9715 * number of iocbs per ring and iotags. This function is
9716 * called while driver attach to the HBA and before the
9717 * interrupts are enabled. So there is no need for locking.
9718 *
9719 * This function always returns 0.
9720 **/
dea3101e 9721int
895427bd
JS
9722lpfc_sli4_setup(struct lpfc_hba *phba)
9723{
9724 struct lpfc_sli_ring *pring;
9725
9726 pring = phba->sli4_hba.els_wq->pring;
9727 pring->num_mask = LPFC_MAX_RING_MASK;
9728 pring->prt[0].profile = 0; /* Mask 0 */
9729 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9730 pring->prt[0].type = FC_TYPE_ELS;
9731 pring->prt[0].lpfc_sli_rcv_unsol_event =
9732 lpfc_els_unsol_event;
9733 pring->prt[1].profile = 0; /* Mask 1 */
9734 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9735 pring->prt[1].type = FC_TYPE_ELS;
9736 pring->prt[1].lpfc_sli_rcv_unsol_event =
9737 lpfc_els_unsol_event;
9738 pring->prt[2].profile = 0; /* Mask 2 */
9739 /* NameServer Inquiry */
9740 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9741 /* NameServer */
9742 pring->prt[2].type = FC_TYPE_CT;
9743 pring->prt[2].lpfc_sli_rcv_unsol_event =
9744 lpfc_ct_unsol_event;
9745 pring->prt[3].profile = 0; /* Mask 3 */
9746 /* NameServer response */
9747 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9748 /* NameServer */
9749 pring->prt[3].type = FC_TYPE_CT;
9750 pring->prt[3].lpfc_sli_rcv_unsol_event =
9751 lpfc_ct_unsol_event;
9752 return 0;
9753}
9754
9755/**
9756 * lpfc_sli_setup - SLI ring setup function
9757 * @phba: Pointer to HBA context object.
9758 *
9759 * lpfc_sli_setup sets up rings of the SLI interface with
9760 * number of iocbs per ring and iotags. This function is
9761 * called while driver attach to the HBA and before the
9762 * interrupts are enabled. So there is no need for locking.
9763 *
9764 * This function always returns 0. SLI3 only.
9765 **/
9766int
dea3101e
JB
9767lpfc_sli_setup(struct lpfc_hba *phba)
9768{
ed957684 9769 int i, totiocbsize = 0;
dea3101e
JB
9770 struct lpfc_sli *psli = &phba->sli;
9771 struct lpfc_sli_ring *pring;
9772
2a76a283 9773 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 9774 psli->sli_flag = 0;
dea3101e 9775
604a3e30
JB
9776 psli->iocbq_lookup = NULL;
9777 psli->iocbq_lookup_len = 0;
9778 psli->last_iotag = 0;
9779
dea3101e 9780 for (i = 0; i < psli->num_rings; i++) {
895427bd 9781 pring = &psli->sli3_ring[i];
dea3101e
JB
9782 switch (i) {
9783 case LPFC_FCP_RING: /* ring 0 - FCP */
9784 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9785 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9786 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9787 pring->sli.sli3.numCiocb +=
9788 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9789 pring->sli.sli3.numRiocb +=
9790 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9791 pring->sli.sli3.numCiocb +=
9792 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9793 pring->sli.sli3.numRiocb +=
9794 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9795 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9796 SLI3_IOCB_CMD_SIZE :
9797 SLI2_IOCB_CMD_SIZE;
7e56aa25 9798 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9799 SLI3_IOCB_RSP_SIZE :
9800 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9801 pring->iotag_ctr = 0;
9802 pring->iotag_max =
92d7f7b0 9803 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
9804 pring->fast_iotag = pring->iotag_max;
9805 pring->num_mask = 0;
9806 break;
a4bc3379 9807 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 9808 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9809 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9810 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9811 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9812 SLI3_IOCB_CMD_SIZE :
9813 SLI2_IOCB_CMD_SIZE;
7e56aa25 9814 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9815 SLI3_IOCB_RSP_SIZE :
9816 SLI2_IOCB_RSP_SIZE;
2e0fef85 9817 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9818 pring->num_mask = 0;
9819 break;
9820 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9821 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9822 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9823 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9824 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9825 SLI3_IOCB_CMD_SIZE :
9826 SLI2_IOCB_CMD_SIZE;
7e56aa25 9827 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9828 SLI3_IOCB_RSP_SIZE :
9829 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9830 pring->fast_iotag = 0;
9831 pring->iotag_ctr = 0;
9832 pring->iotag_max = 4096;
57127f15
JS
9833 pring->lpfc_sli_rcv_async_status =
9834 lpfc_sli_async_event_handler;
6669f9bb 9835 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9836 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9837 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9838 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9839 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9840 lpfc_els_unsol_event;
dea3101e 9841 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9842 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9843 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9844 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9845 lpfc_els_unsol_event;
dea3101e
JB
9846 pring->prt[2].profile = 0; /* Mask 2 */
9847 /* NameServer Inquiry */
6a9c52cf 9848 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9849 /* NameServer */
6a9c52cf 9850 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9851 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9852 lpfc_ct_unsol_event;
dea3101e
JB
9853 pring->prt[3].profile = 0; /* Mask 3 */
9854 /* NameServer response */
6a9c52cf 9855 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9856 /* NameServer */
6a9c52cf 9857 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9858 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9859 lpfc_ct_unsol_event;
dea3101e
JB
9860 break;
9861 }
7e56aa25
JS
9862 totiocbsize += (pring->sli.sli3.numCiocb *
9863 pring->sli.sli3.sizeCiocb) +
9864 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 9865 }
ed957684 9866 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 9867 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
9868 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9869 "SLI2 SLIM Data: x%x x%lx\n",
9870 phba->brd_no, totiocbsize,
9871 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 9872 }
cf5bf97e
JW
9873 if (phba->cfg_multi_ring_support == 2)
9874 lpfc_extra_ring_setup(phba);
dea3101e
JB
9875
9876 return 0;
9877}
9878
e59058c4 9879/**
895427bd 9880 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
9881 * @phba: Pointer to HBA context object.
9882 *
895427bd 9883 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
9884 * ring. This function also initializes ring indices of each ring.
9885 * This function is called during the initialization of the SLI
9886 * interface of an HBA.
9887 * This function is called with no lock held and always returns
9888 * 1.
9889 **/
895427bd
JS
9890void
9891lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
9892{
9893 struct lpfc_sli *psli;
9894 struct lpfc_sli_ring *pring;
604a3e30 9895 int i;
dea3101e
JB
9896
9897 psli = &phba->sli;
2e0fef85 9898 spin_lock_irq(&phba->hbalock);
dea3101e 9899 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9900 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 9901 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
9902 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9903 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 9904 pring->flag = 0;
895427bd 9905 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
9906 INIT_LIST_HEAD(&pring->txq);
9907 INIT_LIST_HEAD(&pring->txcmplq);
9908 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 9909 spin_lock_init(&pring->ring_lock);
dea3101e 9910 }
895427bd
JS
9911 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9912 pring = phba->sli4_hba.nvme_wq[i]->pring;
9913 pring->flag = 0;
9914 pring->ringno = LPFC_FCP_RING;
9915 INIT_LIST_HEAD(&pring->txq);
9916 INIT_LIST_HEAD(&pring->txcmplq);
9917 INIT_LIST_HEAD(&pring->iocb_continueq);
9918 spin_lock_init(&pring->ring_lock);
9919 }
9920 pring = phba->sli4_hba.els_wq->pring;
9921 pring->flag = 0;
9922 pring->ringno = LPFC_ELS_RING;
9923 INIT_LIST_HEAD(&pring->txq);
9924 INIT_LIST_HEAD(&pring->txcmplq);
9925 INIT_LIST_HEAD(&pring->iocb_continueq);
9926 spin_lock_init(&pring->ring_lock);
dea3101e 9927
895427bd
JS
9928 if (phba->cfg_nvme_io_channel) {
9929 pring = phba->sli4_hba.nvmels_wq->pring;
9930 pring->flag = 0;
9931 pring->ringno = LPFC_ELS_RING;
9932 INIT_LIST_HEAD(&pring->txq);
9933 INIT_LIST_HEAD(&pring->txcmplq);
9934 INIT_LIST_HEAD(&pring->iocb_continueq);
9935 spin_lock_init(&pring->ring_lock);
9936 }
9937
9938 if (phba->cfg_fof) {
9939 pring = phba->sli4_hba.oas_wq->pring;
9940 pring->flag = 0;
9941 pring->ringno = LPFC_FCP_RING;
9942 INIT_LIST_HEAD(&pring->txq);
9943 INIT_LIST_HEAD(&pring->txcmplq);
9944 INIT_LIST_HEAD(&pring->iocb_continueq);
9945 spin_lock_init(&pring->ring_lock);
9946 }
9947
9948 spin_unlock_irq(&phba->hbalock);
9949}
9950
9951/**
9952 * lpfc_sli_queue_init - Queue initialization function
9953 * @phba: Pointer to HBA context object.
9954 *
9955 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9956 * ring. This function also initializes ring indices of each ring.
9957 * This function is called during the initialization of the SLI
9958 * interface of an HBA.
9959 * This function is called with no lock held and always returns
9960 * 1.
9961 **/
9962void
9963lpfc_sli_queue_init(struct lpfc_hba *phba)
dea3101e
JB
9964{
9965 struct lpfc_sli *psli;
9966 struct lpfc_sli_ring *pring;
604a3e30 9967 int i;
dea3101e
JB
9968
9969 psli = &phba->sli;
2e0fef85 9970 spin_lock_irq(&phba->hbalock);
dea3101e 9971 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9972 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e
JB
9973 /* Initialize list headers for txq and txcmplq as double linked lists */
9974 for (i = 0; i < psli->num_rings; i++) {
895427bd 9975 pring = &psli->sli3_ring[i];
dea3101e 9976 pring->ringno = i;
7e56aa25
JS
9977 pring->sli.sli3.next_cmdidx = 0;
9978 pring->sli.sli3.local_getidx = 0;
9979 pring->sli.sli3.cmdidx = 0;
dea3101e 9980 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 9981 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 9982 INIT_LIST_HEAD(&pring->postbufq);
895427bd
JS
9983 pring->flag = 0;
9984 INIT_LIST_HEAD(&pring->txq);
9985 INIT_LIST_HEAD(&pring->txcmplq);
7e56aa25 9986 spin_lock_init(&pring->ring_lock);
dea3101e 9987 }
2e0fef85 9988 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
9989}
9990
04c68496
JS
9991/**
9992 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9993 * @phba: Pointer to HBA context object.
9994 *
9995 * This routine flushes the mailbox command subsystem. It will unconditionally
9996 * flush all the mailbox commands in the three possible stages in the mailbox
9997 * command sub-system: pending mailbox command queue; the outstanding mailbox
9998 * command; and completed mailbox command queue. It is caller's responsibility
9999 * to make sure that the driver is in the proper state to flush the mailbox
10000 * command sub-system. Namely, the posting of mailbox commands into the
10001 * pending mailbox command queue from the various clients must be stopped;
10002 * either the HBA is in a state that it will never works on the outstanding
10003 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10004 * mailbox command has been completed.
10005 **/
10006static void
10007lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10008{
10009 LIST_HEAD(completions);
10010 struct lpfc_sli *psli = &phba->sli;
10011 LPFC_MBOXQ_t *pmb;
10012 unsigned long iflag;
10013
10014 /* Flush all the mailbox commands in the mbox system */
10015 spin_lock_irqsave(&phba->hbalock, iflag);
10016 /* The pending mailbox command queue */
10017 list_splice_init(&phba->sli.mboxq, &completions);
10018 /* The outstanding active mailbox command */
10019 if (psli->mbox_active) {
10020 list_add_tail(&psli->mbox_active->list, &completions);
10021 psli->mbox_active = NULL;
10022 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10023 }
10024 /* The completed mailbox command queue */
10025 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10026 spin_unlock_irqrestore(&phba->hbalock, iflag);
10027
10028 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10029 while (!list_empty(&completions)) {
10030 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10031 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10032 if (pmb->mbox_cmpl)
10033 pmb->mbox_cmpl(phba, pmb);
10034 }
10035}
10036
e59058c4 10037/**
3621a710 10038 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
10039 * @vport: Pointer to virtual port object.
10040 *
10041 * lpfc_sli_host_down is called to clean up the resources
10042 * associated with a vport before destroying virtual
10043 * port data structures.
10044 * This function does following operations:
10045 * - Free discovery resources associated with this virtual
10046 * port.
10047 * - Free iocbs associated with this virtual port in
10048 * the txq.
10049 * - Send abort for all iocb commands associated with this
10050 * vport in txcmplq.
10051 *
10052 * This function is called with no lock held and always returns 1.
10053 **/
92d7f7b0
JS
10054int
10055lpfc_sli_host_down(struct lpfc_vport *vport)
10056{
858c9f6c 10057 LIST_HEAD(completions);
92d7f7b0
JS
10058 struct lpfc_hba *phba = vport->phba;
10059 struct lpfc_sli *psli = &phba->sli;
895427bd 10060 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
10061 struct lpfc_sli_ring *pring;
10062 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
10063 int i;
10064 unsigned long flags = 0;
10065 uint16_t prev_pring_flag;
10066
10067 lpfc_cleanup_discovery_resources(vport);
10068
10069 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 10070
895427bd
JS
10071 /*
10072 * Error everything on the txq since these iocbs
10073 * have not been given to the FW yet.
10074 * Also issue ABTS for everything on the txcmplq
10075 */
10076 if (phba->sli_rev != LPFC_SLI_REV4) {
10077 for (i = 0; i < psli->num_rings; i++) {
10078 pring = &psli->sli3_ring[i];
10079 prev_pring_flag = pring->flag;
10080 /* Only slow rings */
10081 if (pring->ringno == LPFC_ELS_RING) {
10082 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10083 /* Set the lpfc data pending flag */
10084 set_bit(LPFC_DATA_READY, &phba->data_flags);
10085 }
10086 list_for_each_entry_safe(iocb, next_iocb,
10087 &pring->txq, list) {
10088 if (iocb->vport != vport)
10089 continue;
10090 list_move_tail(&iocb->list, &completions);
10091 }
10092 list_for_each_entry_safe(iocb, next_iocb,
10093 &pring->txcmplq, list) {
10094 if (iocb->vport != vport)
10095 continue;
10096 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10097 }
10098 pring->flag = prev_pring_flag;
10099 }
10100 } else {
10101 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10102 pring = qp->pring;
10103 if (!pring)
92d7f7b0 10104 continue;
895427bd
JS
10105 if (pring == phba->sli4_hba.els_wq->pring) {
10106 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10107 /* Set the lpfc data pending flag */
10108 set_bit(LPFC_DATA_READY, &phba->data_flags);
10109 }
10110 prev_pring_flag = pring->flag;
10111 spin_lock_irq(&pring->ring_lock);
10112 list_for_each_entry_safe(iocb, next_iocb,
10113 &pring->txq, list) {
10114 if (iocb->vport != vport)
10115 continue;
10116 list_move_tail(&iocb->list, &completions);
10117 }
10118 spin_unlock_irq(&pring->ring_lock);
10119 list_for_each_entry_safe(iocb, next_iocb,
10120 &pring->txcmplq, list) {
10121 if (iocb->vport != vport)
10122 continue;
10123 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10124 }
10125 pring->flag = prev_pring_flag;
92d7f7b0 10126 }
92d7f7b0 10127 }
92d7f7b0
JS
10128 spin_unlock_irqrestore(&phba->hbalock, flags);
10129
a257bf90
JS
10130 /* Cancel all the IOCBs from the completions list */
10131 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10132 IOERR_SLI_DOWN);
92d7f7b0
JS
10133 return 1;
10134}
10135
e59058c4 10136/**
3621a710 10137 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10138 * @phba: Pointer to HBA context object.
10139 *
10140 * This function cleans up all iocb, buffers, mailbox commands
10141 * while shutting down the HBA. This function is called with no
10142 * lock held and always returns 1.
10143 * This function does the following to cleanup driver resources:
10144 * - Free discovery resources for each virtual port
10145 * - Cleanup any pending fabric iocbs
10146 * - Iterate through the iocb txq and free each entry
10147 * in the list.
10148 * - Free up any buffer posted to the HBA
10149 * - Free mailbox commands in the mailbox queue.
10150 **/
dea3101e 10151int
2e0fef85 10152lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10153{
2534ba75 10154 LIST_HEAD(completions);
2e0fef85 10155 struct lpfc_sli *psli = &phba->sli;
895427bd 10156 struct lpfc_queue *qp = NULL;
dea3101e 10157 struct lpfc_sli_ring *pring;
0ff10d46 10158 struct lpfc_dmabuf *buf_ptr;
dea3101e 10159 unsigned long flags = 0;
04c68496
JS
10160 int i;
10161
10162 /* Shutdown the mailbox command sub-system */
618a5230 10163 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10164
dea3101e
JB
10165 lpfc_hba_down_prep(phba);
10166
92d7f7b0
JS
10167 lpfc_fabric_abort_hba(phba);
10168
2e0fef85 10169 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10170
895427bd
JS
10171 /*
10172 * Error everything on the txq since these iocbs
10173 * have not been given to the FW yet.
10174 */
10175 if (phba->sli_rev != LPFC_SLI_REV4) {
10176 for (i = 0; i < psli->num_rings; i++) {
10177 pring = &psli->sli3_ring[i];
10178 /* Only slow rings */
10179 if (pring->ringno == LPFC_ELS_RING) {
10180 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10181 /* Set the lpfc data pending flag */
10182 set_bit(LPFC_DATA_READY, &phba->data_flags);
10183 }
10184 list_splice_init(&pring->txq, &completions);
10185 }
10186 } else {
10187 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10188 pring = qp->pring;
10189 if (!pring)
10190 continue;
10191 spin_lock_irq(&pring->ring_lock);
10192 list_splice_init(&pring->txq, &completions);
10193 spin_unlock_irq(&pring->ring_lock);
10194 if (pring == phba->sli4_hba.els_wq->pring) {
10195 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10196 /* Set the lpfc data pending flag */
10197 set_bit(LPFC_DATA_READY, &phba->data_flags);
10198 }
10199 }
2534ba75 10200 }
2e0fef85 10201 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10202
a257bf90
JS
10203 /* Cancel all the IOCBs from the completions list */
10204 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10205 IOERR_SLI_DOWN);
dea3101e 10206
0ff10d46
JS
10207 spin_lock_irqsave(&phba->hbalock, flags);
10208 list_splice_init(&phba->elsbuf, &completions);
10209 phba->elsbuf_cnt = 0;
10210 phba->elsbuf_prev_cnt = 0;
10211 spin_unlock_irqrestore(&phba->hbalock, flags);
10212
10213 while (!list_empty(&completions)) {
10214 list_remove_head(&completions, buf_ptr,
10215 struct lpfc_dmabuf, list);
10216 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10217 kfree(buf_ptr);
10218 }
10219
dea3101e
JB
10220 /* Return any active mbox cmds */
10221 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10222
da0436e9 10223 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10224 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10225 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10226
da0436e9
JS
10227 return 1;
10228}
10229
e59058c4 10230/**
3621a710 10231 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10232 * @srcp: Source memory pointer.
10233 * @destp: Destination memory pointer.
10234 * @cnt: Number of words required to be copied.
10235 *
10236 * This function is used for copying data between driver memory
10237 * and the SLI memory. This function also changes the endianness
10238 * of each word if native endianness is different from SLI
10239 * endianness. This function can be called with or without
10240 * lock.
10241 **/
dea3101e
JB
10242void
10243lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10244{
10245 uint32_t *src = srcp;
10246 uint32_t *dest = destp;
10247 uint32_t ldata;
10248 int i;
10249
10250 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10251 ldata = *src;
10252 ldata = le32_to_cpu(ldata);
10253 *dest = ldata;
10254 src++;
10255 dest++;
10256 }
10257}
10258
e59058c4 10259
a0c87cbd
JS
10260/**
10261 * lpfc_sli_bemem_bcopy - SLI memory copy function
10262 * @srcp: Source memory pointer.
10263 * @destp: Destination memory pointer.
10264 * @cnt: Number of words required to be copied.
10265 *
10266 * This function is used for copying data between a data structure
10267 * with big endian representation to local endianness.
10268 * This function can be called with or without lock.
10269 **/
10270void
10271lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10272{
10273 uint32_t *src = srcp;
10274 uint32_t *dest = destp;
10275 uint32_t ldata;
10276 int i;
10277
10278 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10279 ldata = *src;
10280 ldata = be32_to_cpu(ldata);
10281 *dest = ldata;
10282 src++;
10283 dest++;
10284 }
10285}
10286
e59058c4 10287/**
3621a710 10288 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10289 * @phba: Pointer to HBA context object.
10290 * @pring: Pointer to driver SLI ring object.
10291 * @mp: Pointer to driver buffer object.
10292 *
10293 * This function is called with no lock held.
10294 * It always return zero after adding the buffer to the postbufq
10295 * buffer list.
10296 **/
dea3101e 10297int
2e0fef85
JS
10298lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10299 struct lpfc_dmabuf *mp)
dea3101e
JB
10300{
10301 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10302 later */
2e0fef85 10303 spin_lock_irq(&phba->hbalock);
dea3101e 10304 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10305 pring->postbufq_cnt++;
2e0fef85 10306 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10307 return 0;
10308}
10309
e59058c4 10310/**
3621a710 10311 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10312 * @phba: Pointer to HBA context object.
10313 *
10314 * When HBQ is enabled, buffers are searched based on tags. This function
10315 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10316 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10317 * does not conflict with tags of buffer posted for unsolicited events.
10318 * The function returns the allocated tag. The function is called with
10319 * no locks held.
10320 **/
76bb24ef
JS
10321uint32_t
10322lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10323{
10324 spin_lock_irq(&phba->hbalock);
10325 phba->buffer_tag_count++;
10326 /*
10327 * Always set the QUE_BUFTAG_BIT to distiguish between
10328 * a tag assigned by HBQ.
10329 */
10330 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10331 spin_unlock_irq(&phba->hbalock);
10332 return phba->buffer_tag_count;
10333}
10334
e59058c4 10335/**
3621a710 10336 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10337 * @phba: Pointer to HBA context object.
10338 * @pring: Pointer to driver SLI ring object.
10339 * @tag: Buffer tag.
10340 *
10341 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10342 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10343 * iocb is posted to the response ring with the tag of the buffer.
10344 * This function searches the pring->postbufq list using the tag
10345 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10346 * iocb. If the buffer is found then lpfc_dmabuf object of the
10347 * buffer is returned to the caller else NULL is returned.
10348 * This function is called with no lock held.
10349 **/
76bb24ef
JS
10350struct lpfc_dmabuf *
10351lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10352 uint32_t tag)
10353{
10354 struct lpfc_dmabuf *mp, *next_mp;
10355 struct list_head *slp = &pring->postbufq;
10356
25985edc 10357 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10358 spin_lock_irq(&phba->hbalock);
10359 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10360 if (mp->buffer_tag == tag) {
10361 list_del_init(&mp->list);
10362 pring->postbufq_cnt--;
10363 spin_unlock_irq(&phba->hbalock);
10364 return mp;
10365 }
10366 }
10367
10368 spin_unlock_irq(&phba->hbalock);
10369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10370 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10371 "ring %d Data x%lx x%p x%p x%x\n",
10372 pring->ringno, (unsigned long) tag,
10373 slp->next, slp->prev, pring->postbufq_cnt);
10374
10375 return NULL;
10376}
dea3101e 10377
e59058c4 10378/**
3621a710 10379 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10380 * @phba: Pointer to HBA context object.
10381 * @pring: Pointer to driver SLI ring object.
10382 * @phys: DMA address of the buffer.
10383 *
10384 * This function searches the buffer list using the dma_address
10385 * of unsolicited event to find the driver's lpfc_dmabuf object
10386 * corresponding to the dma_address. The function returns the
10387 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10388 * This function is called by the ct and els unsolicited event
10389 * handlers to get the buffer associated with the unsolicited
10390 * event.
10391 *
10392 * This function is called with no lock held.
10393 **/
dea3101e
JB
10394struct lpfc_dmabuf *
10395lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10396 dma_addr_t phys)
10397{
10398 struct lpfc_dmabuf *mp, *next_mp;
10399 struct list_head *slp = &pring->postbufq;
10400
25985edc 10401 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10402 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10403 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10404 if (mp->phys == phys) {
10405 list_del_init(&mp->list);
10406 pring->postbufq_cnt--;
2e0fef85 10407 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10408 return mp;
10409 }
10410 }
10411
2e0fef85 10412 spin_unlock_irq(&phba->hbalock);
dea3101e 10413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10414 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10415 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10416 pring->ringno, (unsigned long long)phys,
dea3101e
JB
10417 slp->next, slp->prev, pring->postbufq_cnt);
10418 return NULL;
10419}
10420
e59058c4 10421/**
3621a710 10422 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10423 * @phba: Pointer to HBA context object.
10424 * @cmdiocb: Pointer to driver command iocb object.
10425 * @rspiocb: Pointer to driver response iocb object.
10426 *
10427 * This function is the completion handler for the abort iocbs for
10428 * ELS commands. This function is called from the ELS ring event
10429 * handler with no lock held. This function frees memory resources
10430 * associated with the abort iocb.
10431 **/
dea3101e 10432static void
2e0fef85
JS
10433lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10434 struct lpfc_iocbq *rspiocb)
dea3101e 10435{
2e0fef85 10436 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10437 uint16_t abort_iotag, abort_context;
ff78d8f9 10438 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10439
10440 if (irsp->ulpStatus) {
ff78d8f9
JS
10441
10442 /*
10443 * Assume that the port already completed and returned, or
10444 * will return the iocb. Just Log the message.
10445 */
2680eeaa
JS
10446 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10447 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10448
2e0fef85 10449 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10450 if (phba->sli_rev < LPFC_SLI_REV4) {
10451 if (abort_iotag != 0 &&
10452 abort_iotag <= phba->sli.last_iotag)
10453 abort_iocb =
10454 phba->sli.iocbq_lookup[abort_iotag];
10455 } else
10456 /* For sli4 the abort_tag is the XRI,
10457 * so the abort routine puts the iotag of the iocb
10458 * being aborted in the context field of the abort
10459 * IOCB.
10460 */
10461 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10462
2a9bf3d0
JS
10463 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10464 "0327 Cannot abort els iocb %p "
10465 "with tag %x context %x, abort status %x, "
10466 "abort code %x\n",
10467 abort_iocb, abort_iotag, abort_context,
10468 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10469
ff78d8f9 10470 spin_unlock_irq(&phba->hbalock);
2680eeaa 10471 }
604a3e30 10472 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
10473 return;
10474}
10475
e59058c4 10476/**
3621a710 10477 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10478 * @phba: Pointer to HBA context object.
10479 * @cmdiocb: Pointer to driver command iocb object.
10480 * @rspiocb: Pointer to driver response iocb object.
10481 *
10482 * The function is called from SLI ring event handler with no
10483 * lock held. This function is the completion handler for ELS commands
10484 * which are aborted. The function frees memory resources used for
10485 * the aborted ELS commands.
10486 **/
92d7f7b0
JS
10487static void
10488lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10489 struct lpfc_iocbq *rspiocb)
10490{
10491 IOCB_t *irsp = &rspiocb->iocb;
10492
10493 /* ELS cmd tag <ulpIoTag> completes */
10494 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10495 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10496 "x%x x%x x%x\n",
e8b62011 10497 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10498 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10499 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10500 lpfc_ct_free_iocb(phba, cmdiocb);
10501 else
10502 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10503 return;
10504}
10505
e59058c4 10506/**
5af5eee7 10507 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10508 * @phba: Pointer to HBA context object.
10509 * @pring: Pointer to driver SLI ring object.
10510 * @cmdiocb: Pointer to driver command iocb object.
10511 *
5af5eee7
JS
10512 * This function issues an abort iocb for the provided command iocb down to
10513 * the port. Other than the case the outstanding command iocb is an abort
10514 * request, this function issues abort out unconditionally. This function is
10515 * called with hbalock held. The function returns 0 when it fails due to
10516 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10517 **/
5af5eee7
JS
10518static int
10519lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10520 struct lpfc_iocbq *cmdiocb)
dea3101e 10521{
2e0fef85 10522 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10523 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
10524 IOCB_t *icmd = NULL;
10525 IOCB_t *iabt = NULL;
5af5eee7 10526 int retval;
7e56aa25 10527 unsigned long iflags;
07951076 10528
1c2ba475
JT
10529 lockdep_assert_held(&phba->hbalock);
10530
92d7f7b0
JS
10531 /*
10532 * There are certain command types we don't want to abort. And we
10533 * don't want to abort commands that are already in the process of
10534 * being aborted.
07951076
JS
10535 */
10536 icmd = &cmdiocb->iocb;
2e0fef85 10537 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10538 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10539 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10540 return 0;
10541
dea3101e 10542 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10543 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
10544 if (abtsiocbp == NULL)
10545 return 0;
dea3101e 10546
07951076 10547 /* This signals the response to set the correct status
341af102 10548 * before calling the completion handler
07951076
JS
10549 */
10550 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10551
dea3101e 10552 iabt = &abtsiocbp->iocb;
07951076
JS
10553 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10554 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10555 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10556 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10557 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10558 }
da0436e9
JS
10559 else
10560 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10561 iabt->ulpLe = 1;
10562 iabt->ulpClass = icmd->ulpClass;
dea3101e 10563
5ffc266e 10564 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10565 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10566 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10567 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10568 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10569 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10570
2e0fef85 10571 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10572 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10573 else
10574 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10575
07951076 10576 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10577 abtsiocbp->vport = vport;
5b8bd0c9 10578
e8b62011
JS
10579 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10580 "0339 Abort xri x%x, original iotag x%x, "
10581 "abort cmd iotag x%x\n",
2a9bf3d0 10582 iabt->un.acxri.abortIoTag,
e8b62011 10583 iabt->un.acxri.abortContextTag,
2a9bf3d0 10584 abtsiocbp->iotag);
7e56aa25
JS
10585
10586 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10587 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10588 if (unlikely(pring == NULL))
9bd2bff5 10589 return 0;
7e56aa25
JS
10590 /* Note: both hbalock and ring_lock need to be set here */
10591 spin_lock_irqsave(&pring->ring_lock, iflags);
10592 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10593 abtsiocbp, 0);
10594 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10595 } else {
10596 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10597 abtsiocbp, 0);
10598 }
dea3101e 10599
d7c255b2
JS
10600 if (retval)
10601 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10602
10603 /*
10604 * Caller to this routine should check for IOCB_ERROR
10605 * and handle it properly. This routine no longer removes
10606 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10607 */
10608 return retval;
10609}
10610
10611/**
10612 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10613 * @phba: Pointer to HBA context object.
10614 * @pring: Pointer to driver SLI ring object.
10615 * @cmdiocb: Pointer to driver command iocb object.
10616 *
10617 * This function issues an abort iocb for the provided command iocb. In case
10618 * of unloading, the abort iocb will not be issued to commands on the ELS
10619 * ring. Instead, the callback function shall be changed to those commands
10620 * so that nothing happens when them finishes. This function is called with
10621 * hbalock held. The function returns 0 when the command iocb is an abort
10622 * request.
10623 **/
10624int
10625lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10626 struct lpfc_iocbq *cmdiocb)
10627{
10628 struct lpfc_vport *vport = cmdiocb->vport;
10629 int retval = IOCB_ERROR;
10630 IOCB_t *icmd = NULL;
10631
1c2ba475
JT
10632 lockdep_assert_held(&phba->hbalock);
10633
5af5eee7
JS
10634 /*
10635 * There are certain command types we don't want to abort. And we
10636 * don't want to abort commands that are already in the process of
10637 * being aborted.
10638 */
10639 icmd = &cmdiocb->iocb;
10640 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10641 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10642 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10643 return 0;
10644
1234a6d5
DK
10645 if (!pring) {
10646 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10647 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10648 else
10649 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10650 goto abort_iotag_exit;
10651 }
10652
5af5eee7
JS
10653 /*
10654 * If we're unloading, don't abort iocb on the ELS ring, but change
10655 * the callback so that nothing happens when it finishes.
10656 */
10657 if ((vport->load_flag & FC_UNLOADING) &&
10658 (pring->ringno == LPFC_ELS_RING)) {
10659 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10660 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10661 else
10662 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10663 goto abort_iotag_exit;
10664 }
10665
10666 /* Now, we try to issue the abort to the cmdiocb out */
10667 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10668
07951076 10669abort_iotag_exit:
2e0fef85
JS
10670 /*
10671 * Caller to this routine should check for IOCB_ERROR
10672 * and handle it properly. This routine no longer removes
10673 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10674 */
2e0fef85 10675 return retval;
dea3101e
JB
10676}
10677
895427bd
JS
10678/**
10679 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10680 * @phba: Pointer to HBA context object.
10681 * @pring: Pointer to driver SLI ring object.
10682 * @cmdiocb: Pointer to driver command iocb object.
10683 *
10684 * This function issues an abort iocb for the provided command iocb down to
10685 * the port. Other than the case the outstanding command iocb is an abort
10686 * request, this function issues abort out unconditionally. This function is
10687 * called with hbalock held. The function returns 0 when it fails due to
10688 * memory allocation failure or when the command iocb is an abort request.
10689 **/
10690static int
10691lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10692 struct lpfc_iocbq *cmdiocb)
10693{
10694 struct lpfc_vport *vport = cmdiocb->vport;
10695 struct lpfc_iocbq *abtsiocbp;
10696 union lpfc_wqe *abts_wqe;
10697 int retval;
10698
10699 /*
10700 * There are certain command types we don't want to abort. And we
10701 * don't want to abort commands that are already in the process of
10702 * being aborted.
10703 */
10704 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10705 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10706 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10707 return 0;
10708
10709 /* issue ABTS for this io based on iotag */
10710 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10711 if (abtsiocbp == NULL)
10712 return 0;
10713
10714 /* This signals the response to set the correct status
10715 * before calling the completion handler
10716 */
10717 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10718
10719 /* Complete prepping the abort wqe and issue to the FW. */
10720 abts_wqe = &abtsiocbp->wqe;
10721 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10722 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10723
10724 /* Explicitly set reserved fields to zero.*/
10725 abts_wqe->abort_cmd.rsrvd4 = 0;
10726 abts_wqe->abort_cmd.rsrvd5 = 0;
10727
10728 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10729 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10730 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10731
10732 /* word 7 */
10733 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10734 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10735 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10736 cmdiocb->iocb.ulpClass);
10737
10738 /* word 8 - tell the FW to abort the IO associated with this
10739 * outstanding exchange ID.
10740 */
10741 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10742
10743 /* word 9 - this is the iotag for the abts_wqe completion. */
10744 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10745 abtsiocbp->iotag);
10746
10747 /* word 10 */
10748 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10749 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10750 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10751
10752 /* word 11 */
10753 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10754 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10755 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10756
10757 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10758 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10759 abtsiocbp->vport = vport;
01649561 10760 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd 10761 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
cd22d605 10762 if (retval) {
895427bd
JS
10763 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10764 "6147 Failed abts issue_wqe with status x%x "
10765 "for oxid x%x\n",
10766 retval, cmdiocb->sli4_xritag);
10767 lpfc_sli_release_iocbq(phba, abtsiocbp);
10768 return retval;
10769 }
10770
10771 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10772 "6148 Drv Abort NVME Request Issued for "
10773 "ox_id x%x on reqtag x%x\n",
10774 cmdiocb->sli4_xritag,
10775 abtsiocbp->iotag);
10776
10777 return retval;
10778}
10779
5af5eee7
JS
10780/**
10781 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10782 * @phba: pointer to lpfc HBA data structure.
10783 *
10784 * This routine will abort all pending and outstanding iocbs to an HBA.
10785 **/
10786void
10787lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10788{
10789 struct lpfc_sli *psli = &phba->sli;
10790 struct lpfc_sli_ring *pring;
895427bd 10791 struct lpfc_queue *qp = NULL;
5af5eee7
JS
10792 int i;
10793
895427bd
JS
10794 if (phba->sli_rev != LPFC_SLI_REV4) {
10795 for (i = 0; i < psli->num_rings; i++) {
10796 pring = &psli->sli3_ring[i];
10797 lpfc_sli_abort_iocb_ring(phba, pring);
10798 }
10799 return;
10800 }
10801 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10802 pring = qp->pring;
10803 if (!pring)
10804 continue;
db55fba8 10805 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
10806 }
10807}
10808
e59058c4 10809/**
3621a710 10810 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
10811 * @iocbq: Pointer to driver iocb object.
10812 * @vport: Pointer to driver virtual port object.
10813 * @tgt_id: SCSI ID of the target.
10814 * @lun_id: LUN ID of the scsi device.
10815 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10816 *
3621a710 10817 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
10818 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10819 * 0 if the filtering criteria is met for the given iocb and will return
10820 * 1 if the filtering criteria is not met.
10821 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10822 * given iocb is for the SCSI device specified by vport, tgt_id and
10823 * lun_id parameter.
10824 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10825 * given iocb is for the SCSI target specified by vport and tgt_id
10826 * parameters.
10827 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10828 * given iocb is for the SCSI host associated with the given vport.
10829 * This function is called with no locks held.
10830 **/
dea3101e 10831static int
51ef4c26
JS
10832lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10833 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 10834 lpfc_ctx_cmd ctx_cmd)
dea3101e 10835{
0bd4ca25 10836 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
10837 int rc = 1;
10838
0bd4ca25
JSEC
10839 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10840 return rc;
10841
51ef4c26
JS
10842 if (iocbq->vport != vport)
10843 return rc;
10844
0bd4ca25 10845 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 10846
495a714c 10847 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
10848 return rc;
10849
10850 switch (ctx_cmd) {
10851 case LPFC_CTX_LUN:
495a714c
JS
10852 if ((lpfc_cmd->rdata->pnode) &&
10853 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10854 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
10855 rc = 0;
10856 break;
10857 case LPFC_CTX_TGT:
495a714c
JS
10858 if ((lpfc_cmd->rdata->pnode) &&
10859 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
10860 rc = 0;
10861 break;
dea3101e
JB
10862 case LPFC_CTX_HOST:
10863 rc = 0;
10864 break;
10865 default:
10866 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 10867 __func__, ctx_cmd);
dea3101e
JB
10868 break;
10869 }
10870
10871 return rc;
10872}
10873
e59058c4 10874/**
3621a710 10875 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
10876 * @vport: Pointer to virtual port.
10877 * @tgt_id: SCSI ID of the target.
10878 * @lun_id: LUN ID of the scsi device.
10879 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10880 *
10881 * This function returns number of FCP commands pending for the vport.
10882 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10883 * commands pending on the vport associated with SCSI device specified
10884 * by tgt_id and lun_id parameters.
10885 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10886 * commands pending on the vport associated with SCSI target specified
10887 * by tgt_id parameter.
10888 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10889 * commands pending on the vport.
10890 * This function returns the number of iocbs which satisfy the filter.
10891 * This function is called without any lock held.
10892 **/
dea3101e 10893int
51ef4c26
JS
10894lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10895 lpfc_ctx_cmd ctx_cmd)
dea3101e 10896{
51ef4c26 10897 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10898 struct lpfc_iocbq *iocbq;
10899 int sum, i;
dea3101e 10900
31979008 10901 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
10902 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10903 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10904
51ef4c26
JS
10905 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10906 ctx_cmd) == 0)
0bd4ca25 10907 sum++;
dea3101e 10908 }
31979008 10909 spin_unlock_irq(&phba->hbalock);
0bd4ca25 10910
dea3101e
JB
10911 return sum;
10912}
10913
e59058c4 10914/**
3621a710 10915 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
10916 * @phba: Pointer to HBA context object
10917 * @cmdiocb: Pointer to command iocb object.
10918 * @rspiocb: Pointer to response iocb object.
10919 *
10920 * This function is called when an aborted FCP iocb completes. This
10921 * function is called by the ring event handler with no lock held.
10922 * This function frees the iocb.
10923 **/
5eb95af0 10924void
2e0fef85
JS
10925lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10926 struct lpfc_iocbq *rspiocb)
5eb95af0 10927{
cb69f7de 10928 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 10929 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
10930 "original iotag x%x, abort cmd iotag x%x "
10931 "status 0x%x, reason 0x%x\n",
10932 cmdiocb->iocb.un.acxri.abortContextTag,
10933 cmdiocb->iocb.un.acxri.abortIoTag,
10934 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10935 rspiocb->iocb.un.ulpWord[4]);
604a3e30 10936 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
10937 return;
10938}
10939
e59058c4 10940/**
3621a710 10941 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
10942 * @vport: Pointer to virtual port.
10943 * @pring: Pointer to driver SLI ring object.
10944 * @tgt_id: SCSI ID of the target.
10945 * @lun_id: LUN ID of the scsi device.
10946 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10947 *
10948 * This function sends an abort command for every SCSI command
10949 * associated with the given virtual port pending on the ring
10950 * filtered by lpfc_sli_validate_fcp_iocb function.
10951 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10952 * FCP iocbs associated with lun specified by tgt_id and lun_id
10953 * parameters
10954 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10955 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10956 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10957 * FCP iocbs associated with virtual port.
10958 * This function returns number of iocbs it failed to abort.
10959 * This function is called with no locks held.
10960 **/
dea3101e 10961int
51ef4c26
JS
10962lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10963 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 10964{
51ef4c26 10965 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10966 struct lpfc_iocbq *iocbq;
10967 struct lpfc_iocbq *abtsiocb;
ecbb227e 10968 struct lpfc_sli_ring *pring_s4;
dea3101e 10969 IOCB_t *cmd = NULL;
dea3101e 10970 int errcnt = 0, ret_val = 0;
0bd4ca25 10971 int i;
dea3101e 10972
0bd4ca25
JSEC
10973 for (i = 1; i <= phba->sli.last_iotag; i++) {
10974 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10975
51ef4c26 10976 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 10977 abort_cmd) != 0)
dea3101e
JB
10978 continue;
10979
afbd8d88
JS
10980 /*
10981 * If the iocbq is already being aborted, don't take a second
10982 * action, but do count it.
10983 */
10984 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10985 continue;
10986
dea3101e 10987 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 10988 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
10989 if (abtsiocb == NULL) {
10990 errcnt++;
10991 continue;
10992 }
dea3101e 10993
afbd8d88
JS
10994 /* indicate the IO is being aborted by the driver. */
10995 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10996
0bd4ca25 10997 cmd = &iocbq->iocb;
dea3101e
JB
10998 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10999 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
11000 if (phba->sli_rev == LPFC_SLI_REV4)
11001 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11002 else
11003 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
11004 abtsiocb->iocb.ulpLe = 1;
11005 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 11006 abtsiocb->vport = vport;
dea3101e 11007
5ffc266e 11008 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11009 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
11010 if (iocbq->iocb_flag & LPFC_IO_FCP)
11011 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11012 if (iocbq->iocb_flag & LPFC_IO_FOF)
11013 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 11014
2e0fef85 11015 if (lpfc_is_link_up(phba))
dea3101e
JB
11016 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11017 else
11018 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11019
5eb95af0
JSEC
11020 /* Setup callback routine and issue the command. */
11021 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
ecbb227e
JS
11022 if (phba->sli_rev == LPFC_SLI_REV4) {
11023 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11024 if (!pring_s4)
11025 continue;
11026 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11027 abtsiocb, 0);
11028 } else
11029 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11030 abtsiocb, 0);
dea3101e 11031 if (ret_val == IOCB_ERROR) {
604a3e30 11032 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
11033 errcnt++;
11034 continue;
11035 }
11036 }
11037
11038 return errcnt;
11039}
11040
98912dda
JS
11041/**
11042 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11043 * @vport: Pointer to virtual port.
11044 * @pring: Pointer to driver SLI ring object.
11045 * @tgt_id: SCSI ID of the target.
11046 * @lun_id: LUN ID of the scsi device.
11047 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11048 *
11049 * This function sends an abort command for every SCSI command
11050 * associated with the given virtual port pending on the ring
11051 * filtered by lpfc_sli_validate_fcp_iocb function.
11052 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11053 * FCP iocbs associated with lun specified by tgt_id and lun_id
11054 * parameters
11055 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11056 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11057 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11058 * FCP iocbs associated with virtual port.
11059 * This function returns number of iocbs it aborted .
11060 * This function is called with no locks held right after a taskmgmt
11061 * command is sent.
11062 **/
11063int
11064lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11065 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11066{
11067 struct lpfc_hba *phba = vport->phba;
8c50d25c 11068 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 11069 struct lpfc_iocbq *abtsiocbq;
8c50d25c 11070 struct lpfc_nodelist *ndlp;
98912dda
JS
11071 struct lpfc_iocbq *iocbq;
11072 IOCB_t *icmd;
11073 int sum, i, ret_val;
11074 unsigned long iflags;
11075 struct lpfc_sli_ring *pring_s4;
98912dda
JS
11076
11077 spin_lock_irq(&phba->hbalock);
11078
11079 /* all I/Os are in process of being flushed */
11080 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11081 spin_unlock_irq(&phba->hbalock);
11082 return 0;
11083 }
11084 sum = 0;
11085
11086 for (i = 1; i <= phba->sli.last_iotag; i++) {
11087 iocbq = phba->sli.iocbq_lookup[i];
11088
11089 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11090 cmd) != 0)
11091 continue;
11092
11093 /*
11094 * If the iocbq is already being aborted, don't take a second
11095 * action, but do count it.
11096 */
11097 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11098 continue;
11099
11100 /* issue ABTS for this IOCB based on iotag */
11101 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11102 if (abtsiocbq == NULL)
11103 continue;
11104
11105 icmd = &iocbq->iocb;
11106 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11107 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11108 if (phba->sli_rev == LPFC_SLI_REV4)
11109 abtsiocbq->iocb.un.acxri.abortIoTag =
11110 iocbq->sli4_xritag;
11111 else
11112 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11113 abtsiocbq->iocb.ulpLe = 1;
11114 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11115 abtsiocbq->vport = vport;
11116
11117 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11118 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11119 if (iocbq->iocb_flag & LPFC_IO_FCP)
11120 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11121 if (iocbq->iocb_flag & LPFC_IO_FOF)
11122 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11123
8c50d25c
JS
11124 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11125 ndlp = lpfc_cmd->rdata->pnode;
11126
11127 if (lpfc_is_link_up(phba) &&
11128 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11129 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11130 else
11131 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11132
11133 /* Setup callback routine and issue the command. */
11134 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11135
11136 /*
11137 * Indicate the IO is being aborted by the driver and set
11138 * the caller's flag into the aborted IO.
11139 */
11140 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11141
11142 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11143 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11144 if (pring_s4 == NULL)
11145 continue;
98912dda
JS
11146 /* Note: both hbalock and ring_lock must be set here */
11147 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11148 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11149 abtsiocbq, 0);
11150 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11151 } else {
11152 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11153 abtsiocbq, 0);
11154 }
11155
11156
11157 if (ret_val == IOCB_ERROR)
11158 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11159 else
11160 sum++;
11161 }
11162 spin_unlock_irq(&phba->hbalock);
11163 return sum;
11164}
11165
e59058c4 11166/**
3621a710 11167 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11168 * @phba: Pointer to HBA context object.
11169 * @cmdiocbq: Pointer to command iocb.
11170 * @rspiocbq: Pointer to response iocb.
11171 *
11172 * This function is the completion handler for iocbs issued using
11173 * lpfc_sli_issue_iocb_wait function. This function is called by the
11174 * ring event handler function without any lock held. This function
11175 * can be called from both worker thread context and interrupt
11176 * context. This function also can be called from other thread which
11177 * cleans up the SLI layer objects.
11178 * This function copy the contents of the response iocb to the
11179 * response iocb memory object provided by the caller of
11180 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11181 * sleeps for the iocb completion.
11182 **/
68876920
JSEC
11183static void
11184lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11185 struct lpfc_iocbq *cmdiocbq,
11186 struct lpfc_iocbq *rspiocbq)
dea3101e 11187{
68876920
JSEC
11188 wait_queue_head_t *pdone_q;
11189 unsigned long iflags;
0f65ff68 11190 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11191
2e0fef85 11192 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11193 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11194
11195 /*
11196 * A time out has occurred for the iocb. If a time out
11197 * completion handler has been supplied, call it. Otherwise,
11198 * just free the iocbq.
11199 */
11200
11201 spin_unlock_irqrestore(&phba->hbalock, iflags);
11202 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11203 cmdiocbq->wait_iocb_cmpl = NULL;
11204 if (cmdiocbq->iocb_cmpl)
11205 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11206 else
11207 lpfc_sli_release_iocbq(phba, cmdiocbq);
11208 return;
11209 }
11210
68876920
JSEC
11211 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11212 if (cmdiocbq->context2 && rspiocbq)
11213 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11214 &rspiocbq->iocb, sizeof(IOCB_t));
11215
0f65ff68
JS
11216 /* Set the exchange busy flag for task management commands */
11217 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11218 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11219 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11220 cur_iocbq);
11221 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11222 }
11223
68876920 11224 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11225 if (pdone_q)
11226 wake_up(pdone_q);
858c9f6c 11227 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11228 return;
11229}
11230
d11e31dd
JS
11231/**
11232 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11233 * @phba: Pointer to HBA context object..
11234 * @piocbq: Pointer to command iocb.
11235 * @flag: Flag to test.
11236 *
11237 * This routine grabs the hbalock and then test the iocb_flag to
11238 * see if the passed in flag is set.
11239 * Returns:
11240 * 1 if flag is set.
11241 * 0 if flag is not set.
11242 **/
11243static int
11244lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11245 struct lpfc_iocbq *piocbq, uint32_t flag)
11246{
11247 unsigned long iflags;
11248 int ret;
11249
11250 spin_lock_irqsave(&phba->hbalock, iflags);
11251 ret = piocbq->iocb_flag & flag;
11252 spin_unlock_irqrestore(&phba->hbalock, iflags);
11253 return ret;
11254
11255}
11256
e59058c4 11257/**
3621a710 11258 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11259 * @phba: Pointer to HBA context object..
11260 * @pring: Pointer to sli ring.
11261 * @piocb: Pointer to command iocb.
11262 * @prspiocbq: Pointer to response iocb.
11263 * @timeout: Timeout in number of seconds.
11264 *
11265 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11266 * iocb to complete. The iocb_cmpl field of the shall be used
11267 * to handle iocbs which time out. If the field is NULL, the
11268 * function shall free the iocbq structure. If more clean up is
11269 * needed, the caller is expected to provide a completion function
11270 * that will provide the needed clean up. If the iocb command is
11271 * not completed within timeout seconds, the function will either
11272 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11273 * completion function set in the iocb_cmpl field and then return
11274 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11275 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11276 * The function waits for the iocb completion using an
11277 * non-interruptible wait.
11278 * This function will sleep while waiting for iocb completion.
11279 * So, this function should not be called from any context which
11280 * does not allow sleeping. Due to the same reason, this function
11281 * cannot be called with interrupt disabled.
11282 * This function assumes that the iocb completions occur while
11283 * this function sleep. So, this function cannot be called from
11284 * the thread which process iocb completion for this ring.
11285 * This function clears the iocb_flag of the iocb object before
11286 * issuing the iocb and the iocb completion handler sets this
11287 * flag and wakes this thread when the iocb completes.
11288 * The contents of the response iocb will be copied to prspiocbq
11289 * by the completion handler when the command completes.
11290 * This function returns IOCB_SUCCESS when success.
11291 * This function is called with no lock held.
11292 **/
dea3101e 11293int
2e0fef85 11294lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11295 uint32_t ring_number,
2e0fef85
JS
11296 struct lpfc_iocbq *piocb,
11297 struct lpfc_iocbq *prspiocbq,
68876920 11298 uint32_t timeout)
dea3101e 11299{
7259f0d0 11300 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11301 long timeleft, timeout_req = 0;
11302 int retval = IOCB_SUCCESS;
875fbdfe 11303 uint32_t creg_val;
0e9bb8d7
JS
11304 struct lpfc_iocbq *iocb;
11305 int txq_cnt = 0;
11306 int txcmplq_cnt = 0;
895427bd 11307 struct lpfc_sli_ring *pring;
5a0916b4
JS
11308 unsigned long iflags;
11309 bool iocb_completed = true;
11310
895427bd
JS
11311 if (phba->sli_rev >= LPFC_SLI_REV4)
11312 pring = lpfc_sli4_calc_ring(phba, piocb);
11313 else
11314 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11315 /*
68876920
JSEC
11316 * If the caller has provided a response iocbq buffer, then context2
11317 * is NULL or its an error.
dea3101e 11318 */
68876920
JSEC
11319 if (prspiocbq) {
11320 if (piocb->context2)
11321 return IOCB_ERROR;
11322 piocb->context2 = prspiocbq;
dea3101e
JB
11323 }
11324
5a0916b4 11325 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11326 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11327 piocb->context_un.wait_queue = &done_q;
5a0916b4 11328 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11329
875fbdfe 11330 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11331 if (lpfc_readl(phba->HCregaddr, &creg_val))
11332 return IOCB_ERROR;
875fbdfe
JSEC
11333 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11334 writel(creg_val, phba->HCregaddr);
11335 readl(phba->HCregaddr); /* flush */
11336 }
11337
2a9bf3d0
JS
11338 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11339 SLI_IOCB_RET_IOCB);
68876920 11340 if (retval == IOCB_SUCCESS) {
256ec0d0 11341 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11342 timeleft = wait_event_timeout(done_q,
d11e31dd 11343 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11344 timeout_req);
5a0916b4
JS
11345 spin_lock_irqsave(&phba->hbalock, iflags);
11346 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11347
11348 /*
11349 * IOCB timed out. Inform the wake iocb wait
11350 * completion function and set local status
11351 */
dea3101e 11352
5a0916b4
JS
11353 iocb_completed = false;
11354 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11355 }
11356 spin_unlock_irqrestore(&phba->hbalock, iflags);
11357 if (iocb_completed) {
7054a606 11358 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11359 "0331 IOCB wake signaled\n");
53151bbb
JS
11360 /* Note: we are not indicating if the IOCB has a success
11361 * status or not - that's for the caller to check.
11362 * IOCB_SUCCESS means just that the command was sent and
11363 * completed. Not that it completed successfully.
11364 * */
7054a606 11365 } else if (timeleft == 0) {
68876920 11366 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11367 "0338 IOCB wait timeout error - no "
11368 "wake response Data x%x\n", timeout);
68876920 11369 retval = IOCB_TIMEDOUT;
7054a606 11370 } else {
68876920 11371 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11372 "0330 IOCB wake NOT set, "
11373 "Data x%x x%lx\n",
68876920
JSEC
11374 timeout, (timeleft / jiffies));
11375 retval = IOCB_TIMEDOUT;
dea3101e 11376 }
2a9bf3d0 11377 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11378 if (phba->cfg_log_verbose & LOG_SLI) {
11379 list_for_each_entry(iocb, &pring->txq, list) {
11380 txq_cnt++;
11381 }
11382 list_for_each_entry(iocb, &pring->txcmplq, list) {
11383 txcmplq_cnt++;
11384 }
11385 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11386 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11387 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11388 }
2a9bf3d0 11389 return retval;
68876920
JSEC
11390 } else {
11391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11392 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11393 retval);
68876920 11394 retval = IOCB_ERROR;
dea3101e
JB
11395 }
11396
875fbdfe 11397 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11398 if (lpfc_readl(phba->HCregaddr, &creg_val))
11399 return IOCB_ERROR;
875fbdfe
JSEC
11400 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11401 writel(creg_val, phba->HCregaddr);
11402 readl(phba->HCregaddr); /* flush */
11403 }
11404
68876920
JSEC
11405 if (prspiocbq)
11406 piocb->context2 = NULL;
11407
11408 piocb->context_un.wait_queue = NULL;
11409 piocb->iocb_cmpl = NULL;
dea3101e
JB
11410 return retval;
11411}
68876920 11412
e59058c4 11413/**
3621a710 11414 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11415 * @phba: Pointer to HBA context object.
11416 * @pmboxq: Pointer to driver mailbox object.
11417 * @timeout: Timeout in number of seconds.
11418 *
11419 * This function issues the mailbox to firmware and waits for the
11420 * mailbox command to complete. If the mailbox command is not
11421 * completed within timeout seconds, it returns MBX_TIMEOUT.
11422 * The function waits for the mailbox completion using an
11423 * interruptible wait. If the thread is woken up due to a
11424 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11425 * should not free the mailbox resources, if this function returns
11426 * MBX_TIMEOUT.
11427 * This function will sleep while waiting for mailbox completion.
11428 * So, this function should not be called from any context which
11429 * does not allow sleeping. Due to the same reason, this function
11430 * cannot be called with interrupt disabled.
11431 * This function assumes that the mailbox completion occurs while
11432 * this function sleep. So, this function cannot be called from
11433 * the worker thread which processes mailbox completion.
11434 * This function is called in the context of HBA management
11435 * applications.
11436 * This function returns MBX_SUCCESS when successful.
11437 * This function is called with no lock held.
11438 **/
dea3101e 11439int
2e0fef85 11440lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
11441 uint32_t timeout)
11442{
7259f0d0 11443 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 11444 MAILBOX_t *mb = NULL;
dea3101e 11445 int retval;
858c9f6c 11446 unsigned long flag;
dea3101e 11447
b230b8a2 11448 /* The caller might set context1 for extended buffer */
98c9ea5c 11449 if (pmboxq->context1)
b230b8a2 11450 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 11451
495a714c 11452 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11453 /* setup wake call as IOCB callback */
11454 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11455 /* setup context field to pass wait_queue pointer to wake function */
11456 pmboxq->context1 = &done_q;
11457
dea3101e
JB
11458 /* now issue the command */
11459 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11460 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
11461 wait_event_interruptible_timeout(done_q,
11462 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 11463 msecs_to_jiffies(timeout * 1000));
7054a606 11464
858c9f6c 11465 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
11466 /* restore the possible extended buffer for free resource */
11467 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
11468 /*
11469 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11470 * else do not free the resources.
11471 */
d7c47992 11472 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11473 retval = MBX_SUCCESS;
d7c47992 11474 } else {
7054a606 11475 retval = MBX_TIMEOUT;
858c9f6c
JS
11476 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11477 }
11478 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
11479 } else {
11480 /* restore the possible extended buffer for free resource */
11481 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
11482 }
11483
dea3101e
JB
11484 return retval;
11485}
11486
e59058c4 11487/**
3772a991 11488 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11489 * @phba: Pointer to HBA context.
11490 *
3772a991
JS
11491 * This function is called to shutdown the driver's mailbox sub-system.
11492 * It first marks the mailbox sub-system is in a block state to prevent
11493 * the asynchronous mailbox command from issued off the pending mailbox
11494 * command queue. If the mailbox command sub-system shutdown is due to
11495 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11496 * the mailbox sub-system flush routine to forcefully bring down the
11497 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11498 * as with offline or HBA function reset), this routine will wait for the
11499 * outstanding mailbox command to complete before invoking the mailbox
11500 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11501 **/
3772a991 11502void
618a5230 11503lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11504{
3772a991 11505 struct lpfc_sli *psli = &phba->sli;
3772a991 11506 unsigned long timeout;
b4c02652 11507
618a5230
JS
11508 if (mbx_action == LPFC_MBX_NO_WAIT) {
11509 /* delay 100ms for port state */
11510 msleep(100);
11511 lpfc_sli_mbox_sys_flush(phba);
11512 return;
11513 }
a183a15f 11514 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11515
3772a991
JS
11516 spin_lock_irq(&phba->hbalock);
11517 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11518
3772a991 11519 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11520 /* Determine how long we might wait for the active mailbox
11521 * command to be gracefully completed by firmware.
11522 */
a183a15f
JS
11523 if (phba->sli.mbox_active)
11524 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11525 phba->sli.mbox_active) *
11526 1000) + jiffies;
11527 spin_unlock_irq(&phba->hbalock);
11528
3772a991
JS
11529 while (phba->sli.mbox_active) {
11530 /* Check active mailbox complete status every 2ms */
11531 msleep(2);
11532 if (time_after(jiffies, timeout))
11533 /* Timeout, let the mailbox flush routine to
11534 * forcefully release active mailbox command
11535 */
11536 break;
11537 }
d7069f09
JS
11538 } else
11539 spin_unlock_irq(&phba->hbalock);
11540
3772a991
JS
11541 lpfc_sli_mbox_sys_flush(phba);
11542}
ed957684 11543
3772a991
JS
11544/**
11545 * lpfc_sli_eratt_read - read sli-3 error attention events
11546 * @phba: Pointer to HBA context.
11547 *
11548 * This function is called to read the SLI3 device error attention registers
11549 * for possible error attention events. The caller must hold the hostlock
11550 * with spin_lock_irq().
11551 *
25985edc 11552 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11553 * Register and returns 0 otherwise.
11554 **/
11555static int
11556lpfc_sli_eratt_read(struct lpfc_hba *phba)
11557{
11558 uint32_t ha_copy;
b4c02652 11559
3772a991 11560 /* Read chip Host Attention (HA) register */
9940b97b
JS
11561 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11562 goto unplug_err;
11563
3772a991
JS
11564 if (ha_copy & HA_ERATT) {
11565 /* Read host status register to retrieve error event */
9940b97b
JS
11566 if (lpfc_sli_read_hs(phba))
11567 goto unplug_err;
b4c02652 11568
3772a991
JS
11569 /* Check if there is a deferred error condition is active */
11570 if ((HS_FFER1 & phba->work_hs) &&
11571 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11572 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11573 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11574 /* Clear all interrupt enable conditions */
11575 writel(0, phba->HCregaddr);
11576 readl(phba->HCregaddr);
11577 }
11578
11579 /* Set the driver HA work bitmap */
3772a991
JS
11580 phba->work_ha |= HA_ERATT;
11581 /* Indicate polling handles this ERATT */
11582 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11583 return 1;
11584 }
11585 return 0;
9940b97b
JS
11586
11587unplug_err:
11588 /* Set the driver HS work bitmap */
11589 phba->work_hs |= UNPLUG_ERR;
11590 /* Set the driver HA work bitmap */
11591 phba->work_ha |= HA_ERATT;
11592 /* Indicate polling handles this ERATT */
11593 phba->hba_flag |= HBA_ERATT_HANDLED;
11594 return 1;
b4c02652
JS
11595}
11596
da0436e9
JS
11597/**
11598 * lpfc_sli4_eratt_read - read sli-4 error attention events
11599 * @phba: Pointer to HBA context.
11600 *
11601 * This function is called to read the SLI4 device error attention registers
11602 * for possible error attention events. The caller must hold the hostlock
11603 * with spin_lock_irq().
11604 *
25985edc 11605 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11606 * Register and returns 0 otherwise.
11607 **/
11608static int
11609lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11610{
11611 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11612 uint32_t if_type, portsmphr;
11613 struct lpfc_register portstat_reg;
da0436e9 11614
2fcee4bf
JS
11615 /*
11616 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11617 * registers for error attention. This can be changed later.
11618 */
2fcee4bf
JS
11619 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11620 switch (if_type) {
11621 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11622 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11623 &uerr_sta_lo) ||
11624 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11625 &uerr_sta_hi)) {
11626 phba->work_hs |= UNPLUG_ERR;
11627 phba->work_ha |= HA_ERATT;
11628 phba->hba_flag |= HBA_ERATT_HANDLED;
11629 return 1;
11630 }
2fcee4bf
JS
11631 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11632 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11634 "1423 HBA Unrecoverable error: "
11635 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11636 "ue_mask_lo_reg=0x%x, "
11637 "ue_mask_hi_reg=0x%x\n",
11638 uerr_sta_lo, uerr_sta_hi,
11639 phba->sli4_hba.ue_mask_lo,
11640 phba->sli4_hba.ue_mask_hi);
11641 phba->work_status[0] = uerr_sta_lo;
11642 phba->work_status[1] = uerr_sta_hi;
11643 phba->work_ha |= HA_ERATT;
11644 phba->hba_flag |= HBA_ERATT_HANDLED;
11645 return 1;
11646 }
11647 break;
11648 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
11649 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11650 &portstat_reg.word0) ||
11651 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11652 &portsmphr)){
11653 phba->work_hs |= UNPLUG_ERR;
11654 phba->work_ha |= HA_ERATT;
11655 phba->hba_flag |= HBA_ERATT_HANDLED;
11656 return 1;
11657 }
2fcee4bf
JS
11658 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11659 phba->work_status[0] =
11660 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11661 phba->work_status[1] =
11662 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11664 "2885 Port Status Event: "
2fcee4bf
JS
11665 "port status reg 0x%x, "
11666 "port smphr reg 0x%x, "
11667 "error 1=0x%x, error 2=0x%x\n",
11668 portstat_reg.word0,
11669 portsmphr,
11670 phba->work_status[0],
11671 phba->work_status[1]);
11672 phba->work_ha |= HA_ERATT;
11673 phba->hba_flag |= HBA_ERATT_HANDLED;
11674 return 1;
11675 }
11676 break;
11677 case LPFC_SLI_INTF_IF_TYPE_1:
11678 default:
a747c9ce 11679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11680 "2886 HBA Error Attention on unsupported "
11681 "if type %d.", if_type);
a747c9ce 11682 return 1;
da0436e9 11683 }
2fcee4bf 11684
da0436e9
JS
11685 return 0;
11686}
11687
e59058c4 11688/**
3621a710 11689 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11690 * @phba: Pointer to HBA context.
11691 *
3772a991 11692 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11693 * error attention register bit for error attention events.
11694 *
25985edc 11695 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11696 * Register and returns 0 otherwise.
11697 **/
11698int
11699lpfc_sli_check_eratt(struct lpfc_hba *phba)
11700{
11701 uint32_t ha_copy;
11702
11703 /* If somebody is waiting to handle an eratt, don't process it
11704 * here. The brdkill function will do this.
11705 */
11706 if (phba->link_flag & LS_IGNORE_ERATT)
11707 return 0;
11708
11709 /* Check if interrupt handler handles this ERATT */
11710 spin_lock_irq(&phba->hbalock);
11711 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11712 /* Interrupt handler has handled ERATT */
11713 spin_unlock_irq(&phba->hbalock);
11714 return 0;
11715 }
11716
a257bf90
JS
11717 /*
11718 * If there is deferred error attention, do not check for error
11719 * attention
11720 */
11721 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11722 spin_unlock_irq(&phba->hbalock);
11723 return 0;
11724 }
11725
3772a991
JS
11726 /* If PCI channel is offline, don't process it */
11727 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11728 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11729 return 0;
11730 }
11731
11732 switch (phba->sli_rev) {
11733 case LPFC_SLI_REV2:
11734 case LPFC_SLI_REV3:
11735 /* Read chip Host Attention (HA) register */
11736 ha_copy = lpfc_sli_eratt_read(phba);
11737 break;
da0436e9 11738 case LPFC_SLI_REV4:
2fcee4bf 11739 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11740 ha_copy = lpfc_sli4_eratt_read(phba);
11741 break;
3772a991
JS
11742 default:
11743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11744 "0299 Invalid SLI revision (%d)\n",
11745 phba->sli_rev);
11746 ha_copy = 0;
11747 break;
9399627f
JS
11748 }
11749 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11750
11751 return ha_copy;
11752}
11753
11754/**
11755 * lpfc_intr_state_check - Check device state for interrupt handling
11756 * @phba: Pointer to HBA context.
11757 *
11758 * This inline routine checks whether a device or its PCI slot is in a state
11759 * that the interrupt should be handled.
11760 *
11761 * This function returns 0 if the device or the PCI slot is in a state that
11762 * interrupt should be handled, otherwise -EIO.
11763 */
11764static inline int
11765lpfc_intr_state_check(struct lpfc_hba *phba)
11766{
11767 /* If the pci channel is offline, ignore all the interrupts */
11768 if (unlikely(pci_channel_offline(phba->pcidev)))
11769 return -EIO;
11770
11771 /* Update device level interrupt statistics */
11772 phba->sli.slistat.sli_intr++;
11773
11774 /* Ignore all interrupts during initialization. */
11775 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11776 return -EIO;
11777
9399627f
JS
11778 return 0;
11779}
11780
11781/**
3772a991 11782 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11783 * @irq: Interrupt number.
11784 * @dev_id: The device context pointer.
11785 *
9399627f 11786 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11787 * service routine when device with SLI-3 interface spec is enabled with
11788 * MSI-X multi-message interrupt mode and there are slow-path events in
11789 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11790 * interrupt mode, this function is called as part of the device-level
11791 * interrupt handler. When the PCI slot is in error recovery or the HBA
11792 * is undergoing initialization, the interrupt handler will not process
11793 * the interrupt. The link attention and ELS ring attention events are
11794 * handled by the worker thread. The interrupt handler signals the worker
11795 * thread and returns for these events. This function is called without
11796 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
11797 * structures.
11798 *
11799 * This function returns IRQ_HANDLED when interrupt is handled else it
11800 * returns IRQ_NONE.
e59058c4 11801 **/
dea3101e 11802irqreturn_t
3772a991 11803lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 11804{
2e0fef85 11805 struct lpfc_hba *phba;
a747c9ce 11806 uint32_t ha_copy, hc_copy;
dea3101e
JB
11807 uint32_t work_ha_copy;
11808 unsigned long status;
5b75da2f 11809 unsigned long iflag;
dea3101e
JB
11810 uint32_t control;
11811
92d7f7b0 11812 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
11813 struct lpfc_vport *vport;
11814 struct lpfc_nodelist *ndlp;
11815 struct lpfc_dmabuf *mp;
92d7f7b0
JS
11816 LPFC_MBOXQ_t *pmb;
11817 int rc;
11818
dea3101e
JB
11819 /*
11820 * Get the driver's phba structure from the dev_id and
11821 * assume the HBA is not interrupting.
11822 */
9399627f 11823 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
11824
11825 if (unlikely(!phba))
11826 return IRQ_NONE;
11827
dea3101e 11828 /*
9399627f
JS
11829 * Stuff needs to be attented to when this function is invoked as an
11830 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 11831 */
9399627f 11832 if (phba->intr_type == MSIX) {
3772a991
JS
11833 /* Check device state for handling interrupt */
11834 if (lpfc_intr_state_check(phba))
9399627f
JS
11835 return IRQ_NONE;
11836 /* Need to read HA REG for slow-path events */
5b75da2f 11837 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11838 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11839 goto unplug_error;
9399627f
JS
11840 /* If somebody is waiting to handle an eratt don't process it
11841 * here. The brdkill function will do this.
11842 */
11843 if (phba->link_flag & LS_IGNORE_ERATT)
11844 ha_copy &= ~HA_ERATT;
11845 /* Check the need for handling ERATT in interrupt handler */
11846 if (ha_copy & HA_ERATT) {
11847 if (phba->hba_flag & HBA_ERATT_HANDLED)
11848 /* ERATT polling has handled ERATT */
11849 ha_copy &= ~HA_ERATT;
11850 else
11851 /* Indicate interrupt handler handles ERATT */
11852 phba->hba_flag |= HBA_ERATT_HANDLED;
11853 }
a257bf90
JS
11854
11855 /*
11856 * If there is deferred error attention, do not check for any
11857 * interrupt.
11858 */
11859 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 11860 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
11861 return IRQ_NONE;
11862 }
11863
9399627f 11864 /* Clear up only attention source related to slow-path */
9940b97b
JS
11865 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11866 goto unplug_error;
11867
a747c9ce
JS
11868 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11869 HC_LAINT_ENA | HC_ERINT_ENA),
11870 phba->HCregaddr);
9399627f
JS
11871 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11872 phba->HAregaddr);
a747c9ce 11873 writel(hc_copy, phba->HCregaddr);
9399627f 11874 readl(phba->HAregaddr); /* flush */
5b75da2f 11875 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
11876 } else
11877 ha_copy = phba->ha_copy;
dea3101e 11878
dea3101e
JB
11879 work_ha_copy = ha_copy & phba->work_ha_mask;
11880
9399627f 11881 if (work_ha_copy) {
dea3101e
JB
11882 if (work_ha_copy & HA_LATT) {
11883 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11884 /*
11885 * Turn off Link Attention interrupts
11886 * until CLEAR_LA done
11887 */
5b75da2f 11888 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11889 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
11890 if (lpfc_readl(phba->HCregaddr, &control))
11891 goto unplug_error;
dea3101e
JB
11892 control &= ~HC_LAINT_ENA;
11893 writel(control, phba->HCregaddr);
11894 readl(phba->HCregaddr); /* flush */
5b75da2f 11895 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11896 }
11897 else
11898 work_ha_copy &= ~HA_LATT;
11899 }
11900
9399627f 11901 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
11902 /*
11903 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11904 * the only slow ring.
11905 */
11906 status = (work_ha_copy &
11907 (HA_RXMASK << (4*LPFC_ELS_RING)));
11908 status >>= (4*LPFC_ELS_RING);
11909 if (status & HA_RXMASK) {
5b75da2f 11910 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11911 if (lpfc_readl(phba->HCregaddr, &control))
11912 goto unplug_error;
a58cbd52
JS
11913
11914 lpfc_debugfs_slow_ring_trc(phba,
11915 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11916 control, status,
11917 (uint32_t)phba->sli.slistat.sli_intr);
11918
858c9f6c 11919 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
11920 lpfc_debugfs_slow_ring_trc(phba,
11921 "ISR Disable ring:"
11922 "pwork:x%x hawork:x%x wait:x%x",
11923 phba->work_ha, work_ha_copy,
11924 (uint32_t)((unsigned long)
5e9d9b82 11925 &phba->work_waitq));
a58cbd52 11926
858c9f6c
JS
11927 control &=
11928 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
11929 writel(control, phba->HCregaddr);
11930 readl(phba->HCregaddr); /* flush */
dea3101e 11931 }
a58cbd52
JS
11932 else {
11933 lpfc_debugfs_slow_ring_trc(phba,
11934 "ISR slow ring: pwork:"
11935 "x%x hawork:x%x wait:x%x",
11936 phba->work_ha, work_ha_copy,
11937 (uint32_t)((unsigned long)
5e9d9b82 11938 &phba->work_waitq));
a58cbd52 11939 }
5b75da2f 11940 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11941 }
11942 }
5b75da2f 11943 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 11944 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
11945 if (lpfc_sli_read_hs(phba))
11946 goto unplug_error;
a257bf90
JS
11947 /*
11948 * Check if there is a deferred error condition
11949 * is active
11950 */
11951 if ((HS_FFER1 & phba->work_hs) &&
11952 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
11953 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11954 phba->work_hs)) {
a257bf90
JS
11955 phba->hba_flag |= DEFER_ERATT;
11956 /* Clear all interrupt enable conditions */
11957 writel(0, phba->HCregaddr);
11958 readl(phba->HCregaddr);
11959 }
11960 }
11961
9399627f 11962 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 11963 pmb = phba->sli.mbox_active;
04c68496 11964 pmbox = &pmb->u.mb;
34b02dcd 11965 mbox = phba->mbox;
858c9f6c 11966 vport = pmb->vport;
92d7f7b0
JS
11967
11968 /* First check out the status word */
11969 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11970 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 11971 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
11972 /*
11973 * Stray Mailbox Interrupt, mbxCommand <cmd>
11974 * mbxStatus <status>
11975 */
09372820 11976 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 11977 LOG_SLI,
e8b62011 11978 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
11979 "Interrupt mbxCommand x%x "
11980 "mbxStatus x%x\n",
e8b62011 11981 (vport ? vport->vpi : 0),
92d7f7b0
JS
11982 pmbox->mbxCommand,
11983 pmbox->mbxStatus);
09372820
JS
11984 /* clear mailbox attention bit */
11985 work_ha_copy &= ~HA_MBATT;
11986 } else {
97eab634 11987 phba->sli.mbox_active = NULL;
5b75da2f 11988 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
11989 phba->last_completion_time = jiffies;
11990 del_timer(&phba->sli.mbox_tmo);
09372820
JS
11991 if (pmb->mbox_cmpl) {
11992 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11993 MAILBOX_CMD_SIZE);
7a470277
JS
11994 if (pmb->out_ext_byte_len &&
11995 pmb->context2)
11996 lpfc_sli_pcimem_bcopy(
11997 phba->mbox_ext,
11998 pmb->context2,
11999 pmb->out_ext_byte_len);
09372820
JS
12000 }
12001 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12002 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12003
12004 lpfc_debugfs_disc_trc(vport,
12005 LPFC_DISC_TRC_MBOX_VPORT,
12006 "MBOX dflt rpi: : "
12007 "status:x%x rpi:x%x",
12008 (uint32_t)pmbox->mbxStatus,
12009 pmbox->un.varWords[0], 0);
12010
12011 if (!pmbox->mbxStatus) {
12012 mp = (struct lpfc_dmabuf *)
12013 (pmb->context1);
12014 ndlp = (struct lpfc_nodelist *)
12015 pmb->context2;
12016
12017 /* Reg_LOGIN of dflt RPI was
12018 * successful. new lets get
12019 * rid of the RPI using the
12020 * same mbox buffer.
12021 */
12022 lpfc_unreg_login(phba,
12023 vport->vpi,
12024 pmbox->un.varWords[0],
12025 pmb);
12026 pmb->mbox_cmpl =
12027 lpfc_mbx_cmpl_dflt_rpi;
12028 pmb->context1 = mp;
12029 pmb->context2 = ndlp;
12030 pmb->vport = vport;
58da1ffb
JS
12031 rc = lpfc_sli_issue_mbox(phba,
12032 pmb,
12033 MBX_NOWAIT);
12034 if (rc != MBX_BUSY)
12035 lpfc_printf_log(phba,
12036 KERN_ERR,
12037 LOG_MBOX | LOG_SLI,
d7c255b2 12038 "0350 rc should have"
6a9c52cf 12039 "been MBX_BUSY\n");
3772a991
JS
12040 if (rc != MBX_NOT_FINISHED)
12041 goto send_current_mbox;
09372820 12042 }
858c9f6c 12043 }
5b75da2f
JS
12044 spin_lock_irqsave(
12045 &phba->pport->work_port_lock,
12046 iflag);
09372820
JS
12047 phba->pport->work_port_events &=
12048 ~WORKER_MBOX_TMO;
5b75da2f
JS
12049 spin_unlock_irqrestore(
12050 &phba->pport->work_port_lock,
12051 iflag);
09372820 12052 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 12053 }
97eab634 12054 } else
5b75da2f 12055 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 12056
92d7f7b0
JS
12057 if ((work_ha_copy & HA_MBATT) &&
12058 (phba->sli.mbox_active == NULL)) {
858c9f6c 12059send_current_mbox:
92d7f7b0 12060 /* Process next mailbox command if there is one */
58da1ffb
JS
12061 do {
12062 rc = lpfc_sli_issue_mbox(phba, NULL,
12063 MBX_NOWAIT);
12064 } while (rc == MBX_NOT_FINISHED);
12065 if (rc != MBX_SUCCESS)
12066 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12067 LOG_SLI, "0349 rc should be "
6a9c52cf 12068 "MBX_SUCCESS\n");
92d7f7b0
JS
12069 }
12070
5b75da2f 12071 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 12072 phba->work_ha |= work_ha_copy;
5b75da2f 12073 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 12074 lpfc_worker_wake_up(phba);
dea3101e 12075 }
9399627f 12076 return IRQ_HANDLED;
9940b97b
JS
12077unplug_error:
12078 spin_unlock_irqrestore(&phba->hbalock, iflag);
12079 return IRQ_HANDLED;
dea3101e 12080
3772a991 12081} /* lpfc_sli_sp_intr_handler */
9399627f
JS
12082
12083/**
3772a991 12084 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
12085 * @irq: Interrupt number.
12086 * @dev_id: The device context pointer.
12087 *
12088 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
12089 * service routine when device with SLI-3 interface spec is enabled with
12090 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12091 * ring event in the HBA. However, when the device is enabled with either
12092 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12093 * device-level interrupt handler. When the PCI slot is in error recovery
12094 * or the HBA is undergoing initialization, the interrupt handler will not
12095 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12096 * the intrrupt context. This function is called without any lock held.
12097 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
12098 *
12099 * This function returns IRQ_HANDLED when interrupt is handled else it
12100 * returns IRQ_NONE.
12101 **/
12102irqreturn_t
3772a991 12103lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
12104{
12105 struct lpfc_hba *phba;
12106 uint32_t ha_copy;
12107 unsigned long status;
5b75da2f 12108 unsigned long iflag;
895427bd 12109 struct lpfc_sli_ring *pring;
9399627f
JS
12110
12111 /* Get the driver's phba structure from the dev_id and
12112 * assume the HBA is not interrupting.
12113 */
12114 phba = (struct lpfc_hba *) dev_id;
12115
12116 if (unlikely(!phba))
12117 return IRQ_NONE;
12118
12119 /*
12120 * Stuff needs to be attented to when this function is invoked as an
12121 * individual interrupt handler in MSI-X multi-message interrupt mode
12122 */
12123 if (phba->intr_type == MSIX) {
3772a991
JS
12124 /* Check device state for handling interrupt */
12125 if (lpfc_intr_state_check(phba))
9399627f
JS
12126 return IRQ_NONE;
12127 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12128 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12129 return IRQ_HANDLED;
9399627f 12130 /* Clear up only attention source related to fast-path */
5b75da2f 12131 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12132 /*
12133 * If there is deferred error attention, do not check for
12134 * any interrupt.
12135 */
12136 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12137 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12138 return IRQ_NONE;
12139 }
9399627f
JS
12140 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12141 phba->HAregaddr);
12142 readl(phba->HAregaddr); /* flush */
5b75da2f 12143 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12144 } else
12145 ha_copy = phba->ha_copy;
dea3101e
JB
12146
12147 /*
9399627f 12148 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12149 */
9399627f
JS
12150 ha_copy &= ~(phba->work_ha_mask);
12151
12152 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12153 status >>= (4*LPFC_FCP_RING);
895427bd 12154 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12155 if (status & HA_RXMASK)
895427bd 12156 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12157
12158 if (phba->cfg_multi_ring_support == 2) {
12159 /*
9399627f
JS
12160 * Process all events on extra ring. Take the optimized path
12161 * for extra ring IO.
a4bc3379 12162 */
9399627f 12163 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12164 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12165 if (status & HA_RXMASK) {
a4bc3379 12166 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12167 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12168 status);
12169 }
12170 }
dea3101e 12171 return IRQ_HANDLED;
3772a991 12172} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12173
12174/**
3772a991 12175 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12176 * @irq: Interrupt number.
12177 * @dev_id: The device context pointer.
12178 *
3772a991
JS
12179 * This function is the HBA device-level interrupt handler to device with
12180 * SLI-3 interface spec, called from the PCI layer when either MSI or
12181 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12182 * requires driver attention. This function invokes the slow-path interrupt
12183 * attention handling function and fast-path interrupt attention handling
12184 * function in turn to process the relevant HBA attention events. This
12185 * function is called without any lock held. It gets the hbalock to access
12186 * and update SLI data structures.
9399627f
JS
12187 *
12188 * This function returns IRQ_HANDLED when interrupt is handled, else it
12189 * returns IRQ_NONE.
12190 **/
12191irqreturn_t
3772a991 12192lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12193{
12194 struct lpfc_hba *phba;
12195 irqreturn_t sp_irq_rc, fp_irq_rc;
12196 unsigned long status1, status2;
a747c9ce 12197 uint32_t hc_copy;
9399627f
JS
12198
12199 /*
12200 * Get the driver's phba structure from the dev_id and
12201 * assume the HBA is not interrupting.
12202 */
12203 phba = (struct lpfc_hba *) dev_id;
12204
12205 if (unlikely(!phba))
12206 return IRQ_NONE;
12207
3772a991
JS
12208 /* Check device state for handling interrupt */
12209 if (lpfc_intr_state_check(phba))
9399627f
JS
12210 return IRQ_NONE;
12211
12212 spin_lock(&phba->hbalock);
9940b97b
JS
12213 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12214 spin_unlock(&phba->hbalock);
12215 return IRQ_HANDLED;
12216 }
12217
9399627f
JS
12218 if (unlikely(!phba->ha_copy)) {
12219 spin_unlock(&phba->hbalock);
12220 return IRQ_NONE;
12221 } else if (phba->ha_copy & HA_ERATT) {
12222 if (phba->hba_flag & HBA_ERATT_HANDLED)
12223 /* ERATT polling has handled ERATT */
12224 phba->ha_copy &= ~HA_ERATT;
12225 else
12226 /* Indicate interrupt handler handles ERATT */
12227 phba->hba_flag |= HBA_ERATT_HANDLED;
12228 }
12229
a257bf90
JS
12230 /*
12231 * If there is deferred error attention, do not check for any interrupt.
12232 */
12233 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12234 spin_unlock(&phba->hbalock);
a257bf90
JS
12235 return IRQ_NONE;
12236 }
12237
9399627f 12238 /* Clear attention sources except link and error attentions */
9940b97b
JS
12239 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12240 spin_unlock(&phba->hbalock);
12241 return IRQ_HANDLED;
12242 }
a747c9ce
JS
12243 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12244 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12245 phba->HCregaddr);
9399627f 12246 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12247 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12248 readl(phba->HAregaddr); /* flush */
12249 spin_unlock(&phba->hbalock);
12250
12251 /*
12252 * Invokes slow-path host attention interrupt handling as appropriate.
12253 */
12254
12255 /* status of events with mailbox and link attention */
12256 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12257
12258 /* status of events with ELS ring */
12259 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12260 status2 >>= (4*LPFC_ELS_RING);
12261
12262 if (status1 || (status2 & HA_RXMASK))
3772a991 12263 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12264 else
12265 sp_irq_rc = IRQ_NONE;
12266
12267 /*
12268 * Invoke fast-path host attention interrupt handling as appropriate.
12269 */
12270
12271 /* status of events with FCP ring */
12272 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12273 status1 >>= (4*LPFC_FCP_RING);
12274
12275 /* status of events with extra ring */
12276 if (phba->cfg_multi_ring_support == 2) {
12277 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12278 status2 >>= (4*LPFC_EXTRA_RING);
12279 } else
12280 status2 = 0;
12281
12282 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12283 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12284 else
12285 fp_irq_rc = IRQ_NONE;
dea3101e 12286
9399627f
JS
12287 /* Return device-level interrupt handling status */
12288 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12289} /* lpfc_sli_intr_handler */
4f774513
JS
12290
12291/**
12292 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12293 * @phba: pointer to lpfc hba data structure.
12294 *
12295 * This routine is invoked by the worker thread to process all the pending
12296 * SLI4 FCP abort XRI events.
12297 **/
12298void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12299{
12300 struct lpfc_cq_event *cq_event;
12301
12302 /* First, declare the fcp xri abort event has been handled */
12303 spin_lock_irq(&phba->hbalock);
12304 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12305 spin_unlock_irq(&phba->hbalock);
12306 /* Now, handle all the fcp xri abort events */
12307 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12308 /* Get the first event from the head of the event queue */
12309 spin_lock_irq(&phba->hbalock);
12310 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12311 cq_event, struct lpfc_cq_event, list);
12312 spin_unlock_irq(&phba->hbalock);
12313 /* Notify aborted XRI for FCP work queue */
12314 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12315 /* Free the event processed back to the free pool */
12316 lpfc_sli4_cq_event_release(phba, cq_event);
12317 }
12318}
12319
12320/**
12321 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12322 * @phba: pointer to lpfc hba data structure.
12323 *
12324 * This routine is invoked by the worker thread to process all the pending
12325 * SLI4 els abort xri events.
12326 **/
12327void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12328{
12329 struct lpfc_cq_event *cq_event;
12330
12331 /* First, declare the els xri abort event has been handled */
12332 spin_lock_irq(&phba->hbalock);
12333 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12334 spin_unlock_irq(&phba->hbalock);
12335 /* Now, handle all the els xri abort events */
12336 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12337 /* Get the first event from the head of the event queue */
12338 spin_lock_irq(&phba->hbalock);
12339 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12340 cq_event, struct lpfc_cq_event, list);
12341 spin_unlock_irq(&phba->hbalock);
12342 /* Notify aborted XRI for ELS work queue */
12343 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12344 /* Free the event processed back to the free pool */
12345 lpfc_sli4_cq_event_release(phba, cq_event);
12346 }
12347}
12348
341af102
JS
12349/**
12350 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12351 * @phba: pointer to lpfc hba data structure
12352 * @pIocbIn: pointer to the rspiocbq
12353 * @pIocbOut: pointer to the cmdiocbq
12354 * @wcqe: pointer to the complete wcqe
12355 *
12356 * This routine transfers the fields of a command iocbq to a response iocbq
12357 * by copying all the IOCB fields from command iocbq and transferring the
12358 * completion status information from the complete wcqe.
12359 **/
4f774513 12360static void
341af102
JS
12361lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12362 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12363 struct lpfc_iocbq *pIocbOut,
12364 struct lpfc_wcqe_complete *wcqe)
12365{
af22741c 12366 int numBdes, i;
341af102 12367 unsigned long iflags;
af22741c
JS
12368 uint32_t status, max_response;
12369 struct lpfc_dmabuf *dmabuf;
12370 struct ulp_bde64 *bpl, bde;
4f774513
JS
12371 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12372
12373 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12374 sizeof(struct lpfc_iocbq) - offset);
4f774513 12375 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12376 status = bf_get(lpfc_wcqe_c_status, wcqe);
12377 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12378 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12379 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12380 pIocbIn->iocb.un.fcpi.fcpi_parm =
12381 pIocbOut->iocb.un.fcpi.fcpi_parm -
12382 wcqe->total_data_placed;
12383 else
12384 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12385 else {
4f774513 12386 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12387 switch (pIocbOut->iocb.ulpCommand) {
12388 case CMD_ELS_REQUEST64_CR:
12389 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12390 bpl = (struct ulp_bde64 *)dmabuf->virt;
12391 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12392 max_response = bde.tus.f.bdeSize;
12393 break;
12394 case CMD_GEN_REQUEST64_CR:
12395 max_response = 0;
12396 if (!pIocbOut->context3)
12397 break;
12398 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12399 sizeof(struct ulp_bde64);
12400 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12401 bpl = (struct ulp_bde64 *)dmabuf->virt;
12402 for (i = 0; i < numBdes; i++) {
12403 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12404 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12405 max_response += bde.tus.f.bdeSize;
12406 }
12407 break;
12408 default:
12409 max_response = wcqe->total_data_placed;
12410 break;
12411 }
12412 if (max_response < wcqe->total_data_placed)
12413 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12414 else
12415 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12416 wcqe->total_data_placed;
695a814e 12417 }
341af102 12418
acd6859b
JS
12419 /* Convert BG errors for completion status */
12420 if (status == CQE_STATUS_DI_ERROR) {
12421 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12422
12423 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12424 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12425 else
12426 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12427
12428 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12429 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12430 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12431 BGS_GUARD_ERR_MASK;
12432 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12433 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12434 BGS_APPTAG_ERR_MASK;
12435 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12436 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12437 BGS_REFTAG_ERR_MASK;
12438
12439 /* Check to see if there was any good data before the error */
12440 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12441 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12442 BGS_HI_WATER_MARK_PRESENT_MASK;
12443 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12444 wcqe->total_data_placed;
12445 }
12446
12447 /*
12448 * Set ALL the error bits to indicate we don't know what
12449 * type of error it is.
12450 */
12451 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12452 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12453 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12454 BGS_GUARD_ERR_MASK);
12455 }
12456
341af102
JS
12457 /* Pick up HBA exchange busy condition */
12458 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12459 spin_lock_irqsave(&phba->hbalock, iflags);
12460 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12461 spin_unlock_irqrestore(&phba->hbalock, iflags);
12462 }
4f774513
JS
12463}
12464
45ed1190
JS
12465/**
12466 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12467 * @phba: Pointer to HBA context object.
12468 * @wcqe: Pointer to work-queue completion queue entry.
12469 *
12470 * This routine handles an ELS work-queue completion event and construct
12471 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12472 * discovery engine to handle.
12473 *
12474 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12475 **/
12476static struct lpfc_iocbq *
12477lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12478 struct lpfc_iocbq *irspiocbq)
12479{
895427bd 12480 struct lpfc_sli_ring *pring;
45ed1190
JS
12481 struct lpfc_iocbq *cmdiocbq;
12482 struct lpfc_wcqe_complete *wcqe;
12483 unsigned long iflags;
12484
895427bd 12485 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
12486 if (unlikely(!pring))
12487 return NULL;
895427bd 12488
45ed1190 12489 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12490 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12491 pring->stats.iocb_event++;
12492 /* Look up the ELS command IOCB and create pseudo response IOCB */
12493 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12494 bf_get(lpfc_wcqe_c_request_tag, wcqe));
45ed1190 12495 if (unlikely(!cmdiocbq)) {
401bb416 12496 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12497 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12498 "0386 ELS complete with no corresponding "
401bb416
DK
12499 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12500 wcqe->word0, wcqe->total_data_placed,
12501 wcqe->parameter, wcqe->word3);
45ed1190
JS
12502 lpfc_sli_release_iocbq(phba, irspiocbq);
12503 return NULL;
12504 }
12505
401bb416
DK
12506 /* Put the iocb back on the txcmplq */
12507 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12508 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12509
45ed1190 12510 /* Fake the irspiocbq and copy necessary response information */
341af102 12511 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12512
12513 return irspiocbq;
12514}
12515
8a5ca109
JS
12516inline struct lpfc_cq_event *
12517lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12518{
12519 struct lpfc_cq_event *cq_event;
12520
12521 /* Allocate a new internal CQ_EVENT entry */
12522 cq_event = lpfc_sli4_cq_event_alloc(phba);
12523 if (!cq_event) {
12524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12525 "0602 Failed to alloc CQ_EVENT entry\n");
12526 return NULL;
12527 }
12528
12529 /* Move the CQE into the event */
12530 memcpy(&cq_event->cqe, entry, size);
12531 return cq_event;
12532}
12533
04c68496
JS
12534/**
12535 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12536 * @phba: Pointer to HBA context object.
12537 * @cqe: Pointer to mailbox completion queue entry.
12538 *
12539 * This routine process a mailbox completion queue entry with asynchrous
12540 * event.
12541 *
12542 * Return: true if work posted to worker thread, otherwise false.
12543 **/
12544static bool
12545lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12546{
12547 struct lpfc_cq_event *cq_event;
12548 unsigned long iflags;
12549
12550 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12551 "0392 Async Event: word0:x%x, word1:x%x, "
12552 "word2:x%x, word3:x%x\n", mcqe->word0,
12553 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12554
8a5ca109
JS
12555 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12556 if (!cq_event)
04c68496 12557 return false;
04c68496
JS
12558 spin_lock_irqsave(&phba->hbalock, iflags);
12559 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12560 /* Set the async event flag */
12561 phba->hba_flag |= ASYNC_EVENT;
12562 spin_unlock_irqrestore(&phba->hbalock, iflags);
12563
12564 return true;
12565}
12566
12567/**
12568 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12569 * @phba: Pointer to HBA context object.
12570 * @cqe: Pointer to mailbox completion queue entry.
12571 *
12572 * This routine process a mailbox completion queue entry with mailbox
12573 * completion event.
12574 *
12575 * Return: true if work posted to worker thread, otherwise false.
12576 **/
12577static bool
12578lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12579{
12580 uint32_t mcqe_status;
12581 MAILBOX_t *mbox, *pmbox;
12582 struct lpfc_mqe *mqe;
12583 struct lpfc_vport *vport;
12584 struct lpfc_nodelist *ndlp;
12585 struct lpfc_dmabuf *mp;
12586 unsigned long iflags;
12587 LPFC_MBOXQ_t *pmb;
12588 bool workposted = false;
12589 int rc;
12590
12591 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12592 if (!bf_get(lpfc_trailer_completed, mcqe))
12593 goto out_no_mqe_complete;
12594
12595 /* Get the reference to the active mbox command */
12596 spin_lock_irqsave(&phba->hbalock, iflags);
12597 pmb = phba->sli.mbox_active;
12598 if (unlikely(!pmb)) {
12599 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12600 "1832 No pending MBOX command to handle\n");
12601 spin_unlock_irqrestore(&phba->hbalock, iflags);
12602 goto out_no_mqe_complete;
12603 }
12604 spin_unlock_irqrestore(&phba->hbalock, iflags);
12605 mqe = &pmb->u.mqe;
12606 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12607 mbox = phba->mbox;
12608 vport = pmb->vport;
12609
12610 /* Reset heartbeat timer */
12611 phba->last_completion_time = jiffies;
12612 del_timer(&phba->sli.mbox_tmo);
12613
12614 /* Move mbox data to caller's mailbox region, do endian swapping */
12615 if (pmb->mbox_cmpl && mbox)
12616 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12617
73d91e50
JS
12618 /*
12619 * For mcqe errors, conditionally move a modified error code to
12620 * the mbox so that the error will not be missed.
12621 */
12622 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12623 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12624 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12625 bf_set(lpfc_mqe_status, mqe,
12626 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12627 }
04c68496
JS
12628 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12629 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12630 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12631 "MBOX dflt rpi: status:x%x rpi:x%x",
12632 mcqe_status,
12633 pmbox->un.varWords[0], 0);
12634 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12635 mp = (struct lpfc_dmabuf *)(pmb->context1);
12636 ndlp = (struct lpfc_nodelist *)pmb->context2;
12637 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12638 * RID of the PPI using the same mbox buffer.
12639 */
12640 lpfc_unreg_login(phba, vport->vpi,
12641 pmbox->un.varWords[0], pmb);
12642 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12643 pmb->context1 = mp;
12644 pmb->context2 = ndlp;
12645 pmb->vport = vport;
12646 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12647 if (rc != MBX_BUSY)
12648 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12649 LOG_SLI, "0385 rc should "
12650 "have been MBX_BUSY\n");
12651 if (rc != MBX_NOT_FINISHED)
12652 goto send_current_mbox;
12653 }
12654 }
12655 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12656 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12657 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12658
12659 /* There is mailbox completion work to do */
12660 spin_lock_irqsave(&phba->hbalock, iflags);
12661 __lpfc_mbox_cmpl_put(phba, pmb);
12662 phba->work_ha |= HA_MBATT;
12663 spin_unlock_irqrestore(&phba->hbalock, iflags);
12664 workposted = true;
12665
12666send_current_mbox:
12667 spin_lock_irqsave(&phba->hbalock, iflags);
12668 /* Release the mailbox command posting token */
12669 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12670 /* Setting active mailbox pointer need to be in sync to flag clear */
12671 phba->sli.mbox_active = NULL;
12672 spin_unlock_irqrestore(&phba->hbalock, iflags);
12673 /* Wake up worker thread to post the next pending mailbox command */
12674 lpfc_worker_wake_up(phba);
12675out_no_mqe_complete:
12676 if (bf_get(lpfc_trailer_consumed, mcqe))
12677 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12678 return workposted;
12679}
12680
12681/**
12682 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12683 * @phba: Pointer to HBA context object.
12684 * @cqe: Pointer to mailbox completion queue entry.
12685 *
12686 * This routine process a mailbox completion queue entry, it invokes the
12687 * proper mailbox complete handling or asynchrous event handling routine
12688 * according to the MCQE's async bit.
12689 *
12690 * Return: true if work posted to worker thread, otherwise false.
12691 **/
12692static bool
12693lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12694{
12695 struct lpfc_mcqe mcqe;
12696 bool workposted;
12697
12698 /* Copy the mailbox MCQE and convert endian order as needed */
12699 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12700
12701 /* Invoke the proper event handling routine */
12702 if (!bf_get(lpfc_trailer_async, &mcqe))
12703 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12704 else
12705 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12706 return workposted;
12707}
12708
4f774513
JS
12709/**
12710 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12711 * @phba: Pointer to HBA context object.
2a76a283 12712 * @cq: Pointer to associated CQ
4f774513
JS
12713 * @wcqe: Pointer to work-queue completion queue entry.
12714 *
12715 * This routine handles an ELS work-queue completion event.
12716 *
12717 * Return: true if work posted to worker thread, otherwise false.
12718 **/
12719static bool
2a76a283 12720lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12721 struct lpfc_wcqe_complete *wcqe)
12722{
4f774513
JS
12723 struct lpfc_iocbq *irspiocbq;
12724 unsigned long iflags;
2a76a283 12725 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12726 int txq_cnt = 0;
12727 int txcmplq_cnt = 0;
12728 int fcp_txcmplq_cnt = 0;
4f774513 12729
45ed1190 12730 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12731 irspiocbq = lpfc_sli_get_iocbq(phba);
12732 if (!irspiocbq) {
0e9bb8d7
JS
12733 if (!list_empty(&pring->txq))
12734 txq_cnt++;
12735 if (!list_empty(&pring->txcmplq))
12736 txcmplq_cnt++;
4f774513 12737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12738 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12739 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12740 txq_cnt, phba->iocb_cnt,
12741 fcp_txcmplq_cnt,
12742 txcmplq_cnt);
45ed1190 12743 return false;
4f774513 12744 }
4f774513 12745
45ed1190
JS
12746 /* Save off the slow-path queue event for work thread to process */
12747 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12748 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12749 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12750 &phba->sli4_hba.sp_queue_event);
12751 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12752 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12753
45ed1190 12754 return true;
4f774513
JS
12755}
12756
12757/**
12758 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12759 * @phba: Pointer to HBA context object.
12760 * @wcqe: Pointer to work-queue completion queue entry.
12761 *
3f8b6fb7 12762 * This routine handles slow-path WQ entry consumed event by invoking the
4f774513
JS
12763 * proper WQ release routine to the slow-path WQ.
12764 **/
12765static void
12766lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12767 struct lpfc_wcqe_release *wcqe)
12768{
2e90f4b5
JS
12769 /* sanity check on queue memory */
12770 if (unlikely(!phba->sli4_hba.els_wq))
12771 return;
4f774513
JS
12772 /* Check for the slow-path ELS work queue */
12773 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12774 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12775 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12776 else
12777 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12778 "2579 Slow-path wqe consume event carries "
12779 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12780 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12781 phba->sli4_hba.els_wq->queue_id);
12782}
12783
12784/**
12785 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12786 * @phba: Pointer to HBA context object.
12787 * @cq: Pointer to a WQ completion queue.
12788 * @wcqe: Pointer to work-queue completion queue entry.
12789 *
12790 * This routine handles an XRI abort event.
12791 *
12792 * Return: true if work posted to worker thread, otherwise false.
12793 **/
12794static bool
12795lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12796 struct lpfc_queue *cq,
12797 struct sli4_wcqe_xri_aborted *wcqe)
12798{
12799 bool workposted = false;
12800 struct lpfc_cq_event *cq_event;
12801 unsigned long iflags;
12802
4f774513
JS
12803 switch (cq->subtype) {
12804 case LPFC_FCP:
8a5ca109
JS
12805 cq_event = lpfc_cq_event_setup(
12806 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12807 if (!cq_event)
12808 return false;
4f774513
JS
12809 spin_lock_irqsave(&phba->hbalock, iflags);
12810 list_add_tail(&cq_event->list,
12811 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12812 /* Set the fcp xri abort event flag */
12813 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12814 spin_unlock_irqrestore(&phba->hbalock, iflags);
12815 workposted = true;
12816 break;
422c4cb7 12817 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
4f774513 12818 case LPFC_ELS:
8a5ca109
JS
12819 cq_event = lpfc_cq_event_setup(
12820 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12821 if (!cq_event)
12822 return false;
4f774513
JS
12823 spin_lock_irqsave(&phba->hbalock, iflags);
12824 list_add_tail(&cq_event->list,
12825 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12826 /* Set the els xri abort event flag */
12827 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12828 spin_unlock_irqrestore(&phba->hbalock, iflags);
12829 workposted = true;
12830 break;
318083ad 12831 case LPFC_NVME:
8a5ca109
JS
12832 /* Notify aborted XRI for NVME work queue */
12833 if (phba->nvmet_support)
12834 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12835 else
12836 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12837
12838 workposted = false;
318083ad 12839 break;
4f774513
JS
12840 default:
12841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
318083ad
JS
12842 "0603 Invalid CQ subtype %d: "
12843 "%08x %08x %08x %08x\n",
12844 cq->subtype, wcqe->word0, wcqe->parameter,
12845 wcqe->word2, wcqe->word3);
4f774513
JS
12846 workposted = false;
12847 break;
12848 }
12849 return workposted;
12850}
12851
4f774513
JS
12852/**
12853 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12854 * @phba: Pointer to HBA context object.
12855 * @rcqe: Pointer to receive-queue completion queue entry.
12856 *
12857 * This routine process a receive-queue completion queue entry.
12858 *
12859 * Return: true if work posted to worker thread, otherwise false.
12860 **/
12861static bool
4d9ab994 12862lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 12863{
4f774513 12864 bool workposted = false;
895427bd 12865 struct fc_frame_header *fc_hdr;
4f774513
JS
12866 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12867 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
547077a4 12868 struct lpfc_nvmet_tgtport *tgtp;
4f774513 12869 struct hbq_dmabuf *dma_buf;
7851fe2c 12870 uint32_t status, rq_id;
4f774513
JS
12871 unsigned long iflags;
12872
2e90f4b5
JS
12873 /* sanity check on queue memory */
12874 if (unlikely(!hrq) || unlikely(!drq))
12875 return workposted;
12876
7851fe2c
JS
12877 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12878 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12879 else
12880 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12881 if (rq_id != hrq->queue_id)
4f774513
JS
12882 goto out;
12883
4d9ab994 12884 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
12885 switch (status) {
12886 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12887 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12888 "2537 Receive Frame Truncated!!\n");
12889 case FC_STATUS_RQ_SUCCESS:
5ffc266e 12890 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
12891 spin_lock_irqsave(&phba->hbalock, iflags);
12892 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12893 if (!dma_buf) {
b84daac9 12894 hrq->RQ_no_buf_found++;
4f774513
JS
12895 spin_unlock_irqrestore(&phba->hbalock, iflags);
12896 goto out;
12897 }
b84daac9 12898 hrq->RQ_rcv_buf++;
547077a4 12899 hrq->RQ_buf_posted--;
4d9ab994 12900 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
12901
12902 /* If a NVME LS event (type 0x28), treat it as Fast path */
12903 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12904
4f774513 12905 /* save off the frame for the word thread to process */
4d9ab994 12906 list_add_tail(&dma_buf->cq_event.list,
45ed1190 12907 &phba->sli4_hba.sp_queue_event);
4f774513 12908 /* Frame received */
45ed1190 12909 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
12910 spin_unlock_irqrestore(&phba->hbalock, iflags);
12911 workposted = true;
12912 break;
4f774513 12913 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
12914 if (phba->nvmet_support) {
12915 tgtp = phba->targetport->private;
12916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12917 "6402 RQE Error x%x, posted %d err_cnt "
12918 "%d: %x %x %x\n",
12919 status, hrq->RQ_buf_posted,
12920 hrq->RQ_no_posted_buf,
12921 atomic_read(&tgtp->rcv_fcp_cmd_in),
12922 atomic_read(&tgtp->rcv_fcp_cmd_out),
12923 atomic_read(&tgtp->xmt_fcp_release));
12924 }
12925 /* fallthrough */
12926
12927 case FC_STATUS_INSUFF_BUF_NEED_BUF:
b84daac9 12928 hrq->RQ_no_posted_buf++;
4f774513
JS
12929 /* Post more buffers if possible */
12930 spin_lock_irqsave(&phba->hbalock, iflags);
12931 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12932 spin_unlock_irqrestore(&phba->hbalock, iflags);
12933 workposted = true;
12934 break;
12935 }
12936out:
12937 return workposted;
4f774513
JS
12938}
12939
4d9ab994
JS
12940/**
12941 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12942 * @phba: Pointer to HBA context object.
12943 * @cq: Pointer to the completion queue.
12944 * @wcqe: Pointer to a completion queue entry.
12945 *
25985edc 12946 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
12947 * entry.
12948 *
12949 * Return: true if work posted to worker thread, otherwise false.
12950 **/
12951static bool
12952lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12953 struct lpfc_cqe *cqe)
12954{
45ed1190 12955 struct lpfc_cqe cqevt;
4d9ab994
JS
12956 bool workposted = false;
12957
12958 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 12959 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
12960
12961 /* Check and process for different type of WCQE and dispatch */
45ed1190 12962 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 12963 case CQE_CODE_COMPL_WQE:
45ed1190 12964 /* Process the WQ/RQ complete event */
bc73905a 12965 phba->last_completion_time = jiffies;
2a76a283 12966 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 12967 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
12968 break;
12969 case CQE_CODE_RELEASE_WQE:
12970 /* Process the WQ release event */
12971 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 12972 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
12973 break;
12974 case CQE_CODE_XRI_ABORTED:
12975 /* Process the WQ XRI abort event */
bc73905a 12976 phba->last_completion_time = jiffies;
4d9ab994 12977 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 12978 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
12979 break;
12980 case CQE_CODE_RECEIVE:
7851fe2c 12981 case CQE_CODE_RECEIVE_V1:
4d9ab994 12982 /* Process the RQ event */
bc73905a 12983 phba->last_completion_time = jiffies;
4d9ab994 12984 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 12985 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
12986 break;
12987 default:
12988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12989 "0388 Not a valid WCQE code: x%x\n",
45ed1190 12990 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
12991 break;
12992 }
12993 return workposted;
12994}
12995
4f774513
JS
12996/**
12997 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12998 * @phba: Pointer to HBA context object.
12999 * @eqe: Pointer to fast-path event queue entry.
13000 *
13001 * This routine process a event queue entry from the slow-path event queue.
13002 * It will check the MajorCode and MinorCode to determine this is for a
13003 * completion event on a completion queue, if not, an error shall be logged
13004 * and just return. Otherwise, it will get to the corresponding completion
13005 * queue and process all the entries on that completion queue, rearm the
13006 * completion queue, and then return.
13007 *
13008 **/
f485c18d 13009static void
67d12733
JS
13010lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13011 struct lpfc_queue *speq)
4f774513 13012{
67d12733 13013 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
13014 uint16_t cqid;
13015
4f774513 13016 /* Get the reference to the corresponding CQ */
cb5172ea 13017 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 13018
4f774513
JS
13019 list_for_each_entry(childq, &speq->child_list, list) {
13020 if (childq->queue_id == cqid) {
13021 cq = childq;
13022 break;
13023 }
13024 }
13025 if (unlikely(!cq)) {
75baf696
JS
13026 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13028 "0365 Slow-path CQ identifier "
13029 "(%d) does not exist\n", cqid);
f485c18d 13030 return;
4f774513
JS
13031 }
13032
895427bd
JS
13033 /* Save EQ associated with this CQ */
13034 cq->assoc_qp = speq;
13035
f485c18d
DK
13036 if (!queue_work(phba->wq, &cq->spwork))
13037 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13038 "0390 Cannot schedule soft IRQ "
13039 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13040 cqid, cq->queue_id, smp_processor_id());
13041}
13042
13043/**
13044 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13045 * @phba: Pointer to HBA context object.
13046 *
13047 * This routine process a event queue entry from the slow-path event queue.
13048 * It will check the MajorCode and MinorCode to determine this is for a
13049 * completion event on a completion queue, if not, an error shall be logged
13050 * and just return. Otherwise, it will get to the corresponding completion
13051 * queue and process all the entries on that completion queue, rearm the
13052 * completion queue, and then return.
13053 *
13054 **/
13055static void
13056lpfc_sli4_sp_process_cq(struct work_struct *work)
13057{
13058 struct lpfc_queue *cq =
13059 container_of(work, struct lpfc_queue, spwork);
13060 struct lpfc_hba *phba = cq->phba;
13061 struct lpfc_cqe *cqe;
13062 bool workposted = false;
13063 int ccount = 0;
13064
4f774513
JS
13065 /* Process all the entries to the CQ */
13066 switch (cq->type) {
13067 case LPFC_MCQ:
13068 while ((cqe = lpfc_sli4_cq_get(cq))) {
13069 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
f485c18d 13070 if (!(++ccount % cq->entry_repost))
7869da18 13071 break;
b84daac9 13072 cq->CQ_mbox++;
4f774513
JS
13073 }
13074 break;
13075 case LPFC_WCQ:
13076 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13077 if (cq->subtype == LPFC_FCP ||
13078 cq->subtype == LPFC_NVME) {
13079#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13080 if (phba->ktime_on)
13081 cq->isr_timestamp = ktime_get_ns();
13082 else
13083 cq->isr_timestamp = 0;
13084#endif
895427bd 13085 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c 13086 cqe);
c8a4ce0b 13087 } else {
0558056c
JS
13088 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13089 cqe);
c8a4ce0b 13090 }
f485c18d 13091 if (!(++ccount % cq->entry_repost))
7869da18 13092 break;
4f774513 13093 }
b84daac9
JS
13094
13095 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13096 if (ccount > cq->CQ_max_cqe)
13097 cq->CQ_max_cqe = ccount;
4f774513
JS
13098 break;
13099 default:
13100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13101 "0370 Invalid completion queue type (%d)\n",
13102 cq->type);
f485c18d 13103 return;
4f774513
JS
13104 }
13105
13106 /* Catch the no cq entry condition, log an error */
f485c18d 13107 if (unlikely(ccount == 0))
4f774513
JS
13108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13109 "0371 No entry from the CQ: identifier "
13110 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13111
13112 /* In any case, flash and re-arm the RCQ */
13113 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13114
13115 /* wake up worker thread if there are works to be done */
13116 if (workposted)
13117 lpfc_worker_wake_up(phba);
13118}
13119
13120/**
13121 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
13122 * @phba: Pointer to HBA context object.
13123 * @cq: Pointer to associated CQ
13124 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
13125 *
13126 * This routine process a fast-path work queue completion entry from fast-path
13127 * event queue for FCP command response completion.
13128 **/
13129static void
2a76a283 13130lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13131 struct lpfc_wcqe_complete *wcqe)
13132{
2a76a283 13133 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
13134 struct lpfc_iocbq *cmdiocbq;
13135 struct lpfc_iocbq irspiocbq;
13136 unsigned long iflags;
13137
4f774513
JS
13138 /* Check for response status */
13139 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13140 /* If resource errors reported from HBA, reduce queue
13141 * depth of the SCSI device.
13142 */
e3d2b802
JS
13143 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13144 IOSTAT_LOCAL_REJECT)) &&
13145 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13146 IOERR_NO_RESOURCES))
4f774513 13147 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 13148
4f774513
JS
13149 /* Log the error status */
13150 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13151 "0373 FCP complete error: status=x%x, "
13152 "hw_status=x%x, total_data_specified=%d, "
13153 "parameter=x%x, word3=x%x\n",
13154 bf_get(lpfc_wcqe_c_status, wcqe),
13155 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13156 wcqe->total_data_placed, wcqe->parameter,
13157 wcqe->word3);
13158 }
13159
13160 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
13161 spin_lock_irqsave(&pring->ring_lock, iflags);
13162 pring->stats.iocb_event++;
4f774513
JS
13163 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13164 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 13165 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
13166 if (unlikely(!cmdiocbq)) {
13167 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13168 "0374 FCP complete with no corresponding "
13169 "cmdiocb: iotag (%d)\n",
13170 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13171 return;
13172 }
c8a4ce0b
DK
13173#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13174 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13175#endif
895427bd
JS
13176 if (cmdiocbq->iocb_cmpl == NULL) {
13177 if (cmdiocbq->wqe_cmpl) {
13178 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13179 spin_lock_irqsave(&phba->hbalock, iflags);
13180 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13181 spin_unlock_irqrestore(&phba->hbalock, iflags);
13182 }
13183
13184 /* Pass the cmd_iocb and the wcqe to the upper layer */
13185 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13186 return;
13187 }
4f774513
JS
13188 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13189 "0375 FCP cmdiocb not callback function "
13190 "iotag: (%d)\n",
13191 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13192 return;
13193 }
13194
13195 /* Fake the irspiocb and copy necessary response information */
341af102 13196 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13197
0f65ff68
JS
13198 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13199 spin_lock_irqsave(&phba->hbalock, iflags);
13200 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13201 spin_unlock_irqrestore(&phba->hbalock, iflags);
13202 }
13203
4f774513
JS
13204 /* Pass the cmd_iocb and the rsp state to the upper layer */
13205 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13206}
13207
13208/**
13209 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13210 * @phba: Pointer to HBA context object.
13211 * @cq: Pointer to completion queue.
13212 * @wcqe: Pointer to work-queue completion queue entry.
13213 *
3f8b6fb7 13214 * This routine handles an fast-path WQ entry consumed event by invoking the
4f774513
JS
13215 * proper WQ release routine to the slow-path WQ.
13216 **/
13217static void
13218lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13219 struct lpfc_wcqe_release *wcqe)
13220{
13221 struct lpfc_queue *childwq;
13222 bool wqid_matched = false;
895427bd 13223 uint16_t hba_wqid;
4f774513
JS
13224
13225 /* Check for fast-path FCP work queue release */
895427bd 13226 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13227 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13228 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13229 lpfc_sli4_wq_release(childwq,
13230 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13231 wqid_matched = true;
13232 break;
13233 }
13234 }
13235 /* Report warning log message if no match found */
13236 if (wqid_matched != true)
13237 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13238 "2580 Fast-path wqe consume event carries "
895427bd 13239 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13240}
13241
13242/**
2d7dbc4c
JS
13243 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13244 * @phba: Pointer to HBA context object.
13245 * @rcqe: Pointer to receive-queue completion queue entry.
4f774513 13246 *
2d7dbc4c
JS
13247 * This routine process a receive-queue completion queue entry.
13248 *
13249 * Return: true if work posted to worker thread, otherwise false.
13250 **/
13251static bool
13252lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13253 struct lpfc_rcqe *rcqe)
13254{
13255 bool workposted = false;
13256 struct lpfc_queue *hrq;
13257 struct lpfc_queue *drq;
13258 struct rqb_dmabuf *dma_buf;
13259 struct fc_frame_header *fc_hdr;
547077a4 13260 struct lpfc_nvmet_tgtport *tgtp;
2d7dbc4c
JS
13261 uint32_t status, rq_id;
13262 unsigned long iflags;
13263 uint32_t fctl, idx;
13264
13265 if ((phba->nvmet_support == 0) ||
13266 (phba->sli4_hba.nvmet_cqset == NULL))
13267 return workposted;
13268
13269 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13270 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13271 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13272
13273 /* sanity check on queue memory */
13274 if (unlikely(!hrq) || unlikely(!drq))
13275 return workposted;
13276
13277 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13278 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13279 else
13280 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13281
13282 if ((phba->nvmet_support == 0) ||
13283 (rq_id != hrq->queue_id))
13284 return workposted;
13285
13286 status = bf_get(lpfc_rcqe_status, rcqe);
13287 switch (status) {
13288 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13290 "6126 Receive Frame Truncated!!\n");
78e1d200 13291 /* Drop thru */
2d7dbc4c
JS
13292 case FC_STATUS_RQ_SUCCESS:
13293 lpfc_sli4_rq_release(hrq, drq);
13294 spin_lock_irqsave(&phba->hbalock, iflags);
13295 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13296 if (!dma_buf) {
13297 hrq->RQ_no_buf_found++;
13298 spin_unlock_irqrestore(&phba->hbalock, iflags);
13299 goto out;
13300 }
13301 spin_unlock_irqrestore(&phba->hbalock, iflags);
13302 hrq->RQ_rcv_buf++;
547077a4 13303 hrq->RQ_buf_posted--;
2d7dbc4c
JS
13304 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13305
13306 /* Just some basic sanity checks on FCP Command frame */
13307 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13308 fc_hdr->fh_f_ctl[1] << 8 |
13309 fc_hdr->fh_f_ctl[2]);
13310 if (((fctl &
13311 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13312 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13313 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13314 goto drop;
13315
13316 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13317 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7 13318 lpfc_nvmet_unsol_fcp_event(
66d7ce93 13319 phba, idx, dma_buf,
c8a4ce0b 13320 cq->isr_timestamp);
2d7dbc4c
JS
13321 return false;
13322 }
13323drop:
13324 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13325 break;
2d7dbc4c 13326 case FC_STATUS_INSUFF_BUF_FRM_DISC:
547077a4
JS
13327 if (phba->nvmet_support) {
13328 tgtp = phba->targetport->private;
13329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13330 "6401 RQE Error x%x, posted %d err_cnt "
13331 "%d: %x %x %x\n",
13332 status, hrq->RQ_buf_posted,
13333 hrq->RQ_no_posted_buf,
13334 atomic_read(&tgtp->rcv_fcp_cmd_in),
13335 atomic_read(&tgtp->rcv_fcp_cmd_out),
13336 atomic_read(&tgtp->xmt_fcp_release));
13337 }
13338 /* fallthrough */
13339
13340 case FC_STATUS_INSUFF_BUF_NEED_BUF:
2d7dbc4c
JS
13341 hrq->RQ_no_posted_buf++;
13342 /* Post more buffers if possible */
2d7dbc4c
JS
13343 break;
13344 }
13345out:
13346 return workposted;
13347}
13348
4f774513 13349/**
895427bd 13350 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13351 * @cq: Pointer to the completion queue.
13352 * @eqe: Pointer to fast-path completion queue entry.
13353 *
13354 * This routine process a fast-path work queue completion entry from fast-path
13355 * event queue for FCP command response completion.
13356 **/
13357static int
895427bd 13358lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13359 struct lpfc_cqe *cqe)
13360{
13361 struct lpfc_wcqe_release wcqe;
13362 bool workposted = false;
13363
13364 /* Copy the work queue CQE and convert endian order if needed */
13365 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13366
13367 /* Check and process for different type of WCQE and dispatch */
13368 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13369 case CQE_CODE_COMPL_WQE:
895427bd 13370 case CQE_CODE_NVME_ERSP:
b84daac9 13371 cq->CQ_wq++;
4f774513 13372 /* Process the WQ complete event */
98fc5dd9 13373 phba->last_completion_time = jiffies;
895427bd
JS
13374 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13375 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13376 (struct lpfc_wcqe_complete *)&wcqe);
13377 if (cq->subtype == LPFC_NVME_LS)
13378 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13379 (struct lpfc_wcqe_complete *)&wcqe);
13380 break;
13381 case CQE_CODE_RELEASE_WQE:
b84daac9 13382 cq->CQ_release_wqe++;
4f774513
JS
13383 /* Process the WQ release event */
13384 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13385 (struct lpfc_wcqe_release *)&wcqe);
13386 break;
13387 case CQE_CODE_XRI_ABORTED:
b84daac9 13388 cq->CQ_xri_aborted++;
4f774513 13389 /* Process the WQ XRI abort event */
bc73905a 13390 phba->last_completion_time = jiffies;
4f774513
JS
13391 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13392 (struct sli4_wcqe_xri_aborted *)&wcqe);
13393 break;
895427bd
JS
13394 case CQE_CODE_RECEIVE_V1:
13395 case CQE_CODE_RECEIVE:
13396 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13397 if (cq->subtype == LPFC_NVMET) {
13398 workposted = lpfc_sli4_nvmet_handle_rcqe(
13399 phba, cq, (struct lpfc_rcqe *)&wcqe);
13400 }
895427bd 13401 break;
4f774513
JS
13402 default:
13403 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13404 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13405 bf_get(lpfc_wcqe_c_code, &wcqe));
13406 break;
13407 }
13408 return workposted;
13409}
13410
13411/**
67d12733 13412 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13413 * @phba: Pointer to HBA context object.
13414 * @eqe: Pointer to fast-path event queue entry.
13415 *
13416 * This routine process a event queue entry from the fast-path event queue.
13417 * It will check the MajorCode and MinorCode to determine this is for a
13418 * completion event on a completion queue, if not, an error shall be logged
13419 * and just return. Otherwise, it will get to the corresponding completion
13420 * queue and process all the entries on the completion queue, rearm the
13421 * completion queue, and then return.
13422 **/
f485c18d 13423static void
67d12733
JS
13424lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13425 uint32_t qidx)
4f774513 13426{
895427bd 13427 struct lpfc_queue *cq = NULL;
2d7dbc4c 13428 uint16_t cqid, id;
4f774513 13429
cb5172ea 13430 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13431 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13432 "0366 Not a valid completion "
4f774513 13433 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13434 bf_get_le32(lpfc_eqe_major_code, eqe),
13435 bf_get_le32(lpfc_eqe_minor_code, eqe));
f485c18d 13436 return;
4f774513
JS
13437 }
13438
67d12733
JS
13439 /* Get the reference to the corresponding CQ */
13440 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13441
2d7dbc4c
JS
13442 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13443 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13444 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13445 /* Process NVMET unsol rcv */
13446 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13447 goto process_cq;
13448 }
67d12733
JS
13449 }
13450
895427bd
JS
13451 if (phba->sli4_hba.nvme_cq_map &&
13452 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13453 /* Process NVME / NVMET command completion */
895427bd
JS
13454 cq = phba->sli4_hba.nvme_cq[qidx];
13455 goto process_cq;
2e90f4b5 13456 }
67d12733 13457
895427bd
JS
13458 if (phba->sli4_hba.fcp_cq_map &&
13459 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13460 /* Process FCP command completion */
13461 cq = phba->sli4_hba.fcp_cq[qidx];
13462 goto process_cq;
2e90f4b5 13463 }
895427bd
JS
13464
13465 if (phba->sli4_hba.nvmels_cq &&
13466 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13467 /* Process NVME unsol rcv */
13468 cq = phba->sli4_hba.nvmels_cq;
13469 }
13470
13471 /* Otherwise this is a Slow path event */
13472 if (cq == NULL) {
f485c18d
DK
13473 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13474 return;
4f774513
JS
13475 }
13476
895427bd 13477process_cq:
4f774513
JS
13478 if (unlikely(cqid != cq->queue_id)) {
13479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13480 "0368 Miss-matched fast-path completion "
13481 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13482 cqid, cq->queue_id);
f485c18d 13483 return;
4f774513
JS
13484 }
13485
895427bd
JS
13486 /* Save EQ associated with this CQ */
13487 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13488
f485c18d
DK
13489 if (!queue_work(phba->wq, &cq->irqwork))
13490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13491 "0363 Cannot schedule soft IRQ "
13492 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13493 cqid, cq->queue_id, smp_processor_id());
13494}
13495
13496/**
13497 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13498 * @phba: Pointer to HBA context object.
13499 * @eqe: Pointer to fast-path event queue entry.
13500 *
13501 * This routine process a event queue entry from the fast-path event queue.
13502 * It will check the MajorCode and MinorCode to determine this is for a
13503 * completion event on a completion queue, if not, an error shall be logged
13504 * and just return. Otherwise, it will get to the corresponding completion
13505 * queue and process all the entries on the completion queue, rearm the
13506 * completion queue, and then return.
13507 **/
13508static void
13509lpfc_sli4_hba_process_cq(struct work_struct *work)
13510{
13511 struct lpfc_queue *cq =
13512 container_of(work, struct lpfc_queue, irqwork);
13513 struct lpfc_hba *phba = cq->phba;
13514 struct lpfc_cqe *cqe;
13515 bool workposted = false;
13516 int ccount = 0;
13517
4f774513
JS
13518 /* Process all the entries to the CQ */
13519 while ((cqe = lpfc_sli4_cq_get(cq))) {
c8a4ce0b
DK
13520#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13521 if (phba->ktime_on)
13522 cq->isr_timestamp = ktime_get_ns();
13523 else
13524 cq->isr_timestamp = 0;
13525#endif
895427bd 13526 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
f485c18d 13527 if (!(++ccount % cq->entry_repost))
7869da18 13528 break;
4f774513
JS
13529 }
13530
b84daac9 13531 /* Track the max number of CQEs processed in 1 EQ */
f485c18d
DK
13532 if (ccount > cq->CQ_max_cqe)
13533 cq->CQ_max_cqe = ccount;
13534 cq->assoc_qp->EQ_cqe_cnt += ccount;
b84daac9 13535
4f774513 13536 /* Catch the no cq entry condition */
f485c18d 13537 if (unlikely(ccount == 0))
4f774513
JS
13538 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13539 "0369 No entry from fast-path completion "
13540 "queue fcpcqid=%d\n", cq->queue_id);
13541
13542 /* In any case, flash and re-arm the CQ */
13543 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13544
13545 /* wake up worker thread if there are works to be done */
13546 if (workposted)
13547 lpfc_worker_wake_up(phba);
13548}
13549
13550static void
13551lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13552{
13553 struct lpfc_eqe *eqe;
13554
13555 /* walk all the EQ entries and drop on the floor */
13556 while ((eqe = lpfc_sli4_eq_get(eq)))
13557 ;
13558
13559 /* Clear and re-arm the EQ */
13560 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13561}
13562
1ba981fd
JS
13563
13564/**
13565 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13566 * entry
13567 * @phba: Pointer to HBA context object.
13568 * @eqe: Pointer to fast-path event queue entry.
13569 *
13570 * This routine process a event queue entry from the Flash Optimized Fabric
13571 * event queue. It will check the MajorCode and MinorCode to determine this
13572 * is for a completion event on a completion queue, if not, an error shall be
13573 * logged and just return. Otherwise, it will get to the corresponding
13574 * completion queue and process all the entries on the completion queue, rearm
13575 * the completion queue, and then return.
13576 **/
13577static void
13578lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13579{
13580 struct lpfc_queue *cq;
1ba981fd 13581 uint16_t cqid;
1ba981fd
JS
13582
13583 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13585 "9147 Not a valid completion "
13586 "event: majorcode=x%x, minorcode=x%x\n",
13587 bf_get_le32(lpfc_eqe_major_code, eqe),
13588 bf_get_le32(lpfc_eqe_minor_code, eqe));
13589 return;
13590 }
13591
13592 /* Get the reference to the corresponding CQ */
13593 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13594
13595 /* Next check for OAS */
13596 cq = phba->sli4_hba.oas_cq;
13597 if (unlikely(!cq)) {
13598 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13600 "9148 OAS completion queue "
13601 "does not exist\n");
13602 return;
13603 }
13604
13605 if (unlikely(cqid != cq->queue_id)) {
13606 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13607 "9149 Miss-matched fast-path compl "
13608 "queue id: eqcqid=%d, fcpcqid=%d\n",
13609 cqid, cq->queue_id);
13610 return;
13611 }
13612
d41b65bc
JS
13613 /* Save EQ associated with this CQ */
13614 cq->assoc_qp = phba->sli4_hba.fof_eq;
13615
f485c18d
DK
13616 /* CQ work will be processed on CPU affinitized to this IRQ */
13617 if (!queue_work(phba->wq, &cq->irqwork))
1ba981fd 13618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
f485c18d
DK
13619 "0367 Cannot schedule soft IRQ "
13620 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13621 cqid, cq->queue_id, smp_processor_id());
1ba981fd
JS
13622}
13623
13624/**
13625 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13626 * @irq: Interrupt number.
13627 * @dev_id: The device context pointer.
13628 *
13629 * This function is directly called from the PCI layer as an interrupt
13630 * service routine when device with SLI-4 interface spec is enabled with
13631 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13632 * IOCB ring event in the HBA. However, when the device is enabled with either
13633 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13634 * device-level interrupt handler. When the PCI slot is in error recovery
13635 * or the HBA is undergoing initialization, the interrupt handler will not
13636 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13637 * the intrrupt context. This function is called without any lock held.
13638 * It gets the hbalock to access and update SLI data structures. Note that,
13639 * the EQ to CQ are one-to-one map such that the EQ index is
13640 * equal to that of CQ index.
13641 *
13642 * This function returns IRQ_HANDLED when interrupt is handled else it
13643 * returns IRQ_NONE.
13644 **/
13645irqreturn_t
13646lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13647{
13648 struct lpfc_hba *phba;
895427bd 13649 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13650 struct lpfc_queue *eq;
13651 struct lpfc_eqe *eqe;
13652 unsigned long iflag;
13653 int ecount = 0;
1ba981fd
JS
13654
13655 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13656 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13657 phba = hba_eq_hdl->phba;
1ba981fd
JS
13658
13659 if (unlikely(!phba))
13660 return IRQ_NONE;
13661
13662 /* Get to the EQ struct associated with this vector */
13663 eq = phba->sli4_hba.fof_eq;
13664 if (unlikely(!eq))
13665 return IRQ_NONE;
13666
13667 /* Check device state for handling interrupt */
13668 if (unlikely(lpfc_intr_state_check(phba))) {
1ba981fd
JS
13669 /* Check again for link_state with lock held */
13670 spin_lock_irqsave(&phba->hbalock, iflag);
13671 if (phba->link_state < LPFC_LINK_DOWN)
13672 /* Flush, clear interrupt, and rearm the EQ */
13673 lpfc_sli4_eq_flush(phba, eq);
13674 spin_unlock_irqrestore(&phba->hbalock, iflag);
13675 return IRQ_NONE;
13676 }
13677
13678 /*
13679 * Process all the event on FCP fast-path EQ
13680 */
13681 while ((eqe = lpfc_sli4_eq_get(eq))) {
13682 lpfc_sli4_fof_handle_eqe(phba, eqe);
13683 if (!(++ecount % eq->entry_repost))
7869da18 13684 break;
1ba981fd
JS
13685 eq->EQ_processed++;
13686 }
13687
13688 /* Track the max number of EQEs processed in 1 intr */
13689 if (ecount > eq->EQ_max_eqe)
13690 eq->EQ_max_eqe = ecount;
13691
13692
13693 if (unlikely(ecount == 0)) {
13694 eq->EQ_no_entry++;
13695
13696 if (phba->intr_type == MSIX)
13697 /* MSI-X treated interrupt served as no EQ share INT */
13698 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13699 "9145 MSI-X interrupt with no EQE\n");
13700 else {
13701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13702 "9146 ISR interrupt with no EQE\n");
13703 /* Non MSI-X treated on interrupt as EQ share INT */
13704 return IRQ_NONE;
13705 }
13706 }
13707 /* Always clear and re-arm the fast-path EQ */
13708 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13709 return IRQ_HANDLED;
13710}
13711
4f774513 13712/**
67d12733 13713 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13714 * @irq: Interrupt number.
13715 * @dev_id: The device context pointer.
13716 *
13717 * This function is directly called from the PCI layer as an interrupt
13718 * service routine when device with SLI-4 interface spec is enabled with
13719 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13720 * ring event in the HBA. However, when the device is enabled with either
13721 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13722 * device-level interrupt handler. When the PCI slot is in error recovery
13723 * or the HBA is undergoing initialization, the interrupt handler will not
13724 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13725 * the intrrupt context. This function is called without any lock held.
13726 * It gets the hbalock to access and update SLI data structures. Note that,
13727 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13728 * equal to that of FCP CQ index.
13729 *
67d12733
JS
13730 * The link attention and ELS ring attention events are handled
13731 * by the worker thread. The interrupt handler signals the worker thread
13732 * and returns for these events. This function is called without any lock
13733 * held. It gets the hbalock to access and update SLI data structures.
13734 *
4f774513
JS
13735 * This function returns IRQ_HANDLED when interrupt is handled else it
13736 * returns IRQ_NONE.
13737 **/
13738irqreturn_t
67d12733 13739lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13740{
13741 struct lpfc_hba *phba;
895427bd 13742 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13743 struct lpfc_queue *fpeq;
13744 struct lpfc_eqe *eqe;
13745 unsigned long iflag;
13746 int ecount = 0;
895427bd 13747 int hba_eqidx;
4f774513
JS
13748
13749 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13750 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13751 phba = hba_eq_hdl->phba;
13752 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13753
13754 if (unlikely(!phba))
13755 return IRQ_NONE;
67d12733 13756 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13757 return IRQ_NONE;
4f774513
JS
13758
13759 /* Get to the EQ struct associated with this vector */
895427bd 13760 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13761 if (unlikely(!fpeq))
13762 return IRQ_NONE;
4f774513 13763
ba20c853 13764 if (lpfc_fcp_look_ahead) {
895427bd 13765 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
ba20c853
JS
13766 lpfc_sli4_eq_clr_intr(fpeq);
13767 else {
895427bd 13768 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13769 return IRQ_NONE;
13770 }
13771 }
13772
4f774513
JS
13773 /* Check device state for handling interrupt */
13774 if (unlikely(lpfc_intr_state_check(phba))) {
13775 /* Check again for link_state with lock held */
13776 spin_lock_irqsave(&phba->hbalock, iflag);
13777 if (phba->link_state < LPFC_LINK_DOWN)
13778 /* Flush, clear interrupt, and rearm the EQ */
13779 lpfc_sli4_eq_flush(phba, fpeq);
13780 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13781 if (lpfc_fcp_look_ahead)
895427bd 13782 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
13783 return IRQ_NONE;
13784 }
13785
13786 /*
13787 * Process all the event on FCP fast-path EQ
13788 */
13789 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
f485c18d
DK
13790 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13791 if (!(++ecount % fpeq->entry_repost))
7869da18 13792 break;
b84daac9 13793 fpeq->EQ_processed++;
4f774513
JS
13794 }
13795
b84daac9
JS
13796 /* Track the max number of EQEs processed in 1 intr */
13797 if (ecount > fpeq->EQ_max_eqe)
13798 fpeq->EQ_max_eqe = ecount;
13799
4f774513
JS
13800 /* Always clear and re-arm the fast-path EQ */
13801 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13802
13803 if (unlikely(ecount == 0)) {
b84daac9 13804 fpeq->EQ_no_entry++;
ba20c853
JS
13805
13806 if (lpfc_fcp_look_ahead) {
895427bd 13807 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13808 return IRQ_NONE;
13809 }
13810
4f774513
JS
13811 if (phba->intr_type == MSIX)
13812 /* MSI-X treated interrupt served as no EQ share INT */
13813 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13814 "0358 MSI-X interrupt with no EQE\n");
13815 else
13816 /* Non MSI-X treated on interrupt as EQ share INT */
13817 return IRQ_NONE;
13818 }
13819
ba20c853 13820 if (lpfc_fcp_look_ahead)
895427bd
JS
13821 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13822
4f774513
JS
13823 return IRQ_HANDLED;
13824} /* lpfc_sli4_fp_intr_handler */
13825
13826/**
13827 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13828 * @irq: Interrupt number.
13829 * @dev_id: The device context pointer.
13830 *
13831 * This function is the device-level interrupt handler to device with SLI-4
13832 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13833 * interrupt mode is enabled and there is an event in the HBA which requires
13834 * driver attention. This function invokes the slow-path interrupt attention
13835 * handling function and fast-path interrupt attention handling function in
13836 * turn to process the relevant HBA attention events. This function is called
13837 * without any lock held. It gets the hbalock to access and update SLI data
13838 * structures.
13839 *
13840 * This function returns IRQ_HANDLED when interrupt is handled, else it
13841 * returns IRQ_NONE.
13842 **/
13843irqreturn_t
13844lpfc_sli4_intr_handler(int irq, void *dev_id)
13845{
13846 struct lpfc_hba *phba;
67d12733
JS
13847 irqreturn_t hba_irq_rc;
13848 bool hba_handled = false;
895427bd 13849 int qidx;
4f774513
JS
13850
13851 /* Get the driver's phba structure from the dev_id */
13852 phba = (struct lpfc_hba *)dev_id;
13853
13854 if (unlikely(!phba))
13855 return IRQ_NONE;
13856
4f774513
JS
13857 /*
13858 * Invoke fast-path host attention interrupt handling as appropriate.
13859 */
895427bd 13860 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 13861 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 13862 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
13863 if (hba_irq_rc == IRQ_HANDLED)
13864 hba_handled |= true;
4f774513
JS
13865 }
13866
1ba981fd
JS
13867 if (phba->cfg_fof) {
13868 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 13869 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
13870 if (hba_irq_rc == IRQ_HANDLED)
13871 hba_handled |= true;
13872 }
13873
67d12733 13874 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
13875} /* lpfc_sli4_intr_handler */
13876
13877/**
13878 * lpfc_sli4_queue_free - free a queue structure and associated memory
13879 * @queue: The queue structure to free.
13880 *
b595076a 13881 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
13882 * the host resident queue. This function must be called after destroying the
13883 * queue on the HBA.
13884 **/
13885void
13886lpfc_sli4_queue_free(struct lpfc_queue *queue)
13887{
13888 struct lpfc_dmabuf *dmabuf;
13889
13890 if (!queue)
13891 return;
13892
13893 while (!list_empty(&queue->page_list)) {
13894 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13895 list);
81b96eda 13896 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
4f774513
JS
13897 dmabuf->virt, dmabuf->phys);
13898 kfree(dmabuf);
13899 }
895427bd
JS
13900 if (queue->rqbp) {
13901 lpfc_free_rq_buffer(queue->phba, queue);
13902 kfree(queue->rqbp);
13903 }
d1f525aa
JS
13904
13905 if (!list_empty(&queue->wq_list))
13906 list_del(&queue->wq_list);
13907
4f774513
JS
13908 kfree(queue);
13909 return;
13910}
13911
13912/**
13913 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13914 * @phba: The HBA that this queue is being created on.
81b96eda 13915 * @page_size: The size of a queue page
4f774513
JS
13916 * @entry_size: The size of each queue entry for this queue.
13917 * @entry count: The number of entries that this queue will handle.
13918 *
13919 * This function allocates a queue structure and the DMAable memory used for
13920 * the host resident queue. This function must be called before creating the
13921 * queue on the HBA.
13922 **/
13923struct lpfc_queue *
81b96eda
JS
13924lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13925 uint32_t entry_size, uint32_t entry_count)
4f774513
JS
13926{
13927 struct lpfc_queue *queue;
13928 struct lpfc_dmabuf *dmabuf;
13929 int x, total_qe_count;
13930 void *dma_pointer;
cb5172ea 13931 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 13932
cb5172ea 13933 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 13934 hw_page_size = page_size;
cb5172ea 13935
4f774513
JS
13936 queue = kzalloc(sizeof(struct lpfc_queue) +
13937 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13938 if (!queue)
13939 return NULL;
cb5172ea
JS
13940 queue->page_count = (ALIGN(entry_size * entry_count,
13941 hw_page_size))/hw_page_size;
895427bd
JS
13942
13943 /* If needed, Adjust page count to match the max the adapter supports */
13944 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13945 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13946
4f774513 13947 INIT_LIST_HEAD(&queue->list);
895427bd 13948 INIT_LIST_HEAD(&queue->wq_list);
4f774513
JS
13949 INIT_LIST_HEAD(&queue->page_list);
13950 INIT_LIST_HEAD(&queue->child_list);
81b96eda
JS
13951
13952 /* Set queue parameters now. If the system cannot provide memory
13953 * resources, the free routine needs to know what was allocated.
13954 */
13955 queue->entry_size = entry_size;
13956 queue->entry_count = entry_count;
13957 queue->page_size = hw_page_size;
13958 queue->phba = phba;
13959
4f774513
JS
13960 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13961 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13962 if (!dmabuf)
13963 goto out_fail;
1aee383d
JP
13964 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13965 hw_page_size, &dmabuf->phys,
13966 GFP_KERNEL);
4f774513
JS
13967 if (!dmabuf->virt) {
13968 kfree(dmabuf);
13969 goto out_fail;
13970 }
13971 dmabuf->buffer_tag = x;
13972 list_add_tail(&dmabuf->list, &queue->page_list);
13973 /* initialize queue's entry array */
13974 dma_pointer = dmabuf->virt;
13975 for (; total_qe_count < entry_count &&
cb5172ea 13976 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
13977 total_qe_count++, dma_pointer += entry_size) {
13978 queue->qe[total_qe_count].address = dma_pointer;
13979 }
13980 }
f485c18d
DK
13981 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
13982 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
4f774513 13983
64eb4dcb
JS
13984 /* entry_repost will be set during q creation */
13985
4f774513
JS
13986 return queue;
13987out_fail:
13988 lpfc_sli4_queue_free(queue);
13989 return NULL;
13990}
13991
962bc51b
JS
13992/**
13993 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13994 * @phba: HBA structure that indicates port to create a queue on.
13995 * @pci_barset: PCI BAR set flag.
13996 *
13997 * This function shall perform iomap of the specified PCI BAR address to host
13998 * memory address if not already done so and return it. The returned host
13999 * memory address can be NULL.
14000 */
14001static void __iomem *
14002lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14003{
962bc51b
JS
14004 if (!phba->pcidev)
14005 return NULL;
962bc51b
JS
14006
14007 switch (pci_barset) {
14008 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
14009 return phba->pci_bar0_memmap_p;
14010 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
14011 return phba->pci_bar2_memmap_p;
14012 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
14013 return phba->pci_bar4_memmap_p;
14014 default:
14015 break;
14016 }
14017 return NULL;
14018}
14019
173edbb2 14020/**
895427bd 14021 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
14022 * @phba: HBA structure that indicates port to create a queue on.
14023 * @startq: The starting FCP EQ to modify
14024 *
14025 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
43140ca6
JS
14026 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14027 * updated in one mailbox command.
173edbb2
JS
14028 *
14029 * The @phba struct is used to send mailbox command to HBA. The @startq
14030 * is used to get the starting FCP EQ to change.
14031 * This function is asynchronous and will wait for the mailbox
14032 * command to finish before continuing.
14033 *
14034 * On success this function will return a zero. If unable to allocate enough
14035 * memory this function will return -ENOMEM. If the queue create mailbox command
14036 * fails this function will return -ENXIO.
14037 **/
a2fc4aef 14038int
0cf07f84
JS
14039lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14040 uint32_t numq, uint32_t imax)
173edbb2
JS
14041{
14042 struct lpfc_mbx_modify_eq_delay *eq_delay;
14043 LPFC_MBOXQ_t *mbox;
14044 struct lpfc_queue *eq;
14045 int cnt, rc, length, status = 0;
14046 uint32_t shdr_status, shdr_add_status;
0cf07f84 14047 uint32_t result, val;
895427bd 14048 int qidx;
173edbb2
JS
14049 union lpfc_sli4_cfg_shdr *shdr;
14050 uint16_t dmult;
14051
895427bd 14052 if (startq >= phba->io_channel_irqs)
173edbb2
JS
14053 return 0;
14054
14055 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14056 if (!mbox)
14057 return -ENOMEM;
14058 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14059 sizeof(struct lpfc_sli4_cfg_mhdr));
14060 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14061 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14062 length, LPFC_SLI4_MBX_EMBED);
14063 eq_delay = &mbox->u.mqe.un.eq_delay;
14064
14065 /* Calculate delay multiper from maximum interrupt per second */
0cf07f84 14066 result = imax / phba->io_channel_irqs;
895427bd 14067 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
14068 dmult = 0;
14069 else
14070 dmult = LPFC_DMULT_CONST/result - 1;
0cf07f84
JS
14071 if (dmult > LPFC_DMULT_MAX)
14072 dmult = LPFC_DMULT_MAX;
173edbb2
JS
14073
14074 cnt = 0;
895427bd
JS
14075 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14076 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
14077 if (!eq)
14078 continue;
0cf07f84 14079 eq->q_mode = imax;
173edbb2
JS
14080 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14081 eq_delay->u.request.eq[cnt].phase = 0;
14082 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14083 cnt++;
0cf07f84
JS
14084
14085 /* q_mode is only used for auto_imax */
14086 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14087 /* Use EQ Delay Register method for q_mode */
14088
14089 /* Convert for EQ Delay register */
14090 val = phba->cfg_fcp_imax;
14091 if (val) {
14092 /* First, interrupts per sec per EQ */
14093 val = phba->cfg_fcp_imax /
14094 phba->io_channel_irqs;
14095
14096 /* us delay between each interrupt */
14097 val = LPFC_SEC_TO_USEC / val;
14098 }
14099 eq->q_mode = val;
14100 } else {
14101 eq->q_mode = imax;
14102 }
14103
14104 if (cnt >= numq)
173edbb2
JS
14105 break;
14106 }
14107 eq_delay->u.request.num_eq = cnt;
14108
14109 mbox->vport = phba->pport;
14110 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14111 mbox->context1 = NULL;
14112 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14113 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14114 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14115 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14116 if (shdr_status || shdr_add_status || rc) {
14117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14118 "2512 MODIFY_EQ_DELAY mailbox failed with "
14119 "status x%x add_status x%x, mbx status x%x\n",
14120 shdr_status, shdr_add_status, rc);
14121 status = -ENXIO;
14122 }
14123 mempool_free(mbox, phba->mbox_mem_pool);
14124 return status;
14125}
14126
4f774513
JS
14127/**
14128 * lpfc_eq_create - Create an Event Queue on the HBA
14129 * @phba: HBA structure that indicates port to create a queue on.
14130 * @eq: The queue structure to use to create the event queue.
14131 * @imax: The maximum interrupt per second limit.
14132 *
14133 * This function creates an event queue, as detailed in @eq, on a port,
14134 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14135 *
14136 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14137 * is used to get the entry count and entry size that are necessary to
14138 * determine the number of pages to allocate and use for this queue. This
14139 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14140 * event queue. This function is asynchronous and will wait for the mailbox
14141 * command to finish before continuing.
14142 *
14143 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14144 * memory this function will return -ENOMEM. If the queue create mailbox command
14145 * fails this function will return -ENXIO.
4f774513 14146 **/
a2fc4aef 14147int
ee02006b 14148lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
14149{
14150 struct lpfc_mbx_eq_create *eq_create;
14151 LPFC_MBOXQ_t *mbox;
14152 int rc, length, status = 0;
14153 struct lpfc_dmabuf *dmabuf;
14154 uint32_t shdr_status, shdr_add_status;
14155 union lpfc_sli4_cfg_shdr *shdr;
14156 uint16_t dmult;
49198b37
JS
14157 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14158
2e90f4b5
JS
14159 /* sanity check on queue memory */
14160 if (!eq)
14161 return -ENODEV;
49198b37
JS
14162 if (!phba->sli4_hba.pc_sli4_params.supported)
14163 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14164
14165 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14166 if (!mbox)
14167 return -ENOMEM;
14168 length = (sizeof(struct lpfc_mbx_eq_create) -
14169 sizeof(struct lpfc_sli4_cfg_mhdr));
14170 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14171 LPFC_MBOX_OPCODE_EQ_CREATE,
14172 length, LPFC_SLI4_MBX_EMBED);
14173 eq_create = &mbox->u.mqe.un.eq_create;
14174 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14175 eq->page_count);
14176 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14177 LPFC_EQE_SIZE);
14178 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
14179 /* don't setup delay multiplier using EQ_CREATE */
14180 dmult = 0;
4f774513
JS
14181 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14182 dmult);
14183 switch (eq->entry_count) {
14184 default:
14185 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14186 "0360 Unsupported EQ count. (%d)\n",
14187 eq->entry_count);
14188 if (eq->entry_count < 256)
14189 return -EINVAL;
14190 /* otherwise default to smallest count (drop through) */
14191 case 256:
14192 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14193 LPFC_EQ_CNT_256);
14194 break;
14195 case 512:
14196 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14197 LPFC_EQ_CNT_512);
14198 break;
14199 case 1024:
14200 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14201 LPFC_EQ_CNT_1024);
14202 break;
14203 case 2048:
14204 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14205 LPFC_EQ_CNT_2048);
14206 break;
14207 case 4096:
14208 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14209 LPFC_EQ_CNT_4096);
14210 break;
14211 }
14212 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 14213 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14214 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14215 putPaddrLow(dmabuf->phys);
14216 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14217 putPaddrHigh(dmabuf->phys);
14218 }
14219 mbox->vport = phba->pport;
14220 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14221 mbox->context1 = NULL;
14222 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14223 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14224 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14225 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14226 if (shdr_status || shdr_add_status || rc) {
14227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14228 "2500 EQ_CREATE mailbox failed with "
14229 "status x%x add_status x%x, mbx status x%x\n",
14230 shdr_status, shdr_add_status, rc);
14231 status = -ENXIO;
14232 }
14233 eq->type = LPFC_EQ;
14234 eq->subtype = LPFC_NONE;
14235 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14236 if (eq->queue_id == 0xFFFF)
14237 status = -ENXIO;
14238 eq->host_index = 0;
14239 eq->hba_index = 0;
64eb4dcb 14240 eq->entry_repost = LPFC_EQ_REPOST;
4f774513 14241
8fa38513 14242 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14243 return status;
14244}
14245
14246/**
14247 * lpfc_cq_create - Create a Completion Queue on the HBA
14248 * @phba: HBA structure that indicates port to create a queue on.
14249 * @cq: The queue structure to use to create the completion queue.
14250 * @eq: The event queue to bind this completion queue to.
14251 *
14252 * This function creates a completion queue, as detailed in @wq, on a port,
14253 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14254 *
14255 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14256 * is used to get the entry count and entry size that are necessary to
14257 * determine the number of pages to allocate and use for this queue. The @eq
14258 * is used to indicate which event queue to bind this completion queue to. This
14259 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14260 * completion queue. This function is asynchronous and will wait for the mailbox
14261 * command to finish before continuing.
14262 *
14263 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14264 * memory this function will return -ENOMEM. If the queue create mailbox command
14265 * fails this function will return -ENXIO.
4f774513 14266 **/
a2fc4aef 14267int
4f774513
JS
14268lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14269 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14270{
14271 struct lpfc_mbx_cq_create *cq_create;
14272 struct lpfc_dmabuf *dmabuf;
14273 LPFC_MBOXQ_t *mbox;
14274 int rc, length, status = 0;
14275 uint32_t shdr_status, shdr_add_status;
14276 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14277 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14278
2e90f4b5
JS
14279 /* sanity check on queue memory */
14280 if (!cq || !eq)
14281 return -ENODEV;
49198b37 14282 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14283 hw_page_size = cq->page_size;
49198b37 14284
4f774513
JS
14285 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14286 if (!mbox)
14287 return -ENOMEM;
14288 length = (sizeof(struct lpfc_mbx_cq_create) -
14289 sizeof(struct lpfc_sli4_cfg_mhdr));
14290 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14291 LPFC_MBOX_OPCODE_CQ_CREATE,
14292 length, LPFC_SLI4_MBX_EMBED);
14293 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14294 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14295 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14296 cq->page_count);
14297 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14298 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14299 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14300 phba->sli4_hba.pc_sli4_params.cqv);
14301 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
81b96eda
JS
14302 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14303 (cq->page_size / SLI4_PAGE_SIZE));
5a6f133e
JS
14304 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14305 eq->queue_id);
14306 } else {
14307 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14308 eq->queue_id);
14309 }
4f774513 14310 switch (cq->entry_count) {
81b96eda
JS
14311 case 2048:
14312 case 4096:
14313 if (phba->sli4_hba.pc_sli4_params.cqv ==
14314 LPFC_Q_CREATE_VERSION_2) {
14315 cq_create->u.request.context.lpfc_cq_context_count =
14316 cq->entry_count;
14317 bf_set(lpfc_cq_context_count,
14318 &cq_create->u.request.context,
14319 LPFC_CQ_CNT_WORD7);
14320 break;
14321 }
14322 /* Fall Thru */
4f774513
JS
14323 default:
14324 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee 14325 "0361 Unsupported CQ count: "
64eb4dcb 14326 "entry cnt %d sz %d pg cnt %d\n",
2ea259ee 14327 cq->entry_count, cq->entry_size,
64eb4dcb 14328 cq->page_count);
4f4c1863
JS
14329 if (cq->entry_count < 256) {
14330 status = -EINVAL;
14331 goto out;
14332 }
4f774513
JS
14333 /* otherwise default to smallest count (drop through) */
14334 case 256:
14335 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14336 LPFC_CQ_CNT_256);
14337 break;
14338 case 512:
14339 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14340 LPFC_CQ_CNT_512);
14341 break;
14342 case 1024:
14343 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14344 LPFC_CQ_CNT_1024);
14345 break;
14346 }
14347 list_for_each_entry(dmabuf, &cq->page_list, list) {
81b96eda 14348 memset(dmabuf->virt, 0, cq->page_size);
4f774513
JS
14349 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14350 putPaddrLow(dmabuf->phys);
14351 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14352 putPaddrHigh(dmabuf->phys);
14353 }
14354 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14355
14356 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14357 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14358 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14359 if (shdr_status || shdr_add_status || rc) {
14360 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14361 "2501 CQ_CREATE mailbox failed with "
14362 "status x%x add_status x%x, mbx status x%x\n",
14363 shdr_status, shdr_add_status, rc);
14364 status = -ENXIO;
14365 goto out;
14366 }
14367 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14368 if (cq->queue_id == 0xFFFF) {
14369 status = -ENXIO;
14370 goto out;
14371 }
14372 /* link the cq onto the parent eq child list */
14373 list_add_tail(&cq->list, &eq->child_list);
14374 /* Set up completion queue's type and subtype */
14375 cq->type = type;
14376 cq->subtype = subtype;
14377 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14378 cq->assoc_qid = eq->queue_id;
4f774513
JS
14379 cq->host_index = 0;
14380 cq->hba_index = 0;
64eb4dcb 14381 cq->entry_repost = LPFC_CQ_REPOST;
4f774513 14382
8fa38513
JS
14383out:
14384 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14385 return status;
14386}
14387
2d7dbc4c
JS
14388/**
14389 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14390 * @phba: HBA structure that indicates port to create a queue on.
14391 * @cqp: The queue structure array to use to create the completion queues.
14392 * @eqp: The event queue array to bind these completion queues to.
14393 *
14394 * This function creates a set of completion queue, s to support MRQ
14395 * as detailed in @cqp, on a port,
14396 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14397 *
14398 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14399 * is used to get the entry count and entry size that are necessary to
14400 * determine the number of pages to allocate and use for this queue. The @eq
14401 * is used to indicate which event queue to bind this completion queue to. This
14402 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14403 * completion queue. This function is asynchronous and will wait for the mailbox
14404 * command to finish before continuing.
14405 *
14406 * On success this function will return a zero. If unable to allocate enough
14407 * memory this function will return -ENOMEM. If the queue create mailbox command
14408 * fails this function will return -ENXIO.
14409 **/
14410int
14411lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14412 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14413{
14414 struct lpfc_queue *cq;
14415 struct lpfc_queue *eq;
14416 struct lpfc_mbx_cq_create_set *cq_set;
14417 struct lpfc_dmabuf *dmabuf;
14418 LPFC_MBOXQ_t *mbox;
14419 int rc, length, alloclen, status = 0;
14420 int cnt, idx, numcq, page_idx = 0;
14421 uint32_t shdr_status, shdr_add_status;
14422 union lpfc_sli4_cfg_shdr *shdr;
14423 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14424
14425 /* sanity check on queue memory */
14426 numcq = phba->cfg_nvmet_mrq;
14427 if (!cqp || !eqp || !numcq)
14428 return -ENODEV;
2d7dbc4c
JS
14429
14430 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14431 if (!mbox)
14432 return -ENOMEM;
14433
14434 length = sizeof(struct lpfc_mbx_cq_create_set);
14435 length += ((numcq * cqp[0]->page_count) *
14436 sizeof(struct dma_address));
14437 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14438 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14439 LPFC_SLI4_MBX_NEMBED);
14440 if (alloclen < length) {
14441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14442 "3098 Allocated DMA memory size (%d) is "
14443 "less than the requested DMA memory size "
14444 "(%d)\n", alloclen, length);
14445 status = -ENOMEM;
14446 goto out;
14447 }
14448 cq_set = mbox->sge_array->addr[0];
14449 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14450 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14451
14452 for (idx = 0; idx < numcq; idx++) {
14453 cq = cqp[idx];
14454 eq = eqp[idx];
14455 if (!cq || !eq) {
14456 status = -ENOMEM;
14457 goto out;
14458 }
81b96eda
JS
14459 if (!phba->sli4_hba.pc_sli4_params.supported)
14460 hw_page_size = cq->page_size;
2d7dbc4c
JS
14461
14462 switch (idx) {
14463 case 0:
14464 bf_set(lpfc_mbx_cq_create_set_page_size,
14465 &cq_set->u.request,
14466 (hw_page_size / SLI4_PAGE_SIZE));
14467 bf_set(lpfc_mbx_cq_create_set_num_pages,
14468 &cq_set->u.request, cq->page_count);
14469 bf_set(lpfc_mbx_cq_create_set_evt,
14470 &cq_set->u.request, 1);
14471 bf_set(lpfc_mbx_cq_create_set_valid,
14472 &cq_set->u.request, 1);
14473 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14474 &cq_set->u.request, 0);
14475 bf_set(lpfc_mbx_cq_create_set_num_cq,
14476 &cq_set->u.request, numcq);
14477 switch (cq->entry_count) {
81b96eda
JS
14478 case 2048:
14479 case 4096:
14480 if (phba->sli4_hba.pc_sli4_params.cqv ==
14481 LPFC_Q_CREATE_VERSION_2) {
14482 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14483 &cq_set->u.request,
14484 cq->entry_count);
14485 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14486 &cq_set->u.request,
14487 LPFC_CQ_CNT_WORD7);
14488 break;
14489 }
14490 /* Fall Thru */
2d7dbc4c
JS
14491 default:
14492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14493 "3118 Bad CQ count. (%d)\n",
14494 cq->entry_count);
14495 if (cq->entry_count < 256) {
14496 status = -EINVAL;
14497 goto out;
14498 }
14499 /* otherwise default to smallest (drop thru) */
14500 case 256:
14501 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14502 &cq_set->u.request, LPFC_CQ_CNT_256);
14503 break;
14504 case 512:
14505 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14506 &cq_set->u.request, LPFC_CQ_CNT_512);
14507 break;
14508 case 1024:
14509 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14510 &cq_set->u.request, LPFC_CQ_CNT_1024);
14511 break;
14512 }
14513 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14514 &cq_set->u.request, eq->queue_id);
14515 break;
14516 case 1:
14517 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14518 &cq_set->u.request, eq->queue_id);
14519 break;
14520 case 2:
14521 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14522 &cq_set->u.request, eq->queue_id);
14523 break;
14524 case 3:
14525 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14526 &cq_set->u.request, eq->queue_id);
14527 break;
14528 case 4:
14529 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14530 &cq_set->u.request, eq->queue_id);
14531 break;
14532 case 5:
14533 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14534 &cq_set->u.request, eq->queue_id);
14535 break;
14536 case 6:
14537 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14538 &cq_set->u.request, eq->queue_id);
14539 break;
14540 case 7:
14541 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14542 &cq_set->u.request, eq->queue_id);
14543 break;
14544 case 8:
14545 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14546 &cq_set->u.request, eq->queue_id);
14547 break;
14548 case 9:
14549 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14550 &cq_set->u.request, eq->queue_id);
14551 break;
14552 case 10:
14553 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14554 &cq_set->u.request, eq->queue_id);
14555 break;
14556 case 11:
14557 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14558 &cq_set->u.request, eq->queue_id);
14559 break;
14560 case 12:
14561 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14562 &cq_set->u.request, eq->queue_id);
14563 break;
14564 case 13:
14565 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14566 &cq_set->u.request, eq->queue_id);
14567 break;
14568 case 14:
14569 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14570 &cq_set->u.request, eq->queue_id);
14571 break;
14572 case 15:
14573 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14574 &cq_set->u.request, eq->queue_id);
14575 break;
14576 }
14577
14578 /* link the cq onto the parent eq child list */
14579 list_add_tail(&cq->list, &eq->child_list);
14580 /* Set up completion queue's type and subtype */
14581 cq->type = type;
14582 cq->subtype = subtype;
14583 cq->assoc_qid = eq->queue_id;
14584 cq->host_index = 0;
14585 cq->hba_index = 0;
64eb4dcb 14586 cq->entry_repost = LPFC_CQ_REPOST;
81b96eda 14587 cq->chann = idx;
2d7dbc4c
JS
14588
14589 rc = 0;
14590 list_for_each_entry(dmabuf, &cq->page_list, list) {
14591 memset(dmabuf->virt, 0, hw_page_size);
14592 cnt = page_idx + dmabuf->buffer_tag;
14593 cq_set->u.request.page[cnt].addr_lo =
14594 putPaddrLow(dmabuf->phys);
14595 cq_set->u.request.page[cnt].addr_hi =
14596 putPaddrHigh(dmabuf->phys);
14597 rc++;
14598 }
14599 page_idx += rc;
14600 }
14601
14602 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14603
14604 /* The IOCTL status is embedded in the mailbox subheader. */
14605 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14606 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14607 if (shdr_status || shdr_add_status || rc) {
14608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14609 "3119 CQ_CREATE_SET mailbox failed with "
14610 "status x%x add_status x%x, mbx status x%x\n",
14611 shdr_status, shdr_add_status, rc);
14612 status = -ENXIO;
14613 goto out;
14614 }
14615 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14616 if (rc == 0xFFFF) {
14617 status = -ENXIO;
14618 goto out;
14619 }
14620
14621 for (idx = 0; idx < numcq; idx++) {
14622 cq = cqp[idx];
14623 cq->queue_id = rc + idx;
14624 }
14625
14626out:
14627 lpfc_sli4_mbox_cmd_free(phba, mbox);
14628 return status;
14629}
14630
b19a061a
JS
14631/**
14632 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14633 * @phba: HBA structure that indicates port to create a queue on.
14634 * @mq: The queue structure to use to create the mailbox queue.
14635 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14636 * @cq: The completion queue to associate with this cq.
14637 *
14638 * This function provides failback (fb) functionality when the
14639 * mq_create_ext fails on older FW generations. It's purpose is identical
14640 * to mq_create_ext otherwise.
14641 *
14642 * This routine cannot fail as all attributes were previously accessed and
14643 * initialized in mq_create_ext.
14644 **/
14645static void
14646lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14647 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14648{
14649 struct lpfc_mbx_mq_create *mq_create;
14650 struct lpfc_dmabuf *dmabuf;
14651 int length;
14652
14653 length = (sizeof(struct lpfc_mbx_mq_create) -
14654 sizeof(struct lpfc_sli4_cfg_mhdr));
14655 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14656 LPFC_MBOX_OPCODE_MQ_CREATE,
14657 length, LPFC_SLI4_MBX_EMBED);
14658 mq_create = &mbox->u.mqe.un.mq_create;
14659 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14660 mq->page_count);
14661 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14662 cq->queue_id);
14663 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14664 switch (mq->entry_count) {
14665 case 16:
5a6f133e
JS
14666 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14667 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14668 break;
14669 case 32:
5a6f133e
JS
14670 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14671 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14672 break;
14673 case 64:
5a6f133e
JS
14674 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14675 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14676 break;
14677 case 128:
5a6f133e
JS
14678 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14679 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14680 break;
14681 }
14682 list_for_each_entry(dmabuf, &mq->page_list, list) {
14683 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14684 putPaddrLow(dmabuf->phys);
14685 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14686 putPaddrHigh(dmabuf->phys);
14687 }
14688}
14689
04c68496
JS
14690/**
14691 * lpfc_mq_create - Create a mailbox Queue on the HBA
14692 * @phba: HBA structure that indicates port to create a queue on.
14693 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14694 * @cq: The completion queue to associate with this cq.
14695 * @subtype: The queue's subtype.
04c68496
JS
14696 *
14697 * This function creates a mailbox queue, as detailed in @mq, on a port,
14698 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14699 *
14700 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14701 * is used to get the entry count and entry size that are necessary to
14702 * determine the number of pages to allocate and use for this queue. This
14703 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14704 * mailbox queue. This function is asynchronous and will wait for the mailbox
14705 * command to finish before continuing.
14706 *
14707 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14708 * memory this function will return -ENOMEM. If the queue create mailbox command
14709 * fails this function will return -ENXIO.
04c68496 14710 **/
b19a061a 14711int32_t
04c68496
JS
14712lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14713 struct lpfc_queue *cq, uint32_t subtype)
14714{
14715 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14716 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14717 struct lpfc_dmabuf *dmabuf;
14718 LPFC_MBOXQ_t *mbox;
14719 int rc, length, status = 0;
14720 uint32_t shdr_status, shdr_add_status;
14721 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14722 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14723
2e90f4b5
JS
14724 /* sanity check on queue memory */
14725 if (!mq || !cq)
14726 return -ENODEV;
49198b37
JS
14727 if (!phba->sli4_hba.pc_sli4_params.supported)
14728 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14729
04c68496
JS
14730 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14731 if (!mbox)
14732 return -ENOMEM;
b19a061a 14733 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14734 sizeof(struct lpfc_sli4_cfg_mhdr));
14735 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14736 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14737 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14738
14739 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14740 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14741 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14742 &mq_create_ext->u.request, mq->page_count);
14743 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14744 &mq_create_ext->u.request, 1);
14745 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14746 &mq_create_ext->u.request, 1);
14747 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14748 &mq_create_ext->u.request, 1);
70f3c073
JS
14749 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14750 &mq_create_ext->u.request, 1);
14751 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14752 &mq_create_ext->u.request, 1);
b19a061a 14753 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14754 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14755 phba->sli4_hba.pc_sli4_params.mqv);
14756 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14757 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14758 cq->queue_id);
14759 else
14760 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14761 cq->queue_id);
04c68496
JS
14762 switch (mq->entry_count) {
14763 default:
14764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14765 "0362 Unsupported MQ count. (%d)\n",
14766 mq->entry_count);
4f4c1863
JS
14767 if (mq->entry_count < 16) {
14768 status = -EINVAL;
14769 goto out;
14770 }
04c68496
JS
14771 /* otherwise default to smallest count (drop through) */
14772 case 16:
5a6f133e
JS
14773 bf_set(lpfc_mq_context_ring_size,
14774 &mq_create_ext->u.request.context,
14775 LPFC_MQ_RING_SIZE_16);
04c68496
JS
14776 break;
14777 case 32:
5a6f133e
JS
14778 bf_set(lpfc_mq_context_ring_size,
14779 &mq_create_ext->u.request.context,
14780 LPFC_MQ_RING_SIZE_32);
04c68496
JS
14781 break;
14782 case 64:
5a6f133e
JS
14783 bf_set(lpfc_mq_context_ring_size,
14784 &mq_create_ext->u.request.context,
14785 LPFC_MQ_RING_SIZE_64);
04c68496
JS
14786 break;
14787 case 128:
5a6f133e
JS
14788 bf_set(lpfc_mq_context_ring_size,
14789 &mq_create_ext->u.request.context,
14790 LPFC_MQ_RING_SIZE_128);
04c68496
JS
14791 break;
14792 }
14793 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 14794 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 14795 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 14796 putPaddrLow(dmabuf->phys);
b19a061a 14797 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
14798 putPaddrHigh(dmabuf->phys);
14799 }
14800 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
14801 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14802 &mq_create_ext->u.response);
14803 if (rc != MBX_SUCCESS) {
14804 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14805 "2795 MQ_CREATE_EXT failed with "
14806 "status x%x. Failback to MQ_CREATE.\n",
14807 rc);
14808 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14809 mq_create = &mbox->u.mqe.un.mq_create;
14810 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14811 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14812 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14813 &mq_create->u.response);
14814 }
14815
04c68496 14816 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
14817 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14818 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14819 if (shdr_status || shdr_add_status || rc) {
14820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14821 "2502 MQ_CREATE mailbox failed with "
14822 "status x%x add_status x%x, mbx status x%x\n",
14823 shdr_status, shdr_add_status, rc);
14824 status = -ENXIO;
14825 goto out;
14826 }
04c68496
JS
14827 if (mq->queue_id == 0xFFFF) {
14828 status = -ENXIO;
14829 goto out;
14830 }
14831 mq->type = LPFC_MQ;
2a622bfb 14832 mq->assoc_qid = cq->queue_id;
04c68496
JS
14833 mq->subtype = subtype;
14834 mq->host_index = 0;
14835 mq->hba_index = 0;
64eb4dcb 14836 mq->entry_repost = LPFC_MQ_REPOST;
04c68496
JS
14837
14838 /* link the mq onto the parent cq child list */
14839 list_add_tail(&mq->list, &cq->child_list);
14840out:
8fa38513 14841 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
14842 return status;
14843}
14844
4f774513
JS
14845/**
14846 * lpfc_wq_create - Create a Work Queue on the HBA
14847 * @phba: HBA structure that indicates port to create a queue on.
14848 * @wq: The queue structure to use to create the work queue.
14849 * @cq: The completion queue to bind this work queue to.
14850 * @subtype: The subtype of the work queue indicating its functionality.
14851 *
14852 * This function creates a work queue, as detailed in @wq, on a port, described
14853 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14854 *
14855 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14856 * is used to get the entry count and entry size that are necessary to
14857 * determine the number of pages to allocate and use for this queue. The @cq
14858 * is used to indicate which completion queue to bind this work queue to. This
14859 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14860 * work queue. This function is asynchronous and will wait for the mailbox
14861 * command to finish before continuing.
14862 *
14863 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14864 * memory this function will return -ENOMEM. If the queue create mailbox command
14865 * fails this function will return -ENXIO.
4f774513 14866 **/
a2fc4aef 14867int
4f774513
JS
14868lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14869 struct lpfc_queue *cq, uint32_t subtype)
14870{
14871 struct lpfc_mbx_wq_create *wq_create;
14872 struct lpfc_dmabuf *dmabuf;
14873 LPFC_MBOXQ_t *mbox;
14874 int rc, length, status = 0;
14875 uint32_t shdr_status, shdr_add_status;
14876 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14877 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 14878 struct dma_address *page;
962bc51b
JS
14879 void __iomem *bar_memmap_p;
14880 uint32_t db_offset;
14881 uint16_t pci_barset;
81b96eda 14882 uint8_t wq_create_version;
49198b37 14883
2e90f4b5
JS
14884 /* sanity check on queue memory */
14885 if (!wq || !cq)
14886 return -ENODEV;
49198b37 14887 if (!phba->sli4_hba.pc_sli4_params.supported)
81b96eda 14888 hw_page_size = wq->page_size;
4f774513
JS
14889
14890 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14891 if (!mbox)
14892 return -ENOMEM;
14893 length = (sizeof(struct lpfc_mbx_wq_create) -
14894 sizeof(struct lpfc_sli4_cfg_mhdr));
14895 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14896 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14897 length, LPFC_SLI4_MBX_EMBED);
14898 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 14899 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
14900 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14901 wq->page_count);
14902 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14903 cq->queue_id);
0c651878
JS
14904
14905 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
14906 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14907 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 14908
81b96eda
JS
14909 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
14910 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14911 else
14912 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14913
14914 switch (wq_create_version) {
0c651878
JS
14915 case LPFC_Q_CREATE_VERSION_0:
14916 switch (wq->entry_size) {
14917 default:
14918 case 64:
14919 /* Nothing to do, version 0 ONLY supports 64 byte */
14920 page = wq_create->u.request.page;
14921 break;
14922 case 128:
14923 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14924 LPFC_WQ_SZ128_SUPPORT)) {
14925 status = -ERANGE;
14926 goto out;
14927 }
14928 /* If we get here the HBA MUST also support V1 and
14929 * we MUST use it
14930 */
14931 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14932 LPFC_Q_CREATE_VERSION_1);
14933
14934 bf_set(lpfc_mbx_wq_create_wqe_count,
14935 &wq_create->u.request_1, wq->entry_count);
14936 bf_set(lpfc_mbx_wq_create_wqe_size,
14937 &wq_create->u.request_1,
14938 LPFC_WQ_WQE_SIZE_128);
14939 bf_set(lpfc_mbx_wq_create_page_size,
14940 &wq_create->u.request_1,
8ea73db4 14941 LPFC_WQ_PAGE_SIZE_4096);
0c651878
JS
14942 page = wq_create->u.request_1.page;
14943 break;
14944 }
14945 break;
14946 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
14947 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14948 wq->entry_count);
3f247de7
JS
14949 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14950 LPFC_Q_CREATE_VERSION_1);
14951
5a6f133e
JS
14952 switch (wq->entry_size) {
14953 default:
14954 case 64:
14955 bf_set(lpfc_mbx_wq_create_wqe_size,
14956 &wq_create->u.request_1,
14957 LPFC_WQ_WQE_SIZE_64);
14958 break;
14959 case 128:
0c651878
JS
14960 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14961 LPFC_WQ_SZ128_SUPPORT)) {
14962 status = -ERANGE;
14963 goto out;
14964 }
5a6f133e
JS
14965 bf_set(lpfc_mbx_wq_create_wqe_size,
14966 &wq_create->u.request_1,
14967 LPFC_WQ_WQE_SIZE_128);
14968 break;
14969 }
8ea73db4
JS
14970 bf_set(lpfc_mbx_wq_create_page_size,
14971 &wq_create->u.request_1,
81b96eda 14972 (wq->page_size / SLI4_PAGE_SIZE));
5a6f133e 14973 page = wq_create->u.request_1.page;
0c651878
JS
14974 break;
14975 default:
14976 status = -ERANGE;
14977 goto out;
5a6f133e 14978 }
0c651878 14979
4f774513 14980 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 14981 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
14982 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14983 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 14984 }
962bc51b
JS
14985
14986 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14987 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14988
4f774513
JS
14989 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14990 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14991 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14992 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14993 if (shdr_status || shdr_add_status || rc) {
14994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14995 "2503 WQ_CREATE mailbox failed with "
14996 "status x%x add_status x%x, mbx status x%x\n",
14997 shdr_status, shdr_add_status, rc);
14998 status = -ENXIO;
14999 goto out;
15000 }
15001 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15002 if (wq->queue_id == 0xFFFF) {
15003 status = -ENXIO;
15004 goto out;
15005 }
962bc51b
JS
15006 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15007 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15008 &wq_create->u.response);
15009 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15010 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15012 "3265 WQ[%d] doorbell format not "
15013 "supported: x%x\n", wq->queue_id,
15014 wq->db_format);
15015 status = -EINVAL;
15016 goto out;
15017 }
15018 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15019 &wq_create->u.response);
15020 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15021 if (!bar_memmap_p) {
15022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15023 "3263 WQ[%d] failed to memmap pci "
15024 "barset:x%x\n", wq->queue_id,
15025 pci_barset);
15026 status = -ENOMEM;
15027 goto out;
15028 }
15029 db_offset = wq_create->u.response.doorbell_offset;
15030 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15031 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15033 "3252 WQ[%d] doorbell offset not "
15034 "supported: x%x\n", wq->queue_id,
15035 db_offset);
15036 status = -EINVAL;
15037 goto out;
15038 }
15039 wq->db_regaddr = bar_memmap_p + db_offset;
15040 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15041 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15042 "format:x%x\n", wq->queue_id, pci_barset,
15043 db_offset, wq->db_format);
962bc51b
JS
15044 } else {
15045 wq->db_format = LPFC_DB_LIST_FORMAT;
15046 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15047 }
895427bd
JS
15048 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15049 if (wq->pring == NULL) {
15050 status = -ENOMEM;
15051 goto out;
15052 }
4f774513 15053 wq->type = LPFC_WQ;
2a622bfb 15054 wq->assoc_qid = cq->queue_id;
4f774513
JS
15055 wq->subtype = subtype;
15056 wq->host_index = 0;
15057 wq->hba_index = 0;
ff78d8f9 15058 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
15059
15060 /* link the wq onto the parent cq child list */
15061 list_add_tail(&wq->list, &cq->child_list);
15062out:
8fa38513 15063 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15064 return status;
15065}
15066
15067/**
15068 * lpfc_rq_create - Create a Receive Queue on the HBA
15069 * @phba: HBA structure that indicates port to create a queue on.
15070 * @hrq: The queue structure to use to create the header receive queue.
15071 * @drq: The queue structure to use to create the data receive queue.
15072 * @cq: The completion queue to bind this work queue to.
15073 *
15074 * This function creates a receive buffer queue pair , as detailed in @hrq and
15075 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15076 * to the HBA.
15077 *
15078 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15079 * struct is used to get the entry count that is necessary to determine the
15080 * number of pages to use for this queue. The @cq is used to indicate which
15081 * completion queue to bind received buffers that are posted to these queues to.
15082 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15083 * receive queue pair. This function is asynchronous and will wait for the
15084 * mailbox command to finish before continuing.
15085 *
15086 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
15087 * memory this function will return -ENOMEM. If the queue create mailbox command
15088 * fails this function will return -ENXIO.
4f774513 15089 **/
a2fc4aef 15090int
4f774513
JS
15091lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15092 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15093{
15094 struct lpfc_mbx_rq_create *rq_create;
15095 struct lpfc_dmabuf *dmabuf;
15096 LPFC_MBOXQ_t *mbox;
15097 int rc, length, status = 0;
15098 uint32_t shdr_status, shdr_add_status;
15099 union lpfc_sli4_cfg_shdr *shdr;
49198b37 15100 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
15101 void __iomem *bar_memmap_p;
15102 uint32_t db_offset;
15103 uint16_t pci_barset;
49198b37 15104
2e90f4b5
JS
15105 /* sanity check on queue memory */
15106 if (!hrq || !drq || !cq)
15107 return -ENODEV;
49198b37
JS
15108 if (!phba->sli4_hba.pc_sli4_params.supported)
15109 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
15110
15111 if (hrq->entry_count != drq->entry_count)
15112 return -EINVAL;
15113 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15114 if (!mbox)
15115 return -ENOMEM;
15116 length = (sizeof(struct lpfc_mbx_rq_create) -
15117 sizeof(struct lpfc_sli4_cfg_mhdr));
15118 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15119 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15120 length, LPFC_SLI4_MBX_EMBED);
15121 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
15122 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15123 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15124 phba->sli4_hba.pc_sli4_params.rqv);
15125 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15126 bf_set(lpfc_rq_context_rqe_count_1,
15127 &rq_create->u.request.context,
15128 hrq->entry_count);
15129 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
15130 bf_set(lpfc_rq_context_rqe_size,
15131 &rq_create->u.request.context,
15132 LPFC_RQE_SIZE_8);
15133 bf_set(lpfc_rq_context_page_size,
15134 &rq_create->u.request.context,
8ea73db4 15135 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
15136 } else {
15137 switch (hrq->entry_count) {
15138 default:
15139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15140 "2535 Unsupported RQ count. (%d)\n",
15141 hrq->entry_count);
4f4c1863
JS
15142 if (hrq->entry_count < 512) {
15143 status = -EINVAL;
15144 goto out;
15145 }
5a6f133e
JS
15146 /* otherwise default to smallest count (drop through) */
15147 case 512:
15148 bf_set(lpfc_rq_context_rqe_count,
15149 &rq_create->u.request.context,
15150 LPFC_RQ_RING_SIZE_512);
15151 break;
15152 case 1024:
15153 bf_set(lpfc_rq_context_rqe_count,
15154 &rq_create->u.request.context,
15155 LPFC_RQ_RING_SIZE_1024);
15156 break;
15157 case 2048:
15158 bf_set(lpfc_rq_context_rqe_count,
15159 &rq_create->u.request.context,
15160 LPFC_RQ_RING_SIZE_2048);
15161 break;
15162 case 4096:
15163 bf_set(lpfc_rq_context_rqe_count,
15164 &rq_create->u.request.context,
15165 LPFC_RQ_RING_SIZE_4096);
15166 break;
15167 }
15168 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15169 LPFC_HDR_BUF_SIZE);
4f774513
JS
15170 }
15171 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15172 cq->queue_id);
15173 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15174 hrq->page_count);
4f774513 15175 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 15176 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
15177 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15178 putPaddrLow(dmabuf->phys);
15179 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15180 putPaddrHigh(dmabuf->phys);
15181 }
962bc51b
JS
15182 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15183 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15184
4f774513
JS
15185 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15186 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
15187 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15188 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15189 if (shdr_status || shdr_add_status || rc) {
15190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15191 "2504 RQ_CREATE mailbox failed with "
15192 "status x%x add_status x%x, mbx status x%x\n",
15193 shdr_status, shdr_add_status, rc);
15194 status = -ENXIO;
15195 goto out;
15196 }
15197 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15198 if (hrq->queue_id == 0xFFFF) {
15199 status = -ENXIO;
15200 goto out;
15201 }
962bc51b
JS
15202
15203 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15204 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15205 &rq_create->u.response);
15206 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15207 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15209 "3262 RQ [%d] doorbell format not "
15210 "supported: x%x\n", hrq->queue_id,
15211 hrq->db_format);
15212 status = -EINVAL;
15213 goto out;
15214 }
15215
15216 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15217 &rq_create->u.response);
15218 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15219 if (!bar_memmap_p) {
15220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15221 "3269 RQ[%d] failed to memmap pci "
15222 "barset:x%x\n", hrq->queue_id,
15223 pci_barset);
15224 status = -ENOMEM;
15225 goto out;
15226 }
15227
15228 db_offset = rq_create->u.response.doorbell_offset;
15229 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15230 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15232 "3270 RQ[%d] doorbell offset not "
15233 "supported: x%x\n", hrq->queue_id,
15234 db_offset);
15235 status = -EINVAL;
15236 goto out;
15237 }
15238 hrq->db_regaddr = bar_memmap_p + db_offset;
15239 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15240 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15241 "format:x%x\n", hrq->queue_id, pci_barset,
15242 db_offset, hrq->db_format);
962bc51b
JS
15243 } else {
15244 hrq->db_format = LPFC_DB_RING_FORMAT;
15245 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15246 }
4f774513 15247 hrq->type = LPFC_HRQ;
2a622bfb 15248 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15249 hrq->subtype = subtype;
15250 hrq->host_index = 0;
15251 hrq->hba_index = 0;
61f3d4bf 15252 hrq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15253
15254 /* now create the data queue */
15255 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15256 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15257 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15258 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15259 phba->sli4_hba.pc_sli4_params.rqv);
15260 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15261 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15262 &rq_create->u.request.context, hrq->entry_count);
3c603be9
JS
15263 if (subtype == LPFC_NVMET)
15264 rq_create->u.request.context.buffer_size =
15265 LPFC_NVMET_DATA_BUF_SIZE;
15266 else
15267 rq_create->u.request.context.buffer_size =
15268 LPFC_DATA_BUF_SIZE;
c31098ce
JS
15269 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15270 LPFC_RQE_SIZE_8);
15271 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15272 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15273 } else {
15274 switch (drq->entry_count) {
15275 default:
15276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15277 "2536 Unsupported RQ count. (%d)\n",
15278 drq->entry_count);
4f4c1863
JS
15279 if (drq->entry_count < 512) {
15280 status = -EINVAL;
15281 goto out;
15282 }
5a6f133e
JS
15283 /* otherwise default to smallest count (drop through) */
15284 case 512:
15285 bf_set(lpfc_rq_context_rqe_count,
15286 &rq_create->u.request.context,
15287 LPFC_RQ_RING_SIZE_512);
15288 break;
15289 case 1024:
15290 bf_set(lpfc_rq_context_rqe_count,
15291 &rq_create->u.request.context,
15292 LPFC_RQ_RING_SIZE_1024);
15293 break;
15294 case 2048:
15295 bf_set(lpfc_rq_context_rqe_count,
15296 &rq_create->u.request.context,
15297 LPFC_RQ_RING_SIZE_2048);
15298 break;
15299 case 4096:
15300 bf_set(lpfc_rq_context_rqe_count,
15301 &rq_create->u.request.context,
15302 LPFC_RQ_RING_SIZE_4096);
15303 break;
15304 }
3c603be9
JS
15305 if (subtype == LPFC_NVMET)
15306 bf_set(lpfc_rq_context_buf_size,
15307 &rq_create->u.request.context,
15308 LPFC_NVMET_DATA_BUF_SIZE);
15309 else
15310 bf_set(lpfc_rq_context_buf_size,
15311 &rq_create->u.request.context,
15312 LPFC_DATA_BUF_SIZE);
4f774513
JS
15313 }
15314 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15315 cq->queue_id);
15316 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15317 drq->page_count);
4f774513
JS
15318 list_for_each_entry(dmabuf, &drq->page_list, list) {
15319 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15320 putPaddrLow(dmabuf->phys);
15321 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15322 putPaddrHigh(dmabuf->phys);
15323 }
962bc51b
JS
15324 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15325 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15326 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15327 /* The IOCTL status is embedded in the mailbox subheader. */
15328 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15329 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15330 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15331 if (shdr_status || shdr_add_status || rc) {
15332 status = -ENXIO;
15333 goto out;
15334 }
15335 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15336 if (drq->queue_id == 0xFFFF) {
15337 status = -ENXIO;
15338 goto out;
15339 }
15340 drq->type = LPFC_DRQ;
2a622bfb 15341 drq->assoc_qid = cq->queue_id;
4f774513
JS
15342 drq->subtype = subtype;
15343 drq->host_index = 0;
15344 drq->hba_index = 0;
61f3d4bf 15345 drq->entry_repost = LPFC_RQ_REPOST;
4f774513
JS
15346
15347 /* link the header and data RQs onto the parent cq child list */
15348 list_add_tail(&hrq->list, &cq->child_list);
15349 list_add_tail(&drq->list, &cq->child_list);
15350
15351out:
8fa38513 15352 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15353 return status;
15354}
15355
2d7dbc4c
JS
15356/**
15357 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15358 * @phba: HBA structure that indicates port to create a queue on.
15359 * @hrqp: The queue structure array to use to create the header receive queues.
15360 * @drqp: The queue structure array to use to create the data receive queues.
15361 * @cqp: The completion queue array to bind these receive queues to.
15362 *
15363 * This function creates a receive buffer queue pair , as detailed in @hrq and
15364 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15365 * to the HBA.
15366 *
15367 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15368 * struct is used to get the entry count that is necessary to determine the
15369 * number of pages to use for this queue. The @cq is used to indicate which
15370 * completion queue to bind received buffers that are posted to these queues to.
15371 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15372 * receive queue pair. This function is asynchronous and will wait for the
15373 * mailbox command to finish before continuing.
15374 *
15375 * On success this function will return a zero. If unable to allocate enough
15376 * memory this function will return -ENOMEM. If the queue create mailbox command
15377 * fails this function will return -ENXIO.
15378 **/
15379int
15380lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15381 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15382 uint32_t subtype)
15383{
15384 struct lpfc_queue *hrq, *drq, *cq;
15385 struct lpfc_mbx_rq_create_v2 *rq_create;
15386 struct lpfc_dmabuf *dmabuf;
15387 LPFC_MBOXQ_t *mbox;
15388 int rc, length, alloclen, status = 0;
15389 int cnt, idx, numrq, page_idx = 0;
15390 uint32_t shdr_status, shdr_add_status;
15391 union lpfc_sli4_cfg_shdr *shdr;
15392 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15393
15394 numrq = phba->cfg_nvmet_mrq;
15395 /* sanity check on array memory */
15396 if (!hrqp || !drqp || !cqp || !numrq)
15397 return -ENODEV;
15398 if (!phba->sli4_hba.pc_sli4_params.supported)
15399 hw_page_size = SLI4_PAGE_SIZE;
15400
15401 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15402 if (!mbox)
15403 return -ENOMEM;
15404
15405 length = sizeof(struct lpfc_mbx_rq_create_v2);
15406 length += ((2 * numrq * hrqp[0]->page_count) *
15407 sizeof(struct dma_address));
15408
15409 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15410 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15411 LPFC_SLI4_MBX_NEMBED);
15412 if (alloclen < length) {
15413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15414 "3099 Allocated DMA memory size (%d) is "
15415 "less than the requested DMA memory size "
15416 "(%d)\n", alloclen, length);
15417 status = -ENOMEM;
15418 goto out;
15419 }
15420
15421
15422
15423 rq_create = mbox->sge_array->addr[0];
15424 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15425
15426 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15427 cnt = 0;
15428
15429 for (idx = 0; idx < numrq; idx++) {
15430 hrq = hrqp[idx];
15431 drq = drqp[idx];
15432 cq = cqp[idx];
15433
2d7dbc4c
JS
15434 /* sanity check on queue memory */
15435 if (!hrq || !drq || !cq) {
15436 status = -ENODEV;
15437 goto out;
15438 }
15439
7aabe84b
JS
15440 if (hrq->entry_count != drq->entry_count) {
15441 status = -EINVAL;
15442 goto out;
15443 }
15444
2d7dbc4c
JS
15445 if (idx == 0) {
15446 bf_set(lpfc_mbx_rq_create_num_pages,
15447 &rq_create->u.request,
15448 hrq->page_count);
15449 bf_set(lpfc_mbx_rq_create_rq_cnt,
15450 &rq_create->u.request, (numrq * 2));
15451 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15452 1);
15453 bf_set(lpfc_rq_context_base_cq,
15454 &rq_create->u.request.context,
15455 cq->queue_id);
15456 bf_set(lpfc_rq_context_data_size,
15457 &rq_create->u.request.context,
3c603be9 15458 LPFC_NVMET_DATA_BUF_SIZE);
2d7dbc4c
JS
15459 bf_set(lpfc_rq_context_hdr_size,
15460 &rq_create->u.request.context,
15461 LPFC_HDR_BUF_SIZE);
15462 bf_set(lpfc_rq_context_rqe_count_1,
15463 &rq_create->u.request.context,
15464 hrq->entry_count);
15465 bf_set(lpfc_rq_context_rqe_size,
15466 &rq_create->u.request.context,
15467 LPFC_RQE_SIZE_8);
15468 bf_set(lpfc_rq_context_page_size,
15469 &rq_create->u.request.context,
15470 (PAGE_SIZE/SLI4_PAGE_SIZE));
15471 }
15472 rc = 0;
15473 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15474 memset(dmabuf->virt, 0, hw_page_size);
15475 cnt = page_idx + dmabuf->buffer_tag;
15476 rq_create->u.request.page[cnt].addr_lo =
15477 putPaddrLow(dmabuf->phys);
15478 rq_create->u.request.page[cnt].addr_hi =
15479 putPaddrHigh(dmabuf->phys);
15480 rc++;
15481 }
15482 page_idx += rc;
15483
15484 rc = 0;
15485 list_for_each_entry(dmabuf, &drq->page_list, list) {
15486 memset(dmabuf->virt, 0, hw_page_size);
15487 cnt = page_idx + dmabuf->buffer_tag;
15488 rq_create->u.request.page[cnt].addr_lo =
15489 putPaddrLow(dmabuf->phys);
15490 rq_create->u.request.page[cnt].addr_hi =
15491 putPaddrHigh(dmabuf->phys);
15492 rc++;
15493 }
15494 page_idx += rc;
15495
15496 hrq->db_format = LPFC_DB_RING_FORMAT;
15497 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15498 hrq->type = LPFC_HRQ;
15499 hrq->assoc_qid = cq->queue_id;
15500 hrq->subtype = subtype;
15501 hrq->host_index = 0;
15502 hrq->hba_index = 0;
61f3d4bf 15503 hrq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15504
15505 drq->db_format = LPFC_DB_RING_FORMAT;
15506 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15507 drq->type = LPFC_DRQ;
15508 drq->assoc_qid = cq->queue_id;
15509 drq->subtype = subtype;
15510 drq->host_index = 0;
15511 drq->hba_index = 0;
61f3d4bf 15512 drq->entry_repost = LPFC_RQ_REPOST;
2d7dbc4c
JS
15513
15514 list_add_tail(&hrq->list, &cq->child_list);
15515 list_add_tail(&drq->list, &cq->child_list);
15516 }
15517
15518 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15519 /* The IOCTL status is embedded in the mailbox subheader. */
15520 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15521 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15522 if (shdr_status || shdr_add_status || rc) {
15523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15524 "3120 RQ_CREATE mailbox failed with "
15525 "status x%x add_status x%x, mbx status x%x\n",
15526 shdr_status, shdr_add_status, rc);
15527 status = -ENXIO;
15528 goto out;
15529 }
15530 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15531 if (rc == 0xFFFF) {
15532 status = -ENXIO;
15533 goto out;
15534 }
15535
15536 /* Initialize all RQs with associated queue id */
15537 for (idx = 0; idx < numrq; idx++) {
15538 hrq = hrqp[idx];
15539 hrq->queue_id = rc + (2 * idx);
15540 drq = drqp[idx];
15541 drq->queue_id = rc + (2 * idx) + 1;
15542 }
15543
15544out:
15545 lpfc_sli4_mbox_cmd_free(phba, mbox);
15546 return status;
15547}
15548
4f774513
JS
15549/**
15550 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15551 * @eq: The queue structure associated with the queue to destroy.
15552 *
15553 * This function destroys a queue, as detailed in @eq by sending an mailbox
15554 * command, specific to the type of queue, to the HBA.
15555 *
15556 * The @eq struct is used to get the queue ID of the queue to destroy.
15557 *
15558 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15559 * command fails this function will return -ENXIO.
4f774513 15560 **/
a2fc4aef 15561int
4f774513
JS
15562lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15563{
15564 LPFC_MBOXQ_t *mbox;
15565 int rc, length, status = 0;
15566 uint32_t shdr_status, shdr_add_status;
15567 union lpfc_sli4_cfg_shdr *shdr;
15568
2e90f4b5 15569 /* sanity check on queue memory */
4f774513
JS
15570 if (!eq)
15571 return -ENODEV;
15572 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15573 if (!mbox)
15574 return -ENOMEM;
15575 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15576 sizeof(struct lpfc_sli4_cfg_mhdr));
15577 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15578 LPFC_MBOX_OPCODE_EQ_DESTROY,
15579 length, LPFC_SLI4_MBX_EMBED);
15580 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15581 eq->queue_id);
15582 mbox->vport = eq->phba->pport;
15583 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15584
15585 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15586 /* The IOCTL status is embedded in the mailbox subheader. */
15587 shdr = (union lpfc_sli4_cfg_shdr *)
15588 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15589 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15590 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15591 if (shdr_status || shdr_add_status || rc) {
15592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15593 "2505 EQ_DESTROY mailbox failed with "
15594 "status x%x add_status x%x, mbx status x%x\n",
15595 shdr_status, shdr_add_status, rc);
15596 status = -ENXIO;
15597 }
15598
15599 /* Remove eq from any list */
15600 list_del_init(&eq->list);
8fa38513 15601 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15602 return status;
15603}
15604
15605/**
15606 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15607 * @cq: The queue structure associated with the queue to destroy.
15608 *
15609 * This function destroys a queue, as detailed in @cq by sending an mailbox
15610 * command, specific to the type of queue, to the HBA.
15611 *
15612 * The @cq struct is used to get the queue ID of the queue to destroy.
15613 *
15614 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15615 * command fails this function will return -ENXIO.
4f774513 15616 **/
a2fc4aef 15617int
4f774513
JS
15618lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15619{
15620 LPFC_MBOXQ_t *mbox;
15621 int rc, length, status = 0;
15622 uint32_t shdr_status, shdr_add_status;
15623 union lpfc_sli4_cfg_shdr *shdr;
15624
2e90f4b5 15625 /* sanity check on queue memory */
4f774513
JS
15626 if (!cq)
15627 return -ENODEV;
15628 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15629 if (!mbox)
15630 return -ENOMEM;
15631 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15632 sizeof(struct lpfc_sli4_cfg_mhdr));
15633 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15634 LPFC_MBOX_OPCODE_CQ_DESTROY,
15635 length, LPFC_SLI4_MBX_EMBED);
15636 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15637 cq->queue_id);
15638 mbox->vport = cq->phba->pport;
15639 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15640 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15641 /* The IOCTL status is embedded in the mailbox subheader. */
15642 shdr = (union lpfc_sli4_cfg_shdr *)
15643 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15644 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15645 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15646 if (shdr_status || shdr_add_status || rc) {
15647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15648 "2506 CQ_DESTROY mailbox failed with "
15649 "status x%x add_status x%x, mbx status x%x\n",
15650 shdr_status, shdr_add_status, rc);
15651 status = -ENXIO;
15652 }
15653 /* Remove cq from any list */
15654 list_del_init(&cq->list);
8fa38513 15655 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15656 return status;
15657}
15658
04c68496
JS
15659/**
15660 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15661 * @qm: The queue structure associated with the queue to destroy.
15662 *
15663 * This function destroys a queue, as detailed in @mq by sending an mailbox
15664 * command, specific to the type of queue, to the HBA.
15665 *
15666 * The @mq struct is used to get the queue ID of the queue to destroy.
15667 *
15668 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15669 * command fails this function will return -ENXIO.
04c68496 15670 **/
a2fc4aef 15671int
04c68496
JS
15672lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15673{
15674 LPFC_MBOXQ_t *mbox;
15675 int rc, length, status = 0;
15676 uint32_t shdr_status, shdr_add_status;
15677 union lpfc_sli4_cfg_shdr *shdr;
15678
2e90f4b5 15679 /* sanity check on queue memory */
04c68496
JS
15680 if (!mq)
15681 return -ENODEV;
15682 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15683 if (!mbox)
15684 return -ENOMEM;
15685 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15686 sizeof(struct lpfc_sli4_cfg_mhdr));
15687 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15688 LPFC_MBOX_OPCODE_MQ_DESTROY,
15689 length, LPFC_SLI4_MBX_EMBED);
15690 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15691 mq->queue_id);
15692 mbox->vport = mq->phba->pport;
15693 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15694 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15695 /* The IOCTL status is embedded in the mailbox subheader. */
15696 shdr = (union lpfc_sli4_cfg_shdr *)
15697 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15698 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15699 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15700 if (shdr_status || shdr_add_status || rc) {
15701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15702 "2507 MQ_DESTROY mailbox failed with "
15703 "status x%x add_status x%x, mbx status x%x\n",
15704 shdr_status, shdr_add_status, rc);
15705 status = -ENXIO;
15706 }
15707 /* Remove mq from any list */
15708 list_del_init(&mq->list);
8fa38513 15709 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15710 return status;
15711}
15712
4f774513
JS
15713/**
15714 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15715 * @wq: The queue structure associated with the queue to destroy.
15716 *
15717 * This function destroys a queue, as detailed in @wq by sending an mailbox
15718 * command, specific to the type of queue, to the HBA.
15719 *
15720 * The @wq struct is used to get the queue ID of the queue to destroy.
15721 *
15722 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15723 * command fails this function will return -ENXIO.
4f774513 15724 **/
a2fc4aef 15725int
4f774513
JS
15726lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15727{
15728 LPFC_MBOXQ_t *mbox;
15729 int rc, length, status = 0;
15730 uint32_t shdr_status, shdr_add_status;
15731 union lpfc_sli4_cfg_shdr *shdr;
15732
2e90f4b5 15733 /* sanity check on queue memory */
4f774513
JS
15734 if (!wq)
15735 return -ENODEV;
15736 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15737 if (!mbox)
15738 return -ENOMEM;
15739 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15740 sizeof(struct lpfc_sli4_cfg_mhdr));
15741 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15742 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15743 length, LPFC_SLI4_MBX_EMBED);
15744 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15745 wq->queue_id);
15746 mbox->vport = wq->phba->pport;
15747 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15748 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15749 shdr = (union lpfc_sli4_cfg_shdr *)
15750 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15751 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15752 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15753 if (shdr_status || shdr_add_status || rc) {
15754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15755 "2508 WQ_DESTROY mailbox failed with "
15756 "status x%x add_status x%x, mbx status x%x\n",
15757 shdr_status, shdr_add_status, rc);
15758 status = -ENXIO;
15759 }
15760 /* Remove wq from any list */
15761 list_del_init(&wq->list);
d1f525aa
JS
15762 kfree(wq->pring);
15763 wq->pring = NULL;
8fa38513 15764 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
15765 return status;
15766}
15767
15768/**
15769 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15770 * @rq: The queue structure associated with the queue to destroy.
15771 *
15772 * This function destroys a queue, as detailed in @rq by sending an mailbox
15773 * command, specific to the type of queue, to the HBA.
15774 *
15775 * The @rq struct is used to get the queue ID of the queue to destroy.
15776 *
15777 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15778 * command fails this function will return -ENXIO.
4f774513 15779 **/
a2fc4aef 15780int
4f774513
JS
15781lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15782 struct lpfc_queue *drq)
15783{
15784 LPFC_MBOXQ_t *mbox;
15785 int rc, length, status = 0;
15786 uint32_t shdr_status, shdr_add_status;
15787 union lpfc_sli4_cfg_shdr *shdr;
15788
2e90f4b5 15789 /* sanity check on queue memory */
4f774513
JS
15790 if (!hrq || !drq)
15791 return -ENODEV;
15792 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15793 if (!mbox)
15794 return -ENOMEM;
15795 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 15796 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
15797 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15798 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15799 length, LPFC_SLI4_MBX_EMBED);
15800 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15801 hrq->queue_id);
15802 mbox->vport = hrq->phba->pport;
15803 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15804 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15805 /* The IOCTL status is embedded in the mailbox subheader. */
15806 shdr = (union lpfc_sli4_cfg_shdr *)
15807 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15808 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15810 if (shdr_status || shdr_add_status || rc) {
15811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15812 "2509 RQ_DESTROY mailbox failed with "
15813 "status x%x add_status x%x, mbx status x%x\n",
15814 shdr_status, shdr_add_status, rc);
15815 if (rc != MBX_TIMEOUT)
15816 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15817 return -ENXIO;
15818 }
15819 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15820 drq->queue_id);
15821 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15822 shdr = (union lpfc_sli4_cfg_shdr *)
15823 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15824 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15825 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15826 if (shdr_status || shdr_add_status || rc) {
15827 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15828 "2510 RQ_DESTROY mailbox failed with "
15829 "status x%x add_status x%x, mbx status x%x\n",
15830 shdr_status, shdr_add_status, rc);
15831 status = -ENXIO;
15832 }
15833 list_del_init(&hrq->list);
15834 list_del_init(&drq->list);
8fa38513 15835 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
15836 return status;
15837}
15838
15839/**
15840 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15841 * @phba: The virtual port for which this call being executed.
15842 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15843 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15844 * @xritag: the xritag that ties this io to the SGL pages.
15845 *
15846 * This routine will post the sgl pages for the IO that has the xritag
15847 * that is in the iocbq structure. The xritag is assigned during iocbq
15848 * creation and persists for as long as the driver is loaded.
15849 * if the caller has fewer than 256 scatter gather segments to map then
15850 * pdma_phys_addr1 should be 0.
15851 * If the caller needs to map more than 256 scatter gather segment then
15852 * pdma_phys_addr1 should be a valid physical address.
15853 * physical address for SGLs must be 64 byte aligned.
15854 * If you are going to map 2 SGL's then the first one must have 256 entries
15855 * the second sgl can have between 1 and 256 entries.
15856 *
15857 * Return codes:
15858 * 0 - Success
15859 * -ENXIO, -ENOMEM - Failure
15860 **/
15861int
15862lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15863 dma_addr_t pdma_phys_addr0,
15864 dma_addr_t pdma_phys_addr1,
15865 uint16_t xritag)
15866{
15867 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15868 LPFC_MBOXQ_t *mbox;
15869 int rc;
15870 uint32_t shdr_status, shdr_add_status;
6d368e53 15871 uint32_t mbox_tmo;
4f774513
JS
15872 union lpfc_sli4_cfg_shdr *shdr;
15873
15874 if (xritag == NO_XRI) {
15875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15876 "0364 Invalid param:\n");
15877 return -EINVAL;
15878 }
15879
15880 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15881 if (!mbox)
15882 return -ENOMEM;
15883
15884 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15885 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15886 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 15887 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
15888
15889 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15890 &mbox->u.mqe.un.post_sgl_pages;
15891 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15892 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15893
15894 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15895 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15896 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15897 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15898
15899 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15900 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15901 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15902 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15903 if (!phba->sli4_hba.intr_enable)
15904 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 15905 else {
a183a15f 15906 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
15907 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15908 }
4f774513
JS
15909 /* The IOCTL status is embedded in the mailbox subheader. */
15910 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15911 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15912 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15913 if (rc != MBX_TIMEOUT)
15914 mempool_free(mbox, phba->mbox_mem_pool);
15915 if (shdr_status || shdr_add_status || rc) {
15916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15917 "2511 POST_SGL mailbox failed with "
15918 "status x%x add_status x%x, mbx status x%x\n",
15919 shdr_status, shdr_add_status, rc);
4f774513
JS
15920 }
15921 return 0;
15922}
4f774513 15923
6d368e53 15924/**
88a2cfbb 15925 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
15926 * @phba: pointer to lpfc hba data structure.
15927 *
15928 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
15929 * HBA consistent with the SLI-4 interface spec. This routine
15930 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15931 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 15932 *
88a2cfbb
JS
15933 * Returns
15934 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15935 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15936 **/
5d8b8167 15937static uint16_t
6d368e53
JS
15938lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15939{
15940 unsigned long xri;
15941
15942 /*
15943 * Fetch the next logical xri. Because this index is logical,
15944 * the driver starts at 0 each time.
15945 */
15946 spin_lock_irq(&phba->hbalock);
15947 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15948 phba->sli4_hba.max_cfg_param.max_xri, 0);
15949 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15950 spin_unlock_irq(&phba->hbalock);
15951 return NO_XRI;
15952 } else {
15953 set_bit(xri, phba->sli4_hba.xri_bmask);
15954 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 15955 }
6d368e53
JS
15956 spin_unlock_irq(&phba->hbalock);
15957 return xri;
15958}
15959
15960/**
15961 * lpfc_sli4_free_xri - Release an xri for reuse.
15962 * @phba: pointer to lpfc hba data structure.
15963 *
15964 * This routine is invoked to release an xri to the pool of
15965 * available rpis maintained by the driver.
15966 **/
5d8b8167 15967static void
6d368e53
JS
15968__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15969{
15970 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
15971 phba->sli4_hba.max_cfg_param.xri_used--;
15972 }
15973}
15974
15975/**
15976 * lpfc_sli4_free_xri - Release an xri for reuse.
15977 * @phba: pointer to lpfc hba data structure.
15978 *
15979 * This routine is invoked to release an xri to the pool of
15980 * available rpis maintained by the driver.
15981 **/
15982void
15983lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15984{
15985 spin_lock_irq(&phba->hbalock);
15986 __lpfc_sli4_free_xri(phba, xri);
15987 spin_unlock_irq(&phba->hbalock);
15988}
15989
4f774513
JS
15990/**
15991 * lpfc_sli4_next_xritag - Get an xritag for the io
15992 * @phba: Pointer to HBA context object.
15993 *
15994 * This function gets an xritag for the iocb. If there is no unused xritag
15995 * it will return 0xffff.
15996 * The function returns the allocated xritag if successful, else returns zero.
15997 * Zero is not a valid xritag.
15998 * The caller is not required to hold any lock.
15999 **/
16000uint16_t
16001lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16002{
6d368e53 16003 uint16_t xri_index;
4f774513 16004
6d368e53 16005 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
16006 if (xri_index == NO_XRI)
16007 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16008 "2004 Failed to allocate XRI.last XRITAG is %d"
16009 " Max XRI is %d, Used XRI is %d\n",
16010 xri_index,
16011 phba->sli4_hba.max_cfg_param.max_xri,
16012 phba->sli4_hba.max_cfg_param.xri_used);
16013 return xri_index;
4f774513
JS
16014}
16015
16016/**
895427bd 16017 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 16018 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
16019 * @post_sgl_list: pointer to els sgl entry list.
16020 * @count: number of els sgl entries on the list.
4f774513
JS
16021 *
16022 * This routine is invoked to post a block of driver's sgl pages to the
16023 * HBA using non-embedded mailbox command. No Lock is held. This routine
16024 * is only called when the driver is loading and after all IO has been
16025 * stopped.
16026 **/
8a9d2e80 16027static int
895427bd 16028lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
16029 struct list_head *post_sgl_list,
16030 int post_cnt)
4f774513 16031{
8a9d2e80 16032 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
16033 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16034 struct sgl_page_pairs *sgl_pg_pairs;
16035 void *viraddr;
16036 LPFC_MBOXQ_t *mbox;
16037 uint32_t reqlen, alloclen, pg_pairs;
16038 uint32_t mbox_tmo;
8a9d2e80
JS
16039 uint16_t xritag_start = 0;
16040 int rc = 0;
4f774513
JS
16041 uint32_t shdr_status, shdr_add_status;
16042 union lpfc_sli4_cfg_shdr *shdr;
16043
895427bd 16044 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 16045 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16046 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 16047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
16048 "2559 Block sgl registration required DMA "
16049 "size (%d) great than a page\n", reqlen);
16050 return -ENOMEM;
16051 }
895427bd 16052
4f774513 16053 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 16054 if (!mbox)
4f774513 16055 return -ENOMEM;
4f774513
JS
16056
16057 /* Allocate DMA memory and set up the non-embedded mailbox command */
16058 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16059 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16060 LPFC_SLI4_MBX_NEMBED);
16061
16062 if (alloclen < reqlen) {
16063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16064 "0285 Allocated DMA memory size (%d) is "
16065 "less than the requested DMA memory "
16066 "size (%d)\n", alloclen, reqlen);
16067 lpfc_sli4_mbox_cmd_free(phba, mbox);
16068 return -ENOMEM;
16069 }
4f774513 16070 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 16071 viraddr = mbox->sge_array->addr[0];
4f774513
JS
16072 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16073 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16074
8a9d2e80
JS
16075 pg_pairs = 0;
16076 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
16077 /* Set up the sge entry */
16078 sgl_pg_pairs->sgl_pg0_addr_lo =
16079 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16080 sgl_pg_pairs->sgl_pg0_addr_hi =
16081 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16082 sgl_pg_pairs->sgl_pg1_addr_lo =
16083 cpu_to_le32(putPaddrLow(0));
16084 sgl_pg_pairs->sgl_pg1_addr_hi =
16085 cpu_to_le32(putPaddrHigh(0));
6d368e53 16086
4f774513
JS
16087 /* Keep the first xritag on the list */
16088 if (pg_pairs == 0)
16089 xritag_start = sglq_entry->sli4_xritag;
16090 sgl_pg_pairs++;
8a9d2e80 16091 pg_pairs++;
4f774513 16092 }
6d368e53
JS
16093
16094 /* Complete initialization and perform endian conversion. */
4f774513 16095 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 16096 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 16097 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 16098
4f774513
JS
16099 if (!phba->sli4_hba.intr_enable)
16100 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16101 else {
a183a15f 16102 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16103 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16104 }
16105 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16106 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16107 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16108 if (rc != MBX_TIMEOUT)
16109 lpfc_sli4_mbox_cmd_free(phba, mbox);
16110 if (shdr_status || shdr_add_status || rc) {
16111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16112 "2513 POST_SGL_BLOCK mailbox command failed "
16113 "status x%x add_status x%x mbx status x%x\n",
16114 shdr_status, shdr_add_status, rc);
16115 rc = -ENXIO;
16116 }
16117 return rc;
16118}
16119
16120/**
16121 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16122 * @phba: pointer to lpfc hba data structure.
16123 * @sblist: pointer to scsi buffer list.
16124 * @count: number of scsi buffers on the list.
16125 *
16126 * This routine is invoked to post a block of @count scsi sgl pages from a
16127 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16128 * No Lock is held.
16129 *
16130 **/
16131int
8a9d2e80
JS
16132lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16133 struct list_head *sblist,
16134 int count)
4f774513
JS
16135{
16136 struct lpfc_scsi_buf *psb;
16137 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16138 struct sgl_page_pairs *sgl_pg_pairs;
16139 void *viraddr;
16140 LPFC_MBOXQ_t *mbox;
16141 uint32_t reqlen, alloclen, pg_pairs;
16142 uint32_t mbox_tmo;
16143 uint16_t xritag_start = 0;
16144 int rc = 0;
16145 uint32_t shdr_status, shdr_add_status;
16146 dma_addr_t pdma_phys_bpl1;
16147 union lpfc_sli4_cfg_shdr *shdr;
16148
16149 /* Calculate the requested length of the dma memory */
8a9d2e80 16150 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 16151 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 16152 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
16153 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16154 "0217 Block sgl registration required DMA "
16155 "size (%d) great than a page\n", reqlen);
16156 return -ENOMEM;
16157 }
16158 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16159 if (!mbox) {
16160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16161 "0283 Failed to allocate mbox cmd memory\n");
16162 return -ENOMEM;
16163 }
16164
16165 /* Allocate DMA memory and set up the non-embedded mailbox command */
16166 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16167 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16168 LPFC_SLI4_MBX_NEMBED);
16169
16170 if (alloclen < reqlen) {
16171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16172 "2561 Allocated DMA memory size (%d) is "
16173 "less than the requested DMA memory "
16174 "size (%d)\n", alloclen, reqlen);
16175 lpfc_sli4_mbox_cmd_free(phba, mbox);
16176 return -ENOMEM;
16177 }
6d368e53 16178
4f774513 16179 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
16180 viraddr = mbox->sge_array->addr[0];
16181
16182 /* Set up the SGL pages in the non-embedded DMA pages */
16183 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16184 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16185
16186 pg_pairs = 0;
16187 list_for_each_entry(psb, sblist, list) {
16188 /* Set up the sge entry */
16189 sgl_pg_pairs->sgl_pg0_addr_lo =
16190 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16191 sgl_pg_pairs->sgl_pg0_addr_hi =
16192 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16193 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16194 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16195 else
16196 pdma_phys_bpl1 = 0;
16197 sgl_pg_pairs->sgl_pg1_addr_lo =
16198 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16199 sgl_pg_pairs->sgl_pg1_addr_hi =
16200 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16201 /* Keep the first xritag on the list */
16202 if (pg_pairs == 0)
16203 xritag_start = psb->cur_iocbq.sli4_xritag;
16204 sgl_pg_pairs++;
16205 pg_pairs++;
16206 }
16207 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16208 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16209 /* Perform endian conversion if necessary */
16210 sgl->word0 = cpu_to_le32(sgl->word0);
16211
16212 if (!phba->sli4_hba.intr_enable)
16213 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16214 else {
a183a15f 16215 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
16216 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16217 }
16218 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16219 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16220 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16221 if (rc != MBX_TIMEOUT)
16222 lpfc_sli4_mbox_cmd_free(phba, mbox);
16223 if (shdr_status || shdr_add_status || rc) {
16224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16225 "2564 POST_SGL_BLOCK mailbox command failed "
16226 "status x%x add_status x%x mbx status x%x\n",
16227 shdr_status, shdr_add_status, rc);
16228 rc = -ENXIO;
16229 }
16230 return rc;
16231}
16232
16233/**
16234 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16235 * @phba: pointer to lpfc_hba struct that the frame was received on
16236 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16237 *
16238 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16239 * valid type of frame that the LPFC driver will handle. This function will
16240 * return a zero if the frame is a valid frame or a non zero value when the
16241 * frame does not pass the check.
16242 **/
16243static int
16244lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16245{
474ffb74 16246 /* make rctl_names static to save stack space */
4f774513 16247 struct fc_vft_header *fc_vft_hdr;
546fc854 16248 uint32_t *header = (uint32_t *) fc_hdr;
4f774513 16249
ae9e28f3
JS
16250#define FC_RCTL_MDS_DIAGS 0xF4
16251
4f774513
JS
16252 switch (fc_hdr->fh_r_ctl) {
16253 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16254 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16255 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16256 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16257 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16258 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16259 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16260 case FC_RCTL_DD_CMD_STATUS: /* command status */
16261 case FC_RCTL_ELS_REQ: /* extended link services request */
16262 case FC_RCTL_ELS_REP: /* extended link services reply */
16263 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16264 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16265 case FC_RCTL_BA_NOP: /* basic link service NOP */
16266 case FC_RCTL_BA_ABTS: /* basic link service abort */
16267 case FC_RCTL_BA_RMC: /* remove connection */
16268 case FC_RCTL_BA_ACC: /* basic accept */
16269 case FC_RCTL_BA_RJT: /* basic reject */
16270 case FC_RCTL_BA_PRMT:
16271 case FC_RCTL_ACK_1: /* acknowledge_1 */
16272 case FC_RCTL_ACK_0: /* acknowledge_0 */
16273 case FC_RCTL_P_RJT: /* port reject */
16274 case FC_RCTL_F_RJT: /* fabric reject */
16275 case FC_RCTL_P_BSY: /* port busy */
16276 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16277 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16278 case FC_RCTL_LCR: /* link credit reset */
ae9e28f3 16279 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
4f774513
JS
16280 case FC_RCTL_END: /* end */
16281 break;
16282 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16283 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16284 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16285 return lpfc_fc_frame_check(phba, fc_hdr);
16286 default:
16287 goto drop;
16288 }
ae9e28f3
JS
16289
16290#define FC_TYPE_VENDOR_UNIQUE 0xFF
16291
4f774513
JS
16292 switch (fc_hdr->fh_type) {
16293 case FC_TYPE_BLS:
16294 case FC_TYPE_ELS:
16295 case FC_TYPE_FCP:
16296 case FC_TYPE_CT:
895427bd 16297 case FC_TYPE_NVME:
ae9e28f3 16298 case FC_TYPE_VENDOR_UNIQUE:
4f774513
JS
16299 break;
16300 case FC_TYPE_IP:
16301 case FC_TYPE_ILS:
16302 default:
16303 goto drop;
16304 }
546fc854 16305
4f774513 16306 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
78e1d200 16307 "2538 Received frame rctl:x%x, type:x%x, "
88f43a08 16308 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
78e1d200
JS
16309 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16310 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16311 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16312 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16313 be32_to_cpu(header[6]));
4f774513
JS
16314 return 0;
16315drop:
16316 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
78e1d200
JS
16317 "2539 Dropped frame rctl:x%x type:x%x\n",
16318 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
4f774513
JS
16319 return 1;
16320}
16321
16322/**
16323 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16324 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16325 *
16326 * This function processes the FC header to retrieve the VFI from the VF
16327 * header, if one exists. This function will return the VFI if one exists
16328 * or 0 if no VSAN Header exists.
16329 **/
16330static uint32_t
16331lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16332{
16333 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16334
16335 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16336 return 0;
16337 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16338}
16339
16340/**
16341 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16342 * @phba: Pointer to the HBA structure to search for the vport on
16343 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16344 * @fcfi: The FC Fabric ID that the frame came from
16345 *
16346 * This function searches the @phba for a vport that matches the content of the
16347 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16348 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16349 * returns the matching vport pointer or NULL if unable to match frame to a
16350 * vport.
16351 **/
16352static struct lpfc_vport *
16353lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16354 uint16_t fcfi, uint32_t did)
4f774513
JS
16355{
16356 struct lpfc_vport **vports;
16357 struct lpfc_vport *vport = NULL;
16358 int i;
939723a4 16359
bf08611b
JS
16360 if (did == Fabric_DID)
16361 return phba->pport;
939723a4
JS
16362 if ((phba->pport->fc_flag & FC_PT2PT) &&
16363 !(phba->link_state == LPFC_HBA_READY))
16364 return phba->pport;
16365
4f774513 16366 vports = lpfc_create_vport_work_array(phba);
895427bd 16367 if (vports != NULL) {
4f774513
JS
16368 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16369 if (phba->fcf.fcfi == fcfi &&
16370 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16371 vports[i]->fc_myDID == did) {
16372 vport = vports[i];
16373 break;
16374 }
16375 }
895427bd 16376 }
4f774513
JS
16377 lpfc_destroy_vport_work_array(phba, vports);
16378 return vport;
16379}
16380
45ed1190
JS
16381/**
16382 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16383 * @vport: The vport to work on.
16384 *
16385 * This function updates the receive sequence time stamp for this vport. The
16386 * receive sequence time stamp indicates the time that the last frame of the
16387 * the sequence that has been idle for the longest amount of time was received.
16388 * the driver uses this time stamp to indicate if any received sequences have
16389 * timed out.
16390 **/
5d8b8167 16391static void
45ed1190
JS
16392lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16393{
16394 struct lpfc_dmabuf *h_buf;
16395 struct hbq_dmabuf *dmabuf = NULL;
16396
16397 /* get the oldest sequence on the rcv list */
16398 h_buf = list_get_first(&vport->rcv_buffer_list,
16399 struct lpfc_dmabuf, list);
16400 if (!h_buf)
16401 return;
16402 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16403 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16404}
16405
16406/**
16407 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16408 * @vport: The vport that the received sequences were sent to.
16409 *
16410 * This function cleans up all outstanding received sequences. This is called
16411 * by the driver when a link event or user action invalidates all the received
16412 * sequences.
16413 **/
16414void
16415lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16416{
16417 struct lpfc_dmabuf *h_buf, *hnext;
16418 struct lpfc_dmabuf *d_buf, *dnext;
16419 struct hbq_dmabuf *dmabuf = NULL;
16420
16421 /* start with the oldest sequence on the rcv list */
16422 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16423 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16424 list_del_init(&dmabuf->hbuf.list);
16425 list_for_each_entry_safe(d_buf, dnext,
16426 &dmabuf->dbuf.list, list) {
16427 list_del_init(&d_buf->list);
16428 lpfc_in_buf_free(vport->phba, d_buf);
16429 }
16430 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16431 }
16432}
16433
16434/**
16435 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16436 * @vport: The vport that the received sequences were sent to.
16437 *
16438 * This function determines whether any received sequences have timed out by
16439 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16440 * indicates that there is at least one timed out sequence this routine will
16441 * go through the received sequences one at a time from most inactive to most
16442 * active to determine which ones need to be cleaned up. Once it has determined
16443 * that a sequence needs to be cleaned up it will simply free up the resources
16444 * without sending an abort.
16445 **/
16446void
16447lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16448{
16449 struct lpfc_dmabuf *h_buf, *hnext;
16450 struct lpfc_dmabuf *d_buf, *dnext;
16451 struct hbq_dmabuf *dmabuf = NULL;
16452 unsigned long timeout;
16453 int abort_count = 0;
16454
16455 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16456 vport->rcv_buffer_time_stamp);
16457 if (list_empty(&vport->rcv_buffer_list) ||
16458 time_before(jiffies, timeout))
16459 return;
16460 /* start with the oldest sequence on the rcv list */
16461 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16462 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16463 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16464 dmabuf->time_stamp);
16465 if (time_before(jiffies, timeout))
16466 break;
16467 abort_count++;
16468 list_del_init(&dmabuf->hbuf.list);
16469 list_for_each_entry_safe(d_buf, dnext,
16470 &dmabuf->dbuf.list, list) {
16471 list_del_init(&d_buf->list);
16472 lpfc_in_buf_free(vport->phba, d_buf);
16473 }
16474 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16475 }
16476 if (abort_count)
16477 lpfc_update_rcv_time_stamp(vport);
16478}
16479
4f774513
JS
16480/**
16481 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16482 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16483 *
16484 * This function searches through the existing incomplete sequences that have
16485 * been sent to this @vport. If the frame matches one of the incomplete
16486 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16487 * make up that sequence. If no sequence is found that matches this frame then
16488 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16489 * This function returns a pointer to the first dmabuf in the sequence list that
16490 * the frame was linked to.
16491 **/
16492static struct hbq_dmabuf *
16493lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16494{
16495 struct fc_frame_header *new_hdr;
16496 struct fc_frame_header *temp_hdr;
16497 struct lpfc_dmabuf *d_buf;
16498 struct lpfc_dmabuf *h_buf;
16499 struct hbq_dmabuf *seq_dmabuf = NULL;
16500 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16501 uint8_t found = 0;
4f774513 16502
4d9ab994 16503 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16504 dmabuf->time_stamp = jiffies;
4f774513 16505 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16506
4f774513
JS
16507 /* Use the hdr_buf to find the sequence that this frame belongs to */
16508 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16509 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16510 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16511 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16512 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16513 continue;
16514 /* found a pending sequence that matches this frame */
16515 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16516 break;
16517 }
16518 if (!seq_dmabuf) {
16519 /*
16520 * This indicates first frame received for this sequence.
16521 * Queue the buffer on the vport's rcv_buffer_list.
16522 */
16523 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16524 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16525 return dmabuf;
16526 }
16527 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16528 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16529 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16530 list_del_init(&seq_dmabuf->hbuf.list);
16531 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16532 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16533 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16534 return dmabuf;
16535 }
45ed1190
JS
16536 /* move this sequence to the tail to indicate a young sequence */
16537 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16538 seq_dmabuf->time_stamp = jiffies;
16539 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16540 if (list_empty(&seq_dmabuf->dbuf.list)) {
16541 temp_hdr = dmabuf->hbuf.virt;
16542 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16543 return seq_dmabuf;
16544 }
4f774513 16545 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16546 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16547 while (!found) {
4f774513
JS
16548 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16549 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16550 /*
16551 * If the frame's sequence count is greater than the frame on
16552 * the list then insert the frame right after this frame
16553 */
eeead811
JS
16554 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16555 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16556 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16557 found = 1;
16558 break;
4f774513 16559 }
4360ca9c
JS
16560
16561 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16562 break;
16563 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16564 }
4360ca9c
JS
16565
16566 if (found)
16567 return seq_dmabuf;
4f774513
JS
16568 return NULL;
16569}
16570
6669f9bb
JS
16571/**
16572 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16573 * @vport: pointer to a vitural port
16574 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16575 *
16576 * This function tries to abort from the partially assembed sequence, described
16577 * by the information from basic abbort @dmabuf. It checks to see whether such
16578 * partially assembled sequence held by the driver. If so, it shall free up all
16579 * the frames from the partially assembled sequence.
16580 *
16581 * Return
16582 * true -- if there is matching partially assembled sequence present and all
16583 * the frames freed with the sequence;
16584 * false -- if there is no matching partially assembled sequence present so
16585 * nothing got aborted in the lower layer driver
16586 **/
16587static bool
16588lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16589 struct hbq_dmabuf *dmabuf)
16590{
16591 struct fc_frame_header *new_hdr;
16592 struct fc_frame_header *temp_hdr;
16593 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16594 struct hbq_dmabuf *seq_dmabuf = NULL;
16595
16596 /* Use the hdr_buf to find the sequence that matches this frame */
16597 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16598 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16599 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16600 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16601 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16602 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16603 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16604 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16605 continue;
16606 /* found a pending sequence that matches this frame */
16607 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16608 break;
16609 }
16610
16611 /* Free up all the frames from the partially assembled sequence */
16612 if (seq_dmabuf) {
16613 list_for_each_entry_safe(d_buf, n_buf,
16614 &seq_dmabuf->dbuf.list, list) {
16615 list_del_init(&d_buf->list);
16616 lpfc_in_buf_free(vport->phba, d_buf);
16617 }
16618 return true;
16619 }
16620 return false;
16621}
16622
6dd9e31c
JS
16623/**
16624 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16625 * @vport: pointer to a vitural port
16626 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16627 *
16628 * This function tries to abort from the assembed sequence from upper level
16629 * protocol, described by the information from basic abbort @dmabuf. It
16630 * checks to see whether such pending context exists at upper level protocol.
16631 * If so, it shall clean up the pending context.
16632 *
16633 * Return
16634 * true -- if there is matching pending context of the sequence cleaned
16635 * at ulp;
16636 * false -- if there is no matching pending context of the sequence present
16637 * at ulp.
16638 **/
16639static bool
16640lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16641{
16642 struct lpfc_hba *phba = vport->phba;
16643 int handled;
16644
16645 /* Accepting abort at ulp with SLI4 only */
16646 if (phba->sli_rev < LPFC_SLI_REV4)
16647 return false;
16648
16649 /* Register all caring upper level protocols to attend abort */
16650 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16651 if (handled)
16652 return true;
16653
16654 return false;
16655}
16656
6669f9bb 16657/**
546fc854 16658 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16659 * @phba: Pointer to HBA context object.
16660 * @cmd_iocbq: pointer to the command iocbq structure.
16661 * @rsp_iocbq: pointer to the response iocbq structure.
16662 *
546fc854 16663 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16664 * event. It properly releases the memory allocated to the sequence abort
16665 * accept iocb.
16666 **/
16667static void
546fc854 16668lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16669 struct lpfc_iocbq *cmd_iocbq,
16670 struct lpfc_iocbq *rsp_iocbq)
16671{
6dd9e31c
JS
16672 struct lpfc_nodelist *ndlp;
16673
16674 if (cmd_iocbq) {
16675 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16676 lpfc_nlp_put(ndlp);
16677 lpfc_nlp_not_used(ndlp);
6669f9bb 16678 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16679 }
6b5151fd
JS
16680
16681 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16682 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16684 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16685 rsp_iocbq->iocb.ulpStatus,
16686 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16687}
16688
6d368e53
JS
16689/**
16690 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16691 * @phba: Pointer to HBA context object.
16692 * @xri: xri id in transaction.
16693 *
16694 * This function validates the xri maps to the known range of XRIs allocated an
16695 * used by the driver.
16696 **/
7851fe2c 16697uint16_t
6d368e53
JS
16698lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16699 uint16_t xri)
16700{
a2fc4aef 16701 uint16_t i;
6d368e53
JS
16702
16703 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16704 if (xri == phba->sli4_hba.xri_ids[i])
16705 return i;
16706 }
16707 return NO_XRI;
16708}
16709
6669f9bb 16710/**
546fc854 16711 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16712 * @phba: Pointer to HBA context object.
16713 * @fc_hdr: pointer to a FC frame header.
16714 *
546fc854 16715 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16716 * event after aborting the sequence handling.
16717 **/
86c67379 16718void
6dd9e31c
JS
16719lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16720 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16721{
6dd9e31c 16722 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
16723 struct lpfc_iocbq *ctiocb = NULL;
16724 struct lpfc_nodelist *ndlp;
ee0f4fe1 16725 uint16_t oxid, rxid, xri, lxri;
5ffc266e 16726 uint32_t sid, fctl;
6669f9bb 16727 IOCB_t *icmd;
546fc854 16728 int rc;
6669f9bb
JS
16729
16730 if (!lpfc_is_link_up(phba))
16731 return;
16732
16733 sid = sli4_sid_from_fc_hdr(fc_hdr);
16734 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 16735 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 16736
6dd9e31c 16737 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 16738 if (!ndlp) {
9d3d340d 16739 ndlp = lpfc_nlp_init(vport, sid);
6dd9e31c
JS
16740 if (!ndlp) {
16741 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16742 "1268 Failed to allocate ndlp for "
16743 "oxid:x%x SID:x%x\n", oxid, sid);
16744 return;
16745 }
6dd9e31c
JS
16746 /* Put ndlp onto pport node list */
16747 lpfc_enqueue_node(vport, ndlp);
16748 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16749 /* re-setup ndlp without removing from node list */
16750 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16751 if (!ndlp) {
16752 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16753 "3275 Failed to active ndlp found "
16754 "for oxid:x%x SID:x%x\n", oxid, sid);
16755 return;
16756 }
6669f9bb
JS
16757 }
16758
546fc854 16759 /* Allocate buffer for rsp iocb */
6669f9bb
JS
16760 ctiocb = lpfc_sli_get_iocbq(phba);
16761 if (!ctiocb)
16762 return;
16763
5ffc266e
JS
16764 /* Extract the F_CTL field from FC_HDR */
16765 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16766
6669f9bb 16767 icmd = &ctiocb->iocb;
6669f9bb 16768 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 16769 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
16770 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16771 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16772 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16773
16774 /* Fill in the rest of iocb fields */
16775 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16776 icmd->ulpBdeCount = 0;
16777 icmd->ulpLe = 1;
16778 icmd->ulpClass = CLASS3;
6d368e53 16779 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 16780 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 16781
6669f9bb
JS
16782 ctiocb->iocb_cmpl = NULL;
16783 ctiocb->vport = phba->pport;
546fc854 16784 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 16785 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
16786 ctiocb->sli4_xritag = NO_XRI;
16787
ee0f4fe1
JS
16788 if (fctl & FC_FC_EX_CTX)
16789 /* Exchange responder sent the abort so we
16790 * own the oxid.
16791 */
16792 xri = oxid;
16793 else
16794 xri = rxid;
16795 lxri = lpfc_sli4_xri_inrange(phba, xri);
16796 if (lxri != NO_XRI)
16797 lpfc_set_rrq_active(phba, ndlp, lxri,
16798 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
16799 /* For BA_ABTS from exchange responder, if the logical xri with
16800 * the oxid maps to the FCP XRI range, the port no longer has
16801 * that exchange context, send a BLS_RJT. Override the IOCB for
16802 * a BA_RJT.
16803 */
16804 if ((fctl & FC_FC_EX_CTX) &&
895427bd 16805 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
16806 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16807 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16808 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16809 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16810 }
16811
16812 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16813 * the driver no longer has that exchange, send a BLS_RJT. Override
16814 * the IOCB for a BA_RJT.
546fc854 16815 */
6dd9e31c 16816 if (aborted == false) {
546fc854
JS
16817 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16818 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16819 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16820 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16821 }
6669f9bb 16822
5ffc266e
JS
16823 if (fctl & FC_FC_EX_CTX) {
16824 /* ABTS sent by responder to CT exchange, construction
16825 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16826 * field and RX_ID from ABTS for RX_ID field.
16827 */
546fc854 16828 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
16829 } else {
16830 /* ABTS sent by initiator to CT exchange, construction
16831 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 16832 * XRI_TAG field.
5ffc266e 16833 */
546fc854 16834 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 16835 }
f09c3acc 16836 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 16837 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 16838
546fc854 16839 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
16840 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16841 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16842 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
16843
16844 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16845 if (rc == IOCB_ERROR) {
6dd9e31c
JS
16846 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16847 "2925 Failed to issue CT ABTS RSP x%x on "
16848 "xri x%x, Data x%x\n",
16849 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16850 phba->link_state);
16851 lpfc_nlp_put(ndlp);
16852 ctiocb->context1 = NULL;
546fc854
JS
16853 lpfc_sli_release_iocbq(phba, ctiocb);
16854 }
6669f9bb
JS
16855}
16856
16857/**
16858 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16859 * @vport: Pointer to the vport on which this sequence was received
16860 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16861 *
16862 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16863 * receive sequence is only partially assembed by the driver, it shall abort
16864 * the partially assembled frames for the sequence. Otherwise, if the
16865 * unsolicited receive sequence has been completely assembled and passed to
16866 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16867 * unsolicited sequence has been aborted. After that, it will issue a basic
16868 * accept to accept the abort.
16869 **/
5d8b8167 16870static void
6669f9bb
JS
16871lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16872 struct hbq_dmabuf *dmabuf)
16873{
16874 struct lpfc_hba *phba = vport->phba;
16875 struct fc_frame_header fc_hdr;
5ffc266e 16876 uint32_t fctl;
6dd9e31c 16877 bool aborted;
6669f9bb 16878
6669f9bb
JS
16879 /* Make a copy of fc_hdr before the dmabuf being released */
16880 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 16881 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 16882
5ffc266e 16883 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
16884 /* ABTS by responder to exchange, no cleanup needed */
16885 aborted = true;
5ffc266e 16886 } else {
6dd9e31c
JS
16887 /* ABTS by initiator to exchange, need to do cleanup */
16888 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16889 if (aborted == false)
16890 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 16891 }
6dd9e31c
JS
16892 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16893
86c67379
JS
16894 if (phba->nvmet_support) {
16895 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16896 return;
16897 }
16898
6dd9e31c
JS
16899 /* Respond with BA_ACC or BA_RJT accordingly */
16900 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
16901}
16902
4f774513
JS
16903/**
16904 * lpfc_seq_complete - Indicates if a sequence is complete
16905 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16906 *
16907 * This function checks the sequence, starting with the frame described by
16908 * @dmabuf, to see if all the frames associated with this sequence are present.
16909 * the frames associated with this sequence are linked to the @dmabuf using the
16910 * dbuf list. This function looks for two major things. 1) That the first frame
16911 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16912 * set. 3) That there are no holes in the sequence count. The function will
16913 * return 1 when the sequence is complete, otherwise it will return 0.
16914 **/
16915static int
16916lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16917{
16918 struct fc_frame_header *hdr;
16919 struct lpfc_dmabuf *d_buf;
16920 struct hbq_dmabuf *seq_dmabuf;
16921 uint32_t fctl;
16922 int seq_count = 0;
16923
16924 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16925 /* make sure first fame of sequence has a sequence count of zero */
16926 if (hdr->fh_seq_cnt != seq_count)
16927 return 0;
16928 fctl = (hdr->fh_f_ctl[0] << 16 |
16929 hdr->fh_f_ctl[1] << 8 |
16930 hdr->fh_f_ctl[2]);
16931 /* If last frame of sequence we can return success. */
16932 if (fctl & FC_FC_END_SEQ)
16933 return 1;
16934 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16935 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16936 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16937 /* If there is a hole in the sequence count then fail. */
eeead811 16938 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
16939 return 0;
16940 fctl = (hdr->fh_f_ctl[0] << 16 |
16941 hdr->fh_f_ctl[1] << 8 |
16942 hdr->fh_f_ctl[2]);
16943 /* If last frame of sequence we can return success. */
16944 if (fctl & FC_FC_END_SEQ)
16945 return 1;
16946 }
16947 return 0;
16948}
16949
16950/**
16951 * lpfc_prep_seq - Prep sequence for ULP processing
16952 * @vport: Pointer to the vport on which this sequence was received
16953 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16954 *
16955 * This function takes a sequence, described by a list of frames, and creates
16956 * a list of iocbq structures to describe the sequence. This iocbq list will be
16957 * used to issue to the generic unsolicited sequence handler. This routine
16958 * returns a pointer to the first iocbq in the list. If the function is unable
16959 * to allocate an iocbq then it throw out the received frames that were not
16960 * able to be described and return a pointer to the first iocbq. If unable to
16961 * allocate any iocbqs (including the first) this function will return NULL.
16962 **/
16963static struct lpfc_iocbq *
16964lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16965{
7851fe2c 16966 struct hbq_dmabuf *hbq_buf;
4f774513
JS
16967 struct lpfc_dmabuf *d_buf, *n_buf;
16968 struct lpfc_iocbq *first_iocbq, *iocbq;
16969 struct fc_frame_header *fc_hdr;
16970 uint32_t sid;
7851fe2c 16971 uint32_t len, tot_len;
eeead811 16972 struct ulp_bde64 *pbde;
4f774513
JS
16973
16974 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16975 /* remove from receive buffer list */
16976 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 16977 lpfc_update_rcv_time_stamp(vport);
4f774513 16978 /* get the Remote Port's SID */
6669f9bb 16979 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 16980 tot_len = 0;
4f774513
JS
16981 /* Get an iocbq struct to fill in. */
16982 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16983 if (first_iocbq) {
16984 /* Initialize the first IOCB. */
8fa38513 16985 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 16986 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 16987 first_iocbq->vport = vport;
939723a4
JS
16988
16989 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16990 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16991 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16992 first_iocbq->iocb.un.rcvels.parmRo =
16993 sli4_did_from_fc_hdr(fc_hdr);
16994 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16995 } else
16996 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
16997 first_iocbq->iocb.ulpContext = NO_XRI;
16998 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16999 be16_to_cpu(fc_hdr->fh_ox_id);
17000 /* iocbq is prepped for internal consumption. Physical vpi. */
17001 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17002 vport->phba->vpi_ids[vport->vpi];
4f774513 17003 /* put the first buffer into the first IOCBq */
48a5a664
JS
17004 tot_len = bf_get(lpfc_rcqe_length,
17005 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17006
4f774513
JS
17007 first_iocbq->context2 = &seq_dmabuf->dbuf;
17008 first_iocbq->context3 = NULL;
17009 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17010 if (tot_len > LPFC_DATA_BUF_SIZE)
17011 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17012 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17013 else
17014 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17015
4f774513 17016 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 17017
7851fe2c 17018 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
17019 }
17020 iocbq = first_iocbq;
17021 /*
17022 * Each IOCBq can have two Buffers assigned, so go through the list
17023 * of buffers for this sequence and save two buffers in each IOCBq
17024 */
17025 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17026 if (!iocbq) {
17027 lpfc_in_buf_free(vport->phba, d_buf);
17028 continue;
17029 }
17030 if (!iocbq->context3) {
17031 iocbq->context3 = d_buf;
17032 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
17033 /* We need to get the size out of the right CQE */
17034 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17035 len = bf_get(lpfc_rcqe_length,
17036 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
17037 pbde = (struct ulp_bde64 *)
17038 &iocbq->iocb.unsli3.sli3Words[4];
17039 if (len > LPFC_DATA_BUF_SIZE)
17040 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17041 else
17042 pbde->tus.f.bdeSize = len;
17043
7851fe2c
JS
17044 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17045 tot_len += len;
4f774513
JS
17046 } else {
17047 iocbq = lpfc_sli_get_iocbq(vport->phba);
17048 if (!iocbq) {
17049 if (first_iocbq) {
17050 first_iocbq->iocb.ulpStatus =
17051 IOSTAT_FCP_RSP_ERROR;
17052 first_iocbq->iocb.un.ulpWord[4] =
17053 IOERR_NO_RESOURCES;
17054 }
17055 lpfc_in_buf_free(vport->phba, d_buf);
17056 continue;
17057 }
48a5a664
JS
17058 /* We need to get the size out of the right CQE */
17059 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17060 len = bf_get(lpfc_rcqe_length,
17061 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
17062 iocbq->context2 = d_buf;
17063 iocbq->context3 = NULL;
17064 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
17065 if (len > LPFC_DATA_BUF_SIZE)
17066 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 17067 LPFC_DATA_BUF_SIZE;
48a5a664
JS
17068 else
17069 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 17070
7851fe2c
JS
17071 tot_len += len;
17072 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17073
4f774513
JS
17074 iocbq->iocb.un.rcvels.remoteID = sid;
17075 list_add_tail(&iocbq->list, &first_iocbq->list);
17076 }
17077 }
17078 return first_iocbq;
17079}
17080
6669f9bb
JS
17081static void
17082lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17083 struct hbq_dmabuf *seq_dmabuf)
17084{
17085 struct fc_frame_header *fc_hdr;
17086 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17087 struct lpfc_hba *phba = vport->phba;
17088
17089 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17090 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17091 if (!iocbq) {
17092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17093 "2707 Ring %d handler: Failed to allocate "
17094 "iocb Rctl x%x Type x%x received\n",
17095 LPFC_ELS_RING,
17096 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17097 return;
17098 }
17099 if (!lpfc_complete_unsol_iocb(phba,
895427bd 17100 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
17101 iocbq, fc_hdr->fh_r_ctl,
17102 fc_hdr->fh_type))
6d368e53 17103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
17104 "2540 Ring %d handler: unexpected Rctl "
17105 "x%x Type x%x received\n",
17106 LPFC_ELS_RING,
17107 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17108
17109 /* Free iocb created in lpfc_prep_seq */
17110 list_for_each_entry_safe(curr_iocb, next_iocb,
17111 &iocbq->list, list) {
17112 list_del_init(&curr_iocb->list);
17113 lpfc_sli_release_iocbq(phba, curr_iocb);
17114 }
17115 lpfc_sli_release_iocbq(phba, iocbq);
17116}
17117
ae9e28f3
JS
17118static void
17119lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17120 struct lpfc_iocbq *rspiocb)
17121{
17122 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17123
17124 if (pcmd && pcmd->virt)
771db5c0 17125 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3
JS
17126 kfree(pcmd);
17127 lpfc_sli_release_iocbq(phba, cmdiocb);
17128}
17129
17130static void
17131lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17132 struct hbq_dmabuf *dmabuf)
17133{
17134 struct fc_frame_header *fc_hdr;
17135 struct lpfc_hba *phba = vport->phba;
17136 struct lpfc_iocbq *iocbq = NULL;
17137 union lpfc_wqe *wqe;
17138 struct lpfc_dmabuf *pcmd = NULL;
17139 uint32_t frame_len;
17140 int rc;
17141
17142 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17143 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17144
17145 /* Send the received frame back */
17146 iocbq = lpfc_sli_get_iocbq(phba);
17147 if (!iocbq)
17148 goto exit;
17149
17150 /* Allocate buffer for command payload */
17151 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17152 if (pcmd)
771db5c0 17153 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
ae9e28f3
JS
17154 &pcmd->phys);
17155 if (!pcmd || !pcmd->virt)
17156 goto exit;
17157
17158 INIT_LIST_HEAD(&pcmd->list);
17159
17160 /* copyin the payload */
17161 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17162
17163 /* fill in BDE's for command */
17164 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17165 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17166 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17167 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17168
17169 iocbq->context2 = pcmd;
17170 iocbq->vport = vport;
17171 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17172 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17173
17174 /*
17175 * Setup rest of the iocb as though it were a WQE
17176 * Build the SEND_FRAME WQE
17177 */
17178 wqe = (union lpfc_wqe *)&iocbq->iocb;
17179
17180 wqe->send_frame.frame_len = frame_len;
17181 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17182 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17183 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17184 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17185 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17186 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17187
17188 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17189 iocbq->iocb.ulpLe = 1;
17190 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17191 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17192 if (rc == IOCB_ERROR)
17193 goto exit;
17194
17195 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17196 return;
17197
17198exit:
17199 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17200 "2023 Unable to process MDS loopback frame\n");
17201 if (pcmd && pcmd->virt)
771db5c0 17202 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
ae9e28f3 17203 kfree(pcmd);
401bb416
DK
17204 if (iocbq)
17205 lpfc_sli_release_iocbq(phba, iocbq);
ae9e28f3
JS
17206 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17207}
17208
4f774513
JS
17209/**
17210 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17211 * @phba: Pointer to HBA context object.
17212 *
17213 * This function is called with no lock held. This function processes all
17214 * the received buffers and gives it to upper layers when a received buffer
17215 * indicates that it is the final frame in the sequence. The interrupt
895427bd 17216 * service routine processes received buffers at interrupt contexts.
4f774513
JS
17217 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17218 * appropriate receive function when the final frame in a sequence is received.
17219 **/
4d9ab994
JS
17220void
17221lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17222 struct hbq_dmabuf *dmabuf)
4f774513 17223{
4d9ab994 17224 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
17225 struct fc_frame_header *fc_hdr;
17226 struct lpfc_vport *vport;
17227 uint32_t fcfi;
939723a4 17228 uint32_t did;
4f774513 17229
4f774513 17230 /* Process each received buffer */
4d9ab994 17231 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 17232
4d9ab994
JS
17233 /* check to see if this a valid type of frame */
17234 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17235 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17236 return;
17237 }
2ea259ee 17238
7851fe2c
JS
17239 if ((bf_get(lpfc_cqe_code,
17240 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17241 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17242 &dmabuf->cq_event.cqe.rcqe_cmpl);
17243 else
17244 fcfi = bf_get(lpfc_rcqe_fcf_id,
17245 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 17246
ae9e28f3
JS
17247 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17248 vport = phba->pport;
17249 /* Handle MDS Loopback frames */
17250 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17251 return;
17252 }
17253
895427bd
JS
17254 /* d_id this frame is directed to */
17255 did = sli4_did_from_fc_hdr(fc_hdr);
17256
17257 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 17258 if (!vport) {
4d9ab994
JS
17259 /* throw out the frame */
17260 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17261 return;
17262 }
939723a4 17263
939723a4
JS
17264 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17265 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17266 (did != Fabric_DID)) {
17267 /*
17268 * Throw out the frame if we are not pt2pt.
17269 * The pt2pt protocol allows for discovery frames
17270 * to be received without a registered VPI.
17271 */
17272 if (!(vport->fc_flag & FC_PT2PT) ||
17273 (phba->link_state == LPFC_HBA_READY)) {
17274 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17275 return;
17276 }
17277 }
17278
6669f9bb
JS
17279 /* Handle the basic abort sequence (BA_ABTS) event */
17280 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17281 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17282 return;
17283 }
17284
4d9ab994
JS
17285 /* Link this frame */
17286 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17287 if (!seq_dmabuf) {
17288 /* unable to add frame to vport - throw it out */
17289 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17290 return;
17291 }
17292 /* If not last frame in sequence continue processing frames. */
def9c7a9 17293 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 17294 return;
def9c7a9 17295
6669f9bb
JS
17296 /* Send the complete sequence to the upper layer protocol */
17297 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 17298}
6fb120a7
JS
17299
17300/**
17301 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17302 * @phba: pointer to lpfc hba data structure.
17303 *
17304 * This routine is invoked to post rpi header templates to the
17305 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17306 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17307 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17308 *
17309 * This routine does not require any locks. It's usage is expected
17310 * to be driver load or reset recovery when the driver is
17311 * sequential.
17312 *
17313 * Return codes
af901ca1 17314 * 0 - successful
d439d286 17315 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17316 * When this error occurs, the driver is not guaranteed
17317 * to have any rpi regions posted to the device and
17318 * must either attempt to repost the regions or take a
17319 * fatal error.
17320 **/
17321int
17322lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17323{
17324 struct lpfc_rpi_hdr *rpi_page;
17325 uint32_t rc = 0;
6d368e53
JS
17326 uint16_t lrpi = 0;
17327
17328 /* SLI4 ports that support extents do not require RPI headers. */
17329 if (!phba->sli4_hba.rpi_hdrs_in_use)
17330 goto exit;
17331 if (phba->sli4_hba.extents_in_use)
17332 return -EIO;
6fb120a7 17333
6fb120a7 17334 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
17335 /*
17336 * Assign the rpi headers a physical rpi only if the driver
17337 * has not initialized those resources. A port reset only
17338 * needs the headers posted.
17339 */
17340 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17341 LPFC_RPI_RSRC_RDY)
17342 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17343
6fb120a7
JS
17344 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17345 if (rc != MBX_SUCCESS) {
17346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17347 "2008 Error %d posting all rpi "
17348 "headers\n", rc);
17349 rc = -EIO;
17350 break;
17351 }
17352 }
17353
6d368e53
JS
17354 exit:
17355 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17356 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17357 return rc;
17358}
17359
17360/**
17361 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17362 * @phba: pointer to lpfc hba data structure.
17363 * @rpi_page: pointer to the rpi memory region.
17364 *
17365 * This routine is invoked to post a single rpi header to the
17366 * HBA consistent with the SLI-4 interface spec. This memory region
17367 * maps up to 64 rpi context regions.
17368 *
17369 * Return codes
af901ca1 17370 * 0 - successful
d439d286
JS
17371 * -ENOMEM - No available memory
17372 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17373 **/
17374int
17375lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17376{
17377 LPFC_MBOXQ_t *mboxq;
17378 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17379 uint32_t rc = 0;
6fb120a7
JS
17380 uint32_t shdr_status, shdr_add_status;
17381 union lpfc_sli4_cfg_shdr *shdr;
17382
6d368e53
JS
17383 /* SLI4 ports that support extents do not require RPI headers. */
17384 if (!phba->sli4_hba.rpi_hdrs_in_use)
17385 return rc;
17386 if (phba->sli4_hba.extents_in_use)
17387 return -EIO;
17388
6fb120a7
JS
17389 /* The port is notified of the header region via a mailbox command. */
17390 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17391 if (!mboxq) {
17392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17393 "2001 Unable to allocate memory for issuing "
17394 "SLI_CONFIG_SPECIAL mailbox command\n");
17395 return -ENOMEM;
17396 }
17397
17398 /* Post all rpi memory regions to the port. */
17399 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17400 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17401 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17402 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17403 sizeof(struct lpfc_sli4_cfg_mhdr),
17404 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17405
17406
17407 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17408 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17409 rpi_page->start_rpi);
6d368e53
JS
17410 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17411 hdr_tmpl, rpi_page->page_count);
17412
6fb120a7
JS
17413 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17414 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17415 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17416 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17419 if (rc != MBX_TIMEOUT)
17420 mempool_free(mboxq, phba->mbox_mem_pool);
17421 if (shdr_status || shdr_add_status || rc) {
17422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17423 "2514 POST_RPI_HDR mailbox failed with "
17424 "status x%x add_status x%x, mbx status x%x\n",
17425 shdr_status, shdr_add_status, rc);
17426 rc = -ENXIO;
845d9e8d
JS
17427 } else {
17428 /*
17429 * The next_rpi stores the next logical module-64 rpi value used
17430 * to post physical rpis in subsequent rpi postings.
17431 */
17432 spin_lock_irq(&phba->hbalock);
17433 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17434 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
17435 }
17436 return rc;
17437}
17438
17439/**
17440 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17441 * @phba: pointer to lpfc hba data structure.
17442 *
17443 * This routine is invoked to post rpi header templates to the
17444 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17445 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17446 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17447 *
17448 * Returns
af901ca1 17449 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17450 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17451 **/
17452int
17453lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17454{
6d368e53
JS
17455 unsigned long rpi;
17456 uint16_t max_rpi, rpi_limit;
17457 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17458 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17459 unsigned long iflag;
6fb120a7 17460
6fb120a7 17461 /*
6d368e53
JS
17462 * Fetch the next logical rpi. Because this index is logical,
17463 * the driver starts at 0 each time.
6fb120a7 17464 */
4902b381 17465 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17466 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17467 rpi_limit = phba->sli4_hba.next_rpi;
17468
6d368e53
JS
17469 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17470 if (rpi >= rpi_limit)
6fb120a7
JS
17471 rpi = LPFC_RPI_ALLOC_ERROR;
17472 else {
17473 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17474 phba->sli4_hba.max_cfg_param.rpi_used++;
17475 phba->sli4_hba.rpi_count++;
17476 }
be6bb941
JS
17477 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17478 "0001 rpi:%x max:%x lim:%x\n",
17479 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17480
17481 /*
17482 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17483 * has been exhausted.
6fb120a7
JS
17484 */
17485 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17486 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17487 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17488 return rpi;
17489 }
17490
6d368e53
JS
17491 /*
17492 * RPI header postings are not required for SLI4 ports capable of
17493 * extents.
17494 */
17495 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17496 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17497 return rpi;
17498 }
17499
6fb120a7
JS
17500 /*
17501 * If the driver is running low on rpi resources, allocate another
17502 * page now. Note that the next_rpi value is used because
17503 * it represents how many are actually in use whereas max_rpi notes
17504 * how many are supported max by the device.
17505 */
6d368e53 17506 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17507 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17508 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17509 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17510 if (!rpi_hdr) {
17511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17512 "2002 Error Could not grow rpi "
17513 "count\n");
17514 } else {
6d368e53
JS
17515 lrpi = rpi_hdr->start_rpi;
17516 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17517 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17518 }
17519 }
17520
17521 return rpi;
17522}
17523
d7c47992
JS
17524/**
17525 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17526 * @phba: pointer to lpfc hba data structure.
17527 *
17528 * This routine is invoked to release an rpi to the pool of
17529 * available rpis maintained by the driver.
17530 **/
5d8b8167 17531static void
d7c47992
JS
17532__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17533{
17534 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17535 phba->sli4_hba.rpi_count--;
17536 phba->sli4_hba.max_cfg_param.rpi_used--;
17537 }
17538}
17539
6fb120a7
JS
17540/**
17541 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17542 * @phba: pointer to lpfc hba data structure.
17543 *
17544 * This routine is invoked to release an rpi to the pool of
17545 * available rpis maintained by the driver.
17546 **/
17547void
17548lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17549{
17550 spin_lock_irq(&phba->hbalock);
d7c47992 17551 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17552 spin_unlock_irq(&phba->hbalock);
17553}
17554
17555/**
17556 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17557 * @phba: pointer to lpfc hba data structure.
17558 *
17559 * This routine is invoked to remove the memory region that
17560 * provided rpi via a bitmask.
17561 **/
17562void
17563lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17564{
17565 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17566 kfree(phba->sli4_hba.rpi_ids);
17567 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17568}
17569
17570/**
17571 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17572 * @phba: pointer to lpfc hba data structure.
17573 *
17574 * This routine is invoked to remove the memory region that
17575 * provided rpi via a bitmask.
17576 **/
17577int
6b5151fd
JS
17578lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17579 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17580{
17581 LPFC_MBOXQ_t *mboxq;
17582 struct lpfc_hba *phba = ndlp->phba;
17583 int rc;
17584
17585 /* The port is notified of the header region via a mailbox command. */
17586 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17587 if (!mboxq)
17588 return -ENOMEM;
17589
17590 /* Post all rpi memory regions to the port. */
17591 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17592 if (cmpl) {
17593 mboxq->mbox_cmpl = cmpl;
17594 mboxq->context1 = arg;
17595 mboxq->context2 = ndlp;
72859909
JS
17596 } else
17597 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17598 mboxq->vport = ndlp->vport;
6fb120a7
JS
17599 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17600 if (rc == MBX_NOT_FINISHED) {
17601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17602 "2010 Resume RPI Mailbox failed "
17603 "status %d, mbxStatus x%x\n", rc,
17604 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17605 mempool_free(mboxq, phba->mbox_mem_pool);
17606 return -EIO;
17607 }
17608 return 0;
17609}
17610
17611/**
17612 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17613 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17614 *
76a95d75 17615 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17616 *
17617 * Returns:
17618 * 0 success
17619 * -Evalue otherwise
17620 **/
17621int
76a95d75 17622lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17623{
17624 LPFC_MBOXQ_t *mboxq;
17625 int rc = 0;
6a9c52cf 17626 int retval = MBX_SUCCESS;
6fb120a7 17627 uint32_t mbox_tmo;
76a95d75 17628 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17629 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17630 if (!mboxq)
17631 return -ENOMEM;
76a95d75 17632 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17633 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17634 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17635 if (rc != MBX_SUCCESS) {
76a95d75 17636 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17637 "2022 INIT VPI Mailbox failed "
17638 "status %d, mbxStatus x%x\n", rc,
17639 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17640 retval = -EIO;
6fb120a7 17641 }
6a9c52cf 17642 if (rc != MBX_TIMEOUT)
76a95d75 17643 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17644
17645 return retval;
6fb120a7
JS
17646}
17647
17648/**
17649 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17650 * @phba: pointer to lpfc hba data structure.
17651 * @mboxq: Pointer to mailbox object.
17652 *
17653 * This routine is invoked to manually add a single FCF record. The caller
17654 * must pass a completely initialized FCF_Record. This routine takes
17655 * care of the nonembedded mailbox operations.
17656 **/
17657static void
17658lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17659{
17660 void *virt_addr;
17661 union lpfc_sli4_cfg_shdr *shdr;
17662 uint32_t shdr_status, shdr_add_status;
17663
17664 virt_addr = mboxq->sge_array->addr[0];
17665 /* The IOCTL status is embedded in the mailbox subheader. */
17666 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17667 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17668 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17669
17670 if ((shdr_status || shdr_add_status) &&
17671 (shdr_status != STATUS_FCF_IN_USE))
17672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17673 "2558 ADD_FCF_RECORD mailbox failed with "
17674 "status x%x add_status x%x\n",
17675 shdr_status, shdr_add_status);
17676
17677 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17678}
17679
17680/**
17681 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17682 * @phba: pointer to lpfc hba data structure.
17683 * @fcf_record: pointer to the initialized fcf record to add.
17684 *
17685 * This routine is invoked to manually add a single FCF record. The caller
17686 * must pass a completely initialized FCF_Record. This routine takes
17687 * care of the nonembedded mailbox operations.
17688 **/
17689int
17690lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17691{
17692 int rc = 0;
17693 LPFC_MBOXQ_t *mboxq;
17694 uint8_t *bytep;
17695 void *virt_addr;
6fb120a7
JS
17696 struct lpfc_mbx_sge sge;
17697 uint32_t alloc_len, req_len;
17698 uint32_t fcfindex;
17699
17700 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17701 if (!mboxq) {
17702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17703 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17704 return -ENOMEM;
17705 }
17706
17707 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17708 sizeof(uint32_t);
17709
17710 /* Allocate DMA memory and set up the non-embedded mailbox command */
17711 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17712 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17713 req_len, LPFC_SLI4_MBX_NEMBED);
17714 if (alloc_len < req_len) {
17715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17716 "2523 Allocated DMA memory size (x%x) is "
17717 "less than the requested DMA memory "
17718 "size (x%x)\n", alloc_len, req_len);
17719 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17720 return -ENOMEM;
17721 }
17722
17723 /*
17724 * Get the first SGE entry from the non-embedded DMA memory. This
17725 * routine only uses a single SGE.
17726 */
17727 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
17728 virt_addr = mboxq->sge_array->addr[0];
17729 /*
17730 * Configure the FCF record for FCFI 0. This is the driver's
17731 * hardcoded default and gets used in nonFIP mode.
17732 */
17733 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17734 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17735 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17736
17737 /*
17738 * Copy the fcf_index and the FCF Record Data. The data starts after
17739 * the FCoE header plus word10. The data copy needs to be endian
17740 * correct.
17741 */
17742 bytep += sizeof(uint32_t);
17743 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17744 mboxq->vport = phba->pport;
17745 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17746 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17747 if (rc == MBX_NOT_FINISHED) {
17748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17749 "2515 ADD_FCF_RECORD mailbox failed with "
17750 "status 0x%x\n", rc);
17751 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17752 rc = -EIO;
17753 } else
17754 rc = 0;
17755
17756 return rc;
17757}
17758
17759/**
17760 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17761 * @phba: pointer to lpfc hba data structure.
17762 * @fcf_record: pointer to the fcf record to write the default data.
17763 * @fcf_index: FCF table entry index.
17764 *
17765 * This routine is invoked to build the driver's default FCF record. The
17766 * values used are hardcoded. This routine handles memory initialization.
17767 *
17768 **/
17769void
17770lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17771 struct fcf_record *fcf_record,
17772 uint16_t fcf_index)
17773{
17774 memset(fcf_record, 0, sizeof(struct fcf_record));
17775 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17776 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17777 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17778 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17779 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17780 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17781 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17782 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17783 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17784 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17785 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17786 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17787 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 17788 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
17789 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17790 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17791 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17792 /* Set the VLAN bit map */
17793 if (phba->valid_vlan) {
17794 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17795 = 1 << (phba->vlan_id % 8);
17796 }
17797}
17798
17799/**
0c9ab6f5 17800 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
17801 * @phba: pointer to lpfc hba data structure.
17802 * @fcf_index: FCF table entry offset.
17803 *
0c9ab6f5
JS
17804 * This routine is invoked to scan the entire FCF table by reading FCF
17805 * record and processing it one at a time starting from the @fcf_index
17806 * for initial FCF discovery or fast FCF failover rediscovery.
17807 *
25985edc 17808 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 17809 * otherwise.
6fb120a7
JS
17810 **/
17811int
0c9ab6f5 17812lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
17813{
17814 int rc = 0, error;
17815 LPFC_MBOXQ_t *mboxq;
6fb120a7 17816
32b9793f 17817 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 17818 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
17819 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17820 if (!mboxq) {
17821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17822 "2000 Failed to allocate mbox for "
17823 "READ_FCF cmd\n");
4d9ab994 17824 error = -ENOMEM;
0c9ab6f5 17825 goto fail_fcf_scan;
6fb120a7 17826 }
ecfd03c6 17827 /* Construct the read FCF record mailbox command */
0c9ab6f5 17828 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
17829 if (rc) {
17830 error = -EINVAL;
0c9ab6f5 17831 goto fail_fcf_scan;
6fb120a7 17832 }
ecfd03c6 17833 /* Issue the mailbox command asynchronously */
6fb120a7 17834 mboxq->vport = phba->pport;
0c9ab6f5 17835 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
17836
17837 spin_lock_irq(&phba->hbalock);
17838 phba->hba_flag |= FCF_TS_INPROG;
17839 spin_unlock_irq(&phba->hbalock);
17840
6fb120a7 17841 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 17842 if (rc == MBX_NOT_FINISHED)
6fb120a7 17843 error = -EIO;
ecfd03c6 17844 else {
38b92ef8
JS
17845 /* Reset eligible FCF count for new scan */
17846 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 17847 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 17848 error = 0;
32b9793f 17849 }
0c9ab6f5 17850fail_fcf_scan:
4d9ab994
JS
17851 if (error) {
17852 if (mboxq)
17853 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 17854 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 17855 spin_lock_irq(&phba->hbalock);
a93ff37a 17856 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
17857 spin_unlock_irq(&phba->hbalock);
17858 }
6fb120a7
JS
17859 return error;
17860}
a0c87cbd 17861
0c9ab6f5 17862/**
a93ff37a 17863 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
17864 * @phba: pointer to lpfc hba data structure.
17865 * @fcf_index: FCF table entry offset.
17866 *
17867 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 17868 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 17869 *
25985edc 17870 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17871 * otherwise.
17872 **/
17873int
17874lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17875{
17876 int rc = 0, error;
17877 LPFC_MBOXQ_t *mboxq;
17878
17879 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17880 if (!mboxq) {
17881 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17882 "2763 Failed to allocate mbox for "
17883 "READ_FCF cmd\n");
17884 error = -ENOMEM;
17885 goto fail_fcf_read;
17886 }
17887 /* Construct the read FCF record mailbox command */
17888 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17889 if (rc) {
17890 error = -EINVAL;
17891 goto fail_fcf_read;
17892 }
17893 /* Issue the mailbox command asynchronously */
17894 mboxq->vport = phba->pport;
17895 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17896 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17897 if (rc == MBX_NOT_FINISHED)
17898 error = -EIO;
17899 else
17900 error = 0;
17901
17902fail_fcf_read:
17903 if (error && mboxq)
17904 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17905 return error;
17906}
17907
17908/**
17909 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17910 * @phba: pointer to lpfc hba data structure.
17911 * @fcf_index: FCF table entry offset.
17912 *
17913 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 17914 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 17915 *
25985edc 17916 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17917 * otherwise.
17918 **/
17919int
17920lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17921{
17922 int rc = 0, error;
17923 LPFC_MBOXQ_t *mboxq;
17924
17925 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17926 if (!mboxq) {
17927 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17928 "2758 Failed to allocate mbox for "
17929 "READ_FCF cmd\n");
17930 error = -ENOMEM;
17931 goto fail_fcf_read;
17932 }
17933 /* Construct the read FCF record mailbox command */
17934 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17935 if (rc) {
17936 error = -EINVAL;
17937 goto fail_fcf_read;
17938 }
17939 /* Issue the mailbox command asynchronously */
17940 mboxq->vport = phba->pport;
17941 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17942 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17943 if (rc == MBX_NOT_FINISHED)
17944 error = -EIO;
17945 else
17946 error = 0;
17947
17948fail_fcf_read:
17949 if (error && mboxq)
17950 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17951 return error;
17952}
17953
7d791df7 17954/**
f5cb5304 17955 * lpfc_check_next_fcf_pri_level
7d791df7
JS
17956 * phba pointer to the lpfc_hba struct for this port.
17957 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17958 * routine when the rr_bmask is empty. The FCF indecies are put into the
17959 * rr_bmask based on their priority level. Starting from the highest priority
17960 * to the lowest. The most likely FCF candidate will be in the highest
17961 * priority group. When this routine is called it searches the fcf_pri list for
17962 * next lowest priority group and repopulates the rr_bmask with only those
17963 * fcf_indexes.
17964 * returns:
17965 * 1=success 0=failure
17966 **/
5d8b8167 17967static int
7d791df7
JS
17968lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17969{
17970 uint16_t next_fcf_pri;
17971 uint16_t last_index;
17972 struct lpfc_fcf_pri *fcf_pri;
17973 int rc;
17974 int ret = 0;
17975
17976 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17977 LPFC_SLI4_FCF_TBL_INDX_MAX);
17978 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17979 "3060 Last IDX %d\n", last_index);
2562669c
JS
17980
17981 /* Verify the priority list has 2 or more entries */
17982 spin_lock_irq(&phba->hbalock);
17983 if (list_empty(&phba->fcf.fcf_pri_list) ||
17984 list_is_singular(&phba->fcf.fcf_pri_list)) {
17985 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
17986 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17987 "3061 Last IDX %d\n", last_index);
17988 return 0; /* Empty rr list */
17989 }
2562669c
JS
17990 spin_unlock_irq(&phba->hbalock);
17991
7d791df7
JS
17992 next_fcf_pri = 0;
17993 /*
17994 * Clear the rr_bmask and set all of the bits that are at this
17995 * priority.
17996 */
17997 memset(phba->fcf.fcf_rr_bmask, 0,
17998 sizeof(*phba->fcf.fcf_rr_bmask));
17999 spin_lock_irq(&phba->hbalock);
18000 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18001 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18002 continue;
18003 /*
18004 * the 1st priority that has not FLOGI failed
18005 * will be the highest.
18006 */
18007 if (!next_fcf_pri)
18008 next_fcf_pri = fcf_pri->fcf_rec.priority;
18009 spin_unlock_irq(&phba->hbalock);
18010 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18011 rc = lpfc_sli4_fcf_rr_index_set(phba,
18012 fcf_pri->fcf_rec.fcf_index);
18013 if (rc)
18014 return 0;
18015 }
18016 spin_lock_irq(&phba->hbalock);
18017 }
18018 /*
18019 * if next_fcf_pri was not set above and the list is not empty then
18020 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 18021 * and start at the beginning.
7d791df7
JS
18022 */
18023 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18024 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18025 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18026 /*
18027 * the 1st priority that has not FLOGI failed
18028 * will be the highest.
18029 */
18030 if (!next_fcf_pri)
18031 next_fcf_pri = fcf_pri->fcf_rec.priority;
18032 spin_unlock_irq(&phba->hbalock);
18033 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18034 rc = lpfc_sli4_fcf_rr_index_set(phba,
18035 fcf_pri->fcf_rec.fcf_index);
18036 if (rc)
18037 return 0;
18038 }
18039 spin_lock_irq(&phba->hbalock);
18040 }
18041 } else
18042 ret = 1;
18043 spin_unlock_irq(&phba->hbalock);
18044
18045 return ret;
18046}
0c9ab6f5
JS
18047/**
18048 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18049 * @phba: pointer to lpfc hba data structure.
18050 *
18051 * This routine is to get the next eligible FCF record index in a round
18052 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 18053 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
18054 * shall be returned, otherwise, the next eligible FCF record's index
18055 * shall be returned.
18056 **/
18057uint16_t
18058lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18059{
18060 uint16_t next_fcf_index;
18061
421c6622 18062initial_priority:
3804dc84 18063 /* Search start from next bit of currently registered FCF index */
421c6622
JS
18064 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18065
7d791df7 18066next_priority:
421c6622
JS
18067 /* Determine the next fcf index to check */
18068 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
18069 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18070 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
18071 next_fcf_index);
18072
0c9ab6f5 18073 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
18074 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18075 /*
18076 * If we have wrapped then we need to clear the bits that
18077 * have been tested so that we can detect when we should
18078 * change the priority level.
18079 */
0c9ab6f5
JS
18080 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18081 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
18082 }
18083
3804dc84
JS
18084
18085 /* Check roundrobin failover list empty condition */
7d791df7
JS
18086 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18087 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18088 /*
18089 * If next fcf index is not found check if there are lower
18090 * Priority level fcf's in the fcf_priority list.
18091 * Set up the rr_bmask with all of the avaiable fcf bits
18092 * at that level and continue the selection process.
18093 */
18094 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 18095 goto initial_priority;
3804dc84
JS
18096 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18097 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
18098 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18099 return LPFC_FCOE_FCF_NEXT_NONE;
18100 else {
18101 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18102 "3063 Only FCF available idx %d, flag %x\n",
18103 next_fcf_index,
18104 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18105 return next_fcf_index;
18106 }
3804dc84
JS
18107 }
18108
7d791df7
JS
18109 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18110 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
18111 LPFC_FCF_FLOGI_FAILED) {
18112 if (list_is_singular(&phba->fcf.fcf_pri_list))
18113 return LPFC_FCOE_FCF_NEXT_NONE;
18114
7d791df7 18115 goto next_priority;
f5cb5304 18116 }
7d791df7 18117
3804dc84 18118 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
18119 "2845 Get next roundrobin failover FCF (x%x)\n",
18120 next_fcf_index);
18121
0c9ab6f5
JS
18122 return next_fcf_index;
18123}
18124
18125/**
18126 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18127 * @phba: pointer to lpfc hba data structure.
18128 *
18129 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 18130 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18131 * does not go beyond the range of the driver allocated bmask dimension
18132 * before setting the bit.
18133 *
18134 * Returns 0 if the index bit successfully set, otherwise, it returns
18135 * -EINVAL.
18136 **/
18137int
18138lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18139{
18140 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18141 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18142 "2610 FCF (x%x) reached driver's book "
18143 "keeping dimension:x%x\n",
0c9ab6f5
JS
18144 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18145 return -EINVAL;
18146 }
18147 /* Set the eligible FCF record index bmask */
18148 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18149
3804dc84 18150 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18151 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
18152 "bmask\n", fcf_index);
18153
0c9ab6f5
JS
18154 return 0;
18155}
18156
18157/**
3804dc84 18158 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
18159 * @phba: pointer to lpfc hba data structure.
18160 *
18161 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 18162 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
18163 * does not go beyond the range of the driver allocated bmask dimension
18164 * before clearing the bit.
18165 **/
18166void
18167lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18168{
9a803a74 18169 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
18170 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18171 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
18172 "2762 FCF (x%x) reached driver's book "
18173 "keeping dimension:x%x\n",
0c9ab6f5
JS
18174 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18175 return;
18176 }
18177 /* Clear the eligible FCF record index bmask */
7d791df7 18178 spin_lock_irq(&phba->hbalock);
9a803a74
JS
18179 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18180 list) {
7d791df7
JS
18181 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18182 list_del_init(&fcf_pri->list);
18183 break;
18184 }
18185 }
18186 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 18187 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
18188
18189 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18190 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 18191 "bmask\n", fcf_index);
0c9ab6f5
JS
18192}
18193
ecfd03c6
JS
18194/**
18195 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18196 * @phba: pointer to lpfc hba data structure.
18197 *
18198 * This routine is the completion routine for the rediscover FCF table mailbox
18199 * command. If the mailbox command returned failure, it will try to stop the
18200 * FCF rediscover wait timer.
18201 **/
5d8b8167 18202static void
ecfd03c6
JS
18203lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18204{
18205 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18206 uint32_t shdr_status, shdr_add_status;
18207
18208 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18209
18210 shdr_status = bf_get(lpfc_mbox_hdr_status,
18211 &redisc_fcf->header.cfg_shdr.response);
18212 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18213 &redisc_fcf->header.cfg_shdr.response);
18214 if (shdr_status || shdr_add_status) {
0c9ab6f5 18215 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
18216 "2746 Requesting for FCF rediscovery failed "
18217 "status x%x add_status x%x\n",
18218 shdr_status, shdr_add_status);
0c9ab6f5 18219 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 18220 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18221 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
18222 spin_unlock_irq(&phba->hbalock);
18223 /*
18224 * CVL event triggered FCF rediscover request failed,
18225 * last resort to re-try current registered FCF entry.
18226 */
18227 lpfc_retry_pport_discovery(phba);
18228 } else {
18229 spin_lock_irq(&phba->hbalock);
0c9ab6f5 18230 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
18231 spin_unlock_irq(&phba->hbalock);
18232 /*
18233 * DEAD FCF event triggered FCF rediscover request
18234 * failed, last resort to fail over as a link down
18235 * to FCF registration.
18236 */
18237 lpfc_sli4_fcf_dead_failthrough(phba);
18238 }
0c9ab6f5
JS
18239 } else {
18240 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 18241 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
18242 /*
18243 * Start FCF rediscovery wait timer for pending FCF
18244 * before rescan FCF record table.
18245 */
18246 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 18247 }
ecfd03c6
JS
18248
18249 mempool_free(mbox, phba->mbox_mem_pool);
18250}
18251
18252/**
3804dc84 18253 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
18254 * @phba: pointer to lpfc hba data structure.
18255 *
18256 * This routine is invoked to request for rediscovery of the entire FCF table
18257 * by the port.
18258 **/
18259int
18260lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18261{
18262 LPFC_MBOXQ_t *mbox;
18263 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18264 int rc, length;
18265
0c9ab6f5
JS
18266 /* Cancel retry delay timers to all vports before FCF rediscover */
18267 lpfc_cancel_all_vport_retry_delay_timer(phba);
18268
ecfd03c6
JS
18269 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18270 if (!mbox) {
18271 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18272 "2745 Failed to allocate mbox for "
18273 "requesting FCF rediscover.\n");
18274 return -ENOMEM;
18275 }
18276
18277 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18278 sizeof(struct lpfc_sli4_cfg_mhdr));
18279 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18280 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18281 length, LPFC_SLI4_MBX_EMBED);
18282
18283 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18284 /* Set count to 0 for invalidating the entire FCF database */
18285 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18286
18287 /* Issue the mailbox command asynchronously */
18288 mbox->vport = phba->pport;
18289 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18290 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18291
18292 if (rc == MBX_NOT_FINISHED) {
18293 mempool_free(mbox, phba->mbox_mem_pool);
18294 return -EIO;
18295 }
18296 return 0;
18297}
18298
fc2b989b
JS
18299/**
18300 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18301 * @phba: pointer to lpfc hba data structure.
18302 *
18303 * This function is the failover routine as a last resort to the FCF DEAD
18304 * event when driver failed to perform fast FCF failover.
18305 **/
18306void
18307lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18308{
18309 uint32_t link_state;
18310
18311 /*
18312 * Last resort as FCF DEAD event failover will treat this as
18313 * a link down, but save the link state because we don't want
18314 * it to be changed to Link Down unless it is already down.
18315 */
18316 link_state = phba->link_state;
18317 lpfc_linkdown(phba);
18318 phba->link_state = link_state;
18319
18320 /* Unregister FCF if no devices connected to it */
18321 lpfc_unregister_unused_fcf(phba);
18322}
18323
a0c87cbd 18324/**
026abb87 18325 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 18326 * @phba: pointer to lpfc hba data structure.
026abb87 18327 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 18328 *
026abb87
JS
18329 * This function gets SLI3 port configure region 23 data through memory dump
18330 * mailbox command. When it successfully retrieves data, the size of the data
18331 * will be returned, otherwise, 0 will be returned.
a0c87cbd 18332 **/
026abb87
JS
18333static uint32_t
18334lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
18335{
18336 LPFC_MBOXQ_t *pmb = NULL;
18337 MAILBOX_t *mb;
026abb87 18338 uint32_t offset = 0;
a0c87cbd
JS
18339 int rc;
18340
026abb87
JS
18341 if (!rgn23_data)
18342 return 0;
18343
a0c87cbd
JS
18344 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18345 if (!pmb) {
18346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
18347 "2600 failed to allocate mailbox memory\n");
18348 return 0;
a0c87cbd
JS
18349 }
18350 mb = &pmb->u.mb;
18351
a0c87cbd
JS
18352 do {
18353 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18354 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18355
18356 if (rc != MBX_SUCCESS) {
18357 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
18358 "2601 failed to read config "
18359 "region 23, rc 0x%x Status 0x%x\n",
18360 rc, mb->mbxStatus);
a0c87cbd
JS
18361 mb->un.varDmp.word_cnt = 0;
18362 }
18363 /*
18364 * dump mem may return a zero when finished or we got a
18365 * mailbox error, either way we are done.
18366 */
18367 if (mb->un.varDmp.word_cnt == 0)
18368 break;
18369 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18370 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18371
18372 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18373 rgn23_data + offset,
18374 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18375 offset += mb->un.varDmp.word_cnt;
18376 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18377
026abb87
JS
18378 mempool_free(pmb, phba->mbox_mem_pool);
18379 return offset;
18380}
18381
18382/**
18383 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18384 * @phba: pointer to lpfc hba data structure.
18385 * @rgn23_data: pointer to configure region 23 data.
18386 *
18387 * This function gets SLI4 port configure region 23 data through memory dump
18388 * mailbox command. When it successfully retrieves data, the size of the data
18389 * will be returned, otherwise, 0 will be returned.
18390 **/
18391static uint32_t
18392lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18393{
18394 LPFC_MBOXQ_t *mboxq = NULL;
18395 struct lpfc_dmabuf *mp = NULL;
18396 struct lpfc_mqe *mqe;
18397 uint32_t data_length = 0;
18398 int rc;
18399
18400 if (!rgn23_data)
18401 return 0;
18402
18403 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18404 if (!mboxq) {
18405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18406 "3105 failed to allocate mailbox memory\n");
18407 return 0;
18408 }
18409
18410 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18411 goto out;
18412 mqe = &mboxq->u.mqe;
18413 mp = (struct lpfc_dmabuf *) mboxq->context1;
18414 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18415 if (rc)
18416 goto out;
18417 data_length = mqe->un.mb_words[5];
18418 if (data_length == 0)
18419 goto out;
18420 if (data_length > DMP_RGN23_SIZE) {
18421 data_length = 0;
18422 goto out;
18423 }
18424 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18425out:
18426 mempool_free(mboxq, phba->mbox_mem_pool);
18427 if (mp) {
18428 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18429 kfree(mp);
18430 }
18431 return data_length;
18432}
18433
18434/**
18435 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18436 * @phba: pointer to lpfc hba data structure.
18437 *
18438 * This function read region 23 and parse TLV for port status to
18439 * decide if the user disaled the port. If the TLV indicates the
18440 * port is disabled, the hba_flag is set accordingly.
18441 **/
18442void
18443lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18444{
18445 uint8_t *rgn23_data = NULL;
18446 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18447 uint32_t offset = 0;
18448
18449 /* Get adapter Region 23 data */
18450 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18451 if (!rgn23_data)
18452 goto out;
18453
18454 if (phba->sli_rev < LPFC_SLI_REV4)
18455 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18456 else {
18457 if_type = bf_get(lpfc_sli_intf_if_type,
18458 &phba->sli4_hba.sli_intf);
18459 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18460 goto out;
18461 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18462 }
a0c87cbd
JS
18463
18464 if (!data_size)
18465 goto out;
18466
18467 /* Check the region signature first */
18468 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18470 "2619 Config region 23 has bad signature\n");
18471 goto out;
18472 }
18473 offset += 4;
18474
18475 /* Check the data structure version */
18476 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18478 "2620 Config region 23 has bad version\n");
18479 goto out;
18480 }
18481 offset += 4;
18482
18483 /* Parse TLV entries in the region */
18484 while (offset < data_size) {
18485 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18486 break;
18487 /*
18488 * If the TLV is not driver specific TLV or driver id is
18489 * not linux driver id, skip the record.
18490 */
18491 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18492 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18493 (rgn23_data[offset + 3] != 0)) {
18494 offset += rgn23_data[offset + 1] * 4 + 4;
18495 continue;
18496 }
18497
18498 /* Driver found a driver specific TLV in the config region */
18499 sub_tlv_len = rgn23_data[offset + 1] * 4;
18500 offset += 4;
18501 tlv_offset = 0;
18502
18503 /*
18504 * Search for configured port state sub-TLV.
18505 */
18506 while ((offset < data_size) &&
18507 (tlv_offset < sub_tlv_len)) {
18508 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18509 offset += 4;
18510 tlv_offset += 4;
18511 break;
18512 }
18513 if (rgn23_data[offset] != PORT_STE_TYPE) {
18514 offset += rgn23_data[offset + 1] * 4 + 4;
18515 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18516 continue;
18517 }
18518
18519 /* This HBA contains PORT_STE configured */
18520 if (!rgn23_data[offset + 2])
18521 phba->hba_flag |= LINK_DISABLED;
18522
18523 goto out;
18524 }
18525 }
026abb87 18526
a0c87cbd 18527out:
a0c87cbd
JS
18528 kfree(rgn23_data);
18529 return;
18530}
695a814e 18531
52d52440
JS
18532/**
18533 * lpfc_wr_object - write an object to the firmware
18534 * @phba: HBA structure that indicates port to create a queue on.
18535 * @dmabuf_list: list of dmabufs to write to the port.
18536 * @size: the total byte value of the objects to write to the port.
18537 * @offset: the current offset to be used to start the transfer.
18538 *
18539 * This routine will create a wr_object mailbox command to send to the port.
18540 * the mailbox command will be constructed using the dma buffers described in
18541 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18542 * BDEs that the imbedded mailbox can support. The @offset variable will be
18543 * used to indicate the starting offset of the transfer and will also return
18544 * the offset after the write object mailbox has completed. @size is used to
18545 * determine the end of the object and whether the eof bit should be set.
18546 *
18547 * Return 0 is successful and offset will contain the the new offset to use
18548 * for the next write.
18549 * Return negative value for error cases.
18550 **/
18551int
18552lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18553 uint32_t size, uint32_t *offset)
18554{
18555 struct lpfc_mbx_wr_object *wr_object;
18556 LPFC_MBOXQ_t *mbox;
18557 int rc = 0, i = 0;
18558 uint32_t shdr_status, shdr_add_status;
18559 uint32_t mbox_tmo;
18560 union lpfc_sli4_cfg_shdr *shdr;
18561 struct lpfc_dmabuf *dmabuf;
18562 uint32_t written = 0;
18563
18564 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18565 if (!mbox)
18566 return -ENOMEM;
18567
18568 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18569 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18570 sizeof(struct lpfc_mbx_wr_object) -
18571 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18572
18573 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18574 wr_object->u.request.write_offset = *offset;
18575 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18576 wr_object->u.request.object_name[0] =
18577 cpu_to_le32(wr_object->u.request.object_name[0]);
18578 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18579 list_for_each_entry(dmabuf, dmabuf_list, list) {
18580 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18581 break;
18582 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18583 wr_object->u.request.bde[i].addrHigh =
18584 putPaddrHigh(dmabuf->phys);
18585 if (written + SLI4_PAGE_SIZE >= size) {
18586 wr_object->u.request.bde[i].tus.f.bdeSize =
18587 (size - written);
18588 written += (size - written);
18589 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18590 } else {
18591 wr_object->u.request.bde[i].tus.f.bdeSize =
18592 SLI4_PAGE_SIZE;
18593 written += SLI4_PAGE_SIZE;
18594 }
18595 i++;
18596 }
18597 wr_object->u.request.bde_count = i;
18598 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18599 if (!phba->sli4_hba.intr_enable)
18600 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18601 else {
a183a15f 18602 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18603 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18604 }
18605 /* The IOCTL status is embedded in the mailbox subheader. */
18606 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18607 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18608 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18609 if (rc != MBX_TIMEOUT)
18610 mempool_free(mbox, phba->mbox_mem_pool);
18611 if (shdr_status || shdr_add_status || rc) {
18612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18613 "3025 Write Object mailbox failed with "
18614 "status x%x add_status x%x, mbx status x%x\n",
18615 shdr_status, shdr_add_status, rc);
18616 rc = -ENXIO;
18617 } else
18618 *offset += wr_object->u.response.actual_write_length;
18619 return rc;
18620}
18621
695a814e
JS
18622/**
18623 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18624 * @vport: pointer to vport data structure.
18625 *
18626 * This function iterate through the mailboxq and clean up all REG_LOGIN
18627 * and REG_VPI mailbox commands associated with the vport. This function
18628 * is called when driver want to restart discovery of the vport due to
18629 * a Clear Virtual Link event.
18630 **/
18631void
18632lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18633{
18634 struct lpfc_hba *phba = vport->phba;
18635 LPFC_MBOXQ_t *mb, *nextmb;
18636 struct lpfc_dmabuf *mp;
78730cfe 18637 struct lpfc_nodelist *ndlp;
d439d286 18638 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18639 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18640 LIST_HEAD(mbox_cmd_list);
63e801ce 18641 uint8_t restart_loop;
695a814e 18642
d439d286 18643 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18644 spin_lock_irq(&phba->hbalock);
18645 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18646 if (mb->vport != vport)
18647 continue;
18648
18649 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18650 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18651 continue;
18652
d439d286
JS
18653 list_del(&mb->list);
18654 list_add_tail(&mb->list, &mbox_cmd_list);
18655 }
18656 /* Clean up active mailbox command with the vport */
18657 mb = phba->sli.mbox_active;
18658 if (mb && (mb->vport == vport)) {
18659 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18660 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18661 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18662 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18663 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18664 /* Put reference count for delayed processing */
18665 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18666 /* Unregister the RPI when mailbox complete */
18667 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18668 }
18669 }
63e801ce
JS
18670 /* Cleanup any mailbox completions which are not yet processed */
18671 do {
18672 restart_loop = 0;
18673 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18674 /*
18675 * If this mailox is already processed or it is
18676 * for another vport ignore it.
18677 */
18678 if ((mb->vport != vport) ||
18679 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18680 continue;
18681
18682 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18683 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18684 continue;
18685
18686 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18687 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18688 ndlp = (struct lpfc_nodelist *)mb->context2;
18689 /* Unregister the RPI when mailbox complete */
18690 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18691 restart_loop = 1;
18692 spin_unlock_irq(&phba->hbalock);
18693 spin_lock(shost->host_lock);
18694 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18695 spin_unlock(shost->host_lock);
18696 spin_lock_irq(&phba->hbalock);
18697 break;
18698 }
18699 }
18700 } while (restart_loop);
18701
d439d286
JS
18702 spin_unlock_irq(&phba->hbalock);
18703
18704 /* Release the cleaned-up mailbox commands */
18705 while (!list_empty(&mbox_cmd_list)) {
18706 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18707 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18708 mp = (struct lpfc_dmabuf *) (mb->context1);
18709 if (mp) {
18710 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18711 kfree(mp);
18712 }
78730cfe 18713 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18714 mb->context2 = NULL;
78730cfe 18715 if (ndlp) {
ec21b3b0 18716 spin_lock(shost->host_lock);
589a52d6 18717 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18718 spin_unlock(shost->host_lock);
78730cfe 18719 lpfc_nlp_put(ndlp);
78730cfe 18720 }
695a814e 18721 }
695a814e
JS
18722 mempool_free(mb, phba->mbox_mem_pool);
18723 }
d439d286
JS
18724
18725 /* Release the ndlp with the cleaned-up active mailbox command */
18726 if (act_mbx_ndlp) {
18727 spin_lock(shost->host_lock);
18728 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18729 spin_unlock(shost->host_lock);
18730 lpfc_nlp_put(act_mbx_ndlp);
695a814e 18731 }
695a814e
JS
18732}
18733
2a9bf3d0
JS
18734/**
18735 * lpfc_drain_txq - Drain the txq
18736 * @phba: Pointer to HBA context object.
18737 *
18738 * This function attempt to submit IOCBs on the txq
18739 * to the adapter. For SLI4 adapters, the txq contains
18740 * ELS IOCBs that have been deferred because the there
18741 * are no SGLs. This congestion can occur with large
18742 * vport counts during node discovery.
18743 **/
18744
18745uint32_t
18746lpfc_drain_txq(struct lpfc_hba *phba)
18747{
18748 LIST_HEAD(completions);
895427bd 18749 struct lpfc_sli_ring *pring;
2e706377 18750 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
18751 unsigned long iflags = 0;
18752 char *fail_msg = NULL;
18753 struct lpfc_sglq *sglq;
2f07784f
JS
18754 union lpfc_wqe128 wqe128;
18755 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 18756 uint32_t txq_cnt = 0;
2a9bf3d0 18757
895427bd 18758 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
18759 if (unlikely(!pring))
18760 return 0;
895427bd 18761
398d81c9 18762 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
18763 list_for_each_entry(piocbq, &pring->txq, list) {
18764 txq_cnt++;
18765 }
18766
18767 if (txq_cnt > pring->txq_max)
18768 pring->txq_max = txq_cnt;
2a9bf3d0 18769
398d81c9 18770 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18771
0e9bb8d7 18772 while (!list_empty(&pring->txq)) {
398d81c9 18773 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 18774
19ca7609 18775 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 18776 if (!piocbq) {
398d81c9 18777 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
18778 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18779 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 18780 txq_cnt);
a629852a
JS
18781 break;
18782 }
895427bd 18783 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 18784 if (!sglq) {
19ca7609 18785 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 18786 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18787 break;
2a9bf3d0 18788 }
0e9bb8d7 18789 txq_cnt--;
2a9bf3d0
JS
18790
18791 /* The xri and iocb resources secured,
18792 * attempt to issue request
18793 */
6d368e53 18794 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
18795 piocbq->sli4_xritag = sglq->sli4_xritag;
18796 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18797 fail_msg = "to convert bpl to sgl";
2f07784f 18798 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 18799 fail_msg = "to convert iocb to wqe";
2f07784f 18800 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
18801 fail_msg = " - Wq is full";
18802 else
18803 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18804
18805 if (fail_msg) {
18806 /* Failed means we can't issue and need to cancel */
18807 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18808 "2822 IOCB failed %s iotag 0x%x "
18809 "xri 0x%x\n",
18810 fail_msg,
18811 piocbq->iotag, piocbq->sli4_xritag);
18812 list_add_tail(&piocbq->list, &completions);
18813 }
398d81c9 18814 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
18815 }
18816
2a9bf3d0
JS
18817 /* Cancel all the IOCBs that cannot be issued */
18818 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18819 IOERR_SLI_ABORTED);
18820
0e9bb8d7 18821 return txq_cnt;
2a9bf3d0 18822}
895427bd
JS
18823
18824/**
18825 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18826 * @phba: Pointer to HBA context object.
18827 * @pwqe: Pointer to command WQE.
18828 * @sglq: Pointer to the scatter gather queue object.
18829 *
18830 * This routine converts the bpl or bde that is in the WQE
18831 * to a sgl list for the sli4 hardware. The physical address
18832 * of the bpl/bde is converted back to a virtual address.
18833 * If the WQE contains a BPL then the list of BDE's is
18834 * converted to sli4_sge's. If the WQE contains a single
18835 * BDE then it is converted to a single sli_sge.
18836 * The WQE is still in cpu endianness so the contents of
18837 * the bpl can be used without byte swapping.
18838 *
18839 * Returns valid XRI = Success, NO_XRI = Failure.
18840 */
18841static uint16_t
18842lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18843 struct lpfc_sglq *sglq)
18844{
18845 uint16_t xritag = NO_XRI;
18846 struct ulp_bde64 *bpl = NULL;
18847 struct ulp_bde64 bde;
18848 struct sli4_sge *sgl = NULL;
18849 struct lpfc_dmabuf *dmabuf;
18850 union lpfc_wqe *wqe;
18851 int numBdes = 0;
18852 int i = 0;
18853 uint32_t offset = 0; /* accumulated offset in the sg request list */
18854 int inbound = 0; /* number of sg reply entries inbound from firmware */
18855 uint32_t cmd;
18856
18857 if (!pwqeq || !sglq)
18858 return xritag;
18859
18860 sgl = (struct sli4_sge *)sglq->sgl;
18861 wqe = &pwqeq->wqe;
18862 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18863
18864 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18865 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18866 return sglq->sli4_xritag;
18867 numBdes = pwqeq->rsvd2;
18868 if (numBdes) {
18869 /* The addrHigh and addrLow fields within the WQE
18870 * have not been byteswapped yet so there is no
18871 * need to swap them back.
18872 */
18873 if (pwqeq->context3)
18874 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18875 else
18876 return xritag;
18877
18878 bpl = (struct ulp_bde64 *)dmabuf->virt;
18879 if (!bpl)
18880 return xritag;
18881
18882 for (i = 0; i < numBdes; i++) {
18883 /* Should already be byte swapped. */
18884 sgl->addr_hi = bpl->addrHigh;
18885 sgl->addr_lo = bpl->addrLow;
18886
18887 sgl->word2 = le32_to_cpu(sgl->word2);
18888 if ((i+1) == numBdes)
18889 bf_set(lpfc_sli4_sge_last, sgl, 1);
18890 else
18891 bf_set(lpfc_sli4_sge_last, sgl, 0);
18892 /* swap the size field back to the cpu so we
18893 * can assign it to the sgl.
18894 */
18895 bde.tus.w = le32_to_cpu(bpl->tus.w);
18896 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18897 /* The offsets in the sgl need to be accumulated
18898 * separately for the request and reply lists.
18899 * The request is always first, the reply follows.
18900 */
18901 switch (cmd) {
18902 case CMD_GEN_REQUEST64_WQE:
18903 /* add up the reply sg entries */
18904 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18905 inbound++;
18906 /* first inbound? reset the offset */
18907 if (inbound == 1)
18908 offset = 0;
18909 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18910 bf_set(lpfc_sli4_sge_type, sgl,
18911 LPFC_SGE_TYPE_DATA);
18912 offset += bde.tus.f.bdeSize;
18913 break;
18914 case CMD_FCP_TRSP64_WQE:
18915 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18916 bf_set(lpfc_sli4_sge_type, sgl,
18917 LPFC_SGE_TYPE_DATA);
18918 break;
18919 case CMD_FCP_TSEND64_WQE:
18920 case CMD_FCP_TRECEIVE64_WQE:
18921 bf_set(lpfc_sli4_sge_type, sgl,
18922 bpl->tus.f.bdeFlags);
18923 if (i < 3)
18924 offset = 0;
18925 else
18926 offset += bde.tus.f.bdeSize;
18927 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18928 break;
18929 }
18930 sgl->word2 = cpu_to_le32(sgl->word2);
18931 bpl++;
18932 sgl++;
18933 }
18934 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18935 /* The addrHigh and addrLow fields of the BDE have not
18936 * been byteswapped yet so they need to be swapped
18937 * before putting them in the sgl.
18938 */
18939 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18940 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18941 sgl->word2 = le32_to_cpu(sgl->word2);
18942 bf_set(lpfc_sli4_sge_last, sgl, 1);
18943 sgl->word2 = cpu_to_le32(sgl->word2);
18944 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18945 }
18946 return sglq->sli4_xritag;
18947}
18948
18949/**
18950 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18951 * @phba: Pointer to HBA context object.
18952 * @ring_number: Base sli ring number
18953 * @pwqe: Pointer to command WQE.
18954 **/
18955int
18956lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18957 struct lpfc_iocbq *pwqe)
18958{
18959 union lpfc_wqe *wqe = &pwqe->wqe;
f358dd0c 18960 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
18961 struct lpfc_queue *wq;
18962 struct lpfc_sglq *sglq;
18963 struct lpfc_sli_ring *pring;
18964 unsigned long iflags;
cd22d605 18965 uint32_t ret = 0;
895427bd
JS
18966
18967 /* NVME_LS and NVME_LS ABTS requests. */
18968 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18969 pring = phba->sli4_hba.nvmels_wq->pring;
18970 spin_lock_irqsave(&pring->ring_lock, iflags);
18971 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18972 if (!sglq) {
18973 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18974 return WQE_BUSY;
18975 }
18976 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18977 pwqe->sli4_xritag = sglq->sli4_xritag;
18978 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18979 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18980 return WQE_ERROR;
18981 }
18982 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18983 pwqe->sli4_xritag);
cd22d605
DK
18984 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
18985 if (ret) {
895427bd 18986 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 18987 return ret;
895427bd 18988 }
cd22d605 18989
895427bd
JS
18990 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18991 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18992 return 0;
18993 }
18994
18995 /* NVME_FCREQ and NVME_ABTS requests */
18996 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18997 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18998 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18999
19000 spin_lock_irqsave(&pring->ring_lock, iflags);
19001 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19002 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19003 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19004 ret = lpfc_sli4_wq_put(wq, wqe);
19005 if (ret) {
895427bd 19006 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19007 return ret;
895427bd
JS
19008 }
19009 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19010 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19011 return 0;
19012 }
19013
f358dd0c
JS
19014 /* NVMET requests */
19015 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19016 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19017 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19018
19019 spin_lock_irqsave(&pring->ring_lock, iflags);
19020 ctxp = pwqe->context2;
6c621a22 19021 sglq = ctxp->ctxbuf->sglq;
f358dd0c
JS
19022 if (pwqe->sli4_xritag == NO_XRI) {
19023 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19024 pwqe->sli4_xritag = sglq->sli4_xritag;
19025 }
19026 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19027 pwqe->sli4_xritag);
19028 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19029 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19030 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
cd22d605
DK
19031 ret = lpfc_sli4_wq_put(wq, wqe);
19032 if (ret) {
f358dd0c 19033 spin_unlock_irqrestore(&pring->ring_lock, iflags);
cd22d605 19034 return ret;
f358dd0c
JS
19035 }
19036 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19037 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19038 return 0;
19039 }
895427bd
JS
19040 return WQE_ERROR;
19041}