]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/lpfc/lpfc_sli.c
scsi: lpfc: NVME Target: Add debugfs support
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
50611577 4 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e
JB
20 *******************************************************************/
21
dea3101e
JB
22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
5a0e3ad6 26#include <linux/slab.h>
1c2ba475 27#include <linux/lockdep.h>
dea3101e 28
91886523 29#include <scsi/scsi.h>
dea3101e
JB
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
f888ba3c 33#include <scsi/scsi_transport_fc.h>
da0436e9 34#include <scsi/fc/fc_fs.h>
0d878419 35#include <linux/aer.h>
dea3101e 36
895427bd
JS
37#include <linux/nvme-fc-driver.h>
38
da0436e9 39#include "lpfc_hw4.h"
dea3101e
JB
40#include "lpfc_hw.h"
41#include "lpfc_sli.h"
da0436e9 42#include "lpfc_sli4.h"
ea2151b4 43#include "lpfc_nl.h"
dea3101e 44#include "lpfc_disc.h"
dea3101e 45#include "lpfc.h"
895427bd
JS
46#include "lpfc_scsi.h"
47#include "lpfc_nvme.h"
f358dd0c 48#include "lpfc_nvmet.h"
dea3101e
JB
49#include "lpfc_crtn.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_compat.h"
858c9f6c 52#include "lpfc_debugfs.h"
04c68496 53#include "lpfc_vport.h"
61bda8f7 54#include "lpfc_version.h"
dea3101e
JB
55
56/* There are only four IOCB completion types. */
57typedef enum _lpfc_iocb_type {
58 LPFC_UNKNOWN_IOCB,
59 LPFC_UNSOL_IOCB,
60 LPFC_SOL_IOCB,
61 LPFC_ABORT_IOCB
62} lpfc_iocb_type;
63
4f774513
JS
64
65/* Provide function prototypes local to this module. */
66static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
67 uint32_t);
68static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
69 uint8_t *, uint32_t *);
70static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
71 struct lpfc_iocbq *);
6669f9bb
JS
72static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
73 struct hbq_dmabuf *);
895427bd 74static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
0558056c 75 struct lpfc_cqe *);
895427bd 76static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
8a9d2e80 77 int);
ba20c853
JS
78static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
79 uint32_t);
e8d3c3b1
JS
80static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
81static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
895427bd
JS
82static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
83 struct lpfc_sli_ring *pring,
84 struct lpfc_iocbq *cmdiocb);
0558056c 85
4f774513
JS
86static IOCB_t *
87lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
88{
89 return &iocbq->iocb;
90}
91
92/**
93 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
94 * @q: The Work Queue to operate on.
95 * @wqe: The work Queue Entry to put on the Work queue.
96 *
97 * This routine will copy the contents of @wqe to the next available entry on
98 * the @q. This function will then ring the Work Queue Doorbell to signal the
99 * HBA to start processing the Work Queue Entry. This function returns 0 if
100 * successful. If no entries are available on @q then this function will return
101 * -ENOMEM.
102 * The caller is expected to hold the hbalock when calling this routine.
103 **/
104static uint32_t
105lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
106{
2e90f4b5 107 union lpfc_wqe *temp_wqe;
4f774513
JS
108 struct lpfc_register doorbell;
109 uint32_t host_index;
027140ea 110 uint32_t idx;
4f774513 111
2e90f4b5
JS
112 /* sanity check on queue memory */
113 if (unlikely(!q))
114 return -ENOMEM;
115 temp_wqe = q->qe[q->host_index].wqe;
116
4f774513 117 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
118 idx = ((q->host_index + 1) % q->entry_count);
119 if (idx == q->hba_index) {
b84daac9 120 q->WQ_overflow++;
4f774513 121 return -ENOMEM;
b84daac9
JS
122 }
123 q->WQ_posted++;
4f774513 124 /* set consumption flag every once in a while */
ff78d8f9 125 if (!((q->host_index + 1) % q->entry_repost))
f0d9bccc 126 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
127 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
128 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513 129 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
6b3b3bdb
JS
130 /* ensure WQE bcopy flushed before doorbell write */
131 wmb();
4f774513
JS
132
133 /* Update the host index before invoking device */
134 host_index = q->host_index;
027140ea
JS
135
136 q->host_index = idx;
4f774513
JS
137
138 /* Ring Doorbell */
139 doorbell.word0 = 0;
962bc51b
JS
140 if (q->db_format == LPFC_DB_LIST_FORMAT) {
141 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
142 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
143 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
144 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
145 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
146 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
147 } else {
148 return -EINVAL;
149 }
150 writel(doorbell.word0, q->db_regaddr);
4f774513
JS
151
152 return 0;
153}
154
155/**
156 * lpfc_sli4_wq_release - Updates internal hba index for WQ
157 * @q: The Work Queue to operate on.
158 * @index: The index to advance the hba index to.
159 *
160 * This routine will update the HBA index of a queue to reflect consumption of
161 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
162 * an entry the host calls this function to update the queue's internal
163 * pointers. This routine returns the number of entries that were consumed by
164 * the HBA.
165 **/
166static uint32_t
167lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
168{
169 uint32_t released = 0;
170
2e90f4b5
JS
171 /* sanity check on queue memory */
172 if (unlikely(!q))
173 return 0;
174
4f774513
JS
175 if (q->hba_index == index)
176 return 0;
177 do {
178 q->hba_index = ((q->hba_index + 1) % q->entry_count);
179 released++;
180 } while (q->hba_index != index);
181 return released;
182}
183
184/**
185 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
186 * @q: The Mailbox Queue to operate on.
187 * @wqe: The Mailbox Queue Entry to put on the Work queue.
188 *
189 * This routine will copy the contents of @mqe to the next available entry on
190 * the @q. This function will then ring the Work Queue Doorbell to signal the
191 * HBA to start processing the Work Queue Entry. This function returns 0 if
192 * successful. If no entries are available on @q then this function will return
193 * -ENOMEM.
194 * The caller is expected to hold the hbalock when calling this routine.
195 **/
196static uint32_t
197lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
198{
2e90f4b5 199 struct lpfc_mqe *temp_mqe;
4f774513 200 struct lpfc_register doorbell;
4f774513 201
2e90f4b5
JS
202 /* sanity check on queue memory */
203 if (unlikely(!q))
204 return -ENOMEM;
205 temp_mqe = q->qe[q->host_index].mqe;
206
4f774513
JS
207 /* If the host has not yet processed the next entry then we are done */
208 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
209 return -ENOMEM;
210 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
211 /* Save off the mailbox pointer for completion */
212 q->phba->mbox = (MAILBOX_t *)temp_mqe;
213
214 /* Update the host index before invoking device */
4f774513
JS
215 q->host_index = ((q->host_index + 1) % q->entry_count);
216
217 /* Ring Doorbell */
218 doorbell.word0 = 0;
219 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
220 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
221 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
4f774513
JS
222 return 0;
223}
224
225/**
226 * lpfc_sli4_mq_release - Updates internal hba index for MQ
227 * @q: The Mailbox Queue to operate on.
228 *
229 * This routine will update the HBA index of a queue to reflect consumption of
230 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
231 * an entry the host calls this function to update the queue's internal
232 * pointers. This routine returns the number of entries that were consumed by
233 * the HBA.
234 **/
235static uint32_t
236lpfc_sli4_mq_release(struct lpfc_queue *q)
237{
2e90f4b5
JS
238 /* sanity check on queue memory */
239 if (unlikely(!q))
240 return 0;
241
4f774513
JS
242 /* Clear the mailbox pointer for completion */
243 q->phba->mbox = NULL;
244 q->hba_index = ((q->hba_index + 1) % q->entry_count);
245 return 1;
246}
247
248/**
249 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
250 * @q: The Event Queue to get the first valid EQE from
251 *
252 * This routine will get the first valid Event Queue Entry from @q, update
253 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
254 * the Queue (no more work to do), or the Queue is full of EQEs that have been
255 * processed, but not popped back to the HBA then this routine will return NULL.
256 **/
257static struct lpfc_eqe *
258lpfc_sli4_eq_get(struct lpfc_queue *q)
259{
2e90f4b5 260 struct lpfc_eqe *eqe;
027140ea 261 uint32_t idx;
2e90f4b5
JS
262
263 /* sanity check on queue memory */
264 if (unlikely(!q))
265 return NULL;
266 eqe = q->qe[q->hba_index].eqe;
4f774513
JS
267
268 /* If the next EQE is not valid then we are done */
cb5172ea 269 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
270 return NULL;
271 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
272 idx = ((q->hba_index + 1) % q->entry_count);
273 if (idx == q->host_index)
4f774513
JS
274 return NULL;
275
027140ea 276 q->hba_index = idx;
27f344eb
JS
277
278 /*
279 * insert barrier for instruction interlock : data from the hardware
280 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
281 * upon. Speculative instructions were allowing a bcopy at the start
282 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
283 * after our return, to copy data before the valid bit check above
284 * was done. As such, some of the copied data was stale. The barrier
285 * ensures the check is before any data is copied.
27f344eb
JS
286 */
287 mb();
4f774513
JS
288 return eqe;
289}
290
ba20c853
JS
291/**
292 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
293 * @q: The Event Queue to disable interrupts
294 *
295 **/
296static inline void
297lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
298{
299 struct lpfc_register doorbell;
300
301 doorbell.word0 = 0;
302 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
303 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
304 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
305 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
306 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
307 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
308}
309
4f774513
JS
310/**
311 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
312 * @q: The Event Queue that the host has completed processing for.
313 * @arm: Indicates whether the host wants to arms this CQ.
314 *
315 * This routine will mark all Event Queue Entries on @q, from the last
316 * known completed entry to the last entry that was processed, as completed
317 * by clearing the valid bit for each completion queue entry. Then it will
318 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
319 * The internal host index in the @q will be updated by this routine to indicate
320 * that the host has finished processing the entries. The @arm parameter
321 * indicates that the queue should be rearmed when ringing the doorbell.
322 *
323 * This function will return the number of EQEs that were popped.
324 **/
325uint32_t
326lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
327{
328 uint32_t released = 0;
329 struct lpfc_eqe *temp_eqe;
330 struct lpfc_register doorbell;
331
2e90f4b5
JS
332 /* sanity check on queue memory */
333 if (unlikely(!q))
334 return 0;
335
4f774513
JS
336 /* while there are valid entries */
337 while (q->hba_index != q->host_index) {
338 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 339 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
340 released++;
341 q->host_index = ((q->host_index + 1) % q->entry_count);
342 }
343 if (unlikely(released == 0 && !arm))
344 return 0;
345
346 /* ring doorbell for number popped */
347 doorbell.word0 = 0;
348 if (arm) {
349 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
350 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
351 }
352 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
353 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
6b5151fd
JS
354 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
355 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
356 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
4f774513 357 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
358 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
359 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
360 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
361 return released;
362}
363
364/**
365 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
366 * @q: The Completion Queue to get the first valid CQE from
367 *
368 * This routine will get the first valid Completion Queue Entry from @q, update
369 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
370 * the Queue (no more work to do), or the Queue is full of CQEs that have been
371 * processed, but not popped back to the HBA then this routine will return NULL.
372 **/
373static struct lpfc_cqe *
374lpfc_sli4_cq_get(struct lpfc_queue *q)
375{
376 struct lpfc_cqe *cqe;
027140ea 377 uint32_t idx;
4f774513 378
2e90f4b5
JS
379 /* sanity check on queue memory */
380 if (unlikely(!q))
381 return NULL;
382
4f774513 383 /* If the next CQE is not valid then we are done */
cb5172ea 384 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
385 return NULL;
386 /* If the host has not yet processed the next entry then we are done */
027140ea
JS
387 idx = ((q->hba_index + 1) % q->entry_count);
388 if (idx == q->host_index)
4f774513
JS
389 return NULL;
390
391 cqe = q->qe[q->hba_index].cqe;
027140ea 392 q->hba_index = idx;
27f344eb
JS
393
394 /*
395 * insert barrier for instruction interlock : data from the hardware
396 * must have the valid bit checked before it can be copied and acted
2ea259ee
JS
397 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
398 * instructions allowing action on content before valid bit checked,
399 * add barrier here as well. May not be needed as "content" is a
400 * single 32-bit entity here (vs multi word structure for cq's).
27f344eb
JS
401 */
402 mb();
4f774513
JS
403 return cqe;
404}
405
406/**
407 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
408 * @q: The Completion Queue that the host has completed processing for.
409 * @arm: Indicates whether the host wants to arms this CQ.
410 *
411 * This routine will mark all Completion queue entries on @q, from the last
412 * known completed entry to the last entry that was processed, as completed
413 * by clearing the valid bit for each completion queue entry. Then it will
414 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
415 * The internal host index in the @q will be updated by this routine to indicate
416 * that the host has finished processing the entries. The @arm parameter
417 * indicates that the queue should be rearmed when ringing the doorbell.
418 *
419 * This function will return the number of CQEs that were released.
420 **/
421uint32_t
422lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
423{
424 uint32_t released = 0;
425 struct lpfc_cqe *temp_qe;
426 struct lpfc_register doorbell;
427
2e90f4b5
JS
428 /* sanity check on queue memory */
429 if (unlikely(!q))
430 return 0;
4f774513
JS
431 /* while there are valid entries */
432 while (q->hba_index != q->host_index) {
433 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 434 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
435 released++;
436 q->host_index = ((q->host_index + 1) % q->entry_count);
437 }
438 if (unlikely(released == 0 && !arm))
439 return 0;
440
441 /* ring doorbell for number popped */
442 doorbell.word0 = 0;
443 if (arm)
444 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
445 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
446 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
6b5151fd
JS
447 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
448 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
449 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
4f774513
JS
450 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
451 return released;
452}
453
454/**
455 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
456 * @q: The Header Receive Queue to operate on.
457 * @wqe: The Receive Queue Entry to put on the Receive queue.
458 *
459 * This routine will copy the contents of @wqe to the next available entry on
460 * the @q. This function will then ring the Receive Queue Doorbell to signal the
461 * HBA to start processing the Receive Queue Entry. This function returns the
462 * index that the rqe was copied to if successful. If no entries are available
463 * on @q then this function will return -ENOMEM.
464 * The caller is expected to hold the hbalock when calling this routine.
465 **/
895427bd 466int
4f774513
JS
467lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
468 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
469{
2e90f4b5
JS
470 struct lpfc_rqe *temp_hrqe;
471 struct lpfc_rqe *temp_drqe;
4f774513 472 struct lpfc_register doorbell;
5a25bf36 473 int put_index;
4f774513 474
2e90f4b5
JS
475 /* sanity check on queue memory */
476 if (unlikely(!hq) || unlikely(!dq))
477 return -ENOMEM;
5a25bf36 478 put_index = hq->host_index;
2e90f4b5
JS
479 temp_hrqe = hq->qe[hq->host_index].rqe;
480 temp_drqe = dq->qe[dq->host_index].rqe;
481
4f774513
JS
482 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
483 return -EINVAL;
484 if (hq->host_index != dq->host_index)
485 return -EINVAL;
486 /* If the host has not yet processed the next entry then we are done */
487 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
488 return -EBUSY;
489 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
490 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
491
492 /* Update the host index to point to the next slot */
493 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
494 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
495
496 /* Ring The Header Receive Queue Doorbell */
73d91e50 497 if (!(hq->host_index % hq->entry_repost)) {
4f774513 498 doorbell.word0 = 0;
962bc51b
JS
499 if (hq->db_format == LPFC_DB_RING_FORMAT) {
500 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
501 hq->entry_repost);
502 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
503 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
504 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
505 hq->entry_repost);
506 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
507 hq->host_index);
508 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
509 } else {
510 return -EINVAL;
511 }
512 writel(doorbell.word0, hq->db_regaddr);
4f774513
JS
513 }
514 return put_index;
515}
516
517/**
518 * lpfc_sli4_rq_release - Updates internal hba index for RQ
519 * @q: The Header Receive Queue to operate on.
520 *
521 * This routine will update the HBA index of a queue to reflect consumption of
522 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
523 * consumed an entry the host calls this function to update the queue's
524 * internal pointers. This routine returns the number of entries that were
525 * consumed by the HBA.
526 **/
527static uint32_t
528lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
529{
2e90f4b5
JS
530 /* sanity check on queue memory */
531 if (unlikely(!hq) || unlikely(!dq))
532 return 0;
533
4f774513
JS
534 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
535 return 0;
536 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
537 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
538 return 1;
539}
540
e59058c4 541/**
3621a710 542 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
543 * @phba: Pointer to HBA context object.
544 * @pring: Pointer to driver SLI ring object.
545 *
546 * This function returns pointer to next command iocb entry
547 * in the command ring. The caller must hold hbalock to prevent
548 * other threads consume the next command iocb.
549 * SLI-2/SLI-3 provide different sized iocbs.
550 **/
ed957684
JS
551static inline IOCB_t *
552lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
553{
7e56aa25
JS
554 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
555 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
ed957684
JS
556}
557
e59058c4 558/**
3621a710 559 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
560 * @phba: Pointer to HBA context object.
561 * @pring: Pointer to driver SLI ring object.
562 *
563 * This function returns pointer to next response iocb entry
564 * in the response ring. The caller must hold hbalock to make sure
565 * that no other thread consume the next response iocb.
566 * SLI-2/SLI-3 provide different sized iocbs.
567 **/
ed957684
JS
568static inline IOCB_t *
569lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
570{
7e56aa25
JS
571 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
572 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
ed957684
JS
573}
574
e59058c4 575/**
3621a710 576 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
577 * @phba: Pointer to HBA context object.
578 *
579 * This function is called with hbalock held. This function
580 * allocates a new driver iocb object from the iocb pool. If the
581 * allocation is successful, it returns pointer to the newly
582 * allocated iocb object else it returns NULL.
583 **/
4f2e66c6 584struct lpfc_iocbq *
2e0fef85 585__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
586{
587 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
588 struct lpfc_iocbq * iocbq = NULL;
589
1c2ba475
JT
590 lockdep_assert_held(&phba->hbalock);
591
0bd4ca25 592 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
593 if (iocbq)
594 phba->iocb_cnt++;
595 if (phba->iocb_cnt > phba->iocb_max)
596 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
597 return iocbq;
598}
599
da0436e9
JS
600/**
601 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
602 * @phba: Pointer to HBA context object.
603 * @xritag: XRI value.
604 *
605 * This function clears the sglq pointer from the array of acive
606 * sglq's. The xritag that is passed in is used to index into the
607 * array. Before the xritag can be used it needs to be adjusted
608 * by subtracting the xribase.
609 *
610 * Returns sglq ponter = success, NULL = Failure.
611 **/
895427bd 612struct lpfc_sglq *
da0436e9
JS
613__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
614{
da0436e9 615 struct lpfc_sglq *sglq;
6d368e53
JS
616
617 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
618 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
619 return sglq;
620}
621
622/**
623 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
624 * @phba: Pointer to HBA context object.
625 * @xritag: XRI value.
626 *
627 * This function returns the sglq pointer from the array of acive
628 * sglq's. The xritag that is passed in is used to index into the
629 * array. Before the xritag can be used it needs to be adjusted
630 * by subtracting the xribase.
631 *
632 * Returns sglq ponter = success, NULL = Failure.
633 **/
0f65ff68 634struct lpfc_sglq *
da0436e9
JS
635__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
636{
da0436e9 637 struct lpfc_sglq *sglq;
6d368e53
JS
638
639 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
640 return sglq;
641}
642
19ca7609 643/**
1151e3ec 644 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
645 * @phba: Pointer to HBA context object.
646 * @xritag: xri used in this exchange.
647 * @rrq: The RRQ to be cleared.
648 *
19ca7609 649 **/
1151e3ec
JS
650void
651lpfc_clr_rrq_active(struct lpfc_hba *phba,
652 uint16_t xritag,
653 struct lpfc_node_rrq *rrq)
19ca7609 654{
1151e3ec 655 struct lpfc_nodelist *ndlp = NULL;
19ca7609 656
1151e3ec
JS
657 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
658 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
659
660 /* The target DID could have been swapped (cable swap)
661 * we should use the ndlp from the findnode if it is
662 * available.
663 */
1151e3ec 664 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
665 ndlp = rrq->ndlp;
666
1151e3ec
JS
667 if (!ndlp)
668 goto out;
669
cff261f6 670 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
671 rrq->send_rrq = 0;
672 rrq->xritag = 0;
673 rrq->rrq_stop_time = 0;
674 }
1151e3ec 675out:
19ca7609
JS
676 mempool_free(rrq, phba->rrq_pool);
677}
678
679/**
680 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
681 * @phba: Pointer to HBA context object.
682 *
683 * This function is called with hbalock held. This function
684 * Checks if stop_time (ratov from setting rrq active) has
685 * been reached, if it has and the send_rrq flag is set then
686 * it will call lpfc_send_rrq. If the send_rrq flag is not set
687 * then it will just call the routine to clear the rrq and
688 * free the rrq resource.
689 * The timer is set to the next rrq that is going to expire before
690 * leaving the routine.
691 *
692 **/
693void
694lpfc_handle_rrq_active(struct lpfc_hba *phba)
695{
696 struct lpfc_node_rrq *rrq;
697 struct lpfc_node_rrq *nextrrq;
698 unsigned long next_time;
699 unsigned long iflags;
1151e3ec 700 LIST_HEAD(send_rrq);
19ca7609
JS
701
702 spin_lock_irqsave(&phba->hbalock, iflags);
703 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
256ec0d0 704 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
19ca7609 705 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
706 &phba->active_rrq_list, list) {
707 if (time_after(jiffies, rrq->rrq_stop_time))
708 list_move(&rrq->list, &send_rrq);
709 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
710 next_time = rrq->rrq_stop_time;
711 }
712 spin_unlock_irqrestore(&phba->hbalock, iflags);
06918ac5
JS
713 if ((!list_empty(&phba->active_rrq_list)) &&
714 (!(phba->pport->load_flag & FC_UNLOADING)))
19ca7609 715 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
716 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
717 list_del(&rrq->list);
718 if (!rrq->send_rrq)
719 /* this call will free the rrq */
720 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
721 else if (lpfc_send_rrq(phba, rrq)) {
722 /* if we send the rrq then the completion handler
723 * will clear the bit in the xribitmap.
724 */
725 lpfc_clr_rrq_active(phba, rrq->xritag,
726 rrq);
727 }
728 }
19ca7609
JS
729}
730
731/**
732 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
733 * @vport: Pointer to vport context object.
734 * @xri: The xri used in the exchange.
735 * @did: The targets DID for this exchange.
736 *
737 * returns NULL = rrq not found in the phba->active_rrq_list.
738 * rrq = rrq for this xri and target.
739 **/
740struct lpfc_node_rrq *
741lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
742{
743 struct lpfc_hba *phba = vport->phba;
744 struct lpfc_node_rrq *rrq;
745 struct lpfc_node_rrq *nextrrq;
746 unsigned long iflags;
747
748 if (phba->sli_rev != LPFC_SLI_REV4)
749 return NULL;
750 spin_lock_irqsave(&phba->hbalock, iflags);
751 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
752 if (rrq->vport == vport && rrq->xritag == xri &&
753 rrq->nlp_DID == did){
754 list_del(&rrq->list);
755 spin_unlock_irqrestore(&phba->hbalock, iflags);
756 return rrq;
757 }
758 }
759 spin_unlock_irqrestore(&phba->hbalock, iflags);
760 return NULL;
761}
762
763/**
764 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
765 * @vport: Pointer to vport context object.
1151e3ec
JS
766 * @ndlp: Pointer to the lpfc_node_list structure.
767 * If ndlp is NULL Remove all active RRQs for this vport from the
768 * phba->active_rrq_list and clear the rrq.
769 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
770 **/
771void
1151e3ec 772lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
773
774{
775 struct lpfc_hba *phba = vport->phba;
776 struct lpfc_node_rrq *rrq;
777 struct lpfc_node_rrq *nextrrq;
778 unsigned long iflags;
1151e3ec 779 LIST_HEAD(rrq_list);
19ca7609
JS
780
781 if (phba->sli_rev != LPFC_SLI_REV4)
782 return;
1151e3ec
JS
783 if (!ndlp) {
784 lpfc_sli4_vport_delete_els_xri_aborted(vport);
785 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 786 }
1151e3ec
JS
787 spin_lock_irqsave(&phba->hbalock, iflags);
788 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
789 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
790 list_move(&rrq->list, &rrq_list);
19ca7609 791 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
792
793 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
794 list_del(&rrq->list);
795 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
796 }
19ca7609
JS
797}
798
19ca7609 799/**
1151e3ec 800 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
801 * @phba: Pointer to HBA context object.
802 * @ndlp: Targets nodelist pointer for this exchange.
803 * @xritag the xri in the bitmap to test.
804 *
805 * This function is called with hbalock held. This function
806 * returns 0 = rrq not active for this xri
807 * 1 = rrq is valid for this xri.
808 **/
1151e3ec
JS
809int
810lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
811 uint16_t xritag)
812{
1c2ba475 813 lockdep_assert_held(&phba->hbalock);
19ca7609
JS
814 if (!ndlp)
815 return 0;
cff261f6
JS
816 if (!ndlp->active_rrqs_xri_bitmap)
817 return 0;
818 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
19ca7609
JS
819 return 1;
820 else
821 return 0;
822}
823
824/**
825 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
826 * @phba: Pointer to HBA context object.
827 * @ndlp: nodelist pointer for this target.
828 * @xritag: xri used in this exchange.
829 * @rxid: Remote Exchange ID.
830 * @send_rrq: Flag used to determine if we should send rrq els cmd.
831 *
832 * This function takes the hbalock.
833 * The active bit is always set in the active rrq xri_bitmap even
834 * if there is no slot avaiable for the other rrq information.
835 *
836 * returns 0 rrq actived for this xri
837 * < 0 No memory or invalid ndlp.
838 **/
839int
840lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
b42c07c8 841 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
19ca7609 842{
19ca7609 843 unsigned long iflags;
b42c07c8
JS
844 struct lpfc_node_rrq *rrq;
845 int empty;
846
847 if (!ndlp)
848 return -EINVAL;
849
850 if (!phba->cfg_enable_rrq)
851 return -EINVAL;
19ca7609
JS
852
853 spin_lock_irqsave(&phba->hbalock, iflags);
b42c07c8
JS
854 if (phba->pport->load_flag & FC_UNLOADING) {
855 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
856 goto out;
857 }
858
859 /*
860 * set the active bit even if there is no mem available.
861 */
862 if (NLP_CHK_FREE_REQ(ndlp))
863 goto out;
864
865 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
866 goto out;
867
cff261f6
JS
868 if (!ndlp->active_rrqs_xri_bitmap)
869 goto out;
870
871 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
b42c07c8
JS
872 goto out;
873
19ca7609 874 spin_unlock_irqrestore(&phba->hbalock, iflags);
b42c07c8
JS
875 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
876 if (!rrq) {
877 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
878 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
879 " DID:0x%x Send:%d\n",
880 xritag, rxid, ndlp->nlp_DID, send_rrq);
881 return -EINVAL;
882 }
e5771b4d
JS
883 if (phba->cfg_enable_rrq == 1)
884 rrq->send_rrq = send_rrq;
885 else
886 rrq->send_rrq = 0;
b42c07c8 887 rrq->xritag = xritag;
256ec0d0
JS
888 rrq->rrq_stop_time = jiffies +
889 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
b42c07c8
JS
890 rrq->ndlp = ndlp;
891 rrq->nlp_DID = ndlp->nlp_DID;
892 rrq->vport = ndlp->vport;
893 rrq->rxid = rxid;
b42c07c8
JS
894 spin_lock_irqsave(&phba->hbalock, iflags);
895 empty = list_empty(&phba->active_rrq_list);
896 list_add_tail(&rrq->list, &phba->active_rrq_list);
897 phba->hba_flag |= HBA_RRQ_ACTIVE;
898 if (empty)
899 lpfc_worker_wake_up(phba);
900 spin_unlock_irqrestore(&phba->hbalock, iflags);
901 return 0;
902out:
903 spin_unlock_irqrestore(&phba->hbalock, iflags);
904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
905 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
906 " DID:0x%x Send:%d\n",
907 xritag, rxid, ndlp->nlp_DID, send_rrq);
908 return -EINVAL;
19ca7609
JS
909}
910
da0436e9 911/**
895427bd 912 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
da0436e9 913 * @phba: Pointer to HBA context object.
19ca7609 914 * @piocb: Pointer to the iocbq.
da0436e9 915 *
dafe8cea 916 * This function is called with the ring lock held. This function
6d368e53 917 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
918 * list is not empty then it is successful, it returns pointer to the newly
919 * allocated sglq object else it returns NULL.
920 **/
921static struct lpfc_sglq *
895427bd 922__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9 923{
895427bd 924 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
da0436e9 925 struct lpfc_sglq *sglq = NULL;
19ca7609 926 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
927 struct lpfc_scsi_buf *lpfc_cmd;
928 struct lpfc_nodelist *ndlp;
929 int found = 0;
930
1c2ba475
JT
931 lockdep_assert_held(&phba->hbalock);
932
19ca7609
JS
933 if (piocbq->iocb_flag & LPFC_IO_FCP) {
934 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
935 ndlp = lpfc_cmd->rdata->pnode;
be858b65 936 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
6c7cf486 937 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
19ca7609 938 ndlp = piocbq->context_un.ndlp;
6c7cf486
JS
939 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
940 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
941 ndlp = NULL;
942 else
943 ndlp = piocbq->context_un.ndlp;
944 } else {
19ca7609 945 ndlp = piocbq->context1;
6c7cf486 946 }
19ca7609 947
895427bd
JS
948 spin_lock(&phba->sli4_hba.sgl_list_lock);
949 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
950 start_sglq = sglq;
951 while (!found) {
952 if (!sglq)
953 return NULL;
895427bd
JS
954 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
955 test_bit(sglq->sli4_lxritag,
956 ndlp->active_rrqs_xri_bitmap)) {
19ca7609
JS
957 /* This xri has an rrq outstanding for this DID.
958 * put it back in the list and get another xri.
959 */
895427bd 960 list_add_tail(&sglq->list, lpfc_els_sgl_list);
19ca7609 961 sglq = NULL;
895427bd 962 list_remove_head(lpfc_els_sgl_list, sglq,
19ca7609
JS
963 struct lpfc_sglq, list);
964 if (sglq == start_sglq) {
965 sglq = NULL;
966 break;
967 } else
968 continue;
969 }
970 sglq->ndlp = ndlp;
971 found = 1;
6d368e53 972 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
973 sglq->state = SGL_ALLOCATED;
974 }
895427bd 975 spin_unlock(&phba->sli4_hba.sgl_list_lock);
da0436e9
JS
976 return sglq;
977}
978
f358dd0c
JS
979/**
980 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
981 * @phba: Pointer to HBA context object.
982 * @piocb: Pointer to the iocbq.
983 *
984 * This function is called with the sgl_list lock held. This function
985 * gets a new driver sglq object from the sglq list. If the
986 * list is not empty then it is successful, it returns pointer to the newly
987 * allocated sglq object else it returns NULL.
988 **/
989struct lpfc_sglq *
990__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
991{
992 struct list_head *lpfc_nvmet_sgl_list;
993 struct lpfc_sglq *sglq = NULL;
994
995 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
996
997 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
998
999 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1000 if (!sglq)
1001 return NULL;
1002 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1003 sglq->state = SGL_ALLOCATED;
1004 return sglq;
1005}
1006
e59058c4 1007/**
3621a710 1008 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
1009 * @phba: Pointer to HBA context object.
1010 *
1011 * This function is called with no lock held. This function
1012 * allocates a new driver iocb object from the iocb pool. If the
1013 * allocation is successful, it returns pointer to the newly
1014 * allocated iocb object else it returns NULL.
1015 **/
2e0fef85
JS
1016struct lpfc_iocbq *
1017lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1018{
1019 struct lpfc_iocbq * iocbq = NULL;
1020 unsigned long iflags;
1021
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 iocbq = __lpfc_sli_get_iocbq(phba);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025 return iocbq;
1026}
1027
4f774513
JS
1028/**
1029 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1030 * @phba: Pointer to HBA context object.
1031 * @iocbq: Pointer to driver iocb object.
1032 *
1033 * This function is called with hbalock held to release driver
1034 * iocb object to the iocb pool. The iotag in the iocb object
1035 * does not change for each use of the iocb object. This function
1036 * clears all other fields of the iocb object when it is freed.
1037 * The sqlq structure that holds the xritag and phys and virtual
1038 * mappings for the scatter gather list is retrieved from the
1039 * active array of sglq. The get of the sglq pointer also clears
1040 * the entry in the array. If the status of the IO indiactes that
1041 * this IO was aborted then the sglq entry it put on the
1042 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1043 * IO has good status or fails for any other reason then the sglq
895427bd 1044 * entry is added to the free list (lpfc_els_sgl_list).
4f774513
JS
1045 **/
1046static void
1047__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1048{
1049 struct lpfc_sglq *sglq;
1050 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0 1051 unsigned long iflag = 0;
895427bd 1052 struct lpfc_sli_ring *pring;
4f774513 1053
1c2ba475
JT
1054 lockdep_assert_held(&phba->hbalock);
1055
4f774513
JS
1056 if (iocbq->sli4_xritag == NO_XRI)
1057 sglq = NULL;
1058 else
6d368e53
JS
1059 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1060
0e9bb8d7 1061
4f774513 1062 if (sglq) {
f358dd0c
JS
1063 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1064 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1065 iflag);
1066 sglq->state = SGL_FREED;
1067 sglq->ndlp = NULL;
1068 list_add_tail(&sglq->list,
1069 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1070 spin_unlock_irqrestore(
1071 &phba->sli4_hba.sgl_list_lock, iflag);
1072 goto out;
1073 }
1074
895427bd 1075 pring = phba->sli4_hba.els_wq->pring;
0f65ff68
JS
1076 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1077 (sglq->state != SGL_XRI_ABORTED)) {
895427bd
JS
1078 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1079 iflag);
4f774513 1080 list_add(&sglq->list,
895427bd 1081 &phba->sli4_hba.lpfc_abts_els_sgl_list);
4f774513 1082 spin_unlock_irqrestore(
895427bd 1083 &phba->sli4_hba.sgl_list_lock, iflag);
0f65ff68 1084 } else {
895427bd
JS
1085 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1086 iflag);
0f65ff68 1087 sglq->state = SGL_FREED;
19ca7609 1088 sglq->ndlp = NULL;
fedd3b7b 1089 list_add_tail(&sglq->list,
895427bd
JS
1090 &phba->sli4_hba.lpfc_els_sgl_list);
1091 spin_unlock_irqrestore(
1092 &phba->sli4_hba.sgl_list_lock, iflag);
2a9bf3d0
JS
1093
1094 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 1095 if (!list_empty(&pring->txq))
2a9bf3d0 1096 lpfc_worker_wake_up(phba);
0f65ff68 1097 }
4f774513
JS
1098 }
1099
f358dd0c 1100out:
4f774513
JS
1101 /*
1102 * Clean all volatile data fields, preserve iotag and node struct.
1103 */
1104 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 1105 iocbq->sli4_lxritag = NO_XRI;
4f774513 1106 iocbq->sli4_xritag = NO_XRI;
f358dd0c
JS
1107 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1108 LPFC_IO_NVME_LS);
4f774513
JS
1109 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1110}
1111
2a9bf3d0 1112
e59058c4 1113/**
3772a991 1114 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
1115 * @phba: Pointer to HBA context object.
1116 * @iocbq: Pointer to driver iocb object.
1117 *
1118 * This function is called with hbalock held to release driver
1119 * iocb object to the iocb pool. The iotag in the iocb object
1120 * does not change for each use of the iocb object. This function
1121 * clears all other fields of the iocb object when it is freed.
1122 **/
a6ababd2 1123static void
3772a991 1124__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 1125{
2e0fef85 1126 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30 1127
1c2ba475 1128 lockdep_assert_held(&phba->hbalock);
0e9bb8d7 1129
604a3e30
JB
1130 /*
1131 * Clean all volatile data fields, preserve iotag and node struct.
1132 */
1133 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 1134 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
1135 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1136}
1137
3772a991
JS
1138/**
1139 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1140 * @phba: Pointer to HBA context object.
1141 * @iocbq: Pointer to driver iocb object.
1142 *
1143 * This function is called with hbalock held to release driver
1144 * iocb object to the iocb pool. The iotag in the iocb object
1145 * does not change for each use of the iocb object. This function
1146 * clears all other fields of the iocb object when it is freed.
1147 **/
1148static void
1149__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1150{
1c2ba475
JT
1151 lockdep_assert_held(&phba->hbalock);
1152
3772a991 1153 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1154 phba->iocb_cnt--;
3772a991
JS
1155}
1156
e59058c4 1157/**
3621a710 1158 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1159 * @phba: Pointer to HBA context object.
1160 * @iocbq: Pointer to driver iocb object.
1161 *
1162 * This function is called with no lock held to release the iocb to
1163 * iocb pool.
1164 **/
2e0fef85
JS
1165void
1166lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1167{
1168 unsigned long iflags;
1169
1170 /*
1171 * Clean all volatile data fields, preserve iotag and node struct.
1172 */
1173 spin_lock_irqsave(&phba->hbalock, iflags);
1174 __lpfc_sli_release_iocbq(phba, iocbq);
1175 spin_unlock_irqrestore(&phba->hbalock, iflags);
1176}
1177
a257bf90
JS
1178/**
1179 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1180 * @phba: Pointer to HBA context object.
1181 * @iocblist: List of IOCBs.
1182 * @ulpstatus: ULP status in IOCB command field.
1183 * @ulpWord4: ULP word-4 in IOCB command field.
1184 *
1185 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1186 * on the list by invoking the complete callback function associated with the
1187 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1188 * fields.
1189 **/
1190void
1191lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1192 uint32_t ulpstatus, uint32_t ulpWord4)
1193{
1194 struct lpfc_iocbq *piocb;
1195
1196 while (!list_empty(iocblist)) {
1197 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
a257bf90
JS
1198 if (!piocb->iocb_cmpl)
1199 lpfc_sli_release_iocbq(phba, piocb);
1200 else {
1201 piocb->iocb.ulpStatus = ulpstatus;
1202 piocb->iocb.un.ulpWord[4] = ulpWord4;
1203 (piocb->iocb_cmpl) (phba, piocb, piocb);
1204 }
1205 }
1206 return;
1207}
1208
e59058c4 1209/**
3621a710
JS
1210 * lpfc_sli_iocb_cmd_type - Get the iocb type
1211 * @iocb_cmnd: iocb command code.
e59058c4
JS
1212 *
1213 * This function is called by ring event handler function to get the iocb type.
1214 * This function translates the iocb command to an iocb command type used to
1215 * decide the final disposition of each completed IOCB.
1216 * The function returns
1217 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1218 * LPFC_SOL_IOCB if it is a solicited iocb completion
1219 * LPFC_ABORT_IOCB if it is an abort iocb
1220 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1221 *
1222 * The caller is not required to hold any lock.
1223 **/
dea3101e
JB
1224static lpfc_iocb_type
1225lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1226{
1227 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1228
1229 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1230 return 0;
1231
1232 switch (iocb_cmnd) {
1233 case CMD_XMIT_SEQUENCE_CR:
1234 case CMD_XMIT_SEQUENCE_CX:
1235 case CMD_XMIT_BCAST_CN:
1236 case CMD_XMIT_BCAST_CX:
1237 case CMD_ELS_REQUEST_CR:
1238 case CMD_ELS_REQUEST_CX:
1239 case CMD_CREATE_XRI_CR:
1240 case CMD_CREATE_XRI_CX:
1241 case CMD_GET_RPI_CN:
1242 case CMD_XMIT_ELS_RSP_CX:
1243 case CMD_GET_RPI_CR:
1244 case CMD_FCP_IWRITE_CR:
1245 case CMD_FCP_IWRITE_CX:
1246 case CMD_FCP_IREAD_CR:
1247 case CMD_FCP_IREAD_CX:
1248 case CMD_FCP_ICMND_CR:
1249 case CMD_FCP_ICMND_CX:
f5603511
JS
1250 case CMD_FCP_TSEND_CX:
1251 case CMD_FCP_TRSP_CX:
1252 case CMD_FCP_TRECEIVE_CX:
1253 case CMD_FCP_AUTO_TRSP_CX:
dea3101e
JB
1254 case CMD_ADAPTER_MSG:
1255 case CMD_ADAPTER_DUMP:
1256 case CMD_XMIT_SEQUENCE64_CR:
1257 case CMD_XMIT_SEQUENCE64_CX:
1258 case CMD_XMIT_BCAST64_CN:
1259 case CMD_XMIT_BCAST64_CX:
1260 case CMD_ELS_REQUEST64_CR:
1261 case CMD_ELS_REQUEST64_CX:
1262 case CMD_FCP_IWRITE64_CR:
1263 case CMD_FCP_IWRITE64_CX:
1264 case CMD_FCP_IREAD64_CR:
1265 case CMD_FCP_IREAD64_CX:
1266 case CMD_FCP_ICMND64_CR:
1267 case CMD_FCP_ICMND64_CX:
f5603511
JS
1268 case CMD_FCP_TSEND64_CX:
1269 case CMD_FCP_TRSP64_CX:
1270 case CMD_FCP_TRECEIVE64_CX:
dea3101e
JB
1271 case CMD_GEN_REQUEST64_CR:
1272 case CMD_GEN_REQUEST64_CX:
1273 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1274 case DSSCMD_IWRITE64_CR:
1275 case DSSCMD_IWRITE64_CX:
1276 case DSSCMD_IREAD64_CR:
1277 case DSSCMD_IREAD64_CX:
dea3101e
JB
1278 type = LPFC_SOL_IOCB;
1279 break;
1280 case CMD_ABORT_XRI_CN:
1281 case CMD_ABORT_XRI_CX:
1282 case CMD_CLOSE_XRI_CN:
1283 case CMD_CLOSE_XRI_CX:
1284 case CMD_XRI_ABORTED_CX:
1285 case CMD_ABORT_MXRI64_CN:
6669f9bb 1286 case CMD_XMIT_BLS_RSP64_CX:
dea3101e
JB
1287 type = LPFC_ABORT_IOCB;
1288 break;
1289 case CMD_RCV_SEQUENCE_CX:
1290 case CMD_RCV_ELS_REQ_CX:
1291 case CMD_RCV_SEQUENCE64_CX:
1292 case CMD_RCV_ELS_REQ64_CX:
57127f15 1293 case CMD_ASYNC_STATUS:
ed957684
JS
1294 case CMD_IOCB_RCV_SEQ64_CX:
1295 case CMD_IOCB_RCV_ELS64_CX:
1296 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1297 case CMD_IOCB_RET_XRI64_CX:
dea3101e
JB
1298 type = LPFC_UNSOL_IOCB;
1299 break;
3163f725
JS
1300 case CMD_IOCB_XMIT_MSEQ64_CR:
1301 case CMD_IOCB_XMIT_MSEQ64_CX:
1302 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1303 case CMD_IOCB_RCV_ELS_LIST64_CX:
1304 case CMD_IOCB_CLOSE_EXTENDED_CN:
1305 case CMD_IOCB_ABORT_EXTENDED_CN:
1306 case CMD_IOCB_RET_HBQE64_CN:
1307 case CMD_IOCB_FCP_IBIDIR64_CR:
1308 case CMD_IOCB_FCP_IBIDIR64_CX:
1309 case CMD_IOCB_FCP_ITASKMGT64_CX:
1310 case CMD_IOCB_LOGENTRY_CN:
1311 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1312 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1313 __func__, iocb_cmnd);
3163f725
JS
1314 type = LPFC_UNKNOWN_IOCB;
1315 break;
dea3101e
JB
1316 default:
1317 type = LPFC_UNKNOWN_IOCB;
1318 break;
1319 }
1320
1321 return type;
1322}
1323
e59058c4 1324/**
3621a710 1325 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1326 * @phba: Pointer to HBA context object.
1327 *
1328 * This function is called from SLI initialization code
1329 * to configure every ring of the HBA's SLI interface. The
1330 * caller is not required to hold any lock. This function issues
1331 * a config_ring mailbox command for each ring.
1332 * This function returns zero if successful else returns a negative
1333 * error code.
1334 **/
dea3101e 1335static int
ed957684 1336lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e
JB
1337{
1338 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1339 LPFC_MBOXQ_t *pmb;
1340 MAILBOX_t *pmbox;
1341 int i, rc, ret = 0;
dea3101e 1342
ed957684
JS
1343 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1344 if (!pmb)
1345 return -ENOMEM;
04c68496 1346 pmbox = &pmb->u.mb;
ed957684 1347 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1348 for (i = 0; i < psli->num_rings; i++) {
dea3101e
JB
1349 lpfc_config_ring(phba, i, pmb);
1350 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1351 if (rc != MBX_SUCCESS) {
92d7f7b0 1352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1353 "0446 Adapter failed to init (%d), "
dea3101e
JB
1354 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1355 "ring %d\n",
e8b62011
JS
1356 rc, pmbox->mbxCommand,
1357 pmbox->mbxStatus, i);
2e0fef85 1358 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1359 ret = -ENXIO;
1360 break;
dea3101e
JB
1361 }
1362 }
ed957684
JS
1363 mempool_free(pmb, phba->mbox_mem_pool);
1364 return ret;
dea3101e
JB
1365}
1366
e59058c4 1367/**
3621a710 1368 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1369 * @phba: Pointer to HBA context object.
1370 * @pring: Pointer to driver SLI ring object.
1371 * @piocb: Pointer to the driver iocb object.
1372 *
1373 * This function is called with hbalock held. The function adds the
1374 * new iocb to txcmplq of the given ring. This function always returns
1375 * 0. If this function is called for ELS ring, this function checks if
1376 * there is a vport associated with the ELS command. This function also
1377 * starts els_tmofunc timer if this is an ELS command.
1378 **/
dea3101e 1379static int
2e0fef85
JS
1380lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1381 struct lpfc_iocbq *piocb)
dea3101e 1382{
1c2ba475
JT
1383 lockdep_assert_held(&phba->hbalock);
1384
2319f847 1385 BUG_ON(!piocb);
22466da5 1386
dea3101e 1387 list_add_tail(&piocb->list, &pring->txcmplq);
4f2e66c6 1388 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
2a9bf3d0 1389
92d7f7b0
JS
1390 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1391 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2319f847
MFO
1392 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1393 BUG_ON(!piocb->vport);
1394 if (!(piocb->vport->load_flag & FC_UNLOADING))
1395 mod_timer(&piocb->vport->els_tmofunc,
1396 jiffies +
1397 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1398 }
dea3101e 1399
2e0fef85 1400 return 0;
dea3101e
JB
1401}
1402
e59058c4 1403/**
3621a710 1404 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 *
1408 * This function is called with hbalock held to get next
1409 * iocb in txq of the given ring. If there is any iocb in
1410 * the txq, the function returns first iocb in the list after
1411 * removing the iocb from the list, else it returns NULL.
1412 **/
2a9bf3d0 1413struct lpfc_iocbq *
2e0fef85 1414lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1415{
dea3101e
JB
1416 struct lpfc_iocbq *cmd_iocb;
1417
1c2ba475
JT
1418 lockdep_assert_held(&phba->hbalock);
1419
858c9f6c 1420 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
2e0fef85 1421 return cmd_iocb;
dea3101e
JB
1422}
1423
e59058c4 1424/**
3621a710 1425 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1426 * @phba: Pointer to HBA context object.
1427 * @pring: Pointer to driver SLI ring object.
1428 *
1429 * This function is called with hbalock held and the caller must post the
1430 * iocb without releasing the lock. If the caller releases the lock,
1431 * iocb slot returned by the function is not guaranteed to be available.
1432 * The function returns pointer to the next available iocb slot if there
1433 * is available slot in the ring, else it returns NULL.
1434 * If the get index of the ring is ahead of the put index, the function
1435 * will post an error attention event to the worker thread to take the
1436 * HBA to offline state.
1437 **/
dea3101e
JB
1438static IOCB_t *
1439lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1440{
34b02dcd 1441 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
7e56aa25 1442 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1c2ba475
JT
1443
1444 lockdep_assert_held(&phba->hbalock);
1445
7e56aa25
JS
1446 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1447 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1448 pring->sli.sli3.next_cmdidx = 0;
dea3101e 1449
7e56aa25
JS
1450 if (unlikely(pring->sli.sli3.local_getidx ==
1451 pring->sli.sli3.next_cmdidx)) {
dea3101e 1452
7e56aa25 1453 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e 1454
7e56aa25 1455 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea3101e 1456 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1457 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1458 "is bigger than cmd ring %d\n",
e8b62011 1459 pring->ringno,
7e56aa25
JS
1460 pring->sli.sli3.local_getidx,
1461 max_cmd_idx);
dea3101e 1462
2e0fef85 1463 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
1464 /*
1465 * All error attention handlers are posted to
1466 * worker thread
1467 */
1468 phba->work_ha |= HA_ERATT;
1469 phba->work_hs = HS_FFER3;
92d7f7b0 1470
5e9d9b82 1471 lpfc_worker_wake_up(phba);
dea3101e
JB
1472
1473 return NULL;
1474 }
1475
7e56aa25 1476 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea3101e
JB
1477 return NULL;
1478 }
1479
ed957684 1480 return lpfc_cmd_iocb(phba, pring);
dea3101e
JB
1481}
1482
e59058c4 1483/**
3621a710 1484 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1485 * @phba: Pointer to HBA context object.
1486 * @iocbq: Pointer to driver iocb object.
1487 *
1488 * This function gets an iotag for the iocb. If there is no unused iotag and
1489 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1490 * array and assigns a new iotag.
1491 * The function returns the allocated iotag if successful, else returns zero.
1492 * Zero is not a valid iotag.
1493 * The caller is not required to hold any lock.
1494 **/
604a3e30 1495uint16_t
2e0fef85 1496lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1497{
2e0fef85
JS
1498 struct lpfc_iocbq **new_arr;
1499 struct lpfc_iocbq **old_arr;
604a3e30
JB
1500 size_t new_len;
1501 struct lpfc_sli *psli = &phba->sli;
1502 uint16_t iotag;
dea3101e 1503
2e0fef85 1504 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1505 iotag = psli->last_iotag;
1506 if(++iotag < psli->iocbq_lookup_len) {
1507 psli->last_iotag = iotag;
1508 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1509 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1510 iocbq->iotag = iotag;
1511 return iotag;
2e0fef85 1512 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1513 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1514 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1515 spin_unlock_irq(&phba->hbalock);
1516 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1517 GFP_KERNEL);
1518 if (new_arr) {
2e0fef85 1519 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1520 old_arr = psli->iocbq_lookup;
1521 if (new_len <= psli->iocbq_lookup_len) {
1522 /* highly unprobable case */
1523 kfree(new_arr);
1524 iotag = psli->last_iotag;
1525 if(++iotag < psli->iocbq_lookup_len) {
1526 psli->last_iotag = iotag;
1527 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1528 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1529 iocbq->iotag = iotag;
1530 return iotag;
1531 }
2e0fef85 1532 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1533 return 0;
1534 }
1535 if (psli->iocbq_lookup)
1536 memcpy(new_arr, old_arr,
1537 ((psli->last_iotag + 1) *
311464ec 1538 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1539 psli->iocbq_lookup = new_arr;
1540 psli->iocbq_lookup_len = new_len;
1541 psli->last_iotag = iotag;
1542 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1543 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1544 iocbq->iotag = iotag;
1545 kfree(old_arr);
1546 return iotag;
1547 }
8f6d98d2 1548 } else
2e0fef85 1549 spin_unlock_irq(&phba->hbalock);
dea3101e 1550
bc73905a 1551 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1552 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1553 psli->last_iotag);
dea3101e 1554
604a3e30 1555 return 0;
dea3101e
JB
1556}
1557
e59058c4 1558/**
3621a710 1559 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1560 * @phba: Pointer to HBA context object.
1561 * @pring: Pointer to driver SLI ring object.
1562 * @iocb: Pointer to iocb slot in the ring.
1563 * @nextiocb: Pointer to driver iocb object which need to be
1564 * posted to firmware.
1565 *
1566 * This function is called with hbalock held to post a new iocb to
1567 * the firmware. This function copies the new iocb to ring iocb slot and
1568 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1569 * a completion call back for this iocb else the function will free the
1570 * iocb object.
1571 **/
dea3101e
JB
1572static void
1573lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1574 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1575{
1c2ba475 1576 lockdep_assert_held(&phba->hbalock);
dea3101e 1577 /*
604a3e30 1578 * Set up an iotag
dea3101e 1579 */
604a3e30 1580 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1581
e2a0a9d6 1582
a58cbd52
JS
1583 if (pring->ringno == LPFC_ELS_RING) {
1584 lpfc_debugfs_slow_ring_trc(phba,
1585 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1586 *(((uint32_t *) &nextiocb->iocb) + 4),
1587 *(((uint32_t *) &nextiocb->iocb) + 6),
1588 *(((uint32_t *) &nextiocb->iocb) + 7));
1589 }
1590
dea3101e
JB
1591 /*
1592 * Issue iocb command to adapter
1593 */
92d7f7b0 1594 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e
JB
1595 wmb();
1596 pring->stats.iocb_cmd++;
1597
1598 /*
1599 * If there is no completion routine to call, we can release the
1600 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1601 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1602 */
1603 if (nextiocb->iocb_cmpl)
1604 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1605 else
2e0fef85 1606 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e
JB
1607
1608 /*
1609 * Let the HBA know what IOCB slot will be the next one the
1610 * driver will put a command into.
1611 */
7e56aa25
JS
1612 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1613 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e
JB
1614}
1615
e59058c4 1616/**
3621a710 1617 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1618 * @phba: Pointer to HBA context object.
1619 * @pring: Pointer to driver SLI ring object.
1620 *
1621 * The caller is not required to hold any lock for calling this function.
1622 * This function updates the chip attention bits for the ring to inform firmware
1623 * that there are pending work to be done for this ring and requests an
1624 * interrupt when there is space available in the ring. This function is
1625 * called when the driver is unable to post more iocbs to the ring due
1626 * to unavailability of space in the ring.
1627 **/
dea3101e 1628static void
2e0fef85 1629lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1630{
1631 int ringno = pring->ringno;
1632
1633 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1634
1635 wmb();
1636
1637 /*
1638 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1639 * The HBA will tell us when an IOCB entry is available.
1640 */
1641 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1642 readl(phba->CAregaddr); /* flush */
1643
1644 pring->stats.iocb_cmd_full++;
1645}
1646
e59058c4 1647/**
3621a710 1648 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1649 * @phba: Pointer to HBA context object.
1650 * @pring: Pointer to driver SLI ring object.
1651 *
1652 * This function updates the chip attention register bit for the
1653 * given ring to inform HBA that there is more work to be done
1654 * in this ring. The caller is not required to hold any lock.
1655 **/
dea3101e 1656static void
2e0fef85 1657lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1658{
1659 int ringno = pring->ringno;
1660
1661 /*
1662 * Tell the HBA that there is work to do in this ring.
1663 */
34b02dcd
JS
1664 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1665 wmb();
1666 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1667 readl(phba->CAregaddr); /* flush */
1668 }
dea3101e
JB
1669}
1670
e59058c4 1671/**
3621a710 1672 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1673 * @phba: Pointer to HBA context object.
1674 * @pring: Pointer to driver SLI ring object.
1675 *
1676 * This function is called with hbalock held to post pending iocbs
1677 * in the txq to the firmware. This function is called when driver
1678 * detects space available in the ring.
1679 **/
dea3101e 1680static void
2e0fef85 1681lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e
JB
1682{
1683 IOCB_t *iocb;
1684 struct lpfc_iocbq *nextiocb;
1685
1c2ba475
JT
1686 lockdep_assert_held(&phba->hbalock);
1687
dea3101e
JB
1688 /*
1689 * Check to see if:
1690 * (a) there is anything on the txq to send
1691 * (b) link is up
1692 * (c) link attention events can be processed (fcp ring only)
1693 * (d) IOCB processing is not blocked by the outstanding mbox command.
1694 */
0e9bb8d7
JS
1695
1696 if (lpfc_is_link_up(phba) &&
1697 (!list_empty(&pring->txq)) &&
895427bd 1698 (pring->ringno != LPFC_FCP_RING ||
0b727fea 1699 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e
JB
1700
1701 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1702 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1703 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1704
1705 if (iocb)
1706 lpfc_sli_update_ring(phba, pring);
1707 else
1708 lpfc_sli_update_full_ring(phba, pring);
1709 }
1710
1711 return;
1712}
1713
e59058c4 1714/**
3621a710 1715 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1716 * @phba: Pointer to HBA context object.
1717 * @hbqno: HBQ number.
1718 *
1719 * This function is called with hbalock held to get the next
1720 * available slot for the given HBQ. If there is free slot
1721 * available for the HBQ it will return pointer to the next available
1722 * HBQ entry else it will return NULL.
1723 **/
a6ababd2 1724static struct lpfc_hbq_entry *
ed957684
JS
1725lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1726{
1727 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1728
1c2ba475
JT
1729 lockdep_assert_held(&phba->hbalock);
1730
ed957684
JS
1731 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1732 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1733 hbqp->next_hbqPutIdx = 0;
1734
1735 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1736 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1737 uint32_t getidx = le32_to_cpu(raw_index);
1738
1739 hbqp->local_hbqGetIdx = getidx;
1740
1741 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1742 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1743 LOG_SLI | LOG_VPORT,
e8b62011 1744 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1745 "%u is > than hbqp->entry_count %u\n",
e8b62011 1746 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1747 hbqp->entry_count);
1748
1749 phba->link_state = LPFC_HBA_ERROR;
1750 return NULL;
1751 }
1752
1753 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1754 return NULL;
1755 }
1756
51ef4c26
JS
1757 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1758 hbqp->hbqPutIdx;
ed957684
JS
1759}
1760
e59058c4 1761/**
3621a710 1762 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1763 * @phba: Pointer to HBA context object.
1764 *
1765 * This function is called with no lock held to free all the
1766 * hbq buffers while uninitializing the SLI interface. It also
1767 * frees the HBQ buffers returned by the firmware but not yet
1768 * processed by the upper layers.
1769 **/
ed957684
JS
1770void
1771lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1772{
92d7f7b0
JS
1773 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1774 struct hbq_dmabuf *hbq_buf;
3163f725 1775 unsigned long flags;
51ef4c26 1776 int i, hbq_count;
ed957684 1777
51ef4c26 1778 hbq_count = lpfc_sli_hbq_count();
ed957684 1779 /* Return all memory used by all HBQs */
3163f725 1780 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1781 for (i = 0; i < hbq_count; ++i) {
1782 list_for_each_entry_safe(dmabuf, next_dmabuf,
1783 &phba->hbqs[i].hbq_buffer_list, list) {
1784 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1785 list_del(&hbq_buf->dbuf.list);
1786 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1787 }
a8adb832 1788 phba->hbqs[i].buffer_count = 0;
ed957684 1789 }
3163f725
JS
1790
1791 /* Mark the HBQs not in use */
1792 phba->hbq_in_use = 0;
1793 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1794}
1795
e59058c4 1796/**
3621a710 1797 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1798 * @phba: Pointer to HBA context object.
1799 * @hbqno: HBQ number.
1800 * @hbq_buf: Pointer to HBQ buffer.
1801 *
1802 * This function is called with the hbalock held to post a
1803 * hbq buffer to the firmware. If the function finds an empty
1804 * slot in the HBQ, it will post the buffer. The function will return
1805 * pointer to the hbq entry if it successfully post the buffer
1806 * else it will return NULL.
1807 **/
3772a991 1808static int
ed957684 1809lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1810 struct hbq_dmabuf *hbq_buf)
3772a991 1811{
1c2ba475 1812 lockdep_assert_held(&phba->hbalock);
3772a991
JS
1813 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1814}
1815
1816/**
1817 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1818 * @phba: Pointer to HBA context object.
1819 * @hbqno: HBQ number.
1820 * @hbq_buf: Pointer to HBQ buffer.
1821 *
1822 * This function is called with the hbalock held to post a hbq buffer to the
1823 * firmware. If the function finds an empty slot in the HBQ, it will post the
1824 * buffer and place it on the hbq_buffer_list. The function will return zero if
1825 * it successfully post the buffer else it will return an error.
1826 **/
1827static int
1828lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1829 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1830{
1831 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1832 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684 1833
1c2ba475 1834 lockdep_assert_held(&phba->hbalock);
ed957684
JS
1835 /* Get next HBQ entry slot to use */
1836 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1837 if (hbqe) {
1838 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1839
92d7f7b0
JS
1840 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1841 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
895427bd 1842 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
ed957684 1843 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1844 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1845 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1846 /* Sync SLIM */
ed957684
JS
1847 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1848 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1849 /* flush */
ed957684 1850 readl(phba->hbq_put + hbqno);
51ef4c26 1851 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1852 return 0;
1853 } else
1854 return -ENOMEM;
ed957684
JS
1855}
1856
4f774513
JS
1857/**
1858 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1859 * @phba: Pointer to HBA context object.
1860 * @hbqno: HBQ number.
1861 * @hbq_buf: Pointer to HBQ buffer.
1862 *
1863 * This function is called with the hbalock held to post an RQE to the SLI4
1864 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1865 * the hbq_buffer_list and return zero, otherwise it will return an error.
1866 **/
1867static int
1868lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1869 struct hbq_dmabuf *hbq_buf)
1870{
1871 int rc;
1872 struct lpfc_rqe hrqe;
1873 struct lpfc_rqe drqe;
895427bd
JS
1874 struct lpfc_queue *hrq;
1875 struct lpfc_queue *drq;
1876
1877 if (hbqno != LPFC_ELS_HBQ)
1878 return 1;
1879 hrq = phba->sli4_hba.hdr_rq;
1880 drq = phba->sli4_hba.dat_rq;
4f774513 1881
1c2ba475 1882 lockdep_assert_held(&phba->hbalock);
4f774513
JS
1883 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1884 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1885 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1886 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
895427bd 1887 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
4f774513
JS
1888 if (rc < 0)
1889 return rc;
895427bd 1890 hbq_buf->tag = (rc | (hbqno << 16));
4f774513
JS
1891 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1892 return 0;
1893}
1894
e59058c4 1895/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1896static struct lpfc_hbq_init lpfc_els_hbq = {
1897 .rn = 1,
def9c7a9 1898 .entry_count = 256,
92d7f7b0
JS
1899 .mask_count = 0,
1900 .profile = 0,
51ef4c26 1901 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1902 .buffer_count = 0,
a257bf90
JS
1903 .init_count = 40,
1904 .add_count = 40,
92d7f7b0 1905};
ed957684 1906
e59058c4 1907/* Array of HBQs */
78b2d852 1908struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0
JS
1909 &lpfc_els_hbq,
1910};
ed957684 1911
e59058c4 1912/**
3621a710 1913 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1914 * @phba: Pointer to HBA context object.
1915 * @hbqno: HBQ number.
1916 * @count: Number of HBQ buffers to be posted.
1917 *
d7c255b2
JS
1918 * This function is called with no lock held to post more hbq buffers to the
1919 * given HBQ. The function returns the number of HBQ buffers successfully
1920 * posted.
e59058c4 1921 **/
311464ec 1922static int
92d7f7b0 1923lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1924{
d7c255b2 1925 uint32_t i, posted = 0;
3163f725 1926 unsigned long flags;
92d7f7b0 1927 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1928 LIST_HEAD(hbq_buf_list);
eafe1df9 1929 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1930 return 0;
51ef4c26 1931
d7c255b2
JS
1932 if ((phba->hbqs[hbqno].buffer_count + count) >
1933 lpfc_hbq_defs[hbqno]->entry_count)
1934 count = lpfc_hbq_defs[hbqno]->entry_count -
1935 phba->hbqs[hbqno].buffer_count;
1936 if (!count)
1937 return 0;
1938 /* Allocate HBQ entries */
1939 for (i = 0; i < count; i++) {
1940 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1941 if (!hbq_buffer)
1942 break;
1943 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1944 }
3163f725
JS
1945 /* Check whether HBQ is still in use */
1946 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1947 if (!phba->hbq_in_use)
d7c255b2
JS
1948 goto err;
1949 while (!list_empty(&hbq_buf_list)) {
1950 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1951 dbuf.list);
1952 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1953 (hbqno << 16));
3772a991 1954 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1955 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1956 posted++;
1957 } else
51ef4c26 1958 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1959 }
3163f725 1960 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1961 return posted;
1962err:
eafe1df9 1963 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1964 while (!list_empty(&hbq_buf_list)) {
1965 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1966 dbuf.list);
1967 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1968 }
1969 return 0;
ed957684
JS
1970}
1971
e59058c4 1972/**
3621a710 1973 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1974 * @phba: Pointer to HBA context object.
1975 * @qno: HBQ number.
1976 *
1977 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1978 * is called with no lock held. The function returns the number of HBQ entries
1979 * successfully allocated.
e59058c4 1980 **/
92d7f7b0
JS
1981int
1982lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1983{
def9c7a9
JS
1984 if (phba->sli_rev == LPFC_SLI_REV4)
1985 return 0;
1986 else
1987 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1988 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1989}
ed957684 1990
e59058c4 1991/**
3621a710 1992 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1993 * @phba: Pointer to HBA context object.
1994 * @qno: HBQ queue number.
1995 *
1996 * This function is called from SLI initialization code path with
1997 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1998 * function returns the number of HBQ entries successfully allocated.
e59058c4 1999 **/
a6ababd2 2000static int
92d7f7b0
JS
2001lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2002{
def9c7a9
JS
2003 if (phba->sli_rev == LPFC_SLI_REV4)
2004 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 2005 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
2006 else
2007 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2008 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
2009}
2010
3772a991
JS
2011/**
2012 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2013 * @phba: Pointer to HBA context object.
2014 * @hbqno: HBQ number.
2015 *
2016 * This function removes the first hbq buffer on an hbq list and returns a
2017 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2018 **/
2019static struct hbq_dmabuf *
2020lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2021{
2022 struct lpfc_dmabuf *d_buf;
2023
2024 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2025 if (!d_buf)
2026 return NULL;
2027 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2028}
2029
2d7dbc4c
JS
2030/**
2031 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2032 * @phba: Pointer to HBA context object.
2033 * @hbqno: HBQ number.
2034 *
2035 * This function removes the first RQ buffer on an RQ buffer list and returns a
2036 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2037 **/
2038static struct rqb_dmabuf *
2039lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2040{
2041 struct lpfc_dmabuf *h_buf;
2042 struct lpfc_rqb *rqbp;
2043
2044 rqbp = hrq->rqbp;
2045 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2046 struct lpfc_dmabuf, list);
2047 if (!h_buf)
2048 return NULL;
2049 rqbp->buffer_count--;
2050 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2051}
2052
e59058c4 2053/**
3621a710 2054 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
2055 * @phba: Pointer to HBA context object.
2056 * @tag: Tag of the hbq buffer.
2057 *
71892418
SH
2058 * This function searches for the hbq buffer associated with the given tag in
2059 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2060 * otherwise it returns NULL.
e59058c4 2061 **/
a6ababd2 2062static struct hbq_dmabuf *
92d7f7b0 2063lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 2064{
92d7f7b0
JS
2065 struct lpfc_dmabuf *d_buf;
2066 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
2067 uint32_t hbqno;
2068
2069 hbqno = tag >> 16;
a0a74e45 2070 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 2071 return NULL;
ed957684 2072
3772a991 2073 spin_lock_irq(&phba->hbalock);
51ef4c26 2074 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 2075 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 2076 if (hbq_buf->tag == tag) {
3772a991 2077 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2078 return hbq_buf;
ed957684
JS
2079 }
2080 }
3772a991 2081 spin_unlock_irq(&phba->hbalock);
92d7f7b0 2082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 2083 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 2084 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 2085 return NULL;
ed957684
JS
2086}
2087
e59058c4 2088/**
3621a710 2089 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
2090 * @phba: Pointer to HBA context object.
2091 * @hbq_buffer: Pointer to HBQ buffer.
2092 *
2093 * This function is called with hbalock. This function gives back
2094 * the hbq buffer to firmware. If the HBQ does not have space to
2095 * post the buffer, it will free the buffer.
2096 **/
ed957684 2097void
51ef4c26 2098lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
2099{
2100 uint32_t hbqno;
2101
51ef4c26
JS
2102 if (hbq_buffer) {
2103 hbqno = hbq_buffer->tag >> 16;
3772a991 2104 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 2105 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
2106 }
2107}
2108
e59058c4 2109/**
3621a710 2110 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
2111 * @mbxCommand: mailbox command code.
2112 *
2113 * This function is called by the mailbox event handler function to verify
2114 * that the completed mailbox command is a legitimate mailbox command. If the
2115 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2116 * and the mailbox event handler will take the HBA offline.
2117 **/
dea3101e
JB
2118static int
2119lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2120{
2121 uint8_t ret;
2122
2123 switch (mbxCommand) {
2124 case MBX_LOAD_SM:
2125 case MBX_READ_NV:
2126 case MBX_WRITE_NV:
a8adb832 2127 case MBX_WRITE_VPARMS:
dea3101e
JB
2128 case MBX_RUN_BIU_DIAG:
2129 case MBX_INIT_LINK:
2130 case MBX_DOWN_LINK:
2131 case MBX_CONFIG_LINK:
2132 case MBX_CONFIG_RING:
2133 case MBX_RESET_RING:
2134 case MBX_READ_CONFIG:
2135 case MBX_READ_RCONFIG:
2136 case MBX_READ_SPARM:
2137 case MBX_READ_STATUS:
2138 case MBX_READ_RPI:
2139 case MBX_READ_XRI:
2140 case MBX_READ_REV:
2141 case MBX_READ_LNK_STAT:
2142 case MBX_REG_LOGIN:
2143 case MBX_UNREG_LOGIN:
dea3101e
JB
2144 case MBX_CLEAR_LA:
2145 case MBX_DUMP_MEMORY:
2146 case MBX_DUMP_CONTEXT:
2147 case MBX_RUN_DIAGS:
2148 case MBX_RESTART:
2149 case MBX_UPDATE_CFG:
2150 case MBX_DOWN_LOAD:
2151 case MBX_DEL_LD_ENTRY:
2152 case MBX_RUN_PROGRAM:
2153 case MBX_SET_MASK:
09372820 2154 case MBX_SET_VARIABLE:
dea3101e 2155 case MBX_UNREG_D_ID:
41415862 2156 case MBX_KILL_BOARD:
dea3101e 2157 case MBX_CONFIG_FARP:
41415862 2158 case MBX_BEACON:
dea3101e
JB
2159 case MBX_LOAD_AREA:
2160 case MBX_RUN_BIU_DIAG64:
2161 case MBX_CONFIG_PORT:
2162 case MBX_READ_SPARM64:
2163 case MBX_READ_RPI64:
2164 case MBX_REG_LOGIN64:
76a95d75 2165 case MBX_READ_TOPOLOGY:
09372820 2166 case MBX_WRITE_WWN:
dea3101e
JB
2167 case MBX_SET_DEBUG:
2168 case MBX_LOAD_EXP_ROM:
57127f15 2169 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2170 case MBX_REG_VPI:
2171 case MBX_UNREG_VPI:
858c9f6c 2172 case MBX_HEARTBEAT:
84774a4d
JS
2173 case MBX_PORT_CAPABILITIES:
2174 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2175 case MBX_SLI4_CONFIG:
2176 case MBX_SLI4_REQ_FTRS:
2177 case MBX_REG_FCFI:
2178 case MBX_UNREG_FCFI:
2179 case MBX_REG_VFI:
2180 case MBX_UNREG_VFI:
2181 case MBX_INIT_VPI:
2182 case MBX_INIT_VFI:
2183 case MBX_RESUME_RPI:
c7495937
JS
2184 case MBX_READ_EVENT_LOG_STATUS:
2185 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2186 case MBX_SECURITY_MGMT:
2187 case MBX_AUTH_PORT:
940eb687 2188 case MBX_ACCESS_VDATA:
dea3101e
JB
2189 ret = mbxCommand;
2190 break;
2191 default:
2192 ret = MBX_SHUTDOWN;
2193 break;
2194 }
2e0fef85 2195 return ret;
dea3101e 2196}
e59058c4
JS
2197
2198/**
3621a710 2199 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2200 * @phba: Pointer to HBA context object.
2201 * @pmboxq: Pointer to mailbox command.
2202 *
2203 * This is completion handler function for mailbox commands issued from
2204 * lpfc_sli_issue_mbox_wait function. This function is called by the
2205 * mailbox event handler function with no lock held. This function
2206 * will wake up thread waiting on the wait queue pointed by context1
2207 * of the mailbox.
2208 **/
04c68496 2209void
2e0fef85 2210lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e
JB
2211{
2212 wait_queue_head_t *pdone_q;
858c9f6c 2213 unsigned long drvr_flag;
dea3101e
JB
2214
2215 /*
2216 * If pdone_q is empty, the driver thread gave up waiting and
2217 * continued running.
2218 */
7054a606 2219 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2220 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e
JB
2221 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2222 if (pdone_q)
2223 wake_up_interruptible(pdone_q);
858c9f6c 2224 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
2225 return;
2226}
2227
e59058c4
JS
2228
2229/**
3621a710 2230 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2231 * @phba: Pointer to HBA context object.
2232 * @pmb: Pointer to mailbox object.
2233 *
2234 * This function is the default mailbox completion handler. It
2235 * frees the memory resources associated with the completed mailbox
2236 * command. If the completed command is a REG_LOGIN mailbox command,
2237 * this function will issue a UREG_LOGIN to re-claim the RPI.
2238 **/
dea3101e 2239void
2e0fef85 2240lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2241{
d439d286 2242 struct lpfc_vport *vport = pmb->vport;
dea3101e 2243 struct lpfc_dmabuf *mp;
d439d286 2244 struct lpfc_nodelist *ndlp;
5af5eee7 2245 struct Scsi_Host *shost;
04c68496 2246 uint16_t rpi, vpi;
7054a606
JS
2247 int rc;
2248
dea3101e 2249 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2250
dea3101e
JB
2251 if (mp) {
2252 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2253 kfree(mp);
2254 }
7054a606
JS
2255
2256 /*
2257 * If a REG_LOGIN succeeded after node is destroyed or node
2258 * is in re-discovery driver need to cleanup the RPI.
2259 */
2e0fef85 2260 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2261 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2262 !pmb->u.mb.mbxStatus) {
2263 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2264 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2265 lpfc_unreg_login(phba, vpi, rpi, pmb);
de96e9c5 2266 pmb->vport = vport;
92d7f7b0 2267 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2268 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2269 if (rc != MBX_NOT_FINISHED)
2270 return;
2271 }
2272
695a814e
JS
2273 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2274 !(phba->pport->load_flag & FC_UNLOADING) &&
2275 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2276 shost = lpfc_shost_from_vport(vport);
2277 spin_lock_irq(shost->host_lock);
2278 vport->vpi_state |= LPFC_VPI_REGISTERED;
2279 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2280 spin_unlock_irq(shost->host_lock);
695a814e
JS
2281 }
2282
d439d286
JS
2283 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2284 ndlp = (struct lpfc_nodelist *)pmb->context2;
2285 lpfc_nlp_put(ndlp);
2286 pmb->context2 = NULL;
2287 }
2288
dcf2a4e0
JS
2289 /* Check security permission status on INIT_LINK mailbox command */
2290 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2291 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2292 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2293 "2860 SLI authentication is required "
2294 "for INIT_LINK but has not done yet\n");
2295
04c68496
JS
2296 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2297 lpfc_sli4_mbox_cmd_free(phba, pmb);
2298 else
2299 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2300}
be6bb941
JS
2301 /**
2302 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2303 * @phba: Pointer to HBA context object.
2304 * @pmb: Pointer to mailbox object.
2305 *
2306 * This function is the unreg rpi mailbox completion handler. It
2307 * frees the memory resources associated with the completed mailbox
2308 * command. An additional refrenece is put on the ndlp to prevent
2309 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2310 * the unreg mailbox command completes, this routine puts the
2311 * reference back.
2312 *
2313 **/
2314void
2315lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2316{
2317 struct lpfc_vport *vport = pmb->vport;
2318 struct lpfc_nodelist *ndlp;
2319
2320 ndlp = pmb->context1;
2321 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2322 if (phba->sli_rev == LPFC_SLI_REV4 &&
2323 (bf_get(lpfc_sli_intf_if_type,
2324 &phba->sli4_hba.sli_intf) ==
2325 LPFC_SLI_INTF_IF_TYPE_2)) {
2326 if (ndlp) {
2327 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2328 "0010 UNREG_LOGIN vpi:%x "
2329 "rpi:%x DID:%x map:%x %p\n",
2330 vport->vpi, ndlp->nlp_rpi,
2331 ndlp->nlp_DID,
2332 ndlp->nlp_usg_map, ndlp);
7c5e518c 2333 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
be6bb941
JS
2334 lpfc_nlp_put(ndlp);
2335 }
2336 }
2337 }
2338
2339 mempool_free(pmb, phba->mbox_mem_pool);
2340}
dea3101e 2341
e59058c4 2342/**
3621a710 2343 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2344 * @phba: Pointer to HBA context object.
2345 *
2346 * This function is called with no lock held. This function processes all
2347 * the completed mailbox commands and gives it to upper layers. The interrupt
2348 * service routine processes mailbox completion interrupt and adds completed
2349 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2350 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2351 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2352 * function returns the mailbox commands to the upper layer by calling the
2353 * completion handler function of each mailbox.
2354 **/
dea3101e 2355int
2e0fef85 2356lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2357{
92d7f7b0 2358 MAILBOX_t *pmbox;
dea3101e 2359 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2360 int rc;
2361 LIST_HEAD(cmplq);
dea3101e
JB
2362
2363 phba->sli.slistat.mbox_event++;
2364
92d7f7b0
JS
2365 /* Get all completed mailboxe buffers into the cmplq */
2366 spin_lock_irq(&phba->hbalock);
2367 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2368 spin_unlock_irq(&phba->hbalock);
dea3101e 2369
92d7f7b0
JS
2370 /* Get a Mailbox buffer to setup mailbox commands for callback */
2371 do {
2372 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2373 if (pmb == NULL)
2374 break;
2e0fef85 2375
04c68496 2376 pmbox = &pmb->u.mb;
dea3101e 2377
858c9f6c
JS
2378 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2379 if (pmb->vport) {
2380 lpfc_debugfs_disc_trc(pmb->vport,
2381 LPFC_DISC_TRC_MBOX_VPORT,
2382 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2383 (uint32_t)pmbox->mbxCommand,
2384 pmbox->un.varWords[0],
2385 pmbox->un.varWords[1]);
2386 }
2387 else {
2388 lpfc_debugfs_disc_trc(phba->pport,
2389 LPFC_DISC_TRC_MBOX,
2390 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2391 (uint32_t)pmbox->mbxCommand,
2392 pmbox->un.varWords[0],
2393 pmbox->un.varWords[1]);
2394 }
2395 }
2396
dea3101e
JB
2397 /*
2398 * It is a fatal error if unknown mbox command completion.
2399 */
2400 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2401 MBX_SHUTDOWN) {
af901ca1 2402 /* Unknown mailbox command compl */
92d7f7b0 2403 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2404 "(%d):0323 Unknown Mailbox command "
a183a15f 2405 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2406 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2407 pmbox->mbxCommand,
a183a15f
JS
2408 lpfc_sli_config_mbox_subsys_get(phba,
2409 pmb),
2410 lpfc_sli_config_mbox_opcode_get(phba,
2411 pmb));
2e0fef85 2412 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
2413 phba->work_hs = HS_FFER3;
2414 lpfc_handle_eratt(phba);
92d7f7b0 2415 continue;
dea3101e
JB
2416 }
2417
dea3101e
JB
2418 if (pmbox->mbxStatus) {
2419 phba->sli.slistat.mbox_stat_err++;
2420 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2421 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2422 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2423 LOG_MBOX | LOG_SLI,
2424 "(%d):0305 Mbox cmd cmpl "
2425 "error - RETRYing Data: x%x "
2426 "(x%x/x%x) x%x x%x x%x\n",
2427 pmb->vport ? pmb->vport->vpi : 0,
2428 pmbox->mbxCommand,
2429 lpfc_sli_config_mbox_subsys_get(phba,
2430 pmb),
2431 lpfc_sli_config_mbox_opcode_get(phba,
2432 pmb),
2433 pmbox->mbxStatus,
2434 pmbox->un.varWords[0],
2435 pmb->vport->port_state);
dea3101e
JB
2436 pmbox->mbxStatus = 0;
2437 pmbox->mbxOwner = OWN_HOST;
dea3101e 2438 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2439 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2440 continue;
dea3101e
JB
2441 }
2442 }
2443
2444 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2445 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2446 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
e74c03c8
JS
2447 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2448 "x%x x%x x%x\n",
92d7f7b0 2449 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2450 pmbox->mbxCommand,
a183a15f
JS
2451 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2452 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e
JB
2453 pmb->mbox_cmpl,
2454 *((uint32_t *) pmbox),
2455 pmbox->un.varWords[0],
2456 pmbox->un.varWords[1],
2457 pmbox->un.varWords[2],
2458 pmbox->un.varWords[3],
2459 pmbox->un.varWords[4],
2460 pmbox->un.varWords[5],
2461 pmbox->un.varWords[6],
e74c03c8
JS
2462 pmbox->un.varWords[7],
2463 pmbox->un.varWords[8],
2464 pmbox->un.varWords[9],
2465 pmbox->un.varWords[10]);
dea3101e 2466
92d7f7b0 2467 if (pmb->mbox_cmpl)
dea3101e 2468 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2469 } while (1);
2470 return 0;
2471}
dea3101e 2472
e59058c4 2473/**
3621a710 2474 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2475 * @phba: Pointer to HBA context object.
2476 * @pring: Pointer to driver SLI ring object.
2477 * @tag: buffer tag.
2478 *
2479 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2480 * is set in the tag the buffer is posted for a particular exchange,
2481 * the function will return the buffer without replacing the buffer.
2482 * If the buffer is for unsolicited ELS or CT traffic, this function
2483 * returns the buffer and also posts another buffer to the firmware.
2484 **/
76bb24ef
JS
2485static struct lpfc_dmabuf *
2486lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2487 struct lpfc_sli_ring *pring,
2488 uint32_t tag)
76bb24ef 2489{
9f1e1b50
JS
2490 struct hbq_dmabuf *hbq_entry;
2491
76bb24ef
JS
2492 if (tag & QUE_BUFTAG_BIT)
2493 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2494 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2495 if (!hbq_entry)
2496 return NULL;
2497 return &hbq_entry->dbuf;
76bb24ef 2498}
57127f15 2499
3772a991
JS
2500/**
2501 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2502 * @phba: Pointer to HBA context object.
2503 * @pring: Pointer to driver SLI ring object.
2504 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2505 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2506 * @fch_type: the type for the first frame of the sequence.
2507 *
2508 * This function is called with no lock held. This function uses the r_ctl and
2509 * type of the received sequence to find the correct callback function to call
2510 * to process the sequence.
2511 **/
2512static int
2513lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2514 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2515 uint32_t fch_type)
2516{
2517 int i;
2518
f358dd0c
JS
2519 switch (fch_type) {
2520 case FC_TYPE_NVME:
d613b6a7 2521 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
f358dd0c
JS
2522 return 1;
2523 default:
2524 break;
2525 }
2526
3772a991
JS
2527 /* unSolicited Responses */
2528 if (pring->prt[0].profile) {
2529 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2530 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2531 saveq);
2532 return 1;
2533 }
2534 /* We must search, based on rctl / type
2535 for the right routine */
2536 for (i = 0; i < pring->num_mask; i++) {
2537 if ((pring->prt[i].rctl == fch_r_ctl) &&
2538 (pring->prt[i].type == fch_type)) {
2539 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2540 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2541 (phba, pring, saveq);
2542 return 1;
2543 }
2544 }
2545 return 0;
2546}
e59058c4
JS
2547
2548/**
3621a710 2549 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @saveq: Pointer to the unsolicited iocb.
2553 *
2554 * This function is called with no lock held by the ring event handler
2555 * when there is an unsolicited iocb posted to the response ring by the
2556 * firmware. This function gets the buffer associated with the iocbs
2557 * and calls the event handler for the ring. This function handles both
2558 * qring buffers and hbq buffers.
2559 * When the function returns 1 the caller can free the iocb object otherwise
2560 * upper layer functions will free the iocb objects.
2561 **/
dea3101e
JB
2562static int
2563lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2564 struct lpfc_iocbq *saveq)
2565{
2566 IOCB_t * irsp;
2567 WORD5 * w5p;
2568 uint32_t Rctl, Type;
76bb24ef 2569 struct lpfc_iocbq *iocbq;
3163f725 2570 struct lpfc_dmabuf *dmzbuf;
dea3101e 2571
dea3101e 2572 irsp = &(saveq->iocb);
57127f15
JS
2573
2574 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2575 if (pring->lpfc_sli_rcv_async_status)
2576 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2577 else
2578 lpfc_printf_log(phba,
2579 KERN_WARNING,
2580 LOG_SLI,
2581 "0316 Ring %d handler: unexpected "
2582 "ASYNC_STATUS iocb received evt_code "
2583 "0x%x\n",
2584 pring->ringno,
2585 irsp->un.asyncstat.evt_code);
2586 return 1;
2587 }
2588
3163f725
JS
2589 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2590 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2591 if (irsp->ulpBdeCount > 0) {
2592 dmzbuf = lpfc_sli_get_buff(phba, pring,
2593 irsp->un.ulpWord[3]);
2594 lpfc_in_buf_free(phba, dmzbuf);
2595 }
2596
2597 if (irsp->ulpBdeCount > 1) {
2598 dmzbuf = lpfc_sli_get_buff(phba, pring,
2599 irsp->unsli3.sli3Words[3]);
2600 lpfc_in_buf_free(phba, dmzbuf);
2601 }
2602
2603 if (irsp->ulpBdeCount > 2) {
2604 dmzbuf = lpfc_sli_get_buff(phba, pring,
2605 irsp->unsli3.sli3Words[7]);
2606 lpfc_in_buf_free(phba, dmzbuf);
2607 }
2608
2609 return 1;
2610 }
2611
92d7f7b0 2612 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2613 if (irsp->ulpBdeCount != 0) {
2614 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2615 irsp->un.ulpWord[3]);
2616 if (!saveq->context2)
2617 lpfc_printf_log(phba,
2618 KERN_ERR,
2619 LOG_SLI,
2620 "0341 Ring %d Cannot find buffer for "
2621 "an unsolicited iocb. tag 0x%x\n",
2622 pring->ringno,
2623 irsp->un.ulpWord[3]);
76bb24ef
JS
2624 }
2625 if (irsp->ulpBdeCount == 2) {
2626 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2627 irsp->unsli3.sli3Words[7]);
2628 if (!saveq->context3)
2629 lpfc_printf_log(phba,
2630 KERN_ERR,
2631 LOG_SLI,
2632 "0342 Ring %d Cannot find buffer for an"
2633 " unsolicited iocb. tag 0x%x\n",
2634 pring->ringno,
2635 irsp->unsli3.sli3Words[7]);
2636 }
2637 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2638 irsp = &(iocbq->iocb);
76bb24ef
JS
2639 if (irsp->ulpBdeCount != 0) {
2640 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2641 irsp->un.ulpWord[3]);
9c2face6 2642 if (!iocbq->context2)
76bb24ef
JS
2643 lpfc_printf_log(phba,
2644 KERN_ERR,
2645 LOG_SLI,
2646 "0343 Ring %d Cannot find "
2647 "buffer for an unsolicited iocb"
2648 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2649 irsp->un.ulpWord[3]);
76bb24ef
JS
2650 }
2651 if (irsp->ulpBdeCount == 2) {
2652 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2653 irsp->unsli3.sli3Words[7]);
9c2face6 2654 if (!iocbq->context3)
76bb24ef
JS
2655 lpfc_printf_log(phba,
2656 KERN_ERR,
2657 LOG_SLI,
2658 "0344 Ring %d Cannot find "
2659 "buffer for an unsolicited "
2660 "iocb. tag 0x%x\n",
2661 pring->ringno,
2662 irsp->unsli3.sli3Words[7]);
2663 }
2664 }
92d7f7b0 2665 }
9c2face6
JS
2666 if (irsp->ulpBdeCount != 0 &&
2667 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2668 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2669 int found = 0;
2670
2671 /* search continue save q for same XRI */
2672 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2673 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2674 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2675 list_add_tail(&saveq->list, &iocbq->list);
2676 found = 1;
2677 break;
2678 }
2679 }
2680 if (!found)
2681 list_add_tail(&saveq->clist,
2682 &pring->iocb_continue_saveq);
2683 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2684 list_del_init(&iocbq->clist);
2685 saveq = iocbq;
2686 irsp = &(saveq->iocb);
2687 } else
2688 return 0;
2689 }
2690 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2691 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2692 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2693 Rctl = FC_RCTL_ELS_REQ;
2694 Type = FC_TYPE_ELS;
9c2face6
JS
2695 } else {
2696 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2697 Rctl = w5p->hcsw.Rctl;
2698 Type = w5p->hcsw.Type;
2699
2700 /* Firmware Workaround */
2701 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2702 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2703 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2704 Rctl = FC_RCTL_ELS_REQ;
2705 Type = FC_TYPE_ELS;
9c2face6
JS
2706 w5p->hcsw.Rctl = Rctl;
2707 w5p->hcsw.Type = Type;
2708 }
2709 }
92d7f7b0 2710
3772a991 2711 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2712 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2713 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2714 "Type x%x received\n",
e8b62011 2715 pring->ringno, Rctl, Type);
3772a991 2716
92d7f7b0 2717 return 1;
dea3101e
JB
2718}
2719
e59058c4 2720/**
3621a710 2721 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2722 * @phba: Pointer to HBA context object.
2723 * @pring: Pointer to driver SLI ring object.
2724 * @prspiocb: Pointer to response iocb object.
2725 *
2726 * This function looks up the iocb_lookup table to get the command iocb
2727 * corresponding to the given response iocb using the iotag of the
2728 * response iocb. This function is called with the hbalock held.
2729 * This function returns the command iocb object if it finds the command
2730 * iocb else returns NULL.
2731 **/
dea3101e 2732static struct lpfc_iocbq *
2e0fef85
JS
2733lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2734 struct lpfc_sli_ring *pring,
2735 struct lpfc_iocbq *prspiocb)
dea3101e 2736{
dea3101e
JB
2737 struct lpfc_iocbq *cmd_iocb = NULL;
2738 uint16_t iotag;
1c2ba475 2739 lockdep_assert_held(&phba->hbalock);
dea3101e 2740
604a3e30
JB
2741 iotag = prspiocb->iocb.ulpIoTag;
2742
2743 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2744 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6 2745 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
89533e9b
JS
2746 /* remove from txcmpl queue list */
2747 list_del_init(&cmd_iocb->list);
4f2e66c6 2748 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
89533e9b 2749 return cmd_iocb;
2a9bf3d0 2750 }
dea3101e
JB
2751 }
2752
dea3101e 2753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
89533e9b 2754 "0317 iotag x%x is out of "
604a3e30 2755 "range: max iotag x%x wd0 x%x\n",
e8b62011 2756 iotag, phba->sli.last_iotag,
604a3e30 2757 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e
JB
2758 return NULL;
2759}
2760
3772a991
JS
2761/**
2762 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2763 * @phba: Pointer to HBA context object.
2764 * @pring: Pointer to driver SLI ring object.
2765 * @iotag: IOCB tag.
2766 *
2767 * This function looks up the iocb_lookup table to get the command iocb
2768 * corresponding to the given iotag. This function is called with the
2769 * hbalock held.
2770 * This function returns the command iocb object if it finds the command
2771 * iocb else returns NULL.
2772 **/
2773static struct lpfc_iocbq *
2774lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2775 struct lpfc_sli_ring *pring, uint16_t iotag)
2776{
895427bd 2777 struct lpfc_iocbq *cmd_iocb = NULL;
3772a991 2778
1c2ba475 2779 lockdep_assert_held(&phba->hbalock);
3772a991
JS
2780 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2781 cmd_iocb = phba->sli.iocbq_lookup[iotag];
4f2e66c6
JS
2782 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2783 /* remove from txcmpl queue list */
2784 list_del_init(&cmd_iocb->list);
2785 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4f2e66c6 2786 return cmd_iocb;
2a9bf3d0 2787 }
3772a991 2788 }
89533e9b 2789
3772a991 2790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd
JS
2791 "0372 iotag x%x lookup error: max iotag (x%x) "
2792 "iocb_flag x%x\n",
2793 iotag, phba->sli.last_iotag,
2794 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
3772a991
JS
2795 return NULL;
2796}
2797
e59058c4 2798/**
3621a710 2799 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2800 * @phba: Pointer to HBA context object.
2801 * @pring: Pointer to driver SLI ring object.
2802 * @saveq: Pointer to the response iocb to be processed.
2803 *
2804 * This function is called by the ring event handler for non-fcp
2805 * rings when there is a new response iocb in the response ring.
2806 * The caller is not required to hold any locks. This function
2807 * gets the command iocb associated with the response iocb and
2808 * calls the completion handler for the command iocb. If there
2809 * is no completion handler, the function will free the resources
2810 * associated with command iocb. If the response iocb is for
2811 * an already aborted command iocb, the status of the completion
2812 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2813 * This function always returns 1.
2814 **/
dea3101e 2815static int
2e0fef85 2816lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e
JB
2817 struct lpfc_iocbq *saveq)
2818{
2e0fef85 2819 struct lpfc_iocbq *cmdiocbp;
dea3101e
JB
2820 int rc = 1;
2821 unsigned long iflag;
2822
2823 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2824 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2825 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2826 spin_unlock_irqrestore(&phba->hbalock, iflag);
2827
dea3101e
JB
2828 if (cmdiocbp) {
2829 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2830 /*
2831 * If an ELS command failed send an event to mgmt
2832 * application.
2833 */
2834 if (saveq->iocb.ulpStatus &&
2835 (pring->ringno == LPFC_ELS_RING) &&
2836 (cmdiocbp->iocb.ulpCommand ==
2837 CMD_ELS_REQUEST64_CR))
2838 lpfc_send_els_failure_event(phba,
2839 cmdiocbp, saveq);
2840
dea3101e
JB
2841 /*
2842 * Post all ELS completions to the worker thread.
2843 * All other are passed to the completion callback.
2844 */
2845 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2846 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2847 (cmdiocbp->iocb_flag &
2848 LPFC_DRIVER_ABORTED)) {
2849 spin_lock_irqsave(&phba->hbalock,
2850 iflag);
07951076
JS
2851 cmdiocbp->iocb_flag &=
2852 ~LPFC_DRIVER_ABORTED;
341af102
JS
2853 spin_unlock_irqrestore(&phba->hbalock,
2854 iflag);
07951076
JS
2855 saveq->iocb.ulpStatus =
2856 IOSTAT_LOCAL_REJECT;
2857 saveq->iocb.un.ulpWord[4] =
2858 IOERR_SLI_ABORTED;
0ff10d46
JS
2859
2860 /* Firmware could still be in progress
2861 * of DMAing payload, so don't free data
2862 * buffer till after a hbeat.
2863 */
341af102
JS
2864 spin_lock_irqsave(&phba->hbalock,
2865 iflag);
0ff10d46 2866 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2867 spin_unlock_irqrestore(&phba->hbalock,
2868 iflag);
2869 }
0f65ff68
JS
2870 if (phba->sli_rev == LPFC_SLI_REV4) {
2871 if (saveq->iocb_flag &
2872 LPFC_EXCHANGE_BUSY) {
2873 /* Set cmdiocb flag for the
2874 * exchange busy so sgl (xri)
2875 * will not be released until
2876 * the abort xri is received
2877 * from hba.
2878 */
2879 spin_lock_irqsave(
2880 &phba->hbalock, iflag);
2881 cmdiocbp->iocb_flag |=
2882 LPFC_EXCHANGE_BUSY;
2883 spin_unlock_irqrestore(
2884 &phba->hbalock, iflag);
2885 }
2886 if (cmdiocbp->iocb_flag &
2887 LPFC_DRIVER_ABORTED) {
2888 /*
2889 * Clear LPFC_DRIVER_ABORTED
2890 * bit in case it was driver
2891 * initiated abort.
2892 */
2893 spin_lock_irqsave(
2894 &phba->hbalock, iflag);
2895 cmdiocbp->iocb_flag &=
2896 ~LPFC_DRIVER_ABORTED;
2897 spin_unlock_irqrestore(
2898 &phba->hbalock, iflag);
2899 cmdiocbp->iocb.ulpStatus =
2900 IOSTAT_LOCAL_REJECT;
2901 cmdiocbp->iocb.un.ulpWord[4] =
2902 IOERR_ABORT_REQUESTED;
2903 /*
2904 * For SLI4, irsiocb contains
2905 * NO_XRI in sli_xritag, it
2906 * shall not affect releasing
2907 * sgl (xri) process.
2908 */
2909 saveq->iocb.ulpStatus =
2910 IOSTAT_LOCAL_REJECT;
2911 saveq->iocb.un.ulpWord[4] =
2912 IOERR_SLI_ABORTED;
2913 spin_lock_irqsave(
2914 &phba->hbalock, iflag);
2915 saveq->iocb_flag |=
2916 LPFC_DELAY_MEM_FREE;
2917 spin_unlock_irqrestore(
2918 &phba->hbalock, iflag);
2919 }
07951076 2920 }
dea3101e 2921 }
2e0fef85 2922 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2923 } else
2924 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e
JB
2925 } else {
2926 /*
2927 * Unknown initiating command based on the response iotag.
2928 * This could be the case on the ELS ring because of
2929 * lpfc_els_abort().
2930 */
2931 if (pring->ringno != LPFC_ELS_RING) {
2932 /*
2933 * Ring <ringno> handler: unexpected completion IoTag
2934 * <IoTag>
2935 */
a257bf90 2936 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2937 "0322 Ring %d handler: "
2938 "unexpected completion IoTag x%x "
2939 "Data: x%x x%x x%x x%x\n",
2940 pring->ringno,
2941 saveq->iocb.ulpIoTag,
2942 saveq->iocb.ulpStatus,
2943 saveq->iocb.un.ulpWord[4],
2944 saveq->iocb.ulpCommand,
2945 saveq->iocb.ulpContext);
dea3101e
JB
2946 }
2947 }
68876920 2948
dea3101e
JB
2949 return rc;
2950}
2951
e59058c4 2952/**
3621a710 2953 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2954 * @phba: Pointer to HBA context object.
2955 * @pring: Pointer to driver SLI ring object.
2956 *
2957 * This function is called from the iocb ring event handlers when
2958 * put pointer is ahead of the get pointer for a ring. This function signal
2959 * an error attention condition to the worker thread and the worker
2960 * thread will transition the HBA to offline state.
2961 **/
2e0fef85
JS
2962static void
2963lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2964{
34b02dcd 2965 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2966 /*
025dfdaf 2967 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2968 * rsp ring <portRspMax>
2969 */
2970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2971 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2972 "is bigger than rsp ring %d\n",
e8b62011 2973 pring->ringno, le32_to_cpu(pgp->rspPutInx),
7e56aa25 2974 pring->sli.sli3.numRiocb);
875fbdfe 2975
2e0fef85 2976 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2977
2978 /*
2979 * All error attention handlers are posted to
2980 * worker thread
2981 */
2982 phba->work_ha |= HA_ERATT;
2983 phba->work_hs = HS_FFER3;
92d7f7b0 2984
5e9d9b82 2985 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2986
2987 return;
2988}
2989
9399627f 2990/**
3621a710 2991 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2992 * @ptr: Pointer to address of HBA context object.
2993 *
2994 * This function is invoked by the Error Attention polling timer when the
2995 * timer times out. It will check the SLI Error Attention register for
2996 * possible attention events. If so, it will post an Error Attention event
2997 * and wake up worker thread to process it. Otherwise, it will set up the
2998 * Error Attention polling timer for the next poll.
2999 **/
3000void lpfc_poll_eratt(unsigned long ptr)
3001{
3002 struct lpfc_hba *phba;
eb016566 3003 uint32_t eratt = 0;
aa6fbb75 3004 uint64_t sli_intr, cnt;
9399627f
JS
3005
3006 phba = (struct lpfc_hba *)ptr;
3007
aa6fbb75
JS
3008 /* Here we will also keep track of interrupts per sec of the hba */
3009 sli_intr = phba->sli.slistat.sli_intr;
3010
3011 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3012 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3013 sli_intr);
3014 else
3015 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3016
65791f1f
JS
3017 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3018 do_div(cnt, phba->eratt_poll_interval);
aa6fbb75
JS
3019 phba->sli.slistat.sli_ips = cnt;
3020
3021 phba->sli.slistat.sli_prev_intr = sli_intr;
3022
9399627f
JS
3023 /* Check chip HA register for error event */
3024 eratt = lpfc_sli_check_eratt(phba);
3025
3026 if (eratt)
3027 /* Tell the worker thread there is work to do */
3028 lpfc_worker_wake_up(phba);
3029 else
3030 /* Restart the timer for next eratt poll */
256ec0d0
JS
3031 mod_timer(&phba->eratt_poll,
3032 jiffies +
65791f1f 3033 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
9399627f
JS
3034 return;
3035}
3036
875fbdfe 3037
e59058c4 3038/**
3621a710 3039 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
3040 * @phba: Pointer to HBA context object.
3041 * @pring: Pointer to driver SLI ring object.
3042 * @mask: Host attention register mask for this ring.
3043 *
3044 * This function is called from the interrupt context when there is a ring
3045 * event for the fcp ring. The caller does not hold any lock.
3046 * The function processes each response iocb in the response ring until it
25985edc 3047 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
3048 * LE bit set. The function will call the completion handler of the command iocb
3049 * if the response iocb indicates a completion for a command iocb or it is
3050 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3051 * function if this is an unsolicited iocb.
dea3101e 3052 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
3053 * to check it explicitly.
3054 */
3055int
2e0fef85
JS
3056lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3057 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3058{
34b02dcd 3059 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 3060 IOCB_t *irsp = NULL;
87f6eaff 3061 IOCB_t *entry = NULL;
dea3101e
JB
3062 struct lpfc_iocbq *cmdiocbq = NULL;
3063 struct lpfc_iocbq rspiocbq;
dea3101e
JB
3064 uint32_t status;
3065 uint32_t portRspPut, portRspMax;
3066 int rc = 1;
3067 lpfc_iocb_type type;
3068 unsigned long iflag;
3069 uint32_t rsp_cmpl = 0;
dea3101e 3070
2e0fef85 3071 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3072 pring->stats.iocb_event++;
3073
dea3101e
JB
3074 /*
3075 * The next available response entry should never exceed the maximum
3076 * entries. If it does, treat it as an adapter hardware error.
3077 */
7e56aa25 3078 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3079 portRspPut = le32_to_cpu(pgp->rspPutInx);
3080 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 3081 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 3082 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3083 return 1;
3084 }
45ed1190
JS
3085 if (phba->fcp_ring_in_use) {
3086 spin_unlock_irqrestore(&phba->hbalock, iflag);
3087 return 1;
3088 } else
3089 phba->fcp_ring_in_use = 1;
dea3101e
JB
3090
3091 rmb();
7e56aa25 3092 while (pring->sli.sli3.rspidx != portRspPut) {
87f6eaff
JSEC
3093 /*
3094 * Fetch an entry off the ring and copy it into a local data
3095 * structure. The copy involves a byte-swap since the
3096 * network byte order and pci byte orders are different.
3097 */
ed957684 3098 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 3099 phba->last_completion_time = jiffies;
875fbdfe 3100
7e56aa25
JS
3101 if (++pring->sli.sli3.rspidx >= portRspMax)
3102 pring->sli.sli3.rspidx = 0;
875fbdfe 3103
87f6eaff
JSEC
3104 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3105 (uint32_t *) &rspiocbq.iocb,
ed957684 3106 phba->iocb_rsp_size);
a4bc3379 3107 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
3108 irsp = &rspiocbq.iocb;
3109
dea3101e
JB
3110 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3111 pring->stats.iocb_rsp++;
3112 rsp_cmpl++;
3113
3114 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
3115 /*
3116 * If resource errors reported from HBA, reduce
3117 * queuedepths of the SCSI device.
3118 */
3119 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3120 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3121 IOERR_NO_RESOURCES)) {
92d7f7b0 3122 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3123 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
3124 spin_lock_irqsave(&phba->hbalock, iflag);
3125 }
3126
dea3101e
JB
3127 /* Rsp ring <ringno> error: IOCB */
3128 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 3129 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 3130 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 3131 pring->ringno,
92d7f7b0
JS
3132 irsp->un.ulpWord[0],
3133 irsp->un.ulpWord[1],
3134 irsp->un.ulpWord[2],
3135 irsp->un.ulpWord[3],
3136 irsp->un.ulpWord[4],
3137 irsp->un.ulpWord[5],
d7c255b2
JS
3138 *(uint32_t *)&irsp->un1,
3139 *((uint32_t *)&irsp->un1 + 1));
dea3101e
JB
3140 }
3141
3142 switch (type) {
3143 case LPFC_ABORT_IOCB:
3144 case LPFC_SOL_IOCB:
3145 /*
3146 * Idle exchange closed via ABTS from port. No iocb
3147 * resources need to be recovered.
3148 */
3149 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 3150 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3151 "0333 IOCB cmd 0x%x"
dca9479b 3152 " processed. Skipping"
92d7f7b0 3153 " completion\n",
dca9479b 3154 irsp->ulpCommand);
dea3101e
JB
3155 break;
3156 }
3157
604a3e30
JB
3158 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3159 &rspiocbq);
0f65ff68
JS
3160 if (unlikely(!cmdiocbq))
3161 break;
3162 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3163 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3164 if (cmdiocbq->iocb_cmpl) {
3165 spin_unlock_irqrestore(&phba->hbalock, iflag);
3166 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3167 &rspiocbq);
3168 spin_lock_irqsave(&phba->hbalock, iflag);
3169 }
dea3101e 3170 break;
a4bc3379 3171 case LPFC_UNSOL_IOCB:
2e0fef85 3172 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 3173 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 3174 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 3175 break;
dea3101e
JB
3176 default:
3177 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3178 char adaptermsg[LPFC_MAX_ADPTMSG];
3179 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3180 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3181 MAX_MSG_DATA);
898eb71c
JP
3182 dev_warn(&((phba->pcidev)->dev),
3183 "lpfc%d: %s\n",
dea3101e
JB
3184 phba->brd_no, adaptermsg);
3185 } else {
3186 /* Unknown IOCB command */
3187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3188 "0334 Unknown IOCB command "
92d7f7b0 3189 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 3190 type, irsp->ulpCommand,
92d7f7b0
JS
3191 irsp->ulpStatus,
3192 irsp->ulpIoTag,
3193 irsp->ulpContext);
dea3101e
JB
3194 }
3195 break;
3196 }
3197
3198 /*
3199 * The response IOCB has been processed. Update the ring
3200 * pointer in SLIM. If the port response put pointer has not
3201 * been updated, sync the pgp->rspPutInx and fetch the new port
3202 * response put pointer.
3203 */
7e56aa25
JS
3204 writel(pring->sli.sli3.rspidx,
3205 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3206
7e56aa25 3207 if (pring->sli.sli3.rspidx == portRspPut)
dea3101e
JB
3208 portRspPut = le32_to_cpu(pgp->rspPutInx);
3209 }
3210
3211 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3212 pring->stats.iocb_rsp_full++;
3213 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3214 writel(status, phba->CAregaddr);
3215 readl(phba->CAregaddr);
3216 }
3217 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3218 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3219 pring->stats.iocb_cmd_empty++;
3220
3221 /* Force update of the local copy of cmdGetInx */
7e56aa25 3222 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3223 lpfc_sli_resume_iocb(phba, pring);
3224
3225 if ((pring->lpfc_sli_cmd_available))
3226 (pring->lpfc_sli_cmd_available) (phba, pring);
3227
3228 }
3229
45ed1190 3230 phba->fcp_ring_in_use = 0;
2e0fef85 3231 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3232 return rc;
3233}
3234
e59058c4 3235/**
3772a991
JS
3236 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3237 * @phba: Pointer to HBA context object.
3238 * @pring: Pointer to driver SLI ring object.
3239 * @rspiocbp: Pointer to driver response IOCB object.
3240 *
3241 * This function is called from the worker thread when there is a slow-path
3242 * response IOCB to process. This function chains all the response iocbs until
3243 * seeing the iocb with the LE bit set. The function will call
3244 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3245 * completion of a command iocb. The function will call the
3246 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3247 * The function frees the resources or calls the completion handler if this
3248 * iocb is an abort completion. The function returns NULL when the response
3249 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3250 * this function shall chain the iocb on to the iocb_continueq and return the
3251 * response iocb passed in.
3252 **/
3253static struct lpfc_iocbq *
3254lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3255 struct lpfc_iocbq *rspiocbp)
3256{
3257 struct lpfc_iocbq *saveq;
3258 struct lpfc_iocbq *cmdiocbp;
3259 struct lpfc_iocbq *next_iocb;
3260 IOCB_t *irsp = NULL;
3261 uint32_t free_saveq;
3262 uint8_t iocb_cmd_type;
3263 lpfc_iocb_type type;
3264 unsigned long iflag;
3265 int rc;
3266
3267 spin_lock_irqsave(&phba->hbalock, iflag);
3268 /* First add the response iocb to the countinueq list */
3269 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3270 pring->iocb_continueq_cnt++;
3271
70f23fd6 3272 /* Now, determine whether the list is completed for processing */
3772a991
JS
3273 irsp = &rspiocbp->iocb;
3274 if (irsp->ulpLe) {
3275 /*
3276 * By default, the driver expects to free all resources
3277 * associated with this iocb completion.
3278 */
3279 free_saveq = 1;
3280 saveq = list_get_first(&pring->iocb_continueq,
3281 struct lpfc_iocbq, list);
3282 irsp = &(saveq->iocb);
3283 list_del_init(&pring->iocb_continueq);
3284 pring->iocb_continueq_cnt = 0;
3285
3286 pring->stats.iocb_rsp++;
3287
3288 /*
3289 * If resource errors reported from HBA, reduce
3290 * queuedepths of the SCSI device.
3291 */
3292 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
3293 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3294 IOERR_NO_RESOURCES)) {
3772a991
JS
3295 spin_unlock_irqrestore(&phba->hbalock, iflag);
3296 phba->lpfc_rampdown_queue_depth(phba);
3297 spin_lock_irqsave(&phba->hbalock, iflag);
3298 }
3299
3300 if (irsp->ulpStatus) {
3301 /* Rsp ring <ringno> error: IOCB */
3302 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3303 "0328 Rsp Ring %d error: "
3304 "IOCB Data: "
3305 "x%x x%x x%x x%x "
3306 "x%x x%x x%x x%x "
3307 "x%x x%x x%x x%x "
3308 "x%x x%x x%x x%x\n",
3309 pring->ringno,
3310 irsp->un.ulpWord[0],
3311 irsp->un.ulpWord[1],
3312 irsp->un.ulpWord[2],
3313 irsp->un.ulpWord[3],
3314 irsp->un.ulpWord[4],
3315 irsp->un.ulpWord[5],
3316 *(((uint32_t *) irsp) + 6),
3317 *(((uint32_t *) irsp) + 7),
3318 *(((uint32_t *) irsp) + 8),
3319 *(((uint32_t *) irsp) + 9),
3320 *(((uint32_t *) irsp) + 10),
3321 *(((uint32_t *) irsp) + 11),
3322 *(((uint32_t *) irsp) + 12),
3323 *(((uint32_t *) irsp) + 13),
3324 *(((uint32_t *) irsp) + 14),
3325 *(((uint32_t *) irsp) + 15));
3326 }
3327
3328 /*
3329 * Fetch the IOCB command type and call the correct completion
3330 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3331 * get freed back to the lpfc_iocb_list by the discovery
3332 * kernel thread.
3333 */
3334 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3335 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3336 switch (type) {
3337 case LPFC_SOL_IOCB:
3338 spin_unlock_irqrestore(&phba->hbalock, iflag);
3339 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3340 spin_lock_irqsave(&phba->hbalock, iflag);
3341 break;
3342
3343 case LPFC_UNSOL_IOCB:
3344 spin_unlock_irqrestore(&phba->hbalock, iflag);
3345 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3346 spin_lock_irqsave(&phba->hbalock, iflag);
3347 if (!rc)
3348 free_saveq = 0;
3349 break;
3350
3351 case LPFC_ABORT_IOCB:
3352 cmdiocbp = NULL;
3353 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3354 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3355 saveq);
3356 if (cmdiocbp) {
3357 /* Call the specified completion routine */
3358 if (cmdiocbp->iocb_cmpl) {
3359 spin_unlock_irqrestore(&phba->hbalock,
3360 iflag);
3361 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3362 saveq);
3363 spin_lock_irqsave(&phba->hbalock,
3364 iflag);
3365 } else
3366 __lpfc_sli_release_iocbq(phba,
3367 cmdiocbp);
3368 }
3369 break;
3370
3371 case LPFC_UNKNOWN_IOCB:
3372 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3373 char adaptermsg[LPFC_MAX_ADPTMSG];
3374 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3375 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3376 MAX_MSG_DATA);
3377 dev_warn(&((phba->pcidev)->dev),
3378 "lpfc%d: %s\n",
3379 phba->brd_no, adaptermsg);
3380 } else {
3381 /* Unknown IOCB command */
3382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3383 "0335 Unknown IOCB "
3384 "command Data: x%x "
3385 "x%x x%x x%x\n",
3386 irsp->ulpCommand,
3387 irsp->ulpStatus,
3388 irsp->ulpIoTag,
3389 irsp->ulpContext);
3390 }
3391 break;
3392 }
3393
3394 if (free_saveq) {
3395 list_for_each_entry_safe(rspiocbp, next_iocb,
3396 &saveq->list, list) {
61f35bff 3397 list_del_init(&rspiocbp->list);
3772a991
JS
3398 __lpfc_sli_release_iocbq(phba, rspiocbp);
3399 }
3400 __lpfc_sli_release_iocbq(phba, saveq);
3401 }
3402 rspiocbp = NULL;
3403 }
3404 spin_unlock_irqrestore(&phba->hbalock, iflag);
3405 return rspiocbp;
3406}
3407
3408/**
3409 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3410 * @phba: Pointer to HBA context object.
3411 * @pring: Pointer to driver SLI ring object.
3412 * @mask: Host attention register mask for this ring.
3413 *
3772a991
JS
3414 * This routine wraps the actual slow_ring event process routine from the
3415 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3416 **/
3772a991 3417void
2e0fef85
JS
3418lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3419 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3420{
3421 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3422}
3423
3424/**
3425 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3426 * @phba: Pointer to HBA context object.
3427 * @pring: Pointer to driver SLI ring object.
3428 * @mask: Host attention register mask for this ring.
3429 *
3430 * This function is called from the worker thread when there is a ring event
3431 * for non-fcp rings. The caller does not hold any lock. The function will
3432 * remove each response iocb in the response ring and calls the handle
3433 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3434 **/
3435static void
3436lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3437 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3438{
34b02dcd 3439 struct lpfc_pgp *pgp;
dea3101e
JB
3440 IOCB_t *entry;
3441 IOCB_t *irsp = NULL;
3442 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3443 uint32_t portRspPut, portRspMax;
dea3101e 3444 unsigned long iflag;
3772a991 3445 uint32_t status;
dea3101e 3446
34b02dcd 3447 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3448 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3449 pring->stats.iocb_event++;
3450
dea3101e
JB
3451 /*
3452 * The next available response entry should never exceed the maximum
3453 * entries. If it does, treat it as an adapter hardware error.
3454 */
7e56aa25 3455 portRspMax = pring->sli.sli3.numRiocb;
dea3101e
JB
3456 portRspPut = le32_to_cpu(pgp->rspPutInx);
3457 if (portRspPut >= portRspMax) {
3458 /*
025dfdaf 3459 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e
JB
3460 * rsp ring <portRspMax>
3461 */
ed957684 3462 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3463 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3464 "is bigger than rsp ring %d\n",
e8b62011 3465 pring->ringno, portRspPut, portRspMax);
dea3101e 3466
2e0fef85
JS
3467 phba->link_state = LPFC_HBA_ERROR;
3468 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
3469
3470 phba->work_hs = HS_FFER3;
3471 lpfc_handle_eratt(phba);
3472
3772a991 3473 return;
dea3101e
JB
3474 }
3475
3476 rmb();
7e56aa25 3477 while (pring->sli.sli3.rspidx != portRspPut) {
dea3101e
JB
3478 /*
3479 * Build a completion list and call the appropriate handler.
3480 * The process is to get the next available response iocb, get
3481 * a free iocb from the list, copy the response data into the
3482 * free iocb, insert to the continuation list, and update the
3483 * next response index to slim. This process makes response
3484 * iocb's in the ring available to DMA as fast as possible but
3485 * pays a penalty for a copy operation. Since the iocb is
3486 * only 32 bytes, this penalty is considered small relative to
3487 * the PCI reads for register values and a slim write. When
3488 * the ulpLe field is set, the entire Command has been
3489 * received.
3490 */
ed957684
JS
3491 entry = lpfc_resp_iocb(phba, pring);
3492
858c9f6c 3493 phba->last_completion_time = jiffies;
2e0fef85 3494 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
3495 if (rspiocbp == NULL) {
3496 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3497 "completion.\n", __func__);
dea3101e
JB
3498 break;
3499 }
3500
ed957684
JS
3501 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3502 phba->iocb_rsp_size);
dea3101e
JB
3503 irsp = &rspiocbp->iocb;
3504
7e56aa25
JS
3505 if (++pring->sli.sli3.rspidx >= portRspMax)
3506 pring->sli.sli3.rspidx = 0;
dea3101e 3507
a58cbd52
JS
3508 if (pring->ringno == LPFC_ELS_RING) {
3509 lpfc_debugfs_slow_ring_trc(phba,
3510 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3511 *(((uint32_t *) irsp) + 4),
3512 *(((uint32_t *) irsp) + 6),
3513 *(((uint32_t *) irsp) + 7));
3514 }
3515
7e56aa25
JS
3516 writel(pring->sli.sli3.rspidx,
3517 &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3518
3772a991
JS
3519 spin_unlock_irqrestore(&phba->hbalock, iflag);
3520 /* Handle the response IOCB */
3521 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3522 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e
JB
3523
3524 /*
3525 * If the port response put pointer has not been updated, sync
3526 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3527 * response put pointer.
3528 */
7e56aa25 3529 if (pring->sli.sli3.rspidx == portRspPut) {
dea3101e
JB
3530 portRspPut = le32_to_cpu(pgp->rspPutInx);
3531 }
7e56aa25 3532 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea3101e 3533
92d7f7b0 3534 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e
JB
3535 /* At least one response entry has been freed */
3536 pring->stats.iocb_rsp_full++;
3537 /* SET RxRE_RSP in Chip Att register */
3538 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3539 writel(status, phba->CAregaddr);
3540 readl(phba->CAregaddr); /* flush */
3541 }
3542 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3543 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3544 pring->stats.iocb_cmd_empty++;
3545
3546 /* Force update of the local copy of cmdGetInx */
7e56aa25 3547 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea3101e
JB
3548 lpfc_sli_resume_iocb(phba, pring);
3549
3550 if ((pring->lpfc_sli_cmd_available))
3551 (pring->lpfc_sli_cmd_available) (phba, pring);
3552
3553 }
3554
2e0fef85 3555 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3556 return;
dea3101e
JB
3557}
3558
4f774513
JS
3559/**
3560 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3561 * @phba: Pointer to HBA context object.
3562 * @pring: Pointer to driver SLI ring object.
3563 * @mask: Host attention register mask for this ring.
3564 *
3565 * This function is called from the worker thread when there is a pending
3566 * ELS response iocb on the driver internal slow-path response iocb worker
3567 * queue. The caller does not hold any lock. The function will remove each
3568 * response iocb from the response worker queue and calls the handle
3569 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3570 **/
3571static void
3572lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3573 struct lpfc_sli_ring *pring, uint32_t mask)
3574{
3575 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3576 struct hbq_dmabuf *dmabuf;
3577 struct lpfc_cq_event *cq_event;
4f774513
JS
3578 unsigned long iflag;
3579
45ed1190
JS
3580 spin_lock_irqsave(&phba->hbalock, iflag);
3581 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3582 spin_unlock_irqrestore(&phba->hbalock, iflag);
3583 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3584 /* Get the response iocb from the head of work queue */
3585 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3586 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3587 cq_event, struct lpfc_cq_event, list);
4f774513 3588 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3589
3590 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3591 case CQE_CODE_COMPL_WQE:
3592 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3593 cq_event);
45ed1190
JS
3594 /* Translate ELS WCQE to response IOCBQ */
3595 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3596 irspiocbq);
3597 if (irspiocbq)
3598 lpfc_sli_sp_handle_rspiocb(phba, pring,
3599 irspiocbq);
4d9ab994
JS
3600 break;
3601 case CQE_CODE_RECEIVE:
7851fe2c 3602 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3603 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3604 cq_event);
3605 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3606 break;
3607 default:
3608 break;
3609 }
4f774513
JS
3610 }
3611}
3612
e59058c4 3613/**
3621a710 3614 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3615 * @phba: Pointer to HBA context object.
3616 * @pring: Pointer to driver SLI ring object.
3617 *
3618 * This function aborts all iocbs in the given ring and frees all the iocb
3619 * objects in txq. This function issues an abort iocb for all the iocb commands
3620 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3621 * the return of this function. The caller is not required to hold any locks.
3622 **/
2e0fef85 3623void
dea3101e
JB
3624lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3625{
2534ba75 3626 LIST_HEAD(completions);
dea3101e 3627 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3628
92d7f7b0
JS
3629 if (pring->ringno == LPFC_ELS_RING) {
3630 lpfc_fabric_abort_hba(phba);
3631 }
3632
dea3101e
JB
3633 /* Error everything on txq and txcmplq
3634 * First do the txq.
3635 */
db55fba8
JS
3636 if (phba->sli_rev >= LPFC_SLI_REV4) {
3637 spin_lock_irq(&pring->ring_lock);
3638 list_splice_init(&pring->txq, &completions);
3639 pring->txq_cnt = 0;
3640 spin_unlock_irq(&pring->ring_lock);
dea3101e 3641
db55fba8
JS
3642 spin_lock_irq(&phba->hbalock);
3643 /* Next issue ABTS for everything on the txcmplq */
3644 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3645 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3646 spin_unlock_irq(&phba->hbalock);
3647 } else {
3648 spin_lock_irq(&phba->hbalock);
3649 list_splice_init(&pring->txq, &completions);
3650 pring->txq_cnt = 0;
dea3101e 3651
db55fba8
JS
3652 /* Next issue ABTS for everything on the txcmplq */
3653 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3654 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3655 spin_unlock_irq(&phba->hbalock);
3656 }
dea3101e 3657
a257bf90
JS
3658 /* Cancel all the IOCBs from the completions list */
3659 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3660 IOERR_SLI_ABORTED);
dea3101e
JB
3661}
3662
895427bd
JS
3663/**
3664 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3665 * @phba: Pointer to HBA context object.
3666 * @pring: Pointer to driver SLI ring object.
3667 *
3668 * This function aborts all iocbs in the given ring and frees all the iocb
3669 * objects in txq. This function issues an abort iocb for all the iocb commands
3670 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3671 * the return of this function. The caller is not required to hold any locks.
3672 **/
3673void
3674lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3675{
3676 LIST_HEAD(completions);
3677 struct lpfc_iocbq *iocb, *next_iocb;
3678
3679 if (pring->ringno == LPFC_ELS_RING)
3680 lpfc_fabric_abort_hba(phba);
3681
3682 spin_lock_irq(&phba->hbalock);
3683 /* Next issue ABTS for everything on the txcmplq */
3684 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3685 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3686 spin_unlock_irq(&phba->hbalock);
3687}
3688
3689
db55fba8
JS
3690/**
3691 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3692 * @phba: Pointer to HBA context object.
3693 * @pring: Pointer to driver SLI ring object.
3694 *
3695 * This function aborts all iocbs in FCP rings and frees all the iocb
3696 * objects in txq. This function issues an abort iocb for all the iocb commands
3697 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3698 * the return of this function. The caller is not required to hold any locks.
3699 **/
3700void
3701lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3702{
3703 struct lpfc_sli *psli = &phba->sli;
3704 struct lpfc_sli_ring *pring;
3705 uint32_t i;
3706
3707 /* Look on all the FCP Rings for the iotag */
3708 if (phba->sli_rev >= LPFC_SLI_REV4) {
3709 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3710 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3711 lpfc_sli_abort_iocb_ring(phba, pring);
3712 }
3713 } else {
895427bd 3714 pring = &psli->sli3_ring[LPFC_FCP_RING];
db55fba8
JS
3715 lpfc_sli_abort_iocb_ring(phba, pring);
3716 }
3717}
3718
895427bd
JS
3719/**
3720 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3721 * @phba: Pointer to HBA context object.
3722 *
3723 * This function aborts all wqes in NVME rings. This function issues an
3724 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3725 * the txcmplq is not guaranteed to complete before the return of this
3726 * function. The caller is not required to hold any locks.
3727 **/
3728void
3729lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3730{
3731 struct lpfc_sli_ring *pring;
3732 uint32_t i;
3733
3734 if (phba->sli_rev < LPFC_SLI_REV4)
3735 return;
3736
3737 /* Abort all IO on each NVME ring. */
3738 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3739 pring = phba->sli4_hba.nvme_wq[i]->pring;
3740 lpfc_sli_abort_wqe_ring(phba, pring);
3741 }
3742}
3743
db55fba8 3744
a8e497d5 3745/**
3621a710 3746 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3747 * @phba: Pointer to HBA context object.
3748 *
3749 * This function flushes all iocbs in the fcp ring and frees all the iocb
3750 * objects in txq and txcmplq. This function will not issue abort iocbs
3751 * for all the iocb commands in txcmplq, they will just be returned with
3752 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3753 * slot has been permanently disabled.
3754 **/
3755void
3756lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3757{
3758 LIST_HEAD(txq);
3759 LIST_HEAD(txcmplq);
a8e497d5
JS
3760 struct lpfc_sli *psli = &phba->sli;
3761 struct lpfc_sli_ring *pring;
db55fba8 3762 uint32_t i;
a8e497d5
JS
3763
3764 spin_lock_irq(&phba->hbalock);
4f2e66c6
JS
3765 /* Indicate the I/O queues are flushed */
3766 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
a8e497d5
JS
3767 spin_unlock_irq(&phba->hbalock);
3768
db55fba8
JS
3769 /* Look on all the FCP Rings for the iotag */
3770 if (phba->sli_rev >= LPFC_SLI_REV4) {
3771 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
895427bd 3772 pring = phba->sli4_hba.fcp_wq[i]->pring;
db55fba8
JS
3773
3774 spin_lock_irq(&pring->ring_lock);
3775 /* Retrieve everything on txq */
3776 list_splice_init(&pring->txq, &txq);
3777 /* Retrieve everything on the txcmplq */
3778 list_splice_init(&pring->txcmplq, &txcmplq);
3779 pring->txq_cnt = 0;
3780 pring->txcmplq_cnt = 0;
3781 spin_unlock_irq(&pring->ring_lock);
3782
3783 /* Flush the txq */
3784 lpfc_sli_cancel_iocbs(phba, &txq,
3785 IOSTAT_LOCAL_REJECT,
3786 IOERR_SLI_DOWN);
3787 /* Flush the txcmpq */
3788 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3789 IOSTAT_LOCAL_REJECT,
3790 IOERR_SLI_DOWN);
3791 }
3792 } else {
895427bd 3793 pring = &psli->sli3_ring[LPFC_FCP_RING];
a8e497d5 3794
db55fba8
JS
3795 spin_lock_irq(&phba->hbalock);
3796 /* Retrieve everything on txq */
3797 list_splice_init(&pring->txq, &txq);
3798 /* Retrieve everything on the txcmplq */
3799 list_splice_init(&pring->txcmplq, &txcmplq);
3800 pring->txq_cnt = 0;
3801 pring->txcmplq_cnt = 0;
3802 spin_unlock_irq(&phba->hbalock);
3803
3804 /* Flush the txq */
3805 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3806 IOERR_SLI_DOWN);
3807 /* Flush the txcmpq */
3808 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3809 IOERR_SLI_DOWN);
3810 }
a8e497d5
JS
3811}
3812
895427bd
JS
3813/**
3814 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3815 * @phba: Pointer to HBA context object.
3816 *
3817 * This function flushes all wqes in the nvme rings and frees all resources
3818 * in the txcmplq. This function does not issue abort wqes for the IO
3819 * commands in txcmplq, they will just be returned with
3820 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3821 * slot has been permanently disabled.
3822 **/
3823void
3824lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3825{
3826 LIST_HEAD(txcmplq);
3827 struct lpfc_sli_ring *pring;
3828 uint32_t i;
3829
3830 if (phba->sli_rev < LPFC_SLI_REV4)
3831 return;
3832
3833 /* Hint to other driver operations that a flush is in progress. */
3834 spin_lock_irq(&phba->hbalock);
3835 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3836 spin_unlock_irq(&phba->hbalock);
3837
3838 /* Cycle through all NVME rings and complete each IO with
3839 * a local driver reason code. This is a flush so no
3840 * abort exchange to FW.
3841 */
3842 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3843 pring = phba->sli4_hba.nvme_wq[i]->pring;
3844
3845 /* Retrieve everything on the txcmplq */
3846 spin_lock_irq(&pring->ring_lock);
3847 list_splice_init(&pring->txcmplq, &txcmplq);
3848 pring->txcmplq_cnt = 0;
3849 spin_unlock_irq(&pring->ring_lock);
3850
3851 /* Flush the txcmpq &&&PAE */
3852 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3853 IOSTAT_LOCAL_REJECT,
3854 IOERR_SLI_DOWN);
3855 }
3856}
3857
e59058c4 3858/**
3772a991 3859 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3860 * @phba: Pointer to HBA context object.
3861 * @mask: Bit mask to be checked.
3862 *
3863 * This function reads the host status register and compares
3864 * with the provided bit mask to check if HBA completed
3865 * the restart. This function will wait in a loop for the
3866 * HBA to complete restart. If the HBA does not restart within
3867 * 15 iterations, the function will reset the HBA again. The
3868 * function returns 1 when HBA fail to restart otherwise returns
3869 * zero.
3870 **/
3772a991
JS
3871static int
3872lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3873{
41415862
JW
3874 uint32_t status;
3875 int i = 0;
3876 int retval = 0;
dea3101e 3877
41415862 3878 /* Read the HBA Host Status Register */
9940b97b
JS
3879 if (lpfc_readl(phba->HSregaddr, &status))
3880 return 1;
dea3101e 3881
41415862
JW
3882 /*
3883 * Check status register every 100ms for 5 retries, then every
3884 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3885 * every 2.5 sec for 4.
3886 * Break our of the loop if errors occurred during init.
3887 */
3888 while (((status & mask) != mask) &&
3889 !(status & HS_FFERM) &&
3890 i++ < 20) {
dea3101e 3891
41415862
JW
3892 if (i <= 5)
3893 msleep(10);
3894 else if (i <= 10)
3895 msleep(500);
3896 else
3897 msleep(2500);
dea3101e 3898
41415862 3899 if (i == 15) {
2e0fef85 3900 /* Do post */
92d7f7b0 3901 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3902 lpfc_sli_brdrestart(phba);
3903 }
3904 /* Read the HBA Host Status Register */
9940b97b
JS
3905 if (lpfc_readl(phba->HSregaddr, &status)) {
3906 retval = 1;
3907 break;
3908 }
41415862 3909 }
dea3101e 3910
41415862
JW
3911 /* Check to see if any errors occurred during init */
3912 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3914 "2751 Adapter failed to restart, "
3915 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3916 status,
3917 readl(phba->MBslimaddr + 0xa8),
3918 readl(phba->MBslimaddr + 0xac));
2e0fef85 3919 phba->link_state = LPFC_HBA_ERROR;
41415862 3920 retval = 1;
dea3101e 3921 }
dea3101e 3922
41415862
JW
3923 return retval;
3924}
dea3101e 3925
da0436e9
JS
3926/**
3927 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3928 * @phba: Pointer to HBA context object.
3929 * @mask: Bit mask to be checked.
3930 *
3931 * This function checks the host status register to check if HBA is
3932 * ready. This function will wait in a loop for the HBA to be ready
3933 * If the HBA is not ready , the function will will reset the HBA PCI
3934 * function again. The function returns 1 when HBA fail to be ready
3935 * otherwise returns zero.
3936 **/
3937static int
3938lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3939{
3940 uint32_t status;
3941 int retval = 0;
3942
3943 /* Read the HBA Host Status Register */
3944 status = lpfc_sli4_post_status_check(phba);
3945
3946 if (status) {
3947 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3948 lpfc_sli_brdrestart(phba);
3949 status = lpfc_sli4_post_status_check(phba);
3950 }
3951
3952 /* Check to see if any errors occurred during init */
3953 if (status) {
3954 phba->link_state = LPFC_HBA_ERROR;
3955 retval = 1;
3956 } else
3957 phba->sli4_hba.intr_enable = 0;
3958
3959 return retval;
3960}
3961
3962/**
3963 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3964 * @phba: Pointer to HBA context object.
3965 * @mask: Bit mask to be checked.
3966 *
3967 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3968 * from the API jump table function pointer from the lpfc_hba struct.
3969 **/
3970int
3971lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3972{
3973 return phba->lpfc_sli_brdready(phba, mask);
3974}
3975
9290831f
JS
3976#define BARRIER_TEST_PATTERN (0xdeadbeef)
3977
e59058c4 3978/**
3621a710 3979 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3980 * @phba: Pointer to HBA context object.
3981 *
1b51197d
JS
3982 * This function is called before resetting an HBA. This function is called
3983 * with hbalock held and requests HBA to quiesce DMAs before a reset.
e59058c4 3984 **/
2e0fef85 3985void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3986{
65a29c16
JS
3987 uint32_t __iomem *resp_buf;
3988 uint32_t __iomem *mbox_buf;
9290831f 3989 volatile uint32_t mbox;
9940b97b 3990 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3991 int i;
3992 uint8_t hdrtype;
3993
1c2ba475
JT
3994 lockdep_assert_held(&phba->hbalock);
3995
9290831f
JS
3996 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3997 if (hdrtype != 0x80 ||
3998 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3999 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4000 return;
4001
4002 /*
4003 * Tell the other part of the chip to suspend temporarily all
4004 * its DMA activity.
4005 */
65a29c16 4006 resp_buf = phba->MBslimaddr;
9290831f
JS
4007
4008 /* Disable the error attention */
9940b97b
JS
4009 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4010 return;
9290831f
JS
4011 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4012 readl(phba->HCregaddr); /* flush */
2e0fef85 4013 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 4014
9940b97b
JS
4015 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4016 return;
4017 if (ha_copy & HA_ERATT) {
9290831f
JS
4018 /* Clear Chip error bit */
4019 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4020 phba->pport->stopped = 1;
9290831f
JS
4021 }
4022
4023 mbox = 0;
4024 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4025 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4026
4027 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 4028 mbox_buf = phba->MBslimaddr;
9290831f
JS
4029 writel(mbox, mbox_buf);
4030
9940b97b
JS
4031 for (i = 0; i < 50; i++) {
4032 if (lpfc_readl((resp_buf + 1), &resp_data))
4033 return;
4034 if (resp_data != ~(BARRIER_TEST_PATTERN))
4035 mdelay(1);
4036 else
4037 break;
4038 }
4039 resp_data = 0;
4040 if (lpfc_readl((resp_buf + 1), &resp_data))
4041 return;
4042 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 4043 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 4044 phba->pport->stopped)
9290831f
JS
4045 goto restore_hc;
4046 else
4047 goto clear_errat;
4048 }
4049
4050 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
4051 resp_data = 0;
4052 for (i = 0; i < 500; i++) {
4053 if (lpfc_readl(resp_buf, &resp_data))
4054 return;
4055 if (resp_data != mbox)
4056 mdelay(1);
4057 else
4058 break;
4059 }
9290831f
JS
4060
4061clear_errat:
4062
9940b97b
JS
4063 while (++i < 500) {
4064 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4065 return;
4066 if (!(ha_copy & HA_ERATT))
4067 mdelay(1);
4068 else
4069 break;
4070 }
9290831f
JS
4071
4072 if (readl(phba->HAregaddr) & HA_ERATT) {
4073 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4074 phba->pport->stopped = 1;
9290831f
JS
4075 }
4076
4077restore_hc:
2e0fef85 4078 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
4079 writel(hc_copy, phba->HCregaddr);
4080 readl(phba->HCregaddr); /* flush */
4081}
4082
e59058c4 4083/**
3621a710 4084 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
4085 * @phba: Pointer to HBA context object.
4086 *
4087 * This function issues a kill_board mailbox command and waits for
4088 * the error attention interrupt. This function is called for stopping
4089 * the firmware processing. The caller is not required to hold any
4090 * locks. This function calls lpfc_hba_down_post function to free
4091 * any pending commands after the kill. The function will return 1 when it
4092 * fails to kill the board else will return 0.
4093 **/
41415862 4094int
2e0fef85 4095lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
4096{
4097 struct lpfc_sli *psli;
4098 LPFC_MBOXQ_t *pmb;
4099 uint32_t status;
4100 uint32_t ha_copy;
4101 int retval;
4102 int i = 0;
dea3101e 4103
41415862 4104 psli = &phba->sli;
dea3101e 4105
41415862 4106 /* Kill HBA */
ed957684 4107 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
4108 "0329 Kill HBA Data: x%x x%x\n",
4109 phba->pport->port_state, psli->sli_flag);
41415862 4110
98c9ea5c
JS
4111 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4112 if (!pmb)
41415862 4113 return 1;
41415862
JW
4114
4115 /* Disable the error attention */
2e0fef85 4116 spin_lock_irq(&phba->hbalock);
9940b97b
JS
4117 if (lpfc_readl(phba->HCregaddr, &status)) {
4118 spin_unlock_irq(&phba->hbalock);
4119 mempool_free(pmb, phba->mbox_mem_pool);
4120 return 1;
4121 }
41415862
JW
4122 status &= ~HC_ERINT_ENA;
4123 writel(status, phba->HCregaddr);
4124 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
4125 phba->link_flag |= LS_IGNORE_ERATT;
4126 spin_unlock_irq(&phba->hbalock);
41415862
JW
4127
4128 lpfc_kill_board(phba, pmb);
4129 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4130 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4131
4132 if (retval != MBX_SUCCESS) {
4133 if (retval != MBX_BUSY)
4134 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
4135 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4136 "2752 KILL_BOARD command failed retval %d\n",
4137 retval);
2e0fef85
JS
4138 spin_lock_irq(&phba->hbalock);
4139 phba->link_flag &= ~LS_IGNORE_ERATT;
4140 spin_unlock_irq(&phba->hbalock);
41415862
JW
4141 return 1;
4142 }
4143
f4b4c68f
JS
4144 spin_lock_irq(&phba->hbalock);
4145 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4146 spin_unlock_irq(&phba->hbalock);
9290831f 4147
41415862
JW
4148 mempool_free(pmb, phba->mbox_mem_pool);
4149
4150 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4151 * attention every 100ms for 3 seconds. If we don't get ERATT after
4152 * 3 seconds we still set HBA_ERROR state because the status of the
4153 * board is now undefined.
4154 */
9940b97b
JS
4155 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4156 return 1;
41415862
JW
4157 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4158 mdelay(100);
9940b97b
JS
4159 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4160 return 1;
41415862
JW
4161 }
4162
4163 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
4164 if (ha_copy & HA_ERATT) {
4165 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 4166 phba->pport->stopped = 1;
9290831f 4167 }
2e0fef85 4168 spin_lock_irq(&phba->hbalock);
41415862 4169 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 4170 psli->mbox_active = NULL;
2e0fef85
JS
4171 phba->link_flag &= ~LS_IGNORE_ERATT;
4172 spin_unlock_irq(&phba->hbalock);
41415862 4173
41415862 4174 lpfc_hba_down_post(phba);
2e0fef85 4175 phba->link_state = LPFC_HBA_ERROR;
41415862 4176
2e0fef85 4177 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e
JB
4178}
4179
e59058c4 4180/**
3772a991 4181 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
4182 * @phba: Pointer to HBA context object.
4183 *
4184 * This function resets the HBA by writing HC_INITFF to the control
4185 * register. After the HBA resets, this function resets all the iocb ring
4186 * indices. This function disables PCI layer parity checking during
4187 * the reset.
4188 * This function returns 0 always.
4189 * The caller is not required to hold any locks.
4190 **/
41415862 4191int
2e0fef85 4192lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 4193{
41415862 4194 struct lpfc_sli *psli;
dea3101e 4195 struct lpfc_sli_ring *pring;
41415862 4196 uint16_t cfg_value;
dea3101e 4197 int i;
dea3101e 4198
41415862 4199 psli = &phba->sli;
dea3101e 4200
41415862
JW
4201 /* Reset HBA */
4202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4203 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 4204 phba->pport->port_state, psli->sli_flag);
dea3101e
JB
4205
4206 /* perform board reset */
4207 phba->fc_eventTag = 0;
4d9ab994 4208 phba->link_events = 0;
2e0fef85
JS
4209 phba->pport->fc_myDID = 0;
4210 phba->pport->fc_prevDID = 0;
dea3101e 4211
41415862
JW
4212 /* Turn off parity checking and serr during the physical reset */
4213 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4214 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4215 (cfg_value &
4216 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4217
3772a991
JS
4218 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4219
41415862
JW
4220 /* Now toggle INITFF bit in the Host Control Register */
4221 writel(HC_INITFF, phba->HCregaddr);
4222 mdelay(1);
4223 readl(phba->HCregaddr); /* flush */
4224 writel(0, phba->HCregaddr);
4225 readl(phba->HCregaddr); /* flush */
4226
4227 /* Restore PCI cmd register */
4228 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e
JB
4229
4230 /* Initialize relevant SLI info */
41415862 4231 for (i = 0; i < psli->num_rings; i++) {
895427bd 4232 pring = &psli->sli3_ring[i];
dea3101e 4233 pring->flag = 0;
7e56aa25
JS
4234 pring->sli.sli3.rspidx = 0;
4235 pring->sli.sli3.next_cmdidx = 0;
4236 pring->sli.sli3.local_getidx = 0;
4237 pring->sli.sli3.cmdidx = 0;
dea3101e
JB
4238 pring->missbufcnt = 0;
4239 }
dea3101e 4240
2e0fef85 4241 phba->link_state = LPFC_WARM_START;
41415862
JW
4242 return 0;
4243}
4244
e59058c4 4245/**
da0436e9
JS
4246 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4247 * @phba: Pointer to HBA context object.
4248 *
4249 * This function resets a SLI4 HBA. This function disables PCI layer parity
4250 * checking during resets the device. The caller is not required to hold
4251 * any locks.
4252 *
4253 * This function returns 0 always.
4254 **/
4255int
4256lpfc_sli4_brdreset(struct lpfc_hba *phba)
4257{
4258 struct lpfc_sli *psli = &phba->sli;
4259 uint16_t cfg_value;
0293635e 4260 int rc = 0;
da0436e9
JS
4261
4262 /* Reset HBA */
4263 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
0293635e
JS
4264 "0295 Reset HBA Data: x%x x%x x%x\n",
4265 phba->pport->port_state, psli->sli_flag,
4266 phba->hba_flag);
da0436e9
JS
4267
4268 /* perform board reset */
4269 phba->fc_eventTag = 0;
4d9ab994 4270 phba->link_events = 0;
da0436e9
JS
4271 phba->pport->fc_myDID = 0;
4272 phba->pport->fc_prevDID = 0;
4273
da0436e9
JS
4274 spin_lock_irq(&phba->hbalock);
4275 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4276 phba->fcf.fcf_flag = 0;
da0436e9
JS
4277 spin_unlock_irq(&phba->hbalock);
4278
0293635e
JS
4279 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4280 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4281 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4282 return rc;
4283 }
4284
da0436e9
JS
4285 /* Now physically reset the device */
4286 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4287 "0389 Performing PCI function reset!\n");
be858b65
JS
4288
4289 /* Turn off parity checking and serr during the physical reset */
4290 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4291 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4292 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4293
88318816 4294 /* Perform FCoE PCI function reset before freeing queue memory */
27b01b82 4295 rc = lpfc_pci_function_reset(phba);
88318816 4296 lpfc_sli4_queue_destroy(phba);
da0436e9 4297
be858b65
JS
4298 /* Restore PCI cmd register */
4299 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4300
27b01b82 4301 return rc;
da0436e9
JS
4302}
4303
4304/**
4305 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
4306 * @phba: Pointer to HBA context object.
4307 *
4308 * This function is called in the SLI initialization code path to
4309 * restart the HBA. The caller is not required to hold any lock.
4310 * This function writes MBX_RESTART mailbox command to the SLIM and
4311 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4312 * function to free any pending commands. The function enables
4313 * POST only during the first initialization. The function returns zero.
4314 * The function does not guarantee completion of MBX_RESTART mailbox
4315 * command before the return of this function.
4316 **/
da0436e9
JS
4317static int
4318lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
4319{
4320 MAILBOX_t *mb;
4321 struct lpfc_sli *psli;
41415862
JW
4322 volatile uint32_t word0;
4323 void __iomem *to_slim;
0d878419 4324 uint32_t hba_aer_enabled;
41415862 4325
2e0fef85 4326 spin_lock_irq(&phba->hbalock);
41415862 4327
0d878419
JS
4328 /* Take PCIe device Advanced Error Reporting (AER) state */
4329 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4330
41415862
JW
4331 psli = &phba->sli;
4332
4333 /* Restart HBA */
4334 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 4335 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 4336 phba->pport->port_state, psli->sli_flag);
41415862
JW
4337
4338 word0 = 0;
4339 mb = (MAILBOX_t *) &word0;
4340 mb->mbxCommand = MBX_RESTART;
4341 mb->mbxHc = 1;
4342
9290831f
JS
4343 lpfc_reset_barrier(phba);
4344
41415862
JW
4345 to_slim = phba->MBslimaddr;
4346 writel(*(uint32_t *) mb, to_slim);
4347 readl(to_slim); /* flush */
4348
4349 /* Only skip post after fc_ffinit is completed */
eaf15d5b 4350 if (phba->pport->port_state)
41415862 4351 word0 = 1; /* This is really setting up word1 */
eaf15d5b 4352 else
41415862 4353 word0 = 0; /* This is really setting up word1 */
65a29c16 4354 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
4355 writel(*(uint32_t *) mb, to_slim);
4356 readl(to_slim); /* flush */
dea3101e 4357
41415862 4358 lpfc_sli_brdreset(phba);
2e0fef85
JS
4359 phba->pport->stopped = 0;
4360 phba->link_state = LPFC_INIT_START;
da0436e9 4361 phba->hba_flag = 0;
2e0fef85 4362 spin_unlock_irq(&phba->hbalock);
41415862 4363
64ba8818
JS
4364 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4365 psli->stats_start = get_seconds();
4366
eaf15d5b
JS
4367 /* Give the INITFF and Post time to settle. */
4368 mdelay(100);
41415862 4369
0d878419
JS
4370 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4371 if (hba_aer_enabled)
4372 pci_disable_pcie_error_reporting(phba->pcidev);
4373
41415862 4374 lpfc_hba_down_post(phba);
dea3101e
JB
4375
4376 return 0;
4377}
4378
da0436e9
JS
4379/**
4380 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4381 * @phba: Pointer to HBA context object.
4382 *
4383 * This function is called in the SLI initialization code path to restart
4384 * a SLI4 HBA. The caller is not required to hold any lock.
4385 * At the end of the function, it calls lpfc_hba_down_post function to
4386 * free any pending commands.
4387 **/
4388static int
4389lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4390{
4391 struct lpfc_sli *psli = &phba->sli;
75baf696 4392 uint32_t hba_aer_enabled;
27b01b82 4393 int rc;
da0436e9
JS
4394
4395 /* Restart HBA */
4396 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4397 "0296 Restart HBA Data: x%x x%x\n",
4398 phba->pport->port_state, psli->sli_flag);
4399
75baf696
JS
4400 /* Take PCIe device Advanced Error Reporting (AER) state */
4401 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4402
27b01b82 4403 rc = lpfc_sli4_brdreset(phba);
da0436e9
JS
4404
4405 spin_lock_irq(&phba->hbalock);
4406 phba->pport->stopped = 0;
4407 phba->link_state = LPFC_INIT_START;
4408 phba->hba_flag = 0;
4409 spin_unlock_irq(&phba->hbalock);
4410
4411 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4412 psli->stats_start = get_seconds();
4413
75baf696
JS
4414 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4415 if (hba_aer_enabled)
4416 pci_disable_pcie_error_reporting(phba->pcidev);
4417
da0436e9
JS
4418 lpfc_hba_down_post(phba);
4419
27b01b82 4420 return rc;
da0436e9
JS
4421}
4422
4423/**
4424 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4425 * @phba: Pointer to HBA context object.
4426 *
4427 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4428 * API jump table function pointer from the lpfc_hba struct.
4429**/
4430int
4431lpfc_sli_brdrestart(struct lpfc_hba *phba)
4432{
4433 return phba->lpfc_sli_brdrestart(phba);
4434}
4435
e59058c4 4436/**
3621a710 4437 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4438 * @phba: Pointer to HBA context object.
4439 *
4440 * This function is called after a HBA restart to wait for successful
4441 * restart of the HBA. Successful restart of the HBA is indicated by
4442 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4443 * iteration, the function will restart the HBA again. The function returns
4444 * zero if HBA successfully restarted else returns negative error code.
4445 **/
dea3101e
JB
4446static int
4447lpfc_sli_chipset_init(struct lpfc_hba *phba)
4448{
4449 uint32_t status, i = 0;
4450
4451 /* Read the HBA Host Status Register */
9940b97b
JS
4452 if (lpfc_readl(phba->HSregaddr, &status))
4453 return -EIO;
dea3101e
JB
4454
4455 /* Check status register to see what current state is */
4456 i = 0;
4457 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4458
dcf2a4e0
JS
4459 /* Check every 10ms for 10 retries, then every 100ms for 90
4460 * retries, then every 1 sec for 50 retires for a total of
4461 * ~60 seconds before reset the board again and check every
4462 * 1 sec for 50 retries. The up to 60 seconds before the
4463 * board ready is required by the Falcon FIPS zeroization
4464 * complete, and any reset the board in between shall cause
4465 * restart of zeroization, further delay the board ready.
dea3101e 4466 */
dcf2a4e0 4467 if (i++ >= 200) {
dea3101e
JB
4468 /* Adapter failed to init, timeout, status reg
4469 <status> */
ed957684 4470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4471 "0436 Adapter failed to init, "
09372820
JS
4472 "timeout, status reg x%x, "
4473 "FW Data: A8 x%x AC x%x\n", status,
4474 readl(phba->MBslimaddr + 0xa8),
4475 readl(phba->MBslimaddr + 0xac));
2e0fef85 4476 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4477 return -ETIMEDOUT;
4478 }
4479
4480 /* Check to see if any errors occurred during init */
4481 if (status & HS_FFERM) {
4482 /* ERROR: During chipset initialization */
4483 /* Adapter failed to init, chipset, status reg
4484 <status> */
ed957684 4485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4486 "0437 Adapter failed to init, "
09372820
JS
4487 "chipset, status reg x%x, "
4488 "FW Data: A8 x%x AC x%x\n", status,
4489 readl(phba->MBslimaddr + 0xa8),
4490 readl(phba->MBslimaddr + 0xac));
2e0fef85 4491 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4492 return -EIO;
4493 }
4494
dcf2a4e0 4495 if (i <= 10)
dea3101e 4496 msleep(10);
dcf2a4e0
JS
4497 else if (i <= 100)
4498 msleep(100);
4499 else
4500 msleep(1000);
dea3101e 4501
dcf2a4e0
JS
4502 if (i == 150) {
4503 /* Do post */
92d7f7b0 4504 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4505 lpfc_sli_brdrestart(phba);
dea3101e
JB
4506 }
4507 /* Read the HBA Host Status Register */
9940b97b
JS
4508 if (lpfc_readl(phba->HSregaddr, &status))
4509 return -EIO;
dea3101e
JB
4510 }
4511
4512 /* Check to see if any errors occurred during init */
4513 if (status & HS_FFERM) {
4514 /* ERROR: During chipset initialization */
4515 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4517 "0438 Adapter failed to init, chipset, "
09372820
JS
4518 "status reg x%x, "
4519 "FW Data: A8 x%x AC x%x\n", status,
4520 readl(phba->MBslimaddr + 0xa8),
4521 readl(phba->MBslimaddr + 0xac));
2e0fef85 4522 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4523 return -EIO;
4524 }
4525
4526 /* Clear all interrupt enable conditions */
4527 writel(0, phba->HCregaddr);
4528 readl(phba->HCregaddr); /* flush */
4529
4530 /* setup host attn register */
4531 writel(0xffffffff, phba->HAregaddr);
4532 readl(phba->HAregaddr); /* flush */
4533 return 0;
4534}
4535
e59058c4 4536/**
3621a710 4537 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4538 *
4539 * This function calculates and returns the number of HBQs required to be
4540 * configured.
4541 **/
78b2d852 4542int
ed957684
JS
4543lpfc_sli_hbq_count(void)
4544{
92d7f7b0 4545 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4546}
4547
e59058c4 4548/**
3621a710 4549 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4550 *
4551 * This function adds the number of hbq entries in every HBQ to get
4552 * the total number of hbq entries required for the HBA and returns
4553 * the total count.
4554 **/
ed957684
JS
4555static int
4556lpfc_sli_hbq_entry_count(void)
4557{
4558 int hbq_count = lpfc_sli_hbq_count();
4559 int count = 0;
4560 int i;
4561
4562 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4563 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4564 return count;
4565}
4566
e59058c4 4567/**
3621a710 4568 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4569 *
4570 * This function calculates amount of memory required for all hbq entries
4571 * to be configured and returns the total memory required.
4572 **/
dea3101e 4573int
ed957684
JS
4574lpfc_sli_hbq_size(void)
4575{
4576 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4577}
4578
e59058c4 4579/**
3621a710 4580 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4581 * @phba: Pointer to HBA context object.
4582 *
4583 * This function is called during the SLI initialization to configure
4584 * all the HBQs and post buffers to the HBQ. The caller is not
4585 * required to hold any locks. This function will return zero if successful
4586 * else it will return negative error code.
4587 **/
ed957684
JS
4588static int
4589lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4590{
4591 int hbq_count = lpfc_sli_hbq_count();
4592 LPFC_MBOXQ_t *pmb;
4593 MAILBOX_t *pmbox;
4594 uint32_t hbqno;
4595 uint32_t hbq_entry_index;
ed957684 4596
92d7f7b0
JS
4597 /* Get a Mailbox buffer to setup mailbox
4598 * commands for HBA initialization
4599 */
ed957684
JS
4600 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4601
4602 if (!pmb)
4603 return -ENOMEM;
4604
04c68496 4605 pmbox = &pmb->u.mb;
ed957684
JS
4606
4607 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4608 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4609 phba->hbq_in_use = 1;
ed957684
JS
4610
4611 hbq_entry_index = 0;
4612 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4613 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4614 phba->hbqs[hbqno].hbqPutIdx = 0;
4615 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4616 phba->hbqs[hbqno].entry_count =
92d7f7b0 4617 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4618 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4619 hbq_entry_index, pmb);
ed957684
JS
4620 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4621
4622 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4623 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4624 mbxStatus <status>, ring <num> */
4625
4626 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4627 LOG_SLI | LOG_VPORT,
e8b62011 4628 "1805 Adapter failed to init. "
ed957684 4629 "Data: x%x x%x x%x\n",
e8b62011 4630 pmbox->mbxCommand,
ed957684
JS
4631 pmbox->mbxStatus, hbqno);
4632
4633 phba->link_state = LPFC_HBA_ERROR;
4634 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4635 return -ENXIO;
ed957684
JS
4636 }
4637 }
4638 phba->hbq_count = hbq_count;
4639
ed957684
JS
4640 mempool_free(pmb, phba->mbox_mem_pool);
4641
92d7f7b0 4642 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4643 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4644 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4645 return 0;
4646}
4647
4f774513
JS
4648/**
4649 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4650 * @phba: Pointer to HBA context object.
4651 *
4652 * This function is called during the SLI initialization to configure
4653 * all the HBQs and post buffers to the HBQ. The caller is not
4654 * required to hold any locks. This function will return zero if successful
4655 * else it will return negative error code.
4656 **/
4657static int
4658lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4659{
4660 phba->hbq_in_use = 1;
895427bd
JS
4661 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4662 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4f774513 4663 phba->hbq_count = 1;
895427bd 4664 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4f774513 4665 /* Initially populate or replenish the HBQs */
4f774513
JS
4666 return 0;
4667}
4668
e59058c4 4669/**
3621a710 4670 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4671 * @phba: Pointer to HBA context object.
4672 * @sli_mode: sli mode - 2/3
4673 *
4674 * This function is called by the sli intialization code path
4675 * to issue config_port mailbox command. This function restarts the
4676 * HBA firmware and issues a config_port mailbox command to configure
4677 * the SLI interface in the sli mode specified by sli_mode
4678 * variable. The caller is not required to hold any locks.
4679 * The function returns 0 if successful, else returns negative error
4680 * code.
4681 **/
9399627f
JS
4682int
4683lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e
JB
4684{
4685 LPFC_MBOXQ_t *pmb;
4686 uint32_t resetcount = 0, rc = 0, done = 0;
4687
4688 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4689 if (!pmb) {
2e0fef85 4690 phba->link_state = LPFC_HBA_ERROR;
dea3101e
JB
4691 return -ENOMEM;
4692 }
4693
ed957684 4694 phba->sli_rev = sli_mode;
dea3101e 4695 while (resetcount < 2 && !done) {
2e0fef85 4696 spin_lock_irq(&phba->hbalock);
1c067a42 4697 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4698 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4699 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4700 lpfc_sli_brdrestart(phba);
dea3101e
JB
4701 rc = lpfc_sli_chipset_init(phba);
4702 if (rc)
4703 break;
4704
2e0fef85 4705 spin_lock_irq(&phba->hbalock);
1c067a42 4706 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4707 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4708 resetcount++;
4709
ed957684
JS
4710 /* Call pre CONFIG_PORT mailbox command initialization. A
4711 * value of 0 means the call was successful. Any other
4712 * nonzero value is a failure, but if ERESTART is returned,
4713 * the driver may reset the HBA and try again.
4714 */
dea3101e
JB
4715 rc = lpfc_config_port_prep(phba);
4716 if (rc == -ERESTART) {
ed957684 4717 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4718 continue;
34b02dcd 4719 } else if (rc)
dea3101e 4720 break;
6d368e53 4721
2e0fef85 4722 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e
JB
4723 lpfc_config_port(phba, pmb);
4724 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4725 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4726 LPFC_SLI3_HBQ_ENABLED |
4727 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4728 LPFC_SLI3_BG_ENABLED |
4729 LPFC_SLI3_DSS_ENABLED);
ed957684 4730 if (rc != MBX_SUCCESS) {
dea3101e 4731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4732 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4733 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4734 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4735 spin_lock_irq(&phba->hbalock);
04c68496 4736 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4737 spin_unlock_irq(&phba->hbalock);
4738 rc = -ENXIO;
04c68496
JS
4739 } else {
4740 /* Allow asynchronous mailbox command to go through */
4741 spin_lock_irq(&phba->hbalock);
4742 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4743 spin_unlock_irq(&phba->hbalock);
ed957684 4744 done = 1;
cb69f7de
JS
4745
4746 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4747 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4748 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4749 "3110 Port did not grant ASABT\n");
04c68496 4750 }
dea3101e 4751 }
ed957684
JS
4752 if (!done) {
4753 rc = -EINVAL;
4754 goto do_prep_failed;
4755 }
04c68496
JS
4756 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4757 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4758 rc = -ENXIO;
4759 goto do_prep_failed;
4760 }
04c68496 4761 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4762 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4763 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4764 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4765 phba->max_vpi : phba->max_vports;
4766
34b02dcd
JS
4767 } else
4768 phba->max_vpi = 0;
bc73905a
JS
4769 phba->fips_level = 0;
4770 phba->fips_spec_rev = 0;
4771 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4772 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4773 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4774 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4775 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4776 "2850 Security Crypto Active. FIPS x%d "
4777 "(Spec Rev: x%d)",
4778 phba->fips_level, phba->fips_spec_rev);
4779 }
4780 if (pmb->u.mb.un.varCfgPort.sec_err) {
4781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4782 "2856 Config Port Security Crypto "
4783 "Error: x%x ",
4784 pmb->u.mb.un.varCfgPort.sec_err);
4785 }
04c68496 4786 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4787 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4788 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4789 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4790
4791 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4792 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4793
4794 if (phba->cfg_enable_bg) {
04c68496 4795 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4796 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4797 else
4798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4799 "0443 Adapter did not grant "
4800 "BlockGuard\n");
4801 }
34b02dcd 4802 } else {
8f34f4ce 4803 phba->hbq_get = NULL;
34b02dcd 4804 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4805 phba->max_vpi = 0;
ed957684 4806 }
92d7f7b0 4807do_prep_failed:
ed957684
JS
4808 mempool_free(pmb, phba->mbox_mem_pool);
4809 return rc;
4810}
4811
e59058c4
JS
4812
4813/**
3621a710 4814 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4815 * @phba: Pointer to HBA context object.
4816 *
4817 * This function is the main SLI intialization function. This function
4818 * is called by the HBA intialization code, HBA reset code and HBA
4819 * error attention handler code. Caller is not required to hold any
4820 * locks. This function issues config_port mailbox command to configure
4821 * the SLI, setup iocb rings and HBQ rings. In the end the function
4822 * calls the config_port_post function to issue init_link mailbox
4823 * command and to start the discovery. The function will return zero
4824 * if successful, else it will return negative error code.
4825 **/
ed957684
JS
4826int
4827lpfc_sli_hba_setup(struct lpfc_hba *phba)
4828{
4829 uint32_t rc;
6d368e53
JS
4830 int mode = 3, i;
4831 int longs;
ed957684 4832
12247e81 4833 switch (phba->cfg_sli_mode) {
ed957684 4834 case 2:
78b2d852 4835 if (phba->cfg_enable_npiv) {
92d7f7b0 4836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81 4837 "1824 NPIV enabled: Override sli_mode "
92d7f7b0 4838 "parameter (%d) to auto (0).\n",
12247e81 4839 phba->cfg_sli_mode);
92d7f7b0
JS
4840 break;
4841 }
ed957684
JS
4842 mode = 2;
4843 break;
4844 case 0:
4845 case 3:
4846 break;
4847 default:
92d7f7b0 4848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
12247e81
JS
4849 "1819 Unrecognized sli_mode parameter: %d.\n",
4850 phba->cfg_sli_mode);
ed957684
JS
4851
4852 break;
4853 }
b5c53958 4854 phba->fcp_embed_io = 0; /* SLI4 FC support only */
ed957684 4855
9399627f
JS
4856 rc = lpfc_sli_config_port(phba, mode);
4857
12247e81 4858 if (rc && phba->cfg_sli_mode == 3)
92d7f7b0 4859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4860 "1820 Unable to select SLI-3. "
4861 "Not supported by adapter.\n");
ed957684 4862 if (rc && mode != 2)
9399627f 4863 rc = lpfc_sli_config_port(phba, 2);
4597663f
JS
4864 else if (rc && mode == 2)
4865 rc = lpfc_sli_config_port(phba, 3);
ed957684 4866 if (rc)
dea3101e
JB
4867 goto lpfc_sli_hba_setup_error;
4868
0d878419
JS
4869 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4870 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4871 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4872 if (!rc) {
4873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4874 "2709 This device supports "
4875 "Advanced Error Reporting (AER)\n");
4876 spin_lock_irq(&phba->hbalock);
4877 phba->hba_flag |= HBA_AER_ENABLED;
4878 spin_unlock_irq(&phba->hbalock);
4879 } else {
4880 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4881 "2708 This device does not support "
b069d7eb
JS
4882 "Advanced Error Reporting (AER): %d\n",
4883 rc);
0d878419
JS
4884 phba->cfg_aer_support = 0;
4885 }
4886 }
4887
ed957684
JS
4888 if (phba->sli_rev == 3) {
4889 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4890 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4891 } else {
4892 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4893 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4894 phba->sli3_options = 0;
ed957684
JS
4895 }
4896
4897 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4898 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4899 phba->sli_rev, phba->max_vpi);
ed957684 4900 rc = lpfc_sli_ring_map(phba);
dea3101e
JB
4901
4902 if (rc)
4903 goto lpfc_sli_hba_setup_error;
4904
6d368e53
JS
4905 /* Initialize VPIs. */
4906 if (phba->sli_rev == LPFC_SLI_REV3) {
4907 /*
4908 * The VPI bitmask and physical ID array are allocated
4909 * and initialized once only - at driver load. A port
4910 * reset doesn't need to reinitialize this memory.
4911 */
4912 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4913 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4914 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4915 GFP_KERNEL);
4916 if (!phba->vpi_bmask) {
4917 rc = -ENOMEM;
4918 goto lpfc_sli_hba_setup_error;
4919 }
4920
4921 phba->vpi_ids = kzalloc(
4922 (phba->max_vpi+1) * sizeof(uint16_t),
4923 GFP_KERNEL);
4924 if (!phba->vpi_ids) {
4925 kfree(phba->vpi_bmask);
4926 rc = -ENOMEM;
4927 goto lpfc_sli_hba_setup_error;
4928 }
4929 for (i = 0; i < phba->max_vpi; i++)
4930 phba->vpi_ids[i] = i;
4931 }
4932 }
4933
9399627f 4934 /* Init HBQs */
ed957684
JS
4935 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4936 rc = lpfc_sli_hbq_setup(phba);
4937 if (rc)
4938 goto lpfc_sli_hba_setup_error;
4939 }
04c68496 4940 spin_lock_irq(&phba->hbalock);
dea3101e 4941 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4942 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
4943
4944 rc = lpfc_config_port_post(phba);
4945 if (rc)
4946 goto lpfc_sli_hba_setup_error;
4947
ed957684
JS
4948 return rc;
4949
92d7f7b0 4950lpfc_sli_hba_setup_error:
2e0fef85 4951 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4953 "0445 Firmware initialization failed\n");
dea3101e
JB
4954 return rc;
4955}
4956
e59058c4 4957/**
da0436e9
JS
4958 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4959 * @phba: Pointer to HBA context object.
4960 * @mboxq: mailbox pointer.
4961 * This function issue a dump mailbox command to read config region
4962 * 23 and parse the records in the region and populate driver
4963 * data structure.
e59058c4 4964 **/
da0436e9 4965static int
ff78d8f9 4966lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
dea3101e 4967{
ff78d8f9 4968 LPFC_MBOXQ_t *mboxq;
da0436e9
JS
4969 struct lpfc_dmabuf *mp;
4970 struct lpfc_mqe *mqe;
4971 uint32_t data_length;
4972 int rc;
dea3101e 4973
da0436e9
JS
4974 /* Program the default value of vlan_id and fc_map */
4975 phba->valid_vlan = 0;
4976 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4977 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4978 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4979
ff78d8f9
JS
4980 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4981 if (!mboxq)
da0436e9
JS
4982 return -ENOMEM;
4983
ff78d8f9
JS
4984 mqe = &mboxq->u.mqe;
4985 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4986 rc = -ENOMEM;
4987 goto out_free_mboxq;
4988 }
4989
da0436e9
JS
4990 mp = (struct lpfc_dmabuf *) mboxq->context1;
4991 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4992
4993 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4994 "(%d):2571 Mailbox cmd x%x Status x%x "
4995 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4996 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4997 "CQ: x%x x%x x%x x%x\n",
4998 mboxq->vport ? mboxq->vport->vpi : 0,
4999 bf_get(lpfc_mqe_command, mqe),
5000 bf_get(lpfc_mqe_status, mqe),
5001 mqe->un.mb_words[0], mqe->un.mb_words[1],
5002 mqe->un.mb_words[2], mqe->un.mb_words[3],
5003 mqe->un.mb_words[4], mqe->un.mb_words[5],
5004 mqe->un.mb_words[6], mqe->un.mb_words[7],
5005 mqe->un.mb_words[8], mqe->un.mb_words[9],
5006 mqe->un.mb_words[10], mqe->un.mb_words[11],
5007 mqe->un.mb_words[12], mqe->un.mb_words[13],
5008 mqe->un.mb_words[14], mqe->un.mb_words[15],
5009 mqe->un.mb_words[16], mqe->un.mb_words[50],
5010 mboxq->mcqe.word0,
5011 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5012 mboxq->mcqe.trailer);
5013
5014 if (rc) {
5015 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5016 kfree(mp);
ff78d8f9
JS
5017 rc = -EIO;
5018 goto out_free_mboxq;
da0436e9
JS
5019 }
5020 data_length = mqe->un.mb_words[5];
a0c87cbd 5021 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
5022 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5023 kfree(mp);
ff78d8f9
JS
5024 rc = -EIO;
5025 goto out_free_mboxq;
d11e31dd 5026 }
dea3101e 5027
da0436e9
JS
5028 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5029 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5030 kfree(mp);
ff78d8f9
JS
5031 rc = 0;
5032
5033out_free_mboxq:
5034 mempool_free(mboxq, phba->mbox_mem_pool);
5035 return rc;
da0436e9 5036}
e59058c4
JS
5037
5038/**
da0436e9
JS
5039 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5040 * @phba: pointer to lpfc hba data structure.
5041 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5042 * @vpd: pointer to the memory to hold resulting port vpd data.
5043 * @vpd_size: On input, the number of bytes allocated to @vpd.
5044 * On output, the number of data bytes in @vpd.
e59058c4 5045 *
da0436e9
JS
5046 * This routine executes a READ_REV SLI4 mailbox command. In
5047 * addition, this routine gets the port vpd data.
5048 *
5049 * Return codes
af901ca1 5050 * 0 - successful
d439d286 5051 * -ENOMEM - could not allocated memory.
e59058c4 5052 **/
da0436e9
JS
5053static int
5054lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5055 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 5056{
da0436e9
JS
5057 int rc = 0;
5058 uint32_t dma_size;
5059 struct lpfc_dmabuf *dmabuf;
5060 struct lpfc_mqe *mqe;
dea3101e 5061
da0436e9
JS
5062 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5063 if (!dmabuf)
5064 return -ENOMEM;
5065
5066 /*
5067 * Get a DMA buffer for the vpd data resulting from the READ_REV
5068 * mailbox command.
a257bf90 5069 */
da0436e9 5070 dma_size = *vpd_size;
1aee383d
JP
5071 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5072 &dmabuf->phys, GFP_KERNEL);
da0436e9
JS
5073 if (!dmabuf->virt) {
5074 kfree(dmabuf);
5075 return -ENOMEM;
a257bf90
JS
5076 }
5077
da0436e9
JS
5078 /*
5079 * The SLI4 implementation of READ_REV conflicts at word1,
5080 * bits 31:16 and SLI4 adds vpd functionality not present
5081 * in SLI3. This code corrects the conflicts.
1dcb58e5 5082 */
da0436e9
JS
5083 lpfc_read_rev(phba, mboxq);
5084 mqe = &mboxq->u.mqe;
5085 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5086 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5087 mqe->un.read_rev.word1 &= 0x0000FFFF;
5088 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5089 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5090
5091 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5092 if (rc) {
5093 dma_free_coherent(&phba->pcidev->dev, dma_size,
5094 dmabuf->virt, dmabuf->phys);
def9c7a9 5095 kfree(dmabuf);
da0436e9
JS
5096 return -EIO;
5097 }
1dcb58e5 5098
da0436e9
JS
5099 /*
5100 * The available vpd length cannot be bigger than the
5101 * DMA buffer passed to the port. Catch the less than
5102 * case and update the caller's size.
5103 */
5104 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5105 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 5106
d7c47992
JS
5107 memcpy(vpd, dmabuf->virt, *vpd_size);
5108
da0436e9
JS
5109 dma_free_coherent(&phba->pcidev->dev, dma_size,
5110 dmabuf->virt, dmabuf->phys);
5111 kfree(dmabuf);
5112 return 0;
dea3101e
JB
5113}
5114
cd1c8301
JS
5115/**
5116 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5117 * @phba: pointer to lpfc hba data structure.
5118 *
5119 * This routine retrieves SLI4 device physical port name this PCI function
5120 * is attached to.
5121 *
5122 * Return codes
4907cb7b 5123 * 0 - successful
cd1c8301
JS
5124 * otherwise - failed to retrieve physical port name
5125 **/
5126static int
5127lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5128{
5129 LPFC_MBOXQ_t *mboxq;
cd1c8301
JS
5130 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5131 struct lpfc_controller_attribute *cntl_attr;
5132 struct lpfc_mbx_get_port_name *get_port_name;
5133 void *virtaddr = NULL;
5134 uint32_t alloclen, reqlen;
5135 uint32_t shdr_status, shdr_add_status;
5136 union lpfc_sli4_cfg_shdr *shdr;
5137 char cport_name = 0;
5138 int rc;
5139
5140 /* We assume nothing at this point */
5141 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5142 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5143
5144 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5145 if (!mboxq)
5146 return -ENOMEM;
cd1c8301 5147 /* obtain link type and link number via READ_CONFIG */
ff78d8f9
JS
5148 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5149 lpfc_sli4_read_config(phba);
5150 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5151 goto retrieve_ppname;
cd1c8301
JS
5152
5153 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5154 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5155 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5156 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5157 LPFC_SLI4_MBX_NEMBED);
5158 if (alloclen < reqlen) {
5159 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5160 "3084 Allocated DMA memory size (%d) is "
5161 "less than the requested DMA memory size "
5162 "(%d)\n", alloclen, reqlen);
5163 rc = -ENOMEM;
5164 goto out_free_mboxq;
5165 }
5166 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5167 virtaddr = mboxq->sge_array->addr[0];
5168 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5169 shdr = &mbx_cntl_attr->cfg_shdr;
5170 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5171 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5172 if (shdr_status || shdr_add_status || rc) {
5173 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5174 "3085 Mailbox x%x (x%x/x%x) failed, "
5175 "rc:x%x, status:x%x, add_status:x%x\n",
5176 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5177 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5178 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5179 rc, shdr_status, shdr_add_status);
5180 rc = -ENXIO;
5181 goto out_free_mboxq;
5182 }
5183 cntl_attr = &mbx_cntl_attr->cntl_attr;
5184 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5185 phba->sli4_hba.lnk_info.lnk_tp =
5186 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5187 phba->sli4_hba.lnk_info.lnk_no =
5188 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5189 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5190 "3086 lnk_type:%d, lnk_numb:%d\n",
5191 phba->sli4_hba.lnk_info.lnk_tp,
5192 phba->sli4_hba.lnk_info.lnk_no);
5193
5194retrieve_ppname:
5195 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5196 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5197 sizeof(struct lpfc_mbx_get_port_name) -
5198 sizeof(struct lpfc_sli4_cfg_mhdr),
5199 LPFC_SLI4_MBX_EMBED);
5200 get_port_name = &mboxq->u.mqe.un.get_port_name;
5201 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5202 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5203 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5204 phba->sli4_hba.lnk_info.lnk_tp);
5205 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5206 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5207 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5208 if (shdr_status || shdr_add_status || rc) {
5209 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5210 "3087 Mailbox x%x (x%x/x%x) failed: "
5211 "rc:x%x, status:x%x, add_status:x%x\n",
5212 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5213 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5214 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5215 rc, shdr_status, shdr_add_status);
5216 rc = -ENXIO;
5217 goto out_free_mboxq;
5218 }
5219 switch (phba->sli4_hba.lnk_info.lnk_no) {
5220 case LPFC_LINK_NUMBER_0:
5221 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5222 &get_port_name->u.response);
5223 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5224 break;
5225 case LPFC_LINK_NUMBER_1:
5226 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5227 &get_port_name->u.response);
5228 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5229 break;
5230 case LPFC_LINK_NUMBER_2:
5231 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5232 &get_port_name->u.response);
5233 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5234 break;
5235 case LPFC_LINK_NUMBER_3:
5236 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5237 &get_port_name->u.response);
5238 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5239 break;
5240 default:
5241 break;
5242 }
5243
5244 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5245 phba->Port[0] = cport_name;
5246 phba->Port[1] = '\0';
5247 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5248 "3091 SLI get port name: %s\n", phba->Port);
5249 }
5250
5251out_free_mboxq:
5252 if (rc != MBX_TIMEOUT) {
5253 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5254 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5255 else
5256 mempool_free(mboxq, phba->mbox_mem_pool);
5257 }
5258 return rc;
5259}
5260
e59058c4 5261/**
da0436e9
JS
5262 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5263 * @phba: pointer to lpfc hba data structure.
e59058c4 5264 *
da0436e9
JS
5265 * This routine is called to explicitly arm the SLI4 device's completion and
5266 * event queues
5267 **/
5268static void
5269lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5270{
895427bd 5271 int qidx;
da0436e9
JS
5272
5273 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5274 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
895427bd
JS
5275 if (phba->sli4_hba.nvmels_cq)
5276 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5277 LPFC_QUEUE_REARM);
5278
5279 if (phba->sli4_hba.fcp_cq)
5280 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5281 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5282 LPFC_QUEUE_REARM);
5283
5284 if (phba->sli4_hba.nvme_cq)
5285 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5286 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5287 LPFC_QUEUE_REARM);
1ba981fd 5288
f38fa0bb 5289 if (phba->cfg_fof)
1ba981fd
JS
5290 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5291
895427bd
JS
5292 if (phba->sli4_hba.hba_eq)
5293 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5294 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5295 LPFC_QUEUE_REARM);
1ba981fd 5296
2d7dbc4c
JS
5297 if (phba->nvmet_support) {
5298 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5299 lpfc_sli4_cq_release(
5300 phba->sli4_hba.nvmet_cqset[qidx],
5301 LPFC_QUEUE_REARM);
5302 }
5303 }
5304
1ba981fd
JS
5305 if (phba->cfg_fof)
5306 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
da0436e9
JS
5307}
5308
6d368e53
JS
5309/**
5310 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5311 * @phba: Pointer to HBA context object.
5312 * @type: The resource extent type.
b76f2dc9
JS
5313 * @extnt_count: buffer to hold port available extent count.
5314 * @extnt_size: buffer to hold element count per extent.
6d368e53 5315 *
b76f2dc9
JS
5316 * This function calls the port and retrievs the number of available
5317 * extents and their size for a particular extent type.
5318 *
5319 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 5320 **/
b76f2dc9 5321int
6d368e53
JS
5322lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5323 uint16_t *extnt_count, uint16_t *extnt_size)
5324{
5325 int rc = 0;
5326 uint32_t length;
5327 uint32_t mbox_tmo;
5328 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5329 LPFC_MBOXQ_t *mbox;
5330
5331 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5332 if (!mbox)
5333 return -ENOMEM;
5334
5335 /* Find out how many extents are available for this resource type */
5336 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5337 sizeof(struct lpfc_sli4_cfg_mhdr));
5338 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5339 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5340 length, LPFC_SLI4_MBX_EMBED);
5341
5342 /* Send an extents count of 0 - the GET doesn't use it. */
5343 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5344 LPFC_SLI4_MBX_EMBED);
5345 if (unlikely(rc)) {
5346 rc = -EIO;
5347 goto err_exit;
5348 }
5349
5350 if (!phba->sli4_hba.intr_enable)
5351 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5352 else {
a183a15f 5353 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5354 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5355 }
5356 if (unlikely(rc)) {
5357 rc = -EIO;
5358 goto err_exit;
5359 }
5360
5361 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5362 if (bf_get(lpfc_mbox_hdr_status,
5363 &rsrc_info->header.cfg_shdr.response)) {
5364 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5365 "2930 Failed to get resource extents "
5366 "Status 0x%x Add'l Status 0x%x\n",
5367 bf_get(lpfc_mbox_hdr_status,
5368 &rsrc_info->header.cfg_shdr.response),
5369 bf_get(lpfc_mbox_hdr_add_status,
5370 &rsrc_info->header.cfg_shdr.response));
5371 rc = -EIO;
5372 goto err_exit;
5373 }
5374
5375 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5376 &rsrc_info->u.rsp);
5377 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5378 &rsrc_info->u.rsp);
8a9d2e80
JS
5379
5380 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5381 "3162 Retrieved extents type-%d from port: count:%d, "
5382 "size:%d\n", type, *extnt_count, *extnt_size);
5383
5384err_exit:
6d368e53
JS
5385 mempool_free(mbox, phba->mbox_mem_pool);
5386 return rc;
5387}
5388
5389/**
5390 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5391 * @phba: Pointer to HBA context object.
5392 * @type: The extent type to check.
5393 *
5394 * This function reads the current available extents from the port and checks
5395 * if the extent count or extent size has changed since the last access.
5396 * Callers use this routine post port reset to understand if there is a
5397 * extent reprovisioning requirement.
5398 *
5399 * Returns:
5400 * -Error: error indicates problem.
5401 * 1: Extent count or size has changed.
5402 * 0: No changes.
5403 **/
5404static int
5405lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5406{
5407 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5408 uint16_t size_diff, rsrc_ext_size;
5409 int rc = 0;
5410 struct lpfc_rsrc_blks *rsrc_entry;
5411 struct list_head *rsrc_blk_list = NULL;
5412
5413 size_diff = 0;
5414 curr_ext_cnt = 0;
5415 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5416 &rsrc_ext_cnt,
5417 &rsrc_ext_size);
5418 if (unlikely(rc))
5419 return -EIO;
5420
5421 switch (type) {
5422 case LPFC_RSC_TYPE_FCOE_RPI:
5423 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5424 break;
5425 case LPFC_RSC_TYPE_FCOE_VPI:
5426 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5427 break;
5428 case LPFC_RSC_TYPE_FCOE_XRI:
5429 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5430 break;
5431 case LPFC_RSC_TYPE_FCOE_VFI:
5432 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5433 break;
5434 default:
5435 break;
5436 }
5437
5438 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5439 curr_ext_cnt++;
5440 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5441 size_diff++;
5442 }
5443
5444 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5445 rc = 1;
5446
5447 return rc;
5448}
5449
5450/**
5451 * lpfc_sli4_cfg_post_extnts -
5452 * @phba: Pointer to HBA context object.
5453 * @extnt_cnt - number of available extents.
5454 * @type - the extent type (rpi, xri, vfi, vpi).
5455 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5456 * @mbox - pointer to the caller's allocated mailbox structure.
5457 *
5458 * This function executes the extents allocation request. It also
5459 * takes care of the amount of memory needed to allocate or get the
5460 * allocated extents. It is the caller's responsibility to evaluate
5461 * the response.
5462 *
5463 * Returns:
5464 * -Error: Error value describes the condition found.
5465 * 0: if successful
5466 **/
5467static int
8a9d2e80 5468lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
6d368e53
JS
5469 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5470{
5471 int rc = 0;
5472 uint32_t req_len;
5473 uint32_t emb_len;
5474 uint32_t alloc_len, mbox_tmo;
5475
5476 /* Calculate the total requested length of the dma memory */
8a9d2e80 5477 req_len = extnt_cnt * sizeof(uint16_t);
6d368e53
JS
5478
5479 /*
5480 * Calculate the size of an embedded mailbox. The uint32_t
5481 * accounts for extents-specific word.
5482 */
5483 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5484 sizeof(uint32_t);
5485
5486 /*
5487 * Presume the allocation and response will fit into an embedded
5488 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5489 */
5490 *emb = LPFC_SLI4_MBX_EMBED;
5491 if (req_len > emb_len) {
8a9d2e80 5492 req_len = extnt_cnt * sizeof(uint16_t) +
6d368e53
JS
5493 sizeof(union lpfc_sli4_cfg_shdr) +
5494 sizeof(uint32_t);
5495 *emb = LPFC_SLI4_MBX_NEMBED;
5496 }
5497
5498 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5499 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5500 req_len, *emb);
5501 if (alloc_len < req_len) {
5502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5503 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5504 "less than the requested DMA memory "
5505 "size (x%x)\n", alloc_len, req_len);
5506 return -ENOMEM;
5507 }
8a9d2e80 5508 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
6d368e53
JS
5509 if (unlikely(rc))
5510 return -EIO;
5511
5512 if (!phba->sli4_hba.intr_enable)
5513 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5514 else {
a183a15f 5515 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5516 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5517 }
5518
5519 if (unlikely(rc))
5520 rc = -EIO;
5521 return rc;
5522}
5523
5524/**
5525 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5526 * @phba: Pointer to HBA context object.
5527 * @type: The resource extent type to allocate.
5528 *
5529 * This function allocates the number of elements for the specified
5530 * resource type.
5531 **/
5532static int
5533lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5534{
5535 bool emb = false;
5536 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5537 uint16_t rsrc_id, rsrc_start, j, k;
5538 uint16_t *ids;
5539 int i, rc;
5540 unsigned long longs;
5541 unsigned long *bmask;
5542 struct lpfc_rsrc_blks *rsrc_blks;
5543 LPFC_MBOXQ_t *mbox;
5544 uint32_t length;
5545 struct lpfc_id_range *id_array = NULL;
5546 void *virtaddr = NULL;
5547 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5548 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5549 struct list_head *ext_blk_list;
5550
5551 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5552 &rsrc_cnt,
5553 &rsrc_size);
5554 if (unlikely(rc))
5555 return -EIO;
5556
5557 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5558 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5559 "3009 No available Resource Extents "
5560 "for resource type 0x%x: Count: 0x%x, "
5561 "Size 0x%x\n", type, rsrc_cnt,
5562 rsrc_size);
5563 return -ENOMEM;
5564 }
5565
8a9d2e80
JS
5566 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5567 "2903 Post resource extents type-0x%x: "
5568 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
6d368e53
JS
5569
5570 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5571 if (!mbox)
5572 return -ENOMEM;
5573
8a9d2e80 5574 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
6d368e53
JS
5575 if (unlikely(rc)) {
5576 rc = -EIO;
5577 goto err_exit;
5578 }
5579
5580 /*
5581 * Figure out where the response is located. Then get local pointers
5582 * to the response data. The port does not guarantee to respond to
5583 * all extents counts request so update the local variable with the
5584 * allocated count from the port.
5585 */
5586 if (emb == LPFC_SLI4_MBX_EMBED) {
5587 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5588 id_array = &rsrc_ext->u.rsp.id[0];
5589 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5590 } else {
5591 virtaddr = mbox->sge_array->addr[0];
5592 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5593 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5594 id_array = &n_rsrc->id;
5595 }
5596
5597 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5598 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5599
5600 /*
5601 * Based on the resource size and count, correct the base and max
5602 * resource values.
5603 */
5604 length = sizeof(struct lpfc_rsrc_blks);
5605 switch (type) {
5606 case LPFC_RSC_TYPE_FCOE_RPI:
5607 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5608 sizeof(unsigned long),
5609 GFP_KERNEL);
5610 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5611 rc = -ENOMEM;
5612 goto err_exit;
5613 }
5614 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5615 sizeof(uint16_t),
5616 GFP_KERNEL);
5617 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5618 kfree(phba->sli4_hba.rpi_bmask);
5619 rc = -ENOMEM;
5620 goto err_exit;
5621 }
5622
5623 /*
5624 * The next_rpi was initialized with the maximum available
5625 * count but the port may allocate a smaller number. Catch
5626 * that case and update the next_rpi.
5627 */
5628 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5629
5630 /* Initialize local ptrs for common extent processing later. */
5631 bmask = phba->sli4_hba.rpi_bmask;
5632 ids = phba->sli4_hba.rpi_ids;
5633 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5634 break;
5635 case LPFC_RSC_TYPE_FCOE_VPI:
5636 phba->vpi_bmask = kzalloc(longs *
5637 sizeof(unsigned long),
5638 GFP_KERNEL);
5639 if (unlikely(!phba->vpi_bmask)) {
5640 rc = -ENOMEM;
5641 goto err_exit;
5642 }
5643 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5644 sizeof(uint16_t),
5645 GFP_KERNEL);
5646 if (unlikely(!phba->vpi_ids)) {
5647 kfree(phba->vpi_bmask);
5648 rc = -ENOMEM;
5649 goto err_exit;
5650 }
5651
5652 /* Initialize local ptrs for common extent processing later. */
5653 bmask = phba->vpi_bmask;
5654 ids = phba->vpi_ids;
5655 ext_blk_list = &phba->lpfc_vpi_blk_list;
5656 break;
5657 case LPFC_RSC_TYPE_FCOE_XRI:
5658 phba->sli4_hba.xri_bmask = kzalloc(longs *
5659 sizeof(unsigned long),
5660 GFP_KERNEL);
5661 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5662 rc = -ENOMEM;
5663 goto err_exit;
5664 }
8a9d2e80 5665 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
5666 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5667 sizeof(uint16_t),
5668 GFP_KERNEL);
5669 if (unlikely(!phba->sli4_hba.xri_ids)) {
5670 kfree(phba->sli4_hba.xri_bmask);
5671 rc = -ENOMEM;
5672 goto err_exit;
5673 }
5674
5675 /* Initialize local ptrs for common extent processing later. */
5676 bmask = phba->sli4_hba.xri_bmask;
5677 ids = phba->sli4_hba.xri_ids;
5678 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_VFI:
5681 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5682 sizeof(unsigned long),
5683 GFP_KERNEL);
5684 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5685 rc = -ENOMEM;
5686 goto err_exit;
5687 }
5688 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5689 sizeof(uint16_t),
5690 GFP_KERNEL);
5691 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5692 kfree(phba->sli4_hba.vfi_bmask);
5693 rc = -ENOMEM;
5694 goto err_exit;
5695 }
5696
5697 /* Initialize local ptrs for common extent processing later. */
5698 bmask = phba->sli4_hba.vfi_bmask;
5699 ids = phba->sli4_hba.vfi_ids;
5700 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5701 break;
5702 default:
5703 /* Unsupported Opcode. Fail call. */
5704 id_array = NULL;
5705 bmask = NULL;
5706 ids = NULL;
5707 ext_blk_list = NULL;
5708 goto err_exit;
5709 }
5710
5711 /*
5712 * Complete initializing the extent configuration with the
5713 * allocated ids assigned to this function. The bitmask serves
5714 * as an index into the array and manages the available ids. The
5715 * array just stores the ids communicated to the port via the wqes.
5716 */
5717 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5718 if ((i % 2) == 0)
5719 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5720 &id_array[k]);
5721 else
5722 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5723 &id_array[k]);
5724
5725 rsrc_blks = kzalloc(length, GFP_KERNEL);
5726 if (unlikely(!rsrc_blks)) {
5727 rc = -ENOMEM;
5728 kfree(bmask);
5729 kfree(ids);
5730 goto err_exit;
5731 }
5732 rsrc_blks->rsrc_start = rsrc_id;
5733 rsrc_blks->rsrc_size = rsrc_size;
5734 list_add_tail(&rsrc_blks->list, ext_blk_list);
5735 rsrc_start = rsrc_id;
895427bd 5736 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
6d368e53 5737 phba->sli4_hba.scsi_xri_start = rsrc_start +
895427bd
JS
5738 lpfc_sli4_get_iocb_cnt(phba);
5739 phba->sli4_hba.nvme_xri_start =
5740 phba->sli4_hba.scsi_xri_start +
5741 phba->sli4_hba.scsi_xri_max;
5742 }
6d368e53
JS
5743
5744 while (rsrc_id < (rsrc_start + rsrc_size)) {
5745 ids[j] = rsrc_id;
5746 rsrc_id++;
5747 j++;
5748 }
5749 /* Entire word processed. Get next word.*/
5750 if ((i % 2) == 1)
5751 k++;
5752 }
5753 err_exit:
5754 lpfc_sli4_mbox_cmd_free(phba, mbox);
5755 return rc;
5756}
5757
895427bd
JS
5758
5759
6d368e53
JS
5760/**
5761 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5762 * @phba: Pointer to HBA context object.
5763 * @type: the extent's type.
5764 *
5765 * This function deallocates all extents of a particular resource type.
5766 * SLI4 does not allow for deallocating a particular extent range. It
5767 * is the caller's responsibility to release all kernel memory resources.
5768 **/
5769static int
5770lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5771{
5772 int rc;
5773 uint32_t length, mbox_tmo = 0;
5774 LPFC_MBOXQ_t *mbox;
5775 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5776 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5777
5778 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5779 if (!mbox)
5780 return -ENOMEM;
5781
5782 /*
5783 * This function sends an embedded mailbox because it only sends the
5784 * the resource type. All extents of this type are released by the
5785 * port.
5786 */
5787 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5788 sizeof(struct lpfc_sli4_cfg_mhdr));
5789 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5790 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5791 length, LPFC_SLI4_MBX_EMBED);
5792
5793 /* Send an extents count of 0 - the dealloc doesn't use it. */
5794 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5795 LPFC_SLI4_MBX_EMBED);
5796 if (unlikely(rc)) {
5797 rc = -EIO;
5798 goto out_free_mbox;
5799 }
5800 if (!phba->sli4_hba.intr_enable)
5801 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5802 else {
a183a15f 5803 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5804 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5805 }
5806 if (unlikely(rc)) {
5807 rc = -EIO;
5808 goto out_free_mbox;
5809 }
5810
5811 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5812 if (bf_get(lpfc_mbox_hdr_status,
5813 &dealloc_rsrc->header.cfg_shdr.response)) {
5814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5815 "2919 Failed to release resource extents "
5816 "for type %d - Status 0x%x Add'l Status 0x%x. "
5817 "Resource memory not released.\n",
5818 type,
5819 bf_get(lpfc_mbox_hdr_status,
5820 &dealloc_rsrc->header.cfg_shdr.response),
5821 bf_get(lpfc_mbox_hdr_add_status,
5822 &dealloc_rsrc->header.cfg_shdr.response));
5823 rc = -EIO;
5824 goto out_free_mbox;
5825 }
5826
5827 /* Release kernel memory resources for the specific type. */
5828 switch (type) {
5829 case LPFC_RSC_TYPE_FCOE_VPI:
5830 kfree(phba->vpi_bmask);
5831 kfree(phba->vpi_ids);
5832 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5833 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5834 &phba->lpfc_vpi_blk_list, list) {
5835 list_del_init(&rsrc_blk->list);
5836 kfree(rsrc_blk);
5837 }
16a3a208 5838 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
5839 break;
5840 case LPFC_RSC_TYPE_FCOE_XRI:
5841 kfree(phba->sli4_hba.xri_bmask);
5842 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
5843 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5844 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5845 list_del_init(&rsrc_blk->list);
5846 kfree(rsrc_blk);
5847 }
5848 break;
5849 case LPFC_RSC_TYPE_FCOE_VFI:
5850 kfree(phba->sli4_hba.vfi_bmask);
5851 kfree(phba->sli4_hba.vfi_ids);
5852 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5853 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5854 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5855 list_del_init(&rsrc_blk->list);
5856 kfree(rsrc_blk);
5857 }
5858 break;
5859 case LPFC_RSC_TYPE_FCOE_RPI:
5860 /* RPI bitmask and physical id array are cleaned up earlier. */
5861 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5862 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5863 list_del_init(&rsrc_blk->list);
5864 kfree(rsrc_blk);
5865 }
5866 break;
5867 default:
5868 break;
5869 }
5870
5871 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5872
5873 out_free_mbox:
5874 mempool_free(mbox, phba->mbox_mem_pool);
5875 return rc;
5876}
5877
bd4b3e5c 5878static void
7bdedb34
JS
5879lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5880 uint32_t feature)
65791f1f 5881{
65791f1f 5882 uint32_t len;
65791f1f 5883
65791f1f
JS
5884 len = sizeof(struct lpfc_mbx_set_feature) -
5885 sizeof(struct lpfc_sli4_cfg_mhdr);
5886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5887 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5888 LPFC_SLI4_MBX_EMBED);
7bdedb34
JS
5889
5890 switch (feature) {
5891 case LPFC_SET_UE_RECOVERY:
5892 bf_set(lpfc_mbx_set_feature_UER,
5893 &mbox->u.mqe.un.set_feature, 1);
5894 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5895 mbox->u.mqe.un.set_feature.param_len = 8;
5896 break;
5897 case LPFC_SET_MDS_DIAGS:
5898 bf_set(lpfc_mbx_set_feature_mds,
5899 &mbox->u.mqe.un.set_feature, 1);
5900 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5901 &mbox->u.mqe.un.set_feature, 0);
5902 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5903 mbox->u.mqe.un.set_feature.param_len = 8;
5904 break;
65791f1f 5905 }
7bdedb34
JS
5906
5907 return;
65791f1f
JS
5908}
5909
6d368e53
JS
5910/**
5911 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5912 * @phba: Pointer to HBA context object.
5913 *
5914 * This function allocates all SLI4 resource identifiers.
5915 **/
5916int
5917lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5918{
5919 int i, rc, error = 0;
5920 uint16_t count, base;
5921 unsigned long longs;
5922
ff78d8f9
JS
5923 if (!phba->sli4_hba.rpi_hdrs_in_use)
5924 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6d368e53
JS
5925 if (phba->sli4_hba.extents_in_use) {
5926 /*
5927 * The port supports resource extents. The XRI, VPI, VFI, RPI
5928 * resource extent count must be read and allocated before
5929 * provisioning the resource id arrays.
5930 */
5931 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5932 LPFC_IDX_RSRC_RDY) {
5933 /*
5934 * Extent-based resources are set - the driver could
5935 * be in a port reset. Figure out if any corrective
5936 * actions need to be taken.
5937 */
5938 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5939 LPFC_RSC_TYPE_FCOE_VFI);
5940 if (rc != 0)
5941 error++;
5942 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5943 LPFC_RSC_TYPE_FCOE_VPI);
5944 if (rc != 0)
5945 error++;
5946 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5947 LPFC_RSC_TYPE_FCOE_XRI);
5948 if (rc != 0)
5949 error++;
5950 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5951 LPFC_RSC_TYPE_FCOE_RPI);
5952 if (rc != 0)
5953 error++;
5954
5955 /*
5956 * It's possible that the number of resources
5957 * provided to this port instance changed between
5958 * resets. Detect this condition and reallocate
5959 * resources. Otherwise, there is no action.
5960 */
5961 if (error) {
5962 lpfc_printf_log(phba, KERN_INFO,
5963 LOG_MBOX | LOG_INIT,
5964 "2931 Detected extent resource "
5965 "change. Reallocating all "
5966 "extents.\n");
5967 rc = lpfc_sli4_dealloc_extent(phba,
5968 LPFC_RSC_TYPE_FCOE_VFI);
5969 rc = lpfc_sli4_dealloc_extent(phba,
5970 LPFC_RSC_TYPE_FCOE_VPI);
5971 rc = lpfc_sli4_dealloc_extent(phba,
5972 LPFC_RSC_TYPE_FCOE_XRI);
5973 rc = lpfc_sli4_dealloc_extent(phba,
5974 LPFC_RSC_TYPE_FCOE_RPI);
5975 } else
5976 return 0;
5977 }
5978
5979 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5980 if (unlikely(rc))
5981 goto err_exit;
5982
5983 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5984 if (unlikely(rc))
5985 goto err_exit;
5986
5987 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5988 if (unlikely(rc))
5989 goto err_exit;
5990
5991 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5992 if (unlikely(rc))
5993 goto err_exit;
5994 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5995 LPFC_IDX_RSRC_RDY);
5996 return rc;
5997 } else {
5998 /*
5999 * The port does not support resource extents. The XRI, VPI,
6000 * VFI, RPI resource ids were determined from READ_CONFIG.
6001 * Just allocate the bitmasks and provision the resource id
6002 * arrays. If a port reset is active, the resources don't
6003 * need any action - just exit.
6004 */
6005 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
ff78d8f9
JS
6006 LPFC_IDX_RSRC_RDY) {
6007 lpfc_sli4_dealloc_resource_identifiers(phba);
6008 lpfc_sli4_remove_rpis(phba);
6009 }
6d368e53
JS
6010 /* RPIs. */
6011 count = phba->sli4_hba.max_cfg_param.max_rpi;
0a630c27
JS
6012 if (count <= 0) {
6013 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6014 "3279 Invalid provisioning of "
6015 "rpi:%d\n", count);
6016 rc = -EINVAL;
6017 goto err_exit;
6018 }
6d368e53
JS
6019 base = phba->sli4_hba.max_cfg_param.rpi_base;
6020 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6021 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6022 sizeof(unsigned long),
6023 GFP_KERNEL);
6024 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6025 rc = -ENOMEM;
6026 goto err_exit;
6027 }
6028 phba->sli4_hba.rpi_ids = kzalloc(count *
6029 sizeof(uint16_t),
6030 GFP_KERNEL);
6031 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6032 rc = -ENOMEM;
6033 goto free_rpi_bmask;
6034 }
6035
6036 for (i = 0; i < count; i++)
6037 phba->sli4_hba.rpi_ids[i] = base + i;
6038
6039 /* VPIs. */
6040 count = phba->sli4_hba.max_cfg_param.max_vpi;
0a630c27
JS
6041 if (count <= 0) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6043 "3280 Invalid provisioning of "
6044 "vpi:%d\n", count);
6045 rc = -EINVAL;
6046 goto free_rpi_ids;
6047 }
6d368e53
JS
6048 base = phba->sli4_hba.max_cfg_param.vpi_base;
6049 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6050 phba->vpi_bmask = kzalloc(longs *
6051 sizeof(unsigned long),
6052 GFP_KERNEL);
6053 if (unlikely(!phba->vpi_bmask)) {
6054 rc = -ENOMEM;
6055 goto free_rpi_ids;
6056 }
6057 phba->vpi_ids = kzalloc(count *
6058 sizeof(uint16_t),
6059 GFP_KERNEL);
6060 if (unlikely(!phba->vpi_ids)) {
6061 rc = -ENOMEM;
6062 goto free_vpi_bmask;
6063 }
6064
6065 for (i = 0; i < count; i++)
6066 phba->vpi_ids[i] = base + i;
6067
6068 /* XRIs. */
6069 count = phba->sli4_hba.max_cfg_param.max_xri;
0a630c27
JS
6070 if (count <= 0) {
6071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6072 "3281 Invalid provisioning of "
6073 "xri:%d\n", count);
6074 rc = -EINVAL;
6075 goto free_vpi_ids;
6076 }
6d368e53
JS
6077 base = phba->sli4_hba.max_cfg_param.xri_base;
6078 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6079 phba->sli4_hba.xri_bmask = kzalloc(longs *
6080 sizeof(unsigned long),
6081 GFP_KERNEL);
6082 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6083 rc = -ENOMEM;
6084 goto free_vpi_ids;
6085 }
41899be7 6086 phba->sli4_hba.max_cfg_param.xri_used = 0;
6d368e53
JS
6087 phba->sli4_hba.xri_ids = kzalloc(count *
6088 sizeof(uint16_t),
6089 GFP_KERNEL);
6090 if (unlikely(!phba->sli4_hba.xri_ids)) {
6091 rc = -ENOMEM;
6092 goto free_xri_bmask;
6093 }
6094
6095 for (i = 0; i < count; i++)
6096 phba->sli4_hba.xri_ids[i] = base + i;
6097
6098 /* VFIs. */
6099 count = phba->sli4_hba.max_cfg_param.max_vfi;
0a630c27
JS
6100 if (count <= 0) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6102 "3282 Invalid provisioning of "
6103 "vfi:%d\n", count);
6104 rc = -EINVAL;
6105 goto free_xri_ids;
6106 }
6d368e53
JS
6107 base = phba->sli4_hba.max_cfg_param.vfi_base;
6108 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6109 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6110 sizeof(unsigned long),
6111 GFP_KERNEL);
6112 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6113 rc = -ENOMEM;
6114 goto free_xri_ids;
6115 }
6116 phba->sli4_hba.vfi_ids = kzalloc(count *
6117 sizeof(uint16_t),
6118 GFP_KERNEL);
6119 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6120 rc = -ENOMEM;
6121 goto free_vfi_bmask;
6122 }
6123
6124 for (i = 0; i < count; i++)
6125 phba->sli4_hba.vfi_ids[i] = base + i;
6126
6127 /*
6128 * Mark all resources ready. An HBA reset doesn't need
6129 * to reset the initialization.
6130 */
6131 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6132 LPFC_IDX_RSRC_RDY);
6133 return 0;
6134 }
6135
6136 free_vfi_bmask:
6137 kfree(phba->sli4_hba.vfi_bmask);
cd60be49 6138 phba->sli4_hba.vfi_bmask = NULL;
6d368e53
JS
6139 free_xri_ids:
6140 kfree(phba->sli4_hba.xri_ids);
cd60be49 6141 phba->sli4_hba.xri_ids = NULL;
6d368e53
JS
6142 free_xri_bmask:
6143 kfree(phba->sli4_hba.xri_bmask);
cd60be49 6144 phba->sli4_hba.xri_bmask = NULL;
6d368e53
JS
6145 free_vpi_ids:
6146 kfree(phba->vpi_ids);
cd60be49 6147 phba->vpi_ids = NULL;
6d368e53
JS
6148 free_vpi_bmask:
6149 kfree(phba->vpi_bmask);
cd60be49 6150 phba->vpi_bmask = NULL;
6d368e53
JS
6151 free_rpi_ids:
6152 kfree(phba->sli4_hba.rpi_ids);
cd60be49 6153 phba->sli4_hba.rpi_ids = NULL;
6d368e53
JS
6154 free_rpi_bmask:
6155 kfree(phba->sli4_hba.rpi_bmask);
cd60be49 6156 phba->sli4_hba.rpi_bmask = NULL;
6d368e53
JS
6157 err_exit:
6158 return rc;
6159}
6160
6161/**
6162 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6163 * @phba: Pointer to HBA context object.
6164 *
6165 * This function allocates the number of elements for the specified
6166 * resource type.
6167 **/
6168int
6169lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6170{
6171 if (phba->sli4_hba.extents_in_use) {
6172 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6173 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6174 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6175 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6176 } else {
6177 kfree(phba->vpi_bmask);
16a3a208 6178 phba->sli4_hba.max_cfg_param.vpi_used = 0;
6d368e53
JS
6179 kfree(phba->vpi_ids);
6180 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6181 kfree(phba->sli4_hba.xri_bmask);
6182 kfree(phba->sli4_hba.xri_ids);
6d368e53
JS
6183 kfree(phba->sli4_hba.vfi_bmask);
6184 kfree(phba->sli4_hba.vfi_ids);
6185 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6186 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6187 }
6188
6189 return 0;
6190}
6191
b76f2dc9
JS
6192/**
6193 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6194 * @phba: Pointer to HBA context object.
6195 * @type: The resource extent type.
6196 * @extnt_count: buffer to hold port extent count response
6197 * @extnt_size: buffer to hold port extent size response.
6198 *
6199 * This function calls the port to read the host allocated extents
6200 * for a particular type.
6201 **/
6202int
6203lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6204 uint16_t *extnt_cnt, uint16_t *extnt_size)
6205{
6206 bool emb;
6207 int rc = 0;
6208 uint16_t curr_blks = 0;
6209 uint32_t req_len, emb_len;
6210 uint32_t alloc_len, mbox_tmo;
6211 struct list_head *blk_list_head;
6212 struct lpfc_rsrc_blks *rsrc_blk;
6213 LPFC_MBOXQ_t *mbox;
6214 void *virtaddr = NULL;
6215 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6216 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6217 union lpfc_sli4_cfg_shdr *shdr;
6218
6219 switch (type) {
6220 case LPFC_RSC_TYPE_FCOE_VPI:
6221 blk_list_head = &phba->lpfc_vpi_blk_list;
6222 break;
6223 case LPFC_RSC_TYPE_FCOE_XRI:
6224 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6225 break;
6226 case LPFC_RSC_TYPE_FCOE_VFI:
6227 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6228 break;
6229 case LPFC_RSC_TYPE_FCOE_RPI:
6230 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6231 break;
6232 default:
6233 return -EIO;
6234 }
6235
6236 /* Count the number of extents currently allocatd for this type. */
6237 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6238 if (curr_blks == 0) {
6239 /*
6240 * The GET_ALLOCATED mailbox does not return the size,
6241 * just the count. The size should be just the size
6242 * stored in the current allocated block and all sizes
6243 * for an extent type are the same so set the return
6244 * value now.
6245 */
6246 *extnt_size = rsrc_blk->rsrc_size;
6247 }
6248 curr_blks++;
6249 }
6250
b76f2dc9
JS
6251 /*
6252 * Calculate the size of an embedded mailbox. The uint32_t
6253 * accounts for extents-specific word.
6254 */
6255 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6256 sizeof(uint32_t);
6257
6258 /*
6259 * Presume the allocation and response will fit into an embedded
6260 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6261 */
6262 emb = LPFC_SLI4_MBX_EMBED;
6263 req_len = emb_len;
6264 if (req_len > emb_len) {
6265 req_len = curr_blks * sizeof(uint16_t) +
6266 sizeof(union lpfc_sli4_cfg_shdr) +
6267 sizeof(uint32_t);
6268 emb = LPFC_SLI4_MBX_NEMBED;
6269 }
6270
6271 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6272 if (!mbox)
6273 return -ENOMEM;
6274 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6275
6276 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6277 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6278 req_len, emb);
6279 if (alloc_len < req_len) {
6280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6281 "2983 Allocated DMA memory size (x%x) is "
6282 "less than the requested DMA memory "
6283 "size (x%x)\n", alloc_len, req_len);
6284 rc = -ENOMEM;
6285 goto err_exit;
6286 }
6287 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6288 if (unlikely(rc)) {
6289 rc = -EIO;
6290 goto err_exit;
6291 }
6292
6293 if (!phba->sli4_hba.intr_enable)
6294 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6295 else {
a183a15f 6296 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
6297 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6298 }
6299
6300 if (unlikely(rc)) {
6301 rc = -EIO;
6302 goto err_exit;
6303 }
6304
6305 /*
6306 * Figure out where the response is located. Then get local pointers
6307 * to the response data. The port does not guarantee to respond to
6308 * all extents counts request so update the local variable with the
6309 * allocated count from the port.
6310 */
6311 if (emb == LPFC_SLI4_MBX_EMBED) {
6312 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6313 shdr = &rsrc_ext->header.cfg_shdr;
6314 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6315 } else {
6316 virtaddr = mbox->sge_array->addr[0];
6317 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6318 shdr = &n_rsrc->cfg_shdr;
6319 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6320 }
6321
6322 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6324 "2984 Failed to read allocated resources "
6325 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6326 type,
6327 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6328 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6329 rc = -EIO;
6330 goto err_exit;
6331 }
6332 err_exit:
6333 lpfc_sli4_mbox_cmd_free(phba, mbox);
6334 return rc;
6335}
6336
8a9d2e80 6337/**
895427bd 6338 * lpfc_sli4_repost_sgl_list - Repsot the buffers sgl pages as block
8a9d2e80 6339 * @phba: pointer to lpfc hba data structure.
895427bd
JS
6340 * @pring: Pointer to driver SLI ring object.
6341 * @sgl_list: linked link of sgl buffers to post
6342 * @cnt: number of linked list buffers
8a9d2e80 6343 *
895427bd 6344 * This routine walks the list of buffers that have been allocated and
8a9d2e80
JS
6345 * repost them to the port by using SGL block post. This is needed after a
6346 * pci_function_reset/warm_start or start. It attempts to construct blocks
895427bd
JS
6347 * of buffer sgls which contains contiguous xris and uses the non-embedded
6348 * SGL block post mailbox commands to post them to the port. For single
8a9d2e80
JS
6349 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6350 * mailbox command for posting.
6351 *
6352 * Returns: 0 = success, non-zero failure.
6353 **/
6354static int
895427bd
JS
6355lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6356 struct list_head *sgl_list, int cnt)
8a9d2e80
JS
6357{
6358 struct lpfc_sglq *sglq_entry = NULL;
6359 struct lpfc_sglq *sglq_entry_next = NULL;
6360 struct lpfc_sglq *sglq_entry_first = NULL;
895427bd
JS
6361 int status, total_cnt;
6362 int post_cnt = 0, num_posted = 0, block_cnt = 0;
8a9d2e80
JS
6363 int last_xritag = NO_XRI;
6364 LIST_HEAD(prep_sgl_list);
6365 LIST_HEAD(blck_sgl_list);
6366 LIST_HEAD(allc_sgl_list);
6367 LIST_HEAD(post_sgl_list);
6368 LIST_HEAD(free_sgl_list);
6369
38c20673 6370 spin_lock_irq(&phba->hbalock);
895427bd
JS
6371 spin_lock(&phba->sli4_hba.sgl_list_lock);
6372 list_splice_init(sgl_list, &allc_sgl_list);
6373 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6374 spin_unlock_irq(&phba->hbalock);
8a9d2e80 6375
895427bd 6376 total_cnt = cnt;
8a9d2e80
JS
6377 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6378 &allc_sgl_list, list) {
6379 list_del_init(&sglq_entry->list);
6380 block_cnt++;
6381 if ((last_xritag != NO_XRI) &&
6382 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6383 /* a hole in xri block, form a sgl posting block */
6384 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6385 post_cnt = block_cnt - 1;
6386 /* prepare list for next posting block */
6387 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6388 block_cnt = 1;
6389 } else {
6390 /* prepare list for next posting block */
6391 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6392 /* enough sgls for non-embed sgl mbox command */
6393 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6394 list_splice_init(&prep_sgl_list,
6395 &blck_sgl_list);
6396 post_cnt = block_cnt;
6397 block_cnt = 0;
6398 }
6399 }
6400 num_posted++;
6401
6402 /* keep track of last sgl's xritag */
6403 last_xritag = sglq_entry->sli4_xritag;
6404
895427bd
JS
6405 /* end of repost sgl list condition for buffers */
6406 if (num_posted == total_cnt) {
8a9d2e80
JS
6407 if (post_cnt == 0) {
6408 list_splice_init(&prep_sgl_list,
6409 &blck_sgl_list);
6410 post_cnt = block_cnt;
6411 } else if (block_cnt == 1) {
6412 status = lpfc_sli4_post_sgl(phba,
6413 sglq_entry->phys, 0,
6414 sglq_entry->sli4_xritag);
6415 if (!status) {
6416 /* successful, put sgl to posted list */
6417 list_add_tail(&sglq_entry->list,
6418 &post_sgl_list);
6419 } else {
6420 /* Failure, put sgl to free list */
6421 lpfc_printf_log(phba, KERN_WARNING,
6422 LOG_SLI,
895427bd 6423 "3159 Failed to post "
8a9d2e80
JS
6424 "sgl, xritag:x%x\n",
6425 sglq_entry->sli4_xritag);
6426 list_add_tail(&sglq_entry->list,
6427 &free_sgl_list);
711ea882 6428 total_cnt--;
8a9d2e80
JS
6429 }
6430 }
6431 }
6432
6433 /* continue until a nembed page worth of sgls */
6434 if (post_cnt == 0)
6435 continue;
6436
895427bd
JS
6437 /* post the buffer list sgls as a block */
6438 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6439 post_cnt);
8a9d2e80
JS
6440
6441 if (!status) {
6442 /* success, put sgl list to posted sgl list */
6443 list_splice_init(&blck_sgl_list, &post_sgl_list);
6444 } else {
6445 /* Failure, put sgl list to free sgl list */
6446 sglq_entry_first = list_first_entry(&blck_sgl_list,
6447 struct lpfc_sglq,
6448 list);
6449 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
895427bd 6450 "3160 Failed to post sgl-list, "
8a9d2e80
JS
6451 "xritag:x%x-x%x\n",
6452 sglq_entry_first->sli4_xritag,
6453 (sglq_entry_first->sli4_xritag +
6454 post_cnt - 1));
6455 list_splice_init(&blck_sgl_list, &free_sgl_list);
711ea882 6456 total_cnt -= post_cnt;
8a9d2e80
JS
6457 }
6458
6459 /* don't reset xirtag due to hole in xri block */
6460 if (block_cnt == 0)
6461 last_xritag = NO_XRI;
6462
895427bd 6463 /* reset sgl post count for next round of posting */
8a9d2e80
JS
6464 post_cnt = 0;
6465 }
6466
895427bd 6467 /* free the sgls failed to post */
8a9d2e80
JS
6468 lpfc_free_sgl_list(phba, &free_sgl_list);
6469
895427bd 6470 /* push sgls posted to the available list */
8a9d2e80 6471 if (!list_empty(&post_sgl_list)) {
38c20673 6472 spin_lock_irq(&phba->hbalock);
895427bd
JS
6473 spin_lock(&phba->sli4_hba.sgl_list_lock);
6474 list_splice_init(&post_sgl_list, sgl_list);
6475 spin_unlock(&phba->sli4_hba.sgl_list_lock);
38c20673 6476 spin_unlock_irq(&phba->hbalock);
8a9d2e80
JS
6477 } else {
6478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 6479 "3161 Failure to post sgl to port.\n");
8a9d2e80
JS
6480 return -EIO;
6481 }
895427bd
JS
6482
6483 /* return the number of XRIs actually posted */
6484 return total_cnt;
8a9d2e80
JS
6485}
6486
61bda8f7
JS
6487void
6488lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6489{
6490 uint32_t len;
6491
6492 len = sizeof(struct lpfc_mbx_set_host_data) -
6493 sizeof(struct lpfc_sli4_cfg_mhdr);
6494 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6495 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6496 LPFC_SLI4_MBX_EMBED);
6497
6498 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
b2fd103b
JS
6499 mbox->u.mqe.un.set_host_data.param_len =
6500 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
61bda8f7
JS
6501 snprintf(mbox->u.mqe.un.set_host_data.data,
6502 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6503 "Linux %s v"LPFC_DRIVER_VERSION,
6504 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6505}
6506
da0436e9
JS
6507/**
6508 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6509 * @phba: Pointer to HBA context object.
6510 *
6511 * This function is the main SLI4 device intialization PCI function. This
6512 * function is called by the HBA intialization code, HBA reset code and
6513 * HBA error attention handler code. Caller is not required to hold any
6514 * locks.
6515 **/
6516int
6517lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6518{
2d7dbc4c 6519 int rc, i;
da0436e9
JS
6520 LPFC_MBOXQ_t *mboxq;
6521 struct lpfc_mqe *mqe;
6522 uint8_t *vpd;
6523 uint32_t vpd_size;
6524 uint32_t ftr_rsp = 0;
6525 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6526 struct lpfc_vport *vport = phba->pport;
6527 struct lpfc_dmabuf *mp;
2d7dbc4c 6528 struct lpfc_rqb *rqbp;
da0436e9
JS
6529
6530 /* Perform a PCI function reset to start from clean */
6531 rc = lpfc_pci_function_reset(phba);
6532 if (unlikely(rc))
6533 return -ENODEV;
6534
6535 /* Check the HBA Host Status Register for readyness */
6536 rc = lpfc_sli4_post_status_check(phba);
6537 if (unlikely(rc))
6538 return -ENODEV;
6539 else {
6540 spin_lock_irq(&phba->hbalock);
6541 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6542 spin_unlock_irq(&phba->hbalock);
6543 }
6544
6545 /*
6546 * Allocate a single mailbox container for initializing the
6547 * port.
6548 */
6549 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6550 if (!mboxq)
6551 return -ENOMEM;
6552
da0436e9 6553 /* Issue READ_REV to collect vpd and FW information. */
49198b37 6554 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
6555 vpd = kzalloc(vpd_size, GFP_KERNEL);
6556 if (!vpd) {
6557 rc = -ENOMEM;
6558 goto out_free_mbox;
6559 }
6560
6561 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
6562 if (unlikely(rc)) {
6563 kfree(vpd);
6564 goto out_free_mbox;
6565 }
572709e2 6566
da0436e9 6567 mqe = &mboxq->u.mqe;
f1126688 6568 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
b5c53958 6569 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
76a95d75 6570 phba->hba_flag |= HBA_FCOE_MODE;
b5c53958
JS
6571 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6572 } else {
76a95d75 6573 phba->hba_flag &= ~HBA_FCOE_MODE;
b5c53958 6574 }
45ed1190
JS
6575
6576 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6577 LPFC_DCBX_CEE_MODE)
6578 phba->hba_flag |= HBA_FIP_SUPPORT;
6579 else
6580 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6581
4f2e66c6
JS
6582 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6583
c31098ce 6584 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
6585 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6586 "0376 READ_REV Error. SLI Level %d "
6587 "FCoE enabled %d\n",
76a95d75 6588 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 6589 rc = -EIO;
76a95d75
JS
6590 kfree(vpd);
6591 goto out_free_mbox;
da0436e9 6592 }
cd1c8301 6593
ff78d8f9
JS
6594 /*
6595 * Continue initialization with default values even if driver failed
6596 * to read FCoE param config regions, only read parameters if the
6597 * board is FCoE
6598 */
6599 if (phba->hba_flag & HBA_FCOE_MODE &&
6600 lpfc_sli4_read_fcoe_params(phba))
6601 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6602 "2570 Failed to read FCoE parameters\n");
6603
cd1c8301
JS
6604 /*
6605 * Retrieve sli4 device physical port name, failure of doing it
6606 * is considered as non-fatal.
6607 */
6608 rc = lpfc_sli4_retrieve_pport_name(phba);
6609 if (!rc)
6610 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6611 "3080 Successful retrieving SLI4 device "
6612 "physical port name: %s.\n", phba->Port);
6613
da0436e9
JS
6614 /*
6615 * Evaluate the read rev and vpd data. Populate the driver
6616 * state with the results. If this routine fails, the failure
6617 * is not fatal as the driver will use generic values.
6618 */
6619 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6620 if (unlikely(!rc)) {
6621 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6622 "0377 Error %d parsing vpd. "
6623 "Using defaults.\n", rc);
6624 rc = 0;
6625 }
76a95d75 6626 kfree(vpd);
da0436e9 6627
f1126688
JS
6628 /* Save information as VPD data */
6629 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6630 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6631 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6632 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6633 &mqe->un.read_rev);
6634 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6635 &mqe->un.read_rev);
6636 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6637 &mqe->un.read_rev);
6638 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6639 &mqe->un.read_rev);
6640 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6641 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6642 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6643 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6644 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6645 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6646 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6647 "(%d):0380 READ_REV Status x%x "
6648 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6649 mboxq->vport ? mboxq->vport->vpi : 0,
6650 bf_get(lpfc_mqe_status, mqe),
6651 phba->vpd.rev.opFwName,
6652 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6653 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9 6654
572709e2
JS
6655 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6656 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6657 if (phba->pport->cfg_lun_queue_depth > rc) {
6658 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6659 "3362 LUN queue depth changed from %d to %d\n",
6660 phba->pport->cfg_lun_queue_depth, rc);
6661 phba->pport->cfg_lun_queue_depth = rc;
6662 }
6663
65791f1f 6664 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7bdedb34
JS
6665 LPFC_SLI_INTF_IF_TYPE_0) {
6666 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6667 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6668 if (rc == MBX_SUCCESS) {
6669 phba->hba_flag |= HBA_RECOVERABLE_UE;
6670 /* Set 1Sec interval to detect UE */
6671 phba->eratt_poll_interval = 1;
6672 phba->sli4_hba.ue_to_sr = bf_get(
6673 lpfc_mbx_set_feature_UESR,
6674 &mboxq->u.mqe.un.set_feature);
6675 phba->sli4_hba.ue_to_rp = bf_get(
6676 lpfc_mbx_set_feature_UERP,
6677 &mboxq->u.mqe.un.set_feature);
6678 }
6679 }
6680
6681 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6682 /* Enable MDS Diagnostics only if the SLI Port supports it */
6683 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6684 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6685 if (rc != MBX_SUCCESS)
6686 phba->mds_diags_support = 0;
6687 }
572709e2 6688
da0436e9
JS
6689 /*
6690 * Discover the port's supported feature set and match it against the
6691 * hosts requests.
6692 */
6693 lpfc_request_features(phba, mboxq);
6694 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6695 if (unlikely(rc)) {
6696 rc = -EIO;
76a95d75 6697 goto out_free_mbox;
da0436e9
JS
6698 }
6699
6700 /*
6701 * The port must support FCP initiator mode as this is the
6702 * only mode running in the host.
6703 */
6704 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6705 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6706 "0378 No support for fcpi mode.\n");
6707 ftr_rsp++;
6708 }
fedd3b7b
JS
6709 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6710 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6711 else
6712 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6713 /*
6714 * If the port cannot support the host's requested features
6715 * then turn off the global config parameters to disable the
6716 * feature in the driver. This is not a fatal error.
6717 */
bf08611b
JS
6718 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6719 if (phba->cfg_enable_bg) {
6720 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6721 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6722 else
6723 ftr_rsp++;
6724 }
da0436e9
JS
6725
6726 if (phba->max_vpi && phba->cfg_enable_npiv &&
6727 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6728 ftr_rsp++;
6729
6730 if (ftr_rsp) {
6731 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6732 "0379 Feature Mismatch Data: x%08x %08x "
6733 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6734 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6735 phba->cfg_enable_npiv, phba->max_vpi);
6736 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6737 phba->cfg_enable_bg = 0;
6738 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6739 phba->cfg_enable_npiv = 0;
6740 }
6741
6742 /* These SLI3 features are assumed in SLI4 */
6743 spin_lock_irq(&phba->hbalock);
6744 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6745 spin_unlock_irq(&phba->hbalock);
6746
6d368e53
JS
6747 /*
6748 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6749 * calls depends on these resources to complete port setup.
6750 */
6751 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6752 if (rc) {
6753 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6754 "2920 Failed to alloc Resource IDs "
6755 "rc = x%x\n", rc);
6756 goto out_free_mbox;
6757 }
6758
61bda8f7
JS
6759 lpfc_set_host_data(phba, mboxq);
6760
6761 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6762 if (rc) {
6763 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6764 "2134 Failed to set host os driver version %x",
6765 rc);
6766 }
6767
da0436e9 6768 /* Read the port's service parameters. */
9f1177a3
JS
6769 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6770 if (rc) {
6771 phba->link_state = LPFC_HBA_ERROR;
6772 rc = -ENOMEM;
76a95d75 6773 goto out_free_mbox;
9f1177a3
JS
6774 }
6775
da0436e9
JS
6776 mboxq->vport = vport;
6777 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6778 mp = (struct lpfc_dmabuf *) mboxq->context1;
6779 if (rc == MBX_SUCCESS) {
6780 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6781 rc = 0;
6782 }
6783
6784 /*
6785 * This memory was allocated by the lpfc_read_sparam routine. Release
6786 * it to the mbuf pool.
6787 */
6788 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6789 kfree(mp);
6790 mboxq->context1 = NULL;
6791 if (unlikely(rc)) {
6792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6793 "0382 READ_SPARAM command failed "
6794 "status %d, mbxStatus x%x\n",
6795 rc, bf_get(lpfc_mqe_status, mqe));
6796 phba->link_state = LPFC_HBA_ERROR;
6797 rc = -EIO;
76a95d75 6798 goto out_free_mbox;
da0436e9
JS
6799 }
6800
0558056c 6801 lpfc_update_vport_wwn(vport);
da0436e9
JS
6802
6803 /* Update the fc_host data structures with new wwn. */
6804 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6805 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6806
895427bd
JS
6807 /* Create all the SLI4 queues */
6808 rc = lpfc_sli4_queue_create(phba);
6809 if (rc) {
6810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6811 "3089 Failed to allocate queues\n");
6812 rc = -ENODEV;
6813 goto out_free_mbox;
6814 }
6815 /* Set up all the queues to the device */
6816 rc = lpfc_sli4_queue_setup(phba);
6817 if (unlikely(rc)) {
6818 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6819 "0381 Error %d during queue setup.\n ", rc);
6820 goto out_stop_timers;
6821 }
6822 /* Initialize the driver internal SLI layer lists. */
6823 lpfc_sli4_setup(phba);
6824 lpfc_sli4_queue_init(phba);
6825
6826 /* update host els xri-sgl sizes and mappings */
6827 rc = lpfc_sli4_els_sgl_update(phba);
8a9d2e80
JS
6828 if (unlikely(rc)) {
6829 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6830 "1400 Failed to update xri-sgl size and "
6831 "mapping: %d\n", rc);
895427bd 6832 goto out_destroy_queue;
da0436e9
JS
6833 }
6834
8a9d2e80 6835 /* register the els sgl pool to the port */
895427bd
JS
6836 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6837 phba->sli4_hba.els_xri_cnt);
6838 if (unlikely(rc < 0)) {
8a9d2e80
JS
6839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6840 "0582 Error %d during els sgl post "
6841 "operation\n", rc);
6842 rc = -ENODEV;
895427bd 6843 goto out_destroy_queue;
8a9d2e80 6844 }
895427bd 6845 phba->sli4_hba.els_xri_cnt = rc;
8a9d2e80 6846
f358dd0c
JS
6847 if (phba->nvmet_support) {
6848 /* update host nvmet xri-sgl sizes and mappings */
6849 rc = lpfc_sli4_nvmet_sgl_update(phba);
6850 if (unlikely(rc)) {
6851 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6852 "6308 Failed to update nvmet-sgl size "
6853 "and mapping: %d\n", rc);
6854 goto out_destroy_queue;
6855 }
6856
6857 /* register the nvmet sgl pool to the port */
6858 rc = lpfc_sli4_repost_sgl_list(
6859 phba,
6860 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6861 phba->sli4_hba.nvmet_xri_cnt);
6862 if (unlikely(rc < 0)) {
6863 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6864 "3117 Error %d during nvmet "
6865 "sgl post\n", rc);
6866 rc = -ENODEV;
6867 goto out_destroy_queue;
6868 }
6869 phba->sli4_hba.nvmet_xri_cnt = rc;
d613b6a7 6870 lpfc_nvmet_create_targetport(phba);
f358dd0c 6871 } else {
895427bd
JS
6872 /* update host scsi xri-sgl sizes and mappings */
6873 rc = lpfc_sli4_scsi_sgl_update(phba);
6874 if (unlikely(rc)) {
6875 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6876 "6309 Failed to update scsi-sgl size "
6877 "and mapping: %d\n", rc);
6878 goto out_destroy_queue;
6879 }
6880
6881 /* update host nvme xri-sgl sizes and mappings */
6882 rc = lpfc_sli4_nvme_sgl_update(phba);
6883 if (unlikely(rc)) {
6884 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6885 "6082 Failed to update nvme-sgl size "
6886 "and mapping: %d\n", rc);
6887 goto out_destroy_queue;
6888 }
6889 }
6890
2d7dbc4c
JS
6891 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6892
6893 /* Post initial buffers to all RQs created */
6894 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6895 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6896 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6897 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6898 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6899 rqbp->entry_count = 256;
6900 rqbp->buffer_count = 0;
6901
6902 /* Divide by 4 and round down to multiple of 16 */
6903 rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6904 phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6905 phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6906
6907 lpfc_post_rq_buffer(
6908 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6909 phba->sli4_hba.nvmet_mrq_data[i],
6910 phba->cfg_nvmet_mrq_post);
6911 }
6912 }
6913
895427bd
JS
6914 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6915 /* register the allocated scsi sgl pool to the port */
6916 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6917 if (unlikely(rc)) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "0383 Error %d during scsi sgl post "
6920 "operation\n", rc);
6921 /* Some Scsi buffers were moved to abort scsi list */
6922 /* A pci function reset will repost them */
6923 rc = -ENODEV;
6924 goto out_destroy_queue;
6925 }
da0436e9
JS
6926 }
6927
01649561
JS
6928 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6929 (phba->nvmet_support == 0)) {
6930
6931 /* register the allocated nvme sgl pool to the port */
6932 rc = lpfc_repost_nvme_sgl_list(phba);
6933 if (unlikely(rc)) {
6934 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6935 "6116 Error %d during nvme sgl post "
6936 "operation\n", rc);
6937 /* Some NVME buffers were moved to abort nvme list */
6938 /* A pci function reset will repost them */
6939 rc = -ENODEV;
6940 goto out_destroy_queue;
6941 }
6942 }
6943
da0436e9
JS
6944 /* Post the rpi header region to the device. */
6945 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6946 if (unlikely(rc)) {
6947 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6948 "0393 Error %d during rpi post operation\n",
6949 rc);
6950 rc = -ENODEV;
895427bd 6951 goto out_destroy_queue;
da0436e9 6952 }
97f2ecf1 6953 lpfc_sli4_node_prep(phba);
da0436e9 6954
895427bd 6955 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
2d7dbc4c 6956 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
895427bd
JS
6957 /*
6958 * The FC Port needs to register FCFI (index 0)
6959 */
6960 lpfc_reg_fcfi(phba, mboxq);
6961 mboxq->vport = phba->pport;
6962 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6963 if (rc != MBX_SUCCESS)
6964 goto out_unset_queue;
6965 rc = 0;
6966 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6967 &mboxq->u.mqe.un.reg_fcfi);
2d7dbc4c
JS
6968 } else {
6969 /* We are a NVME Target mode with MRQ > 1 */
6970
6971 /* First register the FCFI */
6972 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6973 mboxq->vport = phba->pport;
6974 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6975 if (rc != MBX_SUCCESS)
6976 goto out_unset_queue;
6977 rc = 0;
6978 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6979 &mboxq->u.mqe.un.reg_fcfi_mrq);
6980
6981 /* Next register the MRQs */
6982 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6983 mboxq->vport = phba->pport;
6984 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6985 if (rc != MBX_SUCCESS)
6986 goto out_unset_queue;
6987 rc = 0;
895427bd
JS
6988 }
6989 /* Check if the port is configured to be disabled */
6990 lpfc_sli_read_link_ste(phba);
da0436e9
JS
6991 }
6992
6993 /* Arm the CQs and then EQs on device */
6994 lpfc_sli4_arm_cqeq_intr(phba);
6995
6996 /* Indicate device interrupt mode */
6997 phba->sli4_hba.intr_enable = 1;
6998
6999 /* Allow asynchronous mailbox command to go through */
7000 spin_lock_irq(&phba->hbalock);
7001 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7002 spin_unlock_irq(&phba->hbalock);
7003
7004 /* Post receive buffers to the device */
7005 lpfc_sli4_rb_setup(phba);
7006
fc2b989b
JS
7007 /* Reset HBA FCF states after HBA reset */
7008 phba->fcf.fcf_flag = 0;
7009 phba->fcf.current_rec.flag = 0;
7010
da0436e9 7011 /* Start the ELS watchdog timer */
8fa38513 7012 mod_timer(&vport->els_tmofunc,
256ec0d0 7013 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
da0436e9
JS
7014
7015 /* Start heart beat timer */
7016 mod_timer(&phba->hb_tmofunc,
256ec0d0 7017 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
da0436e9
JS
7018 phba->hb_outstanding = 0;
7019 phba->last_completion_time = jiffies;
7020
7021 /* Start error attention (ERATT) polling timer */
256ec0d0 7022 mod_timer(&phba->eratt_poll,
65791f1f 7023 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
da0436e9 7024
75baf696
JS
7025 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7026 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7027 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7028 if (!rc) {
7029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7030 "2829 This device supports "
7031 "Advanced Error Reporting (AER)\n");
7032 spin_lock_irq(&phba->hbalock);
7033 phba->hba_flag |= HBA_AER_ENABLED;
7034 spin_unlock_irq(&phba->hbalock);
7035 } else {
7036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7037 "2830 This device does not support "
7038 "Advanced Error Reporting (AER)\n");
7039 phba->cfg_aer_support = 0;
7040 }
0a96e975 7041 rc = 0;
75baf696
JS
7042 }
7043
da0436e9
JS
7044 /*
7045 * The port is ready, set the host's link state to LINK_DOWN
7046 * in preparation for link interrupts.
7047 */
da0436e9
JS
7048 spin_lock_irq(&phba->hbalock);
7049 phba->link_state = LPFC_LINK_DOWN;
7050 spin_unlock_irq(&phba->hbalock);
026abb87
JS
7051 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7052 (phba->hba_flag & LINK_DISABLED)) {
7053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7054 "3103 Adapter Link is disabled.\n");
7055 lpfc_down_link(phba, mboxq);
7056 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7057 if (rc != MBX_SUCCESS) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7059 "3104 Adapter failed to issue "
7060 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7061 goto out_unset_queue;
7062 }
7063 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
1b51197d
JS
7064 /* don't perform init_link on SLI4 FC port loopback test */
7065 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7066 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7067 if (rc)
7068 goto out_unset_queue;
7069 }
5350d872
JS
7070 }
7071 mempool_free(mboxq, phba->mbox_mem_pool);
7072 return rc;
76a95d75 7073out_unset_queue:
da0436e9 7074 /* Unset all the queues set up in this routine when error out */
5350d872
JS
7075 lpfc_sli4_queue_unset(phba);
7076out_destroy_queue:
7077 lpfc_sli4_queue_destroy(phba);
da0436e9 7078out_stop_timers:
5350d872 7079 lpfc_stop_hba_timers(phba);
da0436e9
JS
7080out_free_mbox:
7081 mempool_free(mboxq, phba->mbox_mem_pool);
7082 return rc;
7083}
7084
7085/**
7086 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7087 * @ptr: context object - pointer to hba structure.
7088 *
7089 * This is the callback function for mailbox timer. The mailbox
7090 * timer is armed when a new mailbox command is issued and the timer
7091 * is deleted when the mailbox complete. The function is called by
7092 * the kernel timer code when a mailbox does not complete within
7093 * expected time. This function wakes up the worker thread to
7094 * process the mailbox timeout and returns. All the processing is
7095 * done by the worker thread function lpfc_mbox_timeout_handler.
7096 **/
7097void
7098lpfc_mbox_timeout(unsigned long ptr)
7099{
7100 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
7101 unsigned long iflag;
7102 uint32_t tmo_posted;
7103
7104 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7105 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7106 if (!tmo_posted)
7107 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7108 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7109
7110 if (!tmo_posted)
7111 lpfc_worker_wake_up(phba);
7112 return;
7113}
7114
e8d3c3b1
JS
7115/**
7116 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7117 * are pending
7118 * @phba: Pointer to HBA context object.
7119 *
7120 * This function checks if any mailbox completions are present on the mailbox
7121 * completion queue.
7122 **/
3bb11fc5 7123static bool
e8d3c3b1
JS
7124lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7125{
7126
7127 uint32_t idx;
7128 struct lpfc_queue *mcq;
7129 struct lpfc_mcqe *mcqe;
7130 bool pending_completions = false;
7131
7132 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7133 return false;
7134
7135 /* Check for completions on mailbox completion queue */
7136
7137 mcq = phba->sli4_hba.mbx_cq;
7138 idx = mcq->hba_index;
7139 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7140 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7141 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7142 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7143 pending_completions = true;
7144 break;
7145 }
7146 idx = (idx + 1) % mcq->entry_count;
7147 if (mcq->hba_index == idx)
7148 break;
7149 }
7150 return pending_completions;
7151
7152}
7153
7154/**
7155 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7156 * that were missed.
7157 * @phba: Pointer to HBA context object.
7158 *
7159 * For sli4, it is possible to miss an interrupt. As such mbox completions
7160 * maybe missed causing erroneous mailbox timeouts to occur. This function
7161 * checks to see if mbox completions are on the mailbox completion queue
7162 * and will process all the completions associated with the eq for the
7163 * mailbox completion queue.
7164 **/
7165bool
7166lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7167{
7168
7169 uint32_t eqidx;
7170 struct lpfc_queue *fpeq = NULL;
7171 struct lpfc_eqe *eqe;
7172 bool mbox_pending;
7173
7174 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7175 return false;
7176
7177 /* Find the eq associated with the mcq */
7178
7179 if (phba->sli4_hba.hba_eq)
895427bd 7180 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
e8d3c3b1
JS
7181 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7182 phba->sli4_hba.mbx_cq->assoc_qid) {
7183 fpeq = phba->sli4_hba.hba_eq[eqidx];
7184 break;
7185 }
7186 if (!fpeq)
7187 return false;
7188
7189 /* Turn off interrupts from this EQ */
7190
7191 lpfc_sli4_eq_clr_intr(fpeq);
7192
7193 /* Check to see if a mbox completion is pending */
7194
7195 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7196
7197 /*
7198 * If a mbox completion is pending, process all the events on EQ
7199 * associated with the mbox completion queue (this could include
7200 * mailbox commands, async events, els commands, receive queue data
7201 * and fcp commands)
7202 */
7203
7204 if (mbox_pending)
7205 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7206 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7207 fpeq->EQ_processed++;
7208 }
7209
7210 /* Always clear and re-arm the EQ */
7211
7212 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7213
7214 return mbox_pending;
7215
7216}
da0436e9
JS
7217
7218/**
7219 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7220 * @phba: Pointer to HBA context object.
7221 *
7222 * This function is called from worker thread when a mailbox command times out.
7223 * The caller is not required to hold any locks. This function will reset the
7224 * HBA and recover all the pending commands.
7225 **/
7226void
7227lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7228{
7229 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
eb016566
JS
7230 MAILBOX_t *mb = NULL;
7231
da0436e9 7232 struct lpfc_sli *psli = &phba->sli;
da0436e9 7233
e8d3c3b1
JS
7234 /* If the mailbox completed, process the completion and return */
7235 if (lpfc_sli4_process_missed_mbox_completions(phba))
7236 return;
7237
eb016566
JS
7238 if (pmbox != NULL)
7239 mb = &pmbox->u.mb;
da0436e9
JS
7240 /* Check the pmbox pointer first. There is a race condition
7241 * between the mbox timeout handler getting executed in the
7242 * worklist and the mailbox actually completing. When this
7243 * race condition occurs, the mbox_active will be NULL.
7244 */
7245 spin_lock_irq(&phba->hbalock);
7246 if (pmbox == NULL) {
7247 lpfc_printf_log(phba, KERN_WARNING,
7248 LOG_MBOX | LOG_SLI,
7249 "0353 Active Mailbox cleared - mailbox timeout "
7250 "exiting\n");
7251 spin_unlock_irq(&phba->hbalock);
7252 return;
7253 }
7254
7255 /* Mbox cmd <mbxCommand> timeout */
7256 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7257 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7258 mb->mbxCommand,
7259 phba->pport->port_state,
7260 phba->sli.sli_flag,
7261 phba->sli.mbox_active);
7262 spin_unlock_irq(&phba->hbalock);
7263
7264 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7265 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 7266 * it to fail all outstanding SCSI IO.
da0436e9
JS
7267 */
7268 spin_lock_irq(&phba->pport->work_port_lock);
7269 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7270 spin_unlock_irq(&phba->pport->work_port_lock);
7271 spin_lock_irq(&phba->hbalock);
7272 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 7273 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
7274 spin_unlock_irq(&phba->hbalock);
7275
db55fba8 7276 lpfc_sli_abort_fcp_rings(phba);
da0436e9
JS
7277
7278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7279 "0345 Resetting board due to mailbox timeout\n");
7280
7281 /* Reset the HBA device */
7282 lpfc_reset_hba(phba);
7283}
7284
7285/**
7286 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7287 * @phba: Pointer to HBA context object.
7288 * @pmbox: Pointer to mailbox object.
7289 * @flag: Flag indicating how the mailbox need to be processed.
7290 *
7291 * This function is called by discovery code and HBA management code
7292 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7293 * function gets the hbalock to protect the data structures.
7294 * The mailbox command can be submitted in polling mode, in which case
7295 * this function will wait in a polling loop for the completion of the
7296 * mailbox.
7297 * If the mailbox is submitted in no_wait mode (not polling) the
7298 * function will submit the command and returns immediately without waiting
7299 * for the mailbox completion. The no_wait is supported only when HBA
7300 * is in SLI2/SLI3 mode - interrupts are enabled.
7301 * The SLI interface allows only one mailbox pending at a time. If the
7302 * mailbox is issued in polling mode and there is already a mailbox
7303 * pending, then the function will return an error. If the mailbox is issued
7304 * in NO_WAIT mode and there is a mailbox pending already, the function
7305 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7306 * The sli layer owns the mailbox object until the completion of mailbox
7307 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7308 * return codes the caller owns the mailbox command after the return of
7309 * the function.
e59058c4 7310 **/
3772a991
JS
7311static int
7312lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7313 uint32_t flag)
dea3101e 7314{
bf07bdea 7315 MAILBOX_t *mbx;
2e0fef85 7316 struct lpfc_sli *psli = &phba->sli;
dea3101e 7317 uint32_t status, evtctr;
9940b97b 7318 uint32_t ha_copy, hc_copy;
dea3101e 7319 int i;
09372820 7320 unsigned long timeout;
dea3101e 7321 unsigned long drvr_flag = 0;
34b02dcd 7322 uint32_t word0, ldata;
dea3101e 7323 void __iomem *to_slim;
58da1ffb
JS
7324 int processing_queue = 0;
7325
7326 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7327 if (!pmbox) {
8568a4d2 7328 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 7329 /* processing mbox queue from intr_handler */
3772a991
JS
7330 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7331 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7332 return MBX_SUCCESS;
7333 }
58da1ffb 7334 processing_queue = 1;
58da1ffb
JS
7335 pmbox = lpfc_mbox_get(phba);
7336 if (!pmbox) {
7337 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7338 return MBX_SUCCESS;
7339 }
7340 }
dea3101e 7341
ed957684 7342 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 7343 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 7344 if(!pmbox->vport) {
58da1ffb 7345 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 7346 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 7347 LOG_MBOX | LOG_VPORT,
e8b62011 7348 "1806 Mbox x%x failed. No vport\n",
3772a991 7349 pmbox->u.mb.mbxCommand);
ed957684 7350 dump_stack();
58da1ffb 7351 goto out_not_finished;
ed957684
JS
7352 }
7353 }
7354
8d63f375 7355 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
7356 if (unlikely(pci_channel_offline(phba->pcidev))) {
7357 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7358 goto out_not_finished;
7359 }
8d63f375 7360
a257bf90
JS
7361 /* If HBA has a deferred error attention, fail the iocb. */
7362 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7363 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7364 goto out_not_finished;
7365 }
7366
dea3101e 7367 psli = &phba->sli;
92d7f7b0 7368
bf07bdea 7369 mbx = &pmbox->u.mb;
dea3101e
JB
7370 status = MBX_SUCCESS;
7371
2e0fef85
JS
7372 if (phba->link_state == LPFC_HBA_ERROR) {
7373 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
7374
7375 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7377 "(%d):0311 Mailbox command x%x cannot "
7378 "issue Data: x%x x%x\n",
7379 pmbox->vport ? pmbox->vport->vpi : 0,
7380 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 7381 goto out_not_finished;
41415862
JW
7382 }
7383
bf07bdea 7384 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
9940b97b
JS
7385 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7386 !(hc_copy & HC_MBINT_ENA)) {
7387 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7388 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
7389 "(%d):2528 Mailbox command x%x cannot "
7390 "issue Data: x%x x%x\n",
7391 pmbox->vport ? pmbox->vport->vpi : 0,
7392 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
7393 goto out_not_finished;
7394 }
9290831f
JS
7395 }
7396
dea3101e
JB
7397 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7398 /* Polling for a mbox command when another one is already active
7399 * is not allowed in SLI. Also, the driver must have established
7400 * SLI2 mode to queue and process multiple mbox commands.
7401 */
7402
7403 if (flag & MBX_POLL) {
2e0fef85 7404 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e
JB
7405
7406 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7407 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7408 "(%d):2529 Mailbox command x%x "
7409 "cannot issue Data: x%x x%x\n",
7410 pmbox->vport ? pmbox->vport->vpi : 0,
7411 pmbox->u.mb.mbxCommand,
7412 psli->sli_flag, flag);
58da1ffb 7413 goto out_not_finished;
dea3101e
JB
7414 }
7415
3772a991 7416 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 7417 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7418 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7419 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7420 "(%d):2530 Mailbox command x%x "
7421 "cannot issue Data: x%x x%x\n",
7422 pmbox->vport ? pmbox->vport->vpi : 0,
7423 pmbox->u.mb.mbxCommand,
7424 psli->sli_flag, flag);
58da1ffb 7425 goto out_not_finished;
dea3101e
JB
7426 }
7427
dea3101e
JB
7428 /* Another mailbox command is still being processed, queue this
7429 * command to be processed later.
7430 */
7431 lpfc_mbox_put(phba, pmbox);
7432
7433 /* Mbox cmd issue - BUSY */
ed957684 7434 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7435 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 7436 "x%x x%x x%x x%x\n",
92d7f7b0 7437 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
bf07bdea 7438 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 7439 psli->sli_flag, flag);
dea3101e
JB
7440
7441 psli->slistat.mbox_busy++;
2e0fef85 7442 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7443
858c9f6c
JS
7444 if (pmbox->vport) {
7445 lpfc_debugfs_disc_trc(pmbox->vport,
7446 LPFC_DISC_TRC_MBOX_VPORT,
7447 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7448 (uint32_t)mbx->mbxCommand,
7449 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7450 }
7451 else {
7452 lpfc_debugfs_disc_trc(phba->pport,
7453 LPFC_DISC_TRC_MBOX,
7454 "MBOX Bsy: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7455 (uint32_t)mbx->mbxCommand,
7456 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7457 }
7458
2e0fef85 7459 return MBX_BUSY;
dea3101e
JB
7460 }
7461
dea3101e
JB
7462 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7463
7464 /* If we are not polling, we MUST be in SLI2 mode */
7465 if (flag != MBX_POLL) {
3772a991 7466 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
bf07bdea 7467 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 7468 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7469 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 7470 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
7471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7472 "(%d):2531 Mailbox command x%x "
7473 "cannot issue Data: x%x x%x\n",
7474 pmbox->vport ? pmbox->vport->vpi : 0,
7475 pmbox->u.mb.mbxCommand,
7476 psli->sli_flag, flag);
58da1ffb 7477 goto out_not_finished;
dea3101e
JB
7478 }
7479 /* timeout active mbox command */
256ec0d0
JS
7480 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7481 1000);
7482 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea3101e
JB
7483 }
7484
7485 /* Mailbox cmd <cmd> issue */
ed957684 7486 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 7487 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 7488 "x%x\n",
e8b62011 7489 pmbox->vport ? pmbox->vport->vpi : 0,
bf07bdea 7490 mbx->mbxCommand, phba->pport->port_state,
92d7f7b0 7491 psli->sli_flag, flag);
dea3101e 7492
bf07bdea 7493 if (mbx->mbxCommand != MBX_HEARTBEAT) {
858c9f6c
JS
7494 if (pmbox->vport) {
7495 lpfc_debugfs_disc_trc(pmbox->vport,
7496 LPFC_DISC_TRC_MBOX_VPORT,
7497 "MBOX Send vport: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7498 (uint32_t)mbx->mbxCommand,
7499 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7500 }
7501 else {
7502 lpfc_debugfs_disc_trc(phba->pport,
7503 LPFC_DISC_TRC_MBOX,
7504 "MBOX Send: cmd:x%x mb:x%x x%x",
bf07bdea
RD
7505 (uint32_t)mbx->mbxCommand,
7506 mbx->un.varWords[0], mbx->un.varWords[1]);
858c9f6c
JS
7507 }
7508 }
7509
dea3101e
JB
7510 psli->slistat.mbox_cmd++;
7511 evtctr = psli->slistat.mbox_event;
7512
7513 /* next set own bit for the adapter and copy over command word */
bf07bdea 7514 mbx->mbxOwner = OWN_CHIP;
dea3101e 7515
3772a991 7516 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
7517 /* Populate mbox extension offset word. */
7518 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
bf07bdea 7519 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7520 = (uint8_t *)phba->mbox_ext
7521 - (uint8_t *)phba->mbox;
7522 }
7523
7524 /* Copy the mailbox extension data */
7525 if (pmbox->in_ext_byte_len && pmbox->context2) {
7526 lpfc_sli_pcimem_bcopy(pmbox->context2,
7527 (uint8_t *)phba->mbox_ext,
7528 pmbox->in_ext_byte_len);
7529 }
7530 /* Copy command data to host SLIM area */
bf07bdea 7531 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 7532 } else {
7a470277
JS
7533 /* Populate mbox extension offset word. */
7534 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
bf07bdea 7535 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7a470277
JS
7536 = MAILBOX_HBA_EXT_OFFSET;
7537
7538 /* Copy the mailbox extension data */
895427bd 7539 if (pmbox->in_ext_byte_len && pmbox->context2)
7a470277
JS
7540 lpfc_memcpy_to_slim(phba->MBslimaddr +
7541 MAILBOX_HBA_EXT_OFFSET,
7542 pmbox->context2, pmbox->in_ext_byte_len);
7543
895427bd 7544 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7545 /* copy command data into host mbox for cmpl */
895427bd
JS
7546 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7547 MAILBOX_CMD_SIZE);
dea3101e
JB
7548
7549 /* First copy mbox command data to HBA SLIM, skip past first
7550 word */
7551 to_slim = phba->MBslimaddr + sizeof (uint32_t);
bf07bdea 7552 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea3101e
JB
7553 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7554
7555 /* Next copy over first word, with mbxOwner set */
bf07bdea 7556 ldata = *((uint32_t *)mbx);
dea3101e
JB
7557 to_slim = phba->MBslimaddr;
7558 writel(ldata, to_slim);
7559 readl(to_slim); /* flush */
7560
895427bd 7561 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea3101e 7562 /* switch over to host mailbox */
3772a991 7563 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e
JB
7564 }
7565
7566 wmb();
dea3101e
JB
7567
7568 switch (flag) {
7569 case MBX_NOWAIT:
09372820 7570 /* Set up reference to mailbox command */
dea3101e 7571 psli->mbox_active = pmbox;
09372820
JS
7572 /* Interrupt board to do it */
7573 writel(CA_MBATT, phba->CAregaddr);
7574 readl(phba->CAregaddr); /* flush */
7575 /* Don't wait for it to finish, just return */
dea3101e
JB
7576 break;
7577
7578 case MBX_POLL:
09372820 7579 /* Set up null reference to mailbox command */
dea3101e 7580 psli->mbox_active = NULL;
09372820
JS
7581 /* Interrupt board to do it */
7582 writel(CA_MBATT, phba->CAregaddr);
7583 readl(phba->CAregaddr); /* flush */
7584
3772a991 7585 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7586 /* First read mbox status word */
34b02dcd 7587 word0 = *((uint32_t *)phba->mbox);
dea3101e
JB
7588 word0 = le32_to_cpu(word0);
7589 } else {
7590 /* First read mbox status word */
9940b97b
JS
7591 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7592 spin_unlock_irqrestore(&phba->hbalock,
7593 drvr_flag);
7594 goto out_not_finished;
7595 }
dea3101e
JB
7596 }
7597
7598 /* Read the HBA Host Attention Register */
9940b97b
JS
7599 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7600 spin_unlock_irqrestore(&phba->hbalock,
7601 drvr_flag);
7602 goto out_not_finished;
7603 }
a183a15f
JS
7604 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7605 1000) + jiffies;
09372820 7606 i = 0;
dea3101e 7607 /* Wait for command to complete */
41415862
JW
7608 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7609 (!(ha_copy & HA_MBATT) &&
2e0fef85 7610 (phba->link_state > LPFC_WARM_START))) {
09372820 7611 if (time_after(jiffies, timeout)) {
dea3101e 7612 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 7613 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 7614 drvr_flag);
58da1ffb 7615 goto out_not_finished;
dea3101e
JB
7616 }
7617
7618 /* Check if we took a mbox interrupt while we were
7619 polling */
7620 if (((word0 & OWN_CHIP) != OWN_CHIP)
7621 && (evtctr != psli->slistat.mbox_event))
7622 break;
7623
09372820
JS
7624 if (i++ > 10) {
7625 spin_unlock_irqrestore(&phba->hbalock,
7626 drvr_flag);
7627 msleep(1);
7628 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7629 }
dea3101e 7630
3772a991 7631 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7632 /* First copy command data */
34b02dcd 7633 word0 = *((uint32_t *)phba->mbox);
dea3101e 7634 word0 = le32_to_cpu(word0);
bf07bdea 7635 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 7636 MAILBOX_t *slimmb;
34b02dcd 7637 uint32_t slimword0;
dea3101e
JB
7638 /* Check real SLIM for any errors */
7639 slimword0 = readl(phba->MBslimaddr);
7640 slimmb = (MAILBOX_t *) & slimword0;
7641 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7642 && slimmb->mbxStatus) {
7643 psli->sli_flag &=
3772a991 7644 ~LPFC_SLI_ACTIVE;
dea3101e
JB
7645 word0 = slimword0;
7646 }
7647 }
7648 } else {
7649 /* First copy command data */
7650 word0 = readl(phba->MBslimaddr);
7651 }
7652 /* Read the HBA Host Attention Register */
9940b97b
JS
7653 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7654 spin_unlock_irqrestore(&phba->hbalock,
7655 drvr_flag);
7656 goto out_not_finished;
7657 }
dea3101e
JB
7658 }
7659
3772a991 7660 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 7661 /* copy results back to user */
2ea259ee
JS
7662 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7663 MAILBOX_CMD_SIZE);
7a470277
JS
7664 /* Copy the mailbox extension data */
7665 if (pmbox->out_ext_byte_len && pmbox->context2) {
7666 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7667 pmbox->context2,
7668 pmbox->out_ext_byte_len);
7669 }
dea3101e
JB
7670 } else {
7671 /* First copy command data */
bf07bdea 7672 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
2ea259ee 7673 MAILBOX_CMD_SIZE);
7a470277
JS
7674 /* Copy the mailbox extension data */
7675 if (pmbox->out_ext_byte_len && pmbox->context2) {
7676 lpfc_memcpy_from_slim(pmbox->context2,
7677 phba->MBslimaddr +
7678 MAILBOX_HBA_EXT_OFFSET,
7679 pmbox->out_ext_byte_len);
dea3101e
JB
7680 }
7681 }
7682
7683 writel(HA_MBATT, phba->HAregaddr);
7684 readl(phba->HAregaddr); /* flush */
7685
7686 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
bf07bdea 7687 status = mbx->mbxStatus;
dea3101e
JB
7688 }
7689
2e0fef85
JS
7690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7691 return status;
58da1ffb
JS
7692
7693out_not_finished:
7694 if (processing_queue) {
da0436e9 7695 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
7696 lpfc_mbox_cmpl_put(phba, pmbox);
7697 }
7698 return MBX_NOT_FINISHED;
dea3101e
JB
7699}
7700
f1126688
JS
7701/**
7702 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7703 * @phba: Pointer to HBA context object.
7704 *
7705 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7706 * the driver internal pending mailbox queue. It will then try to wait out the
7707 * possible outstanding mailbox command before return.
7708 *
7709 * Returns:
7710 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7711 * the outstanding mailbox command timed out.
7712 **/
7713static int
7714lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7715{
7716 struct lpfc_sli *psli = &phba->sli;
f1126688 7717 int rc = 0;
a183a15f 7718 unsigned long timeout = 0;
f1126688
JS
7719
7720 /* Mark the asynchronous mailbox command posting as blocked */
7721 spin_lock_irq(&phba->hbalock);
7722 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
7723 /* Determine how long we might wait for the active mailbox
7724 * command to be gracefully completed by firmware.
7725 */
a183a15f
JS
7726 if (phba->sli.mbox_active)
7727 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7728 phba->sli.mbox_active) *
7729 1000) + jiffies;
7730 spin_unlock_irq(&phba->hbalock);
7731
e8d3c3b1
JS
7732 /* Make sure the mailbox is really active */
7733 if (timeout)
7734 lpfc_sli4_process_missed_mbox_completions(phba);
7735
f1126688
JS
7736 /* Wait for the outstnading mailbox command to complete */
7737 while (phba->sli.mbox_active) {
7738 /* Check active mailbox complete status every 2ms */
7739 msleep(2);
7740 if (time_after(jiffies, timeout)) {
7741 /* Timeout, marked the outstanding cmd not complete */
7742 rc = 1;
7743 break;
7744 }
7745 }
7746
7747 /* Can not cleanly block async mailbox command, fails it */
7748 if (rc) {
7749 spin_lock_irq(&phba->hbalock);
7750 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7751 spin_unlock_irq(&phba->hbalock);
7752 }
7753 return rc;
7754}
7755
7756/**
7757 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7758 * @phba: Pointer to HBA context object.
7759 *
7760 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7761 * commands from the driver internal pending mailbox queue. It makes sure
7762 * that there is no outstanding mailbox command before resuming posting
7763 * asynchronous mailbox commands. If, for any reason, there is outstanding
7764 * mailbox command, it will try to wait it out before resuming asynchronous
7765 * mailbox command posting.
7766 **/
7767static void
7768lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7769{
7770 struct lpfc_sli *psli = &phba->sli;
7771
7772 spin_lock_irq(&phba->hbalock);
7773 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7774 /* Asynchronous mailbox posting is not blocked, do nothing */
7775 spin_unlock_irq(&phba->hbalock);
7776 return;
7777 }
7778
7779 /* Outstanding synchronous mailbox command is guaranteed to be done,
7780 * successful or timeout, after timing-out the outstanding mailbox
7781 * command shall always be removed, so just unblock posting async
7782 * mailbox command and resume
7783 */
7784 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7785 spin_unlock_irq(&phba->hbalock);
7786
7787 /* wake up worker thread to post asynchronlous mailbox command */
7788 lpfc_worker_wake_up(phba);
7789}
7790
2d843edc
JS
7791/**
7792 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7793 * @phba: Pointer to HBA context object.
7794 * @mboxq: Pointer to mailbox object.
7795 *
7796 * The function waits for the bootstrap mailbox register ready bit from
7797 * port for twice the regular mailbox command timeout value.
7798 *
7799 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7800 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7801 **/
7802static int
7803lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7804{
7805 uint32_t db_ready;
7806 unsigned long timeout;
7807 struct lpfc_register bmbx_reg;
7808
7809 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7810 * 1000) + jiffies;
7811
7812 do {
7813 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7814 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7815 if (!db_ready)
7816 msleep(2);
7817
7818 if (time_after(jiffies, timeout))
7819 return MBXERR_ERROR;
7820 } while (!db_ready);
7821
7822 return 0;
7823}
7824
da0436e9
JS
7825/**
7826 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7827 * @phba: Pointer to HBA context object.
7828 * @mboxq: Pointer to mailbox object.
7829 *
7830 * The function posts a mailbox to the port. The mailbox is expected
7831 * to be comletely filled in and ready for the port to operate on it.
7832 * This routine executes a synchronous completion operation on the
7833 * mailbox by polling for its completion.
7834 *
7835 * The caller must not be holding any locks when calling this routine.
7836 *
7837 * Returns:
7838 * MBX_SUCCESS - mailbox posted successfully
7839 * Any of the MBX error values.
7840 **/
7841static int
7842lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7843{
7844 int rc = MBX_SUCCESS;
7845 unsigned long iflag;
da0436e9
JS
7846 uint32_t mcqe_status;
7847 uint32_t mbx_cmnd;
da0436e9
JS
7848 struct lpfc_sli *psli = &phba->sli;
7849 struct lpfc_mqe *mb = &mboxq->u.mqe;
7850 struct lpfc_bmbx_create *mbox_rgn;
7851 struct dma_address *dma_address;
da0436e9
JS
7852
7853 /*
7854 * Only one mailbox can be active to the bootstrap mailbox region
7855 * at a time and there is no queueing provided.
7856 */
7857 spin_lock_irqsave(&phba->hbalock, iflag);
7858 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7859 spin_unlock_irqrestore(&phba->hbalock, iflag);
7860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7861 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7862 "cannot issue Data: x%x x%x\n",
7863 mboxq->vport ? mboxq->vport->vpi : 0,
7864 mboxq->u.mb.mbxCommand,
a183a15f
JS
7865 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7866 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7867 psli->sli_flag, MBX_POLL);
7868 return MBXERR_ERROR;
7869 }
7870 /* The server grabs the token and owns it until release */
7871 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7872 phba->sli.mbox_active = mboxq;
7873 spin_unlock_irqrestore(&phba->hbalock, iflag);
7874
2d843edc
JS
7875 /* wait for bootstrap mbox register for readyness */
7876 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7877 if (rc)
7878 goto exit;
7879
da0436e9
JS
7880 /*
7881 * Initialize the bootstrap memory region to avoid stale data areas
7882 * in the mailbox post. Then copy the caller's mailbox contents to
7883 * the bmbx mailbox region.
7884 */
7885 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7886 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7887 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7888 sizeof(struct lpfc_mqe));
7889
7890 /* Post the high mailbox dma address to the port and wait for ready. */
7891 dma_address = &phba->sli4_hba.bmbx.dma_address;
7892 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7893
2d843edc
JS
7894 /* wait for bootstrap mbox register for hi-address write done */
7895 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7896 if (rc)
7897 goto exit;
da0436e9
JS
7898
7899 /* Post the low mailbox dma address to the port. */
7900 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
da0436e9 7901
2d843edc
JS
7902 /* wait for bootstrap mbox register for low address write done */
7903 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7904 if (rc)
7905 goto exit;
da0436e9
JS
7906
7907 /*
7908 * Read the CQ to ensure the mailbox has completed.
7909 * If so, update the mailbox status so that the upper layers
7910 * can complete the request normally.
7911 */
7912 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7913 sizeof(struct lpfc_mqe));
7914 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7915 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7916 sizeof(struct lpfc_mcqe));
7917 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
7918 /*
7919 * When the CQE status indicates a failure and the mailbox status
7920 * indicates success then copy the CQE status into the mailbox status
7921 * (and prefix it with x4000).
7922 */
da0436e9 7923 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
7924 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7925 bf_set(lpfc_mqe_status, mb,
7926 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 7927 rc = MBXERR_ERROR;
d7c47992
JS
7928 } else
7929 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
7930
7931 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7932 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
7933 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7934 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
7935 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7936 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7937 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7938 bf_get(lpfc_mqe_status, mb),
7939 mb->un.mb_words[0], mb->un.mb_words[1],
7940 mb->un.mb_words[2], mb->un.mb_words[3],
7941 mb->un.mb_words[4], mb->un.mb_words[5],
7942 mb->un.mb_words[6], mb->un.mb_words[7],
7943 mb->un.mb_words[8], mb->un.mb_words[9],
7944 mb->un.mb_words[10], mb->un.mb_words[11],
7945 mb->un.mb_words[12], mboxq->mcqe.word0,
7946 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7947 mboxq->mcqe.trailer);
7948exit:
7949 /* We are holding the token, no needed for lock when release */
7950 spin_lock_irqsave(&phba->hbalock, iflag);
7951 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7952 phba->sli.mbox_active = NULL;
7953 spin_unlock_irqrestore(&phba->hbalock, iflag);
7954 return rc;
7955}
7956
7957/**
7958 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7959 * @phba: Pointer to HBA context object.
7960 * @pmbox: Pointer to mailbox object.
7961 * @flag: Flag indicating how the mailbox need to be processed.
7962 *
7963 * This function is called by discovery code and HBA management code to submit
7964 * a mailbox command to firmware with SLI-4 interface spec.
7965 *
7966 * Return codes the caller owns the mailbox command after the return of the
7967 * function.
7968 **/
7969static int
7970lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7971 uint32_t flag)
7972{
7973 struct lpfc_sli *psli = &phba->sli;
7974 unsigned long iflags;
7975 int rc;
7976
b76f2dc9
JS
7977 /* dump from issue mailbox command if setup */
7978 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7979
8fa38513
JS
7980 rc = lpfc_mbox_dev_check(phba);
7981 if (unlikely(rc)) {
7982 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7983 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
7984 "cannot issue Data: x%x x%x\n",
7985 mboxq->vport ? mboxq->vport->vpi : 0,
7986 mboxq->u.mb.mbxCommand,
a183a15f
JS
7987 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7988 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
7989 psli->sli_flag, flag);
7990 goto out_not_finished;
7991 }
7992
da0436e9
JS
7993 /* Detect polling mode and jump to a handler */
7994 if (!phba->sli4_hba.intr_enable) {
7995 if (flag == MBX_POLL)
7996 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7997 else
7998 rc = -EIO;
7999 if (rc != MBX_SUCCESS)
0558056c 8000 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 8001 "(%d):2541 Mailbox command x%x "
cc459f19
JS
8002 "(x%x/x%x) failure: "
8003 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8004 "Data: x%x x%x\n,",
da0436e9
JS
8005 mboxq->vport ? mboxq->vport->vpi : 0,
8006 mboxq->u.mb.mbxCommand,
a183a15f
JS
8007 lpfc_sli_config_mbox_subsys_get(phba,
8008 mboxq),
8009 lpfc_sli_config_mbox_opcode_get(phba,
8010 mboxq),
cc459f19
JS
8011 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8012 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8013 bf_get(lpfc_mcqe_ext_status,
8014 &mboxq->mcqe),
da0436e9
JS
8015 psli->sli_flag, flag);
8016 return rc;
8017 } else if (flag == MBX_POLL) {
f1126688
JS
8018 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8019 "(%d):2542 Try to issue mailbox command "
a183a15f 8020 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 8021 "mailbox command queue: x%x x%x\n",
da0436e9
JS
8022 mboxq->vport ? mboxq->vport->vpi : 0,
8023 mboxq->u.mb.mbxCommand,
a183a15f
JS
8024 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8025 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 8026 psli->sli_flag, flag);
f1126688
JS
8027 /* Try to block the asynchronous mailbox posting */
8028 rc = lpfc_sli4_async_mbox_block(phba);
8029 if (!rc) {
8030 /* Successfully blocked, now issue sync mbox cmd */
8031 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8032 if (rc != MBX_SUCCESS)
cc459f19 8033 lpfc_printf_log(phba, KERN_WARNING,
a183a15f 8034 LOG_MBOX | LOG_SLI,
cc459f19
JS
8035 "(%d):2597 Sync Mailbox command "
8036 "x%x (x%x/x%x) failure: "
8037 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8038 "Data: x%x x%x\n,",
8039 mboxq->vport ? mboxq->vport->vpi : 0,
a183a15f
JS
8040 mboxq->u.mb.mbxCommand,
8041 lpfc_sli_config_mbox_subsys_get(phba,
8042 mboxq),
8043 lpfc_sli_config_mbox_opcode_get(phba,
8044 mboxq),
cc459f19
JS
8045 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8046 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8047 bf_get(lpfc_mcqe_ext_status,
8048 &mboxq->mcqe),
a183a15f 8049 psli->sli_flag, flag);
f1126688
JS
8050 /* Unblock the async mailbox posting afterward */
8051 lpfc_sli4_async_mbox_unblock(phba);
8052 }
8053 return rc;
da0436e9
JS
8054 }
8055
8056 /* Now, interrupt mode asynchrous mailbox command */
8057 rc = lpfc_mbox_cmd_check(phba, mboxq);
8058 if (rc) {
8059 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8060 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8061 "cannot issue Data: x%x x%x\n",
8062 mboxq->vport ? mboxq->vport->vpi : 0,
8063 mboxq->u.mb.mbxCommand,
a183a15f
JS
8064 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8065 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8066 psli->sli_flag, flag);
8067 goto out_not_finished;
8068 }
da0436e9
JS
8069
8070 /* Put the mailbox command to the driver internal FIFO */
8071 psli->slistat.mbox_busy++;
8072 spin_lock_irqsave(&phba->hbalock, iflags);
8073 lpfc_mbox_put(phba, mboxq);
8074 spin_unlock_irqrestore(&phba->hbalock, iflags);
8075 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8076 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 8077 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
8078 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8079 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
8080 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8081 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8082 phba->pport->port_state,
8083 psli->sli_flag, MBX_NOWAIT);
8084 /* Wake up worker thread to transport mailbox command from head */
8085 lpfc_worker_wake_up(phba);
8086
8087 return MBX_BUSY;
8088
8089out_not_finished:
8090 return MBX_NOT_FINISHED;
8091}
8092
8093/**
8094 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8095 * @phba: Pointer to HBA context object.
8096 *
8097 * This function is called by worker thread to send a mailbox command to
8098 * SLI4 HBA firmware.
8099 *
8100 **/
8101int
8102lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8103{
8104 struct lpfc_sli *psli = &phba->sli;
8105 LPFC_MBOXQ_t *mboxq;
8106 int rc = MBX_SUCCESS;
8107 unsigned long iflags;
8108 struct lpfc_mqe *mqe;
8109 uint32_t mbx_cmnd;
8110
8111 /* Check interrupt mode before post async mailbox command */
8112 if (unlikely(!phba->sli4_hba.intr_enable))
8113 return MBX_NOT_FINISHED;
8114
8115 /* Check for mailbox command service token */
8116 spin_lock_irqsave(&phba->hbalock, iflags);
8117 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8118 spin_unlock_irqrestore(&phba->hbalock, iflags);
8119 return MBX_NOT_FINISHED;
8120 }
8121 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8122 spin_unlock_irqrestore(&phba->hbalock, iflags);
8123 return MBX_NOT_FINISHED;
8124 }
8125 if (unlikely(phba->sli.mbox_active)) {
8126 spin_unlock_irqrestore(&phba->hbalock, iflags);
8127 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8128 "0384 There is pending active mailbox cmd\n");
8129 return MBX_NOT_FINISHED;
8130 }
8131 /* Take the mailbox command service token */
8132 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8133
8134 /* Get the next mailbox command from head of queue */
8135 mboxq = lpfc_mbox_get(phba);
8136
8137 /* If no more mailbox command waiting for post, we're done */
8138 if (!mboxq) {
8139 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8140 spin_unlock_irqrestore(&phba->hbalock, iflags);
8141 return MBX_SUCCESS;
8142 }
8143 phba->sli.mbox_active = mboxq;
8144 spin_unlock_irqrestore(&phba->hbalock, iflags);
8145
8146 /* Check device readiness for posting mailbox command */
8147 rc = lpfc_mbox_dev_check(phba);
8148 if (unlikely(rc))
8149 /* Driver clean routine will clean up pending mailbox */
8150 goto out_not_finished;
8151
8152 /* Prepare the mbox command to be posted */
8153 mqe = &mboxq->u.mqe;
8154 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8155
8156 /* Start timer for the mbox_tmo and log some mailbox post messages */
8157 mod_timer(&psli->mbox_tmo, (jiffies +
256ec0d0 8158 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
8159
8160 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 8161 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
8162 "x%x x%x\n",
8163 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
8164 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8165 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8166 phba->pport->port_state, psli->sli_flag);
8167
8168 if (mbx_cmnd != MBX_HEARTBEAT) {
8169 if (mboxq->vport) {
8170 lpfc_debugfs_disc_trc(mboxq->vport,
8171 LPFC_DISC_TRC_MBOX_VPORT,
8172 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8173 mbx_cmnd, mqe->un.mb_words[0],
8174 mqe->un.mb_words[1]);
8175 } else {
8176 lpfc_debugfs_disc_trc(phba->pport,
8177 LPFC_DISC_TRC_MBOX,
8178 "MBOX Send: cmd:x%x mb:x%x x%x",
8179 mbx_cmnd, mqe->un.mb_words[0],
8180 mqe->un.mb_words[1]);
8181 }
8182 }
8183 psli->slistat.mbox_cmd++;
8184
8185 /* Post the mailbox command to the port */
8186 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8187 if (rc != MBX_SUCCESS) {
8188 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 8189 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
8190 "cannot issue Data: x%x x%x\n",
8191 mboxq->vport ? mboxq->vport->vpi : 0,
8192 mboxq->u.mb.mbxCommand,
a183a15f
JS
8193 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8194 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
8195 psli->sli_flag, MBX_NOWAIT);
8196 goto out_not_finished;
8197 }
8198
8199 return rc;
8200
8201out_not_finished:
8202 spin_lock_irqsave(&phba->hbalock, iflags);
d7069f09
JS
8203 if (phba->sli.mbox_active) {
8204 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8205 __lpfc_mbox_cmpl_put(phba, mboxq);
8206 /* Release the token */
8207 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8208 phba->sli.mbox_active = NULL;
8209 }
da0436e9
JS
8210 spin_unlock_irqrestore(&phba->hbalock, iflags);
8211
8212 return MBX_NOT_FINISHED;
8213}
8214
8215/**
8216 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8217 * @phba: Pointer to HBA context object.
8218 * @pmbox: Pointer to mailbox object.
8219 * @flag: Flag indicating how the mailbox need to be processed.
8220 *
8221 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8222 * the API jump table function pointer from the lpfc_hba struct.
8223 *
8224 * Return codes the caller owns the mailbox command after the return of the
8225 * function.
8226 **/
8227int
8228lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8229{
8230 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8231}
8232
8233/**
25985edc 8234 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
8235 * @phba: The hba struct for which this call is being executed.
8236 * @dev_grp: The HBA PCI-Device group number.
8237 *
8238 * This routine sets up the mbox interface API function jump table in @phba
8239 * struct.
8240 * Returns: 0 - success, -ENODEV - failure.
8241 **/
8242int
8243lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8244{
8245
8246 switch (dev_grp) {
8247 case LPFC_PCI_DEV_LP:
8248 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8249 phba->lpfc_sli_handle_slow_ring_event =
8250 lpfc_sli_handle_slow_ring_event_s3;
8251 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8252 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8253 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8254 break;
8255 case LPFC_PCI_DEV_OC:
8256 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8257 phba->lpfc_sli_handle_slow_ring_event =
8258 lpfc_sli_handle_slow_ring_event_s4;
8259 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8260 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8261 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8262 break;
8263 default:
8264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8265 "1420 Invalid HBA PCI-device group: 0x%x\n",
8266 dev_grp);
8267 return -ENODEV;
8268 break;
8269 }
8270 return 0;
8271}
8272
e59058c4 8273/**
3621a710 8274 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
8275 * @phba: Pointer to HBA context object.
8276 * @pring: Pointer to driver SLI ring object.
8277 * @piocb: Pointer to address of newly added command iocb.
8278 *
8279 * This function is called with hbalock held to add a command
8280 * iocb to the txq when SLI layer cannot submit the command iocb
8281 * to the ring.
8282 **/
2a9bf3d0 8283void
92d7f7b0 8284__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8285 struct lpfc_iocbq *piocb)
dea3101e 8286{
1c2ba475 8287 lockdep_assert_held(&phba->hbalock);
dea3101e
JB
8288 /* Insert the caller's iocb in the txq tail for later processing. */
8289 list_add_tail(&piocb->list, &pring->txq);
dea3101e
JB
8290}
8291
e59058c4 8292/**
3621a710 8293 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
8294 * @phba: Pointer to HBA context object.
8295 * @pring: Pointer to driver SLI ring object.
8296 * @piocb: Pointer to address of newly added command iocb.
8297 *
8298 * This function is called with hbalock held before a new
8299 * iocb is submitted to the firmware. This function checks
8300 * txq to flush the iocbs in txq to Firmware before
8301 * submitting new iocbs to the Firmware.
8302 * If there are iocbs in the txq which need to be submitted
8303 * to firmware, lpfc_sli_next_iocb returns the first element
8304 * of the txq after dequeuing it from txq.
8305 * If there is no iocb in the txq then the function will return
8306 * *piocb and *piocb is set to NULL. Caller needs to check
8307 * *piocb to find if there are more commands in the txq.
8308 **/
dea3101e
JB
8309static struct lpfc_iocbq *
8310lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8311 struct lpfc_iocbq **piocb)
dea3101e
JB
8312{
8313 struct lpfc_iocbq * nextiocb;
8314
1c2ba475
JT
8315 lockdep_assert_held(&phba->hbalock);
8316
dea3101e
JB
8317 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8318 if (!nextiocb) {
8319 nextiocb = *piocb;
8320 *piocb = NULL;
8321 }
8322
8323 return nextiocb;
8324}
8325
e59058c4 8326/**
3772a991 8327 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 8328 * @phba: Pointer to HBA context object.
3772a991 8329 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
8330 * @piocb: Pointer to command iocb.
8331 * @flag: Flag indicating if this command can be put into txq.
8332 *
3772a991
JS
8333 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8334 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8335 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8336 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8337 * this function allows only iocbs for posting buffers. This function finds
8338 * next available slot in the command ring and posts the command to the
8339 * available slot and writes the port attention register to request HBA start
8340 * processing new iocb. If there is no slot available in the ring and
8341 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8342 * the function returns IOCB_BUSY.
e59058c4 8343 *
3772a991
JS
8344 * This function is called with hbalock held. The function will return success
8345 * after it successfully submit the iocb to firmware or after adding to the
8346 * txq.
e59058c4 8347 **/
98c9ea5c 8348static int
3772a991 8349__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e
JB
8350 struct lpfc_iocbq *piocb, uint32_t flag)
8351{
8352 struct lpfc_iocbq *nextiocb;
8353 IOCB_t *iocb;
895427bd 8354 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea3101e 8355
1c2ba475
JT
8356 lockdep_assert_held(&phba->hbalock);
8357
92d7f7b0
JS
8358 if (piocb->iocb_cmpl && (!piocb->vport) &&
8359 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8360 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8361 lpfc_printf_log(phba, KERN_ERR,
8362 LOG_SLI | LOG_VPORT,
e8b62011 8363 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
8364 piocb->iocb.ulpCommand);
8365 dump_stack();
8366 return IOCB_ERROR;
8367 }
8368
8369
8d63f375
LV
8370 /* If the PCI channel is in offline state, do not post iocbs. */
8371 if (unlikely(pci_channel_offline(phba->pcidev)))
8372 return IOCB_ERROR;
8373
a257bf90
JS
8374 /* If HBA has a deferred error attention, fail the iocb. */
8375 if (unlikely(phba->hba_flag & DEFER_ERATT))
8376 return IOCB_ERROR;
8377
dea3101e
JB
8378 /*
8379 * We should never get an IOCB if we are in a < LINK_DOWN state
8380 */
2e0fef85 8381 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e
JB
8382 return IOCB_ERROR;
8383
8384 /*
8385 * Check to see if we are blocking IOCB processing because of a
0b727fea 8386 * outstanding event.
dea3101e 8387 */
0b727fea 8388 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e
JB
8389 goto iocb_busy;
8390
2e0fef85 8391 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 8392 /*
2680eeaa 8393 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e
JB
8394 * can be issued if the link is not up.
8395 */
8396 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
8397 case CMD_GEN_REQUEST64_CR:
8398 case CMD_GEN_REQUEST64_CX:
8399 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8400 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 8401 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
8402 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8403 MENLO_TRANSPORT_TYPE))
8404
8405 goto iocb_busy;
8406 break;
dea3101e
JB
8407 case CMD_QUE_RING_BUF_CN:
8408 case CMD_QUE_RING_BUF64_CN:
dea3101e
JB
8409 /*
8410 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8411 * completion, iocb_cmpl MUST be 0.
8412 */
8413 if (piocb->iocb_cmpl)
8414 piocb->iocb_cmpl = NULL;
8415 /*FALLTHROUGH*/
8416 case CMD_CREATE_XRI_CR:
2680eeaa
JS
8417 case CMD_CLOSE_XRI_CN:
8418 case CMD_CLOSE_XRI_CX:
dea3101e
JB
8419 break;
8420 default:
8421 goto iocb_busy;
8422 }
8423
8424 /*
8425 * For FCP commands, we must be in a state where we can process link
8426 * attention events.
8427 */
895427bd 8428 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
92d7f7b0 8429 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 8430 goto iocb_busy;
92d7f7b0 8431 }
dea3101e 8432
dea3101e
JB
8433 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8434 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8435 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8436
8437 if (iocb)
8438 lpfc_sli_update_ring(phba, pring);
8439 else
8440 lpfc_sli_update_full_ring(phba, pring);
8441
8442 if (!piocb)
8443 return IOCB_SUCCESS;
8444
8445 goto out_busy;
8446
8447 iocb_busy:
8448 pring->stats.iocb_cmd_delay++;
8449
8450 out_busy:
8451
8452 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 8453 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e
JB
8454 return IOCB_SUCCESS;
8455 }
8456
8457 return IOCB_BUSY;
8458}
8459
3772a991 8460/**
4f774513
JS
8461 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8462 * @phba: Pointer to HBA context object.
8463 * @piocb: Pointer to command iocb.
8464 * @sglq: Pointer to the scatter gather queue object.
8465 *
8466 * This routine converts the bpl or bde that is in the IOCB
8467 * to a sgl list for the sli4 hardware. The physical address
8468 * of the bpl/bde is converted back to a virtual address.
8469 * If the IOCB contains a BPL then the list of BDE's is
8470 * converted to sli4_sge's. If the IOCB contains a single
8471 * BDE then it is converted to a single sli_sge.
8472 * The IOCB is still in cpu endianess so the contents of
8473 * the bpl can be used without byte swapping.
8474 *
8475 * Returns valid XRI = Success, NO_XRI = Failure.
8476**/
8477static uint16_t
8478lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8479 struct lpfc_sglq *sglq)
3772a991 8480{
4f774513
JS
8481 uint16_t xritag = NO_XRI;
8482 struct ulp_bde64 *bpl = NULL;
8483 struct ulp_bde64 bde;
8484 struct sli4_sge *sgl = NULL;
1b51197d 8485 struct lpfc_dmabuf *dmabuf;
4f774513
JS
8486 IOCB_t *icmd;
8487 int numBdes = 0;
8488 int i = 0;
63e801ce
JS
8489 uint32_t offset = 0; /* accumulated offset in the sg request list */
8490 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 8491
4f774513
JS
8492 if (!piocbq || !sglq)
8493 return xritag;
8494
8495 sgl = (struct sli4_sge *)sglq->sgl;
8496 icmd = &piocbq->iocb;
6b5151fd
JS
8497 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8498 return sglq->sli4_xritag;
4f774513
JS
8499 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8500 numBdes = icmd->un.genreq64.bdl.bdeSize /
8501 sizeof(struct ulp_bde64);
8502 /* The addrHigh and addrLow fields within the IOCB
8503 * have not been byteswapped yet so there is no
8504 * need to swap them back.
8505 */
1b51197d
JS
8506 if (piocbq->context3)
8507 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8508 else
8509 return xritag;
4f774513 8510
1b51197d 8511 bpl = (struct ulp_bde64 *)dmabuf->virt;
4f774513
JS
8512 if (!bpl)
8513 return xritag;
8514
8515 for (i = 0; i < numBdes; i++) {
8516 /* Should already be byte swapped. */
28baac74
JS
8517 sgl->addr_hi = bpl->addrHigh;
8518 sgl->addr_lo = bpl->addrLow;
8519
0558056c 8520 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8521 if ((i+1) == numBdes)
8522 bf_set(lpfc_sli4_sge_last, sgl, 1);
8523 else
8524 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
8525 /* swap the size field back to the cpu so we
8526 * can assign it to the sgl.
8527 */
8528 bde.tus.w = le32_to_cpu(bpl->tus.w);
8529 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
8530 /* The offsets in the sgl need to be accumulated
8531 * separately for the request and reply lists.
8532 * The request is always first, the reply follows.
8533 */
8534 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8535 /* add up the reply sg entries */
8536 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8537 inbound++;
8538 /* first inbound? reset the offset */
8539 if (inbound == 1)
8540 offset = 0;
8541 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
8542 bf_set(lpfc_sli4_sge_type, sgl,
8543 LPFC_SGE_TYPE_DATA);
63e801ce
JS
8544 offset += bde.tus.f.bdeSize;
8545 }
546fc854 8546 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
8547 bpl++;
8548 sgl++;
8549 }
8550 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8551 /* The addrHigh and addrLow fields of the BDE have not
8552 * been byteswapped yet so they need to be swapped
8553 * before putting them in the sgl.
8554 */
8555 sgl->addr_hi =
8556 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8557 sgl->addr_lo =
8558 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 8559 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
8560 bf_set(lpfc_sli4_sge_last, sgl, 1);
8561 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
8562 sgl->sge_len =
8563 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
8564 }
8565 return sglq->sli4_xritag;
3772a991 8566}
92d7f7b0 8567
e59058c4 8568/**
4f774513 8569 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 8570 * @phba: Pointer to HBA context object.
4f774513
JS
8571 * @piocb: Pointer to command iocb.
8572 * @wqe: Pointer to the work queue entry.
e59058c4 8573 *
4f774513
JS
8574 * This routine converts the iocb command to its Work Queue Entry
8575 * equivalent. The wqe pointer should not have any fields set when
8576 * this routine is called because it will memcpy over them.
8577 * This routine does not set the CQ_ID or the WQEC bits in the
8578 * wqe.
e59058c4 8579 *
4f774513 8580 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 8581 **/
cf5bf97e 8582static int
4f774513
JS
8583lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8584 union lpfc_wqe *wqe)
cf5bf97e 8585{
5ffc266e 8586 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
8587 uint8_t ct = 0;
8588 uint32_t fip;
8589 uint32_t abort_tag;
8590 uint8_t command_type = ELS_COMMAND_NON_FIP;
8591 uint8_t cmnd;
8592 uint16_t xritag;
dcf2a4e0
JS
8593 uint16_t abrt_iotag;
8594 struct lpfc_iocbq *abrtiocbq;
4f774513 8595 struct ulp_bde64 *bpl = NULL;
f0d9bccc 8596 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
8597 int numBdes, i;
8598 struct ulp_bde64 bde;
c31098ce 8599 struct lpfc_nodelist *ndlp;
ff78d8f9 8600 uint32_t *pcmd;
1b51197d 8601 uint32_t if_type;
4f774513 8602
45ed1190 8603 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 8604 /* The fcp commands will set command type */
0c287589 8605 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 8606 command_type = FCP_COMMAND;
c868595d 8607 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
8608 command_type = ELS_COMMAND_FIP;
8609 else
8610 command_type = ELS_COMMAND_NON_FIP;
8611
b5c53958
JS
8612 if (phba->fcp_embed_io)
8613 memset(wqe, 0, sizeof(union lpfc_wqe128));
4f774513
JS
8614 /* Some of the fields are in the right position already */
8615 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
f0d9bccc 8616 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
28d7f3df 8617 wqe->generic.wqe_com.word10 = 0;
b5c53958
JS
8618
8619 abort_tag = (uint32_t) iocbq->iotag;
8620 xritag = iocbq->sli4_xritag;
4f774513
JS
8621 /* words0-2 bpl convert bde */
8622 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
8623 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8624 sizeof(struct ulp_bde64);
4f774513
JS
8625 bpl = (struct ulp_bde64 *)
8626 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8627 if (!bpl)
8628 return IOCB_ERROR;
cf5bf97e 8629
4f774513
JS
8630 /* Should already be byte swapped. */
8631 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8632 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8633 /* swap the size field back to the cpu so we
8634 * can assign it to the sgl.
8635 */
8636 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
8637 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8638 total_len = 0;
8639 for (i = 0; i < numBdes; i++) {
8640 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8641 total_len += bde.tus.f.bdeSize;
8642 }
4f774513 8643 } else
5ffc266e 8644 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 8645
4f774513
JS
8646 iocbq->iocb.ulpIoTag = iocbq->iotag;
8647 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 8648
4f774513
JS
8649 switch (iocbq->iocb.ulpCommand) {
8650 case CMD_ELS_REQUEST64_CR:
93d1379e
JS
8651 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8652 ndlp = iocbq->context_un.ndlp;
8653 else
8654 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
8655 if (!iocbq->iocb.ulpLe) {
8656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8657 "2007 Only Limited Edition cmd Format"
8658 " supported 0x%x\n",
8659 iocbq->iocb.ulpCommand);
8660 return IOCB_ERROR;
8661 }
ff78d8f9 8662
5ffc266e 8663 wqe->els_req.payload_len = xmit_len;
4f774513
JS
8664 /* Els_reguest64 has a TMO */
8665 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8666 iocbq->iocb.ulpTimeout);
8667 /* Need a VF for word 4 set the vf bit*/
8668 bf_set(els_req64_vf, &wqe->els_req, 0);
8669 /* And a VFID for word 12 */
8670 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 8671 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
8672 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8673 iocbq->iocb.ulpContext);
8674 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8675 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 8676 /* CCP CCPE PV PRI in word10 were set in the memcpy */
ff78d8f9 8677 if (command_type == ELS_COMMAND_FIP)
c868595d
JS
8678 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8679 >> LPFC_FIP_ELS_ID_SHIFT);
ff78d8f9
JS
8680 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8681 iocbq->context2)->virt);
1b51197d
JS
8682 if_type = bf_get(lpfc_sli_intf_if_type,
8683 &phba->sli4_hba.sli_intf);
8684 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
ff78d8f9 8685 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
cb69f7de 8686 *pcmd == ELS_CMD_SCR ||
6b5151fd 8687 *pcmd == ELS_CMD_FDISC ||
bdcd2b92 8688 *pcmd == ELS_CMD_LOGO ||
ff78d8f9
JS
8689 *pcmd == ELS_CMD_PLOGI)) {
8690 bf_set(els_req64_sp, &wqe->els_req, 1);
8691 bf_set(els_req64_sid, &wqe->els_req,
8692 iocbq->vport->fc_myDID);
939723a4
JS
8693 if ((*pcmd == ELS_CMD_FLOGI) &&
8694 !(phba->fc_topology ==
8695 LPFC_TOPOLOGY_LOOP))
8696 bf_set(els_req64_sid, &wqe->els_req, 0);
ff78d8f9
JS
8697 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8698 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
a7dd9c0f 8699 phba->vpi_ids[iocbq->vport->vpi]);
3ef6d24c 8700 } else if (pcmd && iocbq->context1) {
ff78d8f9
JS
8701 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8702 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8703 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8704 }
c868595d 8705 }
6d368e53
JS
8706 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8707 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
8708 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8709 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8710 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8711 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8712 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8713 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
af22741c 8714 wqe->els_req.max_response_payload_len = total_len - xmit_len;
7851fe2c 8715 break;
5ffc266e 8716 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
8717 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8718 iocbq->iocb.un.ulpWord[3]);
8719 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 8720 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
8721 /* The entire sequence is transmitted for this IOCB */
8722 xmit_len = total_len;
8723 cmnd = CMD_XMIT_SEQUENCE64_CR;
1b51197d
JS
8724 if (phba->link_flag & LS_LOOPBACK_MODE)
8725 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
4f774513 8726 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
8727 /* word3 iocb=io_tag32 wqe=reserved */
8728 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
8729 /* word4 relative_offset memcpy */
8730 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
8731 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8732 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8733 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8734 LPFC_WQE_IOD_WRITE);
8735 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8736 LPFC_WQE_LENLOC_WORD12);
8737 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
8738 wqe->xmit_sequence.xmit_len = xmit_len;
8739 command_type = OTHER_COMMAND;
7851fe2c 8740 break;
4f774513 8741 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
8742 /* word3 iocb=iotag32 wqe=seq_payload_len */
8743 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
8744 /* word4 iocb=rsvd wqe=rsvd */
8745 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8746 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 8747 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 8748 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
8749 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8750 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8751 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8752 LPFC_WQE_LENLOC_WORD3);
8753 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 8754 break;
4f774513
JS
8755 case CMD_FCP_IWRITE64_CR:
8756 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
8757 /* word3 iocb=iotag wqe=payload_offset_len */
8758 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8759 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8760 xmit_len + sizeof(struct fcp_rsp));
8761 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8762 0);
f0d9bccc
JS
8763 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8764 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8765 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8766 iocbq->iocb.ulpFCP2Rcvy);
8767 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8768 /* Always open the exchange */
f0d9bccc
JS
8769 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8770 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8771 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8772 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8773 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
1ba981fd
JS
8774 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8775 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
c92c841c
JS
8776 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8777 if (iocbq->priority) {
8778 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8779 (iocbq->priority << 1));
8780 } else {
1ba981fd
JS
8781 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8782 (phba->cfg_XLanePriority << 1));
8783 }
8784 }
b5c53958
JS
8785 /* Note, word 10 is already initialized to 0 */
8786
8787 if (phba->fcp_embed_io) {
8788 struct lpfc_scsi_buf *lpfc_cmd;
8789 struct sli4_sge *sgl;
8790 union lpfc_wqe128 *wqe128;
8791 struct fcp_cmnd *fcp_cmnd;
8792 uint32_t *ptr;
8793
8794 /* 128 byte wqe support here */
8795 wqe128 = (union lpfc_wqe128 *)wqe;
8796
8797 lpfc_cmd = iocbq->context1;
8798 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8799 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8800
8801 /* Word 0-2 - FCP_CMND */
8802 wqe128->generic.bde.tus.f.bdeFlags =
8803 BUFF_TYPE_BDE_IMMED;
8804 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8805 wqe128->generic.bde.addrHigh = 0;
8806 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8807
8808 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8809
8810 /* Word 22-29 FCP CMND Payload */
8811 ptr = &wqe128->words[22];
8812 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8813 }
7851fe2c 8814 break;
4f774513 8815 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
8816 /* word3 iocb=iotag wqe=payload_offset_len */
8817 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
0ba4b219
JS
8818 bf_set(payload_offset_len, &wqe->fcp_iread,
8819 xmit_len + sizeof(struct fcp_rsp));
8820 bf_set(cmd_buff_len, &wqe->fcp_iread,
8821 0);
f0d9bccc
JS
8822 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8823 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8824 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8825 iocbq->iocb.ulpFCP2Rcvy);
8826 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688 8827 /* Always open the exchange */
f0d9bccc
JS
8828 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8829 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8830 LPFC_WQE_LENLOC_WORD4);
f0d9bccc 8831 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
acd6859b 8832 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
1ba981fd
JS
8833 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8834 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
c92c841c
JS
8835 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8836 if (iocbq->priority) {
8837 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8838 (iocbq->priority << 1));
8839 } else {
1ba981fd
JS
8840 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8841 (phba->cfg_XLanePriority << 1));
8842 }
8843 }
b5c53958
JS
8844 /* Note, word 10 is already initialized to 0 */
8845
8846 if (phba->fcp_embed_io) {
8847 struct lpfc_scsi_buf *lpfc_cmd;
8848 struct sli4_sge *sgl;
8849 union lpfc_wqe128 *wqe128;
8850 struct fcp_cmnd *fcp_cmnd;
8851 uint32_t *ptr;
8852
8853 /* 128 byte wqe support here */
8854 wqe128 = (union lpfc_wqe128 *)wqe;
8855
8856 lpfc_cmd = iocbq->context1;
8857 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8858 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8859
8860 /* Word 0-2 - FCP_CMND */
8861 wqe128->generic.bde.tus.f.bdeFlags =
8862 BUFF_TYPE_BDE_IMMED;
8863 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8864 wqe128->generic.bde.addrHigh = 0;
8865 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8866
8867 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8868
8869 /* Word 22-29 FCP CMND Payload */
8870 ptr = &wqe128->words[22];
8871 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8872 }
7851fe2c 8873 break;
4f774513 8874 case CMD_FCP_ICMND64_CR:
0ba4b219
JS
8875 /* word3 iocb=iotag wqe=payload_offset_len */
8876 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8877 bf_set(payload_offset_len, &wqe->fcp_icmd,
8878 xmit_len + sizeof(struct fcp_rsp));
8879 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8880 0);
f0d9bccc 8881 /* word3 iocb=IO_TAG wqe=reserved */
f0d9bccc 8882 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 8883 /* Always open the exchange */
f0d9bccc
JS
8884 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8885 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8886 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8887 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8888 LPFC_WQE_LENLOC_NONE);
2a94aea4
JS
8889 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8890 iocbq->iocb.ulpFCP2Rcvy);
1ba981fd
JS
8891 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8892 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
c92c841c
JS
8893 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8894 if (iocbq->priority) {
8895 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8896 (iocbq->priority << 1));
8897 } else {
1ba981fd
JS
8898 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8899 (phba->cfg_XLanePriority << 1));
8900 }
8901 }
b5c53958
JS
8902 /* Note, word 10 is already initialized to 0 */
8903
8904 if (phba->fcp_embed_io) {
8905 struct lpfc_scsi_buf *lpfc_cmd;
8906 struct sli4_sge *sgl;
8907 union lpfc_wqe128 *wqe128;
8908 struct fcp_cmnd *fcp_cmnd;
8909 uint32_t *ptr;
8910
8911 /* 128 byte wqe support here */
8912 wqe128 = (union lpfc_wqe128 *)wqe;
8913
8914 lpfc_cmd = iocbq->context1;
8915 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8916 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8917
8918 /* Word 0-2 - FCP_CMND */
8919 wqe128->generic.bde.tus.f.bdeFlags =
8920 BUFF_TYPE_BDE_IMMED;
8921 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8922 wqe128->generic.bde.addrHigh = 0;
8923 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8924
8925 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8926
8927 /* Word 22-29 FCP CMND Payload */
8928 ptr = &wqe128->words[22];
8929 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8930 }
7851fe2c 8931 break;
4f774513 8932 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
8933 /* For this command calculate the xmit length of the
8934 * request bde.
8935 */
8936 xmit_len = 0;
8937 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8938 sizeof(struct ulp_bde64);
8939 for (i = 0; i < numBdes; i++) {
63e801ce 8940 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
8941 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8942 break;
63e801ce
JS
8943 xmit_len += bde.tus.f.bdeSize;
8944 }
f0d9bccc
JS
8945 /* word3 iocb=IO_TAG wqe=request_payload_len */
8946 wqe->gen_req.request_payload_len = xmit_len;
8947 /* word4 iocb=parameter wqe=relative_offset memcpy */
8948 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
8949 /* word6 context tag copied in memcpy */
8950 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8951 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953 "2015 Invalid CT %x command 0x%x\n",
8954 ct, iocbq->iocb.ulpCommand);
8955 return IOCB_ERROR;
8956 }
f0d9bccc
JS
8957 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8958 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8959 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8960 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8961 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8962 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8963 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8964 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
af22741c 8965 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
4f774513 8966 command_type = OTHER_COMMAND;
7851fe2c 8967 break;
4f774513 8968 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 8969 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 8970 /* words0-2 BDE memcpy */
f0d9bccc
JS
8971 /* word3 iocb=iotag32 wqe=response_payload_len */
8972 wqe->xmit_els_rsp.response_payload_len = xmit_len;
939723a4
JS
8973 /* word4 */
8974 wqe->xmit_els_rsp.word4 = 0;
4f774513
JS
8975 /* word5 iocb=rsvd wge=did */
8976 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
939723a4
JS
8977 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8978
8979 if_type = bf_get(lpfc_sli_intf_if_type,
8980 &phba->sli4_hba.sli_intf);
8981 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8982 if (iocbq->vport->fc_flag & FC_PT2PT) {
8983 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8984 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8985 iocbq->vport->fc_myDID);
8986 if (iocbq->vport->fc_myDID == Fabric_DID) {
8987 bf_set(wqe_els_did,
8988 &wqe->xmit_els_rsp.wqe_dest, 0);
8989 }
8990 }
8991 }
f0d9bccc
JS
8992 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8993 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8994 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8995 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 8996 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 8997 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 8998 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 8999 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
9000 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9001 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9002 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9003 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9004 LPFC_WQE_LENLOC_WORD3);
9005 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
9006 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9007 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
ff78d8f9
JS
9008 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9009 iocbq->context2)->virt);
9010 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
939723a4
JS
9011 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9012 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
ff78d8f9 9013 iocbq->vport->fc_myDID);
939723a4
JS
9014 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9015 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
ff78d8f9
JS
9016 phba->vpi_ids[phba->pport->vpi]);
9017 }
4f774513 9018 command_type = OTHER_COMMAND;
7851fe2c 9019 break;
4f774513
JS
9020 case CMD_CLOSE_XRI_CN:
9021 case CMD_ABORT_XRI_CN:
9022 case CMD_ABORT_XRI_CX:
9023 /* words 0-2 memcpy should be 0 rserved */
9024 /* port will send abts */
dcf2a4e0
JS
9025 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9026 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9027 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9028 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9029 } else
9030 fip = 0;
9031
9032 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 9033 /*
dcf2a4e0
JS
9034 * The link is down, or the command was ELS_FIP
9035 * so the fw does not need to send abts
4f774513
JS
9036 * on the wire.
9037 */
9038 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9039 else
9040 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9041 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
9042 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9043 wqe->abort_cmd.rsrvd5 = 0;
9044 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
9045 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9046 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
9047 /*
9048 * The abort handler will send us CMD_ABORT_XRI_CN or
9049 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9050 */
f0d9bccc
JS
9051 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9052 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9053 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9054 LPFC_WQE_LENLOC_NONE);
4f774513
JS
9055 cmnd = CMD_ABORT_XRI_CX;
9056 command_type = OTHER_COMMAND;
9057 xritag = 0;
7851fe2c 9058 break;
6669f9bb 9059 case CMD_XMIT_BLS_RSP64_CX:
6b5151fd 9060 ndlp = (struct lpfc_nodelist *)iocbq->context1;
546fc854 9061 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
9062 * we re-construct this WQE here based on information in
9063 * iocbq from scratch.
9064 */
9065 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 9066 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 9067 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
9068 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9069 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
9070 LPFC_ABTS_UNSOL_INT) {
9071 /* ABTS sent by initiator to CT exchange, the
9072 * RX_ID field will be filled with the newly
9073 * allocated responder XRI.
9074 */
9075 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9076 iocbq->sli4_xritag);
9077 } else {
9078 /* ABTS sent by responder to CT exchange, the
9079 * RX_ID field will be filled with the responder
9080 * RX_ID from ABTS.
9081 */
9082 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 9083 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 9084 }
6669f9bb
JS
9085 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9086 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6b5151fd
JS
9087
9088 /* Use CT=VPI */
9089 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9090 ndlp->nlp_DID);
9091 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9092 iocbq->iocb.ulpContext);
9093 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
6669f9bb 9094 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6b5151fd 9095 phba->vpi_ids[phba->pport->vpi]);
f0d9bccc
JS
9096 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9097 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9098 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
9099 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9100 command_type = OTHER_COMMAND;
546fc854
JS
9101 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9102 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9103 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9104 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9105 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9106 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9107 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9108 }
9109
7851fe2c 9110 break;
4f774513
JS
9111 case CMD_XRI_ABORTED_CX:
9112 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
9113 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9114 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9115 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9116 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9117 default:
9118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9119 "2014 Invalid command 0x%x\n",
9120 iocbq->iocb.ulpCommand);
9121 return IOCB_ERROR;
7851fe2c 9122 break;
4f774513 9123 }
6d368e53 9124
8012cc38
JS
9125 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9126 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9127 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9128 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9129 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9130 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9131 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9132 LPFC_IO_DIF_INSERT);
f0d9bccc
JS
9133 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9134 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9135 wqe->generic.wqe_com.abort_tag = abort_tag;
9136 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9137 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9138 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9139 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
9140 return 0;
9141}
9142
9143/**
9144 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9145 * @phba: Pointer to HBA context object.
9146 * @ring_number: SLI ring number to issue iocb on.
9147 * @piocb: Pointer to command iocb.
9148 * @flag: Flag indicating if this command can be put into txq.
9149 *
9150 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9151 * an iocb command to an HBA with SLI-4 interface spec.
9152 *
9153 * This function is called with hbalock held. The function will return success
9154 * after it successfully submit the iocb to firmware or after adding to the
9155 * txq.
9156 **/
9157static int
9158__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9159 struct lpfc_iocbq *piocb, uint32_t flag)
9160{
9161 struct lpfc_sglq *sglq;
b5c53958
JS
9162 union lpfc_wqe *wqe;
9163 union lpfc_wqe128 wqe128;
1ba981fd 9164 struct lpfc_queue *wq;
895427bd 9165 struct lpfc_sli_ring *pring;
4f774513 9166
895427bd
JS
9167 /* Get the WQ */
9168 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9169 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9170 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9171 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9172 else
9173 wq = phba->sli4_hba.oas_wq;
9174 } else {
9175 wq = phba->sli4_hba.els_wq;
9176 }
9177
9178 /* Get corresponding ring */
9179 pring = wq->pring;
1c2ba475 9180
b5c53958
JS
9181 /*
9182 * The WQE can be either 64 or 128 bytes,
9183 * so allocate space on the stack assuming the largest.
9184 */
9185 wqe = (union lpfc_wqe *)&wqe128;
9186
895427bd
JS
9187 lockdep_assert_held(&phba->hbalock);
9188
4f774513
JS
9189 if (piocb->sli4_xritag == NO_XRI) {
9190 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6b5151fd 9191 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
9192 sglq = NULL;
9193 else {
0e9bb8d7 9194 if (!list_empty(&pring->txq)) {
2a9bf3d0
JS
9195 if (!(flag & SLI_IOCB_RET_IOCB)) {
9196 __lpfc_sli_ringtx_put(phba,
9197 pring, piocb);
9198 return IOCB_SUCCESS;
9199 } else {
9200 return IOCB_BUSY;
9201 }
9202 } else {
895427bd 9203 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
2a9bf3d0
JS
9204 if (!sglq) {
9205 if (!(flag & SLI_IOCB_RET_IOCB)) {
9206 __lpfc_sli_ringtx_put(phba,
9207 pring,
9208 piocb);
9209 return IOCB_SUCCESS;
9210 } else
9211 return IOCB_BUSY;
9212 }
9213 }
4f774513 9214 }
2ea259ee 9215 } else if (piocb->iocb_flag & LPFC_IO_FCP)
6d368e53
JS
9216 /* These IO's already have an XRI and a mapped sgl. */
9217 sglq = NULL;
2ea259ee 9218 else {
6d368e53
JS
9219 /*
9220 * This is a continuation of a commandi,(CX) so this
4f774513
JS
9221 * sglq is on the active list
9222 */
edccdc17 9223 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
4f774513
JS
9224 if (!sglq)
9225 return IOCB_ERROR;
9226 }
9227
9228 if (sglq) {
6d368e53 9229 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 9230 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 9231 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
9232 return IOCB_ERROR;
9233 }
9234
b5c53958 9235 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
4f774513
JS
9236 return IOCB_ERROR;
9237
895427bd
JS
9238 if (lpfc_sli4_wq_put(wq, wqe))
9239 return IOCB_ERROR;
4f774513
JS
9240 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9241
9242 return 0;
9243}
9244
9245/**
9246 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9247 *
9248 * This routine wraps the actual lockless version for issusing IOCB function
9249 * pointer from the lpfc_hba struct.
9250 *
9251 * Return codes:
b5c53958
JS
9252 * IOCB_ERROR - Error
9253 * IOCB_SUCCESS - Success
9254 * IOCB_BUSY - Busy
4f774513 9255 **/
2a9bf3d0 9256int
4f774513
JS
9257__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9258 struct lpfc_iocbq *piocb, uint32_t flag)
9259{
9260 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9261}
9262
9263/**
25985edc 9264 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
9265 * @phba: The hba struct for which this call is being executed.
9266 * @dev_grp: The HBA PCI-Device group number.
9267 *
9268 * This routine sets up the SLI interface API function jump table in @phba
9269 * struct.
9270 * Returns: 0 - success, -ENODEV - failure.
9271 **/
9272int
9273lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9274{
9275
9276 switch (dev_grp) {
9277 case LPFC_PCI_DEV_LP:
9278 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9279 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9280 break;
9281 case LPFC_PCI_DEV_OC:
9282 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9283 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9284 break;
9285 default:
9286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9287 "1419 Invalid HBA PCI-device group: 0x%x\n",
9288 dev_grp);
9289 return -ENODEV;
9290 break;
9291 }
9292 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9293 return 0;
9294}
9295
a1efe163 9296/**
895427bd 9297 * lpfc_sli4_calc_ring - Calculates which ring to use
a1efe163 9298 * @phba: Pointer to HBA context object.
a1efe163
JS
9299 * @piocb: Pointer to command iocb.
9300 *
895427bd
JS
9301 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9302 * hba_wqidx, thus we need to calculate the corresponding ring.
a1efe163 9303 * Since ABORTS must go on the same WQ of the command they are
895427bd 9304 * aborting, we use command's hba_wqidx.
a1efe163 9305 */
895427bd
JS
9306struct lpfc_sli_ring *
9307lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9bd2bff5 9308{
895427bd 9309 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8b0dff14 9310 if (!(phba->cfg_fof) ||
895427bd 9311 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8b0dff14 9312 if (unlikely(!phba->sli4_hba.fcp_wq))
895427bd 9313 return NULL;
8b0dff14 9314 /*
895427bd 9315 * for abort iocb hba_wqidx should already
8b0dff14
JS
9316 * be setup based on what work queue we used.
9317 */
9318 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
895427bd 9319 piocb->hba_wqidx =
8b0dff14
JS
9320 lpfc_sli4_scmd_to_wqidx_distr(phba,
9321 piocb->context1);
895427bd 9322 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
8b0dff14
JS
9323 } else {
9324 if (unlikely(!phba->sli4_hba.oas_wq))
895427bd
JS
9325 return NULL;
9326 piocb->hba_wqidx = 0;
9327 return phba->sli4_hba.oas_wq->pring;
9bd2bff5 9328 }
895427bd
JS
9329 } else {
9330 if (unlikely(!phba->sli4_hba.els_wq))
9331 return NULL;
9332 piocb->hba_wqidx = 0;
9333 return phba->sli4_hba.els_wq->pring;
9bd2bff5 9334 }
9bd2bff5
JS
9335}
9336
4f774513
JS
9337/**
9338 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9339 * @phba: Pointer to HBA context object.
9340 * @pring: Pointer to driver SLI ring object.
9341 * @piocb: Pointer to command iocb.
9342 * @flag: Flag indicating if this command can be put into txq.
9343 *
9344 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9345 * function. This function gets the hbalock and calls
9346 * __lpfc_sli_issue_iocb function and will return the error returned
9347 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9348 * functions which do not hold hbalock.
9349 **/
9350int
9351lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9352 struct lpfc_iocbq *piocb, uint32_t flag)
9353{
895427bd 9354 struct lpfc_hba_eq_hdl *hba_eq_hdl;
2a76a283 9355 struct lpfc_sli_ring *pring;
ba20c853
JS
9356 struct lpfc_queue *fpeq;
9357 struct lpfc_eqe *eqe;
4f774513 9358 unsigned long iflags;
2a76a283 9359 int rc, idx;
4f774513 9360
7e56aa25 9361 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
9362 pring = lpfc_sli4_calc_ring(phba, piocb);
9363 if (unlikely(pring == NULL))
9bd2bff5 9364 return IOCB_ERROR;
ba20c853 9365
9bd2bff5
JS
9366 spin_lock_irqsave(&pring->ring_lock, iflags);
9367 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9368 spin_unlock_irqrestore(&pring->ring_lock, iflags);
ba20c853 9369
9bd2bff5 9370 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
895427bd
JS
9371 idx = piocb->hba_wqidx;
9372 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
4f774513 9373
895427bd 9374 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
ba20c853 9375
9bd2bff5
JS
9376 /* Get associated EQ with this index */
9377 fpeq = phba->sli4_hba.hba_eq[idx];
ba20c853 9378
9bd2bff5
JS
9379 /* Turn off interrupts from this EQ */
9380 lpfc_sli4_eq_clr_intr(fpeq);
ba20c853 9381
9bd2bff5
JS
9382 /*
9383 * Process all the events on FCP EQ
9384 */
9385 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9386 lpfc_sli4_hba_handle_eqe(phba,
9387 eqe, idx);
9388 fpeq->EQ_processed++;
ba20c853 9389 }
ba20c853 9390
9bd2bff5
JS
9391 /* Always clear and re-arm the EQ */
9392 lpfc_sli4_eq_release(fpeq,
9393 LPFC_QUEUE_REARM);
9394 }
895427bd 9395 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
2a76a283 9396 }
7e56aa25
JS
9397 } else {
9398 /* For now, SLI2/3 will still use hbalock */
9399 spin_lock_irqsave(&phba->hbalock, iflags);
9400 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9401 spin_unlock_irqrestore(&phba->hbalock, iflags);
9402 }
4f774513
JS
9403 return rc;
9404}
9405
9406/**
9407 * lpfc_extra_ring_setup - Extra ring setup function
9408 * @phba: Pointer to HBA context object.
9409 *
9410 * This function is called while driver attaches with the
9411 * HBA to setup the extra ring. The extra ring is used
9412 * only when driver needs to support target mode functionality
9413 * or IP over FC functionalities.
9414 *
895427bd 9415 * This function is called with no lock held. SLI3 only.
4f774513
JS
9416 **/
9417static int
9418lpfc_extra_ring_setup( struct lpfc_hba *phba)
9419{
9420 struct lpfc_sli *psli;
9421 struct lpfc_sli_ring *pring;
9422
9423 psli = &phba->sli;
9424
9425 /* Adjust cmd/rsp ring iocb entries more evenly */
9426
9427 /* Take some away from the FCP ring */
895427bd 9428 pring = &psli->sli3_ring[LPFC_FCP_RING];
7e56aa25
JS
9429 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9430 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9431 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9432 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e 9433
a4bc3379 9434 /* and give them to the extra ring */
895427bd 9435 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
a4bc3379 9436
7e56aa25
JS
9437 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9438 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9439 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9440 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
cf5bf97e
JW
9441
9442 /* Setup default profile for this ring */
9443 pring->iotag_max = 4096;
9444 pring->num_mask = 1;
9445 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
9446 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9447 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
9448 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9449 return 0;
9450}
9451
cb69f7de
JS
9452/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9453 * @phba: Pointer to HBA context object.
9454 * @iocbq: Pointer to iocb object.
9455 *
9456 * The async_event handler calls this routine when it receives
9457 * an ASYNC_STATUS_CN event from the port. The port generates
9458 * this event when an Abort Sequence request to an rport fails
9459 * twice in succession. The abort could be originated by the
9460 * driver or by the port. The ABTS could have been for an ELS
9461 * or FCP IO. The port only generates this event when an ABTS
9462 * fails to complete after one retry.
9463 */
9464static void
9465lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9466 struct lpfc_iocbq *iocbq)
9467{
9468 struct lpfc_nodelist *ndlp = NULL;
9469 uint16_t rpi = 0, vpi = 0;
9470 struct lpfc_vport *vport = NULL;
9471
9472 /* The rpi in the ulpContext is vport-sensitive. */
9473 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9474 rpi = iocbq->iocb.ulpContext;
9475
9476 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9477 "3092 Port generated ABTS async event "
9478 "on vpi %d rpi %d status 0x%x\n",
9479 vpi, rpi, iocbq->iocb.ulpStatus);
9480
9481 vport = lpfc_find_vport_by_vpid(phba, vpi);
9482 if (!vport)
9483 goto err_exit;
9484 ndlp = lpfc_findnode_rpi(vport, rpi);
9485 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9486 goto err_exit;
9487
9488 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9489 lpfc_sli_abts_recover_port(vport, ndlp);
9490 return;
9491
9492 err_exit:
9493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9494 "3095 Event Context not found, no "
9495 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9496 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9497 vpi, rpi);
9498}
9499
9500/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9501 * @phba: pointer to HBA context object.
9502 * @ndlp: nodelist pointer for the impacted rport.
9503 * @axri: pointer to the wcqe containing the failed exchange.
9504 *
9505 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9506 * port. The port generates this event when an abort exchange request to an
9507 * rport fails twice in succession with no reply. The abort could be originated
9508 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9509 */
9510void
9511lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9512 struct lpfc_nodelist *ndlp,
9513 struct sli4_wcqe_xri_aborted *axri)
9514{
9515 struct lpfc_vport *vport;
5c1db2ac 9516 uint32_t ext_status = 0;
cb69f7de 9517
6b5151fd 9518 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
cb69f7de
JS
9519 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9520 "3115 Node Context not found, driver "
9521 "ignoring abts err event\n");
6b5151fd
JS
9522 return;
9523 }
9524
cb69f7de
JS
9525 vport = ndlp->vport;
9526 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9527 "3116 Port generated FCP XRI ABORT event on "
5c1db2ac 9528 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8e668af5 9529 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
cb69f7de 9530 bf_get(lpfc_wcqe_xa_xri, axri),
5c1db2ac
JS
9531 bf_get(lpfc_wcqe_xa_status, axri),
9532 axri->parameter);
cb69f7de 9533
5c1db2ac
JS
9534 /*
9535 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9536 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9537 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9538 */
e3d2b802 9539 ext_status = axri->parameter & IOERR_PARAM_MASK;
5c1db2ac
JS
9540 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9541 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
cb69f7de
JS
9542 lpfc_sli_abts_recover_port(vport, ndlp);
9543}
9544
e59058c4 9545/**
3621a710 9546 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
9547 * @phba: Pointer to HBA context object.
9548 * @pring: Pointer to driver SLI ring object.
9549 * @iocbq: Pointer to iocb object.
9550 *
9551 * This function is called by the slow ring event handler
9552 * function when there is an ASYNC event iocb in the ring.
9553 * This function is called with no lock held.
9554 * Currently this function handles only temperature related
9555 * ASYNC events. The function decodes the temperature sensor
9556 * event message and posts events for the management applications.
9557 **/
98c9ea5c 9558static void
57127f15
JS
9559lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9560 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9561{
9562 IOCB_t *icmd;
9563 uint16_t evt_code;
57127f15
JS
9564 struct temp_event temp_event_data;
9565 struct Scsi_Host *shost;
a257bf90 9566 uint32_t *iocb_w;
57127f15
JS
9567
9568 icmd = &iocbq->iocb;
9569 evt_code = icmd->un.asyncstat.evt_code;
57127f15 9570
cb69f7de
JS
9571 switch (evt_code) {
9572 case ASYNC_TEMP_WARN:
9573 case ASYNC_TEMP_SAFE:
9574 temp_event_data.data = (uint32_t) icmd->ulpContext;
9575 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9576 if (evt_code == ASYNC_TEMP_WARN) {
9577 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9578 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9579 "0347 Adapter is very hot, please take "
9580 "corrective action. temperature : %d Celsius\n",
9581 (uint32_t) icmd->ulpContext);
9582 } else {
9583 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9584 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9585 "0340 Adapter temperature is OK now. "
9586 "temperature : %d Celsius\n",
9587 (uint32_t) icmd->ulpContext);
9588 }
9589
9590 /* Send temperature change event to applications */
9591 shost = lpfc_shost_from_vport(phba->pport);
9592 fc_host_post_vendor_event(shost, fc_get_event_number(),
9593 sizeof(temp_event_data), (char *) &temp_event_data,
9594 LPFC_NL_VENDOR_ID);
9595 break;
9596 case ASYNC_STATUS_CN:
9597 lpfc_sli_abts_err_handler(phba, iocbq);
9598 break;
9599 default:
a257bf90 9600 iocb_w = (uint32_t *) icmd;
cb69f7de 9601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
76bb24ef 9602 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 9603 " evt_code 0x%x\n"
a257bf90
JS
9604 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9605 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9606 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9607 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
cb69f7de 9608 pring->ringno, icmd->un.asyncstat.evt_code,
a257bf90
JS
9609 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9610 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9611 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9612 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9613
cb69f7de 9614 break;
57127f15 9615 }
57127f15
JS
9616}
9617
9618
e59058c4 9619/**
895427bd 9620 * lpfc_sli4_setup - SLI ring setup function
e59058c4
JS
9621 * @phba: Pointer to HBA context object.
9622 *
9623 * lpfc_sli_setup sets up rings of the SLI interface with
9624 * number of iocbs per ring and iotags. This function is
9625 * called while driver attach to the HBA and before the
9626 * interrupts are enabled. So there is no need for locking.
9627 *
9628 * This function always returns 0.
9629 **/
dea3101e 9630int
895427bd
JS
9631lpfc_sli4_setup(struct lpfc_hba *phba)
9632{
9633 struct lpfc_sli_ring *pring;
9634
9635 pring = phba->sli4_hba.els_wq->pring;
9636 pring->num_mask = LPFC_MAX_RING_MASK;
9637 pring->prt[0].profile = 0; /* Mask 0 */
9638 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9639 pring->prt[0].type = FC_TYPE_ELS;
9640 pring->prt[0].lpfc_sli_rcv_unsol_event =
9641 lpfc_els_unsol_event;
9642 pring->prt[1].profile = 0; /* Mask 1 */
9643 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9644 pring->prt[1].type = FC_TYPE_ELS;
9645 pring->prt[1].lpfc_sli_rcv_unsol_event =
9646 lpfc_els_unsol_event;
9647 pring->prt[2].profile = 0; /* Mask 2 */
9648 /* NameServer Inquiry */
9649 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9650 /* NameServer */
9651 pring->prt[2].type = FC_TYPE_CT;
9652 pring->prt[2].lpfc_sli_rcv_unsol_event =
9653 lpfc_ct_unsol_event;
9654 pring->prt[3].profile = 0; /* Mask 3 */
9655 /* NameServer response */
9656 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9657 /* NameServer */
9658 pring->prt[3].type = FC_TYPE_CT;
9659 pring->prt[3].lpfc_sli_rcv_unsol_event =
9660 lpfc_ct_unsol_event;
9661 return 0;
9662}
9663
9664/**
9665 * lpfc_sli_setup - SLI ring setup function
9666 * @phba: Pointer to HBA context object.
9667 *
9668 * lpfc_sli_setup sets up rings of the SLI interface with
9669 * number of iocbs per ring and iotags. This function is
9670 * called while driver attach to the HBA and before the
9671 * interrupts are enabled. So there is no need for locking.
9672 *
9673 * This function always returns 0. SLI3 only.
9674 **/
9675int
dea3101e
JB
9676lpfc_sli_setup(struct lpfc_hba *phba)
9677{
ed957684 9678 int i, totiocbsize = 0;
dea3101e
JB
9679 struct lpfc_sli *psli = &phba->sli;
9680 struct lpfc_sli_ring *pring;
9681
2a76a283 9682 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea3101e 9683 psli->sli_flag = 0;
dea3101e 9684
604a3e30
JB
9685 psli->iocbq_lookup = NULL;
9686 psli->iocbq_lookup_len = 0;
9687 psli->last_iotag = 0;
9688
dea3101e 9689 for (i = 0; i < psli->num_rings; i++) {
895427bd 9690 pring = &psli->sli3_ring[i];
dea3101e
JB
9691 switch (i) {
9692 case LPFC_FCP_RING: /* ring 0 - FCP */
9693 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9694 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9695 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9696 pring->sli.sli3.numCiocb +=
9697 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9698 pring->sli.sli3.numRiocb +=
9699 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9700 pring->sli.sli3.numCiocb +=
9701 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9702 pring->sli.sli3.numRiocb +=
9703 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9704 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9705 SLI3_IOCB_CMD_SIZE :
9706 SLI2_IOCB_CMD_SIZE;
7e56aa25 9707 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9708 SLI3_IOCB_RSP_SIZE :
9709 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9710 pring->iotag_ctr = 0;
9711 pring->iotag_max =
92d7f7b0 9712 (phba->cfg_hba_queue_depth * 2);
dea3101e
JB
9713 pring->fast_iotag = pring->iotag_max;
9714 pring->num_mask = 0;
9715 break;
a4bc3379 9716 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 9717 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9718 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9719 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9720 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9721 SLI3_IOCB_CMD_SIZE :
9722 SLI2_IOCB_CMD_SIZE;
7e56aa25 9723 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9724 SLI3_IOCB_RSP_SIZE :
9725 SLI2_IOCB_RSP_SIZE;
2e0fef85 9726 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e
JB
9727 pring->num_mask = 0;
9728 break;
9729 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9730 /* numCiocb and numRiocb are used in config_port */
7e56aa25
JS
9731 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9732 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9733 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9734 SLI3_IOCB_CMD_SIZE :
9735 SLI2_IOCB_CMD_SIZE;
7e56aa25 9736 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
9737 SLI3_IOCB_RSP_SIZE :
9738 SLI2_IOCB_RSP_SIZE;
dea3101e
JB
9739 pring->fast_iotag = 0;
9740 pring->iotag_ctr = 0;
9741 pring->iotag_max = 4096;
57127f15
JS
9742 pring->lpfc_sli_rcv_async_status =
9743 lpfc_sli_async_event_handler;
6669f9bb 9744 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 9745 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
9746 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9747 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 9748 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 9749 lpfc_els_unsol_event;
dea3101e 9750 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
9751 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9752 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 9753 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 9754 lpfc_els_unsol_event;
dea3101e
JB
9755 pring->prt[2].profile = 0; /* Mask 2 */
9756 /* NameServer Inquiry */
6a9c52cf 9757 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 9758 /* NameServer */
6a9c52cf 9759 pring->prt[2].type = FC_TYPE_CT;
dea3101e 9760 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 9761 lpfc_ct_unsol_event;
dea3101e
JB
9762 pring->prt[3].profile = 0; /* Mask 3 */
9763 /* NameServer response */
6a9c52cf 9764 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 9765 /* NameServer */
6a9c52cf 9766 pring->prt[3].type = FC_TYPE_CT;
dea3101e 9767 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 9768 lpfc_ct_unsol_event;
dea3101e
JB
9769 break;
9770 }
7e56aa25
JS
9771 totiocbsize += (pring->sli.sli3.numCiocb *
9772 pring->sli.sli3.sizeCiocb) +
9773 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea3101e 9774 }
ed957684 9775 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 9776 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
9777 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9778 "SLI2 SLIM Data: x%x x%lx\n",
9779 phba->brd_no, totiocbsize,
9780 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 9781 }
cf5bf97e
JW
9782 if (phba->cfg_multi_ring_support == 2)
9783 lpfc_extra_ring_setup(phba);
dea3101e
JB
9784
9785 return 0;
9786}
9787
e59058c4 9788/**
895427bd 9789 * lpfc_sli4_queue_init - Queue initialization function
e59058c4
JS
9790 * @phba: Pointer to HBA context object.
9791 *
895427bd 9792 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
e59058c4
JS
9793 * ring. This function also initializes ring indices of each ring.
9794 * This function is called during the initialization of the SLI
9795 * interface of an HBA.
9796 * This function is called with no lock held and always returns
9797 * 1.
9798 **/
895427bd
JS
9799void
9800lpfc_sli4_queue_init(struct lpfc_hba *phba)
dea3101e
JB
9801{
9802 struct lpfc_sli *psli;
9803 struct lpfc_sli_ring *pring;
604a3e30 9804 int i;
dea3101e
JB
9805
9806 psli = &phba->sli;
2e0fef85 9807 spin_lock_irq(&phba->hbalock);
dea3101e 9808 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 9809 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 9810 /* Initialize list headers for txq and txcmplq as double linked lists */
895427bd
JS
9811 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9812 pring = phba->sli4_hba.fcp_wq[i]->pring;
68e814f5 9813 pring->flag = 0;
895427bd 9814 pring->ringno = LPFC_FCP_RING;
dea3101e
JB
9815 INIT_LIST_HEAD(&pring->txq);
9816 INIT_LIST_HEAD(&pring->txcmplq);
9817 INIT_LIST_HEAD(&pring->iocb_continueq);
7e56aa25 9818 spin_lock_init(&pring->ring_lock);
dea3101e 9819 }
895427bd
JS
9820 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9821 pring = phba->sli4_hba.nvme_wq[i]->pring;
9822 pring->flag = 0;
9823 pring->ringno = LPFC_FCP_RING;
9824 INIT_LIST_HEAD(&pring->txq);
9825 INIT_LIST_HEAD(&pring->txcmplq);
9826 INIT_LIST_HEAD(&pring->iocb_continueq);
9827 spin_lock_init(&pring->ring_lock);
9828 }
9829 pring = phba->sli4_hba.els_wq->pring;
9830 pring->flag = 0;
9831 pring->ringno = LPFC_ELS_RING;
9832 INIT_LIST_HEAD(&pring->txq);
9833 INIT_LIST_HEAD(&pring->txcmplq);
9834 INIT_LIST_HEAD(&pring->iocb_continueq);
9835 spin_lock_init(&pring->ring_lock);
dea3101e 9836
895427bd
JS
9837 if (phba->cfg_nvme_io_channel) {
9838 pring = phba->sli4_hba.nvmels_wq->pring;
9839 pring->flag = 0;
9840 pring->ringno = LPFC_ELS_RING;
9841 INIT_LIST_HEAD(&pring->txq);
9842 INIT_LIST_HEAD(&pring->txcmplq);
9843 INIT_LIST_HEAD(&pring->iocb_continueq);
9844 spin_lock_init(&pring->ring_lock);
9845 }
9846
9847 if (phba->cfg_fof) {
9848 pring = phba->sli4_hba.oas_wq->pring;
9849 pring->flag = 0;
9850 pring->ringno = LPFC_FCP_RING;
9851 INIT_LIST_HEAD(&pring->txq);
9852 INIT_LIST_HEAD(&pring->txcmplq);
9853 INIT_LIST_HEAD(&pring->iocb_continueq);
9854 spin_lock_init(&pring->ring_lock);
9855 }
9856
9857 spin_unlock_irq(&phba->hbalock);
9858}
9859
9860/**
9861 * lpfc_sli_queue_init - Queue initialization function
9862 * @phba: Pointer to HBA context object.
9863 *
9864 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9865 * ring. This function also initializes ring indices of each ring.
9866 * This function is called during the initialization of the SLI
9867 * interface of an HBA.
9868 * This function is called with no lock held and always returns
9869 * 1.
9870 **/
9871void
9872lpfc_sli_queue_init(struct lpfc_hba *phba)
9873{
9874 struct lpfc_sli *psli;
9875 struct lpfc_sli_ring *pring;
9876 int i;
9877
9878 psli = &phba->sli;
9879 spin_lock_irq(&phba->hbalock);
9880 INIT_LIST_HEAD(&psli->mboxq);
9881 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9882 /* Initialize list headers for txq and txcmplq as double linked lists */
9883 for (i = 0; i < psli->num_rings; i++) {
9884 pring = &psli->sli3_ring[i];
9885 pring->ringno = i;
9886 pring->sli.sli3.next_cmdidx = 0;
9887 pring->sli.sli3.local_getidx = 0;
9888 pring->sli.sli3.cmdidx = 0;
9889 INIT_LIST_HEAD(&pring->iocb_continueq);
9890 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9891 INIT_LIST_HEAD(&pring->postbufq);
9892 pring->flag = 0;
9893 INIT_LIST_HEAD(&pring->txq);
9894 INIT_LIST_HEAD(&pring->txcmplq);
9895 spin_lock_init(&pring->ring_lock);
9896 }
9897 spin_unlock_irq(&phba->hbalock);
9898}
9899
9900/**
9901 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9902 * @phba: Pointer to HBA context object.
9903 *
9904 * This routine flushes the mailbox command subsystem. It will unconditionally
9905 * flush all the mailbox commands in the three possible stages in the mailbox
9906 * command sub-system: pending mailbox command queue; the outstanding mailbox
04c68496
JS
9907 * command; and completed mailbox command queue. It is caller's responsibility
9908 * to make sure that the driver is in the proper state to flush the mailbox
9909 * command sub-system. Namely, the posting of mailbox commands into the
9910 * pending mailbox command queue from the various clients must be stopped;
9911 * either the HBA is in a state that it will never works on the outstanding
9912 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9913 * mailbox command has been completed.
9914 **/
9915static void
9916lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9917{
9918 LIST_HEAD(completions);
9919 struct lpfc_sli *psli = &phba->sli;
9920 LPFC_MBOXQ_t *pmb;
9921 unsigned long iflag;
9922
9923 /* Flush all the mailbox commands in the mbox system */
9924 spin_lock_irqsave(&phba->hbalock, iflag);
9925 /* The pending mailbox command queue */
9926 list_splice_init(&phba->sli.mboxq, &completions);
9927 /* The outstanding active mailbox command */
9928 if (psli->mbox_active) {
9929 list_add_tail(&psli->mbox_active->list, &completions);
9930 psli->mbox_active = NULL;
9931 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9932 }
9933 /* The completed mailbox command queue */
9934 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9935 spin_unlock_irqrestore(&phba->hbalock, iflag);
9936
9937 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9938 while (!list_empty(&completions)) {
9939 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9940 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9941 if (pmb->mbox_cmpl)
9942 pmb->mbox_cmpl(phba, pmb);
9943 }
9944}
9945
e59058c4 9946/**
3621a710 9947 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
9948 * @vport: Pointer to virtual port object.
9949 *
9950 * lpfc_sli_host_down is called to clean up the resources
9951 * associated with a vport before destroying virtual
9952 * port data structures.
9953 * This function does following operations:
9954 * - Free discovery resources associated with this virtual
9955 * port.
9956 * - Free iocbs associated with this virtual port in
9957 * the txq.
9958 * - Send abort for all iocb commands associated with this
9959 * vport in txcmplq.
9960 *
9961 * This function is called with no lock held and always returns 1.
9962 **/
92d7f7b0
JS
9963int
9964lpfc_sli_host_down(struct lpfc_vport *vport)
9965{
858c9f6c 9966 LIST_HEAD(completions);
92d7f7b0
JS
9967 struct lpfc_hba *phba = vport->phba;
9968 struct lpfc_sli *psli = &phba->sli;
895427bd 9969 struct lpfc_queue *qp = NULL;
92d7f7b0
JS
9970 struct lpfc_sli_ring *pring;
9971 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
9972 int i;
9973 unsigned long flags = 0;
9974 uint16_t prev_pring_flag;
9975
9976 lpfc_cleanup_discovery_resources(vport);
9977
9978 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 9979
895427bd
JS
9980 /*
9981 * Error everything on the txq since these iocbs
9982 * have not been given to the FW yet.
9983 * Also issue ABTS for everything on the txcmplq
9984 */
9985 if (phba->sli_rev != LPFC_SLI_REV4) {
9986 for (i = 0; i < psli->num_rings; i++) {
9987 pring = &psli->sli3_ring[i];
9988 prev_pring_flag = pring->flag;
9989 /* Only slow rings */
9990 if (pring->ringno == LPFC_ELS_RING) {
9991 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9992 /* Set the lpfc data pending flag */
9993 set_bit(LPFC_DATA_READY, &phba->data_flags);
9994 }
9995 list_for_each_entry_safe(iocb, next_iocb,
9996 &pring->txq, list) {
9997 if (iocb->vport != vport)
9998 continue;
9999 list_move_tail(&iocb->list, &completions);
10000 }
10001 list_for_each_entry_safe(iocb, next_iocb,
10002 &pring->txcmplq, list) {
10003 if (iocb->vport != vport)
10004 continue;
10005 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10006 }
10007 pring->flag = prev_pring_flag;
10008 }
10009 } else {
10010 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10011 pring = qp->pring;
10012 if (!pring)
92d7f7b0 10013 continue;
895427bd
JS
10014 if (pring == phba->sli4_hba.els_wq->pring) {
10015 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10016 /* Set the lpfc data pending flag */
10017 set_bit(LPFC_DATA_READY, &phba->data_flags);
10018 }
10019 prev_pring_flag = pring->flag;
10020 spin_lock_irq(&pring->ring_lock);
10021 list_for_each_entry_safe(iocb, next_iocb,
10022 &pring->txq, list) {
10023 if (iocb->vport != vport)
10024 continue;
10025 list_move_tail(&iocb->list, &completions);
10026 }
10027 spin_unlock_irq(&pring->ring_lock);
10028 list_for_each_entry_safe(iocb, next_iocb,
10029 &pring->txcmplq, list) {
10030 if (iocb->vport != vport)
10031 continue;
10032 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10033 }
10034 pring->flag = prev_pring_flag;
92d7f7b0 10035 }
92d7f7b0 10036 }
92d7f7b0
JS
10037 spin_unlock_irqrestore(&phba->hbalock, flags);
10038
a257bf90
JS
10039 /* Cancel all the IOCBs from the completions list */
10040 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10041 IOERR_SLI_DOWN);
92d7f7b0
JS
10042 return 1;
10043}
10044
e59058c4 10045/**
3621a710 10046 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
10047 * @phba: Pointer to HBA context object.
10048 *
10049 * This function cleans up all iocb, buffers, mailbox commands
10050 * while shutting down the HBA. This function is called with no
10051 * lock held and always returns 1.
10052 * This function does the following to cleanup driver resources:
10053 * - Free discovery resources for each virtual port
10054 * - Cleanup any pending fabric iocbs
10055 * - Iterate through the iocb txq and free each entry
10056 * in the list.
10057 * - Free up any buffer posted to the HBA
10058 * - Free mailbox commands in the mailbox queue.
10059 **/
dea3101e 10060int
2e0fef85 10061lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 10062{
2534ba75 10063 LIST_HEAD(completions);
2e0fef85 10064 struct lpfc_sli *psli = &phba->sli;
895427bd 10065 struct lpfc_queue *qp = NULL;
dea3101e 10066 struct lpfc_sli_ring *pring;
0ff10d46 10067 struct lpfc_dmabuf *buf_ptr;
dea3101e 10068 unsigned long flags = 0;
04c68496
JS
10069 int i;
10070
10071 /* Shutdown the mailbox command sub-system */
618a5230 10072 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea3101e 10073
dea3101e
JB
10074 lpfc_hba_down_prep(phba);
10075
92d7f7b0
JS
10076 lpfc_fabric_abort_hba(phba);
10077
2e0fef85 10078 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 10079
895427bd
JS
10080 /*
10081 * Error everything on the txq since these iocbs
10082 * have not been given to the FW yet.
10083 */
10084 if (phba->sli_rev != LPFC_SLI_REV4) {
10085 for (i = 0; i < psli->num_rings; i++) {
10086 pring = &psli->sli3_ring[i];
10087 /* Only slow rings */
10088 if (pring->ringno == LPFC_ELS_RING) {
10089 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10090 /* Set the lpfc data pending flag */
10091 set_bit(LPFC_DATA_READY, &phba->data_flags);
10092 }
10093 list_splice_init(&pring->txq, &completions);
10094 }
10095 } else {
10096 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10097 pring = qp->pring;
10098 if (!pring)
10099 continue;
10100 spin_lock_irq(&pring->ring_lock);
10101 list_splice_init(&pring->txq, &completions);
10102 spin_unlock_irq(&pring->ring_lock);
10103 if (pring == phba->sli4_hba.els_wq->pring) {
10104 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10105 /* Set the lpfc data pending flag */
10106 set_bit(LPFC_DATA_READY, &phba->data_flags);
10107 }
10108 }
2534ba75 10109 }
2e0fef85 10110 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 10111
a257bf90
JS
10112 /* Cancel all the IOCBs from the completions list */
10113 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10114 IOERR_SLI_DOWN);
dea3101e 10115
0ff10d46
JS
10116 spin_lock_irqsave(&phba->hbalock, flags);
10117 list_splice_init(&phba->elsbuf, &completions);
10118 phba->elsbuf_cnt = 0;
10119 phba->elsbuf_prev_cnt = 0;
10120 spin_unlock_irqrestore(&phba->hbalock, flags);
10121
10122 while (!list_empty(&completions)) {
10123 list_remove_head(&completions, buf_ptr,
10124 struct lpfc_dmabuf, list);
10125 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10126 kfree(buf_ptr);
10127 }
10128
dea3101e
JB
10129 /* Return any active mbox cmds */
10130 del_timer_sync(&psli->mbox_tmo);
2e0fef85 10131
da0436e9 10132 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 10133 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 10134 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 10135
da0436e9
JS
10136 return 1;
10137}
10138
e59058c4 10139/**
3621a710 10140 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
10141 * @srcp: Source memory pointer.
10142 * @destp: Destination memory pointer.
10143 * @cnt: Number of words required to be copied.
10144 *
10145 * This function is used for copying data between driver memory
10146 * and the SLI memory. This function also changes the endianness
10147 * of each word if native endianness is different from SLI
10148 * endianness. This function can be called with or without
10149 * lock.
10150 **/
dea3101e
JB
10151void
10152lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10153{
10154 uint32_t *src = srcp;
10155 uint32_t *dest = destp;
10156 uint32_t ldata;
10157 int i;
10158
10159 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10160 ldata = *src;
10161 ldata = le32_to_cpu(ldata);
10162 *dest = ldata;
10163 src++;
10164 dest++;
10165 }
10166}
10167
e59058c4 10168
a0c87cbd
JS
10169/**
10170 * lpfc_sli_bemem_bcopy - SLI memory copy function
10171 * @srcp: Source memory pointer.
10172 * @destp: Destination memory pointer.
10173 * @cnt: Number of words required to be copied.
10174 *
10175 * This function is used for copying data between a data structure
10176 * with big endian representation to local endianness.
10177 * This function can be called with or without lock.
10178 **/
10179void
10180lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10181{
10182 uint32_t *src = srcp;
10183 uint32_t *dest = destp;
10184 uint32_t ldata;
10185 int i;
10186
10187 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10188 ldata = *src;
10189 ldata = be32_to_cpu(ldata);
10190 *dest = ldata;
10191 src++;
10192 dest++;
10193 }
10194}
10195
e59058c4 10196/**
3621a710 10197 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
10198 * @phba: Pointer to HBA context object.
10199 * @pring: Pointer to driver SLI ring object.
10200 * @mp: Pointer to driver buffer object.
10201 *
10202 * This function is called with no lock held.
10203 * It always return zero after adding the buffer to the postbufq
10204 * buffer list.
10205 **/
dea3101e 10206int
2e0fef85
JS
10207lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10208 struct lpfc_dmabuf *mp)
dea3101e
JB
10209{
10210 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10211 later */
2e0fef85 10212 spin_lock_irq(&phba->hbalock);
dea3101e 10213 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 10214 pring->postbufq_cnt++;
2e0fef85 10215 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10216 return 0;
10217}
10218
e59058c4 10219/**
3621a710 10220 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
10221 * @phba: Pointer to HBA context object.
10222 *
10223 * When HBQ is enabled, buffers are searched based on tags. This function
10224 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10225 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10226 * does not conflict with tags of buffer posted for unsolicited events.
10227 * The function returns the allocated tag. The function is called with
10228 * no locks held.
10229 **/
76bb24ef
JS
10230uint32_t
10231lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10232{
10233 spin_lock_irq(&phba->hbalock);
10234 phba->buffer_tag_count++;
10235 /*
10236 * Always set the QUE_BUFTAG_BIT to distiguish between
10237 * a tag assigned by HBQ.
10238 */
10239 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10240 spin_unlock_irq(&phba->hbalock);
10241 return phba->buffer_tag_count;
10242}
10243
e59058c4 10244/**
3621a710 10245 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
10246 * @phba: Pointer to HBA context object.
10247 * @pring: Pointer to driver SLI ring object.
10248 * @tag: Buffer tag.
10249 *
10250 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10251 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10252 * iocb is posted to the response ring with the tag of the buffer.
10253 * This function searches the pring->postbufq list using the tag
10254 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10255 * iocb. If the buffer is found then lpfc_dmabuf object of the
10256 * buffer is returned to the caller else NULL is returned.
10257 * This function is called with no lock held.
10258 **/
76bb24ef
JS
10259struct lpfc_dmabuf *
10260lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10261 uint32_t tag)
10262{
10263 struct lpfc_dmabuf *mp, *next_mp;
10264 struct list_head *slp = &pring->postbufq;
10265
25985edc 10266 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
10267 spin_lock_irq(&phba->hbalock);
10268 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10269 if (mp->buffer_tag == tag) {
10270 list_del_init(&mp->list);
10271 pring->postbufq_cnt--;
10272 spin_unlock_irq(&phba->hbalock);
10273 return mp;
10274 }
10275 }
10276
10277 spin_unlock_irq(&phba->hbalock);
10278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 10279 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
10280 "ring %d Data x%lx x%p x%p x%x\n",
10281 pring->ringno, (unsigned long) tag,
10282 slp->next, slp->prev, pring->postbufq_cnt);
10283
10284 return NULL;
10285}
dea3101e 10286
e59058c4 10287/**
3621a710 10288 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
10289 * @phba: Pointer to HBA context object.
10290 * @pring: Pointer to driver SLI ring object.
10291 * @phys: DMA address of the buffer.
10292 *
10293 * This function searches the buffer list using the dma_address
10294 * of unsolicited event to find the driver's lpfc_dmabuf object
10295 * corresponding to the dma_address. The function returns the
10296 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10297 * This function is called by the ct and els unsolicited event
10298 * handlers to get the buffer associated with the unsolicited
10299 * event.
10300 *
10301 * This function is called with no lock held.
10302 **/
dea3101e
JB
10303struct lpfc_dmabuf *
10304lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10305 dma_addr_t phys)
10306{
10307 struct lpfc_dmabuf *mp, *next_mp;
10308 struct list_head *slp = &pring->postbufq;
10309
25985edc 10310 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 10311 spin_lock_irq(&phba->hbalock);
dea3101e
JB
10312 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10313 if (mp->phys == phys) {
10314 list_del_init(&mp->list);
10315 pring->postbufq_cnt--;
2e0fef85 10316 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
10317 return mp;
10318 }
10319 }
10320
2e0fef85 10321 spin_unlock_irq(&phba->hbalock);
dea3101e 10322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 10323 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 10324 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 10325 pring->ringno, (unsigned long long)phys,
dea3101e
JB
10326 slp->next, slp->prev, pring->postbufq_cnt);
10327 return NULL;
10328}
10329
e59058c4 10330/**
3621a710 10331 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
10332 * @phba: Pointer to HBA context object.
10333 * @cmdiocb: Pointer to driver command iocb object.
10334 * @rspiocb: Pointer to driver response iocb object.
10335 *
10336 * This function is the completion handler for the abort iocbs for
10337 * ELS commands. This function is called from the ELS ring event
10338 * handler with no lock held. This function frees memory resources
10339 * associated with the abort iocb.
10340 **/
dea3101e 10341static void
2e0fef85
JS
10342lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10343 struct lpfc_iocbq *rspiocb)
dea3101e 10344{
2e0fef85 10345 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 10346 uint16_t abort_iotag, abort_context;
ff78d8f9 10347 struct lpfc_iocbq *abort_iocb = NULL;
2680eeaa
JS
10348
10349 if (irsp->ulpStatus) {
ff78d8f9
JS
10350
10351 /*
10352 * Assume that the port already completed and returned, or
10353 * will return the iocb. Just Log the message.
10354 */
2680eeaa
JS
10355 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10356 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10357
2e0fef85 10358 spin_lock_irq(&phba->hbalock);
45ed1190
JS
10359 if (phba->sli_rev < LPFC_SLI_REV4) {
10360 if (abort_iotag != 0 &&
10361 abort_iotag <= phba->sli.last_iotag)
10362 abort_iocb =
10363 phba->sli.iocbq_lookup[abort_iotag];
10364 } else
10365 /* For sli4 the abort_tag is the XRI,
10366 * so the abort routine puts the iotag of the iocb
10367 * being aborted in the context field of the abort
10368 * IOCB.
10369 */
10370 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 10371
2a9bf3d0
JS
10372 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10373 "0327 Cannot abort els iocb %p "
10374 "with tag %x context %x, abort status %x, "
10375 "abort code %x\n",
10376 abort_iocb, abort_iotag, abort_context,
10377 irsp->ulpStatus, irsp->un.ulpWord[4]);
341af102 10378
ff78d8f9 10379 spin_unlock_irq(&phba->hbalock);
2680eeaa 10380 }
604a3e30 10381 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e
JB
10382 return;
10383}
10384
e59058c4 10385/**
3621a710 10386 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
10387 * @phba: Pointer to HBA context object.
10388 * @cmdiocb: Pointer to driver command iocb object.
10389 * @rspiocb: Pointer to driver response iocb object.
10390 *
10391 * The function is called from SLI ring event handler with no
10392 * lock held. This function is the completion handler for ELS commands
10393 * which are aborted. The function frees memory resources used for
10394 * the aborted ELS commands.
10395 **/
92d7f7b0
JS
10396static void
10397lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10398 struct lpfc_iocbq *rspiocb)
10399{
10400 IOCB_t *irsp = &rspiocb->iocb;
10401
10402 /* ELS cmd tag <ulpIoTag> completes */
10403 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 10404 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 10405 "x%x x%x x%x\n",
e8b62011 10406 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 10407 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
10408 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10409 lpfc_ct_free_iocb(phba, cmdiocb);
10410 else
10411 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
10412 return;
10413}
10414
e59058c4 10415/**
5af5eee7 10416 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
10417 * @phba: Pointer to HBA context object.
10418 * @pring: Pointer to driver SLI ring object.
10419 * @cmdiocb: Pointer to driver command iocb object.
10420 *
5af5eee7
JS
10421 * This function issues an abort iocb for the provided command iocb down to
10422 * the port. Other than the case the outstanding command iocb is an abort
10423 * request, this function issues abort out unconditionally. This function is
10424 * called with hbalock held. The function returns 0 when it fails due to
10425 * memory allocation failure or when the command iocb is an abort request.
e59058c4 10426 **/
5af5eee7
JS
10427static int
10428lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 10429 struct lpfc_iocbq *cmdiocb)
dea3101e 10430{
2e0fef85 10431 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 10432 struct lpfc_iocbq *abtsiocbp;
dea3101e
JB
10433 IOCB_t *icmd = NULL;
10434 IOCB_t *iabt = NULL;
5af5eee7 10435 int retval;
7e56aa25 10436 unsigned long iflags;
07951076 10437
1c2ba475
JT
10438 lockdep_assert_held(&phba->hbalock);
10439
92d7f7b0
JS
10440 /*
10441 * There are certain command types we don't want to abort. And we
10442 * don't want to abort commands that are already in the process of
10443 * being aborted.
07951076
JS
10444 */
10445 icmd = &cmdiocb->iocb;
2e0fef85 10446 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
10447 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10448 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
10449 return 0;
10450
dea3101e 10451 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 10452 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e
JB
10453 if (abtsiocbp == NULL)
10454 return 0;
dea3101e 10455
07951076 10456 /* This signals the response to set the correct status
341af102 10457 * before calling the completion handler
07951076
JS
10458 */
10459 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10460
dea3101e 10461 iabt = &abtsiocbp->iocb;
07951076
JS
10462 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10463 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 10464 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 10465 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
10466 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10467 }
da0436e9
JS
10468 else
10469 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
10470 iabt->ulpLe = 1;
10471 iabt->ulpClass = icmd->ulpClass;
dea3101e 10472
5ffc266e 10473 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10474 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
341af102
JS
10475 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10476 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10477 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10478 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10479
2e0fef85 10480 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
10481 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10482 else
10483 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 10484
07951076 10485 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
e6c6acc0 10486 abtsiocbp->vport = vport;
5b8bd0c9 10487
e8b62011
JS
10488 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10489 "0339 Abort xri x%x, original iotag x%x, "
10490 "abort cmd iotag x%x\n",
2a9bf3d0 10491 iabt->un.acxri.abortIoTag,
e8b62011 10492 iabt->un.acxri.abortContextTag,
2a9bf3d0 10493 abtsiocbp->iotag);
7e56aa25
JS
10494
10495 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
10496 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10497 if (unlikely(pring == NULL))
9bd2bff5 10498 return 0;
7e56aa25
JS
10499 /* Note: both hbalock and ring_lock need to be set here */
10500 spin_lock_irqsave(&pring->ring_lock, iflags);
10501 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10502 abtsiocbp, 0);
10503 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10504 } else {
10505 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10506 abtsiocbp, 0);
10507 }
dea3101e 10508
d7c255b2
JS
10509 if (retval)
10510 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
10511
10512 /*
10513 * Caller to this routine should check for IOCB_ERROR
10514 * and handle it properly. This routine no longer removes
10515 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10516 */
10517 return retval;
10518}
10519
10520/**
10521 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10522 * @phba: Pointer to HBA context object.
10523 * @pring: Pointer to driver SLI ring object.
10524 * @cmdiocb: Pointer to driver command iocb object.
10525 *
10526 * This function issues an abort iocb for the provided command iocb. In case
10527 * of unloading, the abort iocb will not be issued to commands on the ELS
10528 * ring. Instead, the callback function shall be changed to those commands
10529 * so that nothing happens when them finishes. This function is called with
10530 * hbalock held. The function returns 0 when the command iocb is an abort
10531 * request.
10532 **/
10533int
10534lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10535 struct lpfc_iocbq *cmdiocb)
10536{
10537 struct lpfc_vport *vport = cmdiocb->vport;
10538 int retval = IOCB_ERROR;
10539 IOCB_t *icmd = NULL;
10540
1c2ba475
JT
10541 lockdep_assert_held(&phba->hbalock);
10542
5af5eee7
JS
10543 /*
10544 * There are certain command types we don't want to abort. And we
10545 * don't want to abort commands that are already in the process of
10546 * being aborted.
10547 */
10548 icmd = &cmdiocb->iocb;
10549 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10550 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10551 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10552 return 0;
10553
10554 /*
10555 * If we're unloading, don't abort iocb on the ELS ring, but change
10556 * the callback so that nothing happens when it finishes.
10557 */
10558 if ((vport->load_flag & FC_UNLOADING) &&
10559 (pring->ringno == LPFC_ELS_RING)) {
10560 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10561 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10562 else
10563 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10564 goto abort_iotag_exit;
10565 }
10566
10567 /* Now, we try to issue the abort to the cmdiocb out */
10568 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10569
07951076 10570abort_iotag_exit:
2e0fef85
JS
10571 /*
10572 * Caller to this routine should check for IOCB_ERROR
10573 * and handle it properly. This routine no longer removes
10574 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 10575 */
2e0fef85 10576 return retval;
dea3101e
JB
10577}
10578
895427bd
JS
10579/**
10580 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10581 * @phba: Pointer to HBA context object.
10582 * @pring: Pointer to driver SLI ring object.
10583 * @cmdiocb: Pointer to driver command iocb object.
10584 *
10585 * This function issues an abort iocb for the provided command iocb down to
10586 * the port. Other than the case the outstanding command iocb is an abort
10587 * request, this function issues abort out unconditionally. This function is
10588 * called with hbalock held. The function returns 0 when it fails due to
10589 * memory allocation failure or when the command iocb is an abort request.
10590 **/
10591static int
10592lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10593 struct lpfc_iocbq *cmdiocb)
10594{
10595 struct lpfc_vport *vport = cmdiocb->vport;
10596 struct lpfc_iocbq *abtsiocbp;
10597 union lpfc_wqe *abts_wqe;
10598 int retval;
10599
10600 /*
10601 * There are certain command types we don't want to abort. And we
10602 * don't want to abort commands that are already in the process of
10603 * being aborted.
10604 */
10605 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10606 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10607 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10608 return 0;
10609
10610 /* issue ABTS for this io based on iotag */
10611 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10612 if (abtsiocbp == NULL)
10613 return 0;
10614
10615 /* This signals the response to set the correct status
10616 * before calling the completion handler
10617 */
10618 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10619
10620 /* Complete prepping the abort wqe and issue to the FW. */
10621 abts_wqe = &abtsiocbp->wqe;
10622 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10623 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10624
10625 /* Explicitly set reserved fields to zero.*/
10626 abts_wqe->abort_cmd.rsrvd4 = 0;
10627 abts_wqe->abort_cmd.rsrvd5 = 0;
10628
10629 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10630 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10631 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10632
10633 /* word 7 */
10634 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10635 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10636 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10637 cmdiocb->iocb.ulpClass);
10638
10639 /* word 8 - tell the FW to abort the IO associated with this
10640 * outstanding exchange ID.
10641 */
10642 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10643
10644 /* word 9 - this is the iotag for the abts_wqe completion. */
10645 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10646 abtsiocbp->iotag);
10647
10648 /* word 10 */
10649 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10650 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10651 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10652
10653 /* word 11 */
10654 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10655 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10656 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10657
10658 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10659 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10660 abtsiocbp->vport = vport;
01649561 10661 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
895427bd
JS
10662 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10663 if (retval == IOCB_ERROR) {
10664 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10665 "6147 Failed abts issue_wqe with status x%x "
10666 "for oxid x%x\n",
10667 retval, cmdiocb->sli4_xritag);
10668 lpfc_sli_release_iocbq(phba, abtsiocbp);
10669 return retval;
10670 }
10671
10672 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10673 "6148 Drv Abort NVME Request Issued for "
10674 "ox_id x%x on reqtag x%x\n",
10675 cmdiocb->sli4_xritag,
10676 abtsiocbp->iotag);
10677
10678 return retval;
10679}
10680
5af5eee7
JS
10681/**
10682 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10683 * @phba: pointer to lpfc HBA data structure.
10684 *
10685 * This routine will abort all pending and outstanding iocbs to an HBA.
10686 **/
10687void
10688lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10689{
10690 struct lpfc_sli *psli = &phba->sli;
10691 struct lpfc_sli_ring *pring;
895427bd 10692 struct lpfc_queue *qp = NULL;
5af5eee7
JS
10693 int i;
10694
895427bd
JS
10695 if (phba->sli_rev != LPFC_SLI_REV4) {
10696 for (i = 0; i < psli->num_rings; i++) {
10697 pring = &psli->sli3_ring[i];
10698 lpfc_sli_abort_iocb_ring(phba, pring);
10699 }
10700 return;
10701 }
10702 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10703 pring = qp->pring;
10704 if (!pring)
10705 continue;
db55fba8 10706 lpfc_sli_abort_iocb_ring(phba, pring);
5af5eee7
JS
10707 }
10708}
10709
e59058c4 10710/**
3621a710 10711 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
10712 * @iocbq: Pointer to driver iocb object.
10713 * @vport: Pointer to driver virtual port object.
10714 * @tgt_id: SCSI ID of the target.
10715 * @lun_id: LUN ID of the scsi device.
10716 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10717 *
3621a710 10718 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
10719 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10720 * 0 if the filtering criteria is met for the given iocb and will return
10721 * 1 if the filtering criteria is not met.
10722 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10723 * given iocb is for the SCSI device specified by vport, tgt_id and
10724 * lun_id parameter.
10725 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10726 * given iocb is for the SCSI target specified by vport and tgt_id
10727 * parameters.
10728 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10729 * given iocb is for the SCSI host associated with the given vport.
10730 * This function is called with no locks held.
10731 **/
dea3101e 10732static int
51ef4c26
JS
10733lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10734 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 10735 lpfc_ctx_cmd ctx_cmd)
dea3101e 10736{
0bd4ca25 10737 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e
JB
10738 int rc = 1;
10739
0bd4ca25
JSEC
10740 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10741 return rc;
10742
51ef4c26
JS
10743 if (iocbq->vport != vport)
10744 return rc;
10745
0bd4ca25 10746 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 10747
495a714c 10748 if (lpfc_cmd->pCmd == NULL)
dea3101e
JB
10749 return rc;
10750
10751 switch (ctx_cmd) {
10752 case LPFC_CTX_LUN:
495a714c
JS
10753 if ((lpfc_cmd->rdata->pnode) &&
10754 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10755 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e
JB
10756 rc = 0;
10757 break;
10758 case LPFC_CTX_TGT:
495a714c
JS
10759 if ((lpfc_cmd->rdata->pnode) &&
10760 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e
JB
10761 rc = 0;
10762 break;
dea3101e
JB
10763 case LPFC_CTX_HOST:
10764 rc = 0;
10765 break;
10766 default:
10767 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 10768 __func__, ctx_cmd);
dea3101e
JB
10769 break;
10770 }
10771
10772 return rc;
10773}
10774
e59058c4 10775/**
3621a710 10776 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
10777 * @vport: Pointer to virtual port.
10778 * @tgt_id: SCSI ID of the target.
10779 * @lun_id: LUN ID of the scsi device.
10780 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10781 *
10782 * This function returns number of FCP commands pending for the vport.
10783 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10784 * commands pending on the vport associated with SCSI device specified
10785 * by tgt_id and lun_id parameters.
10786 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10787 * commands pending on the vport associated with SCSI target specified
10788 * by tgt_id parameter.
10789 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10790 * commands pending on the vport.
10791 * This function returns the number of iocbs which satisfy the filter.
10792 * This function is called without any lock held.
10793 **/
dea3101e 10794int
51ef4c26
JS
10795lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10796 lpfc_ctx_cmd ctx_cmd)
dea3101e 10797{
51ef4c26 10798 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10799 struct lpfc_iocbq *iocbq;
10800 int sum, i;
dea3101e 10801
31979008 10802 spin_lock_irq(&phba->hbalock);
0bd4ca25
JSEC
10803 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10804 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10805
51ef4c26
JS
10806 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10807 ctx_cmd) == 0)
0bd4ca25 10808 sum++;
dea3101e 10809 }
31979008 10810 spin_unlock_irq(&phba->hbalock);
0bd4ca25 10811
dea3101e
JB
10812 return sum;
10813}
10814
e59058c4 10815/**
3621a710 10816 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
10817 * @phba: Pointer to HBA context object
10818 * @cmdiocb: Pointer to command iocb object.
10819 * @rspiocb: Pointer to response iocb object.
10820 *
10821 * This function is called when an aborted FCP iocb completes. This
10822 * function is called by the ring event handler with no lock held.
10823 * This function frees the iocb.
10824 **/
5eb95af0 10825void
2e0fef85
JS
10826lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10827 struct lpfc_iocbq *rspiocb)
5eb95af0 10828{
cb69f7de 10829 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8e668af5 10830 "3096 ABORT_XRI_CN completing on rpi x%x "
cb69f7de
JS
10831 "original iotag x%x, abort cmd iotag x%x "
10832 "status 0x%x, reason 0x%x\n",
10833 cmdiocb->iocb.un.acxri.abortContextTag,
10834 cmdiocb->iocb.un.acxri.abortIoTag,
10835 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10836 rspiocb->iocb.un.ulpWord[4]);
604a3e30 10837 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
10838 return;
10839}
10840
e59058c4 10841/**
3621a710 10842 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
10843 * @vport: Pointer to virtual port.
10844 * @pring: Pointer to driver SLI ring object.
10845 * @tgt_id: SCSI ID of the target.
10846 * @lun_id: LUN ID of the scsi device.
10847 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10848 *
10849 * This function sends an abort command for every SCSI command
10850 * associated with the given virtual port pending on the ring
10851 * filtered by lpfc_sli_validate_fcp_iocb function.
10852 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10853 * FCP iocbs associated with lun specified by tgt_id and lun_id
10854 * parameters
10855 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10856 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10857 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10858 * FCP iocbs associated with virtual port.
10859 * This function returns number of iocbs it failed to abort.
10860 * This function is called with no locks held.
10861 **/
dea3101e 10862int
51ef4c26
JS
10863lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10864 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 10865{
51ef4c26 10866 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
10867 struct lpfc_iocbq *iocbq;
10868 struct lpfc_iocbq *abtsiocb;
dea3101e 10869 IOCB_t *cmd = NULL;
dea3101e 10870 int errcnt = 0, ret_val = 0;
0bd4ca25 10871 int i;
dea3101e 10872
0bd4ca25
JSEC
10873 for (i = 1; i <= phba->sli.last_iotag; i++) {
10874 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 10875
51ef4c26 10876 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 10877 abort_cmd) != 0)
dea3101e
JB
10878 continue;
10879
afbd8d88
JS
10880 /*
10881 * If the iocbq is already being aborted, don't take a second
10882 * action, but do count it.
10883 */
10884 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10885 continue;
10886
dea3101e 10887 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 10888 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
10889 if (abtsiocb == NULL) {
10890 errcnt++;
10891 continue;
10892 }
dea3101e 10893
afbd8d88
JS
10894 /* indicate the IO is being aborted by the driver. */
10895 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10896
0bd4ca25 10897 cmd = &iocbq->iocb;
dea3101e
JB
10898 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10899 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
10900 if (phba->sli_rev == LPFC_SLI_REV4)
10901 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10902 else
10903 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e
JB
10904 abtsiocb->iocb.ulpLe = 1;
10905 abtsiocb->iocb.ulpClass = cmd->ulpClass;
afbd8d88 10906 abtsiocb->vport = vport;
dea3101e 10907
5ffc266e 10908 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 10909 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
341af102
JS
10910 if (iocbq->iocb_flag & LPFC_IO_FCP)
10911 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
10912 if (iocbq->iocb_flag & LPFC_IO_FOF)
10913 abtsiocb->iocb_flag |= LPFC_IO_FOF;
5ffc266e 10914
2e0fef85 10915 if (lpfc_is_link_up(phba))
dea3101e
JB
10916 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10917 else
10918 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10919
5eb95af0
JSEC
10920 /* Setup callback routine and issue the command. */
10921 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
10922 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10923 abtsiocb, 0);
dea3101e 10924 if (ret_val == IOCB_ERROR) {
604a3e30 10925 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e
JB
10926 errcnt++;
10927 continue;
10928 }
10929 }
10930
10931 return errcnt;
10932}
10933
98912dda
JS
10934/**
10935 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10936 * @vport: Pointer to virtual port.
10937 * @pring: Pointer to driver SLI ring object.
10938 * @tgt_id: SCSI ID of the target.
10939 * @lun_id: LUN ID of the scsi device.
10940 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10941 *
10942 * This function sends an abort command for every SCSI command
10943 * associated with the given virtual port pending on the ring
10944 * filtered by lpfc_sli_validate_fcp_iocb function.
10945 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10946 * FCP iocbs associated with lun specified by tgt_id and lun_id
10947 * parameters
10948 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10949 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10950 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10951 * FCP iocbs associated with virtual port.
10952 * This function returns number of iocbs it aborted .
10953 * This function is called with no locks held right after a taskmgmt
10954 * command is sent.
10955 **/
10956int
10957lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10958 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10959{
10960 struct lpfc_hba *phba = vport->phba;
8c50d25c 10961 struct lpfc_scsi_buf *lpfc_cmd;
98912dda 10962 struct lpfc_iocbq *abtsiocbq;
8c50d25c 10963 struct lpfc_nodelist *ndlp;
98912dda
JS
10964 struct lpfc_iocbq *iocbq;
10965 IOCB_t *icmd;
10966 int sum, i, ret_val;
10967 unsigned long iflags;
10968 struct lpfc_sli_ring *pring_s4;
98912dda
JS
10969
10970 spin_lock_irq(&phba->hbalock);
10971
10972 /* all I/Os are in process of being flushed */
10973 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10974 spin_unlock_irq(&phba->hbalock);
10975 return 0;
10976 }
10977 sum = 0;
10978
10979 for (i = 1; i <= phba->sli.last_iotag; i++) {
10980 iocbq = phba->sli.iocbq_lookup[i];
10981
10982 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10983 cmd) != 0)
10984 continue;
10985
10986 /*
10987 * If the iocbq is already being aborted, don't take a second
10988 * action, but do count it.
10989 */
10990 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10991 continue;
10992
10993 /* issue ABTS for this IOCB based on iotag */
10994 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10995 if (abtsiocbq == NULL)
10996 continue;
10997
10998 icmd = &iocbq->iocb;
10999 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11000 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11001 if (phba->sli_rev == LPFC_SLI_REV4)
11002 abtsiocbq->iocb.un.acxri.abortIoTag =
11003 iocbq->sli4_xritag;
11004 else
11005 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11006 abtsiocbq->iocb.ulpLe = 1;
11007 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11008 abtsiocbq->vport = vport;
11009
11010 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
895427bd 11011 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
98912dda
JS
11012 if (iocbq->iocb_flag & LPFC_IO_FCP)
11013 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
9bd2bff5
JS
11014 if (iocbq->iocb_flag & LPFC_IO_FOF)
11015 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
98912dda 11016
8c50d25c
JS
11017 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11018 ndlp = lpfc_cmd->rdata->pnode;
11019
11020 if (lpfc_is_link_up(phba) &&
11021 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
98912dda
JS
11022 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11023 else
11024 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11025
11026 /* Setup callback routine and issue the command. */
11027 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11028
11029 /*
11030 * Indicate the IO is being aborted by the driver and set
11031 * the caller's flag into the aborted IO.
11032 */
11033 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11034
11035 if (phba->sli_rev == LPFC_SLI_REV4) {
895427bd
JS
11036 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11037 if (pring_s4 == NULL)
11038 continue;
98912dda
JS
11039 /* Note: both hbalock and ring_lock must be set here */
11040 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11041 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11042 abtsiocbq, 0);
11043 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11044 } else {
11045 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11046 abtsiocbq, 0);
11047 }
11048
11049
11050 if (ret_val == IOCB_ERROR)
11051 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11052 else
11053 sum++;
11054 }
11055 spin_unlock_irq(&phba->hbalock);
11056 return sum;
11057}
11058
e59058c4 11059/**
3621a710 11060 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
11061 * @phba: Pointer to HBA context object.
11062 * @cmdiocbq: Pointer to command iocb.
11063 * @rspiocbq: Pointer to response iocb.
11064 *
11065 * This function is the completion handler for iocbs issued using
11066 * lpfc_sli_issue_iocb_wait function. This function is called by the
11067 * ring event handler function without any lock held. This function
11068 * can be called from both worker thread context and interrupt
11069 * context. This function also can be called from other thread which
11070 * cleans up the SLI layer objects.
11071 * This function copy the contents of the response iocb to the
11072 * response iocb memory object provided by the caller of
11073 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11074 * sleeps for the iocb completion.
11075 **/
68876920
JSEC
11076static void
11077lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11078 struct lpfc_iocbq *cmdiocbq,
11079 struct lpfc_iocbq *rspiocbq)
dea3101e 11080{
68876920
JSEC
11081 wait_queue_head_t *pdone_q;
11082 unsigned long iflags;
0f65ff68 11083 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 11084
2e0fef85 11085 spin_lock_irqsave(&phba->hbalock, iflags);
5a0916b4
JS
11086 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11087
11088 /*
11089 * A time out has occurred for the iocb. If a time out
11090 * completion handler has been supplied, call it. Otherwise,
11091 * just free the iocbq.
11092 */
11093
11094 spin_unlock_irqrestore(&phba->hbalock, iflags);
11095 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11096 cmdiocbq->wait_iocb_cmpl = NULL;
11097 if (cmdiocbq->iocb_cmpl)
11098 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11099 else
11100 lpfc_sli_release_iocbq(phba, cmdiocbq);
11101 return;
11102 }
11103
68876920
JSEC
11104 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11105 if (cmdiocbq->context2 && rspiocbq)
11106 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11107 &rspiocbq->iocb, sizeof(IOCB_t));
11108
0f65ff68
JS
11109 /* Set the exchange busy flag for task management commands */
11110 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11111 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11112 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11113 cur_iocbq);
11114 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11115 }
11116
68876920 11117 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
11118 if (pdone_q)
11119 wake_up(pdone_q);
858c9f6c 11120 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e
JB
11121 return;
11122}
11123
d11e31dd
JS
11124/**
11125 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11126 * @phba: Pointer to HBA context object..
11127 * @piocbq: Pointer to command iocb.
11128 * @flag: Flag to test.
11129 *
11130 * This routine grabs the hbalock and then test the iocb_flag to
11131 * see if the passed in flag is set.
11132 * Returns:
11133 * 1 if flag is set.
11134 * 0 if flag is not set.
11135 **/
11136static int
11137lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11138 struct lpfc_iocbq *piocbq, uint32_t flag)
11139{
11140 unsigned long iflags;
11141 int ret;
11142
11143 spin_lock_irqsave(&phba->hbalock, iflags);
11144 ret = piocbq->iocb_flag & flag;
11145 spin_unlock_irqrestore(&phba->hbalock, iflags);
11146 return ret;
11147
11148}
11149
e59058c4 11150/**
3621a710 11151 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
11152 * @phba: Pointer to HBA context object..
11153 * @pring: Pointer to sli ring.
11154 * @piocb: Pointer to command iocb.
11155 * @prspiocbq: Pointer to response iocb.
11156 * @timeout: Timeout in number of seconds.
11157 *
11158 * This function issues the iocb to firmware and waits for the
5a0916b4
JS
11159 * iocb to complete. The iocb_cmpl field of the shall be used
11160 * to handle iocbs which time out. If the field is NULL, the
11161 * function shall free the iocbq structure. If more clean up is
11162 * needed, the caller is expected to provide a completion function
11163 * that will provide the needed clean up. If the iocb command is
11164 * not completed within timeout seconds, the function will either
11165 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11166 * completion function set in the iocb_cmpl field and then return
11167 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11168 * resources if this function returns IOCB_TIMEDOUT.
e59058c4
JS
11169 * The function waits for the iocb completion using an
11170 * non-interruptible wait.
11171 * This function will sleep while waiting for iocb completion.
11172 * So, this function should not be called from any context which
11173 * does not allow sleeping. Due to the same reason, this function
11174 * cannot be called with interrupt disabled.
11175 * This function assumes that the iocb completions occur while
11176 * this function sleep. So, this function cannot be called from
11177 * the thread which process iocb completion for this ring.
11178 * This function clears the iocb_flag of the iocb object before
11179 * issuing the iocb and the iocb completion handler sets this
11180 * flag and wakes this thread when the iocb completes.
11181 * The contents of the response iocb will be copied to prspiocbq
11182 * by the completion handler when the command completes.
11183 * This function returns IOCB_SUCCESS when success.
11184 * This function is called with no lock held.
11185 **/
dea3101e 11186int
2e0fef85 11187lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 11188 uint32_t ring_number,
2e0fef85
JS
11189 struct lpfc_iocbq *piocb,
11190 struct lpfc_iocbq *prspiocbq,
68876920 11191 uint32_t timeout)
dea3101e 11192{
7259f0d0 11193 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
11194 long timeleft, timeout_req = 0;
11195 int retval = IOCB_SUCCESS;
875fbdfe 11196 uint32_t creg_val;
0e9bb8d7
JS
11197 struct lpfc_iocbq *iocb;
11198 int txq_cnt = 0;
11199 int txcmplq_cnt = 0;
895427bd 11200 struct lpfc_sli_ring *pring;
5a0916b4
JS
11201 unsigned long iflags;
11202 bool iocb_completed = true;
11203
895427bd
JS
11204 if (phba->sli_rev >= LPFC_SLI_REV4)
11205 pring = lpfc_sli4_calc_ring(phba, piocb);
11206 else
11207 pring = &phba->sli.sli3_ring[ring_number];
dea3101e 11208 /*
68876920
JSEC
11209 * If the caller has provided a response iocbq buffer, then context2
11210 * is NULL or its an error.
dea3101e 11211 */
68876920
JSEC
11212 if (prspiocbq) {
11213 if (piocb->context2)
11214 return IOCB_ERROR;
11215 piocb->context2 = prspiocbq;
dea3101e
JB
11216 }
11217
5a0916b4 11218 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
68876920
JSEC
11219 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11220 piocb->context_un.wait_queue = &done_q;
5a0916b4 11221 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea3101e 11222
875fbdfe 11223 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11224 if (lpfc_readl(phba->HCregaddr, &creg_val))
11225 return IOCB_ERROR;
875fbdfe
JSEC
11226 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11227 writel(creg_val, phba->HCregaddr);
11228 readl(phba->HCregaddr); /* flush */
11229 }
11230
2a9bf3d0
JS
11231 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11232 SLI_IOCB_RET_IOCB);
68876920 11233 if (retval == IOCB_SUCCESS) {
256ec0d0 11234 timeout_req = msecs_to_jiffies(timeout * 1000);
68876920 11235 timeleft = wait_event_timeout(done_q,
d11e31dd 11236 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 11237 timeout_req);
5a0916b4
JS
11238 spin_lock_irqsave(&phba->hbalock, iflags);
11239 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11240
11241 /*
11242 * IOCB timed out. Inform the wake iocb wait
11243 * completion function and set local status
11244 */
dea3101e 11245
5a0916b4
JS
11246 iocb_completed = false;
11247 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11248 }
11249 spin_unlock_irqrestore(&phba->hbalock, iflags);
11250 if (iocb_completed) {
7054a606 11251 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 11252 "0331 IOCB wake signaled\n");
53151bbb
JS
11253 /* Note: we are not indicating if the IOCB has a success
11254 * status or not - that's for the caller to check.
11255 * IOCB_SUCCESS means just that the command was sent and
11256 * completed. Not that it completed successfully.
11257 * */
7054a606 11258 } else if (timeleft == 0) {
68876920 11259 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11260 "0338 IOCB wait timeout error - no "
11261 "wake response Data x%x\n", timeout);
68876920 11262 retval = IOCB_TIMEDOUT;
7054a606 11263 } else {
68876920 11264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
11265 "0330 IOCB wake NOT set, "
11266 "Data x%x x%lx\n",
68876920
JSEC
11267 timeout, (timeleft / jiffies));
11268 retval = IOCB_TIMEDOUT;
dea3101e 11269 }
2a9bf3d0 11270 } else if (retval == IOCB_BUSY) {
0e9bb8d7
JS
11271 if (phba->cfg_log_verbose & LOG_SLI) {
11272 list_for_each_entry(iocb, &pring->txq, list) {
11273 txq_cnt++;
11274 }
11275 list_for_each_entry(iocb, &pring->txcmplq, list) {
11276 txcmplq_cnt++;
11277 }
11278 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11279 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11280 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11281 }
2a9bf3d0 11282 return retval;
68876920
JSEC
11283 } else {
11284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 11285 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 11286 retval);
68876920 11287 retval = IOCB_ERROR;
dea3101e
JB
11288 }
11289
875fbdfe 11290 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
11291 if (lpfc_readl(phba->HCregaddr, &creg_val))
11292 return IOCB_ERROR;
875fbdfe
JSEC
11293 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11294 writel(creg_val, phba->HCregaddr);
11295 readl(phba->HCregaddr); /* flush */
11296 }
11297
68876920
JSEC
11298 if (prspiocbq)
11299 piocb->context2 = NULL;
11300
11301 piocb->context_un.wait_queue = NULL;
11302 piocb->iocb_cmpl = NULL;
dea3101e
JB
11303 return retval;
11304}
68876920 11305
e59058c4 11306/**
3621a710 11307 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
11308 * @phba: Pointer to HBA context object.
11309 * @pmboxq: Pointer to driver mailbox object.
11310 * @timeout: Timeout in number of seconds.
11311 *
11312 * This function issues the mailbox to firmware and waits for the
11313 * mailbox command to complete. If the mailbox command is not
11314 * completed within timeout seconds, it returns MBX_TIMEOUT.
11315 * The function waits for the mailbox completion using an
11316 * interruptible wait. If the thread is woken up due to a
11317 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11318 * should not free the mailbox resources, if this function returns
11319 * MBX_TIMEOUT.
11320 * This function will sleep while waiting for mailbox completion.
11321 * So, this function should not be called from any context which
11322 * does not allow sleeping. Due to the same reason, this function
11323 * cannot be called with interrupt disabled.
11324 * This function assumes that the mailbox completion occurs while
11325 * this function sleep. So, this function cannot be called from
11326 * the worker thread which processes mailbox completion.
11327 * This function is called in the context of HBA management
11328 * applications.
11329 * This function returns MBX_SUCCESS when successful.
11330 * This function is called with no lock held.
11331 **/
dea3101e 11332int
2e0fef85 11333lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e
JB
11334 uint32_t timeout)
11335{
7259f0d0 11336 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
b230b8a2 11337 MAILBOX_t *mb = NULL;
dea3101e 11338 int retval;
858c9f6c 11339 unsigned long flag;
dea3101e 11340
b230b8a2 11341 /* The caller might set context1 for extended buffer */
98c9ea5c 11342 if (pmboxq->context1)
b230b8a2 11343 mb = (MAILBOX_t *)pmboxq->context1;
dea3101e 11344
495a714c 11345 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e
JB
11346 /* setup wake call as IOCB callback */
11347 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11348 /* setup context field to pass wait_queue pointer to wake function */
11349 pmboxq->context1 = &done_q;
11350
dea3101e
JB
11351 /* now issue the command */
11352 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 11353 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
11354 wait_event_interruptible_timeout(done_q,
11355 pmboxq->mbox_flag & LPFC_MBX_WAKE,
256ec0d0 11356 msecs_to_jiffies(timeout * 1000));
7054a606 11357
858c9f6c 11358 spin_lock_irqsave(&phba->hbalock, flag);
b230b8a2
JS
11359 /* restore the possible extended buffer for free resource */
11360 pmboxq->context1 = (uint8_t *)mb;
7054a606
JS
11361 /*
11362 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11363 * else do not free the resources.
11364 */
d7c47992 11365 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 11366 retval = MBX_SUCCESS;
d7c47992 11367 } else {
7054a606 11368 retval = MBX_TIMEOUT;
858c9f6c
JS
11369 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11370 }
11371 spin_unlock_irqrestore(&phba->hbalock, flag);
b230b8a2
JS
11372 } else {
11373 /* restore the possible extended buffer for free resource */
11374 pmboxq->context1 = (uint8_t *)mb;
dea3101e
JB
11375 }
11376
dea3101e
JB
11377 return retval;
11378}
11379
e59058c4 11380/**
3772a991 11381 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
11382 * @phba: Pointer to HBA context.
11383 *
3772a991
JS
11384 * This function is called to shutdown the driver's mailbox sub-system.
11385 * It first marks the mailbox sub-system is in a block state to prevent
11386 * the asynchronous mailbox command from issued off the pending mailbox
11387 * command queue. If the mailbox command sub-system shutdown is due to
11388 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11389 * the mailbox sub-system flush routine to forcefully bring down the
11390 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11391 * as with offline or HBA function reset), this routine will wait for the
11392 * outstanding mailbox command to complete before invoking the mailbox
11393 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 11394 **/
3772a991 11395void
618a5230 11396lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
b4c02652 11397{
3772a991 11398 struct lpfc_sli *psli = &phba->sli;
3772a991 11399 unsigned long timeout;
b4c02652 11400
618a5230
JS
11401 if (mbx_action == LPFC_MBX_NO_WAIT) {
11402 /* delay 100ms for port state */
11403 msleep(100);
11404 lpfc_sli_mbox_sys_flush(phba);
11405 return;
11406 }
a183a15f 11407 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
d7069f09 11408
3772a991
JS
11409 spin_lock_irq(&phba->hbalock);
11410 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
b4c02652 11411
3772a991 11412 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
3772a991
JS
11413 /* Determine how long we might wait for the active mailbox
11414 * command to be gracefully completed by firmware.
11415 */
a183a15f
JS
11416 if (phba->sli.mbox_active)
11417 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11418 phba->sli.mbox_active) *
11419 1000) + jiffies;
11420 spin_unlock_irq(&phba->hbalock);
11421
3772a991
JS
11422 while (phba->sli.mbox_active) {
11423 /* Check active mailbox complete status every 2ms */
11424 msleep(2);
11425 if (time_after(jiffies, timeout))
11426 /* Timeout, let the mailbox flush routine to
11427 * forcefully release active mailbox command
11428 */
11429 break;
11430 }
d7069f09
JS
11431 } else
11432 spin_unlock_irq(&phba->hbalock);
11433
3772a991
JS
11434 lpfc_sli_mbox_sys_flush(phba);
11435}
ed957684 11436
3772a991
JS
11437/**
11438 * lpfc_sli_eratt_read - read sli-3 error attention events
11439 * @phba: Pointer to HBA context.
11440 *
11441 * This function is called to read the SLI3 device error attention registers
11442 * for possible error attention events. The caller must hold the hostlock
11443 * with spin_lock_irq().
11444 *
25985edc 11445 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
11446 * Register and returns 0 otherwise.
11447 **/
11448static int
11449lpfc_sli_eratt_read(struct lpfc_hba *phba)
11450{
11451 uint32_t ha_copy;
b4c02652 11452
3772a991 11453 /* Read chip Host Attention (HA) register */
9940b97b
JS
11454 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11455 goto unplug_err;
11456
3772a991
JS
11457 if (ha_copy & HA_ERATT) {
11458 /* Read host status register to retrieve error event */
9940b97b
JS
11459 if (lpfc_sli_read_hs(phba))
11460 goto unplug_err;
b4c02652 11461
3772a991
JS
11462 /* Check if there is a deferred error condition is active */
11463 if ((HS_FFER1 & phba->work_hs) &&
11464 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 11465 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 11466 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
11467 /* Clear all interrupt enable conditions */
11468 writel(0, phba->HCregaddr);
11469 readl(phba->HCregaddr);
11470 }
11471
11472 /* Set the driver HA work bitmap */
3772a991
JS
11473 phba->work_ha |= HA_ERATT;
11474 /* Indicate polling handles this ERATT */
11475 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
11476 return 1;
11477 }
11478 return 0;
9940b97b
JS
11479
11480unplug_err:
11481 /* Set the driver HS work bitmap */
11482 phba->work_hs |= UNPLUG_ERR;
11483 /* Set the driver HA work bitmap */
11484 phba->work_ha |= HA_ERATT;
11485 /* Indicate polling handles this ERATT */
11486 phba->hba_flag |= HBA_ERATT_HANDLED;
11487 return 1;
b4c02652
JS
11488}
11489
da0436e9
JS
11490/**
11491 * lpfc_sli4_eratt_read - read sli-4 error attention events
11492 * @phba: Pointer to HBA context.
11493 *
11494 * This function is called to read the SLI4 device error attention registers
11495 * for possible error attention events. The caller must hold the hostlock
11496 * with spin_lock_irq().
11497 *
25985edc 11498 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
11499 * Register and returns 0 otherwise.
11500 **/
11501static int
11502lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11503{
11504 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
11505 uint32_t if_type, portsmphr;
11506 struct lpfc_register portstat_reg;
da0436e9 11507
2fcee4bf
JS
11508 /*
11509 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
11510 * registers for error attention. This can be changed later.
11511 */
2fcee4bf
JS
11512 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11513 switch (if_type) {
11514 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
11515 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11516 &uerr_sta_lo) ||
11517 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11518 &uerr_sta_hi)) {
11519 phba->work_hs |= UNPLUG_ERR;
11520 phba->work_ha |= HA_ERATT;
11521 phba->hba_flag |= HBA_ERATT_HANDLED;
11522 return 1;
11523 }
2fcee4bf
JS
11524 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11525 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11527 "1423 HBA Unrecoverable error: "
11528 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11529 "ue_mask_lo_reg=0x%x, "
11530 "ue_mask_hi_reg=0x%x\n",
11531 uerr_sta_lo, uerr_sta_hi,
11532 phba->sli4_hba.ue_mask_lo,
11533 phba->sli4_hba.ue_mask_hi);
11534 phba->work_status[0] = uerr_sta_lo;
11535 phba->work_status[1] = uerr_sta_hi;
11536 phba->work_ha |= HA_ERATT;
11537 phba->hba_flag |= HBA_ERATT_HANDLED;
11538 return 1;
11539 }
11540 break;
11541 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
11542 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11543 &portstat_reg.word0) ||
11544 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11545 &portsmphr)){
11546 phba->work_hs |= UNPLUG_ERR;
11547 phba->work_ha |= HA_ERATT;
11548 phba->hba_flag |= HBA_ERATT_HANDLED;
11549 return 1;
11550 }
2fcee4bf
JS
11551 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11552 phba->work_status[0] =
11553 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11554 phba->work_status[1] =
11555 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2e90f4b5 11557 "2885 Port Status Event: "
2fcee4bf
JS
11558 "port status reg 0x%x, "
11559 "port smphr reg 0x%x, "
11560 "error 1=0x%x, error 2=0x%x\n",
11561 portstat_reg.word0,
11562 portsmphr,
11563 phba->work_status[0],
11564 phba->work_status[1]);
11565 phba->work_ha |= HA_ERATT;
11566 phba->hba_flag |= HBA_ERATT_HANDLED;
11567 return 1;
11568 }
11569 break;
11570 case LPFC_SLI_INTF_IF_TYPE_1:
11571 default:
a747c9ce 11572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
11573 "2886 HBA Error Attention on unsupported "
11574 "if type %d.", if_type);
a747c9ce 11575 return 1;
da0436e9 11576 }
2fcee4bf 11577
da0436e9
JS
11578 return 0;
11579}
11580
e59058c4 11581/**
3621a710 11582 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
11583 * @phba: Pointer to HBA context.
11584 *
3772a991 11585 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
11586 * error attention register bit for error attention events.
11587 *
25985edc 11588 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
11589 * Register and returns 0 otherwise.
11590 **/
11591int
11592lpfc_sli_check_eratt(struct lpfc_hba *phba)
11593{
11594 uint32_t ha_copy;
11595
11596 /* If somebody is waiting to handle an eratt, don't process it
11597 * here. The brdkill function will do this.
11598 */
11599 if (phba->link_flag & LS_IGNORE_ERATT)
11600 return 0;
11601
11602 /* Check if interrupt handler handles this ERATT */
11603 spin_lock_irq(&phba->hbalock);
11604 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11605 /* Interrupt handler has handled ERATT */
11606 spin_unlock_irq(&phba->hbalock);
11607 return 0;
11608 }
11609
a257bf90
JS
11610 /*
11611 * If there is deferred error attention, do not check for error
11612 * attention
11613 */
11614 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11615 spin_unlock_irq(&phba->hbalock);
11616 return 0;
11617 }
11618
3772a991
JS
11619 /* If PCI channel is offline, don't process it */
11620 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 11621 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11622 return 0;
11623 }
11624
11625 switch (phba->sli_rev) {
11626 case LPFC_SLI_REV2:
11627 case LPFC_SLI_REV3:
11628 /* Read chip Host Attention (HA) register */
11629 ha_copy = lpfc_sli_eratt_read(phba);
11630 break;
da0436e9 11631 case LPFC_SLI_REV4:
2fcee4bf 11632 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
11633 ha_copy = lpfc_sli4_eratt_read(phba);
11634 break;
3772a991
JS
11635 default:
11636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11637 "0299 Invalid SLI revision (%d)\n",
11638 phba->sli_rev);
11639 ha_copy = 0;
11640 break;
9399627f
JS
11641 }
11642 spin_unlock_irq(&phba->hbalock);
3772a991
JS
11643
11644 return ha_copy;
11645}
11646
11647/**
11648 * lpfc_intr_state_check - Check device state for interrupt handling
11649 * @phba: Pointer to HBA context.
11650 *
11651 * This inline routine checks whether a device or its PCI slot is in a state
11652 * that the interrupt should be handled.
11653 *
11654 * This function returns 0 if the device or the PCI slot is in a state that
11655 * interrupt should be handled, otherwise -EIO.
11656 */
11657static inline int
11658lpfc_intr_state_check(struct lpfc_hba *phba)
11659{
11660 /* If the pci channel is offline, ignore all the interrupts */
11661 if (unlikely(pci_channel_offline(phba->pcidev)))
11662 return -EIO;
11663
11664 /* Update device level interrupt statistics */
11665 phba->sli.slistat.sli_intr++;
11666
11667 /* Ignore all interrupts during initialization. */
11668 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11669 return -EIO;
11670
9399627f
JS
11671 return 0;
11672}
11673
11674/**
3772a991 11675 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
11676 * @irq: Interrupt number.
11677 * @dev_id: The device context pointer.
11678 *
9399627f 11679 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11680 * service routine when device with SLI-3 interface spec is enabled with
11681 * MSI-X multi-message interrupt mode and there are slow-path events in
11682 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11683 * interrupt mode, this function is called as part of the device-level
11684 * interrupt handler. When the PCI slot is in error recovery or the HBA
11685 * is undergoing initialization, the interrupt handler will not process
11686 * the interrupt. The link attention and ELS ring attention events are
11687 * handled by the worker thread. The interrupt handler signals the worker
11688 * thread and returns for these events. This function is called without
11689 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
11690 * structures.
11691 *
11692 * This function returns IRQ_HANDLED when interrupt is handled else it
11693 * returns IRQ_NONE.
e59058c4 11694 **/
dea3101e 11695irqreturn_t
3772a991 11696lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 11697{
2e0fef85 11698 struct lpfc_hba *phba;
a747c9ce 11699 uint32_t ha_copy, hc_copy;
dea3101e
JB
11700 uint32_t work_ha_copy;
11701 unsigned long status;
5b75da2f 11702 unsigned long iflag;
dea3101e
JB
11703 uint32_t control;
11704
92d7f7b0 11705 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
11706 struct lpfc_vport *vport;
11707 struct lpfc_nodelist *ndlp;
11708 struct lpfc_dmabuf *mp;
92d7f7b0
JS
11709 LPFC_MBOXQ_t *pmb;
11710 int rc;
11711
dea3101e
JB
11712 /*
11713 * Get the driver's phba structure from the dev_id and
11714 * assume the HBA is not interrupting.
11715 */
9399627f 11716 phba = (struct lpfc_hba *)dev_id;
dea3101e
JB
11717
11718 if (unlikely(!phba))
11719 return IRQ_NONE;
11720
dea3101e 11721 /*
9399627f
JS
11722 * Stuff needs to be attented to when this function is invoked as an
11723 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 11724 */
9399627f 11725 if (phba->intr_type == MSIX) {
3772a991
JS
11726 /* Check device state for handling interrupt */
11727 if (lpfc_intr_state_check(phba))
9399627f
JS
11728 return IRQ_NONE;
11729 /* Need to read HA REG for slow-path events */
5b75da2f 11730 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11731 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11732 goto unplug_error;
9399627f
JS
11733 /* If somebody is waiting to handle an eratt don't process it
11734 * here. The brdkill function will do this.
11735 */
11736 if (phba->link_flag & LS_IGNORE_ERATT)
11737 ha_copy &= ~HA_ERATT;
11738 /* Check the need for handling ERATT in interrupt handler */
11739 if (ha_copy & HA_ERATT) {
11740 if (phba->hba_flag & HBA_ERATT_HANDLED)
11741 /* ERATT polling has handled ERATT */
11742 ha_copy &= ~HA_ERATT;
11743 else
11744 /* Indicate interrupt handler handles ERATT */
11745 phba->hba_flag |= HBA_ERATT_HANDLED;
11746 }
a257bf90
JS
11747
11748 /*
11749 * If there is deferred error attention, do not check for any
11750 * interrupt.
11751 */
11752 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 11753 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
11754 return IRQ_NONE;
11755 }
11756
9399627f 11757 /* Clear up only attention source related to slow-path */
9940b97b
JS
11758 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11759 goto unplug_error;
11760
a747c9ce
JS
11761 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11762 HC_LAINT_ENA | HC_ERINT_ENA),
11763 phba->HCregaddr);
9399627f
JS
11764 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11765 phba->HAregaddr);
a747c9ce 11766 writel(hc_copy, phba->HCregaddr);
9399627f 11767 readl(phba->HAregaddr); /* flush */
5b75da2f 11768 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
11769 } else
11770 ha_copy = phba->ha_copy;
dea3101e 11771
dea3101e
JB
11772 work_ha_copy = ha_copy & phba->work_ha_mask;
11773
9399627f 11774 if (work_ha_copy) {
dea3101e
JB
11775 if (work_ha_copy & HA_LATT) {
11776 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11777 /*
11778 * Turn off Link Attention interrupts
11779 * until CLEAR_LA done
11780 */
5b75da2f 11781 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11782 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
11783 if (lpfc_readl(phba->HCregaddr, &control))
11784 goto unplug_error;
dea3101e
JB
11785 control &= ~HC_LAINT_ENA;
11786 writel(control, phba->HCregaddr);
11787 readl(phba->HCregaddr); /* flush */
5b75da2f 11788 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11789 }
11790 else
11791 work_ha_copy &= ~HA_LATT;
11792 }
11793
9399627f 11794 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
11795 /*
11796 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11797 * the only slow ring.
11798 */
11799 status = (work_ha_copy &
11800 (HA_RXMASK << (4*LPFC_ELS_RING)));
11801 status >>= (4*LPFC_ELS_RING);
11802 if (status & HA_RXMASK) {
5b75da2f 11803 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
11804 if (lpfc_readl(phba->HCregaddr, &control))
11805 goto unplug_error;
a58cbd52
JS
11806
11807 lpfc_debugfs_slow_ring_trc(phba,
11808 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11809 control, status,
11810 (uint32_t)phba->sli.slistat.sli_intr);
11811
858c9f6c 11812 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
11813 lpfc_debugfs_slow_ring_trc(phba,
11814 "ISR Disable ring:"
11815 "pwork:x%x hawork:x%x wait:x%x",
11816 phba->work_ha, work_ha_copy,
11817 (uint32_t)((unsigned long)
5e9d9b82 11818 &phba->work_waitq));
a58cbd52 11819
858c9f6c
JS
11820 control &=
11821 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e
JB
11822 writel(control, phba->HCregaddr);
11823 readl(phba->HCregaddr); /* flush */
dea3101e 11824 }
a58cbd52
JS
11825 else {
11826 lpfc_debugfs_slow_ring_trc(phba,
11827 "ISR slow ring: pwork:"
11828 "x%x hawork:x%x wait:x%x",
11829 phba->work_ha, work_ha_copy,
11830 (uint32_t)((unsigned long)
5e9d9b82 11831 &phba->work_waitq));
a58cbd52 11832 }
5b75da2f 11833 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e
JB
11834 }
11835 }
5b75da2f 11836 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 11837 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
11838 if (lpfc_sli_read_hs(phba))
11839 goto unplug_error;
a257bf90
JS
11840 /*
11841 * Check if there is a deferred error condition
11842 * is active
11843 */
11844 if ((HS_FFER1 & phba->work_hs) &&
11845 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
11846 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11847 phba->work_hs)) {
a257bf90
JS
11848 phba->hba_flag |= DEFER_ERATT;
11849 /* Clear all interrupt enable conditions */
11850 writel(0, phba->HCregaddr);
11851 readl(phba->HCregaddr);
11852 }
11853 }
11854
9399627f 11855 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 11856 pmb = phba->sli.mbox_active;
04c68496 11857 pmbox = &pmb->u.mb;
34b02dcd 11858 mbox = phba->mbox;
858c9f6c 11859 vport = pmb->vport;
92d7f7b0
JS
11860
11861 /* First check out the status word */
11862 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11863 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 11864 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
11865 /*
11866 * Stray Mailbox Interrupt, mbxCommand <cmd>
11867 * mbxStatus <status>
11868 */
09372820 11869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 11870 LOG_SLI,
e8b62011 11871 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
11872 "Interrupt mbxCommand x%x "
11873 "mbxStatus x%x\n",
e8b62011 11874 (vport ? vport->vpi : 0),
92d7f7b0
JS
11875 pmbox->mbxCommand,
11876 pmbox->mbxStatus);
09372820
JS
11877 /* clear mailbox attention bit */
11878 work_ha_copy &= ~HA_MBATT;
11879 } else {
97eab634 11880 phba->sli.mbox_active = NULL;
5b75da2f 11881 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
11882 phba->last_completion_time = jiffies;
11883 del_timer(&phba->sli.mbox_tmo);
09372820
JS
11884 if (pmb->mbox_cmpl) {
11885 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11886 MAILBOX_CMD_SIZE);
7a470277
JS
11887 if (pmb->out_ext_byte_len &&
11888 pmb->context2)
11889 lpfc_sli_pcimem_bcopy(
11890 phba->mbox_ext,
11891 pmb->context2,
11892 pmb->out_ext_byte_len);
09372820
JS
11893 }
11894 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11895 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11896
11897 lpfc_debugfs_disc_trc(vport,
11898 LPFC_DISC_TRC_MBOX_VPORT,
11899 "MBOX dflt rpi: : "
11900 "status:x%x rpi:x%x",
11901 (uint32_t)pmbox->mbxStatus,
11902 pmbox->un.varWords[0], 0);
11903
11904 if (!pmbox->mbxStatus) {
11905 mp = (struct lpfc_dmabuf *)
11906 (pmb->context1);
11907 ndlp = (struct lpfc_nodelist *)
11908 pmb->context2;
11909
11910 /* Reg_LOGIN of dflt RPI was
11911 * successful. new lets get
11912 * rid of the RPI using the
11913 * same mbox buffer.
11914 */
11915 lpfc_unreg_login(phba,
11916 vport->vpi,
11917 pmbox->un.varWords[0],
11918 pmb);
11919 pmb->mbox_cmpl =
11920 lpfc_mbx_cmpl_dflt_rpi;
11921 pmb->context1 = mp;
11922 pmb->context2 = ndlp;
11923 pmb->vport = vport;
58da1ffb
JS
11924 rc = lpfc_sli_issue_mbox(phba,
11925 pmb,
11926 MBX_NOWAIT);
11927 if (rc != MBX_BUSY)
11928 lpfc_printf_log(phba,
11929 KERN_ERR,
11930 LOG_MBOX | LOG_SLI,
d7c255b2 11931 "0350 rc should have"
6a9c52cf 11932 "been MBX_BUSY\n");
3772a991
JS
11933 if (rc != MBX_NOT_FINISHED)
11934 goto send_current_mbox;
09372820 11935 }
858c9f6c 11936 }
5b75da2f
JS
11937 spin_lock_irqsave(
11938 &phba->pport->work_port_lock,
11939 iflag);
09372820
JS
11940 phba->pport->work_port_events &=
11941 ~WORKER_MBOX_TMO;
5b75da2f
JS
11942 spin_unlock_irqrestore(
11943 &phba->pport->work_port_lock,
11944 iflag);
09372820 11945 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 11946 }
97eab634 11947 } else
5b75da2f 11948 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 11949
92d7f7b0
JS
11950 if ((work_ha_copy & HA_MBATT) &&
11951 (phba->sli.mbox_active == NULL)) {
858c9f6c 11952send_current_mbox:
92d7f7b0 11953 /* Process next mailbox command if there is one */
58da1ffb
JS
11954 do {
11955 rc = lpfc_sli_issue_mbox(phba, NULL,
11956 MBX_NOWAIT);
11957 } while (rc == MBX_NOT_FINISHED);
11958 if (rc != MBX_SUCCESS)
11959 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11960 LOG_SLI, "0349 rc should be "
6a9c52cf 11961 "MBX_SUCCESS\n");
92d7f7b0
JS
11962 }
11963
5b75da2f 11964 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 11965 phba->work_ha |= work_ha_copy;
5b75da2f 11966 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 11967 lpfc_worker_wake_up(phba);
dea3101e 11968 }
9399627f 11969 return IRQ_HANDLED;
9940b97b
JS
11970unplug_error:
11971 spin_unlock_irqrestore(&phba->hbalock, iflag);
11972 return IRQ_HANDLED;
dea3101e 11973
3772a991 11974} /* lpfc_sli_sp_intr_handler */
9399627f
JS
11975
11976/**
3772a991 11977 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
11978 * @irq: Interrupt number.
11979 * @dev_id: The device context pointer.
11980 *
11981 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
11982 * service routine when device with SLI-3 interface spec is enabled with
11983 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11984 * ring event in the HBA. However, when the device is enabled with either
11985 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11986 * device-level interrupt handler. When the PCI slot is in error recovery
11987 * or the HBA is undergoing initialization, the interrupt handler will not
11988 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11989 * the intrrupt context. This function is called without any lock held.
11990 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
11991 *
11992 * This function returns IRQ_HANDLED when interrupt is handled else it
11993 * returns IRQ_NONE.
11994 **/
11995irqreturn_t
3772a991 11996lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
11997{
11998 struct lpfc_hba *phba;
11999 uint32_t ha_copy;
12000 unsigned long status;
5b75da2f 12001 unsigned long iflag;
895427bd 12002 struct lpfc_sli_ring *pring;
9399627f
JS
12003
12004 /* Get the driver's phba structure from the dev_id and
12005 * assume the HBA is not interrupting.
12006 */
12007 phba = (struct lpfc_hba *) dev_id;
12008
12009 if (unlikely(!phba))
12010 return IRQ_NONE;
12011
12012 /*
12013 * Stuff needs to be attented to when this function is invoked as an
12014 * individual interrupt handler in MSI-X multi-message interrupt mode
12015 */
12016 if (phba->intr_type == MSIX) {
3772a991
JS
12017 /* Check device state for handling interrupt */
12018 if (lpfc_intr_state_check(phba))
9399627f
JS
12019 return IRQ_NONE;
12020 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
12021 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12022 return IRQ_HANDLED;
9399627f 12023 /* Clear up only attention source related to fast-path */
5b75da2f 12024 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
12025 /*
12026 * If there is deferred error attention, do not check for
12027 * any interrupt.
12028 */
12029 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 12030 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
12031 return IRQ_NONE;
12032 }
9399627f
JS
12033 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12034 phba->HAregaddr);
12035 readl(phba->HAregaddr); /* flush */
5b75da2f 12036 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
12037 } else
12038 ha_copy = phba->ha_copy;
dea3101e
JB
12039
12040 /*
9399627f 12041 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 12042 */
9399627f
JS
12043 ha_copy &= ~(phba->work_ha_mask);
12044
12045 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 12046 status >>= (4*LPFC_FCP_RING);
895427bd 12047 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
858c9f6c 12048 if (status & HA_RXMASK)
895427bd 12049 lpfc_sli_handle_fast_ring_event(phba, pring, status);
a4bc3379
JS
12050
12051 if (phba->cfg_multi_ring_support == 2) {
12052 /*
9399627f
JS
12053 * Process all events on extra ring. Take the optimized path
12054 * for extra ring IO.
a4bc3379 12055 */
9399627f 12056 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 12057 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 12058 if (status & HA_RXMASK) {
a4bc3379 12059 lpfc_sli_handle_fast_ring_event(phba,
895427bd 12060 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
a4bc3379
JS
12061 status);
12062 }
12063 }
dea3101e 12064 return IRQ_HANDLED;
3772a991 12065} /* lpfc_sli_fp_intr_handler */
9399627f
JS
12066
12067/**
3772a991 12068 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
12069 * @irq: Interrupt number.
12070 * @dev_id: The device context pointer.
12071 *
3772a991
JS
12072 * This function is the HBA device-level interrupt handler to device with
12073 * SLI-3 interface spec, called from the PCI layer when either MSI or
12074 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12075 * requires driver attention. This function invokes the slow-path interrupt
12076 * attention handling function and fast-path interrupt attention handling
12077 * function in turn to process the relevant HBA attention events. This
12078 * function is called without any lock held. It gets the hbalock to access
12079 * and update SLI data structures.
9399627f
JS
12080 *
12081 * This function returns IRQ_HANDLED when interrupt is handled, else it
12082 * returns IRQ_NONE.
12083 **/
12084irqreturn_t
3772a991 12085lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
12086{
12087 struct lpfc_hba *phba;
12088 irqreturn_t sp_irq_rc, fp_irq_rc;
12089 unsigned long status1, status2;
a747c9ce 12090 uint32_t hc_copy;
9399627f
JS
12091
12092 /*
12093 * Get the driver's phba structure from the dev_id and
12094 * assume the HBA is not interrupting.
12095 */
12096 phba = (struct lpfc_hba *) dev_id;
12097
12098 if (unlikely(!phba))
12099 return IRQ_NONE;
12100
3772a991
JS
12101 /* Check device state for handling interrupt */
12102 if (lpfc_intr_state_check(phba))
9399627f
JS
12103 return IRQ_NONE;
12104
12105 spin_lock(&phba->hbalock);
9940b97b
JS
12106 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12107 spin_unlock(&phba->hbalock);
12108 return IRQ_HANDLED;
12109 }
12110
9399627f
JS
12111 if (unlikely(!phba->ha_copy)) {
12112 spin_unlock(&phba->hbalock);
12113 return IRQ_NONE;
12114 } else if (phba->ha_copy & HA_ERATT) {
12115 if (phba->hba_flag & HBA_ERATT_HANDLED)
12116 /* ERATT polling has handled ERATT */
12117 phba->ha_copy &= ~HA_ERATT;
12118 else
12119 /* Indicate interrupt handler handles ERATT */
12120 phba->hba_flag |= HBA_ERATT_HANDLED;
12121 }
12122
a257bf90
JS
12123 /*
12124 * If there is deferred error attention, do not check for any interrupt.
12125 */
12126 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 12127 spin_unlock(&phba->hbalock);
a257bf90
JS
12128 return IRQ_NONE;
12129 }
12130
9399627f 12131 /* Clear attention sources except link and error attentions */
9940b97b
JS
12132 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12133 spin_unlock(&phba->hbalock);
12134 return IRQ_HANDLED;
12135 }
a747c9ce
JS
12136 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12137 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12138 phba->HCregaddr);
9399627f 12139 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 12140 writel(hc_copy, phba->HCregaddr);
9399627f
JS
12141 readl(phba->HAregaddr); /* flush */
12142 spin_unlock(&phba->hbalock);
12143
12144 /*
12145 * Invokes slow-path host attention interrupt handling as appropriate.
12146 */
12147
12148 /* status of events with mailbox and link attention */
12149 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12150
12151 /* status of events with ELS ring */
12152 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12153 status2 >>= (4*LPFC_ELS_RING);
12154
12155 if (status1 || (status2 & HA_RXMASK))
3772a991 12156 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
12157 else
12158 sp_irq_rc = IRQ_NONE;
12159
12160 /*
12161 * Invoke fast-path host attention interrupt handling as appropriate.
12162 */
12163
12164 /* status of events with FCP ring */
12165 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12166 status1 >>= (4*LPFC_FCP_RING);
12167
12168 /* status of events with extra ring */
12169 if (phba->cfg_multi_ring_support == 2) {
12170 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12171 status2 >>= (4*LPFC_EXTRA_RING);
12172 } else
12173 status2 = 0;
12174
12175 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 12176 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
12177 else
12178 fp_irq_rc = IRQ_NONE;
dea3101e 12179
9399627f
JS
12180 /* Return device-level interrupt handling status */
12181 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 12182} /* lpfc_sli_intr_handler */
4f774513
JS
12183
12184/**
12185 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12186 * @phba: pointer to lpfc hba data structure.
12187 *
12188 * This routine is invoked by the worker thread to process all the pending
12189 * SLI4 FCP abort XRI events.
12190 **/
12191void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12192{
12193 struct lpfc_cq_event *cq_event;
12194
12195 /* First, declare the fcp xri abort event has been handled */
12196 spin_lock_irq(&phba->hbalock);
12197 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12198 spin_unlock_irq(&phba->hbalock);
12199 /* Now, handle all the fcp xri abort events */
12200 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12201 /* Get the first event from the head of the event queue */
12202 spin_lock_irq(&phba->hbalock);
12203 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12204 cq_event, struct lpfc_cq_event, list);
12205 spin_unlock_irq(&phba->hbalock);
12206 /* Notify aborted XRI for FCP work queue */
12207 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12208 /* Free the event processed back to the free pool */
12209 lpfc_sli4_cq_event_release(phba, cq_event);
12210 }
12211}
12212
12213/**
12214 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12215 * @phba: pointer to lpfc hba data structure.
12216 *
12217 * This routine is invoked by the worker thread to process all the pending
12218 * SLI4 els abort xri events.
12219 **/
12220void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12221{
12222 struct lpfc_cq_event *cq_event;
12223
12224 /* First, declare the els xri abort event has been handled */
12225 spin_lock_irq(&phba->hbalock);
12226 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12227 spin_unlock_irq(&phba->hbalock);
12228 /* Now, handle all the els xri abort events */
12229 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12230 /* Get the first event from the head of the event queue */
12231 spin_lock_irq(&phba->hbalock);
12232 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12233 cq_event, struct lpfc_cq_event, list);
12234 spin_unlock_irq(&phba->hbalock);
12235 /* Notify aborted XRI for ELS work queue */
12236 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12237 /* Free the event processed back to the free pool */
12238 lpfc_sli4_cq_event_release(phba, cq_event);
12239 }
12240}
12241
341af102
JS
12242/**
12243 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12244 * @phba: pointer to lpfc hba data structure
12245 * @pIocbIn: pointer to the rspiocbq
12246 * @pIocbOut: pointer to the cmdiocbq
12247 * @wcqe: pointer to the complete wcqe
12248 *
12249 * This routine transfers the fields of a command iocbq to a response iocbq
12250 * by copying all the IOCB fields from command iocbq and transferring the
12251 * completion status information from the complete wcqe.
12252 **/
4f774513 12253static void
341af102
JS
12254lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12255 struct lpfc_iocbq *pIocbIn,
4f774513
JS
12256 struct lpfc_iocbq *pIocbOut,
12257 struct lpfc_wcqe_complete *wcqe)
12258{
af22741c 12259 int numBdes, i;
341af102 12260 unsigned long iflags;
af22741c
JS
12261 uint32_t status, max_response;
12262 struct lpfc_dmabuf *dmabuf;
12263 struct ulp_bde64 *bpl, bde;
4f774513
JS
12264 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12265
12266 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12267 sizeof(struct lpfc_iocbq) - offset);
4f774513 12268 /* Map WCQE parameters into irspiocb parameters */
acd6859b
JS
12269 status = bf_get(lpfc_wcqe_c_status, wcqe);
12270 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
4f774513
JS
12271 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12272 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12273 pIocbIn->iocb.un.fcpi.fcpi_parm =
12274 pIocbOut->iocb.un.fcpi.fcpi_parm -
12275 wcqe->total_data_placed;
12276 else
12277 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 12278 else {
4f774513 12279 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
af22741c
JS
12280 switch (pIocbOut->iocb.ulpCommand) {
12281 case CMD_ELS_REQUEST64_CR:
12282 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12283 bpl = (struct ulp_bde64 *)dmabuf->virt;
12284 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12285 max_response = bde.tus.f.bdeSize;
12286 break;
12287 case CMD_GEN_REQUEST64_CR:
12288 max_response = 0;
12289 if (!pIocbOut->context3)
12290 break;
12291 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12292 sizeof(struct ulp_bde64);
12293 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12294 bpl = (struct ulp_bde64 *)dmabuf->virt;
12295 for (i = 0; i < numBdes; i++) {
12296 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12297 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12298 max_response += bde.tus.f.bdeSize;
12299 }
12300 break;
12301 default:
12302 max_response = wcqe->total_data_placed;
12303 break;
12304 }
12305 if (max_response < wcqe->total_data_placed)
12306 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12307 else
12308 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12309 wcqe->total_data_placed;
695a814e 12310 }
341af102 12311
acd6859b
JS
12312 /* Convert BG errors for completion status */
12313 if (status == CQE_STATUS_DI_ERROR) {
12314 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12315
12316 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12317 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12318 else
12319 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12320
12321 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12322 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12323 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12324 BGS_GUARD_ERR_MASK;
12325 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12326 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12327 BGS_APPTAG_ERR_MASK;
12328 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12329 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12330 BGS_REFTAG_ERR_MASK;
12331
12332 /* Check to see if there was any good data before the error */
12333 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12334 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12335 BGS_HI_WATER_MARK_PRESENT_MASK;
12336 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12337 wcqe->total_data_placed;
12338 }
12339
12340 /*
12341 * Set ALL the error bits to indicate we don't know what
12342 * type of error it is.
12343 */
12344 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12345 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12346 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12347 BGS_GUARD_ERR_MASK);
12348 }
12349
341af102
JS
12350 /* Pick up HBA exchange busy condition */
12351 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12352 spin_lock_irqsave(&phba->hbalock, iflags);
12353 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12354 spin_unlock_irqrestore(&phba->hbalock, iflags);
12355 }
4f774513
JS
12356}
12357
45ed1190
JS
12358/**
12359 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12360 * @phba: Pointer to HBA context object.
12361 * @wcqe: Pointer to work-queue completion queue entry.
12362 *
12363 * This routine handles an ELS work-queue completion event and construct
12364 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12365 * discovery engine to handle.
12366 *
12367 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12368 **/
12369static struct lpfc_iocbq *
12370lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12371 struct lpfc_iocbq *irspiocbq)
12372{
895427bd 12373 struct lpfc_sli_ring *pring;
45ed1190
JS
12374 struct lpfc_iocbq *cmdiocbq;
12375 struct lpfc_wcqe_complete *wcqe;
12376 unsigned long iflags;
12377
895427bd
JS
12378 pring = lpfc_phba_elsring(phba);
12379
45ed1190 12380 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
7e56aa25 12381 spin_lock_irqsave(&pring->ring_lock, iflags);
45ed1190
JS
12382 pring->stats.iocb_event++;
12383 /* Look up the ELS command IOCB and create pseudo response IOCB */
12384 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12385 bf_get(lpfc_wcqe_c_request_tag, wcqe));
89533e9b
JS
12386 /* Put the iocb back on the txcmplq */
12387 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
7e56aa25 12388 spin_unlock_irqrestore(&pring->ring_lock, iflags);
45ed1190
JS
12389
12390 if (unlikely(!cmdiocbq)) {
12391 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12392 "0386 ELS complete with no corresponding "
12393 "cmdiocb: iotag (%d)\n",
12394 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12395 lpfc_sli_release_iocbq(phba, irspiocbq);
12396 return NULL;
12397 }
12398
12399 /* Fake the irspiocbq and copy necessary response information */
341af102 12400 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
12401
12402 return irspiocbq;
12403}
12404
04c68496
JS
12405/**
12406 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12407 * @phba: Pointer to HBA context object.
12408 * @cqe: Pointer to mailbox completion queue entry.
12409 *
12410 * This routine process a mailbox completion queue entry with asynchrous
12411 * event.
12412 *
12413 * Return: true if work posted to worker thread, otherwise false.
12414 **/
12415static bool
12416lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12417{
12418 struct lpfc_cq_event *cq_event;
12419 unsigned long iflags;
12420
12421 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12422 "0392 Async Event: word0:x%x, word1:x%x, "
12423 "word2:x%x, word3:x%x\n", mcqe->word0,
12424 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12425
12426 /* Allocate a new internal CQ_EVENT entry */
12427 cq_event = lpfc_sli4_cq_event_alloc(phba);
12428 if (!cq_event) {
12429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12430 "0394 Failed to allocate CQ_EVENT entry\n");
12431 return false;
12432 }
12433
12434 /* Move the CQE into an asynchronous event entry */
12435 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12436 spin_lock_irqsave(&phba->hbalock, iflags);
12437 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12438 /* Set the async event flag */
12439 phba->hba_flag |= ASYNC_EVENT;
12440 spin_unlock_irqrestore(&phba->hbalock, iflags);
12441
12442 return true;
12443}
12444
12445/**
12446 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12447 * @phba: Pointer to HBA context object.
12448 * @cqe: Pointer to mailbox completion queue entry.
12449 *
12450 * This routine process a mailbox completion queue entry with mailbox
12451 * completion event.
12452 *
12453 * Return: true if work posted to worker thread, otherwise false.
12454 **/
12455static bool
12456lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12457{
12458 uint32_t mcqe_status;
12459 MAILBOX_t *mbox, *pmbox;
12460 struct lpfc_mqe *mqe;
12461 struct lpfc_vport *vport;
12462 struct lpfc_nodelist *ndlp;
12463 struct lpfc_dmabuf *mp;
12464 unsigned long iflags;
12465 LPFC_MBOXQ_t *pmb;
12466 bool workposted = false;
12467 int rc;
12468
12469 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12470 if (!bf_get(lpfc_trailer_completed, mcqe))
12471 goto out_no_mqe_complete;
12472
12473 /* Get the reference to the active mbox command */
12474 spin_lock_irqsave(&phba->hbalock, iflags);
12475 pmb = phba->sli.mbox_active;
12476 if (unlikely(!pmb)) {
12477 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12478 "1832 No pending MBOX command to handle\n");
12479 spin_unlock_irqrestore(&phba->hbalock, iflags);
12480 goto out_no_mqe_complete;
12481 }
12482 spin_unlock_irqrestore(&phba->hbalock, iflags);
12483 mqe = &pmb->u.mqe;
12484 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12485 mbox = phba->mbox;
12486 vport = pmb->vport;
12487
12488 /* Reset heartbeat timer */
12489 phba->last_completion_time = jiffies;
12490 del_timer(&phba->sli.mbox_tmo);
12491
12492 /* Move mbox data to caller's mailbox region, do endian swapping */
12493 if (pmb->mbox_cmpl && mbox)
12494 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 12495
73d91e50
JS
12496 /*
12497 * For mcqe errors, conditionally move a modified error code to
12498 * the mbox so that the error will not be missed.
12499 */
12500 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12501 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12502 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12503 bf_set(lpfc_mqe_status, mqe,
12504 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12505 }
04c68496
JS
12506 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12507 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12508 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12509 "MBOX dflt rpi: status:x%x rpi:x%x",
12510 mcqe_status,
12511 pmbox->un.varWords[0], 0);
12512 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12513 mp = (struct lpfc_dmabuf *)(pmb->context1);
12514 ndlp = (struct lpfc_nodelist *)pmb->context2;
12515 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12516 * RID of the PPI using the same mbox buffer.
12517 */
12518 lpfc_unreg_login(phba, vport->vpi,
12519 pmbox->un.varWords[0], pmb);
12520 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12521 pmb->context1 = mp;
12522 pmb->context2 = ndlp;
12523 pmb->vport = vport;
12524 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12525 if (rc != MBX_BUSY)
12526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12527 LOG_SLI, "0385 rc should "
12528 "have been MBX_BUSY\n");
12529 if (rc != MBX_NOT_FINISHED)
12530 goto send_current_mbox;
12531 }
12532 }
12533 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12534 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12535 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12536
12537 /* There is mailbox completion work to do */
12538 spin_lock_irqsave(&phba->hbalock, iflags);
12539 __lpfc_mbox_cmpl_put(phba, pmb);
12540 phba->work_ha |= HA_MBATT;
12541 spin_unlock_irqrestore(&phba->hbalock, iflags);
12542 workposted = true;
12543
12544send_current_mbox:
12545 spin_lock_irqsave(&phba->hbalock, iflags);
12546 /* Release the mailbox command posting token */
12547 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12548 /* Setting active mailbox pointer need to be in sync to flag clear */
12549 phba->sli.mbox_active = NULL;
12550 spin_unlock_irqrestore(&phba->hbalock, iflags);
12551 /* Wake up worker thread to post the next pending mailbox command */
12552 lpfc_worker_wake_up(phba);
12553out_no_mqe_complete:
12554 if (bf_get(lpfc_trailer_consumed, mcqe))
12555 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12556 return workposted;
12557}
12558
12559/**
12560 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12561 * @phba: Pointer to HBA context object.
12562 * @cqe: Pointer to mailbox completion queue entry.
12563 *
12564 * This routine process a mailbox completion queue entry, it invokes the
12565 * proper mailbox complete handling or asynchrous event handling routine
12566 * according to the MCQE's async bit.
12567 *
12568 * Return: true if work posted to worker thread, otherwise false.
12569 **/
12570static bool
12571lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12572{
12573 struct lpfc_mcqe mcqe;
12574 bool workposted;
12575
12576 /* Copy the mailbox MCQE and convert endian order as needed */
12577 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12578
12579 /* Invoke the proper event handling routine */
12580 if (!bf_get(lpfc_trailer_async, &mcqe))
12581 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12582 else
12583 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12584 return workposted;
12585}
12586
4f774513
JS
12587/**
12588 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12589 * @phba: Pointer to HBA context object.
2a76a283 12590 * @cq: Pointer to associated CQ
4f774513
JS
12591 * @wcqe: Pointer to work-queue completion queue entry.
12592 *
12593 * This routine handles an ELS work-queue completion event.
12594 *
12595 * Return: true if work posted to worker thread, otherwise false.
12596 **/
12597static bool
2a76a283 12598lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12599 struct lpfc_wcqe_complete *wcqe)
12600{
4f774513
JS
12601 struct lpfc_iocbq *irspiocbq;
12602 unsigned long iflags;
2a76a283 12603 struct lpfc_sli_ring *pring = cq->pring;
0e9bb8d7
JS
12604 int txq_cnt = 0;
12605 int txcmplq_cnt = 0;
12606 int fcp_txcmplq_cnt = 0;
4f774513 12607
45ed1190 12608 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
12609 irspiocbq = lpfc_sli_get_iocbq(phba);
12610 if (!irspiocbq) {
0e9bb8d7
JS
12611 if (!list_empty(&pring->txq))
12612 txq_cnt++;
12613 if (!list_empty(&pring->txcmplq))
12614 txcmplq_cnt++;
4f774513 12615 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
12616 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12617 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
0e9bb8d7
JS
12618 txq_cnt, phba->iocb_cnt,
12619 fcp_txcmplq_cnt,
12620 txcmplq_cnt);
45ed1190 12621 return false;
4f774513 12622 }
4f774513 12623
45ed1190
JS
12624 /* Save off the slow-path queue event for work thread to process */
12625 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 12626 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 12627 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
12628 &phba->sli4_hba.sp_queue_event);
12629 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 12630 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 12631
45ed1190 12632 return true;
4f774513
JS
12633}
12634
12635/**
12636 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12637 * @phba: Pointer to HBA context object.
12638 * @wcqe: Pointer to work-queue completion queue entry.
12639 *
12640 * This routine handles slow-path WQ entry comsumed event by invoking the
12641 * proper WQ release routine to the slow-path WQ.
12642 **/
12643static void
12644lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12645 struct lpfc_wcqe_release *wcqe)
12646{
2e90f4b5
JS
12647 /* sanity check on queue memory */
12648 if (unlikely(!phba->sli4_hba.els_wq))
12649 return;
4f774513
JS
12650 /* Check for the slow-path ELS work queue */
12651 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12652 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12653 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12654 else
12655 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12656 "2579 Slow-path wqe consume event carries "
12657 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12658 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12659 phba->sli4_hba.els_wq->queue_id);
12660}
12661
12662/**
12663 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12664 * @phba: Pointer to HBA context object.
12665 * @cq: Pointer to a WQ completion queue.
12666 * @wcqe: Pointer to work-queue completion queue entry.
12667 *
12668 * This routine handles an XRI abort event.
12669 *
12670 * Return: true if work posted to worker thread, otherwise false.
12671 **/
12672static bool
12673lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12674 struct lpfc_queue *cq,
12675 struct sli4_wcqe_xri_aborted *wcqe)
12676{
12677 bool workposted = false;
12678 struct lpfc_cq_event *cq_event;
12679 unsigned long iflags;
12680
12681 /* Allocate a new internal CQ_EVENT entry */
12682 cq_event = lpfc_sli4_cq_event_alloc(phba);
12683 if (!cq_event) {
12684 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12685 "0602 Failed to allocate CQ_EVENT entry\n");
12686 return false;
12687 }
12688
12689 /* Move the CQE into the proper xri abort event list */
12690 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12691 switch (cq->subtype) {
12692 case LPFC_FCP:
12693 spin_lock_irqsave(&phba->hbalock, iflags);
12694 list_add_tail(&cq_event->list,
12695 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12696 /* Set the fcp xri abort event flag */
12697 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12698 spin_unlock_irqrestore(&phba->hbalock, iflags);
12699 workposted = true;
12700 break;
12701 case LPFC_ELS:
12702 spin_lock_irqsave(&phba->hbalock, iflags);
12703 list_add_tail(&cq_event->list,
12704 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12705 /* Set the els xri abort event flag */
12706 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12707 spin_unlock_irqrestore(&phba->hbalock, iflags);
12708 workposted = true;
12709 break;
12710 default:
12711 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12712 "0603 Invalid work queue CQE subtype (x%x)\n",
12713 cq->subtype);
12714 workposted = false;
12715 break;
12716 }
12717 return workposted;
12718}
12719
4f774513
JS
12720/**
12721 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12722 * @phba: Pointer to HBA context object.
12723 * @rcqe: Pointer to receive-queue completion queue entry.
12724 *
12725 * This routine process a receive-queue completion queue entry.
12726 *
12727 * Return: true if work posted to worker thread, otherwise false.
12728 **/
12729static bool
4d9ab994 12730lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 12731{
4f774513 12732 bool workposted = false;
895427bd 12733 struct fc_frame_header *fc_hdr;
4f774513
JS
12734 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12735 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12736 struct hbq_dmabuf *dma_buf;
7851fe2c 12737 uint32_t status, rq_id;
4f774513
JS
12738 unsigned long iflags;
12739
2e90f4b5
JS
12740 /* sanity check on queue memory */
12741 if (unlikely(!hrq) || unlikely(!drq))
12742 return workposted;
12743
7851fe2c
JS
12744 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12745 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12746 else
12747 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12748 if (rq_id != hrq->queue_id)
4f774513
JS
12749 goto out;
12750
4d9ab994 12751 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
12752 switch (status) {
12753 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12755 "2537 Receive Frame Truncated!!\n");
b84daac9 12756 hrq->RQ_buf_trunc++;
4f774513 12757 case FC_STATUS_RQ_SUCCESS:
5ffc266e 12758 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
12759 spin_lock_irqsave(&phba->hbalock, iflags);
12760 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12761 if (!dma_buf) {
b84daac9 12762 hrq->RQ_no_buf_found++;
4f774513
JS
12763 spin_unlock_irqrestore(&phba->hbalock, iflags);
12764 goto out;
12765 }
b84daac9 12766 hrq->RQ_rcv_buf++;
4d9ab994 12767 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
895427bd
JS
12768
12769 /* If a NVME LS event (type 0x28), treat it as Fast path */
12770 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12771
4f774513 12772 /* save off the frame for the word thread to process */
4d9ab994 12773 list_add_tail(&dma_buf->cq_event.list,
45ed1190 12774 &phba->sli4_hba.sp_queue_event);
4f774513 12775 /* Frame received */
45ed1190 12776 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
12777 spin_unlock_irqrestore(&phba->hbalock, iflags);
12778 workposted = true;
12779 break;
12780 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12781 case FC_STATUS_INSUFF_BUF_FRM_DISC:
b84daac9 12782 hrq->RQ_no_posted_buf++;
4f774513
JS
12783 /* Post more buffers if possible */
12784 spin_lock_irqsave(&phba->hbalock, iflags);
12785 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12786 spin_unlock_irqrestore(&phba->hbalock, iflags);
12787 workposted = true;
12788 break;
12789 }
12790out:
12791 return workposted;
4f774513
JS
12792}
12793
4d9ab994
JS
12794/**
12795 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12796 * @phba: Pointer to HBA context object.
12797 * @cq: Pointer to the completion queue.
12798 * @wcqe: Pointer to a completion queue entry.
12799 *
25985edc 12800 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
12801 * entry.
12802 *
12803 * Return: true if work posted to worker thread, otherwise false.
12804 **/
12805static bool
12806lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12807 struct lpfc_cqe *cqe)
12808{
45ed1190 12809 struct lpfc_cqe cqevt;
4d9ab994
JS
12810 bool workposted = false;
12811
12812 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 12813 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
12814
12815 /* Check and process for different type of WCQE and dispatch */
45ed1190 12816 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 12817 case CQE_CODE_COMPL_WQE:
45ed1190 12818 /* Process the WQ/RQ complete event */
bc73905a 12819 phba->last_completion_time = jiffies;
2a76a283 12820 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
45ed1190 12821 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
12822 break;
12823 case CQE_CODE_RELEASE_WQE:
12824 /* Process the WQ release event */
12825 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 12826 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
12827 break;
12828 case CQE_CODE_XRI_ABORTED:
12829 /* Process the WQ XRI abort event */
bc73905a 12830 phba->last_completion_time = jiffies;
4d9ab994 12831 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 12832 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
12833 break;
12834 case CQE_CODE_RECEIVE:
7851fe2c 12835 case CQE_CODE_RECEIVE_V1:
4d9ab994 12836 /* Process the RQ event */
bc73905a 12837 phba->last_completion_time = jiffies;
4d9ab994 12838 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 12839 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
12840 break;
12841 default:
12842 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12843 "0388 Not a valid WCQE code: x%x\n",
45ed1190 12844 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
12845 break;
12846 }
12847 return workposted;
12848}
12849
4f774513
JS
12850/**
12851 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12852 * @phba: Pointer to HBA context object.
12853 * @eqe: Pointer to fast-path event queue entry.
12854 *
12855 * This routine process a event queue entry from the slow-path event queue.
12856 * It will check the MajorCode and MinorCode to determine this is for a
12857 * completion event on a completion queue, if not, an error shall be logged
12858 * and just return. Otherwise, it will get to the corresponding completion
12859 * queue and process all the entries on that completion queue, rearm the
12860 * completion queue, and then return.
12861 *
12862 **/
12863static void
67d12733
JS
12864lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12865 struct lpfc_queue *speq)
4f774513 12866{
67d12733 12867 struct lpfc_queue *cq = NULL, *childq;
4f774513
JS
12868 struct lpfc_cqe *cqe;
12869 bool workposted = false;
12870 int ecount = 0;
12871 uint16_t cqid;
12872
4f774513 12873 /* Get the reference to the corresponding CQ */
cb5172ea 12874 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513 12875
4f774513
JS
12876 list_for_each_entry(childq, &speq->child_list, list) {
12877 if (childq->queue_id == cqid) {
12878 cq = childq;
12879 break;
12880 }
12881 }
12882 if (unlikely(!cq)) {
75baf696
JS
12883 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12885 "0365 Slow-path CQ identifier "
12886 "(%d) does not exist\n", cqid);
4f774513
JS
12887 return;
12888 }
12889
895427bd
JS
12890 /* Save EQ associated with this CQ */
12891 cq->assoc_qp = speq;
12892
4f774513
JS
12893 /* Process all the entries to the CQ */
12894 switch (cq->type) {
12895 case LPFC_MCQ:
12896 while ((cqe = lpfc_sli4_cq_get(cq))) {
12897 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 12898 if (!(++ecount % cq->entry_repost))
4f774513 12899 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
b84daac9 12900 cq->CQ_mbox++;
4f774513
JS
12901 }
12902 break;
12903 case LPFC_WCQ:
12904 while ((cqe = lpfc_sli4_cq_get(cq))) {
895427bd
JS
12905 if ((cq->subtype == LPFC_FCP) ||
12906 (cq->subtype == LPFC_NVME))
12907 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
0558056c
JS
12908 cqe);
12909 else
12910 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12911 cqe);
73d91e50 12912 if (!(++ecount % cq->entry_repost))
4f774513
JS
12913 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12914 }
b84daac9
JS
12915
12916 /* Track the max number of CQEs processed in 1 EQ */
12917 if (ecount > cq->CQ_max_cqe)
12918 cq->CQ_max_cqe = ecount;
4f774513
JS
12919 break;
12920 default:
12921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12922 "0370 Invalid completion queue type (%d)\n",
12923 cq->type);
12924 return;
12925 }
12926
12927 /* Catch the no cq entry condition, log an error */
12928 if (unlikely(ecount == 0))
12929 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12930 "0371 No entry from the CQ: identifier "
12931 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12932
12933 /* In any case, flash and re-arm the RCQ */
12934 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12935
12936 /* wake up worker thread if there are works to be done */
12937 if (workposted)
12938 lpfc_worker_wake_up(phba);
12939}
12940
12941/**
12942 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
2a76a283
JS
12943 * @phba: Pointer to HBA context object.
12944 * @cq: Pointer to associated CQ
12945 * @wcqe: Pointer to work-queue completion queue entry.
4f774513
JS
12946 *
12947 * This routine process a fast-path work queue completion entry from fast-path
12948 * event queue for FCP command response completion.
12949 **/
12950static void
2a76a283 12951lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
12952 struct lpfc_wcqe_complete *wcqe)
12953{
2a76a283 12954 struct lpfc_sli_ring *pring = cq->pring;
4f774513
JS
12955 struct lpfc_iocbq *cmdiocbq;
12956 struct lpfc_iocbq irspiocbq;
12957 unsigned long iflags;
12958
4f774513
JS
12959 /* Check for response status */
12960 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12961 /* If resource errors reported from HBA, reduce queue
12962 * depth of the SCSI device.
12963 */
e3d2b802
JS
12964 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12965 IOSTAT_LOCAL_REJECT)) &&
12966 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12967 IOERR_NO_RESOURCES))
4f774513 12968 phba->lpfc_rampdown_queue_depth(phba);
e3d2b802 12969
4f774513
JS
12970 /* Log the error status */
12971 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12972 "0373 FCP complete error: status=x%x, "
12973 "hw_status=x%x, total_data_specified=%d, "
12974 "parameter=x%x, word3=x%x\n",
12975 bf_get(lpfc_wcqe_c_status, wcqe),
12976 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12977 wcqe->total_data_placed, wcqe->parameter,
12978 wcqe->word3);
12979 }
12980
12981 /* Look up the FCP command IOCB and create pseudo response IOCB */
7e56aa25
JS
12982 spin_lock_irqsave(&pring->ring_lock, iflags);
12983 pring->stats.iocb_event++;
4f774513
JS
12984 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12985 bf_get(lpfc_wcqe_c_request_tag, wcqe));
7e56aa25 12986 spin_unlock_irqrestore(&pring->ring_lock, iflags);
4f774513
JS
12987 if (unlikely(!cmdiocbq)) {
12988 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12989 "0374 FCP complete with no corresponding "
12990 "cmdiocb: iotag (%d)\n",
12991 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12992 return;
12993 }
895427bd
JS
12994
12995 if (cq->assoc_qp)
12996 cmdiocbq->isr_timestamp =
12997 cq->assoc_qp->isr_timestamp;
12998
12999 if (cmdiocbq->iocb_cmpl == NULL) {
13000 if (cmdiocbq->wqe_cmpl) {
13001 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13002 spin_lock_irqsave(&phba->hbalock, iflags);
13003 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13004 spin_unlock_irqrestore(&phba->hbalock, iflags);
13005 }
13006
13007 /* Pass the cmd_iocb and the wcqe to the upper layer */
13008 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13009 return;
13010 }
4f774513
JS
13011 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13012 "0375 FCP cmdiocb not callback function "
13013 "iotag: (%d)\n",
13014 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13015 return;
13016 }
13017
13018 /* Fake the irspiocb and copy necessary response information */
341af102 13019 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 13020
0f65ff68
JS
13021 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13022 spin_lock_irqsave(&phba->hbalock, iflags);
13023 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13024 spin_unlock_irqrestore(&phba->hbalock, iflags);
13025 }
13026
4f774513
JS
13027 /* Pass the cmd_iocb and the rsp state to the upper layer */
13028 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13029}
13030
13031/**
13032 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13033 * @phba: Pointer to HBA context object.
13034 * @cq: Pointer to completion queue.
13035 * @wcqe: Pointer to work-queue completion queue entry.
13036 *
13037 * This routine handles an fast-path WQ entry comsumed event by invoking the
13038 * proper WQ release routine to the slow-path WQ.
13039 **/
13040static void
13041lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13042 struct lpfc_wcqe_release *wcqe)
13043{
13044 struct lpfc_queue *childwq;
13045 bool wqid_matched = false;
895427bd 13046 uint16_t hba_wqid;
4f774513
JS
13047
13048 /* Check for fast-path FCP work queue release */
895427bd 13049 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
4f774513 13050 list_for_each_entry(childwq, &cq->child_list, list) {
895427bd 13051 if (childwq->queue_id == hba_wqid) {
4f774513
JS
13052 lpfc_sli4_wq_release(childwq,
13053 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13054 wqid_matched = true;
13055 break;
13056 }
13057 }
13058 /* Report warning log message if no match found */
13059 if (wqid_matched != true)
13060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13061 "2580 Fast-path wqe consume event carries "
895427bd 13062 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
4f774513
JS
13063}
13064
2d7dbc4c
JS
13065/**
13066 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13067 * @phba: Pointer to HBA context object.
13068 * @rcqe: Pointer to receive-queue completion queue entry.
13069 *
13070 * This routine process a receive-queue completion queue entry.
13071 *
13072 * Return: true if work posted to worker thread, otherwise false.
13073 **/
13074static bool
13075lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13076 struct lpfc_rcqe *rcqe)
13077{
13078 bool workposted = false;
13079 struct lpfc_queue *hrq;
13080 struct lpfc_queue *drq;
13081 struct rqb_dmabuf *dma_buf;
13082 struct fc_frame_header *fc_hdr;
13083 uint32_t status, rq_id;
13084 unsigned long iflags;
13085 uint32_t fctl, idx;
13086
13087 if ((phba->nvmet_support == 0) ||
13088 (phba->sli4_hba.nvmet_cqset == NULL))
13089 return workposted;
13090
13091 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13092 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13093 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13094
13095 /* sanity check on queue memory */
13096 if (unlikely(!hrq) || unlikely(!drq))
13097 return workposted;
13098
13099 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13100 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13101 else
13102 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13103
13104 if ((phba->nvmet_support == 0) ||
13105 (rq_id != hrq->queue_id))
13106 return workposted;
13107
13108 status = bf_get(lpfc_rcqe_status, rcqe);
13109 switch (status) {
13110 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13112 "6126 Receive Frame Truncated!!\n");
13113 hrq->RQ_buf_trunc++;
13114 break;
13115 case FC_STATUS_RQ_SUCCESS:
13116 lpfc_sli4_rq_release(hrq, drq);
13117 spin_lock_irqsave(&phba->hbalock, iflags);
13118 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13119 if (!dma_buf) {
13120 hrq->RQ_no_buf_found++;
13121 spin_unlock_irqrestore(&phba->hbalock, iflags);
13122 goto out;
13123 }
13124 spin_unlock_irqrestore(&phba->hbalock, iflags);
13125 hrq->RQ_rcv_buf++;
13126 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13127
13128 /* Just some basic sanity checks on FCP Command frame */
13129 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13130 fc_hdr->fh_f_ctl[1] << 8 |
13131 fc_hdr->fh_f_ctl[2]);
13132 if (((fctl &
13133 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13134 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13135 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13136 goto drop;
13137
13138 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13139 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
d613b6a7
JS
13140 lpfc_nvmet_unsol_fcp_event(
13141 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13142 cq->assoc_qp->isr_timestamp);
2d7dbc4c
JS
13143 return false;
13144 }
13145drop:
13146 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13147 break;
13148 case FC_STATUS_INSUFF_BUF_NEED_BUF:
13149 case FC_STATUS_INSUFF_BUF_FRM_DISC:
13150 hrq->RQ_no_posted_buf++;
13151 /* Post more buffers if possible */
13152 spin_lock_irqsave(&phba->hbalock, iflags);
13153 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13154 spin_unlock_irqrestore(&phba->hbalock, iflags);
13155 workposted = true;
13156 break;
13157 }
13158out:
13159 return workposted;
13160}
13161
4f774513 13162/**
895427bd 13163 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
4f774513
JS
13164 * @cq: Pointer to the completion queue.
13165 * @eqe: Pointer to fast-path completion queue entry.
13166 *
13167 * This routine process a fast-path work queue completion entry from fast-path
13168 * event queue for FCP command response completion.
13169 **/
13170static int
895427bd 13171lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
4f774513
JS
13172 struct lpfc_cqe *cqe)
13173{
13174 struct lpfc_wcqe_release wcqe;
13175 bool workposted = false;
13176
13177 /* Copy the work queue CQE and convert endian order if needed */
13178 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13179
13180 /* Check and process for different type of WCQE and dispatch */
13181 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13182 case CQE_CODE_COMPL_WQE:
895427bd 13183 case CQE_CODE_NVME_ERSP:
b84daac9 13184 cq->CQ_wq++;
4f774513 13185 /* Process the WQ complete event */
98fc5dd9 13186 phba->last_completion_time = jiffies;
895427bd
JS
13187 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13188 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13189 (struct lpfc_wcqe_complete *)&wcqe);
13190 if (cq->subtype == LPFC_NVME_LS)
13191 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
4f774513
JS
13192 (struct lpfc_wcqe_complete *)&wcqe);
13193 break;
13194 case CQE_CODE_RELEASE_WQE:
b84daac9 13195 cq->CQ_release_wqe++;
4f774513
JS
13196 /* Process the WQ release event */
13197 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13198 (struct lpfc_wcqe_release *)&wcqe);
13199 break;
13200 case CQE_CODE_XRI_ABORTED:
b84daac9 13201 cq->CQ_xri_aborted++;
4f774513 13202 /* Process the WQ XRI abort event */
bc73905a 13203 phba->last_completion_time = jiffies;
4f774513
JS
13204 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13205 (struct sli4_wcqe_xri_aborted *)&wcqe);
13206 break;
895427bd
JS
13207 case CQE_CODE_RECEIVE_V1:
13208 case CQE_CODE_RECEIVE:
13209 phba->last_completion_time = jiffies;
2d7dbc4c
JS
13210 if (cq->subtype == LPFC_NVMET) {
13211 workposted = lpfc_sli4_nvmet_handle_rcqe(
13212 phba, cq, (struct lpfc_rcqe *)&wcqe);
13213 }
895427bd 13214 break;
4f774513
JS
13215 default:
13216 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
895427bd 13217 "0144 Not a valid CQE code: x%x\n",
4f774513
JS
13218 bf_get(lpfc_wcqe_c_code, &wcqe));
13219 break;
13220 }
13221 return workposted;
13222}
13223
13224/**
67d12733 13225 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
4f774513
JS
13226 * @phba: Pointer to HBA context object.
13227 * @eqe: Pointer to fast-path event queue entry.
13228 *
13229 * This routine process a event queue entry from the fast-path event queue.
13230 * It will check the MajorCode and MinorCode to determine this is for a
13231 * completion event on a completion queue, if not, an error shall be logged
13232 * and just return. Otherwise, it will get to the corresponding completion
13233 * queue and process all the entries on the completion queue, rearm the
13234 * completion queue, and then return.
13235 **/
13236static void
67d12733
JS
13237lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13238 uint32_t qidx)
4f774513 13239{
895427bd 13240 struct lpfc_queue *cq = NULL;
4f774513
JS
13241 struct lpfc_cqe *cqe;
13242 bool workposted = false;
2d7dbc4c 13243 uint16_t cqid, id;
4f774513
JS
13244 int ecount = 0;
13245
cb5172ea 13246 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513 13247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
67d12733 13248 "0366 Not a valid completion "
4f774513 13249 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
13250 bf_get_le32(lpfc_eqe_major_code, eqe),
13251 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
13252 return;
13253 }
13254
67d12733
JS
13255 /* Get the reference to the corresponding CQ */
13256 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13257
2d7dbc4c
JS
13258 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13259 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13260 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13261 /* Process NVMET unsol rcv */
13262 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13263 goto process_cq;
13264 }
13265 }
13266
895427bd
JS
13267 if (phba->sli4_hba.nvme_cq_map &&
13268 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
f358dd0c 13269 /* Process NVME / NVMET command completion */
895427bd
JS
13270 cq = phba->sli4_hba.nvme_cq[qidx];
13271 goto process_cq;
67d12733
JS
13272 }
13273
895427bd
JS
13274 if (phba->sli4_hba.fcp_cq_map &&
13275 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13276 /* Process FCP command completion */
13277 cq = phba->sli4_hba.fcp_cq[qidx];
13278 goto process_cq;
2e90f4b5 13279 }
895427bd
JS
13280
13281 if (phba->sli4_hba.nvmels_cq &&
13282 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13283 /* Process NVME unsol rcv */
13284 cq = phba->sli4_hba.nvmels_cq;
13285 }
13286
13287 /* Otherwise this is a Slow path event */
13288 if (cq == NULL) {
13289 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
4f774513
JS
13290 return;
13291 }
13292
895427bd 13293process_cq:
4f774513
JS
13294 if (unlikely(cqid != cq->queue_id)) {
13295 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13296 "0368 Miss-matched fast-path completion "
13297 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13298 cqid, cq->queue_id);
13299 return;
13300 }
13301
895427bd
JS
13302 /* Save EQ associated with this CQ */
13303 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13304
4f774513
JS
13305 /* Process all the entries to the CQ */
13306 while ((cqe = lpfc_sli4_cq_get(cq))) {
895427bd 13307 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
73d91e50 13308 if (!(++ecount % cq->entry_repost))
4f774513
JS
13309 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13310 }
13311
b84daac9
JS
13312 /* Track the max number of CQEs processed in 1 EQ */
13313 if (ecount > cq->CQ_max_cqe)
13314 cq->CQ_max_cqe = ecount;
13315
4f774513
JS
13316 /* Catch the no cq entry condition */
13317 if (unlikely(ecount == 0))
13318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13319 "0369 No entry from fast-path completion "
13320 "queue fcpcqid=%d\n", cq->queue_id);
13321
13322 /* In any case, flash and re-arm the CQ */
13323 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13324
13325 /* wake up worker thread if there are works to be done */
13326 if (workposted)
13327 lpfc_worker_wake_up(phba);
13328}
13329
13330static void
13331lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13332{
13333 struct lpfc_eqe *eqe;
13334
13335 /* walk all the EQ entries and drop on the floor */
13336 while ((eqe = lpfc_sli4_eq_get(eq)))
13337 ;
13338
13339 /* Clear and re-arm the EQ */
13340 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13341}
13342
1ba981fd
JS
13343
13344/**
13345 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13346 * entry
13347 * @phba: Pointer to HBA context object.
13348 * @eqe: Pointer to fast-path event queue entry.
13349 *
13350 * This routine process a event queue entry from the Flash Optimized Fabric
13351 * event queue. It will check the MajorCode and MinorCode to determine this
13352 * is for a completion event on a completion queue, if not, an error shall be
13353 * logged and just return. Otherwise, it will get to the corresponding
13354 * completion queue and process all the entries on the completion queue, rearm
13355 * the completion queue, and then return.
13356 **/
13357static void
13358lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13359{
13360 struct lpfc_queue *cq;
13361 struct lpfc_cqe *cqe;
13362 bool workposted = false;
13363 uint16_t cqid;
13364 int ecount = 0;
13365
13366 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13368 "9147 Not a valid completion "
13369 "event: majorcode=x%x, minorcode=x%x\n",
13370 bf_get_le32(lpfc_eqe_major_code, eqe),
13371 bf_get_le32(lpfc_eqe_minor_code, eqe));
13372 return;
13373 }
13374
13375 /* Get the reference to the corresponding CQ */
13376 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13377
13378 /* Next check for OAS */
13379 cq = phba->sli4_hba.oas_cq;
13380 if (unlikely(!cq)) {
13381 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13383 "9148 OAS completion queue "
13384 "does not exist\n");
13385 return;
13386 }
13387
13388 if (unlikely(cqid != cq->queue_id)) {
13389 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13390 "9149 Miss-matched fast-path compl "
13391 "queue id: eqcqid=%d, fcpcqid=%d\n",
13392 cqid, cq->queue_id);
13393 return;
13394 }
13395
13396 /* Process all the entries to the OAS CQ */
13397 while ((cqe = lpfc_sli4_cq_get(cq))) {
895427bd 13398 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
1ba981fd
JS
13399 if (!(++ecount % cq->entry_repost))
13400 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13401 }
13402
13403 /* Track the max number of CQEs processed in 1 EQ */
13404 if (ecount > cq->CQ_max_cqe)
13405 cq->CQ_max_cqe = ecount;
13406
13407 /* Catch the no cq entry condition */
13408 if (unlikely(ecount == 0))
13409 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13410 "9153 No entry from fast-path completion "
13411 "queue fcpcqid=%d\n", cq->queue_id);
13412
13413 /* In any case, flash and re-arm the CQ */
13414 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13415
13416 /* wake up worker thread if there are works to be done */
13417 if (workposted)
13418 lpfc_worker_wake_up(phba);
13419}
13420
13421/**
13422 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13423 * @irq: Interrupt number.
13424 * @dev_id: The device context pointer.
13425 *
13426 * This function is directly called from the PCI layer as an interrupt
13427 * service routine when device with SLI-4 interface spec is enabled with
13428 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13429 * IOCB ring event in the HBA. However, when the device is enabled with either
13430 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13431 * device-level interrupt handler. When the PCI slot is in error recovery
13432 * or the HBA is undergoing initialization, the interrupt handler will not
13433 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13434 * the intrrupt context. This function is called without any lock held.
13435 * It gets the hbalock to access and update SLI data structures. Note that,
13436 * the EQ to CQ are one-to-one map such that the EQ index is
13437 * equal to that of CQ index.
13438 *
13439 * This function returns IRQ_HANDLED when interrupt is handled else it
13440 * returns IRQ_NONE.
13441 **/
13442irqreturn_t
13443lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13444{
13445 struct lpfc_hba *phba;
895427bd 13446 struct lpfc_hba_eq_hdl *hba_eq_hdl;
1ba981fd
JS
13447 struct lpfc_queue *eq;
13448 struct lpfc_eqe *eqe;
13449 unsigned long iflag;
13450 int ecount = 0;
1ba981fd
JS
13451
13452 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13453 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13454 phba = hba_eq_hdl->phba;
1ba981fd
JS
13455
13456 if (unlikely(!phba))
13457 return IRQ_NONE;
13458
13459 /* Get to the EQ struct associated with this vector */
13460 eq = phba->sli4_hba.fof_eq;
13461 if (unlikely(!eq))
13462 return IRQ_NONE;
13463
13464 /* Check device state for handling interrupt */
13465 if (unlikely(lpfc_intr_state_check(phba))) {
13466 eq->EQ_badstate++;
13467 /* Check again for link_state with lock held */
13468 spin_lock_irqsave(&phba->hbalock, iflag);
13469 if (phba->link_state < LPFC_LINK_DOWN)
13470 /* Flush, clear interrupt, and rearm the EQ */
13471 lpfc_sli4_eq_flush(phba, eq);
13472 spin_unlock_irqrestore(&phba->hbalock, iflag);
13473 return IRQ_NONE;
13474 }
13475
13476 /*
13477 * Process all the event on FCP fast-path EQ
13478 */
13479 while ((eqe = lpfc_sli4_eq_get(eq))) {
13480 lpfc_sli4_fof_handle_eqe(phba, eqe);
13481 if (!(++ecount % eq->entry_repost))
13482 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13483 eq->EQ_processed++;
13484 }
13485
13486 /* Track the max number of EQEs processed in 1 intr */
13487 if (ecount > eq->EQ_max_eqe)
13488 eq->EQ_max_eqe = ecount;
13489
13490
13491 if (unlikely(ecount == 0)) {
13492 eq->EQ_no_entry++;
13493
13494 if (phba->intr_type == MSIX)
13495 /* MSI-X treated interrupt served as no EQ share INT */
13496 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13497 "9145 MSI-X interrupt with no EQE\n");
13498 else {
13499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13500 "9146 ISR interrupt with no EQE\n");
13501 /* Non MSI-X treated on interrupt as EQ share INT */
13502 return IRQ_NONE;
13503 }
13504 }
13505 /* Always clear and re-arm the fast-path EQ */
13506 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13507 return IRQ_HANDLED;
13508}
13509
4f774513 13510/**
67d12733 13511 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
4f774513
JS
13512 * @irq: Interrupt number.
13513 * @dev_id: The device context pointer.
13514 *
13515 * This function is directly called from the PCI layer as an interrupt
13516 * service routine when device with SLI-4 interface spec is enabled with
13517 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13518 * ring event in the HBA. However, when the device is enabled with either
13519 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13520 * device-level interrupt handler. When the PCI slot is in error recovery
13521 * or the HBA is undergoing initialization, the interrupt handler will not
13522 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13523 * the intrrupt context. This function is called without any lock held.
13524 * It gets the hbalock to access and update SLI data structures. Note that,
13525 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13526 * equal to that of FCP CQ index.
13527 *
67d12733
JS
13528 * The link attention and ELS ring attention events are handled
13529 * by the worker thread. The interrupt handler signals the worker thread
13530 * and returns for these events. This function is called without any lock
13531 * held. It gets the hbalock to access and update SLI data structures.
13532 *
4f774513
JS
13533 * This function returns IRQ_HANDLED when interrupt is handled else it
13534 * returns IRQ_NONE.
13535 **/
13536irqreturn_t
67d12733 13537lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
4f774513
JS
13538{
13539 struct lpfc_hba *phba;
895427bd 13540 struct lpfc_hba_eq_hdl *hba_eq_hdl;
4f774513
JS
13541 struct lpfc_queue *fpeq;
13542 struct lpfc_eqe *eqe;
13543 unsigned long iflag;
13544 int ecount = 0;
895427bd 13545 int hba_eqidx;
4f774513
JS
13546
13547 /* Get the driver's phba structure from the dev_id */
895427bd
JS
13548 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13549 phba = hba_eq_hdl->phba;
13550 hba_eqidx = hba_eq_hdl->idx;
4f774513
JS
13551
13552 if (unlikely(!phba))
13553 return IRQ_NONE;
67d12733 13554 if (unlikely(!phba->sli4_hba.hba_eq))
5350d872 13555 return IRQ_NONE;
4f774513
JS
13556
13557 /* Get to the EQ struct associated with this vector */
895427bd 13558 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
2e90f4b5
JS
13559 if (unlikely(!fpeq))
13560 return IRQ_NONE;
4f774513 13561
bd2cdd5e
JS
13562#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13563 if (phba->ktime_on)
13564 fpeq->isr_timestamp = ktime_get_ns();
13565#endif
13566
ba20c853 13567 if (lpfc_fcp_look_ahead) {
895427bd 13568 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
ba20c853
JS
13569 lpfc_sli4_eq_clr_intr(fpeq);
13570 else {
895427bd 13571 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13572 return IRQ_NONE;
13573 }
13574 }
13575
4f774513
JS
13576 /* Check device state for handling interrupt */
13577 if (unlikely(lpfc_intr_state_check(phba))) {
b84daac9 13578 fpeq->EQ_badstate++;
4f774513
JS
13579 /* Check again for link_state with lock held */
13580 spin_lock_irqsave(&phba->hbalock, iflag);
13581 if (phba->link_state < LPFC_LINK_DOWN)
13582 /* Flush, clear interrupt, and rearm the EQ */
13583 lpfc_sli4_eq_flush(phba, fpeq);
13584 spin_unlock_irqrestore(&phba->hbalock, iflag);
ba20c853 13585 if (lpfc_fcp_look_ahead)
895427bd 13586 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
4f774513
JS
13587 return IRQ_NONE;
13588 }
13589
13590 /*
13591 * Process all the event on FCP fast-path EQ
13592 */
13593 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
eb016566
JS
13594 if (eqe == NULL)
13595 break;
13596
895427bd 13597 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
73d91e50 13598 if (!(++ecount % fpeq->entry_repost))
4f774513 13599 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
b84daac9 13600 fpeq->EQ_processed++;
4f774513
JS
13601 }
13602
b84daac9
JS
13603 /* Track the max number of EQEs processed in 1 intr */
13604 if (ecount > fpeq->EQ_max_eqe)
13605 fpeq->EQ_max_eqe = ecount;
13606
4f774513
JS
13607 /* Always clear and re-arm the fast-path EQ */
13608 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13609
13610 if (unlikely(ecount == 0)) {
b84daac9 13611 fpeq->EQ_no_entry++;
ba20c853
JS
13612
13613 if (lpfc_fcp_look_ahead) {
895427bd 13614 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
ba20c853
JS
13615 return IRQ_NONE;
13616 }
13617
4f774513
JS
13618 if (phba->intr_type == MSIX)
13619 /* MSI-X treated interrupt served as no EQ share INT */
13620 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13621 "0358 MSI-X interrupt with no EQE\n");
13622 else
13623 /* Non MSI-X treated on interrupt as EQ share INT */
13624 return IRQ_NONE;
13625 }
13626
ba20c853 13627 if (lpfc_fcp_look_ahead)
895427bd
JS
13628 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13629
4f774513
JS
13630 return IRQ_HANDLED;
13631} /* lpfc_sli4_fp_intr_handler */
13632
13633/**
13634 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13635 * @irq: Interrupt number.
13636 * @dev_id: The device context pointer.
13637 *
13638 * This function is the device-level interrupt handler to device with SLI-4
13639 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13640 * interrupt mode is enabled and there is an event in the HBA which requires
13641 * driver attention. This function invokes the slow-path interrupt attention
13642 * handling function and fast-path interrupt attention handling function in
13643 * turn to process the relevant HBA attention events. This function is called
13644 * without any lock held. It gets the hbalock to access and update SLI data
13645 * structures.
13646 *
13647 * This function returns IRQ_HANDLED when interrupt is handled, else it
13648 * returns IRQ_NONE.
13649 **/
13650irqreturn_t
13651lpfc_sli4_intr_handler(int irq, void *dev_id)
13652{
13653 struct lpfc_hba *phba;
67d12733
JS
13654 irqreturn_t hba_irq_rc;
13655 bool hba_handled = false;
895427bd 13656 int qidx;
4f774513
JS
13657
13658 /* Get the driver's phba structure from the dev_id */
13659 phba = (struct lpfc_hba *)dev_id;
13660
13661 if (unlikely(!phba))
13662 return IRQ_NONE;
13663
4f774513
JS
13664 /*
13665 * Invoke fast-path host attention interrupt handling as appropriate.
13666 */
895427bd 13667 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
67d12733 13668 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
895427bd 13669 &phba->sli4_hba.hba_eq_hdl[qidx]);
67d12733
JS
13670 if (hba_irq_rc == IRQ_HANDLED)
13671 hba_handled |= true;
4f774513
JS
13672 }
13673
1ba981fd
JS
13674 if (phba->cfg_fof) {
13675 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
895427bd 13676 &phba->sli4_hba.hba_eq_hdl[qidx]);
1ba981fd
JS
13677 if (hba_irq_rc == IRQ_HANDLED)
13678 hba_handled |= true;
13679 }
13680
67d12733 13681 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
4f774513
JS
13682} /* lpfc_sli4_intr_handler */
13683
13684/**
13685 * lpfc_sli4_queue_free - free a queue structure and associated memory
13686 * @queue: The queue structure to free.
13687 *
b595076a 13688 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
13689 * the host resident queue. This function must be called after destroying the
13690 * queue on the HBA.
13691 **/
13692void
13693lpfc_sli4_queue_free(struct lpfc_queue *queue)
13694{
13695 struct lpfc_dmabuf *dmabuf;
13696
13697 if (!queue)
13698 return;
13699
13700 while (!list_empty(&queue->page_list)) {
13701 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13702 list);
49198b37 13703 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
13704 dmabuf->virt, dmabuf->phys);
13705 kfree(dmabuf);
13706 }
895427bd
JS
13707 if (queue->rqbp) {
13708 lpfc_free_rq_buffer(queue->phba, queue);
13709 kfree(queue->rqbp);
13710 }
13711 kfree(queue->pring);
4f774513
JS
13712 kfree(queue);
13713 return;
13714}
13715
13716/**
13717 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13718 * @phba: The HBA that this queue is being created on.
13719 * @entry_size: The size of each queue entry for this queue.
13720 * @entry count: The number of entries that this queue will handle.
13721 *
13722 * This function allocates a queue structure and the DMAable memory used for
13723 * the host resident queue. This function must be called before creating the
13724 * queue on the HBA.
13725 **/
13726struct lpfc_queue *
13727lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13728 uint32_t entry_count)
13729{
13730 struct lpfc_queue *queue;
13731 struct lpfc_dmabuf *dmabuf;
13732 int x, total_qe_count;
13733 void *dma_pointer;
cb5172ea 13734 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 13735
cb5172ea
JS
13736 if (!phba->sli4_hba.pc_sli4_params.supported)
13737 hw_page_size = SLI4_PAGE_SIZE;
13738
4f774513
JS
13739 queue = kzalloc(sizeof(struct lpfc_queue) +
13740 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13741 if (!queue)
13742 return NULL;
cb5172ea
JS
13743 queue->page_count = (ALIGN(entry_size * entry_count,
13744 hw_page_size))/hw_page_size;
895427bd
JS
13745
13746 /* If needed, Adjust page count to match the max the adapter supports */
13747 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13748 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13749
4f774513 13750 INIT_LIST_HEAD(&queue->list);
895427bd 13751 INIT_LIST_HEAD(&queue->wq_list);
4f774513
JS
13752 INIT_LIST_HEAD(&queue->page_list);
13753 INIT_LIST_HEAD(&queue->child_list);
13754 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13755 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13756 if (!dmabuf)
13757 goto out_fail;
1aee383d
JP
13758 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13759 hw_page_size, &dmabuf->phys,
13760 GFP_KERNEL);
4f774513
JS
13761 if (!dmabuf->virt) {
13762 kfree(dmabuf);
13763 goto out_fail;
13764 }
13765 dmabuf->buffer_tag = x;
13766 list_add_tail(&dmabuf->list, &queue->page_list);
13767 /* initialize queue's entry array */
13768 dma_pointer = dmabuf->virt;
13769 for (; total_qe_count < entry_count &&
cb5172ea 13770 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
13771 total_qe_count++, dma_pointer += entry_size) {
13772 queue->qe[total_qe_count].address = dma_pointer;
13773 }
13774 }
13775 queue->entry_size = entry_size;
13776 queue->entry_count = entry_count;
73d91e50
JS
13777
13778 /*
13779 * entry_repost is calculated based on the number of entries in the
13780 * queue. This works out except for RQs. If buffers are NOT initially
13781 * posted for every RQE, entry_repost should be adjusted accordingly.
13782 */
13783 queue->entry_repost = (entry_count >> 3);
13784 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13785 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
13786 queue->phba = phba;
13787
13788 return queue;
13789out_fail:
13790 lpfc_sli4_queue_free(queue);
13791 return NULL;
13792}
13793
962bc51b
JS
13794/**
13795 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13796 * @phba: HBA structure that indicates port to create a queue on.
13797 * @pci_barset: PCI BAR set flag.
13798 *
13799 * This function shall perform iomap of the specified PCI BAR address to host
13800 * memory address if not already done so and return it. The returned host
13801 * memory address can be NULL.
13802 */
13803static void __iomem *
13804lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13805{
962bc51b
JS
13806 if (!phba->pcidev)
13807 return NULL;
962bc51b
JS
13808
13809 switch (pci_barset) {
13810 case WQ_PCI_BAR_0_AND_1:
962bc51b
JS
13811 return phba->pci_bar0_memmap_p;
13812 case WQ_PCI_BAR_2_AND_3:
962bc51b
JS
13813 return phba->pci_bar2_memmap_p;
13814 case WQ_PCI_BAR_4_AND_5:
962bc51b
JS
13815 return phba->pci_bar4_memmap_p;
13816 default:
13817 break;
13818 }
13819 return NULL;
13820}
13821
173edbb2 13822/**
895427bd 13823 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
173edbb2
JS
13824 * @phba: HBA structure that indicates port to create a queue on.
13825 * @startq: The starting FCP EQ to modify
13826 *
13827 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13828 *
13829 * The @phba struct is used to send mailbox command to HBA. The @startq
13830 * is used to get the starting FCP EQ to change.
13831 * This function is asynchronous and will wait for the mailbox
13832 * command to finish before continuing.
13833 *
13834 * On success this function will return a zero. If unable to allocate enough
13835 * memory this function will return -ENOMEM. If the queue create mailbox command
13836 * fails this function will return -ENXIO.
13837 **/
a2fc4aef 13838int
895427bd 13839lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
173edbb2
JS
13840{
13841 struct lpfc_mbx_modify_eq_delay *eq_delay;
13842 LPFC_MBOXQ_t *mbox;
13843 struct lpfc_queue *eq;
13844 int cnt, rc, length, status = 0;
13845 uint32_t shdr_status, shdr_add_status;
ee02006b 13846 uint32_t result;
895427bd 13847 int qidx;
173edbb2
JS
13848 union lpfc_sli4_cfg_shdr *shdr;
13849 uint16_t dmult;
13850
895427bd 13851 if (startq >= phba->io_channel_irqs)
173edbb2
JS
13852 return 0;
13853
13854 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13855 if (!mbox)
13856 return -ENOMEM;
13857 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13858 sizeof(struct lpfc_sli4_cfg_mhdr));
13859 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13860 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13861 length, LPFC_SLI4_MBX_EMBED);
13862 eq_delay = &mbox->u.mqe.un.eq_delay;
13863
13864 /* Calculate delay multiper from maximum interrupt per second */
895427bd
JS
13865 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13866 if (result > LPFC_DMULT_CONST || result == 0)
ee02006b
JS
13867 dmult = 0;
13868 else
13869 dmult = LPFC_DMULT_CONST/result - 1;
173edbb2
JS
13870
13871 cnt = 0;
895427bd
JS
13872 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13873 eq = phba->sli4_hba.hba_eq[qidx];
173edbb2
JS
13874 if (!eq)
13875 continue;
13876 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13877 eq_delay->u.request.eq[cnt].phase = 0;
13878 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13879 cnt++;
13880 if (cnt >= LPFC_MAX_EQ_DELAY)
13881 break;
13882 }
13883 eq_delay->u.request.num_eq = cnt;
13884
13885 mbox->vport = phba->pport;
13886 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13887 mbox->context1 = NULL;
13888 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13889 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13890 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13891 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13892 if (shdr_status || shdr_add_status || rc) {
13893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13894 "2512 MODIFY_EQ_DELAY mailbox failed with "
13895 "status x%x add_status x%x, mbx status x%x\n",
13896 shdr_status, shdr_add_status, rc);
13897 status = -ENXIO;
13898 }
13899 mempool_free(mbox, phba->mbox_mem_pool);
13900 return status;
13901}
13902
4f774513
JS
13903/**
13904 * lpfc_eq_create - Create an Event Queue on the HBA
13905 * @phba: HBA structure that indicates port to create a queue on.
13906 * @eq: The queue structure to use to create the event queue.
13907 * @imax: The maximum interrupt per second limit.
13908 *
13909 * This function creates an event queue, as detailed in @eq, on a port,
13910 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13911 *
13912 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13913 * is used to get the entry count and entry size that are necessary to
13914 * determine the number of pages to allocate and use for this queue. This
13915 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13916 * event queue. This function is asynchronous and will wait for the mailbox
13917 * command to finish before continuing.
13918 *
13919 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
13920 * memory this function will return -ENOMEM. If the queue create mailbox command
13921 * fails this function will return -ENXIO.
4f774513 13922 **/
a2fc4aef 13923int
ee02006b 13924lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
4f774513
JS
13925{
13926 struct lpfc_mbx_eq_create *eq_create;
13927 LPFC_MBOXQ_t *mbox;
13928 int rc, length, status = 0;
13929 struct lpfc_dmabuf *dmabuf;
13930 uint32_t shdr_status, shdr_add_status;
13931 union lpfc_sli4_cfg_shdr *shdr;
13932 uint16_t dmult;
49198b37
JS
13933 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13934
2e90f4b5
JS
13935 /* sanity check on queue memory */
13936 if (!eq)
13937 return -ENODEV;
49198b37
JS
13938 if (!phba->sli4_hba.pc_sli4_params.supported)
13939 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
13940
13941 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13942 if (!mbox)
13943 return -ENOMEM;
13944 length = (sizeof(struct lpfc_mbx_eq_create) -
13945 sizeof(struct lpfc_sli4_cfg_mhdr));
13946 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13947 LPFC_MBOX_OPCODE_EQ_CREATE,
13948 length, LPFC_SLI4_MBX_EMBED);
13949 eq_create = &mbox->u.mqe.un.eq_create;
13950 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13951 eq->page_count);
13952 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13953 LPFC_EQE_SIZE);
13954 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
2c9c5a00
JS
13955 /* don't setup delay multiplier using EQ_CREATE */
13956 dmult = 0;
4f774513
JS
13957 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13958 dmult);
13959 switch (eq->entry_count) {
13960 default:
13961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13962 "0360 Unsupported EQ count. (%d)\n",
13963 eq->entry_count);
13964 if (eq->entry_count < 256)
13965 return -EINVAL;
13966 /* otherwise default to smallest count (drop through) */
13967 case 256:
13968 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13969 LPFC_EQ_CNT_256);
13970 break;
13971 case 512:
13972 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13973 LPFC_EQ_CNT_512);
13974 break;
13975 case 1024:
13976 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13977 LPFC_EQ_CNT_1024);
13978 break;
13979 case 2048:
13980 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13981 LPFC_EQ_CNT_2048);
13982 break;
13983 case 4096:
13984 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13985 LPFC_EQ_CNT_4096);
13986 break;
13987 }
13988 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 13989 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
13990 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13991 putPaddrLow(dmabuf->phys);
13992 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13993 putPaddrHigh(dmabuf->phys);
13994 }
13995 mbox->vport = phba->pport;
13996 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13997 mbox->context1 = NULL;
13998 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13999 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14002 if (shdr_status || shdr_add_status || rc) {
14003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14004 "2500 EQ_CREATE mailbox failed with "
14005 "status x%x add_status x%x, mbx status x%x\n",
14006 shdr_status, shdr_add_status, rc);
14007 status = -ENXIO;
14008 }
14009 eq->type = LPFC_EQ;
14010 eq->subtype = LPFC_NONE;
14011 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14012 if (eq->queue_id == 0xFFFF)
14013 status = -ENXIO;
14014 eq->host_index = 0;
14015 eq->hba_index = 0;
14016
8fa38513 14017 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14018 return status;
14019}
14020
14021/**
14022 * lpfc_cq_create - Create a Completion Queue on the HBA
14023 * @phba: HBA structure that indicates port to create a queue on.
14024 * @cq: The queue structure to use to create the completion queue.
14025 * @eq: The event queue to bind this completion queue to.
14026 *
14027 * This function creates a completion queue, as detailed in @wq, on a port,
14028 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14029 *
14030 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14031 * is used to get the entry count and entry size that are necessary to
14032 * determine the number of pages to allocate and use for this queue. The @eq
14033 * is used to indicate which event queue to bind this completion queue to. This
14034 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14035 * completion queue. This function is asynchronous and will wait for the mailbox
14036 * command to finish before continuing.
14037 *
14038 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14039 * memory this function will return -ENOMEM. If the queue create mailbox command
14040 * fails this function will return -ENXIO.
4f774513 14041 **/
a2fc4aef 14042int
4f774513
JS
14043lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14044 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14045{
14046 struct lpfc_mbx_cq_create *cq_create;
14047 struct lpfc_dmabuf *dmabuf;
14048 LPFC_MBOXQ_t *mbox;
14049 int rc, length, status = 0;
14050 uint32_t shdr_status, shdr_add_status;
14051 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
14052 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14053
2e90f4b5
JS
14054 /* sanity check on queue memory */
14055 if (!cq || !eq)
14056 return -ENODEV;
49198b37
JS
14057 if (!phba->sli4_hba.pc_sli4_params.supported)
14058 hw_page_size = SLI4_PAGE_SIZE;
14059
4f774513
JS
14060 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14061 if (!mbox)
14062 return -ENOMEM;
14063 length = (sizeof(struct lpfc_mbx_cq_create) -
14064 sizeof(struct lpfc_sli4_cfg_mhdr));
14065 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14066 LPFC_MBOX_OPCODE_CQ_CREATE,
14067 length, LPFC_SLI4_MBX_EMBED);
14068 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 14069 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
14070 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14071 cq->page_count);
14072 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14073 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
14074 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14075 phba->sli4_hba.pc_sli4_params.cqv);
14076 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
14077 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14078 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
14079 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14080 eq->queue_id);
14081 } else {
14082 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14083 eq->queue_id);
14084 }
4f774513
JS
14085 switch (cq->entry_count) {
14086 default:
14087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2ea259ee
JS
14088 "0361 Unsupported CQ count: "
14089 "entry cnt %d sz %d pg cnt %d repost %d\n",
14090 cq->entry_count, cq->entry_size,
14091 cq->page_count, cq->entry_repost);
4f4c1863
JS
14092 if (cq->entry_count < 256) {
14093 status = -EINVAL;
14094 goto out;
14095 }
4f774513
JS
14096 /* otherwise default to smallest count (drop through) */
14097 case 256:
14098 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14099 LPFC_CQ_CNT_256);
14100 break;
14101 case 512:
14102 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14103 LPFC_CQ_CNT_512);
14104 break;
14105 case 1024:
14106 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14107 LPFC_CQ_CNT_1024);
14108 break;
14109 }
14110 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 14111 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14112 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14113 putPaddrLow(dmabuf->phys);
14114 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14115 putPaddrHigh(dmabuf->phys);
14116 }
14117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14118
14119 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14120 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14121 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14122 if (shdr_status || shdr_add_status || rc) {
14123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14124 "2501 CQ_CREATE mailbox failed with "
14125 "status x%x add_status x%x, mbx status x%x\n",
14126 shdr_status, shdr_add_status, rc);
14127 status = -ENXIO;
14128 goto out;
14129 }
14130 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14131 if (cq->queue_id == 0xFFFF) {
14132 status = -ENXIO;
14133 goto out;
14134 }
14135 /* link the cq onto the parent eq child list */
14136 list_add_tail(&cq->list, &eq->child_list);
14137 /* Set up completion queue's type and subtype */
14138 cq->type = type;
14139 cq->subtype = subtype;
14140 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 14141 cq->assoc_qid = eq->queue_id;
4f774513
JS
14142 cq->host_index = 0;
14143 cq->hba_index = 0;
4f774513 14144
8fa38513
JS
14145out:
14146 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14147 return status;
14148}
14149
2d7dbc4c
JS
14150/**
14151 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14152 * @phba: HBA structure that indicates port to create a queue on.
14153 * @cqp: The queue structure array to use to create the completion queues.
14154 * @eqp: The event queue array to bind these completion queues to.
14155 *
14156 * This function creates a set of completion queue, s to support MRQ
14157 * as detailed in @cqp, on a port,
14158 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14159 *
14160 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14161 * is used to get the entry count and entry size that are necessary to
14162 * determine the number of pages to allocate and use for this queue. The @eq
14163 * is used to indicate which event queue to bind this completion queue to. This
14164 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14165 * completion queue. This function is asynchronous and will wait for the mailbox
14166 * command to finish before continuing.
14167 *
14168 * On success this function will return a zero. If unable to allocate enough
14169 * memory this function will return -ENOMEM. If the queue create mailbox command
14170 * fails this function will return -ENXIO.
14171 **/
14172int
14173lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14174 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14175{
14176 struct lpfc_queue *cq;
14177 struct lpfc_queue *eq;
14178 struct lpfc_mbx_cq_create_set *cq_set;
14179 struct lpfc_dmabuf *dmabuf;
14180 LPFC_MBOXQ_t *mbox;
14181 int rc, length, alloclen, status = 0;
14182 int cnt, idx, numcq, page_idx = 0;
14183 uint32_t shdr_status, shdr_add_status;
14184 union lpfc_sli4_cfg_shdr *shdr;
14185 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14186
14187 /* sanity check on queue memory */
14188 numcq = phba->cfg_nvmet_mrq;
14189 if (!cqp || !eqp || !numcq)
14190 return -ENODEV;
14191 if (!phba->sli4_hba.pc_sli4_params.supported)
14192 hw_page_size = SLI4_PAGE_SIZE;
14193
14194 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14195 if (!mbox)
14196 return -ENOMEM;
14197
14198 length = sizeof(struct lpfc_mbx_cq_create_set);
14199 length += ((numcq * cqp[0]->page_count) *
14200 sizeof(struct dma_address));
14201 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14202 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14203 LPFC_SLI4_MBX_NEMBED);
14204 if (alloclen < length) {
14205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14206 "3098 Allocated DMA memory size (%d) is "
14207 "less than the requested DMA memory size "
14208 "(%d)\n", alloclen, length);
14209 status = -ENOMEM;
14210 goto out;
14211 }
14212 cq_set = mbox->sge_array->addr[0];
14213 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14214 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14215
14216 for (idx = 0; idx < numcq; idx++) {
14217 cq = cqp[idx];
14218 eq = eqp[idx];
14219 if (!cq || !eq) {
14220 status = -ENOMEM;
14221 goto out;
14222 }
14223
14224 switch (idx) {
14225 case 0:
14226 bf_set(lpfc_mbx_cq_create_set_page_size,
14227 &cq_set->u.request,
14228 (hw_page_size / SLI4_PAGE_SIZE));
14229 bf_set(lpfc_mbx_cq_create_set_num_pages,
14230 &cq_set->u.request, cq->page_count);
14231 bf_set(lpfc_mbx_cq_create_set_evt,
14232 &cq_set->u.request, 1);
14233 bf_set(lpfc_mbx_cq_create_set_valid,
14234 &cq_set->u.request, 1);
14235 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14236 &cq_set->u.request, 0);
14237 bf_set(lpfc_mbx_cq_create_set_num_cq,
14238 &cq_set->u.request, numcq);
14239 switch (cq->entry_count) {
14240 default:
14241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14242 "3118 Bad CQ count. (%d)\n",
14243 cq->entry_count);
14244 if (cq->entry_count < 256) {
14245 status = -EINVAL;
14246 goto out;
14247 }
14248 /* otherwise default to smallest (drop thru) */
14249 case 256:
14250 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14251 &cq_set->u.request, LPFC_CQ_CNT_256);
14252 break;
14253 case 512:
14254 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14255 &cq_set->u.request, LPFC_CQ_CNT_512);
14256 break;
14257 case 1024:
14258 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14259 &cq_set->u.request, LPFC_CQ_CNT_1024);
14260 break;
14261 }
14262 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14263 &cq_set->u.request, eq->queue_id);
14264 break;
14265 case 1:
14266 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14267 &cq_set->u.request, eq->queue_id);
14268 break;
14269 case 2:
14270 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14271 &cq_set->u.request, eq->queue_id);
14272 break;
14273 case 3:
14274 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14275 &cq_set->u.request, eq->queue_id);
14276 break;
14277 case 4:
14278 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14279 &cq_set->u.request, eq->queue_id);
14280 break;
14281 case 5:
14282 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14283 &cq_set->u.request, eq->queue_id);
14284 break;
14285 case 6:
14286 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14287 &cq_set->u.request, eq->queue_id);
14288 break;
14289 case 7:
14290 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14291 &cq_set->u.request, eq->queue_id);
14292 break;
14293 case 8:
14294 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14295 &cq_set->u.request, eq->queue_id);
14296 break;
14297 case 9:
14298 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14299 &cq_set->u.request, eq->queue_id);
14300 break;
14301 case 10:
14302 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14303 &cq_set->u.request, eq->queue_id);
14304 break;
14305 case 11:
14306 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14307 &cq_set->u.request, eq->queue_id);
14308 break;
14309 case 12:
14310 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14311 &cq_set->u.request, eq->queue_id);
14312 break;
14313 case 13:
14314 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14315 &cq_set->u.request, eq->queue_id);
14316 break;
14317 case 14:
14318 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14319 &cq_set->u.request, eq->queue_id);
14320 break;
14321 case 15:
14322 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14323 &cq_set->u.request, eq->queue_id);
14324 break;
14325 }
14326
14327 /* link the cq onto the parent eq child list */
14328 list_add_tail(&cq->list, &eq->child_list);
14329 /* Set up completion queue's type and subtype */
14330 cq->type = type;
14331 cq->subtype = subtype;
14332 cq->assoc_qid = eq->queue_id;
14333 cq->host_index = 0;
14334 cq->hba_index = 0;
14335
14336 rc = 0;
14337 list_for_each_entry(dmabuf, &cq->page_list, list) {
14338 memset(dmabuf->virt, 0, hw_page_size);
14339 cnt = page_idx + dmabuf->buffer_tag;
14340 cq_set->u.request.page[cnt].addr_lo =
14341 putPaddrLow(dmabuf->phys);
14342 cq_set->u.request.page[cnt].addr_hi =
14343 putPaddrHigh(dmabuf->phys);
14344 rc++;
14345 }
14346 page_idx += rc;
14347 }
14348
14349 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14350
14351 /* The IOCTL status is embedded in the mailbox subheader. */
14352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14354 if (shdr_status || shdr_add_status || rc) {
14355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14356 "3119 CQ_CREATE_SET mailbox failed with "
14357 "status x%x add_status x%x, mbx status x%x\n",
14358 shdr_status, shdr_add_status, rc);
14359 status = -ENXIO;
14360 goto out;
14361 }
14362 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14363 if (rc == 0xFFFF) {
14364 status = -ENXIO;
14365 goto out;
14366 }
14367
14368 for (idx = 0; idx < numcq; idx++) {
14369 cq = cqp[idx];
14370 cq->queue_id = rc + idx;
14371 }
14372
14373out:
14374 lpfc_sli4_mbox_cmd_free(phba, mbox);
14375 return status;
14376}
14377
b19a061a
JS
14378/**
14379 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14380 * @phba: HBA structure that indicates port to create a queue on.
14381 * @mq: The queue structure to use to create the mailbox queue.
14382 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14383 * @cq: The completion queue to associate with this cq.
14384 *
14385 * This function provides failback (fb) functionality when the
14386 * mq_create_ext fails on older FW generations. It's purpose is identical
14387 * to mq_create_ext otherwise.
14388 *
14389 * This routine cannot fail as all attributes were previously accessed and
14390 * initialized in mq_create_ext.
14391 **/
14392static void
14393lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14394 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14395{
14396 struct lpfc_mbx_mq_create *mq_create;
14397 struct lpfc_dmabuf *dmabuf;
14398 int length;
14399
14400 length = (sizeof(struct lpfc_mbx_mq_create) -
14401 sizeof(struct lpfc_sli4_cfg_mhdr));
14402 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14403 LPFC_MBOX_OPCODE_MQ_CREATE,
14404 length, LPFC_SLI4_MBX_EMBED);
14405 mq_create = &mbox->u.mqe.un.mq_create;
14406 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14407 mq->page_count);
14408 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14409 cq->queue_id);
14410 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14411 switch (mq->entry_count) {
14412 case 16:
5a6f133e
JS
14413 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14414 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
14415 break;
14416 case 32:
5a6f133e
JS
14417 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14418 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
14419 break;
14420 case 64:
5a6f133e
JS
14421 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14422 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
14423 break;
14424 case 128:
5a6f133e
JS
14425 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14426 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
14427 break;
14428 }
14429 list_for_each_entry(dmabuf, &mq->page_list, list) {
14430 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14431 putPaddrLow(dmabuf->phys);
14432 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14433 putPaddrHigh(dmabuf->phys);
14434 }
14435}
14436
04c68496
JS
14437/**
14438 * lpfc_mq_create - Create a mailbox Queue on the HBA
14439 * @phba: HBA structure that indicates port to create a queue on.
14440 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
14441 * @cq: The completion queue to associate with this cq.
14442 * @subtype: The queue's subtype.
04c68496
JS
14443 *
14444 * This function creates a mailbox queue, as detailed in @mq, on a port,
14445 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14446 *
14447 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14448 * is used to get the entry count and entry size that are necessary to
14449 * determine the number of pages to allocate and use for this queue. This
14450 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14451 * mailbox queue. This function is asynchronous and will wait for the mailbox
14452 * command to finish before continuing.
14453 *
14454 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14455 * memory this function will return -ENOMEM. If the queue create mailbox command
14456 * fails this function will return -ENXIO.
04c68496 14457 **/
b19a061a 14458int32_t
04c68496
JS
14459lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14460 struct lpfc_queue *cq, uint32_t subtype)
14461{
14462 struct lpfc_mbx_mq_create *mq_create;
b19a061a 14463 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
14464 struct lpfc_dmabuf *dmabuf;
14465 LPFC_MBOXQ_t *mbox;
14466 int rc, length, status = 0;
14467 uint32_t shdr_status, shdr_add_status;
14468 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14469 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 14470
2e90f4b5
JS
14471 /* sanity check on queue memory */
14472 if (!mq || !cq)
14473 return -ENODEV;
49198b37
JS
14474 if (!phba->sli4_hba.pc_sli4_params.supported)
14475 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 14476
04c68496
JS
14477 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14478 if (!mbox)
14479 return -ENOMEM;
b19a061a 14480 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
14481 sizeof(struct lpfc_sli4_cfg_mhdr));
14482 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 14483 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 14484 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
14485
14486 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 14487 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
14488 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14489 &mq_create_ext->u.request, mq->page_count);
14490 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14491 &mq_create_ext->u.request, 1);
14492 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
14493 &mq_create_ext->u.request, 1);
14494 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14495 &mq_create_ext->u.request, 1);
70f3c073
JS
14496 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14497 &mq_create_ext->u.request, 1);
14498 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14499 &mq_create_ext->u.request, 1);
b19a061a 14500 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
14501 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14502 phba->sli4_hba.pc_sli4_params.mqv);
14503 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14504 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14505 cq->queue_id);
14506 else
14507 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14508 cq->queue_id);
04c68496
JS
14509 switch (mq->entry_count) {
14510 default:
14511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14512 "0362 Unsupported MQ count. (%d)\n",
14513 mq->entry_count);
4f4c1863
JS
14514 if (mq->entry_count < 16) {
14515 status = -EINVAL;
14516 goto out;
14517 }
04c68496
JS
14518 /* otherwise default to smallest count (drop through) */
14519 case 16:
5a6f133e
JS
14520 bf_set(lpfc_mq_context_ring_size,
14521 &mq_create_ext->u.request.context,
14522 LPFC_MQ_RING_SIZE_16);
04c68496
JS
14523 break;
14524 case 32:
5a6f133e
JS
14525 bf_set(lpfc_mq_context_ring_size,
14526 &mq_create_ext->u.request.context,
14527 LPFC_MQ_RING_SIZE_32);
04c68496
JS
14528 break;
14529 case 64:
5a6f133e
JS
14530 bf_set(lpfc_mq_context_ring_size,
14531 &mq_create_ext->u.request.context,
14532 LPFC_MQ_RING_SIZE_64);
04c68496
JS
14533 break;
14534 case 128:
5a6f133e
JS
14535 bf_set(lpfc_mq_context_ring_size,
14536 &mq_create_ext->u.request.context,
14537 LPFC_MQ_RING_SIZE_128);
04c68496
JS
14538 break;
14539 }
14540 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 14541 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 14542 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 14543 putPaddrLow(dmabuf->phys);
b19a061a 14544 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
14545 putPaddrHigh(dmabuf->phys);
14546 }
14547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
14548 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14549 &mq_create_ext->u.response);
14550 if (rc != MBX_SUCCESS) {
14551 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14552 "2795 MQ_CREATE_EXT failed with "
14553 "status x%x. Failback to MQ_CREATE.\n",
14554 rc);
14555 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14556 mq_create = &mbox->u.mqe.un.mq_create;
14557 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14558 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14559 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14560 &mq_create->u.response);
14561 }
14562
04c68496 14563 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
14564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14566 if (shdr_status || shdr_add_status || rc) {
14567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14568 "2502 MQ_CREATE mailbox failed with "
14569 "status x%x add_status x%x, mbx status x%x\n",
14570 shdr_status, shdr_add_status, rc);
14571 status = -ENXIO;
14572 goto out;
14573 }
04c68496
JS
14574 if (mq->queue_id == 0xFFFF) {
14575 status = -ENXIO;
14576 goto out;
14577 }
14578 mq->type = LPFC_MQ;
2a622bfb 14579 mq->assoc_qid = cq->queue_id;
04c68496
JS
14580 mq->subtype = subtype;
14581 mq->host_index = 0;
14582 mq->hba_index = 0;
14583
14584 /* link the mq onto the parent cq child list */
14585 list_add_tail(&mq->list, &cq->child_list);
14586out:
8fa38513 14587 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
14588 return status;
14589}
14590
4f774513
JS
14591/**
14592 * lpfc_wq_create - Create a Work Queue on the HBA
14593 * @phba: HBA structure that indicates port to create a queue on.
14594 * @wq: The queue structure to use to create the work queue.
14595 * @cq: The completion queue to bind this work queue to.
14596 * @subtype: The subtype of the work queue indicating its functionality.
14597 *
14598 * This function creates a work queue, as detailed in @wq, on a port, described
14599 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14600 *
14601 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14602 * is used to get the entry count and entry size that are necessary to
14603 * determine the number of pages to allocate and use for this queue. The @cq
14604 * is used to indicate which completion queue to bind this work queue to. This
14605 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14606 * work queue. This function is asynchronous and will wait for the mailbox
14607 * command to finish before continuing.
14608 *
14609 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14610 * memory this function will return -ENOMEM. If the queue create mailbox command
14611 * fails this function will return -ENXIO.
4f774513 14612 **/
a2fc4aef 14613int
4f774513
JS
14614lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14615 struct lpfc_queue *cq, uint32_t subtype)
14616{
14617 struct lpfc_mbx_wq_create *wq_create;
14618 struct lpfc_dmabuf *dmabuf;
14619 LPFC_MBOXQ_t *mbox;
14620 int rc, length, status = 0;
14621 uint32_t shdr_status, shdr_add_status;
14622 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14623 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 14624 struct dma_address *page;
962bc51b
JS
14625 void __iomem *bar_memmap_p;
14626 uint32_t db_offset;
14627 uint16_t pci_barset;
49198b37 14628
2e90f4b5
JS
14629 /* sanity check on queue memory */
14630 if (!wq || !cq)
14631 return -ENODEV;
49198b37
JS
14632 if (!phba->sli4_hba.pc_sli4_params.supported)
14633 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14634
14635 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14636 if (!mbox)
14637 return -ENOMEM;
14638 length = (sizeof(struct lpfc_mbx_wq_create) -
14639 sizeof(struct lpfc_sli4_cfg_mhdr));
14640 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14641 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14642 length, LPFC_SLI4_MBX_EMBED);
14643 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 14644 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
14645 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14646 wq->page_count);
14647 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14648 cq->queue_id);
0c651878
JS
14649
14650 /* wqv is the earliest version supported, NOT the latest */
5a6f133e
JS
14651 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14652 phba->sli4_hba.pc_sli4_params.wqv);
962bc51b 14653
0c651878
JS
14654 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14655 case LPFC_Q_CREATE_VERSION_0:
14656 switch (wq->entry_size) {
14657 default:
14658 case 64:
14659 /* Nothing to do, version 0 ONLY supports 64 byte */
14660 page = wq_create->u.request.page;
14661 break;
14662 case 128:
14663 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14664 LPFC_WQ_SZ128_SUPPORT)) {
14665 status = -ERANGE;
14666 goto out;
14667 }
14668 /* If we get here the HBA MUST also support V1 and
14669 * we MUST use it
14670 */
14671 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14672 LPFC_Q_CREATE_VERSION_1);
14673
14674 bf_set(lpfc_mbx_wq_create_wqe_count,
14675 &wq_create->u.request_1, wq->entry_count);
14676 bf_set(lpfc_mbx_wq_create_wqe_size,
14677 &wq_create->u.request_1,
14678 LPFC_WQ_WQE_SIZE_128);
14679 bf_set(lpfc_mbx_wq_create_page_size,
14680 &wq_create->u.request_1,
8ea73db4 14681 LPFC_WQ_PAGE_SIZE_4096);
0c651878
JS
14682 page = wq_create->u.request_1.page;
14683 break;
14684 }
14685 break;
14686 case LPFC_Q_CREATE_VERSION_1:
5a6f133e
JS
14687 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14688 wq->entry_count);
14689 switch (wq->entry_size) {
14690 default:
14691 case 64:
14692 bf_set(lpfc_mbx_wq_create_wqe_size,
14693 &wq_create->u.request_1,
14694 LPFC_WQ_WQE_SIZE_64);
14695 break;
14696 case 128:
0c651878
JS
14697 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14698 LPFC_WQ_SZ128_SUPPORT)) {
14699 status = -ERANGE;
14700 goto out;
14701 }
5a6f133e
JS
14702 bf_set(lpfc_mbx_wq_create_wqe_size,
14703 &wq_create->u.request_1,
14704 LPFC_WQ_WQE_SIZE_128);
14705 break;
14706 }
8ea73db4
JS
14707 bf_set(lpfc_mbx_wq_create_page_size,
14708 &wq_create->u.request_1,
14709 LPFC_WQ_PAGE_SIZE_4096);
5a6f133e 14710 page = wq_create->u.request_1.page;
0c651878
JS
14711 break;
14712 default:
14713 status = -ERANGE;
14714 goto out;
5a6f133e 14715 }
0c651878 14716
4f774513 14717 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 14718 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
14719 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14720 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513 14721 }
962bc51b
JS
14722
14723 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14724 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14725
4f774513
JS
14726 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14727 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14728 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14729 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14730 if (shdr_status || shdr_add_status || rc) {
14731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14732 "2503 WQ_CREATE mailbox failed with "
14733 "status x%x add_status x%x, mbx status x%x\n",
14734 shdr_status, shdr_add_status, rc);
14735 status = -ENXIO;
14736 goto out;
14737 }
14738 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14739 if (wq->queue_id == 0xFFFF) {
14740 status = -ENXIO;
14741 goto out;
14742 }
962bc51b
JS
14743 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14744 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14745 &wq_create->u.response);
14746 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14747 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14749 "3265 WQ[%d] doorbell format not "
14750 "supported: x%x\n", wq->queue_id,
14751 wq->db_format);
14752 status = -EINVAL;
14753 goto out;
14754 }
14755 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14756 &wq_create->u.response);
14757 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14758 if (!bar_memmap_p) {
14759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14760 "3263 WQ[%d] failed to memmap pci "
14761 "barset:x%x\n", wq->queue_id,
14762 pci_barset);
14763 status = -ENOMEM;
14764 goto out;
14765 }
14766 db_offset = wq_create->u.response.doorbell_offset;
14767 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14768 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14770 "3252 WQ[%d] doorbell offset not "
14771 "supported: x%x\n", wq->queue_id,
14772 db_offset);
14773 status = -EINVAL;
14774 goto out;
14775 }
14776 wq->db_regaddr = bar_memmap_p + db_offset;
14777 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
14778 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14779 "format:x%x\n", wq->queue_id, pci_barset,
14780 db_offset, wq->db_format);
962bc51b
JS
14781 } else {
14782 wq->db_format = LPFC_DB_LIST_FORMAT;
14783 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14784 }
895427bd
JS
14785 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14786 if (wq->pring == NULL) {
14787 status = -ENOMEM;
14788 goto out;
14789 }
4f774513 14790 wq->type = LPFC_WQ;
2a622bfb 14791 wq->assoc_qid = cq->queue_id;
4f774513
JS
14792 wq->subtype = subtype;
14793 wq->host_index = 0;
14794 wq->hba_index = 0;
ff78d8f9 14795 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
4f774513
JS
14796
14797 /* link the wq onto the parent cq child list */
14798 list_add_tail(&wq->list, &cq->child_list);
14799out:
8fa38513 14800 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
14801 return status;
14802}
14803
73d91e50
JS
14804/**
14805 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14806 * @phba: HBA structure that indicates port to create a queue on.
14807 * @rq: The queue structure to use for the receive queue.
14808 * @qno: The associated HBQ number
14809 *
14810 *
14811 * For SLI4 we need to adjust the RQ repost value based on
14812 * the number of buffers that are initially posted to the RQ.
14813 */
14814void
14815lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14816{
14817 uint32_t cnt;
14818
2e90f4b5
JS
14819 /* sanity check on queue memory */
14820 if (!rq)
14821 return;
73d91e50
JS
14822 cnt = lpfc_hbq_defs[qno]->entry_count;
14823
14824 /* Recalc repost for RQs based on buffers initially posted */
14825 cnt = (cnt >> 3);
14826 if (cnt < LPFC_QUEUE_MIN_REPOST)
14827 cnt = LPFC_QUEUE_MIN_REPOST;
14828
14829 rq->entry_repost = cnt;
14830}
14831
4f774513
JS
14832/**
14833 * lpfc_rq_create - Create a Receive Queue on the HBA
14834 * @phba: HBA structure that indicates port to create a queue on.
14835 * @hrq: The queue structure to use to create the header receive queue.
14836 * @drq: The queue structure to use to create the data receive queue.
14837 * @cq: The completion queue to bind this work queue to.
14838 *
14839 * This function creates a receive buffer queue pair , as detailed in @hrq and
14840 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14841 * to the HBA.
14842 *
14843 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14844 * struct is used to get the entry count that is necessary to determine the
14845 * number of pages to use for this queue. The @cq is used to indicate which
14846 * completion queue to bind received buffers that are posted to these queues to.
14847 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14848 * receive queue pair. This function is asynchronous and will wait for the
14849 * mailbox command to finish before continuing.
14850 *
14851 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
14852 * memory this function will return -ENOMEM. If the queue create mailbox command
14853 * fails this function will return -ENXIO.
4f774513 14854 **/
a2fc4aef 14855int
4f774513
JS
14856lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14857 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14858{
14859 struct lpfc_mbx_rq_create *rq_create;
14860 struct lpfc_dmabuf *dmabuf;
14861 LPFC_MBOXQ_t *mbox;
14862 int rc, length, status = 0;
14863 uint32_t shdr_status, shdr_add_status;
14864 union lpfc_sli4_cfg_shdr *shdr;
49198b37 14865 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
962bc51b
JS
14866 void __iomem *bar_memmap_p;
14867 uint32_t db_offset;
14868 uint16_t pci_barset;
49198b37 14869
2e90f4b5
JS
14870 /* sanity check on queue memory */
14871 if (!hrq || !drq || !cq)
14872 return -ENODEV;
49198b37
JS
14873 if (!phba->sli4_hba.pc_sli4_params.supported)
14874 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
14875
14876 if (hrq->entry_count != drq->entry_count)
14877 return -EINVAL;
14878 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14879 if (!mbox)
14880 return -ENOMEM;
14881 length = (sizeof(struct lpfc_mbx_rq_create) -
14882 sizeof(struct lpfc_sli4_cfg_mhdr));
14883 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14884 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14885 length, LPFC_SLI4_MBX_EMBED);
14886 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
14887 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14888 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14889 phba->sli4_hba.pc_sli4_params.rqv);
14890 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14891 bf_set(lpfc_rq_context_rqe_count_1,
14892 &rq_create->u.request.context,
14893 hrq->entry_count);
14894 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
14895 bf_set(lpfc_rq_context_rqe_size,
14896 &rq_create->u.request.context,
14897 LPFC_RQE_SIZE_8);
14898 bf_set(lpfc_rq_context_page_size,
14899 &rq_create->u.request.context,
8ea73db4 14900 LPFC_RQ_PAGE_SIZE_4096);
5a6f133e
JS
14901 } else {
14902 switch (hrq->entry_count) {
14903 default:
14904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14905 "2535 Unsupported RQ count. (%d)\n",
14906 hrq->entry_count);
4f4c1863
JS
14907 if (hrq->entry_count < 512) {
14908 status = -EINVAL;
14909 goto out;
14910 }
5a6f133e
JS
14911 /* otherwise default to smallest count (drop through) */
14912 case 512:
14913 bf_set(lpfc_rq_context_rqe_count,
14914 &rq_create->u.request.context,
14915 LPFC_RQ_RING_SIZE_512);
14916 break;
14917 case 1024:
14918 bf_set(lpfc_rq_context_rqe_count,
14919 &rq_create->u.request.context,
14920 LPFC_RQ_RING_SIZE_1024);
14921 break;
14922 case 2048:
14923 bf_set(lpfc_rq_context_rqe_count,
14924 &rq_create->u.request.context,
14925 LPFC_RQ_RING_SIZE_2048);
14926 break;
14927 case 4096:
14928 bf_set(lpfc_rq_context_rqe_count,
14929 &rq_create->u.request.context,
14930 LPFC_RQ_RING_SIZE_4096);
14931 break;
14932 }
14933 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14934 LPFC_HDR_BUF_SIZE);
4f774513
JS
14935 }
14936 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14937 cq->queue_id);
14938 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14939 hrq->page_count);
4f774513 14940 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 14941 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
14942 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14943 putPaddrLow(dmabuf->phys);
14944 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14945 putPaddrHigh(dmabuf->phys);
14946 }
962bc51b
JS
14947 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14948 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14949
4f774513
JS
14950 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14951 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
14952 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14953 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14954 if (shdr_status || shdr_add_status || rc) {
14955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14956 "2504 RQ_CREATE mailbox failed with "
14957 "status x%x add_status x%x, mbx status x%x\n",
14958 shdr_status, shdr_add_status, rc);
14959 status = -ENXIO;
14960 goto out;
14961 }
14962 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14963 if (hrq->queue_id == 0xFFFF) {
14964 status = -ENXIO;
14965 goto out;
14966 }
962bc51b
JS
14967
14968 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14969 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14970 &rq_create->u.response);
14971 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14972 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14974 "3262 RQ [%d] doorbell format not "
14975 "supported: x%x\n", hrq->queue_id,
14976 hrq->db_format);
14977 status = -EINVAL;
14978 goto out;
14979 }
14980
14981 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14982 &rq_create->u.response);
14983 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14984 if (!bar_memmap_p) {
14985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14986 "3269 RQ[%d] failed to memmap pci "
14987 "barset:x%x\n", hrq->queue_id,
14988 pci_barset);
14989 status = -ENOMEM;
14990 goto out;
14991 }
14992
14993 db_offset = rq_create->u.response.doorbell_offset;
14994 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14995 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14997 "3270 RQ[%d] doorbell offset not "
14998 "supported: x%x\n", hrq->queue_id,
14999 db_offset);
15000 status = -EINVAL;
15001 goto out;
15002 }
15003 hrq->db_regaddr = bar_memmap_p + db_offset;
15004 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
a22e7db3
JS
15005 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15006 "format:x%x\n", hrq->queue_id, pci_barset,
15007 db_offset, hrq->db_format);
962bc51b
JS
15008 } else {
15009 hrq->db_format = LPFC_DB_RING_FORMAT;
15010 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15011 }
4f774513 15012 hrq->type = LPFC_HRQ;
2a622bfb 15013 hrq->assoc_qid = cq->queue_id;
4f774513
JS
15014 hrq->subtype = subtype;
15015 hrq->host_index = 0;
15016 hrq->hba_index = 0;
15017
15018 /* now create the data queue */
15019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15020 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15021 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
15022 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15023 phba->sli4_hba.pc_sli4_params.rqv);
15024 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15025 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 15026 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 15027 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
15028 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15029 LPFC_RQE_SIZE_8);
15030 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15031 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
15032 } else {
15033 switch (drq->entry_count) {
15034 default:
15035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15036 "2536 Unsupported RQ count. (%d)\n",
15037 drq->entry_count);
4f4c1863
JS
15038 if (drq->entry_count < 512) {
15039 status = -EINVAL;
15040 goto out;
15041 }
5a6f133e
JS
15042 /* otherwise default to smallest count (drop through) */
15043 case 512:
15044 bf_set(lpfc_rq_context_rqe_count,
15045 &rq_create->u.request.context,
15046 LPFC_RQ_RING_SIZE_512);
15047 break;
15048 case 1024:
15049 bf_set(lpfc_rq_context_rqe_count,
15050 &rq_create->u.request.context,
15051 LPFC_RQ_RING_SIZE_1024);
15052 break;
15053 case 2048:
15054 bf_set(lpfc_rq_context_rqe_count,
15055 &rq_create->u.request.context,
15056 LPFC_RQ_RING_SIZE_2048);
15057 break;
15058 case 4096:
15059 bf_set(lpfc_rq_context_rqe_count,
15060 &rq_create->u.request.context,
15061 LPFC_RQ_RING_SIZE_4096);
15062 break;
15063 }
15064 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15065 LPFC_DATA_BUF_SIZE);
4f774513
JS
15066 }
15067 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15068 cq->queue_id);
15069 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15070 drq->page_count);
4f774513
JS
15071 list_for_each_entry(dmabuf, &drq->page_list, list) {
15072 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15073 putPaddrLow(dmabuf->phys);
15074 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15075 putPaddrHigh(dmabuf->phys);
15076 }
962bc51b
JS
15077 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15078 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
4f774513
JS
15079 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15080 /* The IOCTL status is embedded in the mailbox subheader. */
15081 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15082 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15083 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15084 if (shdr_status || shdr_add_status || rc) {
15085 status = -ENXIO;
15086 goto out;
15087 }
15088 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15089 if (drq->queue_id == 0xFFFF) {
15090 status = -ENXIO;
15091 goto out;
15092 }
15093 drq->type = LPFC_DRQ;
2a622bfb 15094 drq->assoc_qid = cq->queue_id;
4f774513
JS
15095 drq->subtype = subtype;
15096 drq->host_index = 0;
15097 drq->hba_index = 0;
15098
15099 /* link the header and data RQs onto the parent cq child list */
15100 list_add_tail(&hrq->list, &cq->child_list);
15101 list_add_tail(&drq->list, &cq->child_list);
15102
15103out:
8fa38513 15104 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
15105 return status;
15106}
15107
2d7dbc4c
JS
15108/**
15109 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15110 * @phba: HBA structure that indicates port to create a queue on.
15111 * @hrqp: The queue structure array to use to create the header receive queues.
15112 * @drqp: The queue structure array to use to create the data receive queues.
15113 * @cqp: The completion queue array to bind these receive queues to.
15114 *
15115 * This function creates a receive buffer queue pair , as detailed in @hrq and
15116 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15117 * to the HBA.
15118 *
15119 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15120 * struct is used to get the entry count that is necessary to determine the
15121 * number of pages to use for this queue. The @cq is used to indicate which
15122 * completion queue to bind received buffers that are posted to these queues to.
15123 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15124 * receive queue pair. This function is asynchronous and will wait for the
15125 * mailbox command to finish before continuing.
15126 *
15127 * On success this function will return a zero. If unable to allocate enough
15128 * memory this function will return -ENOMEM. If the queue create mailbox command
15129 * fails this function will return -ENXIO.
15130 **/
15131int
15132lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15133 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15134 uint32_t subtype)
15135{
15136 struct lpfc_queue *hrq, *drq, *cq;
15137 struct lpfc_mbx_rq_create_v2 *rq_create;
15138 struct lpfc_dmabuf *dmabuf;
15139 LPFC_MBOXQ_t *mbox;
15140 int rc, length, alloclen, status = 0;
15141 int cnt, idx, numrq, page_idx = 0;
15142 uint32_t shdr_status, shdr_add_status;
15143 union lpfc_sli4_cfg_shdr *shdr;
15144 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15145
15146 numrq = phba->cfg_nvmet_mrq;
15147 /* sanity check on array memory */
15148 if (!hrqp || !drqp || !cqp || !numrq)
15149 return -ENODEV;
15150 if (!phba->sli4_hba.pc_sli4_params.supported)
15151 hw_page_size = SLI4_PAGE_SIZE;
15152
15153 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15154 if (!mbox)
15155 return -ENOMEM;
15156
15157 length = sizeof(struct lpfc_mbx_rq_create_v2);
15158 length += ((2 * numrq * hrqp[0]->page_count) *
15159 sizeof(struct dma_address));
15160
15161 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15162 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15163 LPFC_SLI4_MBX_NEMBED);
15164 if (alloclen < length) {
15165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15166 "3099 Allocated DMA memory size (%d) is "
15167 "less than the requested DMA memory size "
15168 "(%d)\n", alloclen, length);
15169 status = -ENOMEM;
15170 goto out;
15171 }
15172
15173
15174
15175 rq_create = mbox->sge_array->addr[0];
15176 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15177
15178 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15179 cnt = 0;
15180
15181 for (idx = 0; idx < numrq; idx++) {
15182 hrq = hrqp[idx];
15183 drq = drqp[idx];
15184 cq = cqp[idx];
15185
15186 if (hrq->entry_count != drq->entry_count) {
15187 status = -EINVAL;
15188 goto out;
15189 }
15190
15191 /* sanity check on queue memory */
15192 if (!hrq || !drq || !cq) {
15193 status = -ENODEV;
15194 goto out;
15195 }
15196
15197 if (idx == 0) {
15198 bf_set(lpfc_mbx_rq_create_num_pages,
15199 &rq_create->u.request,
15200 hrq->page_count);
15201 bf_set(lpfc_mbx_rq_create_rq_cnt,
15202 &rq_create->u.request, (numrq * 2));
15203 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15204 1);
15205 bf_set(lpfc_rq_context_base_cq,
15206 &rq_create->u.request.context,
15207 cq->queue_id);
15208 bf_set(lpfc_rq_context_data_size,
15209 &rq_create->u.request.context,
15210 LPFC_DATA_BUF_SIZE);
15211 bf_set(lpfc_rq_context_hdr_size,
15212 &rq_create->u.request.context,
15213 LPFC_HDR_BUF_SIZE);
15214 bf_set(lpfc_rq_context_rqe_count_1,
15215 &rq_create->u.request.context,
15216 hrq->entry_count);
15217 bf_set(lpfc_rq_context_rqe_size,
15218 &rq_create->u.request.context,
15219 LPFC_RQE_SIZE_8);
15220 bf_set(lpfc_rq_context_page_size,
15221 &rq_create->u.request.context,
15222 (PAGE_SIZE/SLI4_PAGE_SIZE));
15223 }
15224 rc = 0;
15225 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15226 memset(dmabuf->virt, 0, hw_page_size);
15227 cnt = page_idx + dmabuf->buffer_tag;
15228 rq_create->u.request.page[cnt].addr_lo =
15229 putPaddrLow(dmabuf->phys);
15230 rq_create->u.request.page[cnt].addr_hi =
15231 putPaddrHigh(dmabuf->phys);
15232 rc++;
15233 }
15234 page_idx += rc;
15235
15236 rc = 0;
15237 list_for_each_entry(dmabuf, &drq->page_list, list) {
15238 memset(dmabuf->virt, 0, hw_page_size);
15239 cnt = page_idx + dmabuf->buffer_tag;
15240 rq_create->u.request.page[cnt].addr_lo =
15241 putPaddrLow(dmabuf->phys);
15242 rq_create->u.request.page[cnt].addr_hi =
15243 putPaddrHigh(dmabuf->phys);
15244 rc++;
15245 }
15246 page_idx += rc;
15247
15248 hrq->db_format = LPFC_DB_RING_FORMAT;
15249 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15250 hrq->type = LPFC_HRQ;
15251 hrq->assoc_qid = cq->queue_id;
15252 hrq->subtype = subtype;
15253 hrq->host_index = 0;
15254 hrq->hba_index = 0;
15255
15256 drq->db_format = LPFC_DB_RING_FORMAT;
15257 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15258 drq->type = LPFC_DRQ;
15259 drq->assoc_qid = cq->queue_id;
15260 drq->subtype = subtype;
15261 drq->host_index = 0;
15262 drq->hba_index = 0;
15263
15264 list_add_tail(&hrq->list, &cq->child_list);
15265 list_add_tail(&drq->list, &cq->child_list);
15266 }
15267
15268 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15269 /* The IOCTL status is embedded in the mailbox subheader. */
15270 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15271 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15272 if (shdr_status || shdr_add_status || rc) {
15273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15274 "3120 RQ_CREATE mailbox failed with "
15275 "status x%x add_status x%x, mbx status x%x\n",
15276 shdr_status, shdr_add_status, rc);
15277 status = -ENXIO;
15278 goto out;
15279 }
15280 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15281 if (rc == 0xFFFF) {
15282 status = -ENXIO;
15283 goto out;
15284 }
15285
15286 /* Initialize all RQs with associated queue id */
15287 for (idx = 0; idx < numrq; idx++) {
15288 hrq = hrqp[idx];
15289 hrq->queue_id = rc + (2 * idx);
15290 drq = drqp[idx];
15291 drq->queue_id = rc + (2 * idx) + 1;
15292 }
15293
15294out:
15295 lpfc_sli4_mbox_cmd_free(phba, mbox);
15296 return status;
15297}
15298
4f774513
JS
15299/**
15300 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15301 * @eq: The queue structure associated with the queue to destroy.
15302 *
15303 * This function destroys a queue, as detailed in @eq by sending an mailbox
15304 * command, specific to the type of queue, to the HBA.
15305 *
15306 * The @eq struct is used to get the queue ID of the queue to destroy.
15307 *
15308 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15309 * command fails this function will return -ENXIO.
4f774513 15310 **/
a2fc4aef 15311int
4f774513
JS
15312lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15313{
15314 LPFC_MBOXQ_t *mbox;
15315 int rc, length, status = 0;
15316 uint32_t shdr_status, shdr_add_status;
15317 union lpfc_sli4_cfg_shdr *shdr;
15318
2e90f4b5 15319 /* sanity check on queue memory */
4f774513
JS
15320 if (!eq)
15321 return -ENODEV;
15322 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15323 if (!mbox)
15324 return -ENOMEM;
15325 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15326 sizeof(struct lpfc_sli4_cfg_mhdr));
15327 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15328 LPFC_MBOX_OPCODE_EQ_DESTROY,
15329 length, LPFC_SLI4_MBX_EMBED);
15330 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15331 eq->queue_id);
15332 mbox->vport = eq->phba->pport;
15333 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15334
15335 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15336 /* The IOCTL status is embedded in the mailbox subheader. */
15337 shdr = (union lpfc_sli4_cfg_shdr *)
15338 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15339 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15340 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15341 if (shdr_status || shdr_add_status || rc) {
15342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15343 "2505 EQ_DESTROY mailbox failed with "
15344 "status x%x add_status x%x, mbx status x%x\n",
15345 shdr_status, shdr_add_status, rc);
15346 status = -ENXIO;
15347 }
15348
15349 /* Remove eq from any list */
15350 list_del_init(&eq->list);
8fa38513 15351 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
15352 return status;
15353}
15354
15355/**
15356 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15357 * @cq: The queue structure associated with the queue to destroy.
15358 *
15359 * This function destroys a queue, as detailed in @cq by sending an mailbox
15360 * command, specific to the type of queue, to the HBA.
15361 *
15362 * The @cq struct is used to get the queue ID of the queue to destroy.
15363 *
15364 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15365 * command fails this function will return -ENXIO.
4f774513 15366 **/
a2fc4aef 15367int
4f774513
JS
15368lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15369{
15370 LPFC_MBOXQ_t *mbox;
15371 int rc, length, status = 0;
15372 uint32_t shdr_status, shdr_add_status;
15373 union lpfc_sli4_cfg_shdr *shdr;
15374
2e90f4b5 15375 /* sanity check on queue memory */
4f774513
JS
15376 if (!cq)
15377 return -ENODEV;
15378 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15379 if (!mbox)
15380 return -ENOMEM;
15381 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15382 sizeof(struct lpfc_sli4_cfg_mhdr));
15383 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15384 LPFC_MBOX_OPCODE_CQ_DESTROY,
15385 length, LPFC_SLI4_MBX_EMBED);
15386 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15387 cq->queue_id);
15388 mbox->vport = cq->phba->pport;
15389 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15390 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15391 /* The IOCTL status is embedded in the mailbox subheader. */
15392 shdr = (union lpfc_sli4_cfg_shdr *)
15393 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15396 if (shdr_status || shdr_add_status || rc) {
15397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15398 "2506 CQ_DESTROY mailbox failed with "
15399 "status x%x add_status x%x, mbx status x%x\n",
15400 shdr_status, shdr_add_status, rc);
15401 status = -ENXIO;
15402 }
15403 /* Remove cq from any list */
15404 list_del_init(&cq->list);
8fa38513 15405 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
15406 return status;
15407}
15408
04c68496
JS
15409/**
15410 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15411 * @qm: The queue structure associated with the queue to destroy.
15412 *
15413 * This function destroys a queue, as detailed in @mq by sending an mailbox
15414 * command, specific to the type of queue, to the HBA.
15415 *
15416 * The @mq struct is used to get the queue ID of the queue to destroy.
15417 *
15418 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15419 * command fails this function will return -ENXIO.
04c68496 15420 **/
a2fc4aef 15421int
04c68496
JS
15422lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15423{
15424 LPFC_MBOXQ_t *mbox;
15425 int rc, length, status = 0;
15426 uint32_t shdr_status, shdr_add_status;
15427 union lpfc_sli4_cfg_shdr *shdr;
15428
2e90f4b5 15429 /* sanity check on queue memory */
04c68496
JS
15430 if (!mq)
15431 return -ENODEV;
15432 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15433 if (!mbox)
15434 return -ENOMEM;
15435 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15436 sizeof(struct lpfc_sli4_cfg_mhdr));
15437 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15438 LPFC_MBOX_OPCODE_MQ_DESTROY,
15439 length, LPFC_SLI4_MBX_EMBED);
15440 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15441 mq->queue_id);
15442 mbox->vport = mq->phba->pport;
15443 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15444 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15445 /* The IOCTL status is embedded in the mailbox subheader. */
15446 shdr = (union lpfc_sli4_cfg_shdr *)
15447 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15448 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15449 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15450 if (shdr_status || shdr_add_status || rc) {
15451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15452 "2507 MQ_DESTROY mailbox failed with "
15453 "status x%x add_status x%x, mbx status x%x\n",
15454 shdr_status, shdr_add_status, rc);
15455 status = -ENXIO;
15456 }
15457 /* Remove mq from any list */
15458 list_del_init(&mq->list);
8fa38513 15459 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
15460 return status;
15461}
15462
4f774513
JS
15463/**
15464 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15465 * @wq: The queue structure associated with the queue to destroy.
15466 *
15467 * This function destroys a queue, as detailed in @wq by sending an mailbox
15468 * command, specific to the type of queue, to the HBA.
15469 *
15470 * The @wq struct is used to get the queue ID of the queue to destroy.
15471 *
15472 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15473 * command fails this function will return -ENXIO.
4f774513 15474 **/
a2fc4aef 15475int
4f774513
JS
15476lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15477{
15478 LPFC_MBOXQ_t *mbox;
15479 int rc, length, status = 0;
15480 uint32_t shdr_status, shdr_add_status;
15481 union lpfc_sli4_cfg_shdr *shdr;
15482
2e90f4b5 15483 /* sanity check on queue memory */
4f774513
JS
15484 if (!wq)
15485 return -ENODEV;
15486 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15487 if (!mbox)
15488 return -ENOMEM;
15489 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15490 sizeof(struct lpfc_sli4_cfg_mhdr));
15491 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15492 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15493 length, LPFC_SLI4_MBX_EMBED);
15494 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15495 wq->queue_id);
15496 mbox->vport = wq->phba->pport;
15497 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15498 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15499 shdr = (union lpfc_sli4_cfg_shdr *)
15500 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15503 if (shdr_status || shdr_add_status || rc) {
15504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15505 "2508 WQ_DESTROY mailbox failed with "
15506 "status x%x add_status x%x, mbx status x%x\n",
15507 shdr_status, shdr_add_status, rc);
15508 status = -ENXIO;
15509 }
15510 /* Remove wq from any list */
15511 list_del_init(&wq->list);
8fa38513 15512 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
15513 return status;
15514}
15515
15516/**
15517 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15518 * @rq: The queue structure associated with the queue to destroy.
15519 *
15520 * This function destroys a queue, as detailed in @rq by sending an mailbox
15521 * command, specific to the type of queue, to the HBA.
15522 *
15523 * The @rq struct is used to get the queue ID of the queue to destroy.
15524 *
15525 * On success this function will return a zero. If the queue destroy mailbox
d439d286 15526 * command fails this function will return -ENXIO.
4f774513 15527 **/
a2fc4aef 15528int
4f774513
JS
15529lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15530 struct lpfc_queue *drq)
15531{
15532 LPFC_MBOXQ_t *mbox;
15533 int rc, length, status = 0;
15534 uint32_t shdr_status, shdr_add_status;
15535 union lpfc_sli4_cfg_shdr *shdr;
15536
2e90f4b5 15537 /* sanity check on queue memory */
4f774513
JS
15538 if (!hrq || !drq)
15539 return -ENODEV;
15540 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15541 if (!mbox)
15542 return -ENOMEM;
15543 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 15544 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
15545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15546 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15547 length, LPFC_SLI4_MBX_EMBED);
15548 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15549 hrq->queue_id);
15550 mbox->vport = hrq->phba->pport;
15551 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15552 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15553 /* The IOCTL status is embedded in the mailbox subheader. */
15554 shdr = (union lpfc_sli4_cfg_shdr *)
15555 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15556 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15557 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15558 if (shdr_status || shdr_add_status || rc) {
15559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15560 "2509 RQ_DESTROY mailbox failed with "
15561 "status x%x add_status x%x, mbx status x%x\n",
15562 shdr_status, shdr_add_status, rc);
15563 if (rc != MBX_TIMEOUT)
15564 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15565 return -ENXIO;
15566 }
15567 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15568 drq->queue_id);
15569 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15570 shdr = (union lpfc_sli4_cfg_shdr *)
15571 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15572 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15573 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15574 if (shdr_status || shdr_add_status || rc) {
15575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15576 "2510 RQ_DESTROY mailbox failed with "
15577 "status x%x add_status x%x, mbx status x%x\n",
15578 shdr_status, shdr_add_status, rc);
15579 status = -ENXIO;
15580 }
15581 list_del_init(&hrq->list);
15582 list_del_init(&drq->list);
8fa38513 15583 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
15584 return status;
15585}
15586
15587/**
15588 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15589 * @phba: The virtual port for which this call being executed.
15590 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15591 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15592 * @xritag: the xritag that ties this io to the SGL pages.
15593 *
15594 * This routine will post the sgl pages for the IO that has the xritag
15595 * that is in the iocbq structure. The xritag is assigned during iocbq
15596 * creation and persists for as long as the driver is loaded.
15597 * if the caller has fewer than 256 scatter gather segments to map then
15598 * pdma_phys_addr1 should be 0.
15599 * If the caller needs to map more than 256 scatter gather segment then
15600 * pdma_phys_addr1 should be a valid physical address.
15601 * physical address for SGLs must be 64 byte aligned.
15602 * If you are going to map 2 SGL's then the first one must have 256 entries
15603 * the second sgl can have between 1 and 256 entries.
15604 *
15605 * Return codes:
15606 * 0 - Success
15607 * -ENXIO, -ENOMEM - Failure
15608 **/
15609int
15610lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15611 dma_addr_t pdma_phys_addr0,
15612 dma_addr_t pdma_phys_addr1,
15613 uint16_t xritag)
15614{
15615 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15616 LPFC_MBOXQ_t *mbox;
15617 int rc;
15618 uint32_t shdr_status, shdr_add_status;
6d368e53 15619 uint32_t mbox_tmo;
4f774513
JS
15620 union lpfc_sli4_cfg_shdr *shdr;
15621
15622 if (xritag == NO_XRI) {
15623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15624 "0364 Invalid param:\n");
15625 return -EINVAL;
15626 }
15627
15628 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15629 if (!mbox)
15630 return -ENOMEM;
15631
15632 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15633 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15634 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 15635 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
15636
15637 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15638 &mbox->u.mqe.un.post_sgl_pages;
15639 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15640 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15641
15642 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15643 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15644 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15645 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15646
15647 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15648 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15649 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15650 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15651 if (!phba->sli4_hba.intr_enable)
15652 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 15653 else {
a183a15f 15654 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
15655 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15656 }
4f774513
JS
15657 /* The IOCTL status is embedded in the mailbox subheader. */
15658 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15659 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15660 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15661 if (rc != MBX_TIMEOUT)
15662 mempool_free(mbox, phba->mbox_mem_pool);
15663 if (shdr_status || shdr_add_status || rc) {
15664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15665 "2511 POST_SGL mailbox failed with "
15666 "status x%x add_status x%x, mbx status x%x\n",
15667 shdr_status, shdr_add_status, rc);
4f774513
JS
15668 }
15669 return 0;
15670}
4f774513 15671
6d368e53 15672/**
88a2cfbb 15673 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
15674 * @phba: pointer to lpfc hba data structure.
15675 *
15676 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
15677 * HBA consistent with the SLI-4 interface spec. This routine
15678 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15679 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 15680 *
88a2cfbb
JS
15681 * Returns
15682 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15683 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15684 **/
5d8b8167 15685static uint16_t
6d368e53
JS
15686lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15687{
15688 unsigned long xri;
15689
15690 /*
15691 * Fetch the next logical xri. Because this index is logical,
15692 * the driver starts at 0 each time.
15693 */
15694 spin_lock_irq(&phba->hbalock);
15695 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15696 phba->sli4_hba.max_cfg_param.max_xri, 0);
15697 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15698 spin_unlock_irq(&phba->hbalock);
15699 return NO_XRI;
15700 } else {
15701 set_bit(xri, phba->sli4_hba.xri_bmask);
15702 phba->sli4_hba.max_cfg_param.xri_used++;
6d368e53 15703 }
6d368e53
JS
15704 spin_unlock_irq(&phba->hbalock);
15705 return xri;
15706}
15707
15708/**
15709 * lpfc_sli4_free_xri - Release an xri for reuse.
15710 * @phba: pointer to lpfc hba data structure.
15711 *
15712 * This routine is invoked to release an xri to the pool of
15713 * available rpis maintained by the driver.
15714 **/
5d8b8167 15715static void
6d368e53
JS
15716__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15717{
15718 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
6d368e53
JS
15719 phba->sli4_hba.max_cfg_param.xri_used--;
15720 }
15721}
15722
15723/**
15724 * lpfc_sli4_free_xri - Release an xri for reuse.
15725 * @phba: pointer to lpfc hba data structure.
15726 *
15727 * This routine is invoked to release an xri to the pool of
15728 * available rpis maintained by the driver.
15729 **/
15730void
15731lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15732{
15733 spin_lock_irq(&phba->hbalock);
15734 __lpfc_sli4_free_xri(phba, xri);
15735 spin_unlock_irq(&phba->hbalock);
15736}
15737
4f774513
JS
15738/**
15739 * lpfc_sli4_next_xritag - Get an xritag for the io
15740 * @phba: Pointer to HBA context object.
15741 *
15742 * This function gets an xritag for the iocb. If there is no unused xritag
15743 * it will return 0xffff.
15744 * The function returns the allocated xritag if successful, else returns zero.
15745 * Zero is not a valid xritag.
15746 * The caller is not required to hold any lock.
15747 **/
15748uint16_t
15749lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15750{
6d368e53 15751 uint16_t xri_index;
4f774513 15752
6d368e53 15753 xri_index = lpfc_sli4_alloc_xri(phba);
81378052
JS
15754 if (xri_index == NO_XRI)
15755 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15756 "2004 Failed to allocate XRI.last XRITAG is %d"
15757 " Max XRI is %d, Used XRI is %d\n",
15758 xri_index,
15759 phba->sli4_hba.max_cfg_param.max_xri,
15760 phba->sli4_hba.max_cfg_param.xri_used);
15761 return xri_index;
4f774513
JS
15762}
15763
15764/**
895427bd 15765 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
4f774513 15766 * @phba: pointer to lpfc hba data structure.
8a9d2e80
JS
15767 * @post_sgl_list: pointer to els sgl entry list.
15768 * @count: number of els sgl entries on the list.
4f774513
JS
15769 *
15770 * This routine is invoked to post a block of driver's sgl pages to the
15771 * HBA using non-embedded mailbox command. No Lock is held. This routine
15772 * is only called when the driver is loading and after all IO has been
15773 * stopped.
15774 **/
8a9d2e80 15775static int
895427bd 15776lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
8a9d2e80
JS
15777 struct list_head *post_sgl_list,
15778 int post_cnt)
4f774513 15779{
8a9d2e80 15780 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4f774513
JS
15781 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15782 struct sgl_page_pairs *sgl_pg_pairs;
15783 void *viraddr;
15784 LPFC_MBOXQ_t *mbox;
15785 uint32_t reqlen, alloclen, pg_pairs;
15786 uint32_t mbox_tmo;
8a9d2e80
JS
15787 uint16_t xritag_start = 0;
15788 int rc = 0;
4f774513
JS
15789 uint32_t shdr_status, shdr_add_status;
15790 union lpfc_sli4_cfg_shdr *shdr;
15791
895427bd 15792 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
4f774513 15793 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 15794 if (reqlen > SLI4_PAGE_SIZE) {
895427bd 15795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4f774513
JS
15796 "2559 Block sgl registration required DMA "
15797 "size (%d) great than a page\n", reqlen);
15798 return -ENOMEM;
15799 }
895427bd 15800
4f774513 15801 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 15802 if (!mbox)
4f774513 15803 return -ENOMEM;
4f774513
JS
15804
15805 /* Allocate DMA memory and set up the non-embedded mailbox command */
15806 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15807 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15808 LPFC_SLI4_MBX_NEMBED);
15809
15810 if (alloclen < reqlen) {
15811 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15812 "0285 Allocated DMA memory size (%d) is "
15813 "less than the requested DMA memory "
15814 "size (%d)\n", alloclen, reqlen);
15815 lpfc_sli4_mbox_cmd_free(phba, mbox);
15816 return -ENOMEM;
15817 }
4f774513 15818 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 15819 viraddr = mbox->sge_array->addr[0];
4f774513
JS
15820 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15821 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15822
8a9d2e80
JS
15823 pg_pairs = 0;
15824 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
4f774513
JS
15825 /* Set up the sge entry */
15826 sgl_pg_pairs->sgl_pg0_addr_lo =
15827 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15828 sgl_pg_pairs->sgl_pg0_addr_hi =
15829 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15830 sgl_pg_pairs->sgl_pg1_addr_lo =
15831 cpu_to_le32(putPaddrLow(0));
15832 sgl_pg_pairs->sgl_pg1_addr_hi =
15833 cpu_to_le32(putPaddrHigh(0));
6d368e53 15834
4f774513
JS
15835 /* Keep the first xritag on the list */
15836 if (pg_pairs == 0)
15837 xritag_start = sglq_entry->sli4_xritag;
15838 sgl_pg_pairs++;
8a9d2e80 15839 pg_pairs++;
4f774513 15840 }
6d368e53
JS
15841
15842 /* Complete initialization and perform endian conversion. */
4f774513 15843 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
895427bd 15844 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
4f774513 15845 sgl->word0 = cpu_to_le32(sgl->word0);
895427bd 15846
4f774513
JS
15847 if (!phba->sli4_hba.intr_enable)
15848 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15849 else {
a183a15f 15850 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
15851 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15852 }
15853 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15854 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15855 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15856 if (rc != MBX_TIMEOUT)
15857 lpfc_sli4_mbox_cmd_free(phba, mbox);
15858 if (shdr_status || shdr_add_status || rc) {
15859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15860 "2513 POST_SGL_BLOCK mailbox command failed "
15861 "status x%x add_status x%x mbx status x%x\n",
15862 shdr_status, shdr_add_status, rc);
15863 rc = -ENXIO;
15864 }
15865 return rc;
15866}
15867
15868/**
15869 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15870 * @phba: pointer to lpfc hba data structure.
15871 * @sblist: pointer to scsi buffer list.
15872 * @count: number of scsi buffers on the list.
15873 *
15874 * This routine is invoked to post a block of @count scsi sgl pages from a
15875 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15876 * No Lock is held.
15877 *
15878 **/
15879int
8a9d2e80
JS
15880lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15881 struct list_head *sblist,
15882 int count)
4f774513
JS
15883{
15884 struct lpfc_scsi_buf *psb;
15885 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15886 struct sgl_page_pairs *sgl_pg_pairs;
15887 void *viraddr;
15888 LPFC_MBOXQ_t *mbox;
15889 uint32_t reqlen, alloclen, pg_pairs;
15890 uint32_t mbox_tmo;
15891 uint16_t xritag_start = 0;
15892 int rc = 0;
15893 uint32_t shdr_status, shdr_add_status;
15894 dma_addr_t pdma_phys_bpl1;
15895 union lpfc_sli4_cfg_shdr *shdr;
15896
15897 /* Calculate the requested length of the dma memory */
8a9d2e80 15898 reqlen = count * sizeof(struct sgl_page_pairs) +
4f774513 15899 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 15900 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
15901 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15902 "0217 Block sgl registration required DMA "
15903 "size (%d) great than a page\n", reqlen);
15904 return -ENOMEM;
15905 }
15906 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15907 if (!mbox) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15909 "0283 Failed to allocate mbox cmd memory\n");
15910 return -ENOMEM;
15911 }
15912
15913 /* Allocate DMA memory and set up the non-embedded mailbox command */
15914 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15915 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15916 LPFC_SLI4_MBX_NEMBED);
15917
15918 if (alloclen < reqlen) {
15919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15920 "2561 Allocated DMA memory size (%d) is "
15921 "less than the requested DMA memory "
15922 "size (%d)\n", alloclen, reqlen);
15923 lpfc_sli4_mbox_cmd_free(phba, mbox);
15924 return -ENOMEM;
15925 }
6d368e53 15926
4f774513 15927 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
15928 viraddr = mbox->sge_array->addr[0];
15929
15930 /* Set up the SGL pages in the non-embedded DMA pages */
15931 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15932 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15933
15934 pg_pairs = 0;
15935 list_for_each_entry(psb, sblist, list) {
15936 /* Set up the sge entry */
15937 sgl_pg_pairs->sgl_pg0_addr_lo =
15938 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15939 sgl_pg_pairs->sgl_pg0_addr_hi =
15940 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15941 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15942 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15943 else
15944 pdma_phys_bpl1 = 0;
15945 sgl_pg_pairs->sgl_pg1_addr_lo =
15946 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15947 sgl_pg_pairs->sgl_pg1_addr_hi =
15948 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
15949 /* Keep the first xritag on the list */
15950 if (pg_pairs == 0)
15951 xritag_start = psb->cur_iocbq.sli4_xritag;
15952 sgl_pg_pairs++;
15953 pg_pairs++;
15954 }
15955 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15956 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
15957 /* Perform endian conversion if necessary */
15958 sgl->word0 = cpu_to_le32(sgl->word0);
15959
15960 if (!phba->sli4_hba.intr_enable)
15961 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15962 else {
a183a15f 15963 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
15964 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15965 }
15966 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15967 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15968 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15969 if (rc != MBX_TIMEOUT)
15970 lpfc_sli4_mbox_cmd_free(phba, mbox);
15971 if (shdr_status || shdr_add_status || rc) {
15972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15973 "2564 POST_SGL_BLOCK mailbox command failed "
15974 "status x%x add_status x%x mbx status x%x\n",
15975 shdr_status, shdr_add_status, rc);
15976 rc = -ENXIO;
15977 }
15978 return rc;
15979}
15980
2ea259ee
JS
15981static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
15982static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
15983
4f774513
JS
15984/**
15985 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
15986 * @phba: pointer to lpfc_hba struct that the frame was received on
15987 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15988 *
15989 * This function checks the fields in the @fc_hdr to see if the FC frame is a
15990 * valid type of frame that the LPFC driver will handle. This function will
15991 * return a zero if the frame is a valid frame or a non zero value when the
15992 * frame does not pass the check.
15993 **/
15994static int
15995lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
15996{
474ffb74 15997 /* make rctl_names static to save stack space */
4f774513 15998 struct fc_vft_header *fc_vft_hdr;
546fc854 15999 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
16000
16001 switch (fc_hdr->fh_r_ctl) {
16002 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16003 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16004 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16005 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16006 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16007 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16008 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16009 case FC_RCTL_DD_CMD_STATUS: /* command status */
16010 case FC_RCTL_ELS_REQ: /* extended link services request */
16011 case FC_RCTL_ELS_REP: /* extended link services reply */
16012 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16013 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16014 case FC_RCTL_BA_NOP: /* basic link service NOP */
16015 case FC_RCTL_BA_ABTS: /* basic link service abort */
16016 case FC_RCTL_BA_RMC: /* remove connection */
16017 case FC_RCTL_BA_ACC: /* basic accept */
16018 case FC_RCTL_BA_RJT: /* basic reject */
16019 case FC_RCTL_BA_PRMT:
16020 case FC_RCTL_ACK_1: /* acknowledge_1 */
16021 case FC_RCTL_ACK_0: /* acknowledge_0 */
16022 case FC_RCTL_P_RJT: /* port reject */
16023 case FC_RCTL_F_RJT: /* fabric reject */
16024 case FC_RCTL_P_BSY: /* port busy */
16025 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16026 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16027 case FC_RCTL_LCR: /* link credit reset */
16028 case FC_RCTL_END: /* end */
16029 break;
16030 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16031 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16032 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16033 return lpfc_fc_frame_check(phba, fc_hdr);
16034 default:
16035 goto drop;
16036 }
16037 switch (fc_hdr->fh_type) {
16038 case FC_TYPE_BLS:
16039 case FC_TYPE_ELS:
16040 case FC_TYPE_FCP:
16041 case FC_TYPE_CT:
895427bd 16042 case FC_TYPE_NVME:
4f774513
JS
16043 break;
16044 case FC_TYPE_IP:
16045 case FC_TYPE_ILS:
16046 default:
16047 goto drop;
16048 }
546fc854 16049
4f774513 16050 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
88f43a08
JS
16051 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16052 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
2ea259ee
JS
16053 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16054 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
546fc854
JS
16055 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16056 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
88f43a08
JS
16057 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16058 be32_to_cpu(header[6]));
4f774513
JS
16059 return 0;
16060drop:
16061 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16062 "2539 Dropped frame rctl:%s type:%s\n",
2ea259ee
JS
16063 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16064 lpfc_type_names[fc_hdr->fh_type]);
4f774513
JS
16065 return 1;
16066}
16067
16068/**
16069 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16070 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16071 *
16072 * This function processes the FC header to retrieve the VFI from the VF
16073 * header, if one exists. This function will return the VFI if one exists
16074 * or 0 if no VSAN Header exists.
16075 **/
16076static uint32_t
16077lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16078{
16079 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16080
16081 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16082 return 0;
16083 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16084}
16085
16086/**
16087 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16088 * @phba: Pointer to the HBA structure to search for the vport on
16089 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16090 * @fcfi: The FC Fabric ID that the frame came from
16091 *
16092 * This function searches the @phba for a vport that matches the content of the
16093 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16094 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16095 * returns the matching vport pointer or NULL if unable to match frame to a
16096 * vport.
16097 **/
16098static struct lpfc_vport *
16099lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
895427bd 16100 uint16_t fcfi, uint32_t did)
4f774513
JS
16101{
16102 struct lpfc_vport **vports;
16103 struct lpfc_vport *vport = NULL;
16104 int i;
939723a4 16105
bf08611b
JS
16106 if (did == Fabric_DID)
16107 return phba->pport;
939723a4
JS
16108 if ((phba->pport->fc_flag & FC_PT2PT) &&
16109 !(phba->link_state == LPFC_HBA_READY))
16110 return phba->pport;
16111
4f774513 16112 vports = lpfc_create_vport_work_array(phba);
895427bd 16113 if (vports != NULL) {
4f774513
JS
16114 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16115 if (phba->fcf.fcfi == fcfi &&
16116 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16117 vports[i]->fc_myDID == did) {
16118 vport = vports[i];
16119 break;
16120 }
16121 }
895427bd 16122 }
4f774513
JS
16123 lpfc_destroy_vport_work_array(phba, vports);
16124 return vport;
16125}
16126
45ed1190
JS
16127/**
16128 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16129 * @vport: The vport to work on.
16130 *
16131 * This function updates the receive sequence time stamp for this vport. The
16132 * receive sequence time stamp indicates the time that the last frame of the
16133 * the sequence that has been idle for the longest amount of time was received.
16134 * the driver uses this time stamp to indicate if any received sequences have
16135 * timed out.
16136 **/
5d8b8167 16137static void
45ed1190
JS
16138lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16139{
16140 struct lpfc_dmabuf *h_buf;
16141 struct hbq_dmabuf *dmabuf = NULL;
16142
16143 /* get the oldest sequence on the rcv list */
16144 h_buf = list_get_first(&vport->rcv_buffer_list,
16145 struct lpfc_dmabuf, list);
16146 if (!h_buf)
16147 return;
16148 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16149 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16150}
16151
16152/**
16153 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16154 * @vport: The vport that the received sequences were sent to.
16155 *
16156 * This function cleans up all outstanding received sequences. This is called
16157 * by the driver when a link event or user action invalidates all the received
16158 * sequences.
16159 **/
16160void
16161lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16162{
16163 struct lpfc_dmabuf *h_buf, *hnext;
16164 struct lpfc_dmabuf *d_buf, *dnext;
16165 struct hbq_dmabuf *dmabuf = NULL;
16166
16167 /* start with the oldest sequence on the rcv list */
16168 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16169 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16170 list_del_init(&dmabuf->hbuf.list);
16171 list_for_each_entry_safe(d_buf, dnext,
16172 &dmabuf->dbuf.list, list) {
16173 list_del_init(&d_buf->list);
16174 lpfc_in_buf_free(vport->phba, d_buf);
16175 }
16176 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16177 }
16178}
16179
16180/**
16181 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16182 * @vport: The vport that the received sequences were sent to.
16183 *
16184 * This function determines whether any received sequences have timed out by
16185 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16186 * indicates that there is at least one timed out sequence this routine will
16187 * go through the received sequences one at a time from most inactive to most
16188 * active to determine which ones need to be cleaned up. Once it has determined
16189 * that a sequence needs to be cleaned up it will simply free up the resources
16190 * without sending an abort.
16191 **/
16192void
16193lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16194{
16195 struct lpfc_dmabuf *h_buf, *hnext;
16196 struct lpfc_dmabuf *d_buf, *dnext;
16197 struct hbq_dmabuf *dmabuf = NULL;
16198 unsigned long timeout;
16199 int abort_count = 0;
16200
16201 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16202 vport->rcv_buffer_time_stamp);
16203 if (list_empty(&vport->rcv_buffer_list) ||
16204 time_before(jiffies, timeout))
16205 return;
16206 /* start with the oldest sequence on the rcv list */
16207 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16208 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16209 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16210 dmabuf->time_stamp);
16211 if (time_before(jiffies, timeout))
16212 break;
16213 abort_count++;
16214 list_del_init(&dmabuf->hbuf.list);
16215 list_for_each_entry_safe(d_buf, dnext,
16216 &dmabuf->dbuf.list, list) {
16217 list_del_init(&d_buf->list);
16218 lpfc_in_buf_free(vport->phba, d_buf);
16219 }
16220 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16221 }
16222 if (abort_count)
16223 lpfc_update_rcv_time_stamp(vport);
16224}
16225
4f774513
JS
16226/**
16227 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16228 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16229 *
16230 * This function searches through the existing incomplete sequences that have
16231 * been sent to this @vport. If the frame matches one of the incomplete
16232 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16233 * make up that sequence. If no sequence is found that matches this frame then
16234 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16235 * This function returns a pointer to the first dmabuf in the sequence list that
16236 * the frame was linked to.
16237 **/
16238static struct hbq_dmabuf *
16239lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16240{
16241 struct fc_frame_header *new_hdr;
16242 struct fc_frame_header *temp_hdr;
16243 struct lpfc_dmabuf *d_buf;
16244 struct lpfc_dmabuf *h_buf;
16245 struct hbq_dmabuf *seq_dmabuf = NULL;
16246 struct hbq_dmabuf *temp_dmabuf = NULL;
4360ca9c 16247 uint8_t found = 0;
4f774513 16248
4d9ab994 16249 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 16250 dmabuf->time_stamp = jiffies;
4f774513 16251 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
4360ca9c 16252
4f774513
JS
16253 /* Use the hdr_buf to find the sequence that this frame belongs to */
16254 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16255 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16256 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16257 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16258 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16259 continue;
16260 /* found a pending sequence that matches this frame */
16261 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16262 break;
16263 }
16264 if (!seq_dmabuf) {
16265 /*
16266 * This indicates first frame received for this sequence.
16267 * Queue the buffer on the vport's rcv_buffer_list.
16268 */
16269 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 16270 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16271 return dmabuf;
16272 }
16273 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
16274 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16275 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
16276 list_del_init(&seq_dmabuf->hbuf.list);
16277 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16278 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 16279 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
16280 return dmabuf;
16281 }
45ed1190
JS
16282 /* move this sequence to the tail to indicate a young sequence */
16283 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16284 seq_dmabuf->time_stamp = jiffies;
16285 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
16286 if (list_empty(&seq_dmabuf->dbuf.list)) {
16287 temp_hdr = dmabuf->hbuf.virt;
16288 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16289 return seq_dmabuf;
16290 }
4f774513 16291 /* find the correct place in the sequence to insert this frame */
4360ca9c
JS
16292 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16293 while (!found) {
4f774513
JS
16294 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16295 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16296 /*
16297 * If the frame's sequence count is greater than the frame on
16298 * the list then insert the frame right after this frame
16299 */
eeead811
JS
16300 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16301 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513 16302 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
4360ca9c
JS
16303 found = 1;
16304 break;
4f774513 16305 }
4360ca9c
JS
16306
16307 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16308 break;
16309 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
4f774513 16310 }
4360ca9c
JS
16311
16312 if (found)
16313 return seq_dmabuf;
4f774513
JS
16314 return NULL;
16315}
16316
6669f9bb
JS
16317/**
16318 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16319 * @vport: pointer to a vitural port
16320 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16321 *
16322 * This function tries to abort from the partially assembed sequence, described
16323 * by the information from basic abbort @dmabuf. It checks to see whether such
16324 * partially assembled sequence held by the driver. If so, it shall free up all
16325 * the frames from the partially assembled sequence.
16326 *
16327 * Return
16328 * true -- if there is matching partially assembled sequence present and all
16329 * the frames freed with the sequence;
16330 * false -- if there is no matching partially assembled sequence present so
16331 * nothing got aborted in the lower layer driver
16332 **/
16333static bool
16334lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16335 struct hbq_dmabuf *dmabuf)
16336{
16337 struct fc_frame_header *new_hdr;
16338 struct fc_frame_header *temp_hdr;
16339 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16340 struct hbq_dmabuf *seq_dmabuf = NULL;
16341
16342 /* Use the hdr_buf to find the sequence that matches this frame */
16343 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16344 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16345 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16346 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16347 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16348 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16349 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16350 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16351 continue;
16352 /* found a pending sequence that matches this frame */
16353 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16354 break;
16355 }
16356
16357 /* Free up all the frames from the partially assembled sequence */
16358 if (seq_dmabuf) {
16359 list_for_each_entry_safe(d_buf, n_buf,
16360 &seq_dmabuf->dbuf.list, list) {
16361 list_del_init(&d_buf->list);
16362 lpfc_in_buf_free(vport->phba, d_buf);
16363 }
16364 return true;
16365 }
16366 return false;
16367}
16368
6dd9e31c
JS
16369/**
16370 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16371 * @vport: pointer to a vitural port
16372 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16373 *
16374 * This function tries to abort from the assembed sequence from upper level
16375 * protocol, described by the information from basic abbort @dmabuf. It
16376 * checks to see whether such pending context exists at upper level protocol.
16377 * If so, it shall clean up the pending context.
16378 *
16379 * Return
16380 * true -- if there is matching pending context of the sequence cleaned
16381 * at ulp;
16382 * false -- if there is no matching pending context of the sequence present
16383 * at ulp.
16384 **/
16385static bool
16386lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16387{
16388 struct lpfc_hba *phba = vport->phba;
16389 int handled;
16390
16391 /* Accepting abort at ulp with SLI4 only */
16392 if (phba->sli_rev < LPFC_SLI_REV4)
16393 return false;
16394
16395 /* Register all caring upper level protocols to attend abort */
16396 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16397 if (handled)
16398 return true;
16399
16400 return false;
16401}
16402
6669f9bb 16403/**
546fc854 16404 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
16405 * @phba: Pointer to HBA context object.
16406 * @cmd_iocbq: pointer to the command iocbq structure.
16407 * @rsp_iocbq: pointer to the response iocbq structure.
16408 *
546fc854 16409 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
16410 * event. It properly releases the memory allocated to the sequence abort
16411 * accept iocb.
16412 **/
16413static void
546fc854 16414lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
16415 struct lpfc_iocbq *cmd_iocbq,
16416 struct lpfc_iocbq *rsp_iocbq)
16417{
6dd9e31c
JS
16418 struct lpfc_nodelist *ndlp;
16419
16420 if (cmd_iocbq) {
16421 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16422 lpfc_nlp_put(ndlp);
16423 lpfc_nlp_not_used(ndlp);
6669f9bb 16424 lpfc_sli_release_iocbq(phba, cmd_iocbq);
6dd9e31c 16425 }
6b5151fd
JS
16426
16427 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16428 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16430 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16431 rsp_iocbq->iocb.ulpStatus,
16432 rsp_iocbq->iocb.un.ulpWord[4]);
6669f9bb
JS
16433}
16434
6d368e53
JS
16435/**
16436 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16437 * @phba: Pointer to HBA context object.
16438 * @xri: xri id in transaction.
16439 *
16440 * This function validates the xri maps to the known range of XRIs allocated an
16441 * used by the driver.
16442 **/
7851fe2c 16443uint16_t
6d368e53
JS
16444lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16445 uint16_t xri)
16446{
a2fc4aef 16447 uint16_t i;
6d368e53
JS
16448
16449 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16450 if (xri == phba->sli4_hba.xri_ids[i])
16451 return i;
16452 }
16453 return NO_XRI;
16454}
16455
6669f9bb 16456/**
546fc854 16457 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
16458 * @phba: Pointer to HBA context object.
16459 * @fc_hdr: pointer to a FC frame header.
16460 *
546fc854 16461 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
16462 * event after aborting the sequence handling.
16463 **/
16464static void
6dd9e31c
JS
16465lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16466 struct fc_frame_header *fc_hdr, bool aborted)
6669f9bb 16467{
6dd9e31c 16468 struct lpfc_hba *phba = vport->phba;
6669f9bb
JS
16469 struct lpfc_iocbq *ctiocb = NULL;
16470 struct lpfc_nodelist *ndlp;
ee0f4fe1 16471 uint16_t oxid, rxid, xri, lxri;
5ffc266e 16472 uint32_t sid, fctl;
6669f9bb 16473 IOCB_t *icmd;
546fc854 16474 int rc;
6669f9bb
JS
16475
16476 if (!lpfc_is_link_up(phba))
16477 return;
16478
16479 sid = sli4_sid_from_fc_hdr(fc_hdr);
16480 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 16481 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb 16482
6dd9e31c 16483 ndlp = lpfc_findnode_did(vport, sid);
6669f9bb 16484 if (!ndlp) {
6dd9e31c
JS
16485 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
16486 if (!ndlp) {
16487 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16488 "1268 Failed to allocate ndlp for "
16489 "oxid:x%x SID:x%x\n", oxid, sid);
16490 return;
16491 }
16492 lpfc_nlp_init(vport, ndlp, sid);
16493 /* Put ndlp onto pport node list */
16494 lpfc_enqueue_node(vport, ndlp);
16495 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16496 /* re-setup ndlp without removing from node list */
16497 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16498 if (!ndlp) {
16499 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16500 "3275 Failed to active ndlp found "
16501 "for oxid:x%x SID:x%x\n", oxid, sid);
16502 return;
16503 }
6669f9bb
JS
16504 }
16505
546fc854 16506 /* Allocate buffer for rsp iocb */
6669f9bb
JS
16507 ctiocb = lpfc_sli_get_iocbq(phba);
16508 if (!ctiocb)
16509 return;
16510
5ffc266e
JS
16511 /* Extract the F_CTL field from FC_HDR */
16512 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16513
6669f9bb 16514 icmd = &ctiocb->iocb;
6669f9bb 16515 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 16516 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
16517 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16518 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16519 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16520
16521 /* Fill in the rest of iocb fields */
16522 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16523 icmd->ulpBdeCount = 0;
16524 icmd->ulpLe = 1;
16525 icmd->ulpClass = CLASS3;
6d368e53 16526 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
6dd9e31c 16527 ctiocb->context1 = lpfc_nlp_get(ndlp);
6669f9bb 16528
6669f9bb
JS
16529 ctiocb->iocb_cmpl = NULL;
16530 ctiocb->vport = phba->pport;
546fc854 16531 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 16532 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
16533 ctiocb->sli4_xritag = NO_XRI;
16534
ee0f4fe1
JS
16535 if (fctl & FC_FC_EX_CTX)
16536 /* Exchange responder sent the abort so we
16537 * own the oxid.
16538 */
16539 xri = oxid;
16540 else
16541 xri = rxid;
16542 lxri = lpfc_sli4_xri_inrange(phba, xri);
16543 if (lxri != NO_XRI)
16544 lpfc_set_rrq_active(phba, ndlp, lxri,
16545 (xri == oxid) ? rxid : oxid, 0);
6dd9e31c
JS
16546 /* For BA_ABTS from exchange responder, if the logical xri with
16547 * the oxid maps to the FCP XRI range, the port no longer has
16548 * that exchange context, send a BLS_RJT. Override the IOCB for
16549 * a BA_RJT.
16550 */
16551 if ((fctl & FC_FC_EX_CTX) &&
895427bd 16552 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
6dd9e31c
JS
16553 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16554 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16555 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16556 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16557 }
16558
16559 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16560 * the driver no longer has that exchange, send a BLS_RJT. Override
16561 * the IOCB for a BA_RJT.
546fc854 16562 */
6dd9e31c 16563 if (aborted == false) {
546fc854
JS
16564 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16565 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16566 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16567 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16568 }
6669f9bb 16569
5ffc266e
JS
16570 if (fctl & FC_FC_EX_CTX) {
16571 /* ABTS sent by responder to CT exchange, construction
16572 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16573 * field and RX_ID from ABTS for RX_ID field.
16574 */
546fc854 16575 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
5ffc266e
JS
16576 } else {
16577 /* ABTS sent by initiator to CT exchange, construction
16578 * of BA_ACC will need to allocate a new XRI as for the
f09c3acc 16579 * XRI_TAG field.
5ffc266e 16580 */
546fc854 16581 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
5ffc266e 16582 }
f09c3acc 16583 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
546fc854 16584 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 16585
546fc854 16586 /* Xmit CT abts response on exchange <xid> */
6dd9e31c
JS
16587 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16588 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16589 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
546fc854
JS
16590
16591 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16592 if (rc == IOCB_ERROR) {
6dd9e31c
JS
16593 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16594 "2925 Failed to issue CT ABTS RSP x%x on "
16595 "xri x%x, Data x%x\n",
16596 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16597 phba->link_state);
16598 lpfc_nlp_put(ndlp);
16599 ctiocb->context1 = NULL;
546fc854
JS
16600 lpfc_sli_release_iocbq(phba, ctiocb);
16601 }
6669f9bb
JS
16602}
16603
16604/**
16605 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16606 * @vport: Pointer to the vport on which this sequence was received
16607 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16608 *
16609 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16610 * receive sequence is only partially assembed by the driver, it shall abort
16611 * the partially assembled frames for the sequence. Otherwise, if the
16612 * unsolicited receive sequence has been completely assembled and passed to
16613 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16614 * unsolicited sequence has been aborted. After that, it will issue a basic
16615 * accept to accept the abort.
16616 **/
5d8b8167 16617static void
6669f9bb
JS
16618lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16619 struct hbq_dmabuf *dmabuf)
16620{
16621 struct lpfc_hba *phba = vport->phba;
16622 struct fc_frame_header fc_hdr;
5ffc266e 16623 uint32_t fctl;
6dd9e31c 16624 bool aborted;
6669f9bb 16625
6669f9bb
JS
16626 /* Make a copy of fc_hdr before the dmabuf being released */
16627 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 16628 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 16629
5ffc266e 16630 if (fctl & FC_FC_EX_CTX) {
6dd9e31c
JS
16631 /* ABTS by responder to exchange, no cleanup needed */
16632 aborted = true;
5ffc266e 16633 } else {
6dd9e31c
JS
16634 /* ABTS by initiator to exchange, need to do cleanup */
16635 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16636 if (aborted == false)
16637 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
5ffc266e 16638 }
6dd9e31c
JS
16639 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16640
16641 /* Respond with BA_ACC or BA_RJT accordingly */
16642 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
6669f9bb
JS
16643}
16644
4f774513
JS
16645/**
16646 * lpfc_seq_complete - Indicates if a sequence is complete
16647 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16648 *
16649 * This function checks the sequence, starting with the frame described by
16650 * @dmabuf, to see if all the frames associated with this sequence are present.
16651 * the frames associated with this sequence are linked to the @dmabuf using the
16652 * dbuf list. This function looks for two major things. 1) That the first frame
16653 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16654 * set. 3) That there are no holes in the sequence count. The function will
16655 * return 1 when the sequence is complete, otherwise it will return 0.
16656 **/
16657static int
16658lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16659{
16660 struct fc_frame_header *hdr;
16661 struct lpfc_dmabuf *d_buf;
16662 struct hbq_dmabuf *seq_dmabuf;
16663 uint32_t fctl;
16664 int seq_count = 0;
16665
16666 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16667 /* make sure first fame of sequence has a sequence count of zero */
16668 if (hdr->fh_seq_cnt != seq_count)
16669 return 0;
16670 fctl = (hdr->fh_f_ctl[0] << 16 |
16671 hdr->fh_f_ctl[1] << 8 |
16672 hdr->fh_f_ctl[2]);
16673 /* If last frame of sequence we can return success. */
16674 if (fctl & FC_FC_END_SEQ)
16675 return 1;
16676 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16677 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16678 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16679 /* If there is a hole in the sequence count then fail. */
eeead811 16680 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
16681 return 0;
16682 fctl = (hdr->fh_f_ctl[0] << 16 |
16683 hdr->fh_f_ctl[1] << 8 |
16684 hdr->fh_f_ctl[2]);
16685 /* If last frame of sequence we can return success. */
16686 if (fctl & FC_FC_END_SEQ)
16687 return 1;
16688 }
16689 return 0;
16690}
16691
16692/**
16693 * lpfc_prep_seq - Prep sequence for ULP processing
16694 * @vport: Pointer to the vport on which this sequence was received
16695 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16696 *
16697 * This function takes a sequence, described by a list of frames, and creates
16698 * a list of iocbq structures to describe the sequence. This iocbq list will be
16699 * used to issue to the generic unsolicited sequence handler. This routine
16700 * returns a pointer to the first iocbq in the list. If the function is unable
16701 * to allocate an iocbq then it throw out the received frames that were not
16702 * able to be described and return a pointer to the first iocbq. If unable to
16703 * allocate any iocbqs (including the first) this function will return NULL.
16704 **/
16705static struct lpfc_iocbq *
16706lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16707{
7851fe2c 16708 struct hbq_dmabuf *hbq_buf;
4f774513
JS
16709 struct lpfc_dmabuf *d_buf, *n_buf;
16710 struct lpfc_iocbq *first_iocbq, *iocbq;
16711 struct fc_frame_header *fc_hdr;
16712 uint32_t sid;
7851fe2c 16713 uint32_t len, tot_len;
eeead811 16714 struct ulp_bde64 *pbde;
4f774513
JS
16715
16716 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16717 /* remove from receive buffer list */
16718 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 16719 lpfc_update_rcv_time_stamp(vport);
4f774513 16720 /* get the Remote Port's SID */
6669f9bb 16721 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 16722 tot_len = 0;
4f774513
JS
16723 /* Get an iocbq struct to fill in. */
16724 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16725 if (first_iocbq) {
16726 /* Initialize the first IOCB. */
8fa38513 16727 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513 16728 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
895427bd 16729 first_iocbq->vport = vport;
939723a4
JS
16730
16731 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16732 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16733 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16734 first_iocbq->iocb.un.rcvels.parmRo =
16735 sli4_did_from_fc_hdr(fc_hdr);
16736 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16737 } else
16738 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
16739 first_iocbq->iocb.ulpContext = NO_XRI;
16740 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16741 be16_to_cpu(fc_hdr->fh_ox_id);
16742 /* iocbq is prepped for internal consumption. Physical vpi. */
16743 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16744 vport->phba->vpi_ids[vport->vpi];
4f774513 16745 /* put the first buffer into the first IOCBq */
48a5a664
JS
16746 tot_len = bf_get(lpfc_rcqe_length,
16747 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16748
4f774513
JS
16749 first_iocbq->context2 = &seq_dmabuf->dbuf;
16750 first_iocbq->context3 = NULL;
16751 first_iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
16752 if (tot_len > LPFC_DATA_BUF_SIZE)
16753 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 16754 LPFC_DATA_BUF_SIZE;
48a5a664
JS
16755 else
16756 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16757
4f774513 16758 first_iocbq->iocb.un.rcvels.remoteID = sid;
48a5a664 16759
7851fe2c 16760 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
16761 }
16762 iocbq = first_iocbq;
16763 /*
16764 * Each IOCBq can have two Buffers assigned, so go through the list
16765 * of buffers for this sequence and save two buffers in each IOCBq
16766 */
16767 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16768 if (!iocbq) {
16769 lpfc_in_buf_free(vport->phba, d_buf);
16770 continue;
16771 }
16772 if (!iocbq->context3) {
16773 iocbq->context3 = d_buf;
16774 iocbq->iocb.ulpBdeCount++;
7851fe2c
JS
16775 /* We need to get the size out of the right CQE */
16776 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16777 len = bf_get(lpfc_rcqe_length,
16778 &hbq_buf->cq_event.cqe.rcqe_cmpl);
48a5a664
JS
16779 pbde = (struct ulp_bde64 *)
16780 &iocbq->iocb.unsli3.sli3Words[4];
16781 if (len > LPFC_DATA_BUF_SIZE)
16782 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16783 else
16784 pbde->tus.f.bdeSize = len;
16785
7851fe2c
JS
16786 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16787 tot_len += len;
4f774513
JS
16788 } else {
16789 iocbq = lpfc_sli_get_iocbq(vport->phba);
16790 if (!iocbq) {
16791 if (first_iocbq) {
16792 first_iocbq->iocb.ulpStatus =
16793 IOSTAT_FCP_RSP_ERROR;
16794 first_iocbq->iocb.un.ulpWord[4] =
16795 IOERR_NO_RESOURCES;
16796 }
16797 lpfc_in_buf_free(vport->phba, d_buf);
16798 continue;
16799 }
48a5a664
JS
16800 /* We need to get the size out of the right CQE */
16801 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16802 len = bf_get(lpfc_rcqe_length,
16803 &hbq_buf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
16804 iocbq->context2 = d_buf;
16805 iocbq->context3 = NULL;
16806 iocbq->iocb.ulpBdeCount = 1;
48a5a664
JS
16807 if (len > LPFC_DATA_BUF_SIZE)
16808 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
4f774513 16809 LPFC_DATA_BUF_SIZE;
48a5a664
JS
16810 else
16811 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
7851fe2c 16812
7851fe2c
JS
16813 tot_len += len;
16814 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16815
4f774513
JS
16816 iocbq->iocb.un.rcvels.remoteID = sid;
16817 list_add_tail(&iocbq->list, &first_iocbq->list);
16818 }
16819 }
16820 return first_iocbq;
16821}
16822
6669f9bb
JS
16823static void
16824lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16825 struct hbq_dmabuf *seq_dmabuf)
16826{
16827 struct fc_frame_header *fc_hdr;
16828 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16829 struct lpfc_hba *phba = vport->phba;
16830
16831 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16832 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16833 if (!iocbq) {
16834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16835 "2707 Ring %d handler: Failed to allocate "
16836 "iocb Rctl x%x Type x%x received\n",
16837 LPFC_ELS_RING,
16838 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16839 return;
16840 }
16841 if (!lpfc_complete_unsol_iocb(phba,
895427bd 16842 phba->sli4_hba.els_wq->pring,
6669f9bb
JS
16843 iocbq, fc_hdr->fh_r_ctl,
16844 fc_hdr->fh_type))
6d368e53 16845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
16846 "2540 Ring %d handler: unexpected Rctl "
16847 "x%x Type x%x received\n",
16848 LPFC_ELS_RING,
16849 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16850
16851 /* Free iocb created in lpfc_prep_seq */
16852 list_for_each_entry_safe(curr_iocb, next_iocb,
16853 &iocbq->list, list) {
16854 list_del_init(&curr_iocb->list);
16855 lpfc_sli_release_iocbq(phba, curr_iocb);
16856 }
16857 lpfc_sli_release_iocbq(phba, iocbq);
16858}
16859
4f774513
JS
16860/**
16861 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16862 * @phba: Pointer to HBA context object.
16863 *
16864 * This function is called with no lock held. This function processes all
16865 * the received buffers and gives it to upper layers when a received buffer
16866 * indicates that it is the final frame in the sequence. The interrupt
895427bd 16867 * service routine processes received buffers at interrupt contexts.
4f774513
JS
16868 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16869 * appropriate receive function when the final frame in a sequence is received.
16870 **/
4d9ab994
JS
16871void
16872lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16873 struct hbq_dmabuf *dmabuf)
4f774513 16874{
4d9ab994 16875 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
16876 struct fc_frame_header *fc_hdr;
16877 struct lpfc_vport *vport;
16878 uint32_t fcfi;
939723a4 16879 uint32_t did;
4f774513 16880
4f774513 16881 /* Process each received buffer */
4d9ab994 16882 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
2ea259ee 16883
4d9ab994
JS
16884 /* check to see if this a valid type of frame */
16885 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16886 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16887 return;
16888 }
2ea259ee 16889
7851fe2c
JS
16890 if ((bf_get(lpfc_cqe_code,
16891 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16892 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16893 &dmabuf->cq_event.cqe.rcqe_cmpl);
16894 else
16895 fcfi = bf_get(lpfc_rcqe_fcf_id,
16896 &dmabuf->cq_event.cqe.rcqe_cmpl);
939723a4 16897
895427bd
JS
16898 /* d_id this frame is directed to */
16899 did = sli4_did_from_fc_hdr(fc_hdr);
16900
16901 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
939723a4 16902 if (!vport) {
4d9ab994
JS
16903 /* throw out the frame */
16904 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16905 return;
16906 }
939723a4 16907
939723a4
JS
16908 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16909 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16910 (did != Fabric_DID)) {
16911 /*
16912 * Throw out the frame if we are not pt2pt.
16913 * The pt2pt protocol allows for discovery frames
16914 * to be received without a registered VPI.
16915 */
16916 if (!(vport->fc_flag & FC_PT2PT) ||
16917 (phba->link_state == LPFC_HBA_READY)) {
16918 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16919 return;
16920 }
16921 }
16922
6669f9bb
JS
16923 /* Handle the basic abort sequence (BA_ABTS) event */
16924 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16925 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16926 return;
16927 }
16928
4d9ab994
JS
16929 /* Link this frame */
16930 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16931 if (!seq_dmabuf) {
16932 /* unable to add frame to vport - throw it out */
16933 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16934 return;
16935 }
16936 /* If not last frame in sequence continue processing frames. */
def9c7a9 16937 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 16938 return;
def9c7a9 16939
6669f9bb
JS
16940 /* Send the complete sequence to the upper layer protocol */
16941 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 16942}
6fb120a7
JS
16943
16944/**
16945 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16946 * @phba: pointer to lpfc hba data structure.
16947 *
16948 * This routine is invoked to post rpi header templates to the
16949 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
16950 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16951 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
16952 *
16953 * This routine does not require any locks. It's usage is expected
16954 * to be driver load or reset recovery when the driver is
16955 * sequential.
16956 *
16957 * Return codes
af901ca1 16958 * 0 - successful
d439d286 16959 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
16960 * When this error occurs, the driver is not guaranteed
16961 * to have any rpi regions posted to the device and
16962 * must either attempt to repost the regions or take a
16963 * fatal error.
16964 **/
16965int
16966lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
16967{
16968 struct lpfc_rpi_hdr *rpi_page;
16969 uint32_t rc = 0;
6d368e53
JS
16970 uint16_t lrpi = 0;
16971
16972 /* SLI4 ports that support extents do not require RPI headers. */
16973 if (!phba->sli4_hba.rpi_hdrs_in_use)
16974 goto exit;
16975 if (phba->sli4_hba.extents_in_use)
16976 return -EIO;
6fb120a7 16977
6fb120a7 16978 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
16979 /*
16980 * Assign the rpi headers a physical rpi only if the driver
16981 * has not initialized those resources. A port reset only
16982 * needs the headers posted.
16983 */
16984 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
16985 LPFC_RPI_RSRC_RDY)
16986 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
16987
6fb120a7
JS
16988 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
16989 if (rc != MBX_SUCCESS) {
16990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16991 "2008 Error %d posting all rpi "
16992 "headers\n", rc);
16993 rc = -EIO;
16994 break;
16995 }
16996 }
16997
6d368e53
JS
16998 exit:
16999 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17000 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
17001 return rc;
17002}
17003
17004/**
17005 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17006 * @phba: pointer to lpfc hba data structure.
17007 * @rpi_page: pointer to the rpi memory region.
17008 *
17009 * This routine is invoked to post a single rpi header to the
17010 * HBA consistent with the SLI-4 interface spec. This memory region
17011 * maps up to 64 rpi context regions.
17012 *
17013 * Return codes
af901ca1 17014 * 0 - successful
d439d286
JS
17015 * -ENOMEM - No available memory
17016 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
17017 **/
17018int
17019lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17020{
17021 LPFC_MBOXQ_t *mboxq;
17022 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17023 uint32_t rc = 0;
6fb120a7
JS
17024 uint32_t shdr_status, shdr_add_status;
17025 union lpfc_sli4_cfg_shdr *shdr;
17026
6d368e53
JS
17027 /* SLI4 ports that support extents do not require RPI headers. */
17028 if (!phba->sli4_hba.rpi_hdrs_in_use)
17029 return rc;
17030 if (phba->sli4_hba.extents_in_use)
17031 return -EIO;
17032
6fb120a7
JS
17033 /* The port is notified of the header region via a mailbox command. */
17034 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17035 if (!mboxq) {
17036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17037 "2001 Unable to allocate memory for issuing "
17038 "SLI_CONFIG_SPECIAL mailbox command\n");
17039 return -ENOMEM;
17040 }
17041
17042 /* Post all rpi memory regions to the port. */
17043 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
17044 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17045 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17046 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
17047 sizeof(struct lpfc_sli4_cfg_mhdr),
17048 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
17049
17050
17051 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
17052 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17053 rpi_page->start_rpi);
6d368e53
JS
17054 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17055 hdr_tmpl, rpi_page->page_count);
17056
6fb120a7
JS
17057 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17058 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 17059 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
17060 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17061 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17062 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17063 if (rc != MBX_TIMEOUT)
17064 mempool_free(mboxq, phba->mbox_mem_pool);
17065 if (shdr_status || shdr_add_status || rc) {
17066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17067 "2514 POST_RPI_HDR mailbox failed with "
17068 "status x%x add_status x%x, mbx status x%x\n",
17069 shdr_status, shdr_add_status, rc);
17070 rc = -ENXIO;
17071 }
17072 return rc;
17073}
17074
17075/**
17076 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17077 * @phba: pointer to lpfc hba data structure.
17078 *
17079 * This routine is invoked to post rpi header templates to the
17080 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
17081 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17082 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
17083 *
17084 * Returns
af901ca1 17085 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
17086 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17087 **/
17088int
17089lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17090{
6d368e53
JS
17091 unsigned long rpi;
17092 uint16_t max_rpi, rpi_limit;
17093 uint16_t rpi_remaining, lrpi = 0;
6fb120a7 17094 struct lpfc_rpi_hdr *rpi_hdr;
4902b381 17095 unsigned long iflag;
6fb120a7 17096
6fb120a7 17097 /*
6d368e53
JS
17098 * Fetch the next logical rpi. Because this index is logical,
17099 * the driver starts at 0 each time.
6fb120a7 17100 */
4902b381 17101 spin_lock_irqsave(&phba->hbalock, iflag);
be6bb941
JS
17102 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17103 rpi_limit = phba->sli4_hba.next_rpi;
17104
6d368e53
JS
17105 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17106 if (rpi >= rpi_limit)
6fb120a7
JS
17107 rpi = LPFC_RPI_ALLOC_ERROR;
17108 else {
17109 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17110 phba->sli4_hba.max_cfg_param.rpi_used++;
17111 phba->sli4_hba.rpi_count++;
17112 }
be6bb941
JS
17113 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17114 "0001 rpi:%x max:%x lim:%x\n",
17115 (int) rpi, max_rpi, rpi_limit);
6fb120a7
JS
17116
17117 /*
17118 * Don't try to allocate more rpi header regions if the device limit
6d368e53 17119 * has been exhausted.
6fb120a7
JS
17120 */
17121 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17122 (phba->sli4_hba.rpi_count >= max_rpi)) {
4902b381 17123 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17124 return rpi;
17125 }
17126
6d368e53
JS
17127 /*
17128 * RPI header postings are not required for SLI4 ports capable of
17129 * extents.
17130 */
17131 if (!phba->sli4_hba.rpi_hdrs_in_use) {
4902b381 17132 spin_unlock_irqrestore(&phba->hbalock, iflag);
6d368e53
JS
17133 return rpi;
17134 }
17135
6fb120a7
JS
17136 /*
17137 * If the driver is running low on rpi resources, allocate another
17138 * page now. Note that the next_rpi value is used because
17139 * it represents how many are actually in use whereas max_rpi notes
17140 * how many are supported max by the device.
17141 */
6d368e53 17142 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
4902b381 17143 spin_unlock_irqrestore(&phba->hbalock, iflag);
6fb120a7
JS
17144 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17145 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17146 if (!rpi_hdr) {
17147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17148 "2002 Error Could not grow rpi "
17149 "count\n");
17150 } else {
6d368e53
JS
17151 lrpi = rpi_hdr->start_rpi;
17152 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
17153 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17154 }
17155 }
17156
17157 return rpi;
17158}
17159
d7c47992
JS
17160/**
17161 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17162 * @phba: pointer to lpfc hba data structure.
17163 *
17164 * This routine is invoked to release an rpi to the pool of
17165 * available rpis maintained by the driver.
17166 **/
5d8b8167 17167static void
d7c47992
JS
17168__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17169{
17170 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17171 phba->sli4_hba.rpi_count--;
17172 phba->sli4_hba.max_cfg_param.rpi_used--;
17173 }
17174}
17175
6fb120a7
JS
17176/**
17177 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17178 * @phba: pointer to lpfc hba data structure.
17179 *
17180 * This routine is invoked to release an rpi to the pool of
17181 * available rpis maintained by the driver.
17182 **/
17183void
17184lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17185{
17186 spin_lock_irq(&phba->hbalock);
d7c47992 17187 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
17188 spin_unlock_irq(&phba->hbalock);
17189}
17190
17191/**
17192 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17193 * @phba: pointer to lpfc hba data structure.
17194 *
17195 * This routine is invoked to remove the memory region that
17196 * provided rpi via a bitmask.
17197 **/
17198void
17199lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17200{
17201 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
17202 kfree(phba->sli4_hba.rpi_ids);
17203 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
17204}
17205
17206/**
17207 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17208 * @phba: pointer to lpfc hba data structure.
17209 *
17210 * This routine is invoked to remove the memory region that
17211 * provided rpi via a bitmask.
17212 **/
17213int
6b5151fd
JS
17214lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17215 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
6fb120a7
JS
17216{
17217 LPFC_MBOXQ_t *mboxq;
17218 struct lpfc_hba *phba = ndlp->phba;
17219 int rc;
17220
17221 /* The port is notified of the header region via a mailbox command. */
17222 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17223 if (!mboxq)
17224 return -ENOMEM;
17225
17226 /* Post all rpi memory regions to the port. */
17227 lpfc_resume_rpi(mboxq, ndlp);
6b5151fd
JS
17228 if (cmpl) {
17229 mboxq->mbox_cmpl = cmpl;
17230 mboxq->context1 = arg;
17231 mboxq->context2 = ndlp;
72859909
JS
17232 } else
17233 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6b5151fd 17234 mboxq->vport = ndlp->vport;
6fb120a7
JS
17235 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17236 if (rc == MBX_NOT_FINISHED) {
17237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17238 "2010 Resume RPI Mailbox failed "
17239 "status %d, mbxStatus x%x\n", rc,
17240 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17241 mempool_free(mboxq, phba->mbox_mem_pool);
17242 return -EIO;
17243 }
17244 return 0;
17245}
17246
17247/**
17248 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 17249 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 17250 *
76a95d75 17251 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
17252 *
17253 * Returns:
17254 * 0 success
17255 * -Evalue otherwise
17256 **/
17257int
76a95d75 17258lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
17259{
17260 LPFC_MBOXQ_t *mboxq;
17261 int rc = 0;
6a9c52cf 17262 int retval = MBX_SUCCESS;
6fb120a7 17263 uint32_t mbox_tmo;
76a95d75 17264 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
17265 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17266 if (!mboxq)
17267 return -ENOMEM;
76a95d75 17268 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 17269 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 17270 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 17271 if (rc != MBX_SUCCESS) {
76a95d75 17272 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
17273 "2022 INIT VPI Mailbox failed "
17274 "status %d, mbxStatus x%x\n", rc,
17275 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 17276 retval = -EIO;
6fb120a7 17277 }
6a9c52cf 17278 if (rc != MBX_TIMEOUT)
76a95d75 17279 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
17280
17281 return retval;
6fb120a7
JS
17282}
17283
17284/**
17285 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17286 * @phba: pointer to lpfc hba data structure.
17287 * @mboxq: Pointer to mailbox object.
17288 *
17289 * This routine is invoked to manually add a single FCF record. The caller
17290 * must pass a completely initialized FCF_Record. This routine takes
17291 * care of the nonembedded mailbox operations.
17292 **/
17293static void
17294lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17295{
17296 void *virt_addr;
17297 union lpfc_sli4_cfg_shdr *shdr;
17298 uint32_t shdr_status, shdr_add_status;
17299
17300 virt_addr = mboxq->sge_array->addr[0];
17301 /* The IOCTL status is embedded in the mailbox subheader. */
17302 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17303 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17304 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17305
17306 if ((shdr_status || shdr_add_status) &&
17307 (shdr_status != STATUS_FCF_IN_USE))
17308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17309 "2558 ADD_FCF_RECORD mailbox failed with "
17310 "status x%x add_status x%x\n",
17311 shdr_status, shdr_add_status);
17312
17313 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17314}
17315
17316/**
17317 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17318 * @phba: pointer to lpfc hba data structure.
17319 * @fcf_record: pointer to the initialized fcf record to add.
17320 *
17321 * This routine is invoked to manually add a single FCF record. The caller
17322 * must pass a completely initialized FCF_Record. This routine takes
17323 * care of the nonembedded mailbox operations.
17324 **/
17325int
17326lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17327{
17328 int rc = 0;
17329 LPFC_MBOXQ_t *mboxq;
17330 uint8_t *bytep;
17331 void *virt_addr;
6fb120a7
JS
17332 struct lpfc_mbx_sge sge;
17333 uint32_t alloc_len, req_len;
17334 uint32_t fcfindex;
17335
17336 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17337 if (!mboxq) {
17338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17339 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17340 return -ENOMEM;
17341 }
17342
17343 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17344 sizeof(uint32_t);
17345
17346 /* Allocate DMA memory and set up the non-embedded mailbox command */
17347 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17348 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17349 req_len, LPFC_SLI4_MBX_NEMBED);
17350 if (alloc_len < req_len) {
17351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17352 "2523 Allocated DMA memory size (x%x) is "
17353 "less than the requested DMA memory "
17354 "size (x%x)\n", alloc_len, req_len);
17355 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17356 return -ENOMEM;
17357 }
17358
17359 /*
17360 * Get the first SGE entry from the non-embedded DMA memory. This
17361 * routine only uses a single SGE.
17362 */
17363 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
6fb120a7
JS
17364 virt_addr = mboxq->sge_array->addr[0];
17365 /*
17366 * Configure the FCF record for FCFI 0. This is the driver's
17367 * hardcoded default and gets used in nonFIP mode.
17368 */
17369 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17370 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17371 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17372
17373 /*
17374 * Copy the fcf_index and the FCF Record Data. The data starts after
17375 * the FCoE header plus word10. The data copy needs to be endian
17376 * correct.
17377 */
17378 bytep += sizeof(uint32_t);
17379 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17380 mboxq->vport = phba->pport;
17381 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17382 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17383 if (rc == MBX_NOT_FINISHED) {
17384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17385 "2515 ADD_FCF_RECORD mailbox failed with "
17386 "status 0x%x\n", rc);
17387 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17388 rc = -EIO;
17389 } else
17390 rc = 0;
17391
17392 return rc;
17393}
17394
17395/**
17396 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17397 * @phba: pointer to lpfc hba data structure.
17398 * @fcf_record: pointer to the fcf record to write the default data.
17399 * @fcf_index: FCF table entry index.
17400 *
17401 * This routine is invoked to build the driver's default FCF record. The
17402 * values used are hardcoded. This routine handles memory initialization.
17403 *
17404 **/
17405void
17406lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17407 struct fcf_record *fcf_record,
17408 uint16_t fcf_index)
17409{
17410 memset(fcf_record, 0, sizeof(struct fcf_record));
17411 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17412 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17413 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17414 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17415 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17416 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17417 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17418 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17419 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17420 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17421 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17422 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17423 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 17424 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
17425 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17426 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17427 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17428 /* Set the VLAN bit map */
17429 if (phba->valid_vlan) {
17430 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17431 = 1 << (phba->vlan_id % 8);
17432 }
17433}
17434
17435/**
0c9ab6f5 17436 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
17437 * @phba: pointer to lpfc hba data structure.
17438 * @fcf_index: FCF table entry offset.
17439 *
0c9ab6f5
JS
17440 * This routine is invoked to scan the entire FCF table by reading FCF
17441 * record and processing it one at a time starting from the @fcf_index
17442 * for initial FCF discovery or fast FCF failover rediscovery.
17443 *
25985edc 17444 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 17445 * otherwise.
6fb120a7
JS
17446 **/
17447int
0c9ab6f5 17448lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
17449{
17450 int rc = 0, error;
17451 LPFC_MBOXQ_t *mboxq;
6fb120a7 17452
32b9793f 17453 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
80c17849 17454 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
6fb120a7
JS
17455 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17456 if (!mboxq) {
17457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17458 "2000 Failed to allocate mbox for "
17459 "READ_FCF cmd\n");
4d9ab994 17460 error = -ENOMEM;
0c9ab6f5 17461 goto fail_fcf_scan;
6fb120a7 17462 }
ecfd03c6 17463 /* Construct the read FCF record mailbox command */
0c9ab6f5 17464 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
17465 if (rc) {
17466 error = -EINVAL;
0c9ab6f5 17467 goto fail_fcf_scan;
6fb120a7 17468 }
ecfd03c6 17469 /* Issue the mailbox command asynchronously */
6fb120a7 17470 mboxq->vport = phba->pport;
0c9ab6f5 17471 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
17472
17473 spin_lock_irq(&phba->hbalock);
17474 phba->hba_flag |= FCF_TS_INPROG;
17475 spin_unlock_irq(&phba->hbalock);
17476
6fb120a7 17477 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 17478 if (rc == MBX_NOT_FINISHED)
6fb120a7 17479 error = -EIO;
ecfd03c6 17480 else {
38b92ef8
JS
17481 /* Reset eligible FCF count for new scan */
17482 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 17483 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 17484 error = 0;
32b9793f 17485 }
0c9ab6f5 17486fail_fcf_scan:
4d9ab994
JS
17487 if (error) {
17488 if (mboxq)
17489 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 17490 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 17491 spin_lock_irq(&phba->hbalock);
a93ff37a 17492 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
17493 spin_unlock_irq(&phba->hbalock);
17494 }
6fb120a7
JS
17495 return error;
17496}
a0c87cbd 17497
0c9ab6f5 17498/**
a93ff37a 17499 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
17500 * @phba: pointer to lpfc hba data structure.
17501 * @fcf_index: FCF table entry offset.
17502 *
17503 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 17504 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 17505 *
25985edc 17506 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17507 * otherwise.
17508 **/
17509int
17510lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17511{
17512 int rc = 0, error;
17513 LPFC_MBOXQ_t *mboxq;
17514
17515 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17516 if (!mboxq) {
17517 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17518 "2763 Failed to allocate mbox for "
17519 "READ_FCF cmd\n");
17520 error = -ENOMEM;
17521 goto fail_fcf_read;
17522 }
17523 /* Construct the read FCF record mailbox command */
17524 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17525 if (rc) {
17526 error = -EINVAL;
17527 goto fail_fcf_read;
17528 }
17529 /* Issue the mailbox command asynchronously */
17530 mboxq->vport = phba->pport;
17531 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17532 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17533 if (rc == MBX_NOT_FINISHED)
17534 error = -EIO;
17535 else
17536 error = 0;
17537
17538fail_fcf_read:
17539 if (error && mboxq)
17540 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17541 return error;
17542}
17543
17544/**
17545 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17546 * @phba: pointer to lpfc hba data structure.
17547 * @fcf_index: FCF table entry offset.
17548 *
17549 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 17550 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 17551 *
25985edc 17552 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
17553 * otherwise.
17554 **/
17555int
17556lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17557{
17558 int rc = 0, error;
17559 LPFC_MBOXQ_t *mboxq;
17560
17561 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17562 if (!mboxq) {
17563 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17564 "2758 Failed to allocate mbox for "
17565 "READ_FCF cmd\n");
17566 error = -ENOMEM;
17567 goto fail_fcf_read;
17568 }
17569 /* Construct the read FCF record mailbox command */
17570 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17571 if (rc) {
17572 error = -EINVAL;
17573 goto fail_fcf_read;
17574 }
17575 /* Issue the mailbox command asynchronously */
17576 mboxq->vport = phba->pport;
17577 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17578 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17579 if (rc == MBX_NOT_FINISHED)
17580 error = -EIO;
17581 else
17582 error = 0;
17583
17584fail_fcf_read:
17585 if (error && mboxq)
17586 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17587 return error;
17588}
17589
7d791df7 17590/**
f5cb5304 17591 * lpfc_check_next_fcf_pri_level
7d791df7
JS
17592 * phba pointer to the lpfc_hba struct for this port.
17593 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17594 * routine when the rr_bmask is empty. The FCF indecies are put into the
17595 * rr_bmask based on their priority level. Starting from the highest priority
17596 * to the lowest. The most likely FCF candidate will be in the highest
17597 * priority group. When this routine is called it searches the fcf_pri list for
17598 * next lowest priority group and repopulates the rr_bmask with only those
17599 * fcf_indexes.
17600 * returns:
17601 * 1=success 0=failure
17602 **/
5d8b8167 17603static int
7d791df7
JS
17604lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17605{
17606 uint16_t next_fcf_pri;
17607 uint16_t last_index;
17608 struct lpfc_fcf_pri *fcf_pri;
17609 int rc;
17610 int ret = 0;
17611
17612 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17613 LPFC_SLI4_FCF_TBL_INDX_MAX);
17614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17615 "3060 Last IDX %d\n", last_index);
2562669c
JS
17616
17617 /* Verify the priority list has 2 or more entries */
17618 spin_lock_irq(&phba->hbalock);
17619 if (list_empty(&phba->fcf.fcf_pri_list) ||
17620 list_is_singular(&phba->fcf.fcf_pri_list)) {
17621 spin_unlock_irq(&phba->hbalock);
7d791df7
JS
17622 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17623 "3061 Last IDX %d\n", last_index);
17624 return 0; /* Empty rr list */
17625 }
2562669c
JS
17626 spin_unlock_irq(&phba->hbalock);
17627
7d791df7
JS
17628 next_fcf_pri = 0;
17629 /*
17630 * Clear the rr_bmask and set all of the bits that are at this
17631 * priority.
17632 */
17633 memset(phba->fcf.fcf_rr_bmask, 0,
17634 sizeof(*phba->fcf.fcf_rr_bmask));
17635 spin_lock_irq(&phba->hbalock);
17636 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17637 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17638 continue;
17639 /*
17640 * the 1st priority that has not FLOGI failed
17641 * will be the highest.
17642 */
17643 if (!next_fcf_pri)
17644 next_fcf_pri = fcf_pri->fcf_rec.priority;
17645 spin_unlock_irq(&phba->hbalock);
17646 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17647 rc = lpfc_sli4_fcf_rr_index_set(phba,
17648 fcf_pri->fcf_rec.fcf_index);
17649 if (rc)
17650 return 0;
17651 }
17652 spin_lock_irq(&phba->hbalock);
17653 }
17654 /*
17655 * if next_fcf_pri was not set above and the list is not empty then
17656 * we have failed flogis on all of them. So reset flogi failed
4907cb7b 17657 * and start at the beginning.
7d791df7
JS
17658 */
17659 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17660 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17661 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17662 /*
17663 * the 1st priority that has not FLOGI failed
17664 * will be the highest.
17665 */
17666 if (!next_fcf_pri)
17667 next_fcf_pri = fcf_pri->fcf_rec.priority;
17668 spin_unlock_irq(&phba->hbalock);
17669 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17670 rc = lpfc_sli4_fcf_rr_index_set(phba,
17671 fcf_pri->fcf_rec.fcf_index);
17672 if (rc)
17673 return 0;
17674 }
17675 spin_lock_irq(&phba->hbalock);
17676 }
17677 } else
17678 ret = 1;
17679 spin_unlock_irq(&phba->hbalock);
17680
17681 return ret;
17682}
0c9ab6f5
JS
17683/**
17684 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17685 * @phba: pointer to lpfc hba data structure.
17686 *
17687 * This routine is to get the next eligible FCF record index in a round
17688 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 17689 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
17690 * shall be returned, otherwise, the next eligible FCF record's index
17691 * shall be returned.
17692 **/
17693uint16_t
17694lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17695{
17696 uint16_t next_fcf_index;
17697
421c6622 17698initial_priority:
3804dc84 17699 /* Search start from next bit of currently registered FCF index */
421c6622
JS
17700 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17701
7d791df7 17702next_priority:
421c6622
JS
17703 /* Determine the next fcf index to check */
17704 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
17705 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17706 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
17707 next_fcf_index);
17708
0c9ab6f5 17709 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
17710 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17711 /*
17712 * If we have wrapped then we need to clear the bits that
17713 * have been tested so that we can detect when we should
17714 * change the priority level.
17715 */
0c9ab6f5
JS
17716 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17717 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
17718 }
17719
3804dc84
JS
17720
17721 /* Check roundrobin failover list empty condition */
7d791df7
JS
17722 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17723 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17724 /*
17725 * If next fcf index is not found check if there are lower
17726 * Priority level fcf's in the fcf_priority list.
17727 * Set up the rr_bmask with all of the avaiable fcf bits
17728 * at that level and continue the selection process.
17729 */
17730 if (lpfc_check_next_fcf_pri_level(phba))
421c6622 17731 goto initial_priority;
3804dc84
JS
17732 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17733 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
17734 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17735 return LPFC_FCOE_FCF_NEXT_NONE;
17736 else {
17737 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17738 "3063 Only FCF available idx %d, flag %x\n",
17739 next_fcf_index,
17740 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17741 return next_fcf_index;
17742 }
3804dc84
JS
17743 }
17744
7d791df7
JS
17745 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17746 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
f5cb5304
JS
17747 LPFC_FCF_FLOGI_FAILED) {
17748 if (list_is_singular(&phba->fcf.fcf_pri_list))
17749 return LPFC_FCOE_FCF_NEXT_NONE;
17750
7d791df7 17751 goto next_priority;
f5cb5304 17752 }
7d791df7 17753
3804dc84 17754 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
17755 "2845 Get next roundrobin failover FCF (x%x)\n",
17756 next_fcf_index);
17757
0c9ab6f5
JS
17758 return next_fcf_index;
17759}
17760
17761/**
17762 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17763 * @phba: pointer to lpfc hba data structure.
17764 *
17765 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 17766 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
17767 * does not go beyond the range of the driver allocated bmask dimension
17768 * before setting the bit.
17769 *
17770 * Returns 0 if the index bit successfully set, otherwise, it returns
17771 * -EINVAL.
17772 **/
17773int
17774lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17775{
17776 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17777 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
17778 "2610 FCF (x%x) reached driver's book "
17779 "keeping dimension:x%x\n",
0c9ab6f5
JS
17780 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17781 return -EINVAL;
17782 }
17783 /* Set the eligible FCF record index bmask */
17784 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17785
3804dc84 17786 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 17787 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
17788 "bmask\n", fcf_index);
17789
0c9ab6f5
JS
17790 return 0;
17791}
17792
17793/**
3804dc84 17794 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
17795 * @phba: pointer to lpfc hba data structure.
17796 *
17797 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 17798 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
17799 * does not go beyond the range of the driver allocated bmask dimension
17800 * before clearing the bit.
17801 **/
17802void
17803lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17804{
9a803a74 17805 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
0c9ab6f5
JS
17806 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17807 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
17808 "2762 FCF (x%x) reached driver's book "
17809 "keeping dimension:x%x\n",
0c9ab6f5
JS
17810 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17811 return;
17812 }
17813 /* Clear the eligible FCF record index bmask */
7d791df7 17814 spin_lock_irq(&phba->hbalock);
9a803a74
JS
17815 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17816 list) {
7d791df7
JS
17817 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17818 list_del_init(&fcf_pri->list);
17819 break;
17820 }
17821 }
17822 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 17823 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
17824
17825 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 17826 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 17827 "bmask\n", fcf_index);
0c9ab6f5
JS
17828}
17829
ecfd03c6
JS
17830/**
17831 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17832 * @phba: pointer to lpfc hba data structure.
17833 *
17834 * This routine is the completion routine for the rediscover FCF table mailbox
17835 * command. If the mailbox command returned failure, it will try to stop the
17836 * FCF rediscover wait timer.
17837 **/
5d8b8167 17838static void
ecfd03c6
JS
17839lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17840{
17841 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17842 uint32_t shdr_status, shdr_add_status;
17843
17844 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17845
17846 shdr_status = bf_get(lpfc_mbox_hdr_status,
17847 &redisc_fcf->header.cfg_shdr.response);
17848 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17849 &redisc_fcf->header.cfg_shdr.response);
17850 if (shdr_status || shdr_add_status) {
0c9ab6f5 17851 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
17852 "2746 Requesting for FCF rediscovery failed "
17853 "status x%x add_status x%x\n",
17854 shdr_status, shdr_add_status);
0c9ab6f5 17855 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 17856 spin_lock_irq(&phba->hbalock);
0c9ab6f5 17857 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
17858 spin_unlock_irq(&phba->hbalock);
17859 /*
17860 * CVL event triggered FCF rediscover request failed,
17861 * last resort to re-try current registered FCF entry.
17862 */
17863 lpfc_retry_pport_discovery(phba);
17864 } else {
17865 spin_lock_irq(&phba->hbalock);
0c9ab6f5 17866 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
17867 spin_unlock_irq(&phba->hbalock);
17868 /*
17869 * DEAD FCF event triggered FCF rediscover request
17870 * failed, last resort to fail over as a link down
17871 * to FCF registration.
17872 */
17873 lpfc_sli4_fcf_dead_failthrough(phba);
17874 }
0c9ab6f5
JS
17875 } else {
17876 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 17877 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
17878 /*
17879 * Start FCF rediscovery wait timer for pending FCF
17880 * before rescan FCF record table.
17881 */
17882 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 17883 }
ecfd03c6
JS
17884
17885 mempool_free(mbox, phba->mbox_mem_pool);
17886}
17887
17888/**
3804dc84 17889 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
17890 * @phba: pointer to lpfc hba data structure.
17891 *
17892 * This routine is invoked to request for rediscovery of the entire FCF table
17893 * by the port.
17894 **/
17895int
17896lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17897{
17898 LPFC_MBOXQ_t *mbox;
17899 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17900 int rc, length;
17901
0c9ab6f5
JS
17902 /* Cancel retry delay timers to all vports before FCF rediscover */
17903 lpfc_cancel_all_vport_retry_delay_timer(phba);
17904
ecfd03c6
JS
17905 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17906 if (!mbox) {
17907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17908 "2745 Failed to allocate mbox for "
17909 "requesting FCF rediscover.\n");
17910 return -ENOMEM;
17911 }
17912
17913 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17914 sizeof(struct lpfc_sli4_cfg_mhdr));
17915 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17916 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17917 length, LPFC_SLI4_MBX_EMBED);
17918
17919 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17920 /* Set count to 0 for invalidating the entire FCF database */
17921 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17922
17923 /* Issue the mailbox command asynchronously */
17924 mbox->vport = phba->pport;
17925 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17926 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17927
17928 if (rc == MBX_NOT_FINISHED) {
17929 mempool_free(mbox, phba->mbox_mem_pool);
17930 return -EIO;
17931 }
17932 return 0;
17933}
17934
fc2b989b
JS
17935/**
17936 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17937 * @phba: pointer to lpfc hba data structure.
17938 *
17939 * This function is the failover routine as a last resort to the FCF DEAD
17940 * event when driver failed to perform fast FCF failover.
17941 **/
17942void
17943lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17944{
17945 uint32_t link_state;
17946
17947 /*
17948 * Last resort as FCF DEAD event failover will treat this as
17949 * a link down, but save the link state because we don't want
17950 * it to be changed to Link Down unless it is already down.
17951 */
17952 link_state = phba->link_state;
17953 lpfc_linkdown(phba);
17954 phba->link_state = link_state;
17955
17956 /* Unregister FCF if no devices connected to it */
17957 lpfc_unregister_unused_fcf(phba);
17958}
17959
a0c87cbd 17960/**
026abb87 17961 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 17962 * @phba: pointer to lpfc hba data structure.
026abb87 17963 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 17964 *
026abb87
JS
17965 * This function gets SLI3 port configure region 23 data through memory dump
17966 * mailbox command. When it successfully retrieves data, the size of the data
17967 * will be returned, otherwise, 0 will be returned.
a0c87cbd 17968 **/
026abb87
JS
17969static uint32_t
17970lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
17971{
17972 LPFC_MBOXQ_t *pmb = NULL;
17973 MAILBOX_t *mb;
026abb87 17974 uint32_t offset = 0;
a0c87cbd
JS
17975 int rc;
17976
026abb87
JS
17977 if (!rgn23_data)
17978 return 0;
17979
a0c87cbd
JS
17980 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17981 if (!pmb) {
17982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
17983 "2600 failed to allocate mailbox memory\n");
17984 return 0;
a0c87cbd
JS
17985 }
17986 mb = &pmb->u.mb;
17987
a0c87cbd
JS
17988 do {
17989 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
17990 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
17991
17992 if (rc != MBX_SUCCESS) {
17993 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
17994 "2601 failed to read config "
17995 "region 23, rc 0x%x Status 0x%x\n",
17996 rc, mb->mbxStatus);
a0c87cbd
JS
17997 mb->un.varDmp.word_cnt = 0;
17998 }
17999 /*
18000 * dump mem may return a zero when finished or we got a
18001 * mailbox error, either way we are done.
18002 */
18003 if (mb->un.varDmp.word_cnt == 0)
18004 break;
18005 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18006 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18007
18008 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
18009 rgn23_data + offset,
18010 mb->un.varDmp.word_cnt);
a0c87cbd
JS
18011 offset += mb->un.varDmp.word_cnt;
18012 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18013
026abb87
JS
18014 mempool_free(pmb, phba->mbox_mem_pool);
18015 return offset;
18016}
18017
18018/**
18019 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18020 * @phba: pointer to lpfc hba data structure.
18021 * @rgn23_data: pointer to configure region 23 data.
18022 *
18023 * This function gets SLI4 port configure region 23 data through memory dump
18024 * mailbox command. When it successfully retrieves data, the size of the data
18025 * will be returned, otherwise, 0 will be returned.
18026 **/
18027static uint32_t
18028lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18029{
18030 LPFC_MBOXQ_t *mboxq = NULL;
18031 struct lpfc_dmabuf *mp = NULL;
18032 struct lpfc_mqe *mqe;
18033 uint32_t data_length = 0;
18034 int rc;
18035
18036 if (!rgn23_data)
18037 return 0;
18038
18039 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18040 if (!mboxq) {
18041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18042 "3105 failed to allocate mailbox memory\n");
18043 return 0;
18044 }
18045
18046 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18047 goto out;
18048 mqe = &mboxq->u.mqe;
18049 mp = (struct lpfc_dmabuf *) mboxq->context1;
18050 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18051 if (rc)
18052 goto out;
18053 data_length = mqe->un.mb_words[5];
18054 if (data_length == 0)
18055 goto out;
18056 if (data_length > DMP_RGN23_SIZE) {
18057 data_length = 0;
18058 goto out;
18059 }
18060 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18061out:
18062 mempool_free(mboxq, phba->mbox_mem_pool);
18063 if (mp) {
18064 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18065 kfree(mp);
18066 }
18067 return data_length;
18068}
18069
18070/**
18071 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18072 * @phba: pointer to lpfc hba data structure.
18073 *
18074 * This function read region 23 and parse TLV for port status to
18075 * decide if the user disaled the port. If the TLV indicates the
18076 * port is disabled, the hba_flag is set accordingly.
18077 **/
18078void
18079lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18080{
18081 uint8_t *rgn23_data = NULL;
18082 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18083 uint32_t offset = 0;
18084
18085 /* Get adapter Region 23 data */
18086 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18087 if (!rgn23_data)
18088 goto out;
18089
18090 if (phba->sli_rev < LPFC_SLI_REV4)
18091 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18092 else {
18093 if_type = bf_get(lpfc_sli_intf_if_type,
18094 &phba->sli4_hba.sli_intf);
18095 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18096 goto out;
18097 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18098 }
a0c87cbd
JS
18099
18100 if (!data_size)
18101 goto out;
18102
18103 /* Check the region signature first */
18104 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18106 "2619 Config region 23 has bad signature\n");
18107 goto out;
18108 }
18109 offset += 4;
18110
18111 /* Check the data structure version */
18112 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18113 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18114 "2620 Config region 23 has bad version\n");
18115 goto out;
18116 }
18117 offset += 4;
18118
18119 /* Parse TLV entries in the region */
18120 while (offset < data_size) {
18121 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18122 break;
18123 /*
18124 * If the TLV is not driver specific TLV or driver id is
18125 * not linux driver id, skip the record.
18126 */
18127 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18128 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18129 (rgn23_data[offset + 3] != 0)) {
18130 offset += rgn23_data[offset + 1] * 4 + 4;
18131 continue;
18132 }
18133
18134 /* Driver found a driver specific TLV in the config region */
18135 sub_tlv_len = rgn23_data[offset + 1] * 4;
18136 offset += 4;
18137 tlv_offset = 0;
18138
18139 /*
18140 * Search for configured port state sub-TLV.
18141 */
18142 while ((offset < data_size) &&
18143 (tlv_offset < sub_tlv_len)) {
18144 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18145 offset += 4;
18146 tlv_offset += 4;
18147 break;
18148 }
18149 if (rgn23_data[offset] != PORT_STE_TYPE) {
18150 offset += rgn23_data[offset + 1] * 4 + 4;
18151 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18152 continue;
18153 }
18154
18155 /* This HBA contains PORT_STE configured */
18156 if (!rgn23_data[offset + 2])
18157 phba->hba_flag |= LINK_DISABLED;
18158
18159 goto out;
18160 }
18161 }
026abb87 18162
a0c87cbd 18163out:
a0c87cbd
JS
18164 kfree(rgn23_data);
18165 return;
18166}
695a814e 18167
52d52440
JS
18168/**
18169 * lpfc_wr_object - write an object to the firmware
18170 * @phba: HBA structure that indicates port to create a queue on.
18171 * @dmabuf_list: list of dmabufs to write to the port.
18172 * @size: the total byte value of the objects to write to the port.
18173 * @offset: the current offset to be used to start the transfer.
18174 *
18175 * This routine will create a wr_object mailbox command to send to the port.
18176 * the mailbox command will be constructed using the dma buffers described in
18177 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18178 * BDEs that the imbedded mailbox can support. The @offset variable will be
18179 * used to indicate the starting offset of the transfer and will also return
18180 * the offset after the write object mailbox has completed. @size is used to
18181 * determine the end of the object and whether the eof bit should be set.
18182 *
18183 * Return 0 is successful and offset will contain the the new offset to use
18184 * for the next write.
18185 * Return negative value for error cases.
18186 **/
18187int
18188lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18189 uint32_t size, uint32_t *offset)
18190{
18191 struct lpfc_mbx_wr_object *wr_object;
18192 LPFC_MBOXQ_t *mbox;
18193 int rc = 0, i = 0;
18194 uint32_t shdr_status, shdr_add_status;
18195 uint32_t mbox_tmo;
18196 union lpfc_sli4_cfg_shdr *shdr;
18197 struct lpfc_dmabuf *dmabuf;
18198 uint32_t written = 0;
18199
18200 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18201 if (!mbox)
18202 return -ENOMEM;
18203
18204 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18205 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18206 sizeof(struct lpfc_mbx_wr_object) -
18207 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18208
18209 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18210 wr_object->u.request.write_offset = *offset;
18211 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18212 wr_object->u.request.object_name[0] =
18213 cpu_to_le32(wr_object->u.request.object_name[0]);
18214 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18215 list_for_each_entry(dmabuf, dmabuf_list, list) {
18216 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18217 break;
18218 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18219 wr_object->u.request.bde[i].addrHigh =
18220 putPaddrHigh(dmabuf->phys);
18221 if (written + SLI4_PAGE_SIZE >= size) {
18222 wr_object->u.request.bde[i].tus.f.bdeSize =
18223 (size - written);
18224 written += (size - written);
18225 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18226 } else {
18227 wr_object->u.request.bde[i].tus.f.bdeSize =
18228 SLI4_PAGE_SIZE;
18229 written += SLI4_PAGE_SIZE;
18230 }
18231 i++;
18232 }
18233 wr_object->u.request.bde_count = i;
18234 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18235 if (!phba->sli4_hba.intr_enable)
18236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18237 else {
a183a15f 18238 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
18239 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18240 }
18241 /* The IOCTL status is embedded in the mailbox subheader. */
18242 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18243 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18244 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18245 if (rc != MBX_TIMEOUT)
18246 mempool_free(mbox, phba->mbox_mem_pool);
18247 if (shdr_status || shdr_add_status || rc) {
18248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18249 "3025 Write Object mailbox failed with "
18250 "status x%x add_status x%x, mbx status x%x\n",
18251 shdr_status, shdr_add_status, rc);
18252 rc = -ENXIO;
18253 } else
18254 *offset += wr_object->u.response.actual_write_length;
18255 return rc;
18256}
18257
695a814e
JS
18258/**
18259 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18260 * @vport: pointer to vport data structure.
18261 *
18262 * This function iterate through the mailboxq and clean up all REG_LOGIN
18263 * and REG_VPI mailbox commands associated with the vport. This function
18264 * is called when driver want to restart discovery of the vport due to
18265 * a Clear Virtual Link event.
18266 **/
18267void
18268lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18269{
18270 struct lpfc_hba *phba = vport->phba;
18271 LPFC_MBOXQ_t *mb, *nextmb;
18272 struct lpfc_dmabuf *mp;
78730cfe 18273 struct lpfc_nodelist *ndlp;
d439d286 18274 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 18275 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 18276 LIST_HEAD(mbox_cmd_list);
63e801ce 18277 uint8_t restart_loop;
695a814e 18278
d439d286 18279 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
18280 spin_lock_irq(&phba->hbalock);
18281 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18282 if (mb->vport != vport)
18283 continue;
18284
18285 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18286 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18287 continue;
18288
d439d286
JS
18289 list_del(&mb->list);
18290 list_add_tail(&mb->list, &mbox_cmd_list);
18291 }
18292 /* Clean up active mailbox command with the vport */
18293 mb = phba->sli.mbox_active;
18294 if (mb && (mb->vport == vport)) {
18295 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18296 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18297 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18298 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18299 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18300 /* Put reference count for delayed processing */
18301 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18302 /* Unregister the RPI when mailbox complete */
18303 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18304 }
18305 }
63e801ce
JS
18306 /* Cleanup any mailbox completions which are not yet processed */
18307 do {
18308 restart_loop = 0;
18309 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18310 /*
18311 * If this mailox is already processed or it is
18312 * for another vport ignore it.
18313 */
18314 if ((mb->vport != vport) ||
18315 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18316 continue;
18317
18318 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18319 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18320 continue;
18321
18322 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18323 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18324 ndlp = (struct lpfc_nodelist *)mb->context2;
18325 /* Unregister the RPI when mailbox complete */
18326 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18327 restart_loop = 1;
18328 spin_unlock_irq(&phba->hbalock);
18329 spin_lock(shost->host_lock);
18330 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18331 spin_unlock(shost->host_lock);
18332 spin_lock_irq(&phba->hbalock);
18333 break;
18334 }
18335 }
18336 } while (restart_loop);
18337
d439d286
JS
18338 spin_unlock_irq(&phba->hbalock);
18339
18340 /* Release the cleaned-up mailbox commands */
18341 while (!list_empty(&mbox_cmd_list)) {
18342 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
18343 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18344 mp = (struct lpfc_dmabuf *) (mb->context1);
18345 if (mp) {
18346 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18347 kfree(mp);
18348 }
78730cfe 18349 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 18350 mb->context2 = NULL;
78730cfe 18351 if (ndlp) {
ec21b3b0 18352 spin_lock(shost->host_lock);
589a52d6 18353 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 18354 spin_unlock(shost->host_lock);
78730cfe 18355 lpfc_nlp_put(ndlp);
78730cfe 18356 }
695a814e 18357 }
695a814e
JS
18358 mempool_free(mb, phba->mbox_mem_pool);
18359 }
d439d286
JS
18360
18361 /* Release the ndlp with the cleaned-up active mailbox command */
18362 if (act_mbx_ndlp) {
18363 spin_lock(shost->host_lock);
18364 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18365 spin_unlock(shost->host_lock);
18366 lpfc_nlp_put(act_mbx_ndlp);
695a814e 18367 }
695a814e
JS
18368}
18369
2a9bf3d0
JS
18370/**
18371 * lpfc_drain_txq - Drain the txq
18372 * @phba: Pointer to HBA context object.
18373 *
18374 * This function attempt to submit IOCBs on the txq
18375 * to the adapter. For SLI4 adapters, the txq contains
18376 * ELS IOCBs that have been deferred because the there
18377 * are no SGLs. This congestion can occur with large
18378 * vport counts during node discovery.
18379 **/
18380
18381uint32_t
18382lpfc_drain_txq(struct lpfc_hba *phba)
18383{
18384 LIST_HEAD(completions);
895427bd 18385 struct lpfc_sli_ring *pring;
2e706377 18386 struct lpfc_iocbq *piocbq = NULL;
2a9bf3d0
JS
18387 unsigned long iflags = 0;
18388 char *fail_msg = NULL;
18389 struct lpfc_sglq *sglq;
2f07784f
JS
18390 union lpfc_wqe128 wqe128;
18391 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
a2fc4aef 18392 uint32_t txq_cnt = 0;
2a9bf3d0 18393
895427bd
JS
18394 pring = lpfc_phba_elsring(phba);
18395
398d81c9 18396 spin_lock_irqsave(&pring->ring_lock, iflags);
0e9bb8d7
JS
18397 list_for_each_entry(piocbq, &pring->txq, list) {
18398 txq_cnt++;
18399 }
18400
18401 if (txq_cnt > pring->txq_max)
18402 pring->txq_max = txq_cnt;
2a9bf3d0 18403
398d81c9 18404 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18405
0e9bb8d7 18406 while (!list_empty(&pring->txq)) {
398d81c9 18407 spin_lock_irqsave(&pring->ring_lock, iflags);
2a9bf3d0 18408
19ca7609 18409 piocbq = lpfc_sli_ringtx_get(phba, pring);
a629852a 18410 if (!piocbq) {
398d81c9 18411 spin_unlock_irqrestore(&pring->ring_lock, iflags);
a629852a
JS
18412 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18413 "2823 txq empty and txq_cnt is %d\n ",
0e9bb8d7 18414 txq_cnt);
a629852a
JS
18415 break;
18416 }
895427bd 18417 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
2a9bf3d0 18418 if (!sglq) {
19ca7609 18419 __lpfc_sli_ringtx_put(phba, pring, piocbq);
398d81c9 18420 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0 18421 break;
2a9bf3d0 18422 }
0e9bb8d7 18423 txq_cnt--;
2a9bf3d0
JS
18424
18425 /* The xri and iocb resources secured,
18426 * attempt to issue request
18427 */
6d368e53 18428 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
18429 piocbq->sli4_xritag = sglq->sli4_xritag;
18430 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18431 fail_msg = "to convert bpl to sgl";
2f07784f 18432 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
2a9bf3d0 18433 fail_msg = "to convert iocb to wqe";
2f07784f 18434 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
2a9bf3d0
JS
18435 fail_msg = " - Wq is full";
18436 else
18437 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18438
18439 if (fail_msg) {
18440 /* Failed means we can't issue and need to cancel */
18441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18442 "2822 IOCB failed %s iotag 0x%x "
18443 "xri 0x%x\n",
18444 fail_msg,
18445 piocbq->iotag, piocbq->sli4_xritag);
18446 list_add_tail(&piocbq->list, &completions);
18447 }
398d81c9 18448 spin_unlock_irqrestore(&pring->ring_lock, iflags);
2a9bf3d0
JS
18449 }
18450
2a9bf3d0
JS
18451 /* Cancel all the IOCBs that cannot be issued */
18452 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18453 IOERR_SLI_ABORTED);
18454
0e9bb8d7 18455 return txq_cnt;
2a9bf3d0 18456}
895427bd
JS
18457
18458/**
18459 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18460 * @phba: Pointer to HBA context object.
18461 * @pwqe: Pointer to command WQE.
18462 * @sglq: Pointer to the scatter gather queue object.
18463 *
18464 * This routine converts the bpl or bde that is in the WQE
18465 * to a sgl list for the sli4 hardware. The physical address
18466 * of the bpl/bde is converted back to a virtual address.
18467 * If the WQE contains a BPL then the list of BDE's is
18468 * converted to sli4_sge's. If the WQE contains a single
18469 * BDE then it is converted to a single sli_sge.
18470 * The WQE is still in cpu endianness so the contents of
18471 * the bpl can be used without byte swapping.
18472 *
18473 * Returns valid XRI = Success, NO_XRI = Failure.
18474 */
18475static uint16_t
18476lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18477 struct lpfc_sglq *sglq)
18478{
18479 uint16_t xritag = NO_XRI;
18480 struct ulp_bde64 *bpl = NULL;
18481 struct ulp_bde64 bde;
18482 struct sli4_sge *sgl = NULL;
18483 struct lpfc_dmabuf *dmabuf;
18484 union lpfc_wqe *wqe;
18485 int numBdes = 0;
18486 int i = 0;
18487 uint32_t offset = 0; /* accumulated offset in the sg request list */
18488 int inbound = 0; /* number of sg reply entries inbound from firmware */
18489 uint32_t cmd;
18490
18491 if (!pwqeq || !sglq)
18492 return xritag;
18493
18494 sgl = (struct sli4_sge *)sglq->sgl;
18495 wqe = &pwqeq->wqe;
18496 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18497
18498 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18499 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18500 return sglq->sli4_xritag;
18501 numBdes = pwqeq->rsvd2;
18502 if (numBdes) {
18503 /* The addrHigh and addrLow fields within the WQE
18504 * have not been byteswapped yet so there is no
18505 * need to swap them back.
18506 */
18507 if (pwqeq->context3)
18508 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18509 else
18510 return xritag;
18511
18512 bpl = (struct ulp_bde64 *)dmabuf->virt;
18513 if (!bpl)
18514 return xritag;
18515
18516 for (i = 0; i < numBdes; i++) {
18517 /* Should already be byte swapped. */
18518 sgl->addr_hi = bpl->addrHigh;
18519 sgl->addr_lo = bpl->addrLow;
18520
18521 sgl->word2 = le32_to_cpu(sgl->word2);
18522 if ((i+1) == numBdes)
18523 bf_set(lpfc_sli4_sge_last, sgl, 1);
18524 else
18525 bf_set(lpfc_sli4_sge_last, sgl, 0);
18526 /* swap the size field back to the cpu so we
18527 * can assign it to the sgl.
18528 */
18529 bde.tus.w = le32_to_cpu(bpl->tus.w);
18530 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18531 /* The offsets in the sgl need to be accumulated
18532 * separately for the request and reply lists.
18533 * The request is always first, the reply follows.
18534 */
18535 switch (cmd) {
18536 case CMD_GEN_REQUEST64_WQE:
18537 /* add up the reply sg entries */
18538 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18539 inbound++;
18540 /* first inbound? reset the offset */
18541 if (inbound == 1)
18542 offset = 0;
18543 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18544 bf_set(lpfc_sli4_sge_type, sgl,
18545 LPFC_SGE_TYPE_DATA);
18546 offset += bde.tus.f.bdeSize;
18547 break;
18548 case CMD_FCP_TRSP64_WQE:
18549 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18550 bf_set(lpfc_sli4_sge_type, sgl,
18551 LPFC_SGE_TYPE_DATA);
18552 break;
18553 case CMD_FCP_TSEND64_WQE:
18554 case CMD_FCP_TRECEIVE64_WQE:
18555 bf_set(lpfc_sli4_sge_type, sgl,
18556 bpl->tus.f.bdeFlags);
18557 if (i < 3)
18558 offset = 0;
18559 else
18560 offset += bde.tus.f.bdeSize;
18561 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18562 break;
18563 }
18564 sgl->word2 = cpu_to_le32(sgl->word2);
18565 bpl++;
18566 sgl++;
18567 }
18568 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18569 /* The addrHigh and addrLow fields of the BDE have not
18570 * been byteswapped yet so they need to be swapped
18571 * before putting them in the sgl.
18572 */
18573 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18574 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18575 sgl->word2 = le32_to_cpu(sgl->word2);
18576 bf_set(lpfc_sli4_sge_last, sgl, 1);
18577 sgl->word2 = cpu_to_le32(sgl->word2);
18578 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18579 }
18580 return sglq->sli4_xritag;
18581}
18582
18583/**
18584 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18585 * @phba: Pointer to HBA context object.
18586 * @ring_number: Base sli ring number
18587 * @pwqe: Pointer to command WQE.
18588 **/
18589int
18590lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18591 struct lpfc_iocbq *pwqe)
18592{
18593 union lpfc_wqe *wqe = &pwqe->wqe;
f358dd0c 18594 struct lpfc_nvmet_rcv_ctx *ctxp;
895427bd
JS
18595 struct lpfc_queue *wq;
18596 struct lpfc_sglq *sglq;
18597 struct lpfc_sli_ring *pring;
18598 unsigned long iflags;
18599
18600 /* NVME_LS and NVME_LS ABTS requests. */
18601 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18602 pring = phba->sli4_hba.nvmels_wq->pring;
18603 spin_lock_irqsave(&pring->ring_lock, iflags);
18604 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18605 if (!sglq) {
18606 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18607 return WQE_BUSY;
18608 }
18609 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18610 pwqe->sli4_xritag = sglq->sli4_xritag;
18611 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18612 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18613 return WQE_ERROR;
18614 }
18615 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18616 pwqe->sli4_xritag);
18617 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18618 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18619 return WQE_ERROR;
18620 }
18621 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18622 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18623 return 0;
18624 }
18625
18626 /* NVME_FCREQ and NVME_ABTS requests */
18627 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18628 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18629 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18630
18631 spin_lock_irqsave(&pring->ring_lock, iflags);
18632 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18633 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18634 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18635 if (lpfc_sli4_wq_put(wq, wqe)) {
18636 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18637 return WQE_ERROR;
18638 }
18639 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18640 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18641 return 0;
18642 }
18643
f358dd0c
JS
18644 /* NVMET requests */
18645 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18646 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18647 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18648
18649 spin_lock_irqsave(&pring->ring_lock, iflags);
18650 ctxp = pwqe->context2;
18651 sglq = ctxp->rqb_buffer->sglq;
18652 if (pwqe->sli4_xritag == NO_XRI) {
18653 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18654 pwqe->sli4_xritag = sglq->sli4_xritag;
18655 }
18656 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18657 pwqe->sli4_xritag);
18658 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18659 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18660 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18661 if (lpfc_sli4_wq_put(wq, wqe)) {
18662 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18663 return WQE_ERROR;
18664 }
18665 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18666 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18667 return 0;
18668 }
895427bd
JS
18669 return WQE_ERROR;
18670}